blob: c3599cb63a3a12af4934663755af36521b750563 [file] [log] [blame]
Simon Hunt584122a2015-01-21 15:32:40 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Hunt584122a2015-01-21 15:32:40 -08003 *
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/*
18 ONOS GUI -- Remote -- Web Socket Service - Unit Tests
19 */
Matteo Scandolo812aa5a2016-04-19 18:12:45 -070020
Matteo Scandolo231c7542016-05-20 11:13:11 -070021describe('factory: fw/remote/websocket.js', function () {
Simon Hunt584122a2015-01-21 15:32:40 -080022 var $log, fs, wss;
23
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -070024 var noop = function () {},
25 send = jasmine.createSpy('send').and.callFake(function (ev) {
26 return ev;
27 }),
28 mockWebSocket = {
29 send: send
30 };
31
Simon Hunt2d16fc82015-03-10 20:19:52 -070032 beforeEach(module('onosRemote', 'onosLayer', 'ngRoute', 'onosNav', 'onosSvg'));
Simon Hunt584122a2015-01-21 15:32:40 -080033
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -070034 beforeEach(function () {
35 mockWebSocket = {
36 send: send
37 };
38 });
39
40 beforeEach(function () {
41 module(function ($provide) {
42 $provide.factory('WSock', function () {
43 return {
44 newWebSocket: function () {
45 return mockWebSocket;
46 }
47 };
48 });
49 });
50 });
51
Simon Hunt584122a2015-01-21 15:32:40 -080052 beforeEach(module(function($provide) {
Simon Hunt2d16fc82015-03-10 20:19:52 -070053 $provide.factory('$location', function () {
Simon Hunt584122a2015-01-21 15:32:40 -080054 return {
55 protocol: function () { return 'http'; },
56 host: function () { return 'foo'; },
Matteo Scandolo231c7542016-05-20 11:13:11 -070057 port: function () { return '80'; },
58 search: function() {
59 return {debug: 'true'};
60 }
Simon Hunt584122a2015-01-21 15:32:40 -080061 };
62 })
63 }));
64
65 beforeEach(inject(function (_$log_, FnService, WebSocketService) {
66 $log = _$log_;
67 fs = FnService;
68 wss = WebSocketService;
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -070069 wss.resetState();
Simon Hunt584122a2015-01-21 15:32:40 -080070 }));
71
72
73 it('should define WebSocketService', function () {
74 expect(wss).toBeDefined();
75 });
76
77 it('should define api functions', function () {
78 expect(fs.areFunctions(wss, [
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -070079 'resetSid', 'resetState',
80 'createWebSocket', 'bindHandlers', 'unbindHandlers',
Matteo Scandolo231c7542016-05-20 11:13:11 -070081 'addOpenListener', 'removeOpenListener', 'sendEvent',
82 'isConnected', 'loggedInUser',
83 '_setVeilDelegate', '_setLoadingDelegate'
Simon Hunt584122a2015-01-21 15:32:40 -080084 ])).toBeTruthy();
85 });
86
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -070087 it('should use the appropriate URL, createWebsocket', function () {
Simon Hunt2d16fc82015-03-10 20:19:52 -070088 var url = wss.createWebSocket();
89 expect(url).toEqual('ws://foo:80/onos/ui/websock/core');
Simon Hunt584122a2015-01-21 15:32:40 -080090 });
91
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -070092 it('should use the appropriate URL with modified port, createWebsocket',
93 function () {
94 var url = wss.createWebSocket({ wsport: 1243 });
95 expect(url).toEqual('ws://foo:1243/onos/ui/websock/core');
Simon Huntacf410b2015-01-23 10:05:48 -080096 });
97
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -070098 it('should verify websocket event handlers, createWebsocket', function () {
99 wss.createWebSocket({ wsport: 1234 });
100 expect(fs.isF(mockWebSocket.onopen)).toBeTruthy();
101 expect(fs.isF(mockWebSocket.onmessage)).toBeTruthy();
102 expect(fs.isF(mockWebSocket.onclose)).toBeTruthy();
103 });
104
105 it('should invoke listener callbacks when websocket is up, handleOpen',
106 function () {
107 var num = 0;
108 function incrementNum() { num++; }
109 wss.addOpenListener(incrementNum);
110 wss.createWebSocket({ wsport: 1234 });
111 mockWebSocket.onopen();
112 expect(num).toBe(1);
113 });
114
115 it('should send pending events, handleOpen', function () {
116 var fakeEvent = {
117 event: 'mockEv',
118 sid: 1,
119 payload: { mock: 'thing' }
120 };
121 wss.sendEvent(fakeEvent.event, fakeEvent.payload);
122 expect(mockWebSocket.send).not.toHaveBeenCalled();
123 wss.createWebSocket({ wsport: 1234 });
124 mockWebSocket.onopen();
125 expect(mockWebSocket.send).toHaveBeenCalledWith(JSON.stringify(fakeEvent));
126 });
127
128 it('should handle an incoming bad JSON message, handleMessage', function () {
129 spyOn($log, 'error');
130 var badMsg = {
131 data: 'bad message'
132 };
133 wss.createWebSocket({ wsport: 1234 });
134 expect(mockWebSocket.onmessage(badMsg)).toBeNull();
135 expect($log.error).toHaveBeenCalled();
136 });
137
138 it('should verify message was handled, handleMessage', function () {
139 var num = 0,
140 fakeHandler = {
141 mockEvResp: function () { num++; }
142 },
143 data = JSON.stringify({
144 event: 'mockEvResp',
145 payload: {}
146 }),
147 event = {
148 data: data
149 };
150 wss.createWebSocket({ wsport: 1234 });
151 wss.bindHandlers(fakeHandler);
152 expect(mockWebSocket.onmessage(event)).toBe(undefined);
153 expect(num).toBe(1);
154 });
155
156 it('should warn if there is an unhandled event, handleMessage', function () {
157 spyOn($log, 'warn');
158 var data = { foo: 'bar', bar: 'baz'},
159 dataString = JSON.stringify(data),
160 badEv = {
161 data: dataString
162 };
163 wss.createWebSocket({ wsport: 1234 });
164 mockWebSocket.onmessage(badEv);
165 expect($log.warn).toHaveBeenCalledWith('Unhandled event:', data);
166 });
167
168 it('should not warn if valid input, bindHandlers', function () {
169 spyOn($log, 'warn');
170 expect(wss.bindHandlers({
171 foo: noop,
172 bar: noop
173 })).toBe(undefined);
174 expect($log.warn).not.toHaveBeenCalled();
175 });
176
177 it('should warn if no arguments, bindHandlers', function () {
178 spyOn($log, 'warn');
179 expect(wss.bindHandlers()).toBeNull();
180 expect($log.warn).toHaveBeenCalledWith(
181 'WSS.bindHandlers(): no event handlers'
182 );
183 expect(wss.bindHandlers({})).toBeNull();
184 expect($log.warn).toHaveBeenCalledWith(
185 'WSS.bindHandlers(): no event handlers'
186 );
187 });
188
189 it('should warn if handler is not a function, bindHandlers', function () {
190 spyOn($log, 'warn');
191 expect(wss.bindHandlers({
192 foo: 'handler1',
193 bar: 3,
194 baz: noop
195 })).toBe(undefined);
196 expect($log.warn).toHaveBeenCalledWith('foo handler not a function');
197 expect($log.warn).toHaveBeenCalledWith('bar handler not a function');
198 });
199
200 it('should warn if duplicate handlers were given, bindHandlers',
201 function () {
202 spyOn($log, 'warn');
203 wss.bindHandlers({
204 foo: noop
205 });
206 expect(wss.bindHandlers({
207 foo: noop
208 })).toBe(undefined);
209 expect($log.warn).toHaveBeenCalledWith('duplicate bindings ignored:',
210 ['foo']);
211 });
212
213 it('should warn if no arguments, unbindHandlers', function () {
214 spyOn($log, 'warn');
215 expect(wss.unbindHandlers()).toBeNull();
216 expect($log.warn).toHaveBeenCalledWith(
217 'WSS.unbindHandlers(): no event handlers'
218 );
219 expect(wss.unbindHandlers({})).toBeNull();
220 expect($log.warn).toHaveBeenCalledWith(
221 'WSS.unbindHandlers(): no event handlers'
222 );
223 });
224 // Note: cannot test unbindHandlers' forEach due to it using closure variable
225
226 it('should not warn if valid argument, addOpenListener', function () {
227 spyOn($log, 'warn');
228 var o = wss.addOpenListener(noop);
229 expect(o.id === 1);
230 expect(o.cb === noop);
231 expect($log.warn).not.toHaveBeenCalled();
232 o = wss.addOpenListener(noop);
233 expect(o.id === 2);
234 expect(o.cb === noop);
235 expect($log.warn).not.toHaveBeenCalled();
236 });
237
238 it('should log error if callback not a function, addOpenListener',
239 function () {
240 spyOn($log, 'error');
241 var o = wss.addOpenListener('foo');
242 expect(o.id === 1);
243 expect(o.cb === 'foo');
244 expect(o.error === 'No callback defined');
245 expect($log.error).toHaveBeenCalledWith(
246 'WSS.addOpenListener(): callback not a function'
247 );
248 });
249
250 it('should not warn if valid listener object, removeOpenListener', function () {
251 spyOn($log, 'warn');
252 expect(wss.removeOpenListener({
253 id: 1,
254 cb: noop
255 })).toBe(undefined);
256 expect($log.warn).not.toHaveBeenCalled();
257 });
258
259 it('should warn if listener is invalid, removeOpenListener', function () {
260 spyOn($log, 'warn');
261 expect(wss.removeOpenListener({})).toBeNull();
262 expect($log.warn).toHaveBeenCalledWith(
263 'WSS.removeOpenListener(): invalid listener', {}
264 );
265 expect(wss.removeOpenListener('listener')).toBeNull();
266 expect($log.warn).toHaveBeenCalledWith(
267 'WSS.removeOpenListener(): invalid listener', 'listener'
268 );
269 });
270
271 // Note: handleClose is not currently tested due to all work it does relies
272 // on closure variables that cannot be mocked
Simon Huntacf410b2015-01-23 10:05:48 -0800273
Simon Hunt584122a2015-01-21 15:32:40 -0800274});