blob: a6e6340fe32e43a11500ea773227fbc4c26a0e84 [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')
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)
Matteo Scandolo63498472017-09-26 17:21:41 -0700104 .value('AuthService', {})
105 .service('XosModeldefsCache', XosModeldefsCache);
Matteo Scandolo02229382017-04-18 11:52:23 -0700106
107 angular.mock.module('test');
108 });
109
110 beforeEach(angular.mock.inject((
111 XosModelDiscoverer: IXosModelDiscovererService,
112 $rootScope: ng.IScope,
113 $q: ng.IQService
114 ) => {
115 service = XosModelDiscoverer;
116 scope = $rootScope;
117 MockXosModelDefs.get = jasmine.createSpy('modelDefs.get')
118 .and.callFake(() => {
119 const d = $q.defer();
120 d.resolve(stubModels);
121 return d.promise;
122 });
123 }));
124
125 it('should setup the progress bar', () => {
126 expect(MockngProgressFactory.createInstance).toHaveBeenCalled();
127 expect(MockProgressBar.setColor).toHaveBeenCalled();
128 });
129
130 it('should not have loaded models', () => {
131 expect(service.areModelsLoaded()).toBeFalsy();
132 });
133
134 it('should get the url from a core model', () => {
135 const model = {
136 name: 'Node',
137 app: 'core',
Matteo Scandolo580033a2017-08-17 11:16:39 -0700138 fields: [],
139 description: '',
140 verbose_name: ''
Matteo Scandolo02229382017-04-18 11:52:23 -0700141 };
142 expect(service.getApiUrlFromModel(model)).toBe('/core/nodes');
143 });
144
145 it('should get the url from a service model', () => {
146 const model = {
147 name: 'Tenant',
148 app: 'services.test',
Matteo Scandolo580033a2017-08-17 11:16:39 -0700149 fields: [],
150 description: '',
151 verbose_name: ''
Matteo Scandolo02229382017-04-18 11:52:23 -0700152 };
153 expect(service.getApiUrlFromModel(model)).toBe('/test/tenants');
154 });
155
Matteo Scandolo02229382017-04-18 11:52:23 -0700156 it('should get the state name from the model', () => {
157 expect(service['stateNameFromModel']({name: 'Tenant', app: 'services.vsg'})).toBe('xos.vsg.tenant');
158 });
159
160 it('should get the parent state name from a core model', () => {
161 expect(service['getParentStateFromModel']({name: 'Nodes', app: 'core'})).toBe('xos.core');
162 });
163
164 it('should get the parent state name from a service model', () => {
165 expect(service['getParentStateFromModel']({name: 'Tenant', app: 'services.vsg'})).toBe('xos.vsg');
166 });
167
168 it('should add a new service entry in the system', () => {
169 service['addService']({name: 'Tenant', app: 'services.vsg'});
170 expect(MockXosRuntimeStates.addState).toHaveBeenCalledWith('xos.vsg', {
171 url: 'vsg',
172 parent: 'xos',
173 abstract: true,
174 template: '<div ui-view></div>'
175 });
176 expect(MockXosNavigationService.add).toHaveBeenCalledWith({
177 label: 'vsg',
178 state: 'xos.vsg'
179 });
180 expect(service['xosServices'][0]).toEqual('vsg');
181 expect(service['xosServices'].length).toBe(1);
182 });
183
184 it('should add a state in the system', (done) => {
185 MockXosRuntimeStates.addState.calls.reset();
186 service['addState']({name: 'Tenant', app: 'services.vsg'})
187 .then((model) => {
188 expect(MockXosRuntimeStates.addState).toHaveBeenCalledWith('xos.vsg.tenant', {
189 parent: 'xos.vsg',
190 url: '/tenants/:id?',
191 params: {
192 id: null
193 },
194 data: {
195 model: 'Tenant'
196 },
197 component: 'xosCrud',
198 });
199 expect(model.clientUrl).toBe('vsg/tenants/:id?');
200 done();
201 });
202 scope.$apply();
203 });
204
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700205 it('should add a state with relations in the system', (done) => {
206 MockXosRuntimeStates.addState.calls.reset();
207 service['addState']({name: 'Tenant', app: 'services.vsg', relations: [{model: 'Something', type: 'manytoone'}]})
208 .then((model) => {
209 expect(MockXosRuntimeStates.addState).toHaveBeenCalledWith('xos.vsg.tenant', {
210 parent: 'xos.vsg',
211 url: '/tenants/:id?',
212 params: {
213 id: null
214 },
215 data: {
216 model: 'Tenant',
217 relations: [
218 {model: 'Something', type: 'manytoone'}
219 ]
220 },
221 component: 'xosCrud',
222 });
223 expect(model.clientUrl).toBe('vsg/tenants/:id?');
224 done();
225 });
226 scope.$apply();
227 });
228
Matteo Scandolo02229382017-04-18 11:52:23 -0700229 it('should add an item to navigation', () => {
230 service['addNavItem']({name: 'Tenant', app: 'services.vsg'});
231 expect(MockXosNavigationService.add).toHaveBeenCalledWith({
232 label: 'Tenants',
233 state: 'xos.vsg.tenant',
234 parent: 'xos.vsg'
235 });
Matteo Scandolo580033a2017-08-17 11:16:39 -0700236
237 service['addNavItem']({name: 'Tenant', verbose_name: 'Verbose', app: 'services.vsg'});
238 expect(MockXosNavigationService.add).toHaveBeenCalledWith({
239 label: 'Verboses',
240 state: 'xos.vsg.tenant',
241 parent: 'xos.vsg'
242 });
Matteo Scandolo02229382017-04-18 11:52:23 -0700243 });
244
245 it('should cache a model', () => {
246 service['cacheModelEntries']({name: 'Tenant', app: 'services.vsg'});
247 expect(MockXosModelStore.query).toHaveBeenCalledWith('Tenant', '/vsg/tenants');
248 });
249
250 it('should get the table config', () => {
251 service['getTableCfg']({name: 'Tenant', app: 'services.vsg'});
252 expect(MockConfigHelpers.modelToTableCfg).toHaveBeenCalledWith(
253 {name: 'Tenant', app: 'services.vsg', tableCfg: true},
254 'xos.vsg.tenant'
255 );
256 });
257
258 it('should get the form config', () => {
259 service['getFormCfg']({name: 'Tenant', app: 'services.vsg'});
260 expect(MockConfigHelpers.modelToFormCfg).toHaveBeenCalledWith(
261 {name: 'Tenant', app: 'services.vsg', formCfg: true}
262 );
263 });
264
Matteo Scandolo02229382017-04-18 11:52:23 -0700265 describe('when discovering models', () => {
266 beforeEach(() => {
267 spyOn(service, 'cacheModelEntries').and.callThrough();
268 spyOn(service, 'addState').and.callThrough();
269 spyOn(service, 'addNavItem').and.callThrough();
270 spyOn(service, 'getTableCfg').and.callThrough();
271 spyOn(service, 'getFormCfg').and.callThrough();
272 spyOn(service, 'storeModel').and.callThrough();
273 });
274
275 it('should call all the function chain', (done) => {
276 service.discover()
277 .then((res) => {
278 expect(MockProgressBar.start).toHaveBeenCalled();
Matteo Scandolo63498472017-09-26 17:21:41 -0700279 expect(MockXosModelDefs.get).toHaveBeenCalled(); // FIXME replace correct spy
Matteo Scandolo02229382017-04-18 11:52:23 -0700280 expect(service['cacheModelEntries'].calls.count()).toBe(2);
281 expect(service['addState'].calls.count()).toBe(2);
282 expect(service['addNavItem'].calls.count()).toBe(2);
283 expect(service['getTableCfg'].calls.count()).toBe(2);
284 expect(service['getFormCfg'].calls.count()).toBe(2);
285 expect(service['storeModel'].calls.count()).toBe(2);
286 expect(res).toBeTruthy();
287 done();
288 });
289 scope.$apply();
290 });
291 });
292});