Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | */ |
Sean Condon | c13d956 | 2019-04-18 13:24:42 +0100 | [diff] [blame] | 16 | import {async, ComponentFixture, TestBed} from '@angular/core/testing'; |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 17 | |
Sean Condon | c13d956 | 2019-04-18 13:24:42 +0100 | [diff] [blame] | 18 | import {ForceSvgComponent} from './forcesvg.component'; |
Sean Condon | 64060ff | 2019-05-30 15:48:11 +0100 | [diff] [blame] | 19 | import { |
| 20 | FnService, IconService, |
| 21 | LionService, |
| 22 | LogService, SvgUtilService, |
| 23 | UrlFnService, |
| 24 | WebSocketService |
Sean Condon | a3ad779 | 2020-01-04 19:26:34 +0000 | [diff] [blame] | 25 | } from 'gui2-fw-lib/public_api'; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 26 | import {DraggableDirective} from './draggable/draggable.directive'; |
| 27 | import {ActivatedRoute, Params} from '@angular/router'; |
| 28 | import {of} from 'rxjs'; |
Sean Condon | ff85fbe | 2019-03-16 14:28:46 +0000 | [diff] [blame] | 29 | import {DeviceNodeSvgComponent} from './visuals/devicenodesvg/devicenodesvg.component'; |
| 30 | import {SubRegionNodeSvgComponent} from './visuals/subregionnodesvg/subregionnodesvg.component'; |
| 31 | import {HostNodeSvgComponent} from './visuals/hostnodesvg/hostnodesvg.component'; |
| 32 | import {LinkSvgComponent} from './visuals/linksvg/linksvg.component'; |
Sean Condon | d92b0bd | 2020-03-23 08:40:55 +0000 | [diff] [blame] | 33 | import {Device, Host, Link, LinkType, LinkHighlight, Region} from './models'; |
Sean Condon | 64060ff | 2019-05-30 15:48:11 +0100 | [diff] [blame] | 34 | import {ChangeDetectorRef, SimpleChange} from '@angular/core'; |
| 35 | import {TopologyService} from '../../topology.service'; |
Sean Condon | 590b34b | 2019-12-04 18:44:37 +0000 | [diff] [blame] | 36 | import {BadgeSvgComponent} from './visuals/badgesvg/badgesvg.component'; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 37 | |
| 38 | class MockActivatedRoute extends ActivatedRoute { |
| 39 | constructor(params: Params) { |
| 40 | super(); |
| 41 | this.queryParams = of(params); |
| 42 | } |
| 43 | } |
| 44 | |
Sean Condon | 64060ff | 2019-05-30 15:48:11 +0100 | [diff] [blame] | 45 | class MockIconService { |
| 46 | loadIconDef() { } |
| 47 | } |
| 48 | |
| 49 | class MockSvgUtilService { |
| 50 | |
| 51 | cat7() { |
| 52 | const tcid = 'd3utilTestCard'; |
| 53 | |
| 54 | function getColor(id, muted, theme) { |
| 55 | // NOTE: since we are lazily assigning domain ids, we need to |
| 56 | // get the color from all 4 scales, to keep the domains |
| 57 | // in sync. |
| 58 | const ln = '#5b99d2'; |
| 59 | const lm = '#9ebedf'; |
| 60 | const dn = '#5b99d2'; |
| 61 | const dm = '#9ebedf'; |
| 62 | if (theme === 'dark') { |
| 63 | return muted ? dm : dn; |
| 64 | } else { |
| 65 | return muted ? lm : ln; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return { |
| 70 | // testCard: testCard, |
| 71 | getColor: getColor, |
| 72 | }; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | class MockUrlFnService { } |
| 77 | |
| 78 | class MockWebSocketService { |
| 79 | createWebSocket() { } |
| 80 | isConnected() { return false; } |
| 81 | unbindHandlers() { } |
| 82 | bindHandlers() { } |
| 83 | } |
| 84 | |
| 85 | class MockTopologyService { |
| 86 | public instancesIndex: Map<string, number>; |
| 87 | constructor() { |
| 88 | this.instancesIndex = new Map(); |
| 89 | } |
| 90 | } |
| 91 | |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 92 | describe('ForceSvgComponent', () => { |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 93 | let fs: FnService; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 94 | let ar: MockActivatedRoute; |
| 95 | let windowMock: Window; |
| 96 | let logServiceSpy: jasmine.SpyObj<LogService>; |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 97 | let component: ForceSvgComponent; |
| 98 | let fixture: ComponentFixture<ForceSvgComponent>; |
Sean Condon | b77768e | 2019-05-04 20:23:42 +0100 | [diff] [blame] | 99 | const openflowSampleData = require('./tests/test-module-topo2CurrentRegion.json'); |
| 100 | const openflowRegionData: Region = <Region><unknown>(openflowSampleData.payload); |
| 101 | |
| 102 | const odtnSampleData = require('./tests/test-OdtnConfig-topo2CurrentRegion.json'); |
| 103 | const odtnRegionData: Region = <Region><unknown>(odtnSampleData.payload); |
| 104 | |
Sean Condon | d92b0bd | 2020-03-23 08:40:55 +0000 | [diff] [blame] | 105 | const topo2BaseData = require('./tests/topo2Highlights-base-data.json'); |
| 106 | const topo2BaseRegionData: Region = <Region><unknown>(topo2BaseData.payload); |
| 107 | |
| 108 | const highlightSampleData = require('./tests/topo2Highlights-sample.json'); |
| 109 | const linkHightlights: LinkHighlight[] = <LinkHighlight[]><unknown>(highlightSampleData.payload.links); |
| 110 | |
Sean Condon | c13d956 | 2019-04-18 13:24:42 +0100 | [diff] [blame] | 111 | const emptyRegion: Region = <Region>{devices: [ [], [], [] ], hosts: [ [], [], [] ], links: []}; |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 112 | |
Sean Condon | 64060ff | 2019-05-30 15:48:11 +0100 | [diff] [blame] | 113 | beforeEach(() => { |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 114 | const logSpy = jasmine.createSpyObj('LogService', ['info', 'debug', 'warn', 'error']); |
| 115 | ar = new MockActivatedRoute({ 'debug': 'txrx' }); |
| 116 | |
| 117 | windowMock = <any>{ |
| 118 | location: <any>{ |
| 119 | hostname: 'foo', |
| 120 | host: 'foo', |
| 121 | port: '80', |
| 122 | protocol: 'http', |
| 123 | search: { debug: 'true' }, |
| 124 | href: 'ws://foo:123/onos/ui/websock/path', |
| 125 | absUrl: 'ws://foo:123/onos/ui/websock/path' |
| 126 | } |
| 127 | }; |
| 128 | |
Sean Condon | 64060ff | 2019-05-30 15:48:11 +0100 | [diff] [blame] | 129 | const bundleObj = { |
| 130 | 'core.view.Topo': { |
| 131 | test: 'test1' |
| 132 | } |
| 133 | }; |
| 134 | const mockLion = (key) => { |
| 135 | return bundleObj[key] || '%' + key + '%'; |
| 136 | }; |
| 137 | |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 138 | fs = new FnService(ar, logSpy, windowMock); |
| 139 | |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 140 | TestBed.configureTestingModule({ |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 141 | declarations: [ |
| 142 | ForceSvgComponent, |
| 143 | DeviceNodeSvgComponent, |
| 144 | HostNodeSvgComponent, |
| 145 | SubRegionNodeSvgComponent, |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 146 | LinkSvgComponent, |
Sean Condon | 590b34b | 2019-12-04 18:44:37 +0000 | [diff] [blame] | 147 | DraggableDirective, |
| 148 | BadgeSvgComponent |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 149 | ], |
| 150 | providers: [ |
| 151 | { provide: LogService, useValue: logSpy }, |
Sean Condon | 64060ff | 2019-05-30 15:48:11 +0100 | [diff] [blame] | 152 | { provide: ActivatedRoute, useValue: ar }, |
| 153 | { provide: FnService, useValue: fs }, |
| 154 | { provide: ChangeDetectorRef, useClass: ChangeDetectorRef }, |
| 155 | { provide: UrlFnService, useClass: MockUrlFnService }, |
| 156 | { provide: WebSocketService, useClass: MockWebSocketService }, |
| 157 | { provide: LionService, useFactory: (() => { |
| 158 | return { |
| 159 | bundle: ((bundleId) => mockLion), |
| 160 | ubercache: new Array(), |
| 161 | loadCbs: new Map<string, () => void>([]) |
| 162 | }; |
| 163 | }) |
| 164 | }, |
| 165 | { provide: IconService, useClass: MockIconService }, |
| 166 | { provide: SvgUtilService, useClass: MockSvgUtilService }, |
| 167 | { provide: TopologyService, useClass: MockTopologyService }, |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 168 | { provide: 'Window', useValue: windowMock }, |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 169 | ] |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 170 | }) |
| 171 | .compileComponents(); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 172 | logServiceSpy = TestBed.get(LogService); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 173 | |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 174 | fixture = TestBed.createComponent(ForceSvgComponent); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 175 | component = fixture.debugElement.componentInstance; |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 176 | fixture.detectChanges(); |
| 177 | }); |
| 178 | |
| 179 | it('should create', () => { |
| 180 | expect(component).toBeTruthy(); |
| 181 | }); |
Sean Condon | c13d956 | 2019-04-18 13:24:42 +0100 | [diff] [blame] | 182 | |
Sean Condon | b77768e | 2019-05-04 20:23:42 +0100 | [diff] [blame] | 183 | it('load sample files', () => { |
| 184 | expect(openflowSampleData).toBeTruthy(); |
| 185 | expect(openflowSampleData.payload).toBeTruthy(); |
| 186 | expect(openflowSampleData.payload.id).toBe('(root)'); |
| 187 | |
| 188 | expect(odtnSampleData).toBeTruthy(); |
| 189 | expect(odtnSampleData.payload).toBeTruthy(); |
| 190 | expect(odtnSampleData.payload.id).toBe('(root)'); |
Sean Condon | c13d956 | 2019-04-18 13:24:42 +0100 | [diff] [blame] | 191 | }); |
| 192 | |
| 193 | it('should read sample data payload as Region', () => { |
Sean Condon | b77768e | 2019-05-04 20:23:42 +0100 | [diff] [blame] | 194 | expect(openflowRegionData).toBeTruthy(); |
Sean Condon | c13d956 | 2019-04-18 13:24:42 +0100 | [diff] [blame] | 195 | // console.log(regionData); |
Sean Condon | b77768e | 2019-05-04 20:23:42 +0100 | [diff] [blame] | 196 | expect(openflowRegionData.id).toBe('(root)'); |
| 197 | expect(openflowRegionData.devices).toBeTruthy(); |
| 198 | expect(openflowRegionData.devices.length).toBe(3); |
| 199 | expect(openflowRegionData.devices[2].length).toBe(10); |
| 200 | expect(openflowRegionData.hosts.length).toBe(3); |
| 201 | expect(openflowRegionData.hosts[2].length).toBe(20); |
| 202 | expect(openflowRegionData.links.length).toBe(44); |
Sean Condon | c13d956 | 2019-04-18 13:24:42 +0100 | [diff] [blame] | 203 | }); |
| 204 | |
| 205 | it('should read device246 correctly', () => { |
Sean Condon | b77768e | 2019-05-04 20:23:42 +0100 | [diff] [blame] | 206 | const device246: Device = openflowRegionData.devices[2][0]; |
Sean Condon | c13d956 | 2019-04-18 13:24:42 +0100 | [diff] [blame] | 207 | expect(device246.id).toBe('of:0000000000000246'); |
| 208 | expect(device246.nodeType).toBe('device'); |
| 209 | expect(device246.type).toBe('switch'); |
| 210 | expect(device246.online).toBe(true); |
| 211 | expect(device246.master).toBe('10.192.19.68'); |
| 212 | expect(device246.layer).toBe('def'); |
| 213 | |
| 214 | expect(device246.props.managementAddress).toBe('10.192.19.69'); |
| 215 | expect(device246.props.protocol).toBe('OF_13'); |
| 216 | expect(device246.props.driver).toBe('ofdpa-ovs'); |
| 217 | expect(device246.props.latitude).toBe('40.15'); |
| 218 | expect(device246.props.name).toBe('s246'); |
| 219 | expect(device246.props.locType).toBe('geo'); |
| 220 | expect(device246.props.channelId).toBe('10.192.19.69:59980'); |
| 221 | expect(device246.props.longitude).toBe('-121.679'); |
| 222 | |
| 223 | expect(device246.location.locType).toBe('geo'); |
| 224 | expect(device246.location.latOrY).toBe(40.15); |
| 225 | expect(device246.location.longOrX).toBe(-121.679); |
| 226 | }); |
| 227 | |
| 228 | it('should read host 3 correctly', () => { |
Sean Condon | b77768e | 2019-05-04 20:23:42 +0100 | [diff] [blame] | 229 | const host3: Host = openflowRegionData.hosts[2][0]; |
Sean Condon | c13d956 | 2019-04-18 13:24:42 +0100 | [diff] [blame] | 230 | expect(host3.id).toBe('00:88:00:00:00:03/110'); |
| 231 | expect(host3.nodeType).toBe('host'); |
| 232 | expect(host3.layer).toBe('def'); |
| 233 | expect(host3.configured).toBe(false); |
| 234 | expect(host3.ips.length).toBe(3); |
| 235 | expect(host3.ips[0]).toBe('fe80::288:ff:fe00:3'); |
| 236 | expect(host3.ips[1]).toBe('2000::102'); |
| 237 | expect(host3.ips[2]).toBe('10.0.1.2'); |
| 238 | }); |
| 239 | |
| 240 | it('should read link 3-205 correctly', () => { |
Sean Condon | b77768e | 2019-05-04 20:23:42 +0100 | [diff] [blame] | 241 | const link3_205: Link = openflowRegionData.links[0]; |
Sean Condon | c13d956 | 2019-04-18 13:24:42 +0100 | [diff] [blame] | 242 | expect(link3_205.id).toBe('00:AA:00:00:00:03/None~of:0000000000000205/6'); |
| 243 | expect(link3_205.epA).toBe('00:AA:00:00:00:03/None'); |
| 244 | expect(link3_205.epB).toBe('of:0000000000000205'); |
| 245 | expect(String(LinkType[link3_205.type])).toBe('2'); |
| 246 | expect(link3_205.portA).toBe(undefined); |
| 247 | expect(link3_205.portB).toBe('6'); |
| 248 | |
| 249 | expect(link3_205.rollup).toBeTruthy(); |
| 250 | expect(link3_205.rollup.length).toBe(1); |
| 251 | expect(link3_205.rollup[0].id).toBe('00:AA:00:00:00:03/None~of:0000000000000205/6'); |
| 252 | expect(link3_205.rollup[0].epA).toBe('00:AA:00:00:00:03/None'); |
| 253 | expect(link3_205.rollup[0].epB).toBe('of:0000000000000205'); |
| 254 | expect(String(LinkType[link3_205.rollup[0].type])).toBe('2'); |
| 255 | expect(link3_205.rollup[0].portA).toBe(undefined); |
| 256 | expect(link3_205.rollup[0].portB).toBe('6'); |
| 257 | |
| 258 | }); |
| 259 | |
| 260 | it('should handle regionData change - empty Region', () => { |
| 261 | component.ngOnChanges( |
| 262 | {'regionData' : new SimpleChange(<Region>{}, emptyRegion, true)}); |
| 263 | |
| 264 | expect(component.graph.nodes.length).toBe(0); |
| 265 | }); |
| 266 | |
Sean Condon | b77768e | 2019-05-04 20:23:42 +0100 | [diff] [blame] | 267 | it('should know how to format names', () => { |
| 268 | expect(ForceSvgComponent.extractNodeName('00:AA:00:00:00:03/None', undefined)) |
Sean Condon | c13d956 | 2019-04-18 13:24:42 +0100 | [diff] [blame] | 269 | .toEqual('00:AA:00:00:00:03/None'); |
| 270 | |
Sean Condon | b77768e | 2019-05-04 20:23:42 +0100 | [diff] [blame] | 271 | expect(ForceSvgComponent.extractNodeName('00:AA:00:00:00:03/161', '161')) |
| 272 | .toEqual('00:AA:00:00:00:03'); |
Sean Condon | c13d956 | 2019-04-18 13:24:42 +0100 | [diff] [blame] | 273 | |
Sean Condon | b77768e | 2019-05-04 20:23:42 +0100 | [diff] [blame] | 274 | // Like epB of first example in sampleData file - endPtStr contains port number |
| 275 | expect(ForceSvgComponent.extractNodeName('of:0000000000000206/6', '6')) |
| 276 | .toEqual('of:0000000000000206'); |
| 277 | |
| 278 | // Like epB of second example in sampleData file - endPtStr does not contain port number |
| 279 | expect(ForceSvgComponent.extractNodeName('of:0000000000000206', '6')) |
Sean Condon | c13d956 | 2019-04-18 13:24:42 +0100 | [diff] [blame] | 280 | .toEqual('of:0000000000000206'); |
| 281 | }); |
| 282 | |
Sean Condon | b77768e | 2019-05-04 20:23:42 +0100 | [diff] [blame] | 283 | it('should handle openflow regionData change - sample Region', () => { |
| 284 | component.regionData = openflowRegionData; |
Sean Condon | c13d956 | 2019-04-18 13:24:42 +0100 | [diff] [blame] | 285 | component.ngOnChanges( |
Sean Condon | b77768e | 2019-05-04 20:23:42 +0100 | [diff] [blame] | 286 | {'regionData' : new SimpleChange(<Region>{}, openflowRegionData, true)}); |
Sean Condon | c13d956 | 2019-04-18 13:24:42 +0100 | [diff] [blame] | 287 | |
| 288 | expect(component.graph.nodes.length).toBe(30); |
| 289 | |
| 290 | expect(component.graph.links.length).toBe(44); |
| 291 | |
| 292 | }); |
Sean Condon | b77768e | 2019-05-04 20:23:42 +0100 | [diff] [blame] | 293 | |
| 294 | it('should handle odtn regionData change - sample odtn Region', () => { |
| 295 | component.regionData = odtnRegionData; |
| 296 | component.ngOnChanges( |
| 297 | {'regionData' : new SimpleChange(<Region>{}, odtnRegionData, true)}); |
| 298 | |
| 299 | expect(component.graph.nodes.length).toBe(2); |
| 300 | |
| 301 | expect(component.graph.links.length).toBe(6); |
| 302 | |
| 303 | }); |
Sean Condon | d92b0bd | 2020-03-23 08:40:55 +0000 | [diff] [blame] | 304 | |
| 305 | it('should handle highlights and match them to existing links', () => { |
| 306 | component.regionData = topo2BaseRegionData; |
| 307 | component.ngOnChanges( |
| 308 | {'regionData' : new SimpleChange(<Region>{}, topo2BaseRegionData, true)}); |
| 309 | |
| 310 | expect(component.graph.links.length).toBe(9); |
| 311 | |
| 312 | expect(linkHightlights.length).toBe(6); |
| 313 | |
| 314 | // should be able to find all of the highlighted links in the original data set |
| 315 | linkHightlights.forEach((lh: LinkHighlight) => { |
| 316 | const foundLink = component.graph.links.find((l: Link) => l.id === Link.linkIdFromShowHighlights(lh.id)); |
| 317 | expect(foundLink).toBeDefined(); |
| 318 | }); |
| 319 | }); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 320 | }); |