blob: e1d0178bf37ed993e6cce35060a9be0ececb83c9 [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();
94 });
95
96 it('should send a websocket event when it receive a redis event that is not JSON', (done) => {
97 publisher.publish(channelName, 'I am sending a message.');
98 setTimeout(() => {
99 expect(socketSpy).to.have.been.called;
100 expect(socketSpy).to.have.been.calledWith('event', {
101 model: channelName,
102 msg: 'I am sending a message.'
103 });
104 done();
105 }, 500)
106 });
107
108 it('should send a websocket event when it receive a redis event that is JSON', (done) => {
109 publisher.publish(channelName, JSON.stringify({msg: 'Json Message'}));
110 setTimeout(() => {
111 expect(socketSpy).to.have.been.called;
112 expect(socketSpy).to.have.been.calledWith('event', {
113 model: channelName,
114 msg: {msg: 'Json Message'}
115 });
116 done();
117 }, 1000)
118 });
119 });
120})();