blob: 7a9e54f0956ac5a9446692963458ebdec2b5cb35 [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandolo02229382017-04-18 11:52:23 -070019import * as angular from 'angular';
20import 'angular-mocks';
21import 'angular-ui-router';
22import {XosModelDiscovererService, IXosModelDiscovererService} from './model-discoverer.service';
23import {IXosModeldef} from '../rest/modeldefs.rest';
24import {BehaviorSubject} from 'rxjs';
25
26const stubModels: IXosModeldef[] = [
27 {
28 fields: [
29 {name: 'id', type: 'number'},
30 {name: 'foo', type: 'string'}
31 ],
32 relations: [],
33 name: 'Node',
Matteo Scandolo580033a2017-08-17 11:16:39 -070034 app: 'core',
35 description: '',
36 verbose_name: ''
Matteo Scandolo02229382017-04-18 11:52:23 -070037 },
38 {
39 fields: [
40 {name: 'id', type: 'number'},
41 {name: 'bar', type: 'string'}
42 ],
43 relations: [],
44 name: 'VSGTenant',
Matteo Scandolo580033a2017-08-17 11:16:39 -070045 app: 'service.vsg',
46 description: '',
47 verbose_name: ''
Matteo Scandolo02229382017-04-18 11:52:23 -070048 }
49];
50
51let service: IXosModelDiscovererService;
52let scope: ng.IScope;
53const MockXosModelDefs = {
54 get: null
55};
56let MockXosRuntimeStates = {
57 addState: jasmine.createSpy('runtimeState.addState')
58 .and.callFake(() => true)
59};
60let MockConfigHelpers = {
61 pluralize: jasmine.createSpy('config.pluralize')
62 .and.callFake((string: string) => `${string}s`),
63 toLabel: jasmine.createSpy('config.toLabel')
64 .and.callFake((string: string) => string.toLowerCase()),
65 modelToTableCfg: jasmine.createSpy('config.modelToTableCfg')
66 .and.callFake(() => true),
67 modelToFormCfg: jasmine.createSpy('config.modelToFormCfg')
68 .and.callFake(() => true),
69};
70let MockXosNavigationService = {
71 add: jasmine.createSpy('navigationService.add')
72 .and.callFake(() => true)
73};
74const MockXosModelStore = {
75 query: jasmine.createSpy('modelStore.query')
76 .and.callFake(() => {
77 const list = new BehaviorSubject([]);
78 list.next([]);
79 return list.asObservable();
80 })
81};
82const MockProgressBar = {
83 setColor: jasmine.createSpy('progressBar.setColor'),
84 start: jasmine.createSpy('progressBar.start'),
85 complete: jasmine.createSpy('progressBar.complete')
86};
87const MockngProgressFactory = {
88 createInstance: jasmine.createSpy('ngProgress.createInstance')
89 .and.callFake(() => MockProgressBar)
90};
91
92describe('The ModelDicoverer service', () => {
93
94 beforeEach(() => {
95 angular
96 .module('test', [])
97 .service('XosModelDiscoverer', XosModelDiscovererService)
98 .value('ConfigHelpers', MockConfigHelpers)
99 .value('XosModelDefs', MockXosModelDefs)
100 .value('XosRuntimeStates', MockXosRuntimeStates)
101 .value('XosModelStore', MockXosModelStore)
102 .value('ngProgressFactory', MockngProgressFactory)
Matteo Scandolo0f3692e2017-07-10 14:06:41 -0700103 .value('XosNavigationService', MockXosNavigationService)
104 .value('AuthService', {});
Matteo Scandolo02229382017-04-18 11:52:23 -0700105
106 angular.mock.module('test');
107 });
108
109 beforeEach(angular.mock.inject((
110 XosModelDiscoverer: IXosModelDiscovererService,
111 $rootScope: ng.IScope,
112 $q: ng.IQService
113 ) => {
114 service = XosModelDiscoverer;
115 scope = $rootScope;
116 MockXosModelDefs.get = jasmine.createSpy('modelDefs.get')
117 .and.callFake(() => {
118 const d = $q.defer();
119 d.resolve(stubModels);
120 return d.promise;
121 });
122 }));
123
124 it('should setup the progress bar', () => {
125 expect(MockngProgressFactory.createInstance).toHaveBeenCalled();
126 expect(MockProgressBar.setColor).toHaveBeenCalled();
127 });
128
129 it('should not have loaded models', () => {
130 expect(service.areModelsLoaded()).toBeFalsy();
131 });
132
133 it('should get the url from a core model', () => {
134 const model = {
135 name: 'Node',
136 app: 'core',
Matteo Scandolo580033a2017-08-17 11:16:39 -0700137 fields: [],
138 description: '',
139 verbose_name: ''
Matteo Scandolo02229382017-04-18 11:52:23 -0700140 };
141 expect(service.getApiUrlFromModel(model)).toBe('/core/nodes');
142 });
143
144 it('should get the url from a service model', () => {
145 const model = {
146 name: 'Tenant',
147 app: 'services.test',
Matteo Scandolo580033a2017-08-17 11:16:39 -0700148 fields: [],
149 description: '',
150 verbose_name: ''
Matteo Scandolo02229382017-04-18 11:52:23 -0700151 };
152 expect(service.getApiUrlFromModel(model)).toBe('/test/tenants');
153 });
154
155 it('should retrieve a model definition from local cache', () => {
156 const model = {
157 name: 'Node',
158 app: 'core'
159 };
160 service['xosModels'] = [
161 model
162 ];
163 expect(service.get('Node')).toEqual(model);
164 });
165
166 it('should get the service name from the app name', () => {
167 expect(service['serviceNameFromAppName']('services.vsg')).toBe('vsg');
168 });
169
170 it('should get the state name from the model', () => {
171 expect(service['stateNameFromModel']({name: 'Tenant', app: 'services.vsg'})).toBe('xos.vsg.tenant');
172 });
173
174 it('should get the parent state name from a core model', () => {
175 expect(service['getParentStateFromModel']({name: 'Nodes', app: 'core'})).toBe('xos.core');
176 });
177
178 it('should get the parent state name from a service model', () => {
179 expect(service['getParentStateFromModel']({name: 'Tenant', app: 'services.vsg'})).toBe('xos.vsg');
180 });
181
182 it('should add a new service entry in the system', () => {
183 service['addService']({name: 'Tenant', app: 'services.vsg'});
184 expect(MockXosRuntimeStates.addState).toHaveBeenCalledWith('xos.vsg', {
185 url: 'vsg',
186 parent: 'xos',
187 abstract: true,
188 template: '<div ui-view></div>'
189 });
190 expect(MockXosNavigationService.add).toHaveBeenCalledWith({
191 label: 'vsg',
192 state: 'xos.vsg'
193 });
194 expect(service['xosServices'][0]).toEqual('vsg');
195 expect(service['xosServices'].length).toBe(1);
196 });
197
198 it('should add a state in the system', (done) => {
199 MockXosRuntimeStates.addState.calls.reset();
200 service['addState']({name: 'Tenant', app: 'services.vsg'})
201 .then((model) => {
202 expect(MockXosRuntimeStates.addState).toHaveBeenCalledWith('xos.vsg.tenant', {
203 parent: 'xos.vsg',
204 url: '/tenants/:id?',
205 params: {
206 id: null
207 },
208 data: {
209 model: 'Tenant'
210 },
211 component: 'xosCrud',
212 });
213 expect(model.clientUrl).toBe('vsg/tenants/:id?');
214 done();
215 });
216 scope.$apply();
217 });
218
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700219 it('should add a state with relations in the system', (done) => {
220 MockXosRuntimeStates.addState.calls.reset();
221 service['addState']({name: 'Tenant', app: 'services.vsg', relations: [{model: 'Something', type: 'manytoone'}]})
222 .then((model) => {
223 expect(MockXosRuntimeStates.addState).toHaveBeenCalledWith('xos.vsg.tenant', {
224 parent: 'xos.vsg',
225 url: '/tenants/:id?',
226 params: {
227 id: null
228 },
229 data: {
230 model: 'Tenant',
231 relations: [
232 {model: 'Something', type: 'manytoone'}
233 ]
234 },
235 component: 'xosCrud',
236 });
237 expect(model.clientUrl).toBe('vsg/tenants/:id?');
238 done();
239 });
240 scope.$apply();
241 });
242
Matteo Scandolo02229382017-04-18 11:52:23 -0700243 it('should add an item to navigation', () => {
244 service['addNavItem']({name: 'Tenant', app: 'services.vsg'});
245 expect(MockXosNavigationService.add).toHaveBeenCalledWith({
246 label: 'Tenants',
247 state: 'xos.vsg.tenant',
248 parent: 'xos.vsg'
249 });
Matteo Scandolo580033a2017-08-17 11:16:39 -0700250
251 service['addNavItem']({name: 'Tenant', verbose_name: 'Verbose', app: 'services.vsg'});
252 expect(MockXosNavigationService.add).toHaveBeenCalledWith({
253 label: 'Verboses',
254 state: 'xos.vsg.tenant',
255 parent: 'xos.vsg'
256 });
Matteo Scandolo02229382017-04-18 11:52:23 -0700257 });
258
259 it('should cache a model', () => {
260 service['cacheModelEntries']({name: 'Tenant', app: 'services.vsg'});
261 expect(MockXosModelStore.query).toHaveBeenCalledWith('Tenant', '/vsg/tenants');
262 });
263
264 it('should get the table config', () => {
265 service['getTableCfg']({name: 'Tenant', app: 'services.vsg'});
266 expect(MockConfigHelpers.modelToTableCfg).toHaveBeenCalledWith(
267 {name: 'Tenant', app: 'services.vsg', tableCfg: true},
268 'xos.vsg.tenant'
269 );
270 });
271
272 it('should get the form config', () => {
273 service['getFormCfg']({name: 'Tenant', app: 'services.vsg'});
274 expect(MockConfigHelpers.modelToFormCfg).toHaveBeenCalledWith(
275 {name: 'Tenant', app: 'services.vsg', formCfg: true}
276 );
277 });
278
279 it('should store the model in memory', () => {
280 service['storeModel']({name: 'Tenant'});
281 expect(service['xosModels'][0]).toEqual({name: 'Tenant'});
282 expect(service['xosModels'].length).toEqual(1);
283 });
284
285 describe('when discovering models', () => {
286 beforeEach(() => {
287 spyOn(service, 'cacheModelEntries').and.callThrough();
288 spyOn(service, 'addState').and.callThrough();
289 spyOn(service, 'addNavItem').and.callThrough();
290 spyOn(service, 'getTableCfg').and.callThrough();
291 spyOn(service, 'getFormCfg').and.callThrough();
292 spyOn(service, 'storeModel').and.callThrough();
293 });
294
295 it('should call all the function chain', (done) => {
296 service.discover()
297 .then((res) => {
298 expect(MockProgressBar.start).toHaveBeenCalled();
299 expect(MockXosModelDefs.get).toHaveBeenCalled();
300 expect(service['cacheModelEntries'].calls.count()).toBe(2);
301 expect(service['addState'].calls.count()).toBe(2);
302 expect(service['addNavItem'].calls.count()).toBe(2);
303 expect(service['getTableCfg'].calls.count()).toBe(2);
304 expect(service['getFormCfg'].calls.count()).toBe(2);
305 expect(service['storeModel'].calls.count()).toBe(2);
306 expect(res).toBeTruthy();
307 done();
308 });
309 scope.$apply();
310 });
311 });
312});