Testing library separation: now not in launcher but isolated
[anna.git] / source / testing / TestCondition.cpp
diff --git a/source/testing/TestCondition.cpp b/source/testing/TestCondition.cpp
new file mode 100644 (file)
index 0000000..ba1db97
--- /dev/null
@@ -0,0 +1,182 @@
+// 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
+
+// Project
+#include <anna/testing/TestCondition.hpp>
+
+#include <anna/xml/Node.hpp>
+#include <anna/xml/Compiler.hpp>
+#include <anna/diameter/defines.hpp>
+#include <anna/diameter/codec/functions.hpp>
+#include <anna/diameter/codec/Message.hpp>
+#include <anna/diameter/helpers/base/functions.hpp>
+
+#include <anna/diameter/helpers/dcca/defines.hpp>
+#include <anna/diameter/helpers/dcca/functions.hpp>
+#include <anna/core/util/defines.hpp>
+
+
+using namespace anna::testing;
+
+
+const char* TestCondition::asText(const Type::_v type)
+throw() {
+  static const char* text [] = { "Generic", "Basic" };
+  return text [type];
+}
+
+bool TestCondition::exists() const throw() {
+  if (a_type == Type::Generic)
+    return (a_regexp != "");
+  else
+    return (a_code != "" || a_bitR != "" || a_hopByHop != "" || a_applicationId != "" || a_sessionId != "" || a_resultCode != "" || a_msisdn != "" || a_imsi != "" || a_serviceContextId != "");
+}
+
+/*
+bool anna::testing::operator==(const TestCondition &c1, const TestCondition &c2) throw() {
+
+  if (c1.getType() != c2.getType()) return false;
+
+  if (c1.getType() == TestCondition::Type::Generic) {
+    if (c1.getRegexp() != c2.getRegexp()) return false;
+  }
+  else {
+    if (c1.getCode() != c2.getCode()) return false;
+    if (c1.getBitR() != c2.getBitR()) return false;
+    if (c1.getHopByHop() != c2.getHopByHop()) return false;
+    if (c1.getApplicationId() != c2.getApplicationId()) return false;
+    if (c1.getSessionId() != c2.getSessionId()) return false;
+    if (c1.getResultCode() != c2.getResultCode()) return false;
+    if (c1.getMsisdn() != c2.getMsisdn()) return false;
+    if (c1.getImsi() != c2.getImsi()) return false;
+    if (c1.getServiceContextId() != c2.getServiceContextId()) return false;
+  }
+
+  return true;
+}
+*/
+
+bool TestCondition::comply(const anna::DataBlock &message/*, bool matchSessionId*/) const throw() {
+
+  if (a_type == Type::Generic) {
+    anna::diameter::codec::Message codecMsg;
+    try { codecMsg.decode(message); } catch (anna::RuntimeException &ex) { ex.trace(); }
+    return codecMsg.isLike(a_regexp);
+  }
+
+  // Basic
+  std::string compare;
+  anna::diameter::CommandId cid;
+
+  if (a_code != "" || a_bitR != "") {
+    try {
+      cid = anna::diameter::codec::functions::getCommandId(message);
+    }
+    catch (anna::RuntimeException &) { return false; }
+  }
+
+  if (a_code != "") {
+    compare = anna::functions::asString(cid.first);
+    if (a_code != compare) return false;
+  }
+
+  if (a_bitR != "") {
+    compare = (cid.second ? "1":"0");
+    if (a_bitR != compare) return false;
+  }
+
+  if (a_hopByHop != "") {
+    try {
+      anna::diameter::HopByHop h = anna::diameter::codec::functions::getHopByHop(message);
+      compare = anna::functions::asString(h);
+    }
+    catch (anna::RuntimeException &) { return false; }
+    if (a_hopByHop != compare) return false;
+  }
+
+  if (a_applicationId != "") {
+    try {
+      anna::diameter::ApplicationId a = anna::diameter::codec::functions::getApplicationId(message);
+      compare = anna::functions::asString(a);
+    }
+    catch (anna::RuntimeException &) { return false; }
+    if (a_applicationId != compare) return false;
+  }
+
+  //if (matchSessionId) {
+  if (a_sessionId != "") {
+    try {
+      compare = anna::diameter::helpers::base::functions::getSessionId(message);
+    }
+    catch (anna::RuntimeException &) { return false; }
+    if (a_sessionId != compare) return false;
+  }
+  //}
+
+  if (a_resultCode != "") {
+    try {
+      anna::U32 rc = anna::diameter::helpers::base::functions::getResultCode(message);
+      compare = anna::functions::asString(rc);
+    }
+    catch (anna::RuntimeException &) { return false; }
+    if (a_resultCode != compare) return false;
+  }
+
+  if (a_msisdn != "") {
+    try {
+      compare = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(message, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_E164);
+    }
+    catch (anna::RuntimeException &) { return false; }
+    if (a_msisdn != compare) return false;
+  }
+
+  if (a_imsi != "") {
+    try {
+      compare = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(message, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_IMSI);
+    }
+    catch (anna::RuntimeException &) { return false; }
+    if (a_imsi != compare) return false;
+  }
+
+  if (a_serviceContextId != "") {
+    try {
+      compare = anna::diameter::helpers::dcca::functions::getServiceContextId(message);
+    }
+    catch (anna::RuntimeException &) { return false; }
+    if (a_serviceContextId != compare) return false;
+  }
+
+  return true;
+}
+
+anna::xml::Node* TestCondition::asXML(anna::xml::Node* parent) const
+throw() {
+  anna::xml::Node* result = parent->createChild("TestCondition");
+  if (!exists()) return result;
+
+  if (a_type == Type::Generic) {
+    if (a_regexp != "") result->createAttribute("Regexp", a_regexp);
+  }
+  else {
+    if (a_code != "") result->createAttribute("Code", atoi(a_code.c_str()));
+    if (a_bitR != "") result->createAttribute("BitR", ((a_bitR == "1") ? "yes":"no"));
+    if (a_hopByHop != "") result->createAttribute("HopByHop", atoll(a_hopByHop.c_str()));
+    if (a_applicationId != "") result->createAttribute("ApplicationId", atoll(a_applicationId.c_str()));
+    if (a_sessionId != "") result->createAttribute("SessionId", a_sessionId);
+    if (a_resultCode != "") result->createAttribute("ResultCode", atoi(a_resultCode.c_str()));
+    if (a_msisdn != "") result->createAttribute("Msisdn", a_msisdn);
+    if (a_imsi != "") result->createAttribute("Imsi", a_imsi);
+    if (a_serviceContextId != "") result->createAttribute("ServiceContextId", a_serviceContextId);
+  }
+
+  result->createAttribute("ExpectedSource", a_rcvFromEntity ? "entity":"client");
+
+  return result;
+}