Dynamic lib selection and deployment
[anna.git] / example / diameter / launcher / testing / TestCase.cpp
index e95a670..087e29b 100644 (file)
@@ -11,8 +11,6 @@
 #include <fstream>
 #include <sstream>
 #include <cmath>
-#include <iterator>
-#include <vector>
 #include <iostream>
 
 // Project
@@ -30,6 +28,7 @@
 #include <TestManager.hpp>
 
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
 void TestCase::DebugSummary::addHint(const std::string &hint) throw() {
   event_t event;
   event.Timestamp = anna::functions::millisecond();
@@ -53,12 +52,24 @@ anna::xml::Node* TestCase::DebugSummary::asXML(anna::xml::Node* parent) const th
 
   return result;
 };
+///////////////////////////////////////////////////////////////////////////////////////////////////
 
 
+TestCase::TestCase(unsigned int id) :
+    a_id(id),
+    a_state(State::Initialized),
+    a_startTime(0),
+    a_interactiveAmount(-1) {
+
+  /*a_stepsIt = a_steps.end()*/;
+  TestManager &testManager = TestManager::instantiate();
+  testManager.tcsStateStats(State::Initialized, State::Initialized);
+}
+
 TestCase::~TestCase() {
   reset(true); // hard reset
-  std::map<int/* step number*/, TestStep*>::const_iterator it;
-  for (it = a_steps.begin(); it != a_steps.end(); it++) delete (it->second);
+  std::vector<TestStep*>::const_iterator it;
+  for (it = a_steps.begin(); it != a_steps.end(); it++) delete (*it);
 }
 
 const char* TestCase::asText(const State::_v state)
