blob: 9d5660d9879ca56aacdfc10bd5b7e2e94971f3f1 [file] [log] [blame]
Matteo Scandolo63498472017-09-26 17:21:41 -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
17import * as angular from 'angular';
18import 'angular-mocks';
19import 'angular-ui-router';
20import {XosModeldefsCache, IXosModeldefsCache} from './modeldefs.service';
21import {IXosModel} from './model-discoverer.service';
22
23describe('The XosModeldefsCache service', () => {
24 let service;
25
26 beforeEach(() => {
27 angular
28 .module('test', [])
29 .service('XosModeldefsCache', XosModeldefsCache);
30
31 angular.mock.module('test');
32 });
33
34 beforeEach(angular.mock.inject((XosModeldefsCache: IXosModeldefsCache) => {
35 service = XosModeldefsCache;
36 }));
37
38 beforeEach(() => {
39 service['xosModelDefs'] = [];
40 });
41
42 describe('the cache method', () => {
43
44 const modelDef: IXosModel = {
45 fields: [
46 {name: 'id', type: 'number'},
47 {name: 'foo', type: 'string'}
48 ],
49 relations: [],
50 name: 'Node',
51 app: 'core',
52 description: '',
53 verbose_name: ''
54 };
55 it('should add a modelDefs', () => {
56 service.cache(modelDef);
57 expect(service['xosModelDefs'].length).toBe(1);
58 });
59
60 it('should not add a modelDefs twice', () => {
61 service.cache(modelDef);
62 service.cache(modelDef);
63 expect(service['xosModelDefs'].length).toBe(1);
64 });
65 });
66
67 it('should get the service name from the app name', () => {
68 expect(service.serviceNameFromAppName('services.vsg')).toBe('vsg');
69 });
70
71 describe('the get method', () => {
72 it('should retrieve a model definition from local cache', () => {
73 const model = {
74 name: 'Node',
75 app: 'core'
76 };
77 service['xosModelDefs'] = [
78 model
79 ];
80 expect(service.get('Node')).toEqual(model);
81 });
82 });
83});