blob: fc7aa09d3666e3e62fe05442ace2956e4d77ce27 [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'};
Simon Huntfa875bb2016-08-24 11:41:02 -070060 },
61 absUrl: function () {
62 return 'ws://foo:123/onos/ui/websock/path';
Matteo Scandolo231c7542016-05-20 11:13:11 -070063 }
Simon Hunt584122a2015-01-21 15:32:40 -080064 };
65 })
66 }));
67
68 beforeEach(inject(function (_$log_, FnService, WebSocketService) {
69 $log = _$log_;
70 fs = FnService;
71 wss = WebSocketService;
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -070072 wss.resetState();
Simon Hunt584122a2015-01-21 15:32:40 -080073 }));
74
75
76 it('should define WebSocketService', function () {
77 expect(wss).toBeDefined();
78 });
79
80 it('should define api functions', function () {
81 expect(fs.areFunctions(wss, [
Simon Hunt8a0429a2017-01-06 16:52:47 -080082 'resetState',
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -070083 'createWebSocket', 'bindHandlers', 'unbindHandlers',
Matteo Scandolo231c7542016-05-20 11:13:11 -070084 'addOpenListener', 'removeOpenListener', 'sendEvent',
85 'isConnected', 'loggedInUser',
86 '_setVeilDelegate', '_setLoadingDelegate'
Simon Hunt584122a2015-01-21 15:32:40 -080087 ])).toBeTruthy();
88 });
89
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -070090 it('should use the appropriate URL, createWebsocket', function () {
Simon Hunt2d16fc82015-03-10 20:19:52 -070091 var url = wss.createWebSocket();
92 expect(url).toEqual('ws://foo:80/onos/ui/websock/core');
Simon Hunt584122a2015-01-21 15:32:40 -080093 });
94
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -070095 it('should use the appropriate URL with modified port, createWebsocket',
96 function () {
97 var url = wss.createWebSocket({ wsport: 1243 });
98 expect(url).toEqual('ws://foo:1243/onos/ui/websock/core');
Simon Huntacf410b2015-01-23 10:05:48 -080099 });
100
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -0700101 it('should verify websocket event handlers, createWebsocket', function () {
102 wss.createWebSocket({ wsport: 1234 });
103 expect(fs.isF(mockWebSocket.onopen)).toBeTruthy();
104 expect(fs.isF(mockWebSocket.onmessage)).toBeTruthy();
105 expect(fs.isF(mockWebSocket.onclose)).toBeTruthy();
106 });
107
108 it('should invoke listener callbacks when websocket is up, handleOpen',
109 function () {
110 var num = 0;
111 function incrementNum() { num++; }
112 wss.addOpenListener(incrementNum);
113 wss.createWebSocket({ wsport: 1234 });
114 mockWebSocket.onopen();
115 expect(num).toBe(1);
116 });
117
118 it('should send pending events, handleOpen', function () {
119 var fakeEvent = {
120 event: 'mockEv',
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -0700121 payload: { mock: 'thing' }
122 };
123 wss.sendEvent(fakeEvent.event, fakeEvent.payload);
124 expect(mockWebSocket.send).not.toHaveBeenCalled();
125 wss.createWebSocket({ wsport: 1234 });
126 mockWebSocket.onopen();
127 expect(mockWebSocket.send).toHaveBeenCalledWith(JSON.stringify(fakeEvent));
128 });
129
130 it('should handle an incoming bad JSON message, handleMessage', function () {
131 spyOn($log, 'error');
132 var badMsg = {
133 data: 'bad message'
134 };
135 wss.createWebSocket({ wsport: 1234 });
136 expect(mockWebSocket.onmessage(badMsg)).toBeNull();
137 expect($log.error).toHaveBeenCalled();
138 });
139
140 it('should verify message was handled, handleMessage', function () {
141 var num = 0,
142 fakeHandler = {
143 mockEvResp: function () { num++; }
144 },
145 data = JSON.stringify({
146 event: 'mockEvResp',
147 payload: {}
148 }),
149 event = {
150 data: data
151 };
152 wss.createWebSocket({ wsport: 1234 });
153 wss.bindHandlers(fakeHandler);
154 expect(mockWebSocket.onmessage(event)).toBe(undefined);
155 expect(num).toBe(1);
156 });
157
158 it('should warn if there is an unhandled event, handleMessage', function () {
159 spyOn($log, 'warn');
160 var data = { foo: 'bar', bar: 'baz'},
161 dataString = JSON.stringify(data),
162 badEv = {
163 data: dataString
164 };
165 wss.createWebSocket({ wsport: 1234 });
166 mockWebSocket.onmessage(badEv);
167 expect($log.warn).toHaveBeenCalledWith('Unhandled event:', data);
168 });
169
170 it('should not warn if valid input, bindHandlers', function () {
171 spyOn($log, 'warn');
172 expect(wss.bindHandlers({
173 foo: noop,
174 bar: noop
175 })).toBe(undefined);
176 expect($log.warn).not.toHaveBeenCalled();
177 });
178
179 it('should warn if no arguments, bindHandlers', function () {
180 spyOn($log, 'warn');
181 expect(wss.bindHandlers()).toBeNull();
182 expect($log.warn).toHaveBeenCalledWith(
183 'WSS.bindHandlers(): no event handlers'
184 );
185 expect(wss.bindHandlers({})).toBeNull();
186 expect($log.warn).toHaveBeenCalledWith(
187 'WSS.bindHandlers(): no event handlers'
188 );
189 });
190
191 it('should warn if handler is not a function, bindHandlers', function () {
192 spyOn($log, 'warn');
193 expect(wss.bindHandlers({
194 foo: 'handler1',
195 bar: 3,
196 baz: noop
197 })).toBe(undefined);
198 expect($log.warn).toHaveBeenCalledWith('foo handler not a function');
199 expect($log.warn).toHaveBeenCalledWith('bar handler not a function');
200 });
201
202 it('should warn if duplicate handlers were given, bindHandlers',
203 function () {
204 spyOn($log, 'warn');
205 wss.bindHandlers({
206 foo: noop
207 });
208 expect(wss.bindHandlers({
209 foo: noop
210 })).toBe(undefined);
211 expect($log.warn).toHaveBeenCalledWith('duplicate bindings ignored:',
212 ['foo']);
213 });
214
215 it('should warn if no arguments, unbindHandlers', function () {
216 spyOn($log, 'warn');
217 expect(wss.unbindHandlers()).toBeNull();
218 expect($log.warn).toHaveBeenCalledWith(
219 'WSS.unbindHandlers(): no event handlers'
220 );
221 expect(wss.unbindHandlers({})).toBeNull();
222 expect($log.warn).toHaveBeenCalledWith(
223 'WSS.unbindHandlers(): no event handlers'
224 );
225 });
226 // Note: cannot test unbindHandlers' forEach due to it using closure variable
227
228 it('should not warn if valid argument, addOpenListener', function () {
229 spyOn($log, 'warn');
230 var o = wss.addOpenListener(noop);
231 expect(o.id === 1);
232 expect(o.cb === noop);
233 expect($log.warn).not.toHaveBeenCalled();
234 o = wss.addOpenListener(noop);
235 expect(o.id === 2);
236 expect(o.cb === noop);
237 expect($log.warn).not.toHaveBeenCalled();
238 });
239
240 it('should log error if callback not a function, addOpenListener',
241 function () {
242 spyOn($log, 'error');
243 var o = wss.addOpenListener('foo');
244 expect(o.id === 1);
245 expect(o.cb === 'foo');
246 expect(o.error === 'No callback defined');
247 expect($log.error).toHaveBeenCalledWith(
248 'WSS.addOpenListener(): callback not a function'
249 );
250 });
251
252 it('should not warn if valid listener object, removeOpenListener', function () {
253 spyOn($log, 'warn');
254 expect(wss.removeOpenListener({
255 id: 1,
256 cb: noop
257 })).toBe(undefined);
258 expect($log.warn).not.toHaveBeenCalled();
259 });
260
261 it('should warn if listener is invalid, removeOpenListener', function () {
262 spyOn($log, 'warn');
263 expect(wss.removeOpenListener({})).toBeNull();
264 expect($log.warn).toHaveBeenCalledWith(
265 'WSS.removeOpenListener(): invalid listener', {}
266 );
267 expect(wss.removeOpenListener('listener')).toBeNull();
268 expect($log.warn).toHaveBeenCalledWith(
269 'WSS.removeOpenListener(): invalid listener', 'listener'
270 );
271 });
272
273 // Note: handleClose is not currently tested due to all work it does relies
274 // on closure variables that cannot be mocked
Simon Huntacf410b2015-01-23 10:05:48 -0800275
Simon Hunt584122a2015-01-21 15:32:40 -0800276});