Improvements from anna fork
[anna.git] / source / testing / TestCase.cpp
index 818093b..fd4ed42 100644 (file)
@@ -56,13 +56,27 @@ anna::xml::Node* TestCase::DebugSummary::asXML(anna::xml::Node* parent) const th
 
   return result;
 };
+
+std::string TestCase::DebugSummary::asString() const throw() {
+  std::string result = "";
+
+  std::vector<event_t>::const_iterator it;
+  for (it = a_events.begin(); it != a_events.end(); it++) {
+    result += anna::functions::asString("[Timestamp: %s] %s", (*it).Timestamp.asString().c_str(), (*it).Hint.c_str());
+  }
+
+  return result;
+};
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 
-TestCase::TestCase(unsigned int id) :
+TestCase::TestCase(unsigned int id, const std::string &description) :
     a_id(id),
+    a_description((description != "") ? description : (anna::functions::asString("Testcase_%d", id))),
     a_state(State::Initialized),
-    a_startTime(0),
+    a_startTimestamp(0),
+    a_finishTimestamp(0),
     a_interactiveAmount(-1) {
 
   /*a_stepsIt = a_steps.end()*/;
@@ -82,13 +96,24 @@ throw() {
   return text [state];
 }
 
+anna::Millisecond TestCase::getLapseMs() const throw() {
+  return ((a_finishTimestamp >= a_startTimestamp) ? (a_finishTimestamp - a_startTimestamp) : (anna::Millisecond)0);
+}
+
 anna::xml::Node* TestCase::asXML(anna::xml::Node* parent) const
 throw() {
   anna::xml::Node* result = parent->createChild("TestCase");
 
   result->createAttribute("Id", a_id);
+  result->createAttribute("Description", a_description);
   result->createAttribute("State", asText(a_state));
-  result->createAttribute("StartTimestamp", a_startTime.asString());
+  result->createAttribute("StartTimestamp", a_startTimestamp.asString());
+
+  if (a_finishTimestamp != 0) {
+    result->createAttribute("FinishTimestamp", a_finishTimestamp.asString());
+    result->createAttribute("LapseMs", getLapseMs());
+  }
+
   int steps = a_steps.size();
   if (steps != 0) {
     result->createAttribute("NumberOfTestSteps", steps);
@@ -112,12 +137,12 @@ std::string TestCase::asXMLString() const throw() {
   return anna::xml::Compiler().apply(asXML(&root));
 }
 
-bool TestCase::hasSameCondition(const TestCondition &condition) const throw() {
+bool TestCase::hasSameCondition(const TestDiameterCondition &condition) const throw() {
   std::vector<TestStep*>::const_iterator it;
-  TestStepWait *step;
+  TestStepWaitDiameter *step;
   for (it = a_steps.begin(); it != a_steps.end(); it++) {
     if ((*it)->getType() != TestStep::Type::Wait) continue;
-    step = (TestStepWait *)(*it);
+    step = (TestStepWaitDiameter *)(*it);
     if (step->getCondition() == condition) return true;
   }
   return false;
@@ -136,6 +161,29 @@ void TestCase::setState(const State::_v &state) throw() {
 
 
   if (isFinished()) {
+    const char *literal = "FINISHED Test Case %llu/%llu [%s] => %s";
+    TestManager& testManager (TestManager::instantiate ());
+    LOGDEBUG(anna::Logger::debug(anna::functions::asString(literal, getId(), testManager.tests(), getDescription().c_str(), asText(a_state)), ANNA_FILE_LOCATION));
+
+    if (testManager.getDumpStdout()) {
+      std::cout << std::endl << anna::functions::asString(literal, getId(), testManager.tests(), getDescription().c_str(), asText(a_state)) << std::endl;
+    }
+
+    a_finishTimestamp = anna::functions::millisecond();
+
+    // Cancel existing timers:
+    std::vector<TestStep*>::iterator it;
+    for (it = a_steps.begin(); it != a_steps.end(); it++) {
+      if ((*it)->getType() == TestStep::Type::Timeout) {
+        TestStepTimeout *step = (TestStepTimeout *)(*it);
+        step->cancelTimer();
+      }
+      else if ((*it)->getType() == TestStep::Type::Delay) {
+        TestStepDelay *step = (TestStepDelay *)(*it);
+        step->cancelTimer();
+      }
+    }
+
     if ((getState() == State::Failed) && (!testManager.getDumpFailedReports())) return;
     if ((getState() == State::Success) && (!testManager.getDumpSuccessReports())) return;
     // report file name: cycle-<cycle id>.testcase-<test case id>.xml
@@ -176,20 +224,28 @@ bool TestCase::done() throw() {
 
 bool TestCase::process() throw() {
   if (steps() == 0) {
-    LOGWARNING(anna::Logger::warning(anna::functions::asString("Test case %llu is empty, nothing to execute", a_id), ANNA_FILE_LOCATION));
+    LOGWARNING(anna::Logger::warning(anna::functions::asString("Test case %llu (%s) is empty, nothing to execute", a_id, a_description.c_str()), ANNA_FILE_LOCATION));
     return false;
   }
   if (isFinished()) {
-    LOGDEBUG(anna::Logger::debug(anna::functions::asString("Test case %llu is finished, nothing done until soft-reset", a_id), ANNA_FILE_LOCATION));
+    LOGDEBUG(anna::Logger::debug(anna::functions::asString("Test case %llu (%s) is finished, nothing done until soft-reset", a_id, a_description.c_str()), ANNA_FILE_LOCATION));
     return false;
   }
 
+  const char *literal = "PROCESS Test Case %llu/%llu [%s]";
+  TestManager& testManager (TestManager::instantiate ());
+  LOGDEBUG(anna::Logger::debug(anna::functions::asString(literal, getId(), testManager.tests(), getDescription().c_str()), ANNA_FILE_LOCATION));
+
   if (a_state == State::Initialized) {
+    if (testManager.getDumpStdout()) {
+      std::cout << std::endl << std::endl << anna::functions::asString(literal, getId(), testManager.tests(), getDescription().c_str()) << std::endl;
+    }
+
     a_stepsIt = a_steps.begin();
     setState(State::InProgress);
 
     // For 'wait' steps (not really useful, but better than nothing: begin timestamp on test case start timestamp...):
-    a_startTime = anna::functions::millisecond();
+    a_startTimestamp = anna::functions::millisecond();
   }
 
   // Check end of the test case:
@@ -224,7 +280,8 @@ bool TestCase::reset(bool hard) throw() {
     (*it)->reset();
 
   a_debugSummary.clear();
-  a_startTime = 0;
+  a_startTimestamp = 0;
+  a_finishTimestamp = 0;
   a_interactiveAmount = -1;
 
   setState(State::Initialized);
@@ -234,7 +291,7 @@ bool TestCase::reset(bool hard) throw() {
 
 void TestCase::assertInitialized() const throw(anna::RuntimeException) {
   if (isFinished())
-    throw anna::RuntimeException(anna::functions::asString("Cannot program anymore. The test case %llu has finished. You must reset it to append new steps (or do it during execution, which is also allowed).", a_id), ANNA_FILE_LOCATION);
+    throw anna::RuntimeException(anna::functions::asString("Cannot program anymore. The test case %llu (%s) has finished. You must reset it to append new steps (or do it during execution, which is also allowed).", a_id, a_description.c_str()), ANNA_FILE_LOCATION);
 }
 
 void TestCase::assertMessage(const anna::DataBlock &db, bool toEntity) throw(anna::RuntimeException) {
@@ -250,7 +307,7 @@ void TestCase::assertMessage(const anna::DataBlock &db, bool toEntity) throw(ann
   if (isRequest) {
     anna::diameter::HopByHop hbh = anna::diameter::codec::functions::getHopByHop(db);
     if (a_hopByHops.find(hbh) != a_hopByHops.end())
-      throw anna::RuntimeException(anna::functions::asString("Another request has been programmed with the same hop-by-hop (%llu) in this test case (%llu)", hbh, a_id), ANNA_FILE_LOCATION);
+      throw anna::RuntimeException(anna::functions::asString("Another request has been programmed with the same hop-by-hop (%llu) in this test case (%llu, '%s')", hbh, a_id, a_description.c_str()), ANNA_FILE_LOCATION);
     a_hopByHops[hbh] = NULL; // may be assigned to a wait condition
   }
 
@@ -275,50 +332,50 @@ void TestCase::addTimeout(const anna::Millisecond &timeout) throw(anna::RuntimeE
   addStep(step);
 }
 
-void TestCase::addSendxml2e(const anna::DataBlock &db, anna::diameter::comm::OriginHost *host, int stepNumber) throw(anna::RuntimeException) {
+void TestCase::addSendDiameterXml2e(const anna::DataBlock &db, anna::diameter::comm::OriginHost *host, int stepNumber) throw(anna::RuntimeException) {
   assertInitialized();
   assertMessage(db, true /* to entity */);
 
   if (stepNumber != -1) {
     const TestStep *stepReferred = getStep(stepNumber);
     if (!stepReferred)
-      throw anna::RuntimeException(anna::functions::asString("Step number (%d) do not exists (test case %llu)", stepNumber, a_id), ANNA_FILE_LOCATION);
+      throw anna::RuntimeException(anna::functions::asString("Step number (%d) do not exists (test case %llu, '%s')", stepNumber, a_id, a_description.c_str()), ANNA_FILE_LOCATION);
 
     if (stepReferred->getType() != TestStep::Type::Wait)
-      throw anna::RuntimeException(anna::functions::asString("Step number (%d) must refer to a 'wait' step (test case %llu)", stepNumber, a_id), ANNA_FILE_LOCATION);
+      throw anna::RuntimeException(anna::functions::asString("Step number (%d) must refer to a 'wait' step (test case %llu, '%s')", stepNumber, a_id, a_description.c_str()), ANNA_FILE_LOCATION);
 
-    const TestCondition &tc = (static_cast<const TestStepWait*>(stepReferred))->getCondition();
+    const TestDiameterCondition &tc = (static_cast<const TestStepWaitDiameter*>(stepReferred))->getCondition();
     if (tc.getCode() == "0") { // if regexp used, is not possible to detect this kind of errors
-      throw anna::RuntimeException(anna::functions::asString("Step number (%d) must refer to a 'wait for request' step (test case %llu)", stepNumber, a_id), ANNA_FILE_LOCATION);
+      throw anna::RuntimeException(anna::functions::asString("Step number (%d) must refer to a 'wait for request' step (test case %llu, '%s')", stepNumber, a_id, a_description.c_str()), ANNA_FILE_LOCATION);
     }
   }
 
-  TestStepSendxml2e *step = new TestStepSendxml2e(this);
+  TestStepSendDiameterXml2e *step = new TestStepSendDiameterXml2e(this);
   step->setMsgDataBlock(db);
   step->setOriginHost(host);
   step->setWaitForRequestStepNumber(stepNumber); // -1 means, no reference
   addStep(step);
 }
 
-void TestCase::addSendxml2c(const anna::DataBlock &db, anna::diameter::comm::OriginHost *host, int stepNumber) throw(anna::RuntimeException) {
+void TestCase::addSendDiameterXml2c(const anna::DataBlock &db, anna::diameter::comm::OriginHost *host, int stepNumber) throw(anna::RuntimeException) {
   assertInitialized();
   assertMessage(db, false /* to client */);
 
   if (stepNumber != -1) {
     const TestStep *stepReferred = getStep(stepNumber);
     if (!stepReferred)
-      throw anna::RuntimeException(anna::functions::asString("Step number (%d) do not exists (test case %llu)", stepNumber, a_id), ANNA_FILE_LOCATION);
+      throw anna::RuntimeException(anna::functions::asString("Step number (%d) do not exists (test case %llu, '%s')", stepNumber, a_id, a_description.c_str()), ANNA_FILE_LOCATION);
 
     if (stepReferred->getType() != TestStep::Type::Wait)
-      throw anna::RuntimeException(anna::functions::asString("Step number (%d) must refer to a 'wait' step (test case %llu)", stepNumber, a_id), ANNA_FILE_LOCATION);
+      throw anna::RuntimeException(anna::functions::asString("Step number (%d) must refer to a 'wait' step (test case %llu, '%s')", stepNumber, a_id, a_description.c_str()), ANNA_FILE_LOCATION);
 
-    const TestCondition &tc = (static_cast<const TestStepWait*>(stepReferred))->getCondition();
+    const TestDiameterCondition &tc = (static_cast<const TestStepWaitDiameter*>(stepReferred))->getCondition();
     if (tc.getCode() == "0") { // if regexp used, is not possible to detect this kind of errors
-      throw anna::RuntimeException(anna::functions::asString("Step number (%d) must refer to a 'wait for request' step (test case %llu)", stepNumber, a_id), ANNA_FILE_LOCATION);
+      throw anna::RuntimeException(anna::functions::asString("Step number (%d) must refer to a 'wait for request' step (test case %llu, '%s')", stepNumber, a_id, a_description.c_str()), ANNA_FILE_LOCATION);
     }
   }
 
-  TestStepSendxml2c *step = new TestStepSendxml2c(this);
+  TestStepSendDiameterXml2c *step = new TestStepSendDiameterXml2c(this);
   step->setMsgDataBlock(db);
   step->setOriginHost(host);
   step->setWaitForRequestStepNumber(stepNumber); // -1 means, no reference
@@ -332,84 +389,84 @@ void TestCase::addDelay(const anna::Millisecond &delay) throw(anna::RuntimeExcep
   addStep(step);
 }
 
-void TestCase::addWait(bool fromEntity,
+void TestCase::addWaitDiameter(bool fromEntity,
               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(anna::RuntimeException) {
   assertInitialized();
   std::string usedHopByHop = hopByHop;
-  TestStepWait *step = NULL;
+  TestStepWaitDiameter *step = NULL;
 
   // Check basic conditions:
   if (bitR == "1") {
     if (resultCode != "")
-      throw anna::RuntimeException(anna::functions::asString("You cannot specify Result-Code (%s) for a wait condition of a diameter request message (test case %llu)", resultCode.c_str(), a_id), ANNA_FILE_LOCATION);
+      throw anna::RuntimeException(anna::functions::asString("You cannot specify Result-Code (%s) for a wait condition of a diameter request message (test case %llu, '%s')", resultCode.c_str(), a_id, a_description.c_str()), ANNA_FILE_LOCATION);
     if (hopByHop != "")
-      throw anna::RuntimeException(anna::functions::asString("You cannot specify Hop-by-hop (%s) for a wait condition of a diameter request message (test case %llu)", hopByHop.c_str(), a_id), ANNA_FILE_LOCATION);
+      throw anna::RuntimeException(anna::functions::asString("You cannot specify Hop-by-hop (%s) for a wait condition of a diameter request message (test case %llu, '%s')", hopByHop.c_str(), a_id, a_description.c_str()), ANNA_FILE_LOCATION);
   }
   else {
     if (hopByHop != "") {
       if (hopByHop[0] == '#') {
         if (steps() == 0)
-          throw anna::RuntimeException(anna::functions::asString("No steps has been programmed, step reference is nonsense (test case %llu)", a_id), ANNA_FILE_LOCATION);
+          throw anna::RuntimeException(anna::functions::asString("No steps has been programmed, step reference is nonsense (test case %llu, '%s')", a_id, a_description.c_str()), ANNA_FILE_LOCATION);
 
         int stepNumber = atoi(hopByHop.substr(1).c_str());
 
         const TestStep *stepReferred = getStep(stepNumber);
         if (!stepReferred)
-          throw anna::RuntimeException(anna::functions::asString("Step reference number (%d) do not exists (test case %llu)", stepNumber, a_id), ANNA_FILE_LOCATION);
+          throw anna::RuntimeException(anna::functions::asString("Step reference number (%d) do not exists (test case %llu, '%s')", stepNumber, a_id, a_description.c_str()), ANNA_FILE_LOCATION);
 
         if (stepReferred->getType() != TestStep::Type::Sendxml2e && stepReferred->getType() != TestStep::Type::Sendxml2c)
-          throw anna::RuntimeException(anna::functions::asString("Step number must refer to a 'sendxml2e' or 'sendxml2c' step (test case %llu)", a_id), ANNA_FILE_LOCATION);
+          throw anna::RuntimeException(anna::functions::asString("Step number must refer to a 'sendxml2e' or 'sendxml2c' step (test case %llu, '%s')", a_id, a_description.c_str()), ANNA_FILE_LOCATION);
 
-        const anna::DataBlock &db = (static_cast<const TestStepSendxml*>(stepReferred))->getMsgDataBlock();
+        const anna::DataBlock &db = (static_cast<const TestStepSendDiameterXml*>(stepReferred))->getMsgDataBlock();
         bool isAnswer = anna::diameter::codec::functions::isAnswer(db);
         if (isAnswer)
-          throw anna::RuntimeException(anna::functions::asString("Step number must refer to a request message (test case %llu)", a_id), ANNA_FILE_LOCATION);
+          throw anna::RuntimeException(anna::functions::asString("Step number must refer to a request message (test case %llu, '%s')", a_id, a_description.c_str()), ANNA_FILE_LOCATION);
 
         // Hop-by-hop:
         anna::diameter::HopByHop hbh = anna::diameter::codec::functions::getHopByHop(db);
         usedHopByHop = anna::functions::asString(hbh);
-        step = new TestStepWait(this);
+        step = new TestStepWaitDiameter(this);
         a_hopByHops[hbh /* always exists: is the info we calculated above */] = step;
       }
     }
   }
 
-  if (!step) step = new TestStepWait(this);
+  if (!step) step = new TestStepWaitDiameter(this);
   step->setCondition(fromEntity, code, bitR, usedHopByHop, applicationId, sessionId, resultCode, msisdn, imsi, serviceContextId);
 
   LOGINFORMATION(
     if (hasSameCondition(step->getCondition()))
-      anna::Logger::information(anna::functions::asString("The same wait condition has already been programmed in this test case (%llu). Are you sure ?", a_id), ANNA_FILE_LOCATION);
+      anna::Logger::information(anna::functions::asString("The same wait condition has already been programmed in this test case (%llu, '%s'). Are you sure ?", a_id, a_description.c_str()), ANNA_FILE_LOCATION);
   );
 
   addStep(step);
 }
 
-void TestCase::addWaitRegexpHex(bool fromEntity, const std::string &regexp) throw(anna::RuntimeException) {
+void TestCase::addWaitDiameterRegexpHex(bool fromEntity, const std::string &regexp) throw(anna::RuntimeException) {
   assertInitialized();
 
-  TestStepWait *step = new TestStepWait(this);
+  TestStepWaitDiameter *step = new TestStepWaitDiameter(this);
   step->setConditionRegexpHex(fromEntity, regexp);
 
   LOGINFORMATION(
     if (hasSameCondition(step->getCondition()))
-      anna::Logger::information(anna::functions::asString("The same wait condition has already been programmed in this test case (%llu). Are you sure ?", a_id), ANNA_FILE_LOCATION);
+      anna::Logger::information(anna::functions::asString("The same wait condition has already been programmed in this test case (%llu, '%s'). Are you sure ?", a_id, a_description.c_str()), ANNA_FILE_LOCATION);
   );
 
   addStep(step);
 }
 
-void TestCase::addWaitRegexpXml(bool fromEntity, const std::string &regexp) throw(anna::RuntimeException) {
+void TestCase::addWaitDiameterRegexpXml(bool fromEntity, const std::string &regexp) throw(anna::RuntimeException) {
   assertInitialized();
 
-  TestStepWait *step = new TestStepWait(this);
+  TestStepWaitDiameter *step = new TestStepWaitDiameter(this);
   step->setConditionRegexpXml(fromEntity, regexp);
 
   LOGINFORMATION(
     if (hasSameCondition(step->getCondition()))
-      anna::Logger::information(anna::functions::asString("The same wait condition has already been programmed in this test case (%llu). Are you sure ?", a_id), ANNA_FILE_LOCATION);
+      anna::Logger::information(anna::functions::asString("The same wait condition has already been programmed in this test case (%llu, '%s'). Are you sure ?", a_id, a_description.c_str()), ANNA_FILE_LOCATION);
   );
 
   addStep(step);
@@ -424,13 +481,22 @@ void TestCase::addCommand(const std::string &cmd) throw(anna::RuntimeException)
   addStep(step);
 }
 
-TestStepWait *TestCase::searchNextWaitConditionFulfilled(const anna::DataBlock &message, bool waitFromEntity) throw() {
+void TestCase::addIpLimit(unsigned int ipLimit) throw(anna::RuntimeException) {
+  assertInitialized();
+
+  TestStepIpLimit *step = new TestStepIpLimit(this);
+  step->setIpLimit(ipLimit);
+
+  addStep(step);
+}
+
+TestStepWaitDiameter *TestCase::searchNextWaitConditionFulfilled(const anna::DataBlock &message, bool waitFromEntity) throw() {
 
-  TestStepWait *result;
+  TestStepWaitDiameter *result;
   for (std::vector<TestStep*>::const_iterator it = a_stepsIt /* current */; it != a_steps.end(); it++) {
     if ((*it)->getType() != TestStep::Type::Wait) continue;
     if ((*it)->isCompleted()) continue;
-    result = (TestStepWait*)(*it);
+    result = (TestStepWaitDiameter*)(*it);
     if ((result->getCondition().receivedFromEntity() == waitFromEntity) && (result->fulfilled(message)))
       return result;
   }