[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / test / nodes / core / core / 20-inject_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 injectNode = require("../../../../nodes/core/core/20-inject.js");
19 var helper = require("../../helper.js");
20
21 describe('inject node', function() {
22
23     before(function(done) {
24         helper.startServer(done);
25     });
26     
27     afterEach(function() {
28         helper.unload();
29     });
30
31     it('should inject once', function(done) {
32
33         helper.load(injectNode, [{id:"n1", type:"inject",
34                     payload:"payload", topic: "t1",
35                     once: true, wires:[["n2"]] },
36                    {id:"n2", type:"helper"}],
37                   function() {
38                       var n2 = helper.getNode("n2");
39                       n2.on("input", function(msg) {
40                           msg.should.have.property('topic', 't1');
41                           msg.should.have.property('payload', 'payload');
42                           done();
43                       });
44                   });
45     });
46
47     it('should inject repeatedly', function(done) {
48
49         helper.load(injectNode, [{id:"n1", type:"inject",
50                     payload:"payload", topic: "t2",
51                     repeat: 0.2, wires:[["n2"]] },
52                    {id:"n2", type:"helper"}],
53                   function() {
54                       var n2 = helper.getNode("n2");
55                       var count = 0;
56                       n2.on("input", function(msg) {
57                           msg.should.have.property('topic', 't2');
58                           msg.should.have.property('payload', 'payload');
59                           count += 1;
60                           if (count > 2) {
61                               helper.clearFlows().then(function() {
62                                   done();
63                               });
64                           }
65                       });
66                   });
67     });
68
69     it('should inject with cron', function(done) {
70         helper.load(injectNode, [{id:"n1", type:"inject",
71                     payloadType:"date", topic: "t3",
72                     crontab: "* * * * * *", wires:[["n3"]] },
73                    {id:"n3", type:"helper"}],
74                   function() {
75                       var n3 = helper.getNode("n3");
76                       n3.on("input", function(msg) {
77                           msg.should.have.property('topic', 't3');
78                           msg.should.have.property('payload').be.a.Number;
79                           helper.clearFlows().then(function() {
80                               done();
81                           });
82                       });
83                   });
84     });
85
86     describe('post', function() {
87         it('should inject message', function(done) {
88             helper.load(injectNode,
89                         [{id:"n1", type:"inject",
90                           payloadType:"some type", topic: "t4",
91                           wires:[["n4"]] },
92                          { id:"n4", type:"helper"}], function() {
93                              var n4 = helper.getNode("n4");
94                              n4.on("input", function(msg) {
95                                  msg.should.have.property('topic', 't4');
96                                  msg.should.have.property('payload', '');
97                                  helper.clearFlows().then(function() {
98                                      done();
99                                  });
100                              });
101                              helper.request()
102                                  .post('/inject/n1')
103                                  .expect(200).end(function(err) {
104                                      if (err) {
105                                          return helper.clearFlows()
106                                              .then(function () {
107                                                  done(err);
108                                              });
109                                      }
110                                  });
111                          });
112         });
113
114         it('should fail for invalid node', function(done) {
115             helper.request().post('/inject/invalid').expect(404).end(done);
116         });
117     });
118 });