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 //
14 #include <anna/xml/Compiler.hpp>
15 #include <anna/diameter/defines.hpp>
16 #include <anna/diameter/helpers/dcca/defines.hpp>
17 #include <anna/diameter/codec/functions.hpp>
18 #include <anna/diameter/helpers/base/functions.hpp>
19 #include <anna/diameter/helpers/dcca/functions.hpp>
20 #include <anna/core/util/Millisecond.hpp>
21 #include <anna/core/tracing/Logger.hpp>
24 #include <TestCase.hpp>
25 #include <TestManager.hpp>
28 void TestCase::DebugSummary::addHint(const std::string &hint) throw() {
30 event.Timestamp = anna::functions::millisecond();
32 a_events.push_back(event);
35 void TestCase::DebugSummary::clear() throw() {
39 anna::xml::Node* TestCase::DebugSummary::asXML(anna::xml::Node* parent) const throw() {
40 anna::xml::Node* result = parent->createChild("DebugSummary");
42 std::vector<event_t>::const_iterator it;
43 for (it = a_events.begin(); it != a_events.end(); it++) {
44 anna::xml::Node* event = result->createChild("Event");
45 event->createAttribute("Timestamp", (*it).Timestamp.asString());
46 event->createAttribute("Hint", (*it).Hint);
53 TestCase::~TestCase() {
54 reset(true); // hard reset
55 std::vector<TestStep*>::const_iterator it;
56 for (it = a_steps.begin(); it != a_steps.end(); it++) delete (*it);
59 const char* TestCase::asText(const State::_v state)
61 static const char* text [] = { "Initialized", "InProgress", "Failed", "Success" };
65 anna::xml::Node* TestCase::asXML(anna::xml::Node* parent) const
67 anna::xml::Node* result = parent->createChild("TestCase");
69 result->createAttribute("Id", a_id);
70 result->createAttribute("State", asText(a_state));
71 result->createAttribute("StartTimestamp", a_startTime.asString());
72 int steps = a_steps.size();
74 result->createAttribute("NumberOfTestSteps", steps);
75 std::vector<TestStep*>::const_iterator it;
76 for (it = a_steps.begin(); it != a_steps.end(); it++) {
81 if (a_debugSummary.events()) {
82 a_debugSummary.asXML(result);
88 std::string TestCase::asXMLString() const throw() {
89 anna::xml::Node root("root");
90 return anna::xml::Compiler().apply(asXML(&root));
93 bool TestCase::hasSameCondition(const TestCondition &condition) const throw() {
94 std::vector<TestStep*>::const_iterator it;
96 for (it = a_steps.begin(); it != a_steps.end(); it++) {
97 if ((*it)->getType() != TestStep::Type::Wait) continue;
98 step = (TestStepWait *)(*it);
99 if (step->getCondition() == condition) return true;
105 void TestCase::setState(const State::_v &state) throw() {
107 State::_v previousState = a_state;
108 if (state == previousState) return;
110 TestManager &testManager = TestManager::instantiate();
112 if (!testManager.getDumpReports()) return;
113 // report file name: cycle-<cycle id>.testcase-<test case id>.xml
114 std::string file = testManager.getReportsDirectory() + anna::functions::asString("/cycle-%d.testcase-%llu.xml", testManager.getPoolCycle(), a_id);
116 out.open(file.c_str(), std::ofstream::out | std::ofstream::app);
117 if(out.is_open() == false) {
118 std::string msg("Error opening '");
120 msg += "' for writting";
121 anna::Logger::error(msg, ANNA_FILE_LOCATION);
124 out << asXMLString() << std::endl;
129 // Count in-progress test cases:
131 testManager.setInProgressCountDelta(1);
133 else if (previousState == State::InProgress){
134 testManager.setInProgressCountDelta(-1);
138 bool TestCase::done() throw() {
139 if (a_stepsIt == a_steps.end()) {
140 setState(State::Success);
147 bool TestCase::process() throw() {
148 if (a_steps.size() == 0) {
149 LOGWARNING(anna::Logger::warning(anna::functions::asString("Test case %llu is empty, nothing to execute", a_id), ANNA_FILE_LOCATION));
153 LOGDEBUG(anna::Logger::debug(anna::functions::asString("Test case %llu is finished, nothing done until soft-reset", a_id), ANNA_FILE_LOCATION));
157 if (a_state == State::Initialized) {
158 a_stepsIt = a_steps.begin();
159 setState(State::InProgress);
161 // For 'wait' steps (not really useful, but better than nothing: begin timestamp on test case start timestamp...):
162 a_startTime = anna::functions::millisecond();
165 // Check end of the test case:
166 if (done()) return false;
168 bool somethingDone = false;
169 while ((*a_stepsIt)->execute()) { // executes returns 'true' if the next step must be also executed (execute until can't stand no more)
171 // Check end of the test case:
172 if (done()) return false;
173 somethingDone = true;
176 return somethingDone;
179 bool TestCase::reset(bool hard) throw() {
181 // Soft reset if finished:
182 if (!hard /* is soft reset */ && !isFinished()) return false;
184 // Clean stage ////////////////////////////
186 std::vector<TestStep*>::iterator it;
187 for (it = a_steps.begin(); it != a_steps.end(); it++)
190 a_debugSummary.clear();
193 setState(State::Initialized);
198 void TestCase::assertInitialized() const throw(anna::RuntimeException) {
199 if (a_state != State::Initialized)
200 throw anna::RuntimeException(anna::functions::asString("Cannot program anymore. The test case %llu was started. You must reset it to append new steps.", a_id), ANNA_FILE_LOCATION);
203 void TestCase::assertMessage(const anna::DataBlock &db, bool toEntity) throw(anna::RuntimeException) {
205 bool isRequest = anna::diameter::codec::functions::isRequest(db);
206 bool registerSessionId = ((isRequest && toEntity) || (!isRequest && !toEntity) /* (*) */);
207 // (*) we register answers Session-Id assuming that we will know the Session-Id values created by the client (OCS)
208 // This is another solution for TODO(***) regarding diameter server testing. No tsure about the final implementation.
212 anna::diameter::HopByHop hbh = anna::diameter::codec::functions::getHopByHop(db);
213 if (a_hopByHops.find(hbh) != a_hopByHops.end())
214 throw anna::RuntimeException(anna::functions::asString("Another request has been programmed with the same hop-by-hop (%llu) in this test case (%llu)", hbh, a_id), ANNA_FILE_LOCATION);
215 a_hopByHops[hbh] = NULL; // may be assigned to a wait condition
218 if (registerSessionId)
219 TestManager::instantiate().registerSessionId(anna::diameter::helpers::base::functions::getSessionId(db), this);
222 void TestCase::addTimeout(const anna::Millisecond &timeout) throw(anna::RuntimeException) {
224 TestStepTimeout *step = new TestStepTimeout(this);
225 step->setTimeout(timeout);
226 a_steps.push_back(step);
229 void TestCase::addSendxml2e(const anna::DataBlock &db, RealmNode *realm, int stepNumber) throw(anna::RuntimeException) {
231 assertMessage(db, true /* to entity */);
233 if (stepNumber != -1) {
234 int steps = a_steps.size();
235 int stepIndx = stepNumber - 1;
236 if ((stepIndx < 0) || (stepIndx > (a_steps.size()-1)))
237 throw anna::RuntimeException(anna::functions::asString("Step number out of range (test case %llu)", a_id), ANNA_FILE_LOCATION);
239 TestStep *stepReferred = a_steps[stepIndx];
240 if (stepReferred->getType() != TestStep::Type::Wait)
241 throw anna::RuntimeException(anna::functions::asString("Step number must refer to a 'wait' step (test case %llu)", a_id), ANNA_FILE_LOCATION);
243 const TestCondition &tc = (static_cast<TestStepWait*>(stepReferred))->getCondition();
244 if (tc.getCode() == "0") { // if regexp used, is not possible to detect this kind of errors
245 throw anna::RuntimeException(anna::functions::asString("Step number must refer to a 'wait for request' step (test case %llu)", a_id), ANNA_FILE_LOCATION);
249 TestStepSendxml2e *step = new TestStepSendxml2e(this);
250 step->setMsgDataBlock(db);
251 step->setRealmNode(realm);
252 step->setWaitForRequestStepNumber(stepNumber); // -1 means, no reference
253 a_steps.push_back(step);
256 void TestCase::addSendxml2c(const anna::DataBlock &db, RealmNode *realm, int stepNumber) throw(anna::RuntimeException) {
258 assertMessage(db, false /* to client */);
260 TestStepSendxml2c *step = new TestStepSendxml2c(this);
261 step->setMsgDataBlock(db);
262 step->setRealmNode(realm);
263 a_steps.push_back(step);
266 void TestCase::addDelay(const anna::Millisecond &delay) throw(anna::RuntimeException) {
268 TestStepDelay *step = new TestStepDelay(this);
269 step->setDelay(delay);
270 a_steps.push_back(step);
273 void TestCase::addWait(bool fromEntity,
274 const std::string &code, const std::string &bitR, const std::string &resultCode, const std::string &sessionId,
275 const std::string &hopByHop, const std::string &msisdn, const std::string &imsi, const std::string &serviceContextId) throw(anna::RuntimeException) {
277 std::string usedHopByHop = hopByHop;
278 TestStepWait *step = NULL;
280 // Check basic conditions:
282 if (resultCode != "")
283 throw anna::RuntimeException(anna::functions::asString("You cannot specify Result-Code (%s) for a wait condition of a diameter request message (test case %llu)", resultCode.c_str(), a_id), ANNA_FILE_LOCATION);
285 throw anna::RuntimeException(anna::functions::asString("You cannot specify Hop-by-hop (%s) for a wait condition of a diameter request message (test case %llu)", hopByHop.c_str(), a_id), ANNA_FILE_LOCATION);
288 if (hopByHop != "") {
289 if (hopByHop[0] == '#') {
290 int steps = a_steps.size();
292 throw anna::RuntimeException(anna::functions::asString("No steps has been programmed, step reference is nonsense (test case %llu)", a_id), ANNA_FILE_LOCATION);
294 int stepNumber = atoi(hopByHop.substr(1).c_str());
295 int stepIndx = stepNumber - 1;
296 if ((stepIndx < 0) || (stepIndx > (steps-1)))
297 throw anna::RuntimeException(anna::functions::asString("Step reference number %d out of range [1-%d]", stepNumber, steps), ANNA_FILE_LOCATION);
299 TestStep *stepReferred = a_steps[stepIndx];
300 if (stepReferred->getType() != TestStep::Type::Sendxml2e && stepReferred->getType() != TestStep::Type::Sendxml2c)
301 throw anna::RuntimeException(anna::functions::asString("Step number must refer to a 'sendxml2e' or 'sendxml2c' step (test case %llu)", a_id), ANNA_FILE_LOCATION);
303 const anna::DataBlock &db = (static_cast<TestStepSendxml*>(stepReferred))->getMsgDataBlock();
304 bool isAnswer = anna::diameter::codec::functions::isAnswer(db);
306 throw anna::RuntimeException(anna::functions::asString("Step number must refer to a request message (test case %llu)", a_id), ANNA_FILE_LOCATION);
309 anna::diameter::HopByHop hbh = anna::diameter::codec::functions::getHopByHop(db);
310 usedHopByHop = anna::functions::asString(hbh);
311 step = new TestStepWait(this);
312 a_hopByHops[hbh /* always exists: is the info we calculated above */] = step;
317 if (!step) step = new TestStepWait(this);
318 step->setCondition(fromEntity, code, bitR, resultCode, sessionId, usedHopByHop, msisdn, imsi, serviceContextId);
321 if (hasSameCondition(step->getCondition()))
322 anna::Logger::warning(anna::functions::asString("The same wait condition has already been programmed in this test case (%llu). Are you sure ?", a_id), ANNA_FILE_LOCATION);
325 a_steps.push_back(step);
328 void TestCase::addWaitRegexp(bool fromEntity, const std::string ®exp) throw(anna::RuntimeException) {
331 TestStepWait *step = new TestStepWait(this);
332 step->setCondition(fromEntity, regexp);
335 if (hasSameCondition(step->getCondition()))
336 anna::Logger::warning(anna::functions::asString("The same wait condition has already been programmed in this test case (%llu). Are you sure ?", a_id), ANNA_FILE_LOCATION);
339 a_steps.push_back(step);
342 void TestCase::addCmd(const std::string &script, const std::string ¶meters) throw(anna::RuntimeException) {
345 TestStepCmd *step = new TestStepCmd(this);
346 step->setScript(script);
347 step->setParameters(parameters);
349 a_steps.push_back(step);
352 TestStepWait *TestCase::searchNextWaitConditionFulfilled(const anna::DataBlock &message, bool waitFromEntity) throw() {
354 TestStepWait *result;
355 for (std::vector<TestStep*>::const_iterator it = a_stepsIt /* current */; it != a_steps.end(); it++) {
356 if ((*it)->getType() != TestStep::Type::Wait) continue;
357 if ((*it)->isCompleted()) continue;
358 result = (TestStepWait*)(*it);
359 if ((result->getCondition().receivedFromEntity() == waitFromEntity) && (result->fulfilled(message)))
366 const TestStep *TestCase::getStep(int stepNumber) const throw() {
367 int stepIndx = stepNumber - 1;
368 if ((stepIndx < 0) || (stepIndx > (a_steps.size()-1))) return NULL;
369 std::vector<TestStep*>::const_iterator it = (a_steps.begin() + stepNumber);