Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 3 | * |
| 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 Scandolo | 812aa5a | 2016-04-19 18:12:45 -0700 | [diff] [blame] | 20 | |
Matteo Scandolo | 231c754 | 2016-05-20 11:13:11 -0700 | [diff] [blame] | 21 | describe('factory: fw/remote/websocket.js', function () { |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 22 | var $log, fs, wss; |
| 23 | |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 24 | var noop = function () {}, |
| 25 | send = jasmine.createSpy('send').and.callFake(function (ev) { |
| 26 | return ev; |
| 27 | }), |
| 28 | mockWebSocket = { |
| 29 | send: send |
| 30 | }; |
| 31 | |
Simon Hunt | 2d16fc8 | 2015-03-10 20:19:52 -0700 | [diff] [blame] | 32 | beforeEach(module('onosRemote', 'onosLayer', 'ngRoute', 'onosNav', 'onosSvg')); |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 33 | |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 34 | 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 Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 52 | beforeEach(module(function($provide) { |
Simon Hunt | 2d16fc8 | 2015-03-10 20:19:52 -0700 | [diff] [blame] | 53 | $provide.factory('$location', function () { |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 54 | return { |
| 55 | protocol: function () { return 'http'; }, |
| 56 | host: function () { return 'foo'; }, |
Matteo Scandolo | 231c754 | 2016-05-20 11:13:11 -0700 | [diff] [blame] | 57 | port: function () { return '80'; }, |
| 58 | search: function() { |
| 59 | return {debug: 'true'}; |
Simon Hunt | fa875bb | 2016-08-24 11:41:02 -0700 | [diff] [blame] | 60 | }, |
| 61 | absUrl: function () { |
| 62 | return 'ws://foo:123/onos/ui/websock/path'; |
Matteo Scandolo | 231c754 | 2016-05-20 11:13:11 -0700 | [diff] [blame] | 63 | } |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 64 | }; |
| 65 | }) |
| 66 | })); |
| 67 | |
| 68 | beforeEach(inject(function (_$log_, FnService, WebSocketService) { |
| 69 | $log = _$log_; |
| 70 | fs = FnService; |
| 71 | wss = WebSocketService; |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 72 | wss.resetState(); |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 73 | })); |
| 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 Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 82 | 'resetState', |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 83 | 'createWebSocket', 'bindHandlers', 'unbindHandlers', |
Matteo Scandolo | 231c754 | 2016-05-20 11:13:11 -0700 | [diff] [blame] | 84 | 'addOpenListener', 'removeOpenListener', 'sendEvent', |
| 85 | 'isConnected', 'loggedInUser', |
| 86 | '_setVeilDelegate', '_setLoadingDelegate' |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 87 | ])).toBeTruthy(); |
| 88 | }); |
| 89 | |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 90 | it('should use the appropriate URL, createWebsocket', function () { |
Simon Hunt | 2d16fc8 | 2015-03-10 20:19:52 -0700 | [diff] [blame] | 91 | var url = wss.createWebSocket(); |
| 92 | expect(url).toEqual('ws://foo:80/onos/ui/websock/core'); |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 93 | }); |
| 94 | |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 95 | 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 Hunt | acf410b | 2015-01-23 10:05:48 -0800 | [diff] [blame] | 99 | }); |
| 100 | |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 101 | 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 Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 121 | payload: { mock: 'thing' } |
| 122 | }; |
| 123 | wss.sendEvent(fakeEvent.event, fakeEvent.payload); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 124 | // on opening the socket, a single authentication event should have |
| 125 | // been sent already... |
| 126 | expect(mockWebSocket.send.calls.count()).toEqual(1); |
| 127 | |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 128 | wss.createWebSocket({ wsport: 1234 }); |
| 129 | mockWebSocket.onopen(); |
| 130 | expect(mockWebSocket.send).toHaveBeenCalledWith(JSON.stringify(fakeEvent)); |
| 131 | }); |
| 132 | |
| 133 | it('should handle an incoming bad JSON message, handleMessage', function () { |
| 134 | spyOn($log, 'error'); |
| 135 | var badMsg = { |
| 136 | data: 'bad message' |
| 137 | }; |
| 138 | wss.createWebSocket({ wsport: 1234 }); |
| 139 | expect(mockWebSocket.onmessage(badMsg)).toBeNull(); |
| 140 | expect($log.error).toHaveBeenCalled(); |
| 141 | }); |
| 142 | |
| 143 | it('should verify message was handled, handleMessage', function () { |
| 144 | var num = 0, |
| 145 | fakeHandler = { |
| 146 | mockEvResp: function () { num++; } |
| 147 | }, |
| 148 | data = JSON.stringify({ |
| 149 | event: 'mockEvResp', |
| 150 | payload: {} |
| 151 | }), |
| 152 | event = { |
| 153 | data: data |
| 154 | }; |
| 155 | wss.createWebSocket({ wsport: 1234 }); |
| 156 | wss.bindHandlers(fakeHandler); |
| 157 | expect(mockWebSocket.onmessage(event)).toBe(undefined); |
| 158 | expect(num).toBe(1); |
| 159 | }); |
| 160 | |
| 161 | it('should warn if there is an unhandled event, handleMessage', function () { |
| 162 | spyOn($log, 'warn'); |
| 163 | var data = { foo: 'bar', bar: 'baz'}, |
| 164 | dataString = JSON.stringify(data), |
| 165 | badEv = { |
| 166 | data: dataString |
| 167 | }; |
| 168 | wss.createWebSocket({ wsport: 1234 }); |
| 169 | mockWebSocket.onmessage(badEv); |
| 170 | expect($log.warn).toHaveBeenCalledWith('Unhandled event:', data); |
| 171 | }); |
| 172 | |
| 173 | it('should not warn if valid input, bindHandlers', function () { |
| 174 | spyOn($log, 'warn'); |
| 175 | expect(wss.bindHandlers({ |
| 176 | foo: noop, |
| 177 | bar: noop |
| 178 | })).toBe(undefined); |
| 179 | expect($log.warn).not.toHaveBeenCalled(); |
| 180 | }); |
| 181 | |
| 182 | it('should warn if no arguments, bindHandlers', function () { |
| 183 | spyOn($log, 'warn'); |
| 184 | expect(wss.bindHandlers()).toBeNull(); |
| 185 | expect($log.warn).toHaveBeenCalledWith( |
| 186 | 'WSS.bindHandlers(): no event handlers' |
| 187 | ); |
| 188 | expect(wss.bindHandlers({})).toBeNull(); |
| 189 | expect($log.warn).toHaveBeenCalledWith( |
| 190 | 'WSS.bindHandlers(): no event handlers' |
| 191 | ); |
| 192 | }); |
| 193 | |
| 194 | it('should warn if handler is not a function, bindHandlers', function () { |
| 195 | spyOn($log, 'warn'); |
| 196 | expect(wss.bindHandlers({ |
| 197 | foo: 'handler1', |
| 198 | bar: 3, |
| 199 | baz: noop |
| 200 | })).toBe(undefined); |
| 201 | expect($log.warn).toHaveBeenCalledWith('foo handler not a function'); |
| 202 | expect($log.warn).toHaveBeenCalledWith('bar handler not a function'); |
| 203 | }); |
| 204 | |
| 205 | it('should warn if duplicate handlers were given, bindHandlers', |
| 206 | function () { |
| 207 | spyOn($log, 'warn'); |
| 208 | wss.bindHandlers({ |
| 209 | foo: noop |
| 210 | }); |
| 211 | expect(wss.bindHandlers({ |
| 212 | foo: noop |
| 213 | })).toBe(undefined); |
| 214 | expect($log.warn).toHaveBeenCalledWith('duplicate bindings ignored:', |
| 215 | ['foo']); |
| 216 | }); |
| 217 | |
| 218 | it('should warn if no arguments, unbindHandlers', function () { |
| 219 | spyOn($log, 'warn'); |
| 220 | expect(wss.unbindHandlers()).toBeNull(); |
| 221 | expect($log.warn).toHaveBeenCalledWith( |
| 222 | 'WSS.unbindHandlers(): no event handlers' |
| 223 | ); |
| 224 | expect(wss.unbindHandlers({})).toBeNull(); |
| 225 | expect($log.warn).toHaveBeenCalledWith( |
| 226 | 'WSS.unbindHandlers(): no event handlers' |
| 227 | ); |
| 228 | }); |
| 229 | // Note: cannot test unbindHandlers' forEach due to it using closure variable |
| 230 | |
| 231 | it('should not warn if valid argument, addOpenListener', function () { |
| 232 | spyOn($log, 'warn'); |
| 233 | var o = wss.addOpenListener(noop); |
| 234 | expect(o.id === 1); |
| 235 | expect(o.cb === noop); |
| 236 | expect($log.warn).not.toHaveBeenCalled(); |
| 237 | o = wss.addOpenListener(noop); |
| 238 | expect(o.id === 2); |
| 239 | expect(o.cb === noop); |
| 240 | expect($log.warn).not.toHaveBeenCalled(); |
| 241 | }); |
| 242 | |
| 243 | it('should log error if callback not a function, addOpenListener', |
| 244 | function () { |
| 245 | spyOn($log, 'error'); |
| 246 | var o = wss.addOpenListener('foo'); |
| 247 | expect(o.id === 1); |
| 248 | expect(o.cb === 'foo'); |
| 249 | expect(o.error === 'No callback defined'); |
| 250 | expect($log.error).toHaveBeenCalledWith( |
| 251 | 'WSS.addOpenListener(): callback not a function' |
| 252 | ); |
| 253 | }); |
| 254 | |
| 255 | it('should not warn if valid listener object, removeOpenListener', function () { |
| 256 | spyOn($log, 'warn'); |
| 257 | expect(wss.removeOpenListener({ |
| 258 | id: 1, |
| 259 | cb: noop |
| 260 | })).toBe(undefined); |
| 261 | expect($log.warn).not.toHaveBeenCalled(); |
| 262 | }); |
| 263 | |
| 264 | it('should warn if listener is invalid, removeOpenListener', function () { |
| 265 | spyOn($log, 'warn'); |
| 266 | expect(wss.removeOpenListener({})).toBeNull(); |
| 267 | expect($log.warn).toHaveBeenCalledWith( |
| 268 | 'WSS.removeOpenListener(): invalid listener', {} |
| 269 | ); |
| 270 | expect(wss.removeOpenListener('listener')).toBeNull(); |
| 271 | expect($log.warn).toHaveBeenCalledWith( |
| 272 | 'WSS.removeOpenListener(): invalid listener', 'listener' |
| 273 | ); |
| 274 | }); |
| 275 | |
| 276 | // Note: handleClose is not currently tested due to all work it does relies |
| 277 | // on closure variables that cannot be mocked |
Simon Hunt | acf410b | 2015-01-23 10:05:48 -0800 | [diff] [blame] | 278 | |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 279 | }); |