2 * Copyright 2014 IBM Corp.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 var should = require("should");
19 var switchNode = require("../../../../nodes/core/logic/10-switch.js");
20 var helper = require("../../helper.js");
22 describe('SwitchNode', function() {
24 beforeEach(function(done) {
25 helper.startServer(done);
28 afterEach(function(done) {
30 helper.stopServer(done);
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');
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
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);
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
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);
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
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);
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
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) {
101 if(shouldReceive === true) {
102 msg.payload.should.equal(sendPayload);
105 should.fail(null, null, "We should never get an input!");
111 switchNode1.receive({payload:sendPayload});
112 if(shouldReceive === false) {
113 setTimeout(function() {
120 it('should check if payload equals given value', function(done) {
121 genericSwitchTest("eq", "Hello", true, true, "Hello", done);
124 it('should return nothing when the payload doesn\'t equal to desired string', function(done) {
125 genericSwitchTest("eq", "Hello", true, false, "Hello!", done);
128 it('should check if payload NOT equals given value', function(done) {
129 genericSwitchTest("neq", "Hello", true, true, "HEllO", done);
132 it('should return nothing when the payload does equal to desired string', function(done) {
133 genericSwitchTest("neq", "Hello", true, false, "Hello", done);
136 it('should check if payload equals given numeric value', function(done) {
137 genericSwitchTest("eq", 3, true, true, 3, done);
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);
144 it('should check if payload NOT equals given numeric value', function(done) {
145 genericSwitchTest("neq", 55667744, true, true, -1234, done);
148 it('should return nothing when the payload does equal to desired numeric value', function(done) {
149 genericSwitchTest("neq", 10, true, false, 10, done);
152 it('should check if payload is less than given value', function(done) {
153 genericSwitchTest("lt", 3, true, true, 2, done);
156 it('should return nothing when the payload is not less than desired string', function(done) {
157 genericSwitchTest("lt", 3, true, false, 4, done);
160 it('should check if payload less than equals given value', function(done) {
161 genericSwitchTest("lte", 3, true, true, 3, done);
164 it('should check if payload is greater than given value', function(done) {
165 genericSwitchTest("gt", 3, true, true, 6, done);
168 it('should return nothing when the payload is not greater than desired string', function(done) {
169 genericSwitchTest("gt", 3, true, false, -1, done);
172 it('should check if payload is greater than/equals given value', function(done) {
173 genericSwitchTest("gte", 3, true, true, 3, done);
176 it('should return nothing when the payload is not greater than desired string', function(done) {
177 genericSwitchTest("gt", 3, true, false, -1, done);
180 it('should check if payload is greater than/equals given value', function(done) {
181 genericSwitchTest("gte", 3, true, true, 3, done);
184 it('should check if payload is between given values', function(done) {
185 twoFieldSwitchTest("btwn", 3, 5, true, true, 4, done);
188 it('should check if payload is not between given values', function(done) {
189 twoFieldSwitchTest("btwn", 3, 5, true, false, 12, done);
192 it('should check if payload contains given value', function(done) {
193 genericSwitchTest("cont", "Hello", true, true, "Hello World!", done);
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);
200 it('should match regex', function(done) {
201 genericSwitchTest("regex", "[abc]+", true, true, "abbabac", done);
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);
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);
212 it('should check if input is true', function(done) {
213 singularSwitchTest(true, true, true, true, done);
216 it('sends nothing when input is false and checking for true', function(done) {
217 singularSwitchTest(true, true, false, false, done);
220 it('should check if input is indeed false', function(done) {
221 singularSwitchTest(false, true, true, false, done);
224 it('sends nothing when input is false and checking for true', function(done) {
225 singularSwitchTest(false, true, false, true, done);
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:[]}];
233 helper.load(switchNode, flow, function() {
234 var switchNode1 = helper.getNode("switchNode1");
235 var helperNode1 = helper.getNode("helperNode1");
236 helperNode1.on("input", function(msg) {
239 should.fail(null, null, "msg.payload should be undefined!");
247 switchNode1.receive({payload:undefined});
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:[]}];
256 helper.load(switchNode, flow, function() {
257 var switchNode1 = helper.getNode("switchNode1");
258 var helperNode1 = helper.getNode("helperNode1");
259 helperNode1.on("input", function(msg) {
264 msg.payload.should.equal("Anything here");
270 switchNode1.receive({payload:"Anything here"});
274 it('sends a message when the "else/otherwise" statement is selected' , function(done) {
275 singularSwitchTest("else", true, true, 123456, done);
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:[]}];
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");
291 var nodeHitCount = 0;
292 helperNode1.on("input", function(msg) {
294 msg.payload.should.equal("Hello");
300 helperNode2.on("input", function(msg) {
302 msg.payload.should.equal("Hello");
304 if(nodeHitCount == 2) {
308 should.fail(null, null, "Both statements should be triggered!");
317 helperNode3.on("input", function(msg) {
319 should.fail(null, null, "The otherwise/else statement should not be triggered here!");
324 switchNode1.receive({payload:"Hello"});
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:[]}];
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");
341 helperNode1.on("input", function(msg) {
343 msg.payload.should.equal("Hello");
349 helperNode2.on("input", function(msg) {
351 should.fail(null, null, "The otherwise/else statement should not be triggered here!");
356 helperNode3.on("input", function(msg) {
358 should.fail(null, null, "The otherwise/else statement should not be triggered here!");
363 switchNode1.receive({payload:"Hello"});