Simplify command (no parameter field, all inside command). Interactive mode.
[anna.git] / example / diameter / launcher / testing / TestStep.cpp
index c8aef4e..c1f27d8 100644 (file)
@@ -24,7 +24,6 @@
 #include <anna/diameter.comm/ServerSession.hpp>
 #include <anna/diameter.comm/Server.hpp>
 #include <anna/core/tracing/Logger.hpp>
-#include <anna/diameter/codec/Message.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 {
@@ -93,19 +92,15 @@ namespace {
     // TODO: mutex the step while setting data here !!
   }
 
-  bool decodeMessage(const anna::DataBlock &message, anna::diameter::codec::Message *messageCodec) throw() {
+  bool decodeMessage(const anna::DataBlock &message, anna::diameter::codec::Message &messageCodec) throw() {
 
     if (message.isEmpty())
       return false;
 
     bool result = true;
     try {
-      if (!messageCodec) {
-        Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
-        messageCodec = new anna::diameter::codec::Message(my_app.getCodecEngine());
-      }
-
-      messageCodec->decode(message);
+      messageCodec.setEngine(NULL); // perhaps we will need another codec engine ...
+      messageCodec.decode(message);
     }
     catch (anna::RuntimeException &ex) {
       ex.trace();
@@ -114,7 +109,6 @@ namespace {
 
     return result;
   }
-
 }
 
 
@@ -130,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");
 
@@ -159,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();
 }
 
@@ -181,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();
@@ -195,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");
@@ -247,12 +262,7 @@ void TestStepTimeout::do_reset() throw() {
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
 // TestStepSendxml
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
-TestStepSendxml::~TestStepSendxml() {
-  delete a_messageCodec;
-  a_messageCodec = NULL;
-}
-
-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");
@@ -268,7 +278,7 @@ throw() {
     }
   }
 
-  if (decodeMessage(a_message, a_messageCodec)) {
+  if (decodeMessage()) {
     xmlmsg = "\n";
     xmlmsg += a_messageCodec->asXMLString();
     xmlmsg += "\n";
@@ -346,10 +356,7 @@ bool TestStepSendxml::do_execute() throw() {
 
       // Detailed log:
       if(a_realmNode->logEnabled()) {
-        if (!a_messageCodec)
-          decodeMessage(a_message, a_messageCodec);
-
-        if (a_messageCodec) {
+        if (decodeMessage()) {
           std::string detail = usedClientSession ? usedClientSession->asString() : "<null client session>"; // shouldn't happen
           a_realmNode->writeLogFile(*a_messageCodec, (success ? "sent2e" : "send2eError"), detail);
         }
@@ -383,10 +390,7 @@ bool TestStepSendxml::do_execute() throw() {
 
       // Detailed log:
       if(a_realmNode->logEnabled()) {
-        if (!a_messageCodec)
-          decodeMessage(a_message, a_messageCodec);
-
-        if (a_messageCodec) {
+        if (decodeMessage()) {
           std::string detail = usedServerSession ? usedServerSession->asString() : "<null server session>"; // shouldn't happen
           a_realmNode->writeLogFile(*a_messageCodec, (success ? "sent2c" : "send2cError"), detail);
         }
@@ -414,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");
@@ -461,14 +466,10 @@ void TestStepDelay::do_reset() throw() {
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
 // TestStepWait
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
-TestStepWait::~TestStepWait() {
-  delete a_messageCodec;
-  a_messageCodec = NULL;
-}
-
 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);
@@ -487,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");
@@ -506,7 +507,7 @@ throw() {
     }
   }
 
-  if (decodeMessage(a_message, a_messageCodec)) {
+  if (decodeMessage()) {
     xmlmsg = "\n";
     xmlmsg += a_messageCodec->asXMLString();
     xmlmsg += "\n";
@@ -523,13 +524,13 @@ 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() {
   if (a_condition.comply(db/*, matchSessionId*/)) {
-    //a_message = db; // store matched
-    a_message.assign(db);
+    a_message = db; // store matched
     complete();
     return true;
   }
@@ -546,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);
@@ -566,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()));