blob: 05ddadcc453b96595d29b94510148e8de26a00b6 [file] [log] [blame]
Matteo Scandolobd13aab2017-08-08 13:05:24 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080019(function () {
20 'use strict';
21
22 const chai = require('chai');
23 const expect = chai.expect;
24 const sinon = require('sinon');
25 const sinonChai = require('sinon-chai');
26 const mockery = require('mockery');
27 chai.use(sinonChai);
28 const fakeredis = require('fakeredis');
29
30 const client = fakeredis.createClient('test-client');
31 const publisher = fakeredis.createClient('test-client');
32
33 const socketSpy = sinon.spy();
34 const mockSocket = {
35 get: () => {
36 return {
37 emit: socketSpy
38 }
39 }
40 };
Matteo Scandolo606751e2017-01-13 13:07:02 -080041
42 const mockRequest = {
43 get: () => {
44 return {
45 end: (fn) => {
46 fn(null, {body: [
47 {name: 'Slice'},
48 {name: 'Site'}
49 ]});
50 }
51 }
52 }
53 }
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080054 const channelName = 'Site';
55
56 describe('The event system', () => {
57
58 before((done) => {
59
60 // Enable mockery to mock objects
61 mockery.enable({
62 warnOnReplace: false,
63 warnOnUnregistered: false
64 });
65
66 // Stub the createClient method to *always* return the client created above
67 sinon.stub(fakeredis, 'createClient', () => client);
68
69 // Override the redis module with our fakeredis instance
70 mockery.registerMock('redis', fakeredis);
71
Matteo Scandolo606751e2017-01-13 13:07:02 -080072 // Override the superagent module with our mockRequest instance
73 mockery.registerMock('superagent', mockRequest);
74
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080075 // mock the socketIo client to have a spy
76 mockery.registerMock('./websocket.js', mockSocket);
77
78 require('../src/controllers/redis.js');
79 setTimeout(() => {
80 done();
81 }, 1000);
82 });
83
84 after(() => {
85 mockery.disable();
86 fakeredis.createClient.restore();
87 });
88
89 // run after each test
90 beforeEach(() => {
91 client.unsubscribe(channelName);
92 client.subscribe(channelName);
93 publisher.flushdb();
Matteo Scandolo9f15c682017-11-20 15:49:55 -080094 socketSpy.reset();
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080095 });
96
97 it('should send a websocket event when it receive a redis event that is not JSON', (done) => {
98 publisher.publish(channelName, 'I am sending a message.');
99 setTimeout(() => {
100 expect(socketSpy).to.have.been.called;
Matteo Scandolo9f15c682017-11-20 15:49:55 -0800101 expect(socketSpy).to.have.been.calledWith('update', {
Matteo Scandoloe3ed0162016-12-01 10:09:12 -0800102 model: channelName,
103 msg: 'I am sending a message.'
104 });
105 done();
106 }, 500)
107 });
108
109 it('should send a websocket event when it receive a redis event that is JSON', (done) => {
110 publisher.publish(channelName, JSON.stringify({msg: 'Json Message'}));
111 setTimeout(() => {
112 expect(socketSpy).to.have.been.called;
Matteo Scandolo9f15c682017-11-20 15:49:55 -0800113 expect(socketSpy).to.have.been.calledWith('update', {
Matteo Scandoloe3ed0162016-12-01 10:09:12 -0800114 model: channelName,
115 msg: {msg: 'Json Message'}
116 });
117 done();
118 }, 1000)
119 });
Matteo Scandolo9f15c682017-11-20 15:49:55 -0800120
121 it('should send a websocket event when an object has been removed', (done) => {
122 publisher.publish(channelName, JSON.stringify({msg: 'Deleted', deleted: true}));
123 setTimeout(() => {
124 expect(socketSpy).to.have.been.called;
125 expect(socketSpy).to.have.been.calledWith('remove', {
126 model: channelName,
127 msg: {
128 msg: 'Deleted',
129 deleted: true
130 },
131 deleted: true
132 });
133
134 done();
135 }, 1000)
136 });
137
138 it('should not send a websocket event if the channel is Diag', (done) => {
139 publisher.publish('Diag', JSON.stringify({msg: 'Json Message'}));
140 setTimeout(() => {
141 expect(socketSpy).not.to.have.been.called;
142 done();
143 }, 1000)
144 });
Matteo Scandoloe3ed0162016-12-01 10:09:12 -0800145 });
146})();