Merge "Add Graph/Node to org.ops4j.pax.logging.cfg"
[ccsdk/distribution.git] / dgbuilder / test / nodes / core / core / 80-function_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 var functionNode = require("../../../../nodes/core/core/80-function.js");
19 var helper = require("../../helper.js");
20
21 describe('function node', function() {
22
23     before(function(done) {
24         helper.startServer(done);
25     });
26     
27     afterEach(function() {
28         helper.unload();
29     });
30
31     it('should be loaded', function(done) {
32         var flow = [{id:"n1", type:"function", name: "function" }];
33         helper.load(functionNode, flow, function() {
34             var n1 = helper.getNode("n1");
35             n1.should.have.property('name', 'function');
36             done();
37         });
38     });
39
40     it('should send returned message', function(done) {
41         var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"return msg;"},
42                     {id:"n2", type:"helper"}];
43         helper.load(functionNode, flow, function() {
44             var n1 = helper.getNode("n1");
45             var n2 = helper.getNode("n2");
46             n2.on("input", function(msg) {
47                 msg.should.have.property('topic', 'bar');
48                 msg.should.have.property('payload', 'foo');
49                 done();
50             });
51             n1.receive({payload:"foo",topic: "bar"});
52         });
53     });
54
55     it('should pass through _topic', function(done) {
56         var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"return msg;"},
57                     {id:"n2", type:"helper"}];
58         helper.load(functionNode, flow, function() {
59             var n1 = helper.getNode("n1");
60             var n2 = helper.getNode("n2");
61             n2.on("input", function(msg) {
62                 msg.should.have.property('topic', 'bar');
63                 msg.should.have.property('payload', 'foo');
64                 msg.should.have.property('_topic', 'baz');
65                 done();
66             });
67             n1.receive({payload:"foo",topic: "bar", _topic: "baz"});
68         });
69     });
70
71     it('should send to multiple outputs', function(done) {
72         var flow = [{id:"n1",type:"function",wires:[["n2"],["n3"]],
73                      func:"return [{payload: '1'},{payload: '2'}];"},
74                     {id:"n2", type:"helper"}, {id:"n3", type:"helper"} ];
75         helper.load(functionNode, flow, function() {
76             var n1 = helper.getNode("n1");
77             var n2 = helper.getNode("n2");
78             var n3 = helper.getNode("n3");
79             var count = 0;
80             n2.on("input", function(msg) {
81                 should(msg).have.property('payload', '1');
82                 count++;
83                 if (count == 2) {
84                     done();
85                 }
86             });
87             n3.on("input", function(msg) {
88                 should(msg).have.property('payload', '2');
89                 count++;
90                 if (count == 2) {
91                     done();
92                 }
93             });
94             n1.receive({payload:"foo",topic: "bar"});
95         });
96     });
97
98     it('should send to multiple messages', function(done) {
99         var flow = [{id:"n1",type:"function",wires:[["n2"]],
100                      func:"return [[{payload: 1},{payload: 2}]];"},
101                     {id:"n2", type:"helper"} ];
102         helper.load(functionNode, flow, function() {
103             var n1 = helper.getNode("n1");
104             var n2 = helper.getNode("n2");
105             var count = 0;
106             n2.on("input", function(msg) {
107                 count++;
108                 should(msg).have.property('payload', count);
109                 should(msg).have.property('_topic', 'baz');
110                 if (count == 2) {
111                     done();
112                 }
113             });
114             n1.receive({payload:"foo", topic: "bar", _topic:"baz"});
115         });
116     });
117
118     it('should allow input to be discarded by returning null', function(done) {
119         var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"return null"},
120                     {id:"n2", type:"helper"}];
121         helper.load(functionNode, flow, function() {
122             var n1 = helper.getNode("n1");
123             var n2 = helper.getNode("n2");
124             setTimeout(function() {
125                 done();
126             }, 200);
127             n2.on("input", function(msg) {
128                 should.fail(null,null,"unexpected message");
129             });
130             n1.receive({payload:"foo",topic: "bar"});
131         });
132     });
133
134     it('should handle and log script error', function(done) {
135         var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"retunr"}];
136         helper.load(functionNode, flow, function() {
137             var n1 = helper.getNode("n1");
138             n1.on("log", function(msg) {
139                 msg.should.have.property('level', 'error');
140                 msg.should.have.property('id', 'n1');
141                 msg.should.have.property('type', 'function');
142                 msg.should.have.property('msg', 'ReferenceError: retunr is not defined');
143                 done();
144             });
145             n1.receive({payload:"foo",topic: "bar"});
146         });
147     });
148
149 });