Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | /* |
| 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 Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 18 | import * as angular from 'angular'; |
| 19 | import 'angular-mocks'; |
| 20 | import 'angular-ui-router'; |
| 21 | import {XosModelDiscovererService, IXosModelDiscovererService} from './model-discoverer.service'; |
| 22 | import {IXosModeldef} from '../rest/modeldefs.rest'; |
| 23 | import {BehaviorSubject} from 'rxjs'; |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 24 | import {XosModeldefsCache} from './modeldefs.service'; |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 25 | |
| 26 | const stubModels: IXosModeldef[] = [ |
| 27 | { |
| 28 | fields: [ |
Matteo Scandolo | d67adee | 2018-03-08 16:27:05 -0800 | [diff] [blame] | 29 | {name: 'id', type: 'number', read_only: false}, |
| 30 | {name: 'foo', type: 'string', read_only: false} |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 31 | ], |
| 32 | relations: [], |
| 33 | name: 'Node', |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 34 | app: 'core', |
| 35 | description: '', |
| 36 | verbose_name: '' |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 37 | }, |
| 38 | { |
| 39 | fields: [ |
Matteo Scandolo | d67adee | 2018-03-08 16:27:05 -0800 | [diff] [blame] | 40 | {name: 'id', type: 'number', read_only: false}, |
| 41 | {name: 'bar', type: 'string', read_only: false} |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 42 | ], |
| 43 | relations: [], |
| 44 | name: 'VSGTenant', |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 45 | app: 'service.vsg', |
| 46 | description: '', |
| 47 | verbose_name: '' |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 48 | } |
| 49 | ]; |
| 50 | |
| 51 | let service: IXosModelDiscovererService; |
| 52 | let scope: ng.IScope; |
| 53 | const MockXosModelDefs = { |
| 54 | get: null |
| 55 | }; |
| 56 | let MockXosRuntimeStates = { |
| 57 | addState: jasmine.createSpy('runtimeState.addState') |
| 58 | .and.callFake(() => true) |
| 59 | }; |
| 60 | let 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 | }; |
| 70 | let MockXosNavigationService = { |
| 71 | add: jasmine.createSpy('navigationService.add') |
| 72 | .and.callFake(() => true) |
| 73 | }; |
| 74 | const MockXosModelStore = { |
| 75 | query: jasmine.createSpy('modelStore.query') |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 76 | .and.callFake((model) => { |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 77 | const list = new BehaviorSubject([]); |
| 78 | list.next([]); |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 79 | 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 Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 83 | return list.asObservable(); |
| 84 | }) |
| 85 | }; |
| 86 | const MockProgressBar = { |
| 87 | setColor: jasmine.createSpy('progressBar.setColor'), |
Matteo Scandolo | bf4e840 | 2019-06-24 12:22:49 -0700 | [diff] [blame] | 88 | set: jasmine.createSpy('progressBar.set'), |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 89 | complete: jasmine.createSpy('progressBar.complete') |
| 90 | }; |
| 91 | const MockngProgressFactory = { |
| 92 | createInstance: jasmine.createSpy('ngProgress.createInstance') |
| 93 | .and.callFake(() => MockProgressBar) |
| 94 | }; |
| 95 | |
| 96 | describe('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 Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 107 | .value('XosNavigationService', MockXosNavigationService) |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 108 | .value('AuthService', {}) |
| 109 | .service('XosModeldefsCache', XosModeldefsCache); |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 110 | |
| 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 Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 142 | fields: [], |
| 143 | description: '', |
| 144 | verbose_name: '' |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 145 | }; |
| 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 Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 153 | fields: [], |
| 154 | description: '', |
| 155 | verbose_name: '' |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 156 | }; |
| 157 | expect(service.getApiUrlFromModel(model)).toBe('/test/tenants'); |
| 158 | }); |
| 159 | |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 160 | 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 Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 172 | 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 Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 188 | 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 Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 225 | 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 Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 249 | 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 Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 269 | 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 Scandolo | bf4e840 | 2019-06-24 12:22:49 -0700 | [diff] [blame] | 282 | expect(MockProgressBar.set).toHaveBeenCalled(); |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 283 | expect(MockXosModelDefs.get).toHaveBeenCalled(); |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 284 | 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 Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 294 | window.setTimeout(() => { |
| 295 | // resolve promises after the observable.next as been called twice (cacheModelEntries requires it) |
| 296 | scope.$apply(); |
| 297 | }, 101); |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 298 | }); |
| 299 | }); |
| 300 | }); |