Testing library separation: now not in launcher but isolated
[anna.git] / source / testing / TestCase.cpp
diff --git a/source/testing/TestCase.cpp b/source/testing/TestCase.cpp
new file mode 100644 (file)
index 0000000..efaba5c
--- /dev/null
@@ -0,0 +1,410 @@
+// ANNA - Anna is Not Nothingness Anymore                                                         //
+//                                                                                                //
+// (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
+//                                                                                                //
+// See project site at http://redmine.teslayout.com/projects/anna-suite                           //
+// See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
+
+
+// Standard
+#include <string>
+#include <fstream>
+#include <sstream>
+#include <cmath>
+#include <iostream>
+
+// Project
+#include <anna/testing/TestCase.hpp>
+#include <anna/testing/TestStep.hpp>
+
+#include <anna/xml/Compiler.hpp>
+#include <anna/diameter/defines.hpp>
+#include <anna/diameter/helpers/dcca/defines.hpp>
+#include <anna/diameter/codec/functions.hpp>
+#include <anna/diameter.comm/OriginHost.hpp>
+#include <anna/diameter/helpers/base/functions.hpp>
+#include <anna/diameter/helpers/dcca/functions.hpp>
+#include <anna/core/util/Millisecond.hpp>
+#include <anna/core/tracing/Logger.hpp>
+#include <anna/testing/TestManager.hpp>
+
+
+using namespace anna::testing;
+
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+void TestCase::DebugSummary::addHint(const std::string &hint) throw() {
+  event_t event;
+  event.Timestamp = anna::functions::millisecond();
+  event.Hint = hint;
+  a_events.push_back(event);
+}
+
+void TestCase::DebugSummary::clear() throw() {
+  a_events.clear();
+}
+
+anna::xml::Node* TestCase::DebugSummary::asXML(anna::xml::Node* parent) const throw() {
+  anna::xml::Node* result = parent->createChild("DebugSummary");
+
+  std::vector<event_t>::const_iterator it;
+  for (it = a_events.begin(); it != a_events.end(); it++) {
+    anna::xml::Node* event = result->createChild("Event");
+    event->createAttribute("Timestamp", (*it).Timestamp.asString());
+    event->createAttribute("Hint", (*it).Hint);
+  }
+
+  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::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)
+throw() {
+  static const char* text [] = { "Initialized", "InProgress", "Failed", "Success" };
+  return text [state];
+}
+
+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("State", asText(a_state));
+  result->createAttribute("StartTimestamp", a_startTime.asString());
+  int steps = a_steps.size();
+  if (steps != 0) {
+    result->createAttribute("NumberOfTestSteps", steps);
+    std::vector<TestStep*>::const_iterator it;
+    for (it = a_steps.begin(); it != a_steps.end(); it++) {
+      (*it)->asXML(result);
+    }
+  }
+
+  if (a_debugSummary.events()) {
+    a_debugSummary.asXML(result);
+  }
+
+  result->createAttribute("Interactive", (a_interactiveAmount != -1) ? "yes":"no");
+
+  return result;
+}
+
+std::string TestCase::asXMLString() const throw() {
+  anna::xml::Node root("root");
+  return anna::xml::Compiler().apply(asXML(&root));
+}
+
+bool TestCase::hasSameCondition(const TestCondition &condition) const throw() {
+  std::vector<TestStep*>::const_iterator it;
+  TestStepWait *step;
+  for (it = a_steps.begin(); it != a_steps.end(); it++) {
+    if ((*it)->getType() != TestStep::Type::Wait) continue;
+    step = (TestStepWait *)(*it);
+    if (step->getCondition() == condition) return true;
+  }
+  return false;
+}
+
+
+void TestCase::setState(const State::_v &state) throw() {
+
+  State::_v previousState = a_state;
+  if (state == previousState) return;
+  a_state = state;
+  TestManager &testManager = TestManager::instantiate();
+
+  // stats:
+  testManager.tcsStateStats(previousState, state);
+
+
+  if (isFinished()) {
+    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:
+    int cycles = testManager.getPoolRepeats();
+    int tests = testManager.tests();
+    int cyclesWidth = (cycles<=0) ? 3 /* 1000 cycles !! */: ((int) log10 ((double) cycles) + 1);
+    int testsWidth = (tests<=0) ? 9 /* subscribers */: ((int) log10 ((double) tests) + 1);
+    std::stringstream format;
+    format << "/cycle-%0" << cyclesWidth << "d.testcase-%0" << testsWidth << "llu.xml";
+
+    // FILE NAME:
+    std::string file = testManager.getReportsDirectory() + anna::functions::asString(format.str().c_str(), testManager.getPoolCycle(), a_id);
+    std::ofstream out;
+    out.open(file.c_str(), std::ofstream::out | std::ofstream::app);
+    if(out.is_open() == false) {
+      std::string msg("Error opening '");
+      msg += file;
+      msg += "' for writting";
+      anna::Logger::error(msg, ANNA_FILE_LOCATION);
+    }
+    else {
+      out << asXMLString() << std::endl;
+      out.close();
+    }
+  }
+}
+
+bool TestCase::done() throw() {
+  if (a_stepsIt == a_steps.end()) {
+    setState(State::Success);
+    return true;
+  }
+
+  return false;
+}
+
+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));
+    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));
+    return false;
+  }
+
+  if (a_state == State::Initialized) {
+    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();
+  }
+
+  // Check end of the test case:
+  if (done()) return false;
+
+  bool somethingDone = false;
+  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;
+    somethingDone = true;
+  }
+
+  return somethingDone;
+}
+
+bool TestCase::reset(bool hard) throw() {
+
+  // Soft reset if finished:
+  if (!hard /* is soft reset */  && !isFinished()) return false;
+
+  // Clean stage ////////////////////////////
+  // id is kept
+  std::vector<TestStep*>::iterator it;
+  for (it = a_steps.begin(); it != a_steps.end(); it++)
+    (*it)->reset();
+
+  a_debugSummary.clear();
+  a_startTime = 0;
+  a_interactiveAmount = -1;
+
+  setState(State::Initialized);
+
+  return true;
+}
+
+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);
+}
+
+void TestCase::assertMessage(const anna::DataBlock &db, bool toEntity) throw(anna::RuntimeException) {
+
+  bool isRequest = anna::diameter::codec::functions::isRequest(db);
+  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) {
+    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);
+    a_hopByHops[hbh] = NULL; // may be assigned to a wait condition
+  }
+
+  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) {
+  assertInitialized();
+  TestStepTimeout *step = new TestStepTimeout(this);
+  step->setTimeout(timeout);
+  addStep(step);
+}
+
+void TestCase::addSendxml2e(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);
+
+    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);
+
+    const TestCondition &tc = (static_cast<const TestStepWait*>(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);
+    }
+  }
+
+  TestStepSendxml2e *step = new TestStepSendxml2e(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) {
+  assertInitialized();
+  assertMessage(db, false /* to client */);
+
+  TestStepSendxml2c *step = new TestStepSendxml2c(this);
+  step->setMsgDataBlock(db);
+  step->setOriginHost(host);
+  addStep(step);
+}
+
+void TestCase::addDelay(const anna::Millisecond &delay) throw(anna::RuntimeException) {
+  assertInitialized();
+  TestStepDelay *step = new TestStepDelay(this);
+  step->setDelay(delay);
+  addStep(step);
+}
+
+void TestCase::addWait(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;
+
+  // 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);
+    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);
+  }
+  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);
+
+        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);
+
+        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);
+
+        const anna::DataBlock &db = (static_cast<const TestStepSendxml*>(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);
+
+        // Hop-by-hop:
+        anna::diameter::HopByHop hbh = anna::diameter::codec::functions::getHopByHop(db);
+        usedHopByHop = anna::functions::asString(hbh);
+        step = new TestStepWait(this);
+        a_hopByHops[hbh /* always exists: is the info we calculated above */] = step;
+      }
+    }
+  }
+
+  if (!step) step = new TestStepWait(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);
+  );
+
+  addStep(step);
+}
+
+void TestCase::addWaitRegexp(bool fromEntity, const std::string &regexp) throw(anna::RuntimeException) {
+  assertInitialized();
+
+  TestStepWait *step = new TestStepWait(this);
+  step->setCondition(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);
+  );
+
+  addStep(step);
+}
+
+void TestCase::addCommand(const std::string &cmd) throw(anna::RuntimeException) {
+  assertInitialized();
+
+  TestStepCmd *step = new TestStepCmd(this);
+  step->setScript(cmd);
+
+  addStep(step);
+}
+
+TestStepWait *TestCase::searchNextWaitConditionFulfilled(const anna::DataBlock &message, bool waitFromEntity) throw() {
+
+  TestStepWait *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);
+    if ((result->getCondition().receivedFromEntity() == waitFromEntity) && (result->fulfilled(message)))
+      return result;
+  }
+
+  return NULL;
+}
+
+const TestStep *TestCase::getStep(int stepNumber) const throw() {
+  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];
+}