Improved wait for regexp operations
[anna.git] / source / testing / TestCondition.cpp
index ba1db97..fcb3c08 100644 (file)
@@ -7,6 +7,7 @@
 
 
 // Standard
+#include <algorithm>
 
 // Project
 #include <anna/testing/TestCondition.hpp>
@@ -21,6 +22,7 @@
 #include <anna/diameter/helpers/dcca/defines.hpp>
 #include <anna/diameter/helpers/dcca/functions.hpp>
 #include <anna/core/util/defines.hpp>
+#include <anna/core/util/RegularExpression.hpp>
 
 
 using namespace anna::testing;
@@ -28,47 +30,36 @@ using namespace anna::testing;
 
 const char* TestCondition::asText(const Type::_v type)
 throw() {
-  static const char* text [] = { "Generic", "Basic" };
+  static const char* text [] = { "RegexpXml", "RegexpHex", "Fields" };
   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;
-  }
+  if (a_type != Type::Fields) return (getRegexp() != "");
 
-  return true;
+  return (a_code != "" || a_bitR != "" || a_hopByHop != "" || a_applicationId != "" || a_sessionId != "" || a_resultCode != "" || a_msisdn != "" || a_imsi != "" || a_serviceContextId != "");
 }
-*/
 
-bool TestCondition::comply(const anna::DataBlock &message/*, bool matchSessionId*/) const throw() {
+bool TestCondition::comply(const anna::DataBlock &message) const throw() {
 
-  if (a_type == Type::Generic) {
+  if (a_type == Type::RegexpXml) {
     anna::diameter::codec::Message codecMsg;
     try { codecMsg.decode(message); } catch (anna::RuntimeException &ex) { ex.trace(); }
-    return codecMsg.isLike(a_regexp);
+
+    //return codecMsg.isLike(getRegexp());
+    // We will remove LF from both sides to ease regexp management:
+    std::string regexp = getRegexp();
+    regexp.erase(std::remove(regexp.begin(), regexp.end(), '\n'), regexp.end());
+    anna::RegularExpression re(regexp);
+
+    std::string msgString = codecMsg.asXMLString();
+    msgString.erase(std::remove(msgString.begin(), msgString.end(), '\n'), msgString.end());
+
+    return re.isLike(msgString); 
+  }
+  else if (a_type == Type::RegexpHex) {
+    anna::RegularExpression re(getRegexp());
+    return re.isLike(anna::functions::asHexString(message));
   }
 
   // Basic
@@ -161,8 +152,11 @@ 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);
+  if (a_type == Type::RegexpXml) {
+    if (getRegexp() != "") result->createAttribute("RegexpXml", getRegexp());
+  }
+  else if (a_type == Type::RegexpHex) {
+    if (getRegexp() != "") result->createAttribute("RegexpHex", getRegexp());
   }
   else {
     if (a_code != "") result->createAttribute("Code", atoi(a_code.c_str()));