Simplify command (no parameter field, all inside command). Interactive mode.
[anna.git] / example / diameter / launcher / testing / TestStep.cpp
index 491ec65..c1f27d8 100644 (file)
@@ -20,6 +20,9 @@
 #include <anna/xml/Compiler.hpp>
 #include <anna/core/util/Millisecond.hpp>
 #include <anna/diameter.comm/Message.hpp>
+#include <anna/diameter.comm/ClientSession.hpp>
+#include <anna/diameter.comm/ServerSession.hpp>
+#include <anna/diameter.comm/Server.hpp>
 #include <anna/core/tracing/Logger.hpp>
 #include <anna/diameter/codec/functions.hpp>
 #include <anna/diameter/helpers/base/functions.hpp>
 #include <MyDiameterEntity.hpp>
 #include <MyLocalServer.hpp>
 #include <TestStep.hpp>
-#include <Launcher.hpp>
 #include <TestCase.hpp>
 #include <TestManager.hpp>
 #include <TestTimer.hpp>
+#include <Launcher.hpp>
 
 
 namespace {
@@ -88,6 +91,24 @@ namespace {
     // TODO: terminate thread when deprecated (RT signal ?)
     // TODO: mutex the step while setting data here !!
   }
+
+  bool decodeMessage(const anna::DataBlock &message, anna::diameter::codec::Message &messageCodec) throw() {
+
+    if (message.isEmpty())
+      return false;
+
+    bool result = true;
+    try {
+      messageCodec.setEngine(NULL); // perhaps we will need another codec engine ...
+      messageCodec.decode(message);
+    }
+    catch (anna::RuntimeException &ex) {
+      ex.trace();
+      result = false;
+    }
+
+    return result;
+  }
 }
 
 
@@ -103,13 +124,23 @@ void TestStep::initialize(TestCase *testCase) {
   a_number = testCase->steps() + 1; // testCase is not NULL
 }
 
+bool TestStep::decodeMessage() throw() {
+  if (a_messageCodec) return true;
+  a_messageCodec = new anna::diameter::codec::Message;
+  if (::decodeMessage(a_message, *a_messageCodec)) return true;
+
+  delete a_messageCodec;
+  a_messageCodec = NULL;
+  return false;
+}
+
 const char* TestStep::asText(const Type::_v type)
 throw() {
-  static const char* text [] = { "Unconfigured", "Timeout", "Sendxml2e", "Sendxml2c", "Delay", "Wait", "Cmd" };
+  static const char* text [] = { "Unconfigured", "Timeout", "Sendxml2e", "Sendxml2c", "Delay", "Wait", "Command" };
   return text [type];
 }
 
