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");
18 var jsonNode = require("../../../../nodes/core/parsers/70-JSON.js");
19 var helper = require("../../helper.js");
21 describe('JSON node', function() {
23 before(function(done) {
24 helper.startServer(done);
27 afterEach(function() {
31 it('should be loaded', function(done) {
32 var flow = [{id:"jsonNode1", type:"json", name: "jsonNode" }];
33 helper.load(jsonNode, flow, function() {
34 var jsonNode1 = helper.getNode("jsonNode1");
35 jsonNode1.should.have.property('name', 'jsonNode');
40 it('should convert a valid json string to a javascript object', function(done) {
41 var flow = [{id:"jn1",type:"json",wires:[["jn2"]],func:"return msg;"},
42 {id:"jn2", type:"helper"}];
43 helper.load(jsonNode, flow, function() {
44 var jn1 = helper.getNode("jn1");
45 var jn2 = helper.getNode("jn2");
46 jn2.on("input", function(msg) {
47 msg.should.have.property('topic', 'bar');
48 msg.payload.should.have.property('employees');
49 msg.payload.employees[0].should.have.property('firstName', 'John');
50 msg.payload.employees[0].should.have.property('lastName', 'Smith');
53 var jsonString = '{"employees":[{"firstName":"John", "lastName":"Smith"}]}';
54 jn1.receive({payload:jsonString,topic: "bar"});
58 it('should convert a javascript object to a json string', function(done) {
59 var flow = [{id:"jn1",type:"json",wires:[["jn2"]],func:"return msg;"},
60 {id:"jn2", type:"helper"}];
61 helper.load(jsonNode, flow, function() {
62 var jn1 = helper.getNode("jn1");
63 var jn2 = helper.getNode("jn2");
64 jn2.on("input", function(msg) {
65 msg.should.have.property('topic', 'bar');
66 should.equal(msg.payload, '{"employees":[{"firstName":"John","lastName":"Smith"}]}');
69 var obj = {employees:[{firstName:"John", lastName:"Smith"}]};
70 jn1.receive({payload:obj,topic: "bar"});
74 it('should log an error if asked to parse an invalid json string', function(done) {
75 var flow = [{id:"jn1",type:"json",wires:[["jn2"]],func:"return msg;"},
76 {id:"jn2", type:"helper"}];
77 helper.load(jsonNode, flow, function() {
78 var jn1 = helper.getNode("jn1");
79 var jn2 = helper.getNode("jn2");
80 jn1.on("log", function(msg) {
81 msg.should.have.property('msg');
82 should.deepEqual("SyntaxError: Unexpected token o"+ "\nfoo", msg.msg);
85 jn1.receive({payload:'foo',topic: "bar"});
89 it('should log an error if asked to parse something thats not json or js', function(done) {
90 var flow = [{id:"jn1",type:"json",wires:[["jn2"]],func:"return msg;"},
91 {id:"jn2", type:"helper"}];
92 helper.load(jsonNode, flow, function() {
93 var jn1 = helper.getNode("jn1");
94 var jn2 = helper.getNode("jn2");
95 jn1.on("log", function(msg) {
96 msg.should.have.property('msg');
97 should.deepEqual("dropped: 1", msg.msg);
100 jn1.receive({payload:1,topic: "bar"});