[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / test / nodes / core / core / 58-debug_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 debugNode = require("../../../../nodes/core/core/58-debug.js");
19 var helper = require("../../helper.js");
20 var WebSocket = require('ws');
21
22 describe('debug node', function() {
23
24     before(function(done) {
25         helper.startServer(done);
26     });
27     
28     afterEach(function() {
29         helper.unload();
30     });
31
32
33     it('should be loaded', function(done) {
34         var flow = [{id:"n1", type:"debug", name: "Debug" }];
35         helper.load(debugNode, flow, function() {
36             var n1 = helper.getNode("n1");
37             n1.should.have.property('name', 'Debug');
38             done();
39         });
40     });
41
42     it('should publish on input', function(done) {
43         var flow = [{id:"n1", type:"debug", name: "Debug" }];
44         helper.load(debugNode, flow, function() {
45             var n1 = helper.getNode("n1");
46             websocket_test(function() {
47                 n1.emit("input", {payload:"test"});
48             }, function(msg) {
49                 JSON.parse(msg).should.eql({
50                     topic:"debug",data:{id:"n1",name:"Debug",msg:"test"}
51                 });
52             }, done);
53         });
54     });
55
56     it('should publish to console', function(done) {
57         var flow = [{id:"n1", type:"debug", console: "true" }];
58         helper.load(debugNode, flow, function() {
59             var n1 = helper.getNode("n1");
60             var count = 0;
61             n1.on('log', function(msg) {
62                 msg.should.eql({level:'log',id:'n1',type:'debug',msg:'test'});
63                 count++;
64                 if (count == 2) {
65                     done();
66                 }
67             });
68             websocket_test(function() {
69                 n1.emit("input", {payload:"test"});
70             }, function(msg) {
71                 JSON.parse(msg).should.eql({
72                     topic:"debug",data:{id:"n1",msg:"test"}
73                 });
74                 count++;
75             }, function() {
76                 if (count == 2) {
77                     done();
78                 }
79             });
80         });
81     });
82
83     it('should publish complete message', function(done) {
84         var flow = [{id:"n1", type:"debug", complete: "true" }];
85         helper.load(debugNode, flow, function() {
86             var n1 = helper.getNode("n1");
87             websocket_test(function() {
88                 n1.emit("input", {payload:"test"});
89             }, function(msg) {
90                 JSON.parse(msg).should.eql({
91                     topic:"debug",
92                     data:{id:"n1",msg:'(Object) {\n "payload": "test"\n}'}
93                 });
94             }, done);
95         });
96     });
97
98     it('should publish an Error', function(done) {
99         var flow = [{id:"n1", type:"debug" }];
100         helper.load(debugNode, flow, function() {
101             var n1 = helper.getNode("n1");
102             websocket_test(function() {
103                 n1.emit("input", {payload: new Error("oops")});
104             }, function(msg) {
105                 JSON.parse(msg).should.eql({
106                     topic:"debug",data:{id:"n1",msg:"Error: oops"}
107                 });
108             }, done);
109         });
110     });
111
112     it('should publish a boolean', function(done) {
113         var flow = [{id:"n1", type:"debug" }];
114         helper.load(debugNode, flow, function() {
115             var n1 = helper.getNode("n1");
116             websocket_test(function() {
117                 n1.emit("input", {payload: true});
118             }, function(msg) {
119                 JSON.parse(msg).should.eql({
120                     topic:"debug",data:{id:"n1",msg: '(boolean) true'}
121                 });
122             }, done);
123         });
124     });
125
126     it('should publish with no payload', function(done) {
127         var flow = [{id:"n1", type:"debug" }];
128         helper.load(debugNode, flow, function() {
129             var n1 = helper.getNode("n1");
130             websocket_test(function() {
131                 n1.emit("input", {});
132             }, function(msg) {
133                 JSON.parse(msg).should.eql({
134                     topic:"debug",data:{id:"n1",msg: '(undefined)'}
135                 });
136             }, done);
137         });
138     });
139
140     it('should publish an object', function(done) {
141         var flow = [{id:"n1", type:"debug" }];
142         helper.load(debugNode, flow, function() {
143             var n1 = helper.getNode("n1");
144             websocket_test(function() {
145                 n1.emit("input", {payload: {type:'foo'}});
146             }, function(msg) {
147                 JSON.parse(msg).should.eql({
148                     topic:"debug",
149                     data:{id:"n1",msg:'(Object) {\n "type": "foo"\n}'}
150                 });
151             }, done);
152         });
153     });
154
155     it('should publish an array', function(done) {
156         var flow = [{id:"n1", type:"debug" }];
157         helper.load(debugNode, flow, function() {
158             var n1 = helper.getNode("n1");
159             websocket_test(function() {
160                 n1.emit("input", {payload: [0,1,2,3]});
161             }, function(msg) {
162                 JSON.parse(msg).should.eql({
163                     topic:"debug",
164                     data:{id:"n1",msg: '(Array) [\n 0,\n 1,\n 2,\n 3\n]'}
165                 });
166             }, done);
167         });
168     });
169
170     it('should publish an object with circular references', function(done) {
171         var flow = [{id:"n1", type:"debug" }];
172         helper.load(debugNode, flow, function() {
173             var n1 = helper.getNode("n1");
174             websocket_test(function() {
175                 var o = { name: 'bar' };
176                 o.o = o;
177                 n1.emit("input", {payload: o});
178             }, function(msg) {
179                 JSON.parse(msg).should.eql({
180                     topic:"debug",
181                     data:{
182                         id:"n1",
183                         msg:'(Object) {\n "name": "bar",\n "o": "[circular]"\n}'
184                     }
185                 });
186             }, done);
187         });
188     });
189
190     it('should truncated a long message', function(done) {
191         var flow = [{id:"n1", type:"debug" }];
192         helper.load(debugNode, flow, function() {
193             var n1 = helper.getNode("n1");
194             websocket_test(function() {
195                 n1.emit("input", {payload: Array(1002).join("X")});
196             }, function(msg) {
197                 JSON.parse(msg).should.eql({
198                     topic:"debug",
199                     data:{
200                         id:"n1",
201                         msg: Array(1001).join("X")+' ....'
202                     }
203                 });
204             }, done);
205         });
206     });
207
208     it('should convert Buffer to hex', function(done) {
209         var flow = [{id:"n1", type:"debug" }];
210         helper.load(debugNode, flow, function() {
211             var n1 = helper.getNode("n1");
212             websocket_test(function() {
213                 n1.emit("input", {payload: new Buffer('HELLO', 'utf8')});
214             }, function(msg) {
215                 JSON.parse(msg).should.eql({
216                     topic:"debug",
217                     data:{
218                         id:"n1",
219                         msg: '(Buffer) 48454c4c4f',
220                     }
221                 });
222             }, done);
223         });
224     });
225
226     it('should publish when active', function(done) {
227         var flow = [{id:"n1", type:"debug", active: false }];
228         helper.load(debugNode, flow, function() {
229             var n1 = helper.getNode("n1");
230             websocket_test(function() {
231                 n1.emit("input", {payload:"message 1"});
232                 helper.request()
233                     .post('/debug/n1/enable')
234                     .expect(200).end(function(err) {
235                         if (err) { return done(err); }
236                         n1.emit("input", {payload:"message 2"});
237                     });
238             }, function(msg) {
239                 JSON.parse(msg).should.eql({
240                     topic:"debug",data:{id:"n1",msg:"message 2"}
241                 });
242             }, done);
243         });
244     });
245
246     it('should not publish when inactive', function(done) {
247         var flow = [{id:"n1", type:"debug", active: true }];
248         helper.load(debugNode, flow, function() {
249             var n1 = helper.getNode("n1");
250             websocket_test(function(close) {
251                 helper.request()
252                     .post('/debug/n1/disable')
253                     .expect(201).end(function(err) {
254                         if (err) {
255                             close();
256                             return done(err);
257                         }
258                         n1.emit("input", {payload:"message"});
259                         setTimeout(function() {
260                             close();
261                             done();
262                         }, 200);
263                     });
264             }, function(msg) {
265                 should.fail(null,null,"unexpected message");
266             }, function() {});
267         });
268     });
269
270     describe('post', function() {
271         it('should return 404 on invalid state', function(done) {
272             var flow = [{id:"n1", type:"debug", active: true }];
273             helper.load(debugNode, flow, function() {
274                 helper.request()
275                     .post('/debug/n1/foobar')
276                     .expect(404).end(done);
277             });
278         });
279
280         it('should return 404 on invalid node', function(done) {
281             helper.request()
282                 .post('/debug/n99/enable')
283                 .expect(404).end(done);
284         });
285     });
286
287 });
288
289 function websocket_test(open_callback, message_callback, done_callback) {
290     var ws = new WebSocket(helper.url() + "/comms");
291     var close_callback = function() { ws.close(); };
292     ws.on('open', function() { open_callback(close_callback); });
293     ws.on('message', function(msg) {
294         message_callback(msg, close_callback);
295         ws.close();
296         done_callback();
297     });
298 }