[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / test / nodes / core / logic / 10-switch_spec.js
1 /**
2  * Copyright 2014 IBM Corp.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  **/
16
17 var should = require("should");
18
19 var switchNode = require("../../../../nodes/core/logic/10-switch.js");
20 var helper = require("../../helper.js");
21
22 describe('SwitchNode', function() {
23     
24     beforeEach(function(done) {
25         helper.startServer(done);
26     });
27     
28     afterEach(function(done) {
29         helper.unload();
30         helper.stopServer(done);
31     });
32     
33     it('should be loaded', function(done) {
34         var flow = [{"id":"switchNode1","type":"switch","name":"switchNode","property":"payload","rules":[{"t":"eq","v":""}],"checkall":"true","outputs":1,"wires":[[]]}];
35         helper.load(switchNode, flow, function() {
36             var switchNode1 = helper.getNode("switchNode1");
37             switchNode1.should.have.property('name', 'switchNode');
38             done();
39         });
40     });
41     
42     /**
43      * Test a switch node where one argument is consumed by the rule (such as greater than).
44      * @param rule - the switch rule (see 10-switc.js) string we're using
45      * @param ruleWith - whatever the rule should be executed with (say greater than 5)
46      * @param aCheckall - whether the switch flow should have the checkall flag set to true/false
47      * @param shouldReceive - whether the helper node should receive a payload
48      * @param sendPayload - the payload message we're sending
49      * @param done - callback when done
50      */
51     function genericSwitchTest(rule, ruleWith, aCheckall, shouldReceive, sendPayload, done) {
52         var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":rule,"v":ruleWith}],checkall:aCheckall,outputs:1,wires:[["helperNode1"]]},
53                     {id:"helperNode1", type:"helper", wires:[]}];
54         customFlowSwitchTest(flow, shouldReceive, sendPayload, done);
55     }
56     
57     /**
58      * Test a switch node where NO arguments are consumed by the rule (such as TRUE/FALSE)
59      * @param rule - the switch rule (see 10-switc.js) string we're using
60      * @param aCheckall - whether the switch flow should have the checkall flag set to true/false
61      * @param shouldReceive - whether the helper node should receive a payload
62      * @param sendPayload - the payload message we're sending
63      * @param done - callback when done
64      */
65     function singularSwitchTest(rule, aCheckall, shouldReceive, sendPayload, done) {
66         var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":rule}],checkall:aCheckall,outputs:1,wires:[["helperNode1"]]},
67                     {id:"helperNode1", type:"helper", wires:[]}];
68         customFlowSwitchTest(flow, shouldReceive, sendPayload, done);
69     }
70     
71     /**
72      * Test a switch node where two arguments are consumed by the rule (such as between).
73      * @param rule - the switch rule (see 10-switc.js) string we're using
74      * @param ruleWith - whatever the rule should be executed with (say between 5...)
75      * @param ruleWith2 - whatever the rule should be executed with (say ...and 5)
76      * @param aCheckall - whether the switch flow should have the checkall flag set to true/false
77      * @param shouldReceive - whether the helper node should receive a payload
78      * @param sendPayload - the payload message we're sending
79      * @param done - callback when done
80      */
81     function twoFieldSwitchTest(rule, ruleWith, ruleWith2, aCheckall, shouldReceive, sendPayload, done) {
82         var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":rule,"v":ruleWith,"v2":ruleWith2}],checkall:aCheckall,outputs:1,wires:[["helperNode1"]]},
83                     {id:"helperNode1", type:"helper", wires:[]}];
84         customFlowSwitchTest(flow, shouldReceive, sendPayload, done);
85     }
86     
87     /**
88      * Execute a switch test. Can specify whether the should node is expected to send a payload onwards to the helper node.
89      * The flow and the payload can be customised
90      * @param flow - the custom flow to be tested => must contain a switch node (switchNode1) wiring a helper node (helperNode1)
91      * @param shouldReceive - whether the helper node should receive a payload
92      * @param sendPayload - the payload message we're sending
93      * @param done - callback when done
94      */
95     function customFlowSwitchTest(flow, shouldReceive, sendPayload, done) {
96         helper.load(switchNode, flow, function() {
97             var switchNode1 = helper.getNode("switchNode1");
98             var helperNode1 = helper.getNode("helperNode1");
99             helperNode1.on("input", function(msg) {
100                 try {
101                     if(shouldReceive === true) {
102                         msg.payload.should.equal(sendPayload);
103                         done();   
104                     } else {
105                         should.fail(null, null, "We should never get an input!");
106                     }
107                 } catch(err) {
108                     done(err);
109                 }
110             });
111             switchNode1.receive({payload:sendPayload});
112             if(shouldReceive === false) {
113                 setTimeout(function() {
114                     done();
115                 }, 200);
116             }
117         });
118     }
119     
120     it('should check if payload equals given value', function(done) {
121         genericSwitchTest("eq", "Hello", true, true, "Hello", done);
122     });
123     
124     it('should return nothing when the payload doesn\'t equal to desired string', function(done) {
125         genericSwitchTest("eq", "Hello", true, false, "Hello!", done);
126     });
127     
128     it('should check if payload NOT equals given value', function(done) {
129         genericSwitchTest("neq", "Hello", true, true, "HEllO", done);
130     });
131     
132     it('should return nothing when the payload does equal to desired string', function(done) {
133         genericSwitchTest("neq", "Hello", true, false, "Hello", done);
134     });
135     
136     it('should check if payload equals given numeric value', function(done) {
137         genericSwitchTest("eq", 3, true, true, 3, done);
138     });
139     
140     it('should return nothing when the payload doesn\'t equal to desired numeric value', function(done) {
141         genericSwitchTest("eq", 2, true, false, 4, done);
142     });
143     
144     it('should check if payload NOT equals given numeric value', function(done) {
145         genericSwitchTest("neq", 55667744, true, true, -1234, done);
146     });
147     
148     it('should return nothing when the payload does equal to desired numeric value', function(done) {
149         genericSwitchTest("neq", 10, true, false, 10, done);
150     });
151     
152     it('should check if payload is less than given value', function(done) {
153         genericSwitchTest("lt", 3, true, true, 2, done);
154     });
155     
156     it('should return nothing when the payload is not less than desired string', function(done) {
157         genericSwitchTest("lt", 3, true, false, 4, done);
158     });
159     
160     it('should check if payload less than equals given value', function(done) {
161         genericSwitchTest("lte", 3, true, true, 3, done);
162     });
163     
164     it('should check if payload is greater than given value', function(done) {
165         genericSwitchTest("gt", 3, true, true, 6, done);
166     });
167     
168     it('should return nothing when the payload is not greater than desired string', function(done) {
169         genericSwitchTest("gt", 3, true, false, -1, done);
170     });
171     
172     it('should check if payload is greater than/equals given value', function(done) {
173         genericSwitchTest("gte", 3, true, true, 3, done);
174     });
175     
176     it('should return nothing when the payload is not greater than desired string', function(done) {
177         genericSwitchTest("gt", 3, true, false, -1, done);
178     });
179     
180     it('should check if payload is greater than/equals given value', function(done) {
181         genericSwitchTest("gte", 3, true, true, 3, done);
182     });
183     
184     it('should check if payload is between given values', function(done) {
185         twoFieldSwitchTest("btwn", 3, 5, true, true, 4, done);
186     });
187     
188     it('should check if payload is not between given values', function(done) {
189         twoFieldSwitchTest("btwn", 3, 5, true, false, 12, done);
190     });
191     
192     it('should check if payload contains given value', function(done) {
193         genericSwitchTest("cont", "Hello", true, true, "Hello World!", done);
194     });
195     
196     it('should return nothing when the payload doesn\'t contain desired string', function(done) {
197         genericSwitchTest("cont", "Hello", true, false, "This is not a greeting!", done);
198     });
199     
200     it('should match regex', function(done) {
201         genericSwitchTest("regex", "[abc]+", true, true, "abbabac", done);
202     });
203     
204     it('should return nothing when the payload doesn\'t match regex', function(done) {
205         genericSwitchTest("regex", "\\d+", true, false, "This is not a digit", done);
206     });
207     
208     it('should return nothing when the payload doesn\'t contain desired string', function(done) {
209         genericSwitchTest("cont", "Hello", true, false, "This is not a greeting!", done);
210     });
211     
212     it('should check if input is true', function(done) {
213         singularSwitchTest(true, true, true, true, done);
214     });
215     
216     it('sends nothing when input is false and checking for true', function(done) {
217         singularSwitchTest(true, true, false, false, done);
218     });
219     
220     it('should check if input is indeed false', function(done) {
221         singularSwitchTest(false, true, true, false, done);
222     });
223     
224     it('sends nothing when input is false and checking for true', function(done) {
225         singularSwitchTest(false, true, false, true, done);
226     });
227     
228     it('should check if input is indeed null', function(done) {
229         var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"null"}],checkall:true,outputs:1,wires:[["helperNode1"]]},
230                     {id:"helperNode1", type:"helper", wires:[]}];
231         
232         
233         helper.load(switchNode, flow, function() {
234             var switchNode1 = helper.getNode("switchNode1");
235             var helperNode1 = helper.getNode("helperNode1");
236             helperNode1.on("input", function(msg) {
237                 if(msg.payload) {
238                     try {
239                         should.fail(null, null, "msg.payload should be undefined!");   
240                     } catch (err) {
241                         done(err);
242                     }
243                 } else {
244                     done();
245                 }
246             });
247             switchNode1.receive({payload:undefined});
248         });
249     });
250     
251     it('should check if input is indeed not null', function(done) {
252         var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"nnull"}],checkall:false,outputs:1,wires:[["helperNode1"]]},
253                     {id:"helperNode1", type:"helper", wires:[]}];
254         
255         
256         helper.load(switchNode, flow, function() {
257             var switchNode1 = helper.getNode("switchNode1");
258             var helperNode1 = helper.getNode("helperNode1");
259             helperNode1.on("input", function(msg) {
260                 if(msg.payload) {
261                     done();
262                 } else {
263                     try {
264                         msg.payload.should.equal("Anything here");
265                     } catch (err) {
266                         done(err);
267                     }
268                 }
269             });
270             switchNode1.receive({payload:"Anything here"});
271         });
272     });
273     
274     it('sends a message when the "else/otherwise" statement is selected' , function(done) {
275         singularSwitchTest("else", true, true, 123456, done);
276     });
277     
278     it('handles more than one switch statement' , function(done) {
279         var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"eq","v":"Hello"},{"t":"cont","v":"ello"}, {"t":"else"}],checkall:true,outputs:3,wires:[["helperNode1"], ["helperNode2"], ["helperNode3"]]},
280                     {id:"helperNode1", type:"helper", wires:[]},
281                     {id:"helperNode2", type:"helper", wires:[]},
282                     {id:"helperNode3", type:"helper", wires:[]}];
283         
284         
285         helper.load(switchNode, flow, function() {
286             var switchNode1 = helper.getNode("switchNode1");
287             var helperNode1 = helper.getNode("helperNode1");
288             var helperNode2 = helper.getNode("helperNode2");
289             var helperNode3 = helper.getNode("helperNode3");
290             
291             var nodeHitCount = 0;
292             helperNode1.on("input", function(msg) {
293                 try {
294                     msg.payload.should.equal("Hello");
295                     nodeHitCount++;
296                 } catch (err) {
297                     done(err);
298                 }
299             });
300             helperNode2.on("input", function(msg) {
301                 try {
302                     msg.payload.should.equal("Hello");
303                     nodeHitCount++;
304                     if(nodeHitCount == 2) {
305                         done();
306                     } else {
307                         try {
308                             should.fail(null, null, "Both statements should be triggered!");   
309                         } catch (err) {
310                             done(err);
311                         }
312                     }
313                 } catch (err) {
314                     done(err);
315                 }
316             });
317             helperNode3.on("input", function(msg) {
318                 try {
319                     should.fail(null, null, "The otherwise/else statement should not be triggered here!");   
320                 } catch (err) {
321                     done(err);
322                 }
323             });
324             switchNode1.receive({payload:"Hello"});
325         });
326     });
327     
328     it('stops after first statement' , function(done) {
329         var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"eq","v":"Hello"},{"t":"cont","v":"ello"}, {"t":"else"}],checkall:"false",outputs:3,wires:[["helperNode1"], ["helperNode2"], ["helperNode3"]]},
330                     {id:"helperNode1", type:"helper", wires:[]},
331                     {id:"helperNode2", type:"helper", wires:[]},
332                     {id:"helperNode3", type:"helper", wires:[]}];
333         
334         
335         helper.load(switchNode, flow, function() {
336             var switchNode1 = helper.getNode("switchNode1");
337             var helperNode1 = helper.getNode("helperNode1");
338             var helperNode2 = helper.getNode("helperNode2");
339             var helperNode3 = helper.getNode("helperNode3");
340             
341             helperNode1.on("input", function(msg) {
342                 try {
343                     msg.payload.should.equal("Hello");
344                     done();
345                 } catch (err) {
346                     done(err);
347                 }
348             });
349             helperNode2.on("input", function(msg) {
350                 try {
351                     should.fail(null, null, "The otherwise/else statement should not be triggered here!");   
352                 } catch (err) {
353                     done(err);
354                 }
355             });
356             helperNode3.on("input", function(msg) {
357                 try {
358                     should.fail(null, null, "The otherwise/else statement should not be triggered here!");   
359                 } catch (err) {
360                     done(err);
361                 }
362             });
363             switchNode1.receive({payload:"Hello"});
364         });
365     });
366     
367 });