0a2c93bf57a33cba69a9d829f15562cb6aed8043
[anna.git] / example / diameter / launcher / testing / TestCase.hpp
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 #ifndef example_diameter_launcher_TestCase_hpp
10 #define example_diameter_launcher_TestCase_hpp
11
12 // Standard
13 #include <string>
14 #include <vector>
15
16 // Project
17 #include <anna/core/DataBlock.hpp>
18 #include <anna/xml/Node.hpp>
19 #include <anna/diameter/defines.hpp>
20
21 // Process
22 #include <TestStep.hpp>
23
24
25 namespace anna {
26   class Millisecond;
27
28   namespace xml {
29     class Node;
30   }
31 }
32
33 class RealmNode;
34
35
36 class TestCase {
37
38   void assertInitialized() const throw(anna::RuntimeException);
39   void assertMessage(const anna::DataBlock &db, bool toEntity) throw(anna::RuntimeException);
40
41 public:
42
43   // Debug summary:
44   class DebugSummary {
45
46     typedef struct {
47       anna::Millisecond Timestamp;
48       std::string Hint;
49     } event_t;
50
51     std::vector<event_t> a_events;
52   public:
53     void addHint(const std::string &hint) throw();
54     void clear() throw();
55     int events() const throw() { return a_events.size(); }
56     anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
57   };
58
59   TestCase(unsigned int id) : a_id(id), a_state(State::Initialized), a_startTime(0) { /*a_stepsIt = a_steps.end()*/;}
60   ~TestCase();
61
62   struct State { enum _v { Initialized, InProgress, Failed, Success }; };
63   static const char* asText(const State::_v state) throw();
64   const State::_v &getState() const throw() { return a_state; }
65   const anna::Millisecond &getStartTimestamp() const throw() { return a_startTime; }
66   void addDebugSummaryHint(const std::string &hint) throw() { a_debugSummary.addHint(hint); }
67   void setState(const State::_v &state) throw();
68   bool isFinished() const throw() { return (getState() == State::Failed || getState() == State::Success); }
69   bool inProgress() const throw() { return (getState() == State::InProgress); }
70   bool hasSameCondition(const TestCondition &condition) const throw();
71
72   // Step type & information
73   void addTimeout(const anna::Millisecond &timeout) throw(anna::RuntimeException);
74   void addSendxml2e(const anna::DataBlock &db, RealmNode *realm, int stepNumber) throw(anna::RuntimeException);
75   void addSendxml2c(const anna::DataBlock &db, RealmNode *realm, int stepNumber) throw(anna::RuntimeException);
76   void addDelay(const anna::Millisecond &delay) throw(anna::RuntimeException);
77   void addWait(bool fromEntity,
78                 const std::string &code, const std::string &bitR, const std::string &hopByHop, const std::string &applicationId,
79                 const std::string &sessionId, const std::string &resultCode,
80                 const std::string &msisdn, const std::string &imsi, const std::string &serviceContextId) throw(anna::RuntimeException);
81   void addWaitAnswer(bool fromEntity, int stepNumber) throw(anna::RuntimeException);
82   void addWaitRegexp(bool fromEntity, const std::string &regexp) throw(anna::RuntimeException);
83   void addCmd(const std::string &script, const std::string &parameters) throw(anna::RuntimeException);
84
85
86   // Process:
87   void nextStep() throw() { a_stepsIt++; }
88   bool done() throw();
89   bool process() throw(); // false to stop
90
91   // Reset test case and underlaying information (steps context)
92   bool reset(bool hard /* hard reset includes in-progress test cases */) throw();
93
94   // getters
95   const anna::Millisecond &getStartTime() const throw() { return a_startTime; }
96   const unsigned int &getId() const throw() { return a_id; }
97
98   //helpers
99   int steps() const throw() { return a_steps.size(); }
100   TestStepWait *searchNextWaitConditionFulfilled(const anna::DataBlock &message, bool waitFromEntity) throw();
101       // When a message arrives, we identify the test case by mean the Session-Id. Then, from the current step iterator (included),
102       //  we search for a fulfilling condition for that message. The first found, is 'completed' and then breaks the search.
103   const TestStep *getStep(int stepNumber) const throw();
104
105   anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
106   std::string asXMLString() const throw();
107
108
109 private:
110   // private members:
111   unsigned int a_id;
112   std::vector<TestStep*> a_steps;
113   std::vector<TestStep*>::const_iterator a_stepsIt;
114   std::map<anna::diameter::HopByHop, TestStep*> a_hopByHops; // for wait-answer
115   State::_v a_state;
116   anna::Millisecond a_startTime;
117   DebugSummary a_debugSummary; // used when a test case has failed, uncovered message conditions, and any other hint.
118
119   friend class TestStep;
120 };
121
122 #endif