@@ -77,9 +88,9 @@ throw() {
   int steps = a_steps.size();
   if (steps != 0) {
     result->createAttribute("NumberOfTestSteps", steps);
-    std::map<int/* step number*/, TestStep*>::const_iterator it;
+    std::vector<TestStep*>::const_iterator it;
     for (it = a_steps.begin(); it != a_steps.end(); it++) {
-      it->second->asXML(result);
+      (*it)->asXML(result);
     }
   }
 
@@ -98,11 +109,11 @@ std::string TestCase::asXMLString() const throw() {
 }
 
 bool TestCase::hasSameCondition(const TestCondition &condition) const throw() {
-  std::map<int/* step number*/, TestStep*>::const_iterator it;
+  std::vector<TestStep*>::const_iterator it;
   TestStepWait *step;
   for (it = a_steps.begin(); it != a_steps.end(); it++) {
-    if (it->second->getType() != TestStep::Type::Wait) continue;
-    step = (TestStepWait *)(it->second);
+    if ((*it)->getType() != TestStep::Type::Wait) continue;
+    step = (TestStepWait *)(*it);
     if (step->getCondition() == condition) return true;
   }
   return false;
@@ -115,8 +126,14 @@ void TestCase::setState(const State::_v &state) throw() {
   if (state == previousState) return;
   a_state = state;
   TestManager &testManager = TestManager::instantiate();
+
+  // stats:
+  testManager.tcsStateStats(previousState, state);
+
+
   if (isFinished()) {
-    if (!testManager.getDumpReports()) return;
+    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
 
     // FORMAT: We tabulate the cycle and test case in order to ease ordering of files by mean ls:
@@ -142,14 +159,6 @@ void TestCase::setState(const State::_v &state) throw() {
       out.close();
     }
   }
-
-  // Count in-progress test cases:
-  if (inProgress()) {
-    testManager.setInProgressCountDelta(1);
-  }
-  else if (previousState == State::InProgress){
-    testManager.setInProgressCountDelta(-1);
-  }
 }
 
 bool TestCase::done() throw() {
@@ -162,7 +171,7 @@ bool TestCase::done() throw() {
 }
 
 bool TestCase::process() throw() {
-  if (a_steps.size() == 0) {
+  if (steps() == 0) {
     LOGWARNING(anna::Logger::warning(anna::functions::asString("Test case %llu is empty, nothing to execute", a_id), ANNA_FILE_LOCATION));
     return false;
   }
@@ -183,7 +192,7 @@ bool TestCase::process() throw() {
   if (done()) return false;
 
   bool somethingDone = false;
-  while (a_stepsIt->second->execute()) { // executes returns 'true' if the next step must be also executed (execute until can't stand no more)
+  while ((*a_stepsIt)->execute()) { // executes returns 'true' if the next step must be also executed (execute until can't stand no more)
     nextStep();
     // Check end of the test case:
     if (done()) return false;
@@ -200,9 +209,9 @@ bool TestCase::reset(bool hard) throw() {
 
   // Clean stage ////////////////////////////
   // id is kept
-  std::map<int/* step number*/, TestStep*>::iterator it;
+  std::vector<TestStep*>::iterator it;
   for (it = a_steps.begin(); it != a_steps.end(); it++)
-    it->second->reset();
+    (*it)->reset();
 
   a_debugSummary.clear();
   a_startTime = 0;
@@ -214,16 +223,18 @@ bool TestCase::reset(bool hard) throw() {
 }
 
 void TestCase::assertInitialized() const throw(anna::RuntimeException) {
-  if (a_state != State::Initialized)
-    throw anna::RuntimeException(anna::functions::asString("Cannot program anymore. The test case %llu was started. You must reset it to append new steps.", a_id), ANNA_FILE_LOCATION);
+  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);
 }
 
 void TestCase::assertMessage(const anna::DataBlock &db, bool toEntity) throw(anna::RuntimeException) {
 
   bool isRequest = anna::diameter::codec::functions::isRequest(db);
-  bool registerSessionId = ((isRequest && toEntity) || (!isRequest && !toEntity) /* (*) */);
-  // (*) we register answers Session-Id assuming that we will know the Session-Id values created by the client (OCS)
-  // This is another solution for TODO(***) regarding diameter server testing. No tsure about the final implementation.
+  bool registerKeys = ((isRequest && toEntity) || (!isRequest && !toEntity) /* (*) */);
+  // (*) we register answers Session-Id "assuming" that we will know the Session-Id values created by the client.
+  // This is another solution regarding diameter server testing. No sure about the final implementation.
+  // We will help registering also subscriber data, because certain messages (i.e. SLR) coming from clients could
+  //  have specific Session-Id value (unknown at test programming), and normally are identified by subscriber.
 
   // Check hop-by-hop:
   if (isRequest) {
@@ -233,8 +244,18 @@ void TestCase::assertMessage(const anna::DataBlock &db, bool toEntity) throw(ann
     a_hopByHops[hbh] = NULL; // may be assigned to a wait condition
   }
 
-  if (registerSessionId)
-    TestManager::instantiate().registerSessionId(anna::diameter::helpers::base::functions::getSessionId(db), this);
+  if (registerKeys) {
+    TestManager &testManager = TestManager::instantiate();
+    testManager.registerSessionId(anna::diameter::helpers::base::functions::getSessionId(db), this);
+
+
+    std::string subscriberId = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(db, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_E164);
+    if (subscriberId == "") // try with IMSI
+      subscriberId = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(db, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_IMSI);
+
+    if (subscriberId != "")
+      testManager.registerSubscriberId(subscriberId, this);
+  }
 }
 
 void TestCase::addTimeout(const anna::Millisecond &timeout) throw(anna::RuntimeException) {
@@ -244,7 +265,7 @@ void TestCase::addTimeout(const anna::Millisecond &timeout) throw(anna::RuntimeE
   addStep(step);
 }
 
-void TestCase::addSendxml2e(const anna::DataBlock &db, RealmNode *realm, int stepNumber) throw(anna::RuntimeException) {
+void TestCase::addSendxml2e(const anna::DataBlock &db, OriginHost *host, int stepNumber) throw(anna::RuntimeException) {
   assertInitialized();
   assertMessage(db, true /* to entity */);
 
@@ -264,18 +285,18 @@ void TestCase::addSendxml2e(const anna::DataBlock &db, RealmNode *realm, int ste
 
   TestStepSendxml2e *step = new TestStepSendxml2e(this);
   step->setMsgDataBlock(db);
-  step->setRealmNode(realm);
+  step->setOriginHost(host);
   step->setWaitForRequestStepNumber(stepNumber); // -1 means, no reference
   addStep(step);
 }
 
-void TestCase::addSendxml2c(const anna::DataBlock &db, RealmNode *realm, int stepNumber) throw(anna::RuntimeException) {
+void TestCase::addSendxml2c(const anna::DataBlock &db, OriginHost *host, int stepNumber) throw(anna::RuntimeException) {
   assertInitialized();
   assertMessage(db, false /* to client */);
 
   TestStepSendxml2c *step = new TestStepSendxml2c(this);
   step->setMsgDataBlock(db);
-  step->setRealmNode(realm);
+  step->setOriginHost(host);
   addStep(step);
 }
 
@@ -333,9 +354,9 @@ void TestCase::addWait(bool fromEntity,
   if (!step) step = new TestStepWait(this);
   step->setCondition(fromEntity, code, bitR, usedHopByHop, applicationId, sessionId, resultCode, msisdn, imsi, serviceContextId);
 
-  LOGWARNING(
+  LOGINFORMATION(
     if (hasSameCondition(step->getCondition()))
-      anna::Logger::warning(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). Are you sure ?", a_id), ANNA_FILE_LOCATION);
   );
 
   addStep(step);
@@ -347,9 +368,9 @@ void TestCase::addWaitRegexp(bool fromEntity, const std::string &regexp) throw(a
   TestStepWait *step = new TestStepWait(this);
   step->setCondition(fromEntity, regexp);
 
-  LOGWARNING(
+  LOGINFORMATION(
     if (hasSameCondition(step->getCondition()))
-      anna::Logger::warning(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). Are you sure ?", a_id), ANNA_FILE_LOCATION);
   );
 
   addStep(step);
@@ -367,10 +388,10 @@ void TestCase::addCommand(const std::string &cmd) throw(anna::RuntimeException)
 TestStepWait *TestCase::searchNextWaitConditionFulfilled(const anna::DataBlock &message, bool waitFromEntity) throw() {
 
   TestStepWait *result;
-  for (std::map<int/* step number*/, TestStep*>::const_iterator it = a_stepsIt /* current */; it != a_steps.end(); it++) {
-    if (it->second->getType() != TestStep::Type::Wait) continue;
-    if (it->second->isCompleted()) continue;
-    result = (TestStepWait*)(it->second);
+  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);
     if ((result->getCondition().receivedFromEntity() == waitFromEntity) && (result->fulfilled(message)))
       return result;
   }
@@ -379,7 +400,7 @@ TestStepWait *TestCase::searchNextWaitConditionFulfilled(const anna::DataBlock &
 }
 
 const TestStep *TestCase::getStep(int stepNumber) const throw() {
-  std::map<int/* step number*/, TestStep*>::const_iterator it = a_steps.find(stepNumber);
-  if (it != a_steps.end()) return it->second;
-  return NULL;
+  if (stepNumber < 1 || stepNumber > steps()) return NULL;
+//  return a_steps.at(stepNumber-1);  // http://stackoverflow.com/questions/3269809/stdvectorat-vs-operator-surprising-results-5-to-10-times-slower-f
+  return a_steps[stepNumber-1];
 }