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