Implement dynamic procedure at REST interface
[anna.git] / example / diameter / launcher / MyHandler.cpp
index 7d312ef..9086582 100644 (file)
@@ -368,31 +368,6 @@ bool MyHandler::doPOST(const std::string &uri, const nlohmann::json &j, std::str
     auto it = j.find("condition");
     if (it != j.end() && it->is_object()) {
 
-/*
-      auto j2 = it->get<nlohmann::json::object_t>();
-
-      // [code]|[bitR]|[hopByHop]|[applicationId]|[sessionId]|[resultCode]|[msisdn]|[imsi]|[serviceContextId]
-      auto it_code = j2.find("code");
-      auto it_bitR = j2.find("bitR");
-      auto it_hopByHop = j2.find("hopByHop");
-      auto it_applicationId = j2.find("applicationId");
-      auto it_sessionId = j2.find("sessionId");
-      auto it_resultCode = j2.find("resultCode");
-      auto it_msisdn = j2.find("msisdn");
-      auto it_imsi = j2.find("imsi");
-      auto it_serviceContextId = j2.find("serviceContextId");
-
-      std::string p1 = (it_code != j2.end() && it_code->is_string()) ? *it_code : "";
-      std::string p2 = (it_bitR != j2.end() && it_bitR->is_string()) ? *it_bitR : "";
-      std::string p3 = (it_hopByHop != it->end() && it_hopByHop->is_string()) ? *it_hopByHop : "";
-      std::string p4 = (it_applicationId != it->end() && it_applicationId->is_string()) ? *it_applicationId : "";
-      std::string p5 = (it_sessionId != it->end() && it_sessionId->is_string()) ? *it_sessionId : "";
-      std::string p6 = (it_resultCode != it->end() && it_resultCode->is_string()) ? *it_resultCode : "";
-      std::string p7 = (it_msisdn != it->end() && it_msisdn->is_string()) ? *it_msisdn : "";
-      std::string p8 = (it_imsi != it->end() && it_imsi->is_string()) ? *it_imsi : "";
-      std::string p9 = (it_serviceContextId != it->end() && it_serviceContextId->is_string()) ? *it_serviceContextId : "";
-*/
-
       // [code]|[bitR]|[hopByHop]|[applicationId]|[sessionId]|[resultCode]|[msisdn]|[imsi]|[serviceContextId]
       auto it_code = it->find("code");
       auto it_bitR = it->find("bitR");
@@ -477,19 +452,16 @@ bool MyHandler::doPOST(const std::string &uri, const nlohmann::json &j, std::str
   }
   else if (opType == "/test-reset") {
     auto it = j.find("type");
-    if (it != j.end() && it->is_string()) {
+    std::string type = (it != j.end() && it->is_string()) ? *it : "soft";
 
-      auto itI = j.find("id");
-      int id = (itI != j.end() && itI->is_number_integer()) ? itI->get<int>() : -2; // default is: apply to all the tests
+    auto itI = j.find("id");
+    int id = (itI != j.end() && itI->is_number_integer()) ? itI->get<int>() : -1; // default is: apply to all the tests
 
-      if ((*it == "soft") || (*it == "hard")) {
-        result = eop.test__reset(response, (*it == "soft"), id);
-      }
-      else
-        response += "invalid 'type' string field (allowed: soft|hard)";
+    if ((type == "soft") || (type == "hard")) {
+      result = eop.test__reset(response, (type == "soft"), id);
     }
     else
-      response += "missing 'type' string field";
+      response += "invalid 'type' string field (allowed: [soft]|hard)";
   }
   else if (opType == "/test-repeats") {
     auto it = j.find("amount");
@@ -522,10 +494,8 @@ bool MyHandler::doPOST(const std::string &uri, const nlohmann::json &j, std::str
   }
   else if (opType == "/test-junit") {
     auto it = j.find("targetFile");
-    if (it != j.end() && it->is_string())
-      result = eop.test__junit(response, *it);
-    else
-      response += "missing 'targetFile' string field";
+    std::string targetFile = (it != j.end() && it->is_string()) ? *it : "/tmp/junit.xml";
+    result = eop.test__junit(response, targetFile);
   }
   else if (opType == "/test-summary-counts") {
     result = eop.test__summary_counts(response);
@@ -549,7 +519,7 @@ bool MyHandler::doPOST(const std::string &uri, const nlohmann::json &j, std::str
     else
       response += "invalid 'action' string field (allowed: enable|disable)";
   }
-  else if ((opType == "/test-report-hex")||(opType == "/test-dump_stdout")) {
+  else if ((opType == "/test-report-hex")||(opType == "/test-dump-stdout")) {
 
     auto itA = j.find("action");
     std::string action = (itA != j.end() && itA->is_string()) ? *itA : "enable"; // default is: enable
@@ -566,6 +536,16 @@ bool MyHandler::doPOST(const std::string &uri, const nlohmann::json &j, std::str
       response += "invalid 'action' string field (allowed: enable|disable)";
   }
 
+  // Dynamic procedure
+  else if ((opType == "/dynamic")) {
+    auto it = j.find("arguments");
+    if (it != j.end() && it->is_object()) {
+      result = eop.test__dynamic(response, *it);
+    }
+    else
+      response += "missing 'arguments' object field";
+  }
+
 
   return result;
 }