076a227d50fb7f19e3e626f8613148a2e5e8afd5
[anna.git] / example / diameter / launcher / testing / TestStep.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 #include <string>
11
12 // Project
13 #include <anna/xml/Compiler.hpp>
14 #include <anna/core/util/Millisecond.hpp>
15 #include <anna/diameter.comm/Message.hpp>
16 #include <anna/core/tracing/Logger.hpp>
17 #include <anna/diameter/codec/functions.hpp>
18 #include <anna/diameter/helpers/base/functions.hpp>
19
20 // Process
21 #include <RealmNode.hpp>
22 #include <MyDiameterEntity.hpp>
23 #include <MyLocalServer.hpp>
24 #include <TestStep.hpp>
25 #include <Launcher.hpp>
26 #include <TestCase.hpp>
27 #include <TestManager.hpp>
28 #include <TestTimer.hpp>
29
30
31 ////////////////////////////////////////////////////////////////////////////////////////////////////////
32 // TestStep
33 ////////////////////////////////////////////////////////////////////////////////////////////////////////
34 void TestStep::initialize(TestCase *testCase) {
35   a_testCase = testCase;
36   a_completed = false;
37   a_type = Type::Unconfigured;
38   a_beginTimestamp = 0;
39   a_endTimestamp = 0;
40   a_number = testCase->steps() + 1; // testCase is not NULL
41 }
42
43 const char* TestStep::asText(const Type::_v type)
44 throw() {
45   static const char* text [] = { "Unconfigured", "Timeout", "Sendxml2e", "Sendxml2c", "Delay", "Wait" };
46   return text [type];
47 }
48
49 anna::xml::Node* TestStep::asXML(anna::xml::Node* parent) const
50 throw() {
51   anna::xml::Node* result = parent->createChild("TestStep");
52
53   result->createAttribute("Number", a_number);
54   result->createAttribute("Type", asText(a_type));
55   result->createAttribute("Completed", (a_completed ? "yes":"no"));
56
57   // Begin
58   std::string s_aux = a_beginTimestamp.asString();
59 //  int deltaMs = (int)(a_beginTimestamp - a_testCase->getStartTimestamp());
60 //  if (a_beginTimestamp != 0 && deltaMs > 0) s_aux += anna::functions::asString(" [%.3f]", deltaMs/1000.0);
61   result->createAttribute("BeginTimestamp", s_aux);
62
63   // End
64   s_aux = a_endTimestamp.asString();
65 //  deltaMs = (int)(a_endTimestamp - a_testCase->getStartTimestamp());
66 //  if (a_endTimestamp != 0 && deltaMs > 0) s_aux += anna::functions::asString(" [%.3f]", deltaMs/1000.0);
67   result->createAttribute("EndTimestamp", s_aux);
68
69   return result;
70 }
71
72 std::string TestStep::asXMLString() const throw() {
73   anna::xml::Node root("root");
74   return anna::xml::Compiler().apply(asXML(&root));
75 }
76
77 bool TestStep::execute() throw() {
78   LOGDEBUG(anna::Logger::debug(anna::functions::asString("EXECUTING %s for Test Case %llu (%p) (%p)", asText(a_type), a_testCase->getId(), (TestCaseStep*)this, this), ANNA_FILE_LOCATION));
79   setBeginTimestamp(anna::functions::millisecond());
80   return do_execute();
81 }
82
83 void TestStep::complete() throw() {
84   LOGDEBUG(anna::Logger::debug(anna::functions::asString("COMPLETE %s for Test Case %llu (%p) (%p)", asText(a_type), a_testCase->getId(), (TestCaseStep*)this, this), ANNA_FILE_LOCATION));
85   a_completed = true;
86   setEndTimestamp(anna::functions::millisecond());
87   do_complete();
88 }
89
90 void TestStep::reset() throw() {
91   LOGDEBUG(anna::Logger::debug(anna::functions::asString("RESET %s for Test Case %llu (%p) (%p)", asText(a_type), a_testCase->getId(), (TestCaseStep*)this, this), ANNA_FILE_LOCATION));
92   // type and testCase kept
93   a_completed = false;
94   a_beginTimestamp = 0;
95   a_endTimestamp = 0;
96   do_reset();
97 }
98
99 void TestStep::next() throw() {
100   a_testCase->nextStep();
101   a_testCase->process();
102 }
103
104
105 ////////////////////////////////////////////////////////////////////////////////////////////////////////
106 // TestStepTimeout
107 ////////////////////////////////////////////////////////////////////////////////////////////////////////
108 anna::xml::Node* TestStepTimeout::asXML(anna::xml::Node* parent) const
109 throw() {
110   anna::xml::Node* result = TestStep::asXML(parent); // end timestamp will be 0 if test finished OK
111   //parent->createChild("TestStepTimeout");
112   result->createAttribute("Timeout", a_timeout.asString());
113
114   return result;
115 }
116
117 bool TestStepTimeout::do_execute() throw() {
118   try {
119     a_timer = TestManager::instantiate().createTimer((TestCaseStep*)this, a_timeout, TestTimer::Type::TimeLeft);
120   }
121   catch (anna::RuntimeException &ex) {
122     ex.trace();
123     a_testCase->addDebugSummaryHint(ex.asString()); // before report (when set Failed state)
124     a_testCase->setState(TestCase::State::Failed);
125   }
126
127   return true; // go next
128 }
129
130 void TestStepTimeout::do_complete() throw() {
131   int stepNumber = getNumber();
132   if (stepNumber == a_testCase->steps()) {
133     a_testCase->addDebugSummaryHint(anna::functions::asString("Timeout expired (step number %d) but it was the last test case step", stepNumber)); // before report (when set Failed state)
134     a_testCase->setState(TestCase::State::Success);
135   }
136   else if (a_testCase->getState() == TestCase::State::InProgress) { // sure
137     a_testCase->addDebugSummaryHint(anna::functions::asString("Timeout expired (step number %d) before test case finished", stepNumber)); // before report (when set Failed state)
138     a_testCase->setState(TestCase::State::Failed);
139   }
140
141   a_timer = NULL;
142 }
143
144 void TestStepTimeout::do_reset() throw() {
145   LOGDEBUG(anna::Logger::debug(anna::functions::asString("REXXXXXET %s for Test Case %llu (%p) (%p) (a_timer %p)", asText(a_type), a_testCase->getId(), (TestCaseStep*)this, this, a_timer), ANNA_FILE_LOCATION));
146
147   try {
148     TestManager::instantiate().cancelTimer(a_timer);
149   }
150   catch (anna::RuntimeException &ex) {
151     ex.trace();
152   }
153   a_timer = NULL;
154   //a_timeout = 0; THIS IS CONFIGURATION INFO
155 }
156
157 ////////////////////////////////////////////////////////////////////////////////////////////////////////
158 // TestStepSendxml
159 ////////////////////////////////////////////////////////////////////////////////////////////////////////
160 anna::xml::Node* TestStepSendxml::asXML(anna::xml::Node* parent) const
161 throw() {
162   anna::xml::Node* result = TestStep::asXML(parent);
163   //parent->createChild("TestStepSendxml");
164
165   // Message
166   std::string msg = "", xmlmsg = "";
167   if (a_message.isEmpty()) {
168     msg = "<empty>";
169   }
170   else {
171     msg = "\n"; msg += a_message.asString(); msg += "\n";
172     // Helper
173     try {
174       Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
175       static anna::diameter::codec::Message codecMsg(my_app.getCodecEngine());
176       codecMsg.decode(a_message);
177       xmlmsg = "\n"; xmlmsg += codecMsg.asXMLString(); xmlmsg += "\n";
178     }
179     catch (anna::RuntimeException &ex) {
180       ex.trace();
181     }
182   }
183
184   result->createAttribute("Message", msg);
185   if (xmlmsg != "") result->createAttribute("XMLMessage", xmlmsg);
186   result->createAttribute("Expired", (a_expired ? "yes":"no"));
187
188   return result;
189 }
190
191 bool TestStepSendxml::do_execute() throw() {
192   anna::diameter::comm::Message *msg = a_realmNode->createCommMessage();
193   bool success = false;
194   std::string failReason, s_warn;
195
196   // Update sequence for answers:
197   if (a_waitForRequestStepNumber != -1) { // is an answer: try to copy sequence information; alert about Session-Id discrepance
198     // Request which was received:
199     const TestStepWait *tsw = (const TestStepWait*)(a_testCase->getStep(a_waitForRequestStepNumber));
200     const anna::DataBlock &request = tsw->getMsgDataBlock();
201     anna::diameter::HopByHop hbh = anna::diameter::codec::functions::getHopByHop(request);
202     anna::diameter::EndToEnd ete = anna::diameter::codec::functions::getEndToEnd(request);
203     // Update sequence:
204     anna::diameter::codec::functions::setHopByHop(a_message, hbh);
205     anna::diameter::codec::functions::setEndToEnd(a_message, ete);
206
207     // Check Session-Id for warning ...
208     std::string sessionIdAnswer = anna::diameter::helpers::base::functions::getSessionId(a_message);
209     std::string sessionIdRequest = anna::diameter::helpers::base::functions::getSessionId(request);
210     if (sessionIdRequest != sessionIdAnswer) {
211       s_warn = anna::functions::asString("Sending an answer which Session-Id (%s) is different than supposed corresponding request (%s)", sessionIdAnswer.c_str(), sessionIdRequest.c_str());
212       LOGWARNING(anna::Logger::warning(s_warn, ANNA_FILE_LOCATION));
213       a_testCase->addDebugSummaryHint(s_warn);
214     }
215   }
216
217   if (getType() == Type::Sendxml2e) {
218     MyDiameterEntity *entity = a_realmNode->getEntity();
219     if (entity) {
220       try {
221         //msg->clearBody();
222         msg->setBody(a_message);
223         /* response = NULL =*/entity->send(msg);
224         success = true;
225       } catch(anna::RuntimeException &ex) {
226         ex.trace();
227         failReason = ex.asString();
228       }
229     }
230     else {
231       failReason = "There is no diameter entity currently configured. Unable to send the message";
232       LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION));
233     }
234   }
235   else if (getType() == Type::Sendxml2c) {
236     MyLocalServer *localServer = a_realmNode->getDiameterServer();
237     if (localServer) {
238       try {
239         //msg->clearBody();
240         msg->setBody(a_message);
241         /* response = NULL =*/localServer->send(msg);
242         success = true;
243       } catch(anna::RuntimeException &ex) {
244         ex.trace();
245         failReason = ex.asString();
246       }
247     }
248     else {
249       failReason = "There is no diameter local server currently configured. Unable to send the message";
250       LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION));
251     }
252   }
253
254   // release msg
255   a_realmNode->releaseCommMessage(msg);
256
257   if (!success) {
258     a_testCase->addDebugSummaryHint(failReason); // before report (when set Failed state);
259     a_testCase->setState(TestCase::State::Failed);
260   }
261   else {
262     complete();
263   }
264
265   return success; // go next if sent was OK
266 }
267
268 void TestStepSendxml::do_complete() throw() {
269   next();
270 }
271
272 void TestStepSendxml::do_reset() throw() {
273   a_expired = false;
274   //a_message.clear();
275 }
276
277 ////////////////////////////////////////////////////////////////////////////////////////////////////////
278 // TestStepDelay
279 ////////////////////////////////////////////////////////////////////////////////////////////////////////
280 anna::xml::Node* TestStepDelay::asXML(anna::xml::Node* parent) const
281 throw() {
282   anna::xml::Node* result = TestStep::asXML(parent);
283   //parent->createChild("TestStepDelay");
284
285   result->createAttribute("Delay", a_delay.asString());
286
287   return result;
288 }
289
290 bool TestStepDelay::do_execute() throw() {
291   try {
292     a_timer = TestManager::instantiate().createTimer((TestCaseStep*)this, a_delay, TestTimer::Type::Delay);
293   }
294   catch (anna::RuntimeException &ex) {
295     ex.trace();
296     a_testCase->addDebugSummaryHint(ex.asString()); // before report (when set Failed state)
297     a_testCase->setState(TestCase::State::Failed);
298   }
299
300   return false; // don't go next (wait complete)
301 }
302
303 void TestStepDelay::do_complete() throw() {
304   a_timer = NULL;
305   next();
306 }
307
308 void TestStepDelay::do_reset() throw() {
309   try {
310     TestManager::instantiate().cancelTimer(a_timer);
311   }
312   catch (anna::RuntimeException &ex) {
313     ex.trace();
314   }
315   a_timer = NULL;
316   //a_delay = 0; THIS IS CONFIGURATION INFO
317 }
318
319 ////////////////////////////////////////////////////////////////////////////////////////////////////////
320 // TestStepWait
321 ////////////////////////////////////////////////////////////////////////////////////////////////////////
322 void TestStepWait::setCondition(bool fromEntity,
323                                   const std::string &code, const std::string &bitR, const std::string &resultCode, const std::string &sessionId,
324                                   const std::string &hopByHop, const std::string &msisdn, const std::string &imsi, const std::string &serviceContextId) throw() {
325
326   a_condition.setReceivedFromEntity(fromEntity);
327   a_condition.setCode(code);
328   a_condition.setBitR(bitR);
329   a_condition.setResultCode(resultCode);
330   a_condition.setSessionId(sessionId);
331   a_condition.setHopByHop(hopByHop);
332   a_condition.setMsisdn(msisdn);
333   a_condition.setImsi(imsi);
334   a_condition.setServiceContextId(serviceContextId);
335 }
336
337 void TestStepWait::setCondition(bool fromEntity, const std::string &regexp) throw() {
338
339   a_condition.setReceivedFromEntity(fromEntity);
340   a_condition.setRegexp(regexp);
341 }
342
343 anna::xml::Node* TestStepWait::asXML(anna::xml::Node* parent) const
344 throw() {
345   anna::xml::Node* result = TestStep::asXML(parent);
346   //parent->createChild("TestStepWait");
347
348   // Condition
349   a_condition.asXML(result);
350
351   if (!a_message.isEmpty()) {
352     // Message
353     result->createAttribute("MatchedMessage", a_message.asString());
354     // Helper
355     try {
356       Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
357       static anna::diameter::codec::Message codecMsg(my_app.getCodecEngine());
358       codecMsg.decode(a_message);
359       result->createAttribute("MatchedXMLMessage", codecMsg.asXMLString());
360     }
361     catch (anna::RuntimeException &ex) {
362       ex.trace();
363     }
364   }
365
366   return result;
367 }
368
369 bool TestStepWait::do_execute() throw() {
370   return a_completed;
371 }
372
373 void TestStepWait::do_complete() throw() {
374   next();
375 }
376
377 bool TestStepWait::fulfilled(const anna::DataBlock &db/*, bool matchSessionId*/) throw() {
378   if (a_condition.comply(db/*, matchSessionId*/)) {
379     a_message = db; // store matched
380     complete();
381     return true;
382   }
383
384   return false;
385 }
386
387 void TestStepWait::do_reset() throw() {
388   a_message.clear();
389   a_clientSession = NULL;
390   a_serverSession = NULL;
391 }
392