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