Add third work package for REST API implementation
[anna.git] / example / diameter / launcher / EventOperation.cpp
index 935940d..7709fb1 100644 (file)
@@ -36,7 +36,8 @@
 //#include <anna/http/Transport.hpp>
 //#include <anna/diameter/stack/Engine.hpp>
 #include <anna/diameter/helpers/base/functions.hpp>
-//#include <anna/time/functions.hpp>
+#include <anna/time/functions.hpp>
+#include <anna/core/functions.hpp>
 //#include <anna/diameter.comm/ApplicationMessageOamModule.hpp>
 //#include <anna/testing/defines.hpp>
 #include <anna/xml/xml.hpp>
@@ -50,7 +51,7 @@
 //#include <EventOperation.hpp>
 #include <MyDiameterEngine.hpp>
 #include <MyLocalServer.hpp>
-//#include <anna/testing/TestManager.hpp>
+#include <anna/testing/TestManager.hpp>
 //#include <anna/testing/TestCase.hpp>
 
 
@@ -577,8 +578,17 @@ bool EventOperation::answermsg_action_2c(std::string &response, const std::strin
 bool EventOperation::test_id__description(std::string &response, unsigned int id, const std::string & description) {
 
   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+  anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate();
 
-
+  try {
+    testManager.getTestCase(id)->setDescription(description); // creates / reuses
+    response = "Done";
+  }
+  catch(anna::RuntimeException &ex) {
+    ex.trace();
+    response += "invalid ip-limit";
+    return false;
+  }
 
   return true; // OK
 }
@@ -586,8 +596,17 @@ bool EventOperation::test_id__description(std::string &response, unsigned int id
 bool EventOperation::test_id__ip_limit(std::string &response, unsigned int id, int amount) {
 
   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+  anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate();
 
-
+  try {
+    testManager.getTestCase(id)->addIpLimit(amount); // creates / reuses
+    response = "Done";
+  }
+  catch(anna::RuntimeException &ex) {
+    ex.trace();
+    response += "invalid ip-limit";
+    return false;
+  }
 
   return true; // OK
 }
@@ -595,26 +614,55 @@ bool EventOperation::test_id__ip_limit(std::string &response, unsigned int id, i
 bool EventOperation::test_id__timeout(std::string &response, unsigned int id, int msecs) {
 
   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+  anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate();
 
-
+  try {
+    anna::Millisecond timeout = my_app.checkTimeMeasure("Test case timeout", anna::functions::asString(msecs));
+    testManager.getTestCase(id)->addTimeout(timeout); // creates / reuses
+    response = "Done";
+  }
+  catch(anna::RuntimeException &ex) {
+    ex.trace();
+    response += "invalid timeout";
+    return false;
+  }
 
   return true; // OK
 }
 
-bool EventOperation::test_id__sendmsg2e(std::string &response, unsigned int id, const std::string & diameterJson, int stepNumber) {
+bool EventOperation::test_id__sendmsg2e_2c(std::string &response, unsigned int id, bool _2e_or_2c, const std::string & diameterJson, int stepNumber) {
 
   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+  anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate();
 
+  bool success;
+  std::string diameterXml = anna::json::functions::json2xml(diameterJson, success);
+  if (!success) {
+    response += "json to xml failed, unable to load message !";
+    return false;
+  }
+  anna::diameter::codec::Message codecMsg; // auxiliary codec message
+  codecMsg.loadXMLString(diameterXml);
 
+  LOGWARNING(
+     if (!codecMsg.isRequest() && (stepNumber == -1))
+      anna::Logger::warning("Step number has not been provided. Take into account that this answer message will be sent 'as is' and sequence information could be wrong at the remote peer", ANNA_FILE_LOCATION);
+  );
 
-  return true; // OK
-}
-
-bool EventOperation::test_id__sendmsg2c(std::string &response, unsigned int id, const std::string & diameterJson, int stepNumber) {
-
-  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
-
+  try {
+    my_app.updateOperatedOriginHostWithMessage(codecMsg);
+    if (_2e_or_2c)
+      testManager.getTestCase(id)->addSendDiameterXml2e(codecMsg.code(), my_app.getOperatedHost(), stepNumber); // creates / reuses
+    else
+      testManager.getTestCase(id)->addSendDiameterXml2c(codecMsg.code(), my_app.getOperatedHost(), stepNumber); // creates / reuses
 
+    response = "Done";
+  }
+  catch(anna::RuntimeException &ex) {
+    ex.trace();
+    response += "failed";
+    return false;
+  }
 
   return true; // OK
 }
@@ -622,8 +670,18 @@ bool EventOperation::test_id__sendmsg2c(std::string &response, unsigned int id,
 bool EventOperation::test_id__delay(std::string &response, unsigned int id, int msecs) {
 
   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+  anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate();
 
-
+  try {
+    anna::Millisecond delay = ((msecs == 0 /* special case */) ? (anna::Millisecond)0 : my_app.checkTimeMeasure("Test case delay step", anna::functions::asString(msecs)));
+    testManager.getTestCase(id)->addDelay(delay); // creates / reuses
+    response = "Done";
+  }
+  catch(anna::RuntimeException &ex) {
+    ex.trace();
+    response += "invalid delay";
+    return false;
+  }
 
   return true; // OK
 }
@@ -631,62 +689,164 @@ bool EventOperation::test_id__delay(std::string &response, unsigned int id, int
 bool EventOperation::test_id__sh_command(std::string &response, unsigned int id, const std::string & script) {
 
   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+  anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate();
 
-
-
-  return true; // OK
-}
-
-bool EventOperation::test_id__waitfe_hex(std::string &response, unsigned int id, const std::string & hex, bool strict) {
-
-  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
-
-
+  try {
+    testManager.getTestCase(id)->addCommand(script); // creates / reuses
+    response = "Done";
+  }
+  catch(anna::RuntimeException &ex) {
+    ex.trace();
+    response += "failed";
+    return false;
+  }
 
   return true; // OK
 }
 
-bool EventOperation::test_id__waitfc_hex(std::string &response, unsigned int id, const std::string & hex, bool strict) {
+bool EventOperation::test_id__waitfefc_hex(std::string &response, unsigned int id, bool fe_or_fc, const std::string & hex, bool strict) {
 
   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+  anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate();
 
+  // Get DataBlock from hex content:
+  anna::DataBlock db_aux(true);
+  std::string hexString = hex;
+  hexString.erase(std::remove(hexString.begin(), hexString.end(), ':'), hexString.end());
+  LOGDEBUG(
+      std::string msg = "Hex string (remove colons if exists): ";
+  msg += hexString;
+  anna::Logger::debug(msg, ANNA_FILE_LOCATION);
+  );
+
+  std::string regexp;
+  try {
+    anna::functions::fromHexString(hexString, db_aux); // could launch exception
+    regexp = anna::functions::asHexString(db_aux);
+  }
+  catch(anna::RuntimeException &ex) {
+    ex.trace();
+    response += "failed";
+    return false;
+  }
 
+  // optional 'full':
+  if(!strict) {
+    //// If request, we will ignore sequence data:
+    //if (anna::diameter::codec::functions::requestBit(db_aux))
+    regexp.replace (24, 16, "[0-9A-Fa-f]{16}");
 
-  return true; // OK
-}
-
-bool EventOperation::test_id__waitfe_msg(std::string &response, unsigned int id, const std::string & diameterJson, bool strict) {
-
-  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
-
+    regexp.insert(0, "^");
+    regexp += "$";
+  }
 
+  try {
+    testManager.getTestCase(id)->addWaitDiameterRegexpHex(fe_or_fc, regexp);
+    response = "Done";
+  }
+  catch(anna::RuntimeException &ex) {
+    ex.trace();
+    response += "failed";
+    return false;
+  }
 
   return true; // OK
 }
 
-bool EventOperation::test_id__waitfc_msg(std::string &response, unsigned int id, const std::string & diameterJson, bool strict) {
+bool EventOperation::test_id__waitfefc_msg(std::string &response, unsigned int id, bool fe_or_fc, const std::string & diameterJson, bool strict) {
 
   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+  anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate();
 
+  bool success;
+  std::string diameterXml = anna::json::functions::json2xml(diameterJson, success);
+  if (!success) {
+    response += "json to xml failed, unable to load message !";
+    return false;
+  }
 
+  try {
+    anna::diameter::codec::Message codecMsg; // auxiliary codec message
+    codecMsg.loadXMLString(diameterXml);
+    std::string regexp = codecMsg.asXMLString(true /* normalization */);
+
+    // Now we must insert regular expressions in hop-by-hop, end-to-end and Origin-State-Id:
+
+    // optional 'full':
+    if(!strict) {
+      std::string::size_type pos, pos_1, pos_2;
+
+      pos = regexp.find("end-to-end-id=", 0u);
+      pos = regexp.find("\"", pos);
+      pos_1 = pos;
+      pos = regexp.find("\"", pos+1);
+      pos_2 = pos;
+      regexp.replace(pos_1 + 1, pos_2 - pos_1 - 1, "[0-9]+");
+
+      pos = regexp.find("hop-by-hop-id=", 0u);
+      pos = regexp.find("\"", pos);
+      pos_1 = pos;
+      pos = regexp.find("\"", pos+1);
+      pos_2 = pos;
+      regexp.replace(pos_1 + 1, pos_2 - pos_1 - 1, "[0-9]+");
+
+      // For this representation: <avp name="Origin-State-Id" data="1428633668"/>
+      //pos = regexp.find("Origin-State-Id", 0u);
+      //pos = regexp.find("\"", pos);
+      //pos = regexp.find("\"", pos+1);
+      //pos_1 = pos;
+      //pos = regexp.find("\"", pos+1);
+      //pos_2 = pos;
+      //regexp.replace(pos_1 + 1, pos_2 - pos_1 - 1, "[0-9]+");
+      // But we have this one: <avp data="1428633668" name="Origin-State-Id"/>
+      pos = regexp.find("Origin-State-Id", 0u);
+      pos = regexp.rfind("\"", pos);
+      pos = regexp.rfind("\"", pos-1);
+      pos = regexp.rfind("\"", pos-1);
+      pos_1 = pos;
+      pos = regexp.find("\"", pos+1);
+      pos_2 = pos;
+      regexp.replace(pos_1 + 1, pos_2 - pos_1 - 1, "[0-9]+");
+
+      //regexp.insert(0, "^");
+      //regexp += "$";
+    }
 
-  return true; // OK
-}
-
-bool EventOperation::test_id__waitfe(std::string &response, unsigned int id, const std::string & condition) {
-
-  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
-
-
+    testManager.getTestCase(id)->addWaitDiameterRegexpXml(fe_or_fc, regexp);
+    response = "Done";
+  }
+  catch(anna::RuntimeException &ex) {
+    ex.trace();
+    response += "failed";
+    return false;
+  }
 
   return true; // OK
 }
 
-bool EventOperation::test_id__waitfc(std::string &response, unsigned int id, const std::string & condition) {
+bool EventOperation::test_id__waitfefc(std::string &response, unsigned int id, bool fe_or_fc,
+                         const std::string & code,
+                         const std::string & bitR,
+                         const std::string & hopByHop,
+                         const std::string & applicationId,
+                         const std::string & sessionId,
+                         const std::string & resultCode,
+                         const std::string & msisdn,
+                         const std::string & imsi,
+                         const std::string & serviceContextId) {
 
   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+  anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate();
 
-
+  try { // [code]|[bitR]|[hopByHop]|[applicationId]|[sessionId]|[resultCode]|[msisdn]|[imsi]|[serviceContextId]
+    testManager.getTestCase(id)->addWaitDiameter(fe_or_fc, code, bitR, hopByHop, applicationId, sessionId, resultCode, msisdn, imsi, serviceContextId);
+    response = "Done";
+  }
+  catch(anna::RuntimeException &ex) {
+    ex.trace();
+    response += "failed";
+    return false;
+  }
 
   return true; // OK
 }
@@ -814,8 +974,21 @@ bool EventOperation::test__finished(std::string &response) {
 bool EventOperation::test__clear(std::string &response) {
 
   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+  anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate();
 
-
+  try {
+    if (testManager.clearPool()) {
+      response = "All the programmed test cases have been dropped";
+    }
+    else {
+      response = "There are not programmed test cases to be removed";
+    }
+  }
+  catch(anna::RuntimeException &ex) {
+    ex.trace();
+    response += "failed";
+    return false;
+  }
 
   return true; // OK
 }