test_ModuleHTTP.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <gtest/gtest.h>
  2. #include <json.hpp>
  3. #include <httplib.h>
  4. //#define private public
  5. #include <Registration.h>
  6. #include <math.h>
  7. #include <thread>
  8. using namespace mdd;
  9. using namespace httplib;
  10. namespace TEST_MODULE_HTTP{
  11. void serverThread()
  12. {
  13. Server svr;
  14. json input;
  15. json output;
  16. std::string status = "ready";
  17. for (int i = 0; i < 5; ++i) {
  18. json entity;
  19. entity["type"] = "INPUT";
  20. entity["value"] = { i };
  21. input.push_back(entity);
  22. }
  23. for (int i = 0; i < 3; ++i) {
  24. json entity;
  25. entity["type"] = "OUTPUT";
  26. entity["value"] = { i * 2 };
  27. output.push_back(entity);
  28. }
  29. svr.Get("/inputs", [&](const Request& req, Response& res) {
  30. res.set_content(input.dump(), "application/json");
  31. });
  32. svr.Get("/outputs", [&](const Request& req, Response& res) {
  33. res.set_content(output.dump(), "application/json");
  34. });
  35. svr.Get("/status", [&](const Request& req, Response& res) {
  36. try{
  37. json body;
  38. body["status"] = status;
  39. std::cout << status << std::endl;
  40. res.set_content(body.dump(), "application/json");
  41. }
  42. catch(std::exception){
  43. std::cout << "Exeption caught"<<std::endl;
  44. }
  45. });
  46. svr.Post("/update",
  47. [&](const Request& req, Response& res, const ContentReader &content_reader) {
  48. status = "updating...";
  49. std::string body;
  50. content_reader([&](const char *data, size_t data_length) {
  51. body.append(data, data_length);
  52. return true;
  53. });
  54. body = std::string(R"()") + body;
  55. json inputs = json::parse(body.c_str());
  56. for (int i = 0; i < inputs.size(); ++i) {
  57. input[i]["value"] = inputs[i]["value"];
  58. }
  59. output[0]["value"][0] = input[0]["value"][0].get<int>() + input[1]["value"][0].get<int>();
  60. output[1]["value"][0] = input[2]["value"][0].get<int>() * input[2]["value"][0].get<int>();
  61. output[2]["value"][0] = input[3]["value"][0].get<int>() - input[4]["value"][0].get<int>();
  62. //std::cout << output[0].dump() << std::endl;
  63. //std::cout << output[1].dump() << std::endl;
  64. //std::cout << output[2].dump() << std::endl;
  65. res.set_content(output.dump(), "application/json");
  66. status = "ready";
  67. }
  68. );
  69. svr.Get("/stop", [&](const Request& req, Response& res) {
  70. std::cout << "server stoped"<<std::endl;
  71. svr.stop();
  72. });
  73. svr.listen("localhost", 8888);
  74. std::cout << "server closed"<<std::endl;
  75. }
  76. auto regi = Registration();
  77. TEST(ModuleHTTP, updateLayout_intern){
  78. std::thread server (serverThread);
  79. auto mod = regi.generateModule("ModuleHTTP");// ("", "localhost", 8888);
  80. json config = json::parse(mod->getConfiguration());
  81. config[0]["value"] = "";
  82. config[1]["value"] = "localhost";
  83. config[2]["value"] = 8888;
  84. mod->configure(config.dump());
  85. Client cli("localhost",8888);
  86. cli.Get("/stop");
  87. server.join();
  88. for (int i = 0; i < mod->getNumInputs(); ++i) {
  89. EXPECT_EQ(mod->getInput(i)->getType(), "INPUT");
  90. EXPECT_EQ((int)mod->getInput(i)->getValue()[0], i);
  91. }
  92. for (int i = 0; i < mod->getNumOutputs(); ++i) {
  93. EXPECT_EQ(mod->getOutput(i)->getType(), "OUTPUT");
  94. EXPECT_EQ((int)mod->getOutput(i)->getValue()[0], i * 2);
  95. }
  96. mod->disconnect();
  97. }
  98. TEST(ModuleHTTP, updateLayout_extern){
  99. auto mod = regi.generateModule("ModuleHTTP");// ("", "localhost", 8888);
  100. json config = json::parse(mod->getConfiguration());
  101. config[0]["value"] = "../../../lib/test/server/server.py";
  102. config[1]["value"] = "localhost";
  103. config[2]["value"] = 8889;
  104. mod->configure(config.dump());
  105. for (int i = 0; i < mod->getNumInputs(); ++i) {
  106. EXPECT_EQ(mod->getInput(i)->getType(), "INPUT");
  107. EXPECT_EQ((int)mod->getInput(i)->getValue()[0], i);
  108. }
  109. for (int i = 0; i < mod->getNumOutputs(); ++i) {
  110. EXPECT_EQ(mod->getOutput(i)->getType(), "OUTPUT");
  111. EXPECT_EQ((int)mod->getOutput(i)->getValue()[0], i * 2);
  112. }
  113. Client cli("localhost",8889);
  114. cli.Get("/stop");
  115. mod->disconnect();
  116. }
  117. TEST(ModuleHTTP, update_intern){
  118. std::thread server (serverThread);
  119. auto mod = regi.generateModule("ModuleHTTP");// ("", "localhost", 8888);
  120. json config = json::parse(mod->getConfiguration());
  121. config[0]["value"] = "";
  122. config[1]["value"] = "localhost";
  123. config[2]["value"] = 8888;
  124. mod->configure(config.dump());
  125. for (int i = 0; i < mod->getNumInputs(); ++i) {
  126. mod->getInput(i)->setValue() = { (double)(10 - i) };
  127. }
  128. mod->update();
  129. Client cli("localhost", 8888);
  130. cli.Get("/stop");
  131. server.join();
  132. for (int i = 0; i < mod->getNumInputs(); ++i) {
  133. EXPECT_EQ(mod->getInput(i)->getType(), "INPUT");
  134. EXPECT_EQ(mod->getInput(i)->getValue()[0], 10-i);
  135. }
  136. //std::cout << "WORKED" << std::endl;
  137. //std::cout << module.getOutput(outputs_ids[0])->getValue() << std::endl;
  138. EXPECT_EQ((int)mod->getOutput(0)->getValue()[0], 19);
  139. EXPECT_EQ((int)mod->getOutput(1)->getValue()[0], 64);
  140. EXPECT_EQ((int)mod->getOutput(2)->getValue()[0], 1);
  141. mod->disconnect();
  142. }
  143. //*/
  144. }