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