1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
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 //
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>
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>
31 ////////////////////////////////////////////////////////////////////////////////////////////////////////
33 ////////////////////////////////////////////////////////////////////////////////////////////////////////
34 void TestStep::initialize(TestCase *testCase) {
35 a_testCase = testCase;
37 a_type = Type::Unconfigured;
40 a_number = testCase->steps() + 1; // testCase is not NULL
43 const char* TestStep::asText(const Type::_v type)
45 static const char* text [] = { "Unconfigured", "Timeout", "Sendxml2e", "Sendxml2c", "Delay", "Wait" };
49 anna::xml::Node* TestStep::asXML(anna::xml::Node* parent) const
51 anna::xml::Node* result = parent->createChild("TestStep");
53 result->createAttribute("Number", a_number);
54 result->createAttribute("Type", asText(a_type));
55 result->createAttribute("Completed", (a_completed ? "yes":"no"));
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);
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);
72 std::string TestStep::asXMLString() const throw() {
73 anna::xml::Node root("root");
74 return anna::xml::Compiler().apply(asXML(&root));
77 bool TestStep::execute() throw() {
78 LOGDEBUG(anna::Logger::debug(anna::functions::asString("EXECUTING %s (step number %d) for Test Case %llu (%p) (%p)", asText(a_type), a_number, a_testCase->getId(), (TestCaseStep*)this, this), ANNA_FILE_LOCATION));
79 setBeginTimestamp(anna::functions::millisecond());
83 void TestStep::complete() throw() {
84 LOGDEBUG(anna::Logger::debug(anna::functions::asString("COMPLETE %s (step number %d) for Test Case %llu (%p) (%p)", asText(a_type), a_number, a_testCase->getId(), (TestCaseStep*)this, this), ANNA_FILE_LOCATION));
86 setEndTimestamp(anna::functions::millisecond());
90 void TestStep::reset() throw() {
91 LOGDEBUG(anna::Logger::debug(anna::functions::asString("RESET %s (step number %d) for Test Case %llu (%p) (%p)", asText(a_type), a_number, a_testCase->getId(), (TestCaseStep*)this, this), ANNA_FILE_LOCATION));
92 // type and testCase kept
99 void TestStep::next() throw() {
100 a_testCase->nextStep();
101 a_testCase->process();
105 ////////////////////////////////////////////////////////////////////////////////////////////////////////
107 ////////////////////////////////////////////////////////////////////////////////////////////////////////
108 anna::xml::Node* TestStepTimeout::asXML(anna::xml::Node* parent) const
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());
117 bool TestStepTimeout::do_execute() throw() {
119 a_timer = TestManager::instantiate().createTimer((TestCaseStep*)this, a_timeout, TestTimer::Type::TimeLeft);
121 catch (anna::RuntimeException &ex) {
123 a_testCase->addDebugSummaryHint(ex.asString()); // before report (when set Failed state)
124 a_testCase->setState(TestCase::State::Failed);
127 return true; // go next
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);
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);
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));
148 TestManager::instantiate().cancelTimer(a_timer);
150 catch (anna::RuntimeException &ex) {
154 //a_timeout = 0; THIS IS CONFIGURATION INFO
157 ////////////////////////////////////////////////////////////////////////////////////////////////////////
159 ////////////////////////////////////////////////////////////////////////////////////////////////////////
160 anna::xml::Node* TestStepSendxml::asXML(anna::xml::Node* parent) const
162 anna::xml::Node* result = TestStep::asXML(parent);
163 //parent->createChild("TestStepSendxml");
166 std::string msg = "", xmlmsg = "";
167 if (a_message.isEmpty()) {
171 msg = "\n"; msg += a_message.asString(); msg += "\n";
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";
179 catch (anna::RuntimeException &ex) {
184 result->createAttribute("Message", msg);
185 if (xmlmsg != "") result->createAttribute("XMLMessage", xmlmsg);
186 result->createAttribute("Expired", (a_expired ? "yes":"no"));
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;
197 // Update sequence for answers:
198 if (a_waitForRequestStepNumber != -1) { // is an answer: try to copy sequence information; alert about Session-Id discrepance
199 // Request which was received:
200 const TestStepWait *tsw = (const TestStepWait*)(a_testCase->getStep(a_waitForRequestStepNumber));
201 const anna::DataBlock &request = tsw->getMsgDataBlock();
202 anna::diameter::HopByHop hbh = anna::diameter::codec::functions::getHopByHop(request);
203 anna::diameter::EndToEnd ete = anna::diameter::codec::functions::getEndToEnd(request);
205 anna::diameter::codec::functions::setHopByHop(a_message, hbh);
206 anna::diameter::codec::functions::setEndToEnd(a_message, ete);
208 // Check Session-Id for warning ...
209 std::string sessionIdAnswer = anna::diameter::helpers::base::functions::getSessionId(a_message);
210 std::string sessionIdRequest = anna::diameter::helpers::base::functions::getSessionId(request);
211 if (sessionIdRequest != sessionIdAnswer) {
212 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());
213 LOGWARNING(anna::Logger::warning(s_warn, ANNA_FILE_LOCATION));
214 a_testCase->addDebugSummaryHint(s_warn);
218 if (getType() == Type::Sendxml2e) {
219 MyDiameterEntity *entity = a_realmNode->getEntity();
222 msg->setBody(a_message);
223 /* response = NULL =*/entity->send(msg);
227 failReason = "There is no diameter entity currently configured. Unable to send the message";
228 LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION));
231 else if (getType() == Type::Sendxml2c) {
232 MyLocalServer *localServer = a_realmNode->getDiameterServer();
235 msg->setBody(a_message);
236 /* response = NULL =*/localServer->send(msg);
240 failReason = "There is no diameter local server currently configured. Unable to send the message";
241 LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION));
244 } catch(anna::RuntimeException &ex) {
245 failReason = ex.asString();
249 a_realmNode->releaseCommMessage(msg);
252 a_testCase->addDebugSummaryHint(failReason); // before report (when set Failed state);
253 a_testCase->setState(TestCase::State::Failed);
259 return success; // go next if sent was OK
262 void TestStepSendxml::do_reset() throw() {
267 ////////////////////////////////////////////////////////////////////////////////////////////////////////
269 ////////////////////////////////////////////////////////////////////////////////////////////////////////
270 anna::xml::Node* TestStepDelay::asXML(anna::xml::Node* parent) const
272 anna::xml::Node* result = TestStep::asXML(parent);
273 //parent->createChild("TestStepDelay");
275 result->createAttribute("Delay", a_delay.asString());
280 bool TestStepDelay::do_execute() throw() {
282 a_timer = TestManager::instantiate().createTimer((TestCaseStep*)this, a_delay, TestTimer::Type::Delay);
284 catch (anna::RuntimeException &ex) {
286 a_testCase->addDebugSummaryHint(ex.asString()); // before report (when set Failed state)
287 a_testCase->setState(TestCase::State::Failed);
290 return false; // don't go next (wait complete)
293 void TestStepDelay::do_complete() throw() {
295 next(); // next() invoked here because execute() is always false for delay and never dvance the iterator
298 void TestStepDelay::do_reset() throw() {
300 TestManager::instantiate().cancelTimer(a_timer);
302 catch (anna::RuntimeException &ex) {
306 //a_delay = 0; THIS IS CONFIGURATION INFO
309 ////////////////////////////////////////////////////////////////////////////////////////////////////////
311 ////////////////////////////////////////////////////////////////////////////////////////////////////////
312 void TestStepWait::setCondition(bool fromEntity,
313 const std::string &code, const std::string &bitR, const std::string &resultCode, const std::string &sessionId,
314 const std::string &hopByHop, const std::string &msisdn, const std::string &imsi, const std::string &serviceContextId) throw() {
316 a_condition.setReceivedFromEntity(fromEntity);
317 a_condition.setCode(code);
318 a_condition.setBitR(bitR);
319 a_condition.setResultCode(resultCode);
320 a_condition.setSessionId(sessionId);
321 a_condition.setHopByHop(hopByHop);
322 a_condition.setMsisdn(msisdn);
323 a_condition.setImsi(imsi);
324 a_condition.setServiceContextId(serviceContextId);
327 void TestStepWait::setCondition(bool fromEntity, const std::string ®exp) throw() {
329 a_condition.setReceivedFromEntity(fromEntity);
330 a_condition.setRegexp(regexp);
333 anna::xml::Node* TestStepWait::asXML(anna::xml::Node* parent) const
335 anna::xml::Node* result = TestStep::asXML(parent);
336 //parent->createChild("TestStepWait");
339 a_condition.asXML(result);
341 if (!a_message.isEmpty()) {
343 result->createAttribute("MatchedMessage", a_message.asString());
346 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
347 static anna::diameter::codec::Message codecMsg(my_app.getCodecEngine());
348 codecMsg.decode(a_message);
349 result->createAttribute("MatchedXMLMessage", codecMsg.asXMLString());
351 catch (anna::RuntimeException &ex) {
359 bool TestStepWait::do_execute() throw() {
363 void TestStepWait::do_complete() throw() {
364 next(); // next() invoked here because execute() never do this.
367 bool TestStepWait::fulfilled(const anna::DataBlock &db/*, bool matchSessionId*/) throw() {
368 if (a_condition.comply(db/*, matchSessionId*/)) {
369 a_message = db; // store matched
377 void TestStepWait::do_reset() throw() {
379 a_clientSession = NULL;
380 a_serverSession = NULL;