-anna::xml::Node* TestStep::asXML(anna::xml::Node* parent) const
+anna::xml::Node* TestStep::asXML(anna::xml::Node* parent)
 throw() {
   anna::xml::Node* result = parent->createChild("TestStep");
 
@@ -132,14 +163,24 @@ throw() {
   return result;
 }
 
-std::string TestStep::asXMLString() const throw() {
+std::string TestStep::asXMLString() throw() {
   anna::xml::Node root("root");
   return anna::xml::Compiler().apply(asXML(&root));
 }
 
 bool TestStep::execute() throw() {
+
+  int ia = a_testCase->interactiveAmount();
+  if (ia > -1) {
+    if (ia == 0) return false;
+    a_testCase->interactiveExecution();
+    LOGDEBUG(anna::Logger::debug("Interactive execution ...", ANNA_FILE_LOCATION));
+    if (a_executed) return false; // avoid repeating (this implies amount consumption)
+  }
+
   LOGDEBUG(anna::Logger::debug(anna::functions::asString("EXECUTING %s (step number %d) for Test Case %llu (%p) (%p)", asText(a_type), a_number, a_testCase->getId(), (TestCaseStep*)this, this), ANNA_FILE_LOCATION));
   setBeginTimestamp(anna::functions::millisecond());
+  a_executed = true;
   return do_execute();
 }
 
@@ -154,6 +195,7 @@ void TestStep::reset() throw() {
   LOGDEBUG(anna::Logger::debug(anna::functions::asString("RESET %s (step number %d) for Test Case %llu (%p) (%p)", asText(a_type), a_number, a_testCase->getId(), (TestCaseStep*)this, this), ANNA_FILE_LOCATION));
   // type and testCase kept
   a_completed = false;
+  a_executed = false;
   a_beginTimestamp = 0;
   a_endTimestamp = 0;
   do_reset();
@@ -168,7 +210,7 @@ void TestStep::next() throw() {
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
 // TestStepTimeout
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
-anna::xml::Node* TestStepTimeout::asXML(anna::xml::Node* parent) const
+anna::xml::Node* TestStepTimeout::asXML(anna::xml::Node* parent)
 throw() {
   anna::xml::Node* result = TestStep::asXML(parent); // end timestamp will be 0 if test finished OK
   //parent->createChild("TestStepTimeout");
@@ -220,7 +262,7 @@ void TestStepTimeout::do_reset() throw() {
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
 // TestStepSendxml
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
-anna::xml::Node* TestStepSendxml::asXML(anna::xml::Node* parent) const
+anna::xml::Node* TestStepSendxml::asXML(anna::xml::Node* parent)
 throw() {
   anna::xml::Node* result = TestStep::asXML(parent);
   //parent->createChild("TestStepSendxml");
@@ -236,16 +278,10 @@ throw() {
     }
   }
 
-  if (!a_message.isEmpty()) {
-    try {
-      Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
-      static anna::diameter::codec::Message codecMsg(my_app.getCodecEngine());
-      codecMsg.decode(a_message);
-      xmlmsg = "\n"; xmlmsg += codecMsg.asXMLString(); xmlmsg += "\n";
-    }
-    catch (anna::RuntimeException &ex) {
-      ex.trace();
-    }
+  if (decodeMessage()) {
+    xmlmsg = "\n";
+    xmlmsg += a_messageCodec->asXMLString();
+    xmlmsg += "\n";
   }
 
   if (msg != "") result->createAttribute("Message", msg);
@@ -258,15 +294,22 @@ throw() {
 }
 
 bool TestStepSendxml::do_execute() throw() {
-  anna::diameter::comm::Message *msg = a_realmNode->createCommMessage();
   bool success = false;
   std::string failReason, s_warn;
+  MyDiameterEntity *entity = a_realmNode->getEntity(); // by default
+  MyLocalServer *localServer = a_realmNode->getDiameterServer(); // by default
+  const TestStepWait *tsw = NULL;
+
+  // Create comm message:
+  anna::diameter::comm::Message *msg = a_realmNode->createCommMessage();
+  //msg->clearBody();
+  msg->setBody(a_message);
 
   try {
     // Update sequence for answers:
     if (a_waitForRequestStepNumber != -1) { // is an answer: try to copy sequence information; alert about Session-Id discrepance
       // Request which was received:
-      const TestStepWait *tsw = (const TestStepWait*)(a_testCase->getStep(a_waitForRequestStepNumber));
+      tsw = (const TestStepWait*)(a_testCase->getStep(a_waitForRequestStepNumber));
       const anna::DataBlock &request = tsw->getMsgDataBlock();
       anna::diameter::HopByHop hbh = anna::diameter::codec::functions::getHopByHop(request);
       anna::diameter::EndToEnd ete = anna::diameter::codec::functions::getEndToEnd(request);
@@ -285,31 +328,75 @@ bool TestStepSendxml::do_execute() throw() {
     }
 
     if (getType() == Type::Sendxml2e) {
-      MyDiameterEntity *entity = a_realmNode->getEntity();
-      if (entity) {
-        //msg->clearBody();
-        msg->setBody(a_message);
-        /* response = NULL =*/entity->send(msg);
-        success = true;
+      anna::diameter::comm::ClientSession *usedClientSession = NULL;
+
+      if (tsw) { // is an answer for a received request on wait condition
+        anna::diameter::comm::ClientSession *clientSession = tsw->getClientSession();
+        if (clientSession) {
+          /* response NULL (is an answer) */clientSession->send(msg);
+          success = true;
+          usedClientSession = clientSession;
+        }
+        else {
+          failReason = "Reference wait step didn't store a valid client session. Unable to send the message";
+          LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION));
+        }
       }
       else {
-        failReason = "There is no diameter entity currently configured. Unable to send the message";
-        LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION));
+        if (entity) {
+          success = entity->send(msg);
+          anna::diameter::comm::Server *usedServer = entity->getLastUsedResource();
+          usedClientSession = usedServer ? usedServer->getLastUsedResource() : NULL;
+        }
+        else {
+          failReason = "There is no diameter entity currently configured. Unable to send the message";
+          LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION));
+        }
+      } // else (normal sending)
+
+      // Detailed log:
+      if(a_realmNode->logEnabled()) {
+        if (decodeMessage()) {
+          std::string detail = usedClientSession ? usedClientSession->asString() : "<null client session>"; // shouldn't happen
+          a_realmNode->writeLogFile(*a_messageCodec, (success ? "sent2e" : "send2eError"), detail);
+        }
       }
     }
     else if (getType() == Type::Sendxml2c) {
-      MyLocalServer *localServer = a_realmNode->getDiameterServer();
-      if (localServer) {
-        //msg->clearBody();
-        msg->setBody(a_message);
-        /* response = NULL =*/localServer->send(msg);
-        success = true;
+      anna::diameter::comm::ServerSession *usedServerSession = NULL;
+
+      if (tsw) { // is an answer for a received request on wait condition
+        anna::diameter::comm::ServerSession *serverSession = tsw->getServerSession();
+        if (serverSession) {
+          /* response NULL (is an answer) */serverSession->send(msg);
+          success = true;
+          usedServerSession = serverSession;
+        }
+        else {
+          failReason = "Reference wait step didn't store a valid server session. Unable to send the message";
+          LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION));
+        }
       }
       else {
-        failReason = "There is no diameter local server currently configured. Unable to send the message";
-        LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION));
+        if (localServer) {
+          success = localServer->send(msg);
+          usedServerSession = localServer->getLastUsedResource();
+        }
+        else {
+          failReason = "There is no diameter local server currently configured. Unable to send the message";
+          LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION));
+        }
+      } // else (normal sending)
+
+      // Detailed log:
+      if(a_realmNode->logEnabled()) {
+        if (decodeMessage()) {
+          std::string detail = usedServerSession ? usedServerSession->asString() : "<null server session>"; // shouldn't happen
+          a_realmNode->writeLogFile(*a_messageCodec, (success ? "sent2c" : "send2cError"), detail);
+        }
       }
     }
