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