Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 19 | import * as angular from 'angular'; |
| 20 | import 'angular-mocks'; |
| 21 | import 'angular-ui-router'; |
| 22 | import {XosModelDiscovererService, IXosModelDiscovererService} from './model-discoverer.service'; |
| 23 | import {IXosModeldef} from '../rest/modeldefs.rest'; |
| 24 | import {BehaviorSubject} from 'rxjs'; |
| 25 | |
| 26 | const stubModels: IXosModeldef[] = [ |
| 27 | { |
| 28 | fields: [ |
| 29 | {name: 'id', type: 'number'}, |
| 30 | {name: 'foo', type: 'string'} |
| 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: [ |
| 40 | {name: 'id', type: 'number'}, |
| 41 | {name: 'bar', type: 'string'} |
| 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') |
| 76 | .and.callFake(() => { |
| 77 | const list = new BehaviorSubject([]); |
| 78 | list.next([]); |
| 79 | return list.asObservable(); |
| 80 | }) |
| 81 | }; |
| 82 | const MockProgressBar = { |
| 83 | setColor: jasmine.createSpy('progressBar.setColor'), |
| 84 | start: jasmine.createSpy('progressBar.start'), |
| 85 | complete: jasmine.createSpy('progressBar.complete') |
| 86 | }; |
| 87 | const MockngProgressFactory = { |
| 88 | createInstance: jasmine.createSpy('ngProgress.createInstance') |
| 89 | .and.callFake(() => MockProgressBar) |
| 90 | }; |
| 91 | |
| 92 | describe('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 Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 103 | .value('XosNavigationService', MockXosNavigationService) |
| 104 | .value('AuthService', {}); |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 105 | |
| 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 Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 137 | fields: [], |
| 138 | description: '', |
| 139 | verbose_name: '' |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 140 | }; |
| 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 Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 148 | fields: [], |
| 149 | description: '', |
| 150 | verbose_name: '' |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 151 | }; |
| 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 Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 219 | 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 Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 243 | 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 Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 250 | |
| 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 Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 257 | }); |
| 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 | }); |