6ec64ad86f3e097e0b5e34add5ea29d682f26eac
[anna.git] / include / anna / 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 anna_testing_TestCase_hpp
10 #define anna_testing_TestCase_hpp
11
12 // Standard
13 #include <string>
14 #include <vector>
15 #include <map>
16
17 // Project
18 #include <anna/core/DataBlock.hpp>
19 #include <anna/core/util/Millisecond.hpp>
20
21 // Diameter-specific
22 #include <anna/diameter/defines.hpp>
23 #include <anna/testing/TestDiameterCondition.hpp>
24
25
26 namespace anna {
27   class Millisecond;
28
29   namespace xml {
30     class Node;
31   }
32   namespace diameter {
33     namespace comm {
34       class OriginHost;
35     }
36   }
37
38 namespace testing {
39
40   class TestStep;
41   class TestStepWaitDiameter;
42 class TestCase {
43
44   void assertInitialized() const throw(anna::RuntimeException);
45   void assertMessage(const anna::DataBlock &db, bool toEntity) throw(anna::RuntimeException);
46
47 public:
48
49   // Debug summary:
50   class DebugSummary {
51
52     typedef struct {
53       anna::Millisecond Timestamp;
54       std::string Hint;
55     } event_t;
56
57     std::vector<event_t> a_events;
58   public:
59     void addHint(const std::string &hint) throw();
60     void clear() throw();
61     int events() const throw() { return a_events.size(); }
62     anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
63     std::string asString() const throw();
64   };
65
66   TestCase(unsigned int id, const std::string &description = "");
67   ~TestCase();
68
69   struct State { enum _v { Initialized, InProgress, Failed, Success }; };
70   static const char* asText(const State::_v state) throw();
71   const State::_v &getState() const throw() { return a_state; }
72   const anna::Millisecond &getStartTimestamp() const throw() { return a_startTimestamp; }
73   const anna::Millisecond &getFinishTimestamp() const throw() { return a_finishTimestamp; }
74   anna::Millisecond getLapseMs() const throw();
75   anna::Millisecond getProcessingTimeMs() const throw();
76   void addDebugSummaryHint(const std::string &hint) throw() { a_debugSummary.addHint(hint); }
77   void setState(const State::_v &state) throw();
78   bool isFinished() const throw() { return (getState() == State::Failed || getState() == State::Success); }
79   bool inProgress() const throw() { return (getState() == State::InProgress); }
80   bool isFailed() const throw() { return (getState() == State::Failed); }
81   bool hasSameCondition(const TestDiameterCondition &condition) const throw();
82   const DebugSummary & getDebugSummary() const throw() { return a_debugSummary; }
83
84   // Interactivity:
85   void makeInteractive(bool yes = true) throw() { a_interactiveAmount = (yes ? 0:-1); }
86   void addInteractiveAmount(unsigned int amount) throw() {
87     if (a_interactiveAmount == -1) makeInteractive();
88     if (amount == 0) return;
89     a_interactiveAmount += amount;
90     process();
91   }
92   int interactiveAmount() const throw() { return a_interactiveAmount; }
93   void interactiveExecution() throw() { a_interactiveAmount --; }
94
95   // Step type & information
96   void addTimeout(const anna::Millisecond &timeout) throw(anna::RuntimeException);
97   void addDelay(const anna::Millisecond &delay) throw(anna::RuntimeException);
98   void addWaitDiameter(bool fromEntity,
99                 const std::string &code, const std::string &bitR, const std::string &hopByHop, const std::string &applicationId,
100                 const std::string &sessionId, const std::string &resultCode,
101                 const std::string &msisdn, const std::string &imsi, const std::string &serviceContextId) throw(anna::RuntimeException);
102   void addCommand(const std::string &cmd) throw(anna::RuntimeException);
103   void addIpLimit(unsigned int ipLimit) throw(anna::RuntimeException);
104
105   // Diameter-specifc
106   void addSendDiameterXml2e(const anna::DataBlock &db, anna::diameter::comm::OriginHost *host, int stepNumber) throw(anna::RuntimeException);
107   void addSendDiameterXml2c(const anna::DataBlock &db, anna::diameter::comm::OriginHost *host, int stepNumber) throw(anna::RuntimeException);
108   void addWaitDiameterAnswer(bool fromEntity, int stepNumber) throw(anna::RuntimeException);
109   void addWaitDiameterRegexpHex(bool fromEntity, const std::string &regexp) throw(anna::RuntimeException);
110   void addWaitDiameterRegexpXml(bool fromEntity, const std::string &regexp) throw(anna::RuntimeException);
111
112
113   // Process:
114   void nextStep() throw() { a_stepsIt++; }
115   bool done() throw();
116   bool process() throw(); // false to stop
117
118   // Reset test case and underlaying information (steps context)
119   bool reset(bool hard /* hard reset includes in-progress test cases */) throw();
120
121   // getters
122   const unsigned int &getId() const throw() { return a_id; }
123   const std::string &getDescription() const throw() { return a_description; }
124
125   // setters
126   void setDescription(const std::string &description) throw() { a_description = description; }
127
128   //helpers
129   int steps() const throw() { return a_steps.size(); }
130   void addStep(TestStep *step) throw() { a_steps.push_back(step); }
131
132   TestStepWaitDiameter *searchNextWaitConditionFulfilled(const anna::DataBlock &message, bool waitFromEntity) throw();
133       // When a message arrives, we identify the test case by mean the Session-Id. Then, from the current step iterator (included),
134       //  we search for a fulfilling condition for that message. The first found, is 'completed' and then breaks the search.
135   const TestStep *getStep(int stepNumber) const throw();
136
137   anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
138   std::string asXMLString() const throw();
139
140
141 private:
142   // private members:
143   unsigned int a_id;
144   std::string a_description;
145   std::vector<TestStep*> a_steps;
146   std::vector<TestStep*>::const_iterator a_stepsIt;
147   std::map<anna::diameter::HopByHop, TestStep*> a_hopByHops; // for wait-answer
148   State::_v a_state;
149   anna::Millisecond a_startTimestamp;
150   anna::Millisecond a_finishTimestamp;
151   DebugSummary a_debugSummary; // used when a test case has failed, uncovered message conditions, and any other hint.
152   int a_interactiveAmount;
153
154   friend class TestStep;
155 };
156
157 }
158 }
159
160 #endif