blob: 91c7dd03b47b76fd519c24d37e8ce543d71e4ff6 [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
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 Scandolo7629cc42017-03-13 14:12:15 -070019import * as angular from 'angular';
20import 'angular-mocks';
21import 'angular-ui-router';
Matteo Scandolo72181592017-07-25 14:49:40 -070022import {IXosServiceGraphStore, XosServiceGraphStore} from './service-graph.store';
Matteo Scandolo7629cc42017-03-13 14:12:15 -070023import {Subject} from 'rxjs';
24import {XosDebouncer} from '../../core/services/helpers/debounce.helper';
25import {IXosServiceGraph} from '../interfaces';
26import {XosServiceGraphExtender, IXosServiceGraphExtender} from './graph.extender';
27
28let service: IXosServiceGraphStore, extender: IXosServiceGraphExtender;
29
30const subjects = {
31 service: new Subject<any>(),
32 tenant: new Subject<any>(),
33 subscriber: new Subject<any>(),
Matteo Scandolo0e8a8422017-03-25 14:55:40 -070034 tenantroot: new Subject<any>(),
Matteo Scandolo7629cc42017-03-13 14:12:15 -070035 network: new Subject<any>(),
Matteo Scandolod4878532017-03-20 17:39:55 -070036 servicedependency: new Subject<any>()
Matteo Scandolo7629cc42017-03-13 14:12:15 -070037};
38
39// COARSE data
40const coarseServices = [
41 {
42 id: 1,
43 name: 'Service A',
Max Chu549fb3a2017-07-11 14:13:30 -070044 class_names: 'Service, XOSBase'
Matteo Scandolo7629cc42017-03-13 14:12:15 -070045 },
46 {
47 id: 2,
48 name: 'Service B',
Max Chu549fb3a2017-07-11 14:13:30 -070049 class_names: 'Service, XOSBase'
Matteo Scandolo7629cc42017-03-13 14:12:15 -070050 }
51];
52
53const coarseTenants = [
54 {
55 id: 1,
56 provider_service_id: 2,
57 subscriber_service_id: 1,
58 kind: 'coarse',
Max Chu549fb3a2017-07-11 14:13:30 -070059 class_names: 'Tenant, XOSBase'
Matteo Scandolo7629cc42017-03-13 14:12:15 -070060 }
61];
62
63const mockModelStore = {
64 query: (modelName: string) => {
65 return subjects[modelName.toLowerCase()].asObservable();
66 }
67};
68
69describe('The XosServiceGraphStore service', () => {
70
71 beforeEach(() => {
72 angular.module('xosServiceGraphStore', [])
73 .service('XosServiceGraphStore', XosServiceGraphStore)
74 .value('XosModelStore', mockModelStore)
75 .service('XosServiceGraphExtender', XosServiceGraphExtender)
76 .service('XosDebouncer', XosDebouncer);
77
78 angular.mock.module('xosServiceGraphStore');
79 });
80
81 beforeEach(angular.mock.inject((
82 XosServiceGraphStore: IXosServiceGraphStore,
83 XosServiceGraphExtender: IXosServiceGraphExtender
84 ) => {
85 service = XosServiceGraphStore;
86 extender = XosServiceGraphExtender;
87 }));
88
89 describe('when subscribing for the COARSE service graph', () => {
90 beforeEach((done) => {
91 subjects.service.next(coarseServices);
Matteo Scandolod4878532017-03-20 17:39:55 -070092 subjects.servicedependency.next(coarseTenants);
Matteo Scandolo7629cc42017-03-13 14:12:15 -070093 setTimeout(done, 500);
94 });
95
96 it('should return an observer for the Coarse Service Graph', (done) => {
97 service.getCoarse()
98 .subscribe(
99 (res: IXosServiceGraph) => {
100 expect(res.nodes.length).toBe(2);
101 expect(res.nodes[0].d3Class).toBeUndefined();
102 expect(res.links.length).toBe(1);
103 expect(res.links[0].d3Class).toBeUndefined();
104 done();
105 },
106 (err) => {
107 done(err);
108 }
109 );
110 });
111
Matteo Scandolobafd8d62017-03-29 23:23:00 -0700112 xdescribe('when a reducer is registered', () => {
113 // NOTE the reducer appliance has been moved in the component
Matteo Scandolo7629cc42017-03-13 14:12:15 -0700114 beforeEach((done) => {
115 extender.register('coarse', 'test', (graph: IXosServiceGraph) => {
116 graph.nodes = graph.nodes.map(n => {
117 n.d3Class = `testNode`;
118 return n;
119 });
120
121 graph.links = graph.links.map(n => {
122 n.d3Class = `testLink`;
123 return n;
124 });
125
126 return graph;
127 });
128
129 // triggering another next cycle to apply the reducer
130 subjects.service.next(coarseServices);
131 subjects.tenant.next(coarseTenants);
132 setTimeout(done, 500);
133 });
134
135 it('should transform the result', (done) => {
136 service.getCoarse()
137 .subscribe(
138 (res: IXosServiceGraph) => {
139 expect(res.nodes.length).toBe(2);
140 expect(res.nodes[0].d3Class).toEqual('testNode');
141 expect(res.links.length).toBe(1);
142 expect(res.links[0].d3Class).toEqual('testLink');
143 done();
144 },
145 (err) => {
146 done(err);
147 }
148 );
149 });
150 });
151 });
152
153 describe('when subscribing for the Fine-grained service graph', () => {
154 xit('should have a test', () => {
155 expect(true).toBeTruthy();
156 });
157 });
158});