blob: bcdec0df3fe8777e00884b1db433b2f401ba80c5 [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001/*
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
17
Matteo Scandolo02229382017-04-18 11:52:23 -070018import * as angular from 'angular';
19import 'angular-mocks';
20import 'angular-ui-router';
21import {XosModelDiscovererService, IXosModelDiscovererService} from './model-discoverer.service';
22import {IXosModeldef} from '../rest/modeldefs.rest';
23import {BehaviorSubject} from 'rxjs';
Matteo Scandolo63498472017-09-26 17:21:41 -070024import {XosModeldefsCache} from './modeldefs.service';
Matteo Scandolo02229382017-04-18 11:52:23 -070025
26const stubModels: IXosModeldef[] = [
27 {
28 fields: [
Matteo Scandolod67adee2018-03-08 16:27:05 -080029 {name: 'id', type: 'number', read_only: false},
30 {name: 'foo', type: 'string', read_only: false}
Matteo Scandolo02229382017-04-18 11:52:23 -070031 ],
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: [
Matteo Scandolod67adee2018-03-08 16:27:05 -080040 {name: 'id', type: 'number', read_only: false},
41 {name: 'bar', type: 'string', read_only: false}
Matteo Scandolo02229382017-04-18 11:52:23 -070042 ],
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')
Matteo Scandolo29edc0f2018-04-26 17:19:10 +020076 .and.callFake((model) => {
Matteo Scandolo02229382017-04-18 11:52:23 -070077 const list = new BehaviorSubject([]);
78 list.next([]);
Matteo Scandolo29edc0f2018-04-26 17:19:10 +020079 window.setTimeout(() => {
80 // NOTE force the Observable to call next two times, as cacheModelEntries is not caching the first result (since it's generated)
81 list.next([]);
82 }, 100);
Matteo Scandolo02229382017-04-18 11:52:23 -070083 return list.asObservable();
84 })
85};
86const MockProgressBar = {
87 setColor: jasmine.createSpy('progressBar.setColor'),
Matteo Scandolobf4e8402019-06-24 12:22:49 -070088 set: jasmine.createSpy('progressBar.set'),
Matteo Scandolo02229382017-04-18 11:52:23 -070089 complete: jasmine.createSpy('progressBar.complete')
90};
91const MockngProgressFactory = {
92 createInstance: jasmine.createSpy('ngProgress.createInstance')
93 .and.callFake(() => MockProgressBar)
94};
95
96describe('The ModelDicoverer service', () => {
97
98 beforeEach(() => {
99 angular
100 .module('test', [])
101 .service('XosModelDiscoverer', XosModelDiscovererService)
102 .value('ConfigHelpers', MockConfigHelpers)
103 .value('XosModelDefs', MockXosModelDefs)
104 .value('XosRuntimeStates', MockXosRuntimeStates)
105 .value('XosModelStore', MockXosModelStore)
106 .value('ngProgressFactory', MockngProgressFactory)
Matteo Scandolo0f3692e2017-07-10 14:06:41 -0700107 .value('XosNavigationService', MockXosNavigationService)
Matteo Scandolo63498472017-09-26 17:21:41 -0700108 .value('AuthService', {})
109 .service('XosModeldefsCache', XosModeldefsCache);
Matteo Scandolo02229382017-04-18 11:52:23 -0700110
111 angular.mock.module('test');
112 });
113
114 beforeEach(angular.mock.inject((
115 XosModelDiscoverer: IXosModelDiscovererService,
116 $rootScope: ng.IScope,
117 $q: ng.IQService
118 ) => {
119 service = XosModelDiscoverer;
120 scope = $rootScope;
121 MockXosModelDefs.get = jasmine.createSpy('modelDefs.get')
122 .and.callFake(() => {
123 const d = $q.defer();
124 d.resolve(stubModels);
125 return d.promise;
126 });
127 }));
128
129 it('should setup the progress bar', () => {
130 expect(MockngProgressFactory.createInstance).toHaveBeenCalled();
131 expect(MockProgressBar.setColor).toHaveBeenCalled();
132 });
133
134 it('should not have loaded models', () => {
135 expect(service.areModelsLoaded()).toBeFalsy();
136 });
137
138 it('should get the url from a core model', () => {
139 const model = {
140 name: 'Node',
141 app: 'core',
Matteo Scandolo580033a2017-08-17 11:16:39 -0700142 fields: [],
143 description: '',
144 verbose_name: ''
Matteo Scandolo02229382017-04-18 11:52:23 -0700145 };
146 expect(service.getApiUrlFromModel(model)).toBe('/core/nodes');
147 });
148
149 it('should get the url from a service model', () => {
150 const model = {
151 name: 'Tenant',
152 app: 'services.test',
Matteo Scandolo580033a2017-08-17 11:16:39 -0700153 fields: [],
154 description: '',
155 verbose_name: ''
Matteo Scandolo02229382017-04-18 11:52:23 -0700156 };
157 expect(service.getApiUrlFromModel(model)).toBe('/test/tenants');
158 });
159
Matteo Scandolo02229382017-04-18 11:52:23 -0700160 it('should get the state name from the model', () => {
161 expect(service['stateNameFromModel']({name: 'Tenant', app: 'services.vsg'})).toBe('xos.vsg.tenant');
162 });
163
164 it('should get the parent state name from a core model', () => {
165 expect(service['getParentStateFromModel']({name: 'Nodes', app: 'core'})).toBe('xos.core');
166 });
167
168 it('should get the parent state name from a service model', () => {
169 expect(service['getParentStateFromModel']({name: 'Tenant', app: 'services.vsg'})).toBe('xos.vsg');
170 });
171
Matteo Scandolo29edc0f2018-04-26 17:19:10 +0200172 it('should add an item to navigation', () => {
173 service['addNavItem']({name: 'Tenant', app: 'services.vsg'});
174 expect(MockXosNavigationService.add).toHaveBeenCalledWith({
175 label: 'Tenants',
176 state: 'xos.vsg.tenant',
177 parent: 'xos.vsg'
178 });
179
180 service['addNavItem']({name: 'Tenant', verbose_name: 'Verbose', app: 'services.vsg'});
181 expect(MockXosNavigationService.add).toHaveBeenCalledWith({
182 label: 'Verboses',
183 state: 'xos.vsg.tenant',
184 parent: 'xos.vsg'
185 });
186 });
187
Matteo Scandolo02229382017-04-18 11:52:23 -0700188 it('should add a new service entry in the system', () => {
189 service['addService']({name: 'Tenant', app: 'services.vsg'});
190 expect(MockXosRuntimeStates.addState).toHaveBeenCalledWith('xos.vsg', {
191 url: 'vsg',
192 parent: 'xos',
193 abstract: true,
194 template: '<div ui-view></div>'
195 });
196 expect(MockXosNavigationService.add).toHaveBeenCalledWith({
197 label: 'vsg',
198 state: 'xos.vsg'
199 });
200 expect(service['xosServices'][0]).toEqual('vsg');
201 expect(service['xosServices'].length).toBe(1);
202 });
203
204 it('should add a state in the system', (done) => {
205 MockXosRuntimeStates.addState.calls.reset();
206 service['addState']({name: 'Tenant', app: 'services.vsg'})
207 .then((model) => {
208 expect(MockXosRuntimeStates.addState).toHaveBeenCalledWith('xos.vsg.tenant', {
209 parent: 'xos.vsg',
210 url: '/tenants/:id?',
211 params: {
212 id: null
213 },
214 data: {
215 model: 'Tenant'
216 },
217 component: 'xosCrud',
218 });
219 expect(model.clientUrl).toBe('vsg/tenants/:id?');
220 done();
221 });
222 scope.$apply();
223 });
224
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700225 it('should add a state with relations in the system', (done) => {
226 MockXosRuntimeStates.addState.calls.reset();
227 service['addState']({name: 'Tenant', app: 'services.vsg', relations: [{model: 'Something', type: 'manytoone'}]})
228 .then((model) => {
229 expect(MockXosRuntimeStates.addState).toHaveBeenCalledWith('xos.vsg.tenant', {
230 parent: 'xos.vsg',
231 url: '/tenants/:id?',
232 params: {
233 id: null
234 },
235 data: {
236 model: 'Tenant',
237 relations: [
238 {model: 'Something', type: 'manytoone'}
239 ]
240 },
241 component: 'xosCrud',
242 });
243 expect(model.clientUrl).toBe('vsg/tenants/:id?');
244 done();
245 });
246 scope.$apply();
247 });
248
Matteo Scandolo02229382017-04-18 11:52:23 -0700249 it('should cache a model', () => {
250 service['cacheModelEntries']({name: 'Tenant', app: 'services.vsg'});
251 expect(MockXosModelStore.query).toHaveBeenCalledWith('Tenant', '/vsg/tenants');
252 });
253
254 it('should get the table config', () => {
255 service['getTableCfg']({name: 'Tenant', app: 'services.vsg'});
256 expect(MockConfigHelpers.modelToTableCfg).toHaveBeenCalledWith(
257 {name: 'Tenant', app: 'services.vsg', tableCfg: true},
258 'xos.vsg.tenant'
259 );
260 });
261
262 it('should get the form config', () => {
263 service['getFormCfg']({name: 'Tenant', app: 'services.vsg'});
264 expect(MockConfigHelpers.modelToFormCfg).toHaveBeenCalledWith(
265 {name: 'Tenant', app: 'services.vsg', formCfg: true}
266 );
267 });
268
Matteo Scandolo02229382017-04-18 11:52:23 -0700269 describe('when discovering models', () => {
270 beforeEach(() => {
271 spyOn(service, 'cacheModelEntries').and.callThrough();
272 spyOn(service, 'addState').and.callThrough();
273 spyOn(service, 'addNavItem').and.callThrough();
274 spyOn(service, 'getTableCfg').and.callThrough();
275 spyOn(service, 'getFormCfg').and.callThrough();
276 spyOn(service, 'storeModel').and.callThrough();
277 });
278
279 it('should call all the function chain', (done) => {
280 service.discover()
281 .then((res) => {
Matteo Scandolobf4e8402019-06-24 12:22:49 -0700282 expect(MockProgressBar.set).toHaveBeenCalled();
Matteo Scandolo29edc0f2018-04-26 17:19:10 +0200283 expect(MockXosModelDefs.get).toHaveBeenCalled();
Matteo Scandolo02229382017-04-18 11:52:23 -0700284 expect(service['cacheModelEntries'].calls.count()).toBe(2);
285 expect(service['addState'].calls.count()).toBe(2);
286 expect(service['addNavItem'].calls.count()).toBe(2);
287 expect(service['getTableCfg'].calls.count()).toBe(2);
288 expect(service['getFormCfg'].calls.count()).toBe(2);
289 expect(service['storeModel'].calls.count()).toBe(2);
290 expect(res).toBeTruthy();
291 done();
292 });
293 scope.$apply();
Matteo Scandolo29edc0f2018-04-26 17:19:10 +0200294 window.setTimeout(() => {
295 // resolve promises after the observable.next as been called twice (cacheModelEntries requires it)
296 scope.$apply();
297 }, 101);
Matteo Scandolo02229382017-04-18 11:52:23 -0700298 });
299 });
300});