Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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'}; |
| 60 | } |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 61 | }; |
| 62 | }) |
| 63 | })); |
| 64 | |
| 65 | beforeEach(inject(function (_$log_, FnService, WebSocketService) { |
| 66 | $log = _$log_; |
| 67 | fs = FnService; |
| 68 | wss = WebSocketService; |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 69 | wss.resetState(); |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 70 | })); |
| 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 Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 79 | 'resetSid', 'resetState', |
| 80 | 'createWebSocket', 'bindHandlers', 'unbindHandlers', |
Matteo Scandolo | 231c754 | 2016-05-20 11:13:11 -0700 | [diff] [blame] | 81 | 'addOpenListener', 'removeOpenListener', 'sendEvent', |
| 82 | 'isConnected', 'loggedInUser', |
| 83 | '_setVeilDelegate', '_setLoadingDelegate' |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 84 | ])).toBeTruthy(); |
| 85 | }); |
| 86 | |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 87 | it('should use the appropriate URL, createWebsocket', function () { |
Simon Hunt | 2d16fc8 | 2015-03-10 20:19:52 -0700 | [diff] [blame] | 88 | var url = wss.createWebSocket(); |
| 89 | expect(url).toEqual('ws://foo:80/onos/ui/websock/core'); |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 90 | }); |
| 91 | |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 92 | 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 Hunt | acf410b | 2015-01-23 10:05:48 -0800 | [diff] [blame] | 96 | }); |
| 97 | |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 98 | 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 Hunt | acf410b | 2015-01-23 10:05:48 -0800 | [diff] [blame] | 273 | |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 274 | }); |