[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / test / nodes / core / parsers / 70-XML_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 xmlNode = require("../../../../nodes/core/parsers/70-XML.js");
19 var helper = require("../../helper.js");
20
21 describe('XML 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:"xmlNode1", type:"xml", name: "xmlNode" }];
33         helper.load(xmlNode, flow, function() {
34             var xmlNode1 = helper.getNode("xmlNode1");
35             xmlNode1.should.have.property('name', 'xmlNode');
36             done();
37         });
38     });
39
40     it('should convert a valid xml string to a javascript object', function(done) {
41         var flow = [{id:"n1",type:"xml",wires:[["n2"]],func:"return msg;"},
42                     {id:"n2", type:"helper"}];
43         helper.load(xmlNode, 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.payload.should.have.property('employees');
49                 msg.payload.employees.should.have.property('firstName');
50                 should.equal(msg.payload.employees.firstName[0], 'John');
51                 msg.payload.employees.should.have.property('lastName');
52                 should.equal(msg.payload.employees.lastName[0], 'Smith');
53                 done();
54             });
55             var string = '<employees><firstName>John</firstName><lastName>Smith</lastName></employees>';
56             n1.receive({payload:string,topic: "bar"});
57         });
58     });
59    
60     it('should convert a javascript object to an xml string', function(done) {
61         var flow = [{id:"n1",type:"xml",wires:[["n2"]],func:"return msg;"},
62                     {id:"n2", type:"helper"}];
63         helper.load(xmlNode, flow, function() {
64             var n1 = helper.getNode("n1");
65             var n2 = helper.getNode("n2");
66             n2.on("input", function(msg) {
67                 msg.should.have.property('topic', 'bar');
68                 console.log(msg.payload);
69                 var index = msg.payload.indexOf('<employees><firstName>John</firstName><lastName>Smith</lastName></employees>');
70                 index.should.be.above(-1);
71                 done();
72             });
73             var obj = {"employees":{"firstName":["John"],"lastName":["Smith"] }};
74             n1.receive({payload:obj,topic: "bar"});
75         });
76     });
77     
78     it('should log an error if asked to parse an invalid xml string', function(done) {
79         var flow = [{id:"n1",type:"xml",wires:[["n2"]],func:"return msg;"},
80                     {id:"n2", type:"helper"}];
81         helper.load(xmlNode, flow, function() {
82             var n1 = helper.getNode("n1");
83             var n2 = helper.getNode("n2");
84             n1.on("log", function(msg) {
85                 should.deepEqual("error", msg.level);
86                 done();
87             });
88             n1.receive({payload:'<not valid xml>',topic: "bar"});
89         });
90     });
91     
92     it('should log an error if asked to parse something thats not xml or js', function(done) {
93         var flow = [{id:"n1",type:"xml",wires:[["n2"]],func:"return msg;"},
94                     {id:"n2", type:"helper"}];
95         helper.load(xmlNode, flow, function() {
96             var n1 = helper.getNode("n1");
97             var n2 = helper.getNode("n2");
98             n1.on("log", function(msg) {
99                 msg.should.have.property('msg');
100                 should.deepEqual("This node only handles xml strings or js objects.", msg.msg);
101                 done();
102             });
103             n1.receive({payload:1,topic: "bar"});
104         });
105     });
106
107 });