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