Testing library separation: now not in launcher but isolated
[anna.git] / source / testing / TestCondition.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 // Standard
10
11 // Project
12 #include <anna/testing/TestCondition.hpp>
13
14 #include <anna/xml/Node.hpp>
15 #include <anna/xml/Compiler.hpp>
16 #include <anna/diameter/defines.hpp>
17 #include <anna/diameter/codec/functions.hpp>
18 #include <anna/diameter/codec/Message.hpp>
19 #include <anna/diameter/helpers/base/functions.hpp>
20
21 #include <anna/diameter/helpers/dcca/defines.hpp>
22 #include <anna/diameter/helpers/dcca/functions.hpp>
23 #include <anna/core/util/defines.hpp>
24
25
26 using namespace anna::testing;
27
28
29 const char* TestCondition::asText(const Type::_v type)
30 throw() {
31   static const char* text [] = { "Generic", "Basic" };
32   return text [type];
33 }
34
35 bool TestCondition::exists() const throw() {
36   if (a_type == Type::Generic)
37     return (a_regexp != "");
38   else
39     return (a_code != "" || a_bitR != "" || a_hopByHop != "" || a_applicationId != "" || a_sessionId != "" || a_resultCode != "" || a_msisdn != "" || a_imsi != "" || a_serviceContextId != "");
40 }
41
42 /*
43 bool anna::testing::operator==(const TestCondition &c1, const TestCondition &c2) throw() {
44
45   if (c1.getType() != c2.getType()) return false;
46
47   if (c1.getType() == TestCondition::Type::Generic) {
48     if (c1.getRegexp() != c2.getRegexp()) return false;
49   }
50   else {
51     if (c1.getCode() != c2.getCode()) return false;
52     if (c1.getBitR() != c2.getBitR()) return false;
53     if (c1.getHopByHop() != c2.getHopByHop()) return false;
54     if (c1.getApplicationId() != c2.getApplicationId()) return false;
55     if (c1.getSessionId() != c2.getSessionId()) return false;
56     if (c1.getResultCode() != c2.getResultCode()) return false;
57     if (c1.getMsisdn() != c2.getMsisdn()) return false;
58     if (c1.getImsi() != c2.getImsi()) return false;
59     if (c1.getServiceContextId() != c2.getServiceContextId()) return false;
60   }
61
62   return true;
63 }
64 */
65
66 bool TestCondition::comply(const anna::DataBlock &message/*, bool matchSessionId*/) const throw() {
67
68   if (a_type == Type::Generic) {
69     anna::diameter::codec::Message codecMsg;
70     try { codecMsg.decode(message); } catch (anna::RuntimeException &ex) { ex.trace(); }
71     return codecMsg.isLike(a_regexp);
72   }
73
74   // Basic
75   std::string compare;
76   anna::diameter::CommandId cid;
77
78   if (a_code != "" || a_bitR != "") {
79     try {
80       cid = anna::diameter::codec::functions::getCommandId(message);
81     }
82     catch (anna::RuntimeException &) { return false; }
83   }
84
85   if (a_code != "") {
86     compare = anna::functions::asString(cid.first);
87     if (a_code != compare) return false;
88   }
89
90   if (a_bitR != "") {
91     compare = (cid.second ? "1":"0");
92     if (a_bitR != compare) return false;
93   }
94
95   if (a_hopByHop != "") {
96     try {
97       anna::diameter::HopByHop h = anna::diameter::codec::functions::getHopByHop(message);
98       compare = anna::functions::asString(h);
99     }
100     catch (anna::RuntimeException &) { return false; }
101     if (a_hopByHop != compare) return false;
102   }
103
104   if (a_applicationId != "") {
105     try {
106       anna::diameter::ApplicationId a = anna::diameter::codec::functions::getApplicationId(message);
107       compare = anna::functions::asString(a);
108     }
109     catch (anna::RuntimeException &) { return false; }
110     if (a_applicationId != compare) return false;
111   }
112
113   //if (matchSessionId) {
114   if (a_sessionId != "") {
115     try {
116       compare = anna::diameter::helpers::base::functions::getSessionId(message);
117     }
118     catch (anna::RuntimeException &) { return false; }
119     if (a_sessionId != compare) return false;
120   }
121   //}
122
123   if (a_resultCode != "") {
124     try {
125       anna::U32 rc = anna::diameter::helpers::base::functions::getResultCode(message);
126       compare = anna::functions::asString(rc);
127     }
128     catch (anna::RuntimeException &) { return false; }
129     if (a_resultCode != compare) return false;
130   }
131
132   if (a_msisdn != "") {
133     try {
134       compare = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(message, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_E164);
135     }
136     catch (anna::RuntimeException &) { return false; }
137     if (a_msisdn != compare) return false;
138   }
139
140   if (a_imsi != "") {
141     try {
142       compare = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(message, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_IMSI);
143     }
144     catch (anna::RuntimeException &) { return false; }
145     if (a_imsi != compare) return false;
146   }
147
148   if (a_serviceContextId != "") {
149     try {
150       compare = anna::diameter::helpers::dcca::functions::getServiceContextId(message);
151     }
152     catch (anna::RuntimeException &) { return false; }
153     if (a_serviceContextId != compare) return false;
154   }
155
156   return true;
157 }
158
159 anna::xml::Node* TestCondition::asXML(anna::xml::Node* parent) const
160 throw() {
161   anna::xml::Node* result = parent->createChild("TestCondition");
162   if (!exists()) return result;
163
164   if (a_type == Type::Generic) {
165     if (a_regexp != "") result->createAttribute("Regexp", a_regexp);
166   }
167   else {
168     if (a_code != "") result->createAttribute("Code", atoi(a_code.c_str()));
169     if (a_bitR != "") result->createAttribute("BitR", ((a_bitR == "1") ? "yes":"no"));
170     if (a_hopByHop != "") result->createAttribute("HopByHop", atoll(a_hopByHop.c_str()));
171     if (a_applicationId != "") result->createAttribute("ApplicationId", atoll(a_applicationId.c_str()));
172     if (a_sessionId != "") result->createAttribute("SessionId", a_sessionId);
173     if (a_resultCode != "") result->createAttribute("ResultCode", atoi(a_resultCode.c_str()));
174     if (a_msisdn != "") result->createAttribute("Msisdn", a_msisdn);
175     if (a_imsi != "") result->createAttribute("Imsi", a_imsi);
176     if (a_serviceContextId != "") result->createAttribute("ServiceContextId", a_serviceContextId);
177   }
178
179   result->createAttribute("ExpectedSource", a_rcvFromEntity ? "entity":"client");
180
181   return result;
182 }