blob: 7adaf070a9eb83aba3dc8b724d4ff7a1e1dfe271 [file] [log] [blame]
Matteo Scandolo06542252018-01-30 13:35:55 -08001/*
2 * Copyright 2017-present Open Networking Foundation
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
17import * as angular from 'angular';
18import 'angular-mocks';
19import {IWSEvent, IWSEventService, WebSocketEvent} from './global';
20
21const MockAppCfg = {
22 apiEndpoint: 'http://xos-test:3000/api',
23 websocketClient: 'http://xos-test:3000'
24};
25
26class MockSocket {
27 private cbs = {};
28
29 public on(event: string, cb: any) {
30 this.cbs[event] = cb;
31 }
32
33 public send(evt: string, data: any) {
34 const cb = this.cbs[evt];
35 cb(data);
36 }
37
38 public clean() {
39 this.cbs = {};
40 }
41}
42
43describe('The WebSocket service', () => {
44
45 let service, observable, mockWS;
46
47 beforeEach(() => {
48
49 mockWS = new MockSocket();
50
51 angular
52 .module('WebSocketEvent', [])
53 .service('WebSocket', WebSocketEvent)
54 .constant('AppConfig', MockAppCfg)
55 .value('SocketIo', {socket: mockWS});
56
57 angular.mock.module('WebSocketEvent');
58 });
59
60 beforeEach(angular.mock.inject((
61 WebSocket: IWSEventService
62 ) => {
63 service = WebSocket;
64 observable = service['_events'];
65
66 spyOn(observable, 'next');
67 }));
68
69 afterEach(() => {
70 mockWS.clean();
71 observable.next.calls.reset();
72 });
73
74 it('should have a list method', () => {
75 expect(service.list).toBeDefined();
76 });
77
78 describe('the update event', () => {
79 it('should update the base class', () => {
80 const data: IWSEvent = {
81 model: 'Test',
82 msg: {
83 pk: 1,
84 changed_fields: ['name'],
85 object: {}
86 }
87 };
88 mockWS.send('update', data);
89 expect(observable.next).toHaveBeenCalledWith(data);
90 expect(observable.next.calls.count()).toBe(1);
91 });
92
93 it('should not update the class if the changed_fields are not useful to the UI', () => {
94 const data: IWSEvent = {
95 model: 'Test',
96 msg: {
97 pk: 1,
98 changed_fields: ['created', 'updated', 'backend_register', 'backend_status', 'policy_status'],
99 object: {}
100 }
101 };
102 mockWS.send('update', data);
103 expect(observable.next).not.toHaveBeenCalled();
104 });
105
106 it('should update parent classes (if any)', () => {
107 const data: IWSEvent = {
108 model: 'ONOSApp',
109 msg: {
110 pk: 1,
111 changed_fields: ['name'],
112 object: {
113 class_names: 'ONOSApp,ONOSApp_decl,ServiceInstance,XOSBase,Model,PlModelMixIn,AttributeMixin,object'
114 }
115 }
116 };
117 mockWS.send('update', data);
118 expect(observable.next).toHaveBeenCalledWith(data);
119 const siEvent = data;
120 siEvent.model = 'ServiceInstance';
121 siEvent.skip_notification = true;
122 expect(observable.next).toHaveBeenCalledWith(siEvent);
123 expect(observable.next.calls.count()).toBe(2);
124 });
125 });
126
127 describe('the remove event', () => {
128 it('should trigger the remove event', () => {
129 const data: IWSEvent = {
130 model: 'Test',
131 msg: {
132 pk: 1,
133 changed_fields: [],
134 object: {}
135 },
136 };
137 mockWS.send('remove', data);
138 expect(observable.next).toHaveBeenCalledWith(data);
139 });
140
141 it('should update parent classes (if any)', () => {
142 const data: IWSEvent = {
143 model: 'ONOSApp',
144 msg: {
145 pk: 1,
146 changed_fields: ['name'],
147 object: {
148 class_names: 'ONOSApp,ONOSApp_decl,ServiceInstance,XOSBase,Model,PlModelMixIn,AttributeMixin,object'
149 }
150 }
151 };
152 mockWS.send('remove', data);
153 expect(observable.next).toHaveBeenCalledWith(data);
154 const siEvent = data;
155 siEvent.model = 'ServiceInstance';
156 siEvent.skip_notification = true;
157 expect(observable.next).toHaveBeenCalledWith(siEvent);
158 expect(observable.next.calls.count()).toBe(2);
159 });
160
161 it('should update derived class if the original is _decl', () => {
162 const data: IWSEvent = {
163 model: 'ONOSApp_decl',
164 msg: {
165 pk: 1,
166 changed_fields: ['name'],
167 object: {
168 class_names: 'ONOSApp_decl,ServiceInstance,XOSBase,Model,PlModelMixIn,AttributeMixin,object'
169 }
170 }
171 };
172 mockWS.send('remove', data);
173
174 const nextData = angular.copy(data);
175 nextData.model = 'ONOSApp';
176 expect(observable.next).toHaveBeenCalledWith(nextData);
177 const declEvent = nextData;
178 declEvent.model = 'ServiceInstance';
179 declEvent.skip_notification = true;
180 expect(observable.next).toHaveBeenCalledWith(declEvent);
181 expect(observable.next.calls.count()).toBe(2);
182 });
183 });
184});