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 | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 19 | import * as angular from 'angular'; |
| 20 | import 'angular-mocks'; |
| 21 | import 'angular-ui-router'; |
Matteo Scandolo | 7218159 | 2017-07-25 14:49:40 -0700 | [diff] [blame] | 22 | import {IXosServiceGraphStore, XosServiceGraphStore} from './service-graph.store'; |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 23 | import {Subject} from 'rxjs'; |
| 24 | import {XosDebouncer} from '../../core/services/helpers/debounce.helper'; |
| 25 | import {IXosServiceGraph} from '../interfaces'; |
| 26 | import {XosServiceGraphExtender, IXosServiceGraphExtender} from './graph.extender'; |
| 27 | |
| 28 | let service: IXosServiceGraphStore, extender: IXosServiceGraphExtender; |
| 29 | |
| 30 | const subjects = { |
| 31 | service: new Subject<any>(), |
| 32 | tenant: new Subject<any>(), |
| 33 | subscriber: new Subject<any>(), |
Matteo Scandolo | 0e8a842 | 2017-03-25 14:55:40 -0700 | [diff] [blame] | 34 | tenantroot: new Subject<any>(), |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 35 | network: new Subject<any>(), |
Matteo Scandolo | d487853 | 2017-03-20 17:39:55 -0700 | [diff] [blame] | 36 | servicedependency: new Subject<any>() |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | // COARSE data |
| 40 | const coarseServices = [ |
| 41 | { |
| 42 | id: 1, |
| 43 | name: 'Service A', |
Max Chu | 549fb3a | 2017-07-11 14:13:30 -0700 | [diff] [blame] | 44 | class_names: 'Service, XOSBase' |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 45 | }, |
| 46 | { |
| 47 | id: 2, |
| 48 | name: 'Service B', |
Max Chu | 549fb3a | 2017-07-11 14:13:30 -0700 | [diff] [blame] | 49 | class_names: 'Service, XOSBase' |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 50 | } |
| 51 | ]; |
| 52 | |
| 53 | const coarseTenants = [ |
| 54 | { |
| 55 | id: 1, |
| 56 | provider_service_id: 2, |
| 57 | subscriber_service_id: 1, |
| 58 | kind: 'coarse', |
Max Chu | 549fb3a | 2017-07-11 14:13:30 -0700 | [diff] [blame] | 59 | class_names: 'Tenant, XOSBase' |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 60 | } |
| 61 | ]; |
| 62 | |
| 63 | const mockModelStore = { |
| 64 | query: (modelName: string) => { |
| 65 | return subjects[modelName.toLowerCase()].asObservable(); |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | describe('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 Scandolo | d487853 | 2017-03-20 17:39:55 -0700 | [diff] [blame] | 92 | subjects.servicedependency.next(coarseTenants); |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 93 | 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 Scandolo | bafd8d6 | 2017-03-29 23:23:00 -0700 | [diff] [blame] | 112 | xdescribe('when a reducer is registered', () => { |
| 113 | // NOTE the reducer appliance has been moved in the component |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 114 | 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 | }); |