System test feature
[anna.git] / example / diameter / launcher / testing / TestCondition.cpp
diff --git a/example/diameter/launcher/testing/TestCondition.cpp b/example/diameter/launcher/testing/TestCondition.cpp
new file mode 100644 (file)
index 0000000..a772ab0
--- /dev/null
@@ -0,0 +1,174 @@
+// 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/xml/Node.hpp>
+#include <anna/xml/Compiler.hpp>
+#include <anna/diameter/defines.hpp>
+#include <anna/diameter/codec/functions.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>
+
+// Process
+#include <TestCondition.hpp>
+#include <Launcher.hpp>
+
+
+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_resultCode != "" || a_sessionId != "" || a_hopByHop != "" || a_msisdn != "" || a_imsi != "" || a_serviceContextId != "");
+}
+
+bool operator==(const TestCondition &c1, const TestCondition &c2) throw() {
+
+  if (c1.a_type != c2.a_type) return false;
+
+  if (c1.a_type == TestCondition::Type::Generic) {
+    if (c1.a_regexp != c2.a_regexp) return false;
+  }
+  else {
+    if (c1.a_code != c2.a_code) return false;
+    if (c1.a_bitR != c2.a_bitR) return false;
+    if (c1.a_resultCode != c2.a_resultCode) return false;
+    if (c1.a_sessionId != c2.a_sessionId) return false;
+    if (c1.a_hopByHop != c2.a_hopByHop) return false;
+    if (c1.a_msisdn != c2.a_msisdn) return false;
+    if (c1.a_imsi != c2.a_imsi) return false;
+    if (c1.a_serviceContextId != c2.a_serviceContextId) return false;
+  }
+
+  return true;
+}
+
+bool TestCondition::comply(const anna::DataBlock &message/*, bool matchSessionId*/) const throw() {
+
+  if (a_type == Type::Generic) {
+    Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+    static anna::diameter::codec::Message codecMsg(my_app.getCodecEngine());
+    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.first ? "1":"0");
+    if (a_bitR != 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 (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_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_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_sessionId != "") result->createAttribute("SessionId", a_sessionId);
+    if (a_resultCode != "") result->createAttribute("ResultCode", atoi(a_resultCode.c_str()));
+    if (a_hopByHop != "") result->createAttribute("HopByHop", atoll(a_hopByHop.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;
+}