GUI -- Implemented sendEvent() in WsEventService.
Change-Id: Iac4833e66e7688e490b24b4cbaca3514a59ce618
Showing
2 changed files
with
97 additions
and
2 deletions
| ... | @@ -20,11 +20,28 @@ | ... | @@ -20,11 +20,28 @@ |
| 20 | (function () { | 20 | (function () { |
| 21 | 'use strict'; | 21 | 'use strict'; |
| 22 | 22 | ||
| 23 | + var sid = 0; | ||
| 24 | + | ||
| 23 | angular.module('onosRemote') | 25 | angular.module('onosRemote') |
| 24 | - .factory('WsEventService', ['$location', function ($loc) { | 26 | + .factory('WsEventService', [function () { |
| 27 | + | ||
| 28 | + function sendEvent(ws, evType, payload) { | ||
| 29 | + var p = payload || {}; | ||
| 30 | + | ||
| 31 | + ws.send({ | ||
| 32 | + event: evType, | ||
| 33 | + sid: ++sid, | ||
| 34 | + payload: p | ||
| 35 | + }); | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + function resetSid() { | ||
| 39 | + sid = 0; | ||
| 40 | + } | ||
| 25 | 41 | ||
| 26 | return { | 42 | return { |
| 27 | - tbd: function () {} | 43 | + sendEvent: sendEvent, |
| 44 | + resetSid: resetSid | ||
| 28 | }; | 45 | }; |
| 29 | }]); | 46 | }]); |
| 30 | 47 | ... | ... |
| 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 Event Service - Unit Tests | ||
| 19 | + */ | ||
| 20 | +describe('factory: fw/remote/wsevent.js', function () { | ||
| 21 | + var $log, fs, wse; | ||
| 22 | + | ||
| 23 | + beforeEach(module('onosRemote')); | ||
| 24 | + | ||
| 25 | + beforeEach(inject(function (_$log_, FnService, WsEventService) { | ||
| 26 | + $log = _$log_; | ||
| 27 | + fs = FnService; | ||
| 28 | + wse = WsEventService; | ||
| 29 | + wse.resetSid(); | ||
| 30 | + })); | ||
| 31 | + | ||
| 32 | + | ||
| 33 | + it('should define WsEventService', function () { | ||
| 34 | + expect(wse).toBeDefined(); | ||
| 35 | + }); | ||
| 36 | + | ||
| 37 | + it('should define api functions', function () { | ||
| 38 | + expect(fs.areFunctions(wse, [ | ||
| 39 | + 'sendEvent' | ||
| 40 | + ])).toBeTruthy(); | ||
| 41 | + }); | ||
| 42 | + | ||
| 43 | + var event, | ||
| 44 | + fakeWs = { | ||
| 45 | + send: function (ev) { | ||
| 46 | + event = ev; | ||
| 47 | + } | ||
| 48 | + }; | ||
| 49 | + | ||
| 50 | + it('should construct an event object with no payload', function () { | ||
| 51 | + wse.sendEvent(fakeWs, 'foo'); | ||
| 52 | + expect(event.event).toEqual('foo'); | ||
| 53 | + expect(event.sid).toEqual(1); | ||
| 54 | + expect(event.payload).toEqual({}); | ||
| 55 | + }); | ||
| 56 | + | ||
| 57 | + it('should construct an event object with some payload', function () { | ||
| 58 | + wse.sendEvent(fakeWs, 'bar', { val: 42 }); | ||
| 59 | + expect(event.event).toEqual('bar'); | ||
| 60 | + expect(event.sid).toEqual(1); | ||
| 61 | + expect(event.payload).toEqual({ val: 42 }); | ||
| 62 | + }); | ||
| 63 | + | ||
| 64 | + it('should increment the sid', function () { | ||
| 65 | + wse.sendEvent(fakeWs, 'one'); | ||
| 66 | + expect(event.event).toEqual('one'); | ||
| 67 | + expect(event.sid).toEqual(1); | ||
| 68 | + | ||
| 69 | + wse.sendEvent(fakeWs, 'dos'); | ||
| 70 | + expect(event.event).toEqual('dos'); | ||
| 71 | + expect(event.sid).toEqual(2); | ||
| 72 | + | ||
| 73 | + wse.sendEvent(fakeWs, 'tres'); | ||
| 74 | + expect(event.event).toEqual('tres'); | ||
| 75 | + expect(event.sid).toEqual(3); | ||
| 76 | + }); | ||
| 77 | + | ||
| 78 | +}); |
-
Please register or login to post a comment