Added tests for ModelDiscoverer
Change-Id: I8cfd022677b341b28c4765c1ec4e0a4e69b3679a
diff --git a/src/app/core/services/helpers/config.helpers.spec.ts b/src/app/core/services/helpers/config.helpers.spec.ts
index ed41228..d060001 100644
--- a/src/app/core/services/helpers/config.helpers.spec.ts
+++ b/src/app/core/services/helpers/config.helpers.spec.ts
@@ -267,7 +267,7 @@
test: 2
};
it('should add the formatted data to the column definition', () => {
- service = new ConfigHelpers(stateMock, toastr, auth, modelStoreMock);
+ service = new ConfigHelpers(stateMock, toastr, modelStoreMock);
service['populateRelated'](item, item.test, field);
expect(item['test-formatted']).toBe('second');
});
@@ -283,7 +283,7 @@
};
it('should add the available choice to the select', () => {
- service = new ConfigHelpers(stateMock, toastr, auth, modelStoreMock);
+ service = new ConfigHelpers(stateMock, toastr, modelStoreMock);
service['populateSelectField'](field, input);
expect(input.options).toEqual([
{id: 1, label: 'test'},
diff --git a/src/app/core/services/helpers/config.helpers.ts b/src/app/core/services/helpers/config.helpers.ts
index 241d380..1cd9e60 100644
--- a/src/app/core/services/helpers/config.helpers.ts
+++ b/src/app/core/services/helpers/config.helpers.ts
@@ -3,7 +3,6 @@
import {IXosTableColumn, IXosTableCfg} from '../../table/table';
import {IXosModeldef} from '../../../datasources/rest/modeldefs.rest';
import {IXosFormCfg, IXosFormInput, IXosFormInputValidator} from '../../form/form';
-import {IXosAuthService} from '../../../datasources/rest/auth.rest';
import {IXosModelStoreService} from '../../../datasources/stores/model.store';
import {IXosState} from '../runtime-states';
@@ -42,7 +41,6 @@
static $inject = [
'$state',
'toastr',
- 'AuthService',
'XosModelStore'];
public excluded_fields = [
@@ -67,7 +65,6 @@
constructor(
private $state: ng.ui.IStateService,
private toastr: ng.toastr.IToastrService,
- private AuthService: IXosAuthService,
private XosModelStore: IXosModelStoreService
) {
pluralize.addIrregularRule('xos', 'xoses');
@@ -263,9 +260,6 @@
// TODO remove ManyToMany relations and save them separately (how??)
delete item.networks;
- // adding userId as creator
- // item.creator = this.AuthService.getUser().id;
-
// remove field added by xosTable
_.forEach(Object.keys(item), prop => {
if (prop.indexOf('-formatted') > -1) {
diff --git a/src/app/datasources/helpers/model-discoverer.service.ts b/src/app/datasources/helpers/model-discoverer.service.ts
index ba9cb13..4306825 100644
--- a/src/app/datasources/helpers/model-discoverer.service.ts
+++ b/src/app/datasources/helpers/model-discoverer.service.ts
@@ -106,7 +106,7 @@
return this.$q.resolve('true');
})
.catch(err => {
- this.$log.error(`[XosModelDiscovererService] Model ${model.name} NOT stored`);
+ this.$log.error(`[XosModelDiscovererService] Model ${model.name} NOT stored`, err);
return this.$q.resolve('false');
});
pArray.push(p);
@@ -116,11 +116,12 @@
// the Model Loader promise won't ever be reject, in case it will be resolve with value false,
// that's because we want to wait anyway for all the models to be loaded
if (res.indexOf('false') > -1) {
- d.resolve(false);
+ return d.resolve(false);
}
d.resolve(true);
})
- .catch(() => {
+ .catch((e) => {
+ this.$log.error(`[XosModelDiscovererService]`, e);
d.resolve(false);
})
.finally(() => {
diff --git a/src/app/datasources/helpers/model.discoverer.service.spec.ts b/src/app/datasources/helpers/model.discoverer.service.spec.ts
new file mode 100644
index 0000000..8b17f34
--- /dev/null
+++ b/src/app/datasources/helpers/model.discoverer.service.spec.ts
@@ -0,0 +1,254 @@
+import * as angular from 'angular';
+import 'angular-mocks';
+import 'angular-ui-router';
+import {XosModelDiscovererService, IXosModelDiscovererService} from './model-discoverer.service';
+import {IXosModeldef} from '../rest/modeldefs.rest';
+import {BehaviorSubject} from 'rxjs';
+
+const stubModels: IXosModeldef[] = [
+ {
+ fields: [
+ {name: 'id', type: 'number'},
+ {name: 'foo', type: 'string'}
+ ],
+ relations: [],
+ name: 'Node',
+ app: 'core'
+ },
+ {
+ fields: [
+ {name: 'id', type: 'number'},
+ {name: 'bar', type: 'string'}
+ ],
+ relations: [],
+ name: 'VSGTenant',
+ app: 'service.vsg'
+ }
+];
+
+let service: IXosModelDiscovererService;
+let scope: ng.IScope;
+const MockXosModelDefs = {
+ get: null
+};
+let MockXosRuntimeStates = {
+ addState: jasmine.createSpy('runtimeState.addState')
+ .and.callFake(() => true)
+};
+let MockConfigHelpers = {
+ pluralize: jasmine.createSpy('config.pluralize')
+ .and.callFake((string: string) => `${string}s`),
+ toLabel: jasmine.createSpy('config.toLabel')
+ .and.callFake((string: string) => string.toLowerCase()),
+ modelToTableCfg: jasmine.createSpy('config.modelToTableCfg')
+ .and.callFake(() => true),
+ modelToFormCfg: jasmine.createSpy('config.modelToFormCfg')
+ .and.callFake(() => true),
+};
+let MockXosNavigationService = {
+ add: jasmine.createSpy('navigationService.add')
+ .and.callFake(() => true)
+};
+const MockXosModelStore = {
+ query: jasmine.createSpy('modelStore.query')
+ .and.callFake(() => {
+ const list = new BehaviorSubject([]);
+ list.next([]);
+ return list.asObservable();
+ })
+};
+const MockProgressBar = {
+ setColor: jasmine.createSpy('progressBar.setColor'),
+ start: jasmine.createSpy('progressBar.start'),
+ complete: jasmine.createSpy('progressBar.complete')
+};
+const MockngProgressFactory = {
+ createInstance: jasmine.createSpy('ngProgress.createInstance')
+ .and.callFake(() => MockProgressBar)
+};
+
+describe('The ModelDicoverer service', () => {
+
+ beforeEach(() => {
+ angular
+ .module('test', [])
+ .service('XosModelDiscoverer', XosModelDiscovererService)
+ .value('ConfigHelpers', MockConfigHelpers)
+ .value('XosModelDefs', MockXosModelDefs)
+ .value('XosRuntimeStates', MockXosRuntimeStates)
+ .value('XosModelStore', MockXosModelStore)
+ .value('ngProgressFactory', MockngProgressFactory)
+ .value('XosNavigationService', MockXosNavigationService);
+
+ angular.mock.module('test');
+ });
+
+ beforeEach(angular.mock.inject((
+ XosModelDiscoverer: IXosModelDiscovererService,
+ $rootScope: ng.IScope,
+ $q: ng.IQService
+ ) => {
+ service = XosModelDiscoverer;
+ scope = $rootScope;
+ MockXosModelDefs.get = jasmine.createSpy('modelDefs.get')
+ .and.callFake(() => {
+ const d = $q.defer();
+ d.resolve(stubModels);
+ return d.promise;
+ });
+ }));
+
+ it('should setup the progress bar', () => {
+ expect(MockngProgressFactory.createInstance).toHaveBeenCalled();
+ expect(MockProgressBar.setColor).toHaveBeenCalled();
+ });
+
+ it('should not have loaded models', () => {
+ expect(service.areModelsLoaded()).toBeFalsy();
+ });
+
+ it('should get the url from a core model', () => {
+ const model = {
+ name: 'Node',
+ app: 'core',
+ fields: []
+ };
+ expect(service.getApiUrlFromModel(model)).toBe('/core/nodes');
+ });
+
+ it('should get the url from a service model', () => {
+ const model = {
+ name: 'Tenant',
+ app: 'services.test',
+ fields: []
+ };
+ expect(service.getApiUrlFromModel(model)).toBe('/test/tenants');
+ });
+
+ it('should retrieve a model definition from local cache', () => {
+ const model = {
+ name: 'Node',
+ app: 'core'
+ };
+ service['xosModels'] = [
+ model
+ ];
+ expect(service.get('Node')).toEqual(model);
+ });
+
+ it('should get the service name from the app name', () => {
+ expect(service['serviceNameFromAppName']('services.vsg')).toBe('vsg');
+ });
+
+ it('should get the state name from the model', () => {
+ expect(service['stateNameFromModel']({name: 'Tenant', app: 'services.vsg'})).toBe('xos.vsg.tenant');
+ });
+
+ it('should get the parent state name from a core model', () => {
+ expect(service['getParentStateFromModel']({name: 'Nodes', app: 'core'})).toBe('xos.core');
+ });
+
+ it('should get the parent state name from a service model', () => {
+ expect(service['getParentStateFromModel']({name: 'Tenant', app: 'services.vsg'})).toBe('xos.vsg');
+ });
+
+ it('should add a new service entry in the system', () => {
+ service['addService']({name: 'Tenant', app: 'services.vsg'});
+ expect(MockXosRuntimeStates.addState).toHaveBeenCalledWith('xos.vsg', {
+ url: 'vsg',
+ parent: 'xos',
+ abstract: true,
+ template: '<div ui-view></div>'
+ });
+ expect(MockXosNavigationService.add).toHaveBeenCalledWith({
+ label: 'vsg',
+ state: 'xos.vsg'
+ });
+ expect(service['xosServices'][0]).toEqual('vsg');
+ expect(service['xosServices'].length).toBe(1);
+ });
+
+ it('should add a state in the system', (done) => {
+ MockXosRuntimeStates.addState.calls.reset();
+ service['addState']({name: 'Tenant', app: 'services.vsg'})
+ .then((model) => {
+ expect(MockXosRuntimeStates.addState).toHaveBeenCalledWith('xos.vsg.tenant', {
+ parent: 'xos.vsg',
+ url: '/tenants/:id?',
+ params: {
+ id: null
+ },
+ data: {
+ model: 'Tenant'
+ },
+ component: 'xosCrud',
+ });
+ expect(model.clientUrl).toBe('vsg/tenants/:id?');
+ done();
+ });
+ scope.$apply();
+ });
+
+ it('should add an item to navigation', () => {
+ service['addNavItem']({name: 'Tenant', app: 'services.vsg'});
+ expect(MockXosNavigationService.add).toHaveBeenCalledWith({
+ label: 'Tenants',
+ state: 'xos.vsg.tenant',
+ parent: 'xos.vsg'
+ });
+ });
+
+ it('should cache a model', () => {
+ service['cacheModelEntries']({name: 'Tenant', app: 'services.vsg'});
+ expect(MockXosModelStore.query).toHaveBeenCalledWith('Tenant', '/vsg/tenants');
+ });
+
+ it('should get the table config', () => {
+ service['getTableCfg']({name: 'Tenant', app: 'services.vsg'});
+ expect(MockConfigHelpers.modelToTableCfg).toHaveBeenCalledWith(
+ {name: 'Tenant', app: 'services.vsg', tableCfg: true},
+ 'xos.vsg.tenant'
+ );
+ });
+
+ it('should get the form config', () => {
+ service['getFormCfg']({name: 'Tenant', app: 'services.vsg'});
+ expect(MockConfigHelpers.modelToFormCfg).toHaveBeenCalledWith(
+ {name: 'Tenant', app: 'services.vsg', formCfg: true}
+ );
+ });
+
+ it('should store the model in memory', () => {
+ service['storeModel']({name: 'Tenant'});
+ expect(service['xosModels'][0]).toEqual({name: 'Tenant'});
+ expect(service['xosModels'].length).toEqual(1);
+ });
+
+ describe('when discovering models', () => {
+ beforeEach(() => {
+ spyOn(service, 'cacheModelEntries').and.callThrough();
+ spyOn(service, 'addState').and.callThrough();
+ spyOn(service, 'addNavItem').and.callThrough();
+ spyOn(service, 'getTableCfg').and.callThrough();
+ spyOn(service, 'getFormCfg').and.callThrough();
+ spyOn(service, 'storeModel').and.callThrough();
+ });
+
+ it('should call all the function chain', (done) => {
+ service.discover()
+ .then((res) => {
+ expect(MockProgressBar.start).toHaveBeenCalled();
+ expect(MockXosModelDefs.get).toHaveBeenCalled();
+ expect(service['cacheModelEntries'].calls.count()).toBe(2);
+ expect(service['addState'].calls.count()).toBe(2);
+ expect(service['addNavItem'].calls.count()).toBe(2);
+ expect(service['getTableCfg'].calls.count()).toBe(2);
+ expect(service['getFormCfg'].calls.count()).toBe(2);
+ expect(service['storeModel'].calls.count()).toBe(2);
+ expect(res).toBeTruthy();
+ done();
+ });
+ scope.$apply();
+ });
+ });
+});
diff --git a/src/interceptors.ts b/src/interceptors.ts
index 7770f5d..8a11d08 100644
--- a/src/interceptors.ts
+++ b/src/interceptors.ts
@@ -47,7 +47,7 @@
req.headers['Content-Type'] = 'application/json';
if (req.method === 'PUT') {
- // FIXME XosModelStore.search add this value for visualization purpose,
+ // XosModelStore.search add this value for visualization purpose,
// no one should change models
delete req.data.modelName;
}