System test feature
[anna.git] / example / diameter / launcher / testing / TestStep.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_TestStep_hpp
10 #define example_diameter_launcher_TestStep_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
20 // Process
21 #include <TestCondition.hpp>
22
23
24 namespace anna {
25   class Millisecond;
26
27   namespace xml {
28     class Node;
29   }
30   namespace diameter {
31     namespace comm {
32       class ClientSession;
33       class ServerSession;
34     }
35   }
36 }
37
38 class TestCase;
39 class TestTimer;
40 class RealmNode;
41
42 class TestStep {
43
44     int a_number; // step number used for xml (informational)
45     anna::Millisecond a_beginTimestamp; // unix time
46     anna::Millisecond a_endTimestamp; // unix time
47
48     void setBeginTimestamp(const anna::Millisecond &t) throw() { a_beginTimestamp = t; }
49     const anna::Millisecond &getBeginTimestamp() const throw() { return a_beginTimestamp; }
50     void setEndTimestamp(const anna::Millisecond &t) throw() { a_endTimestamp = t; }
51     const anna::Millisecond &getEndTimestamp() const throw() { return a_endTimestamp; }
52
53     void initialize(TestCase *testCase);
54
55   public:
56     struct Type { enum _v { Unconfigured, Timeout, Sendxml2e, Sendxml2c, Delay, Wait }; };
57     static const char* asText(const Type::_v type) throw();
58
59     TestStep(TestCase *testCase) { initialize(testCase); }
60     virtual ~TestStep() {;}
61
62     // setter & getters
63     const Type::_v &getType() const throw() { return a_type; }
64     const int &getNumber() const throw() { return a_number; }
65     bool isCompleted() const throw() { return a_completed; }
66
67     bool execute() throw();
68     void complete() throw();
69     void reset() throw();
70     void next() throw();
71     virtual anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
72     std::string asXMLString() const throw();
73
74   protected:
75     TestCase *a_testCase;
76     bool a_completed;
77     Type::_v a_type;
78
79     virtual bool do_execute() throw() = 0; // returns true if next step must be executed
80     virtual void do_complete() throw() = 0; // end of transaction (delay/timeout expired, wait condition fulfilled, sending done)
81                                             // In all cases, the next step will be executed except 'timeout' which is asynchronous
82                                             //  and will move to the next step just after timer creation (no complete waited)
83     virtual void do_reset() throw() = 0;
84 };
85
86
87 class TestStepTimeout : public TestStep {
88
89     anna::Millisecond a_timeout;
90     TestTimer *a_timer; // just in case i would need to cancel
91
92   public:
93     TestStepTimeout(TestCase *testCase) : TestStep(testCase), a_timeout(0), a_timer(NULL) { a_type = Type::Timeout; }
94
95     // setter & getters
96     void setTimeout(const anna::Millisecond &t) throw() { a_timeout = t; }
97     const anna::Millisecond &getTimeout() const throw() { return a_timeout; }
98
99     // virtuals
100     bool do_execute() throw();
101     void do_complete() throw(); // timeout reached, test case failed
102     void do_reset() throw();
103     anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
104 };
105
106
107 class TestStepSendxml : public TestStep {
108
109   protected:
110     // possible end points:
111     RealmNode *a_realmNode;
112
113     // Step number reference ('wait for request' step)
114     int a_waitForRequestStepNumber;
115
116     // Message
117     anna::DataBlock a_message;
118
119     // Expired ?
120     bool a_expired; // a_endTimestamp will be the expiration reception timestamp
121
122   public:
123     TestStepSendxml(TestCase *testCase) : TestStep(testCase), a_message(true), a_expired(false), a_realmNode(NULL), a_waitForRequestStepNumber(-1) {;}
124
125     // setter & getters
126     void setRealmNode(RealmNode *realm) throw() { a_realmNode = realm; }
127     RealmNode *getRealmNode() const throw() { return a_realmNode; }
128     void setWaitForRequestStepNumber(int stepNumber) throw() { a_waitForRequestStepNumber = stepNumber; }
129     int getWaitForRequestStepNumber() const throw() { return a_waitForRequestStepNumber; }
130     void setMsgDataBlock(const anna::DataBlock &db) throw() { a_message = db; }
131     const anna::DataBlock &getMsgDataBlock() const throw() { return a_message; }
132
133     // virtuals
134     bool do_execute() throw();
135     void do_complete() throw();
136     void do_reset() throw();
137     anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
138 };
139
140 class TestStepSendxml2e : public TestStepSendxml {
141   public:
142     TestStepSendxml2e(TestCase *testCase) : TestStepSendxml(testCase) { a_type = Type::Sendxml2e; }
143 };
144
145 class TestStepSendxml2c : public TestStepSendxml {
146   public:
147     TestStepSendxml2c(TestCase *testCase) : TestStepSendxml(testCase) { a_type = Type::Sendxml2c; }
148 };
149
150
151 class TestStepDelay : public TestStep {
152     anna::Millisecond a_delay;
153     TestTimer *a_timer; // just in case i would need to cancel
154
155   public:
156     TestStepDelay(TestCase *testCase) : TestStep(testCase), a_delay(0), a_timer(NULL) { a_type = Type::Delay; }
157
158     // setter & getters
159     void setDelay(const anna::Millisecond &d) throw() { a_delay = d; }
160     const anna::Millisecond &getDelay() const throw() { return a_delay; }
161
162     // virtuals
163     bool do_execute() throw();
164     void do_complete() throw(); // delay reached
165     void do_reset() throw();
166     anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
167 };
168
169
170 class TestStepWait : public TestStep {
171
172     TestCondition a_condition;
173     anna::DataBlock a_message; // message which complies with condition
174     anna::diameter::comm::ClientSession *a_clientSession;
175     anna::diameter::comm::ServerSession *a_serverSession;
176
177   public:
178     TestStepWait(TestCase *testCase) : TestStep(testCase) { a_type = Type::Wait; a_clientSession = NULL; a_serverSession = NULL; }
179     ~TestStepWait() {;}
180
181     // setter & getters
182     void setCondition(bool fromEntity,
183                         const std::string &code, const std::string &bitR, const std::string &resultCode, const std::string &sessionId,
184                         const std::string &hopByHop, const std::string &msisdn, const std::string &imsi, const std::string &serviceContextId) throw();
185     void setCondition(bool fromEntity, const std::string &regexp) throw();
186
187     void setClientSession(anna::diameter::comm::ClientSession *cs) throw() { a_clientSession = cs; }
188     void setServerSession(anna::diameter::comm::ServerSession *ss) throw() { a_serverSession = ss; }
189
190     const TestCondition &getCondition() const throw() { return a_condition; }
191     //void setMsgDataBlock(const anna::DataBlock &db) throw() { a_message = db; }
192     bool fulfilled(const anna::DataBlock &db/*, bool matchSessionId = true*/) throw();
193     const anna::DataBlock &getMsgDataBlock() const throw() { return a_message; }
194
195
196     // virtuals
197     bool do_execute() throw(); // this will be executed when test case starts (at least we could measure the time until condition is fulfilled)
198     void do_complete() throw(); // condition fulfilled
199     void do_reset() throw();
200     anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
201 };
202
203
204 #endif