+
   } catch(anna::RuntimeException &ex) {
     failReason = ex.asString();
   }
@@ -331,12 +418,13 @@ bool TestStepSendxml::do_execute() throw() {
 void TestStepSendxml::do_reset() throw() {
   a_expired = false;
   //a_message.clear();
+  //a_messageAlreadyDecoded = false;
 }
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
 // TestStepDelay
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
-anna::xml::Node* TestStepDelay::asXML(anna::xml::Node* parent) const
+anna::xml::Node* TestStepDelay::asXML(anna::xml::Node* parent)
 throw() {
   anna::xml::Node* result = TestStep::asXML(parent);
   //parent->createChild("TestStepDelay");
@@ -379,8 +467,9 @@ void TestStepDelay::do_reset() throw() {
 // TestStepWait
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
 void TestStepWait::setCondition(bool fromEntity,
-                                  const std::string &code, const std::string &bitR, const std::string &resultCode, const std::string &sessionId,
-                                  const std::string &hopByHop, const std::string &msisdn, const std::string &imsi, const std::string &serviceContextId) throw() {
+    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) throw() {
 
   a_condition.setReceivedFromEntity(fromEntity);
   a_condition.setCode(code);
@@ -399,7 +488,7 @@ void TestStepWait::setCondition(bool fromEntity, const std::string &regexp) thro
   a_condition.setRegexp(regexp);
 }
 
-anna::xml::Node* TestStepWait::asXML(anna::xml::Node* parent) const
+anna::xml::Node* TestStepWait::asXML(anna::xml::Node* parent)
 throw() {
   anna::xml::Node* result = TestStep::asXML(parent);
   //parent->createChild("TestStepWait");
@@ -418,16 +507,10 @@ throw() {
     }
   }
 
-  if (!a_message.isEmpty()) {
-    try {
-      Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
-      static anna::diameter::codec::Message codecMsg(my_app.getCodecEngine());
-      codecMsg.decode(a_message);
-      xmlmsg = "\n"; xmlmsg += codecMsg.asXMLString(); xmlmsg += "\n";
-    }
-    catch (anna::RuntimeException &ex) {
-      ex.trace();
-    }
+  if (decodeMessage()) {
+    xmlmsg = "\n";
+    xmlmsg += a_messageCodec->asXMLString();
+    xmlmsg += "\n";
   }
 
   if (msg != "") result->createAttribute("MatchedMessage", msg);
@@ -441,7 +524,8 @@ bool TestStepWait::do_execute() throw() {
 }
 
 void TestStepWait::do_complete() throw() {
-  a_testCase->process(); // next() not invoked; we only want to reactivate the test case
+  //a_testCase->process(); // next() not invoked; we only want to reactivate the test case
+  // avoid stack overflow: we will process the test case externally when incoming message is fulfilled (TestCase.cpp), and TestManager is noticed
 }
 
 bool TestStepWait::fulfilled(const anna::DataBlock &db/*, bool matchSessionId*/) throw() {
@@ -463,13 +547,12 @@ void TestStepWait::do_reset() throw() {
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
 // TestStepCmd
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
-anna::xml::Node* TestStepCmd::asXML(anna::xml::Node* parent) const
+anna::xml::Node* TestStepCmd::asXML(anna::xml::Node* parent)
 throw() {
   anna::xml::Node* result = TestStep::asXML(parent);
   //parent->createChild("TestStepCmd");
 
   result->createAttribute("Script", (a_script != "") ? a_script:"<no script>");
-  result->createAttribute("Parameters", (a_parameters != "") ? a_parameters:"<no parameters>");
   if (a_errorMsg != "") result->createAttribute("ErrorMessage", a_errorMsg);
   if (!a_threadRunning && a_resultCode != -2) {
     result->createAttribute("ResultCode", a_resultCode);
@@ -483,8 +566,6 @@ bool TestStepCmd::do_execute() throw() {
   if (!a_threadRunning /* || a_threadDeprecated DO NOT WANT TO OVERLAP ... */) {
     // Special tags to replace:
     std::string cmd = getScript();
-    cmd += " ";
-    cmd += getParameters();
     size_t index;
     while ((index = cmd.find(SH_COMMAND_TAG_FOR_REPLACE__CYCLE_ID)) != std::string::npos)
       cmd.replace(index, strlen(SH_COMMAND_TAG_FOR_REPLACE__CYCLE_ID), anna::functions::asString(TestManager::instantiate().getPoolCycle()));