Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -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 | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 19 | 'use strict'; |
| 20 | |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 21 | describe('The Service Relation Service', () => { |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 22 | |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 23 | var Service; |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 24 | |
Matteo Scandolo | 930e4fd | 2016-03-07 16:41:25 -0800 | [diff] [blame] | 25 | beforeEach(module('xos.diagnostic')); |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 26 | beforeEach(module('templates')); |
| 27 | |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 28 | // inject the cartService |
| 29 | beforeEach(inject(function (_ServiceRelation_) { |
| 30 | // The injector unwraps the underscores (_) from around the parameter names when matching |
| 31 | Service = _ServiceRelation_; |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 32 | })); |
| 33 | |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 34 | describe('given a service', () => { |
| 35 | |
| 36 | const levelRelations = [ |
| 37 | { |
| 38 | subscriber_service: 1 |
| 39 | }, |
| 40 | { |
| 41 | subscriber_service: 1 |
| 42 | }, |
| 43 | { |
| 44 | subscriber_service: 2 |
| 45 | } |
| 46 | ]; |
| 47 | |
| 48 | it('should find all involved relations', () => { |
| 49 | expect(typeof Service.findLevelRelation).toBe('function'); |
| 50 | let levelRelation = Service.findLevelRelation(levelRelations, 1); |
| 51 | expect(levelRelation.length).toBe(2); |
| 52 | }); |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 53 | }); |
| 54 | |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 55 | describe('given a set of relation', () => { |
| 56 | |
| 57 | const levelRelations = [ |
| 58 | { |
Matteo Scandolo | c9ebd92 | 2016-01-28 12:02:57 -0800 | [diff] [blame] | 59 | provider_service: 1, |
| 60 | service_specific_attribute: '{"instance_id": "instance1"}', |
| 61 | subscriber_tenant: 2 |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 62 | }, |
| 63 | { |
| 64 | provider_service: 2 |
| 65 | } |
| 66 | ]; |
| 67 | |
| 68 | const services = [ |
| 69 | { |
| 70 | id: 1 |
| 71 | }, |
| 72 | { |
| 73 | id: 2 |
| 74 | }, |
| 75 | { |
| 76 | id: 3 |
| 77 | } |
| 78 | ]; |
| 79 | |
| 80 | it('should find all the provider service', () => { |
| 81 | expect(typeof Service.findLevelServices).toBe('function'); |
| 82 | let levelServices = Service.findLevelServices(levelRelations, services); |
| 83 | expect(levelServices.length).toBe(2); |
| 84 | }); |
Matteo Scandolo | c9ebd92 | 2016-01-28 12:02:57 -0800 | [diff] [blame] | 85 | |
| 86 | it('should retrieve all service specific information', () => { |
| 87 | let info = Service.findSpecificInformation(levelRelations, 1); |
| 88 | expect(info.instance_id).toBe('instance1'); |
| 89 | }); |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 90 | }); |
| 91 | |
Matteo Scandolo | c9ebd92 | 2016-01-28 12:02:57 -0800 | [diff] [blame] | 92 | |
| 93 | |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 94 | describe('given a list of services and a list of relations', () => { |
| 95 | |
| 96 | const services = [ |
| 97 | { |
| 98 | id: 1, |
| 99 | humanReadableName: 'service-1' |
| 100 | }, |
| 101 | { |
| 102 | id: 2, |
| 103 | humanReadableName: 'service-2' |
| 104 | }, |
| 105 | { |
| 106 | id: 3, |
| 107 | humanReadableName: 'service-3' |
| 108 | }, |
| 109 | { |
| 110 | id: 4, |
| 111 | humanReadableName: 'service-4' |
| 112 | } |
| 113 | ]; |
| 114 | |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 115 | const tenants = [ |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 116 | { |
Matteo Scandolo | 50eeec6 | 2016-02-23 10:04:36 -0800 | [diff] [blame] | 117 | id: 1, |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 118 | provider_service: 2, |
Matteo Scandolo | 50eeec6 | 2016-02-23 10:04:36 -0800 | [diff] [blame] | 119 | subscriber_tenant: 4, |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 120 | subscriber_service: 1, |
| 121 | }, |
| 122 | { |
Matteo Scandolo | 50eeec6 | 2016-02-23 10:04:36 -0800 | [diff] [blame] | 123 | id: 2, |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 124 | provider_service: 3, |
Matteo Scandolo | 50eeec6 | 2016-02-23 10:04:36 -0800 | [diff] [blame] | 125 | subscriber_tenant: 1, |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 126 | subscriber_service: 2 |
| 127 | }, |
| 128 | { |
Matteo Scandolo | 50eeec6 | 2016-02-23 10:04:36 -0800 | [diff] [blame] | 129 | id: 3, |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 130 | provider_service: 4, |
Matteo Scandolo | 50eeec6 | 2016-02-23 10:04:36 -0800 | [diff] [blame] | 131 | subscriber_tenant: 4, |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 132 | subscriber_service: 1 |
| 133 | }, |
| 134 | { |
Matteo Scandolo | 50eeec6 | 2016-02-23 10:04:36 -0800 | [diff] [blame] | 135 | id: 4, |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 136 | subscriber_root: 1, |
| 137 | provider_service: 1 |
| 138 | } |
| 139 | ]; |
| 140 | |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 141 | it('should return a tree ordered by tenants', () => { |
| 142 | let tree = Service.buildSubscriberServiceTree(services, tenants); |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 143 | |
| 144 | expect(tree.name).toBe('fakeSubs'); |
| 145 | expect(tree.parent).toBeNull(); |
| 146 | expect(tree.children.length).toBe(1); |
| 147 | |
| 148 | expect(tree.children[0].name).toBe('service-1'); |
| 149 | expect(tree.children[0].parent).toBeNull(); |
Matteo Scandolo | 50eeec6 | 2016-02-23 10:04:36 -0800 | [diff] [blame] | 150 | expect(tree.children[0].tenant).toEqual({id: 4, subscriber_root: 1, provider_service: 1}); |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 151 | expect(tree.children[0].children.length).toBe(2); |
| 152 | |
| 153 | expect(tree.children[0].children[0].name).toBe('service-2'); |
Matteo Scandolo | 50eeec6 | 2016-02-23 10:04:36 -0800 | [diff] [blame] | 154 | expect(tree.children[0].children[0].tenant).toEqual({ id: 1, provider_service: 2, subscriber_tenant: 4, subscriber_service: 1 });; |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 155 | expect(tree.children[0].children[0].children[0].name).toBe('service-3'); |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 156 | |
Matteo Scandolo | 930e4fd | 2016-03-07 16:41:25 -0800 | [diff] [blame] | 157 | // expect(tree.children[0].children[0].children[0].children[0].name).toBe('Router'); |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 158 | |
| 159 | expect(tree.children[0].children[1].name).toBe('service-4'); |
Matteo Scandolo | 930e4fd | 2016-03-07 16:41:25 -0800 | [diff] [blame] | 160 | // expect(tree.children[0].children[1].children[0].name).toBe('Router'); |
Matteo Scandolo | f2c9901 | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 161 | }); |
| 162 | }); |
| 163 | |
| 164 | describe('given an object', () => { |
| 165 | |
| 166 | const sample = { |
| 167 | name: '1', |
| 168 | children: [ |
| 169 | { |
| 170 | name: '2', |
| 171 | children: [ |
| 172 | { |
| 173 | name: '3' |
| 174 | } |
| 175 | ] |
| 176 | } |
| 177 | ] |
| 178 | }; |
| 179 | |
| 180 | it('should return the depth', () => { |
| 181 | expect(Service.depthOf(sample)).toBe(3); |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 182 | }); |
| 183 | }); |
| 184 | |
Matteo Scandolo | 77d8fa0 | 2016-02-16 17:43:00 -0800 | [diff] [blame] | 185 | describe('Given a list of services and COARSE tenant', () => { |
| 186 | |
| 187 | const coarseTenants = [ |
| 188 | { |
| 189 | humanReadableName: 'coarse-1', |
| 190 | provider_service: 1, |
| 191 | subscriber_service: 2 |
| 192 | }, |
| 193 | { |
| 194 | humanReadableName: 'coarse-2', |
| 195 | provider_service: 2, |
| 196 | subscriber_service: 3 |
| 197 | } |
| 198 | ]; |
| 199 | |
| 200 | const services = [ |
| 201 | { |
| 202 | id: 1, |
Matteo Scandolo | 45fba73 | 2016-02-22 14:53:44 -0800 | [diff] [blame] | 203 | name: 'vbng', |
Matteo Scandolo | 77d8fa0 | 2016-02-16 17:43:00 -0800 | [diff] [blame] | 204 | humanReadableName: 'vbng' |
| 205 | }, |
| 206 | { |
| 207 | id: 2, |
Matteo Scandolo | 45fba73 | 2016-02-22 14:53:44 -0800 | [diff] [blame] | 208 | name: 'vsg', |
Matteo Scandolo | 77d8fa0 | 2016-02-16 17:43:00 -0800 | [diff] [blame] | 209 | humanReadableName: 'vsg' |
| 210 | }, |
| 211 | { |
| 212 | id: 3, |
Matteo Scandolo | 45fba73 | 2016-02-22 14:53:44 -0800 | [diff] [blame] | 213 | name: 'volt', |
Matteo Scandolo | 77d8fa0 | 2016-02-16 17:43:00 -0800 | [diff] [blame] | 214 | humanReadableName: 'volt' |
| 215 | } |
| 216 | ]; |
| 217 | |
| 218 | it('should build the tenancy graph', () => { |
| 219 | let tree = Service.buildServiceTree(services, coarseTenants); |
| 220 | |
| 221 | expect(tree.type).toBe('subscriber'); |
Matteo Scandolo | 45fba73 | 2016-02-22 14:53:44 -0800 | [diff] [blame] | 222 | expect(tree.children[0].name).toBe('volt'); |
| 223 | expect(tree.children[0].service).toBeDefined(); |
| 224 | expect(tree.children[0].children[0].name).toBe('vsg'); |
| 225 | expect(tree.children[0].children[0].children[0].name).toBe('vbng'); |
Matteo Scandolo | 77d8fa0 | 2016-02-16 17:43:00 -0800 | [diff] [blame] | 226 | }); |
| 227 | }); |
| 228 | |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 229 | }); |