blob: b081de098e452ac7dd823af6c2460dfdf07152e0 [file] [log] [blame]
Matteo Scandoloc8a58c82017-08-17 17:14:38 -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 {xosDebugModel} from './debug-model';
21
22const MockConfigHelpers = {
23 toLabel: jasmine.createSpy('toLabel')
24};
25
26const model = {
27 policed: 1503009920,
28 backend_register: '{\"next_run\": 0, \"last_success\": 1502860176.52445, \"exponent\": 0}',
29 backend_status: '1 - OK',
30 id: 1,
31 backend_need_delete: true,
32 self_content_type_id: 'core.instance',
33 backend_need_reap: false,
34 no_sync: false,
35 updated: 1503009920,
36 deleted: false,
37 policy_status: '2 - AuthorizationFailure(Authorization Failed: SSL exception connecting to https://192.168.108.119:5000/v2.0/tokens,) // Exception(Ansible playbook failed. // Error in creating the server, please check logs,) // The VM is available but not Active. state:ERROR,)',
38 lazy_blocked: false,
39 enacted: 1503009920,
40 enabled: 1503009920,
41 leaf_model_name: 'Instance',
42 created: 1503009920,
43 write_protect: false,
44 no_policy: false,
45 class_names: 'Instance,XOSBase'
46};
47
48describe('The xosDebugModel component', () => {
49 let scope, rootScope, element, compile , isolatedScope;
50
51 const compileElement = () => {
52
53 if (!scope) {
54 scope = rootScope.$new();
55 }
56
57 element = angular.element(`<xos-debug-model ng-model="model"></xos-debug-model>`);
58 compile(element)(scope);
59 scope.$digest();
60 isolatedScope = element.isolateScope().vm;
61 };
62
63 beforeEach(() => {
64 angular.module('xosDebugModel', [])
65 .component('xosDebugModel', xosDebugModel)
66 .value('ConfigHelpers', MockConfigHelpers);
67 angular.mock.module('xosDebugModel');
68
69 inject(($compile: ng.ICompileService, $rootScope: ng.IScope) => {
70 rootScope = $rootScope;
71 compile = $compile;
72 });
73 });
74
75 it('should have a toLabel method', () => {
76 compileElement();
77 expect(isolatedScope.toLabel).toBeDefined();
78 isolatedScope.toLabel('a');
79 expect(MockConfigHelpers.toLabel).toHaveBeenCalledWith('a');
80 });
81
82 describe('the parseField method', () => {
83 beforeEach(() => {
84 scope = rootScope.$new();
85 scope.model = model;
86 compileElement();
87 });
88
89 it('should convert dates', () => {
90 const dateFields = ['created', 'updated', 'enacted', 'policed'];
91
92 dateFields.forEach(f => {
93 const date = isolatedScope.parseField(f, model[f]);
Matteo Scandoloa1654572017-11-02 12:45:37 +010094 expect(date).toEqual(new Date(model[f] * 1000).toString());
Matteo Scandoloc8a58c82017-08-17 17:14:38 -070095 });
96 });
97
98 it('should convert strings to JSON', () => {
99 const res = isolatedScope.parseField('backend_register', model['backend_register']);
100 expect(res.next_run).toBe(0);
101 expect(res.exponent).toBe(0);
102 });
103
104 it('should parse backend_status and policy_status', () => {
105 const policy = isolatedScope.parseField('policy_status', model['policy_status']);
106 expect(policy.match(/\n/g).length).toBe(3);
107 });
108 });
109});