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 | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 19 | import {FnService, LogService} from 'gui2-fw-lib'; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 20 | import {DraggableDirective} from './draggable/draggable.directive'; |
| 21 | import {ActivatedRoute, Params} from '@angular/router'; |
| 22 | import {of} from 'rxjs'; |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 23 | import {MapSvgComponent} from '../mapsvg/mapsvg.component'; |
Sean Condon | ff85fbe | 2019-03-16 14:28:46 +0000 | [diff] [blame] | 24 | import {DeviceNodeSvgComponent} from './visuals/devicenodesvg/devicenodesvg.component'; |
| 25 | import {SubRegionNodeSvgComponent} from './visuals/subregionnodesvg/subregionnodesvg.component'; |
| 26 | import {HostNodeSvgComponent} from './visuals/hostnodesvg/hostnodesvg.component'; |
| 27 | import {LinkSvgComponent} from './visuals/linksvg/linksvg.component'; |
Sean Condon | c13d956 | 2019-04-18 13:24:42 +0100 | [diff] [blame] | 28 | import {Device, Host, Link, LinkType, Region} from './models'; |
| 29 | import {SimpleChange} from '@angular/core'; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 30 | |
| 31 | class MockActivatedRoute extends ActivatedRoute { |
| 32 | constructor(params: Params) { |
| 33 | super(); |
| 34 | this.queryParams = of(params); |
| 35 | } |
| 36 | } |
| 37 | |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 38 | describe('ForceSvgComponent', () => { |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 39 | let fs: FnService; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 40 | let ar: MockActivatedRoute; |
| 41 | let windowMock: Window; |
| 42 | let logServiceSpy: jasmine.SpyObj<LogService>; |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 43 | let component: ForceSvgComponent; |
| 44 | let fixture: ComponentFixture<ForceSvgComponent>; |
Sean Condon | c13d956 | 2019-04-18 13:24:42 +0100 | [diff] [blame] | 45 | const sampledata = require('./tests/test-module-topo2CurrentRegion.json'); |
| 46 | const regionData: Region = <Region><unknown>(sampledata.payload); |
| 47 | const emptyRegion: Region = <Region>{devices: [ [], [], [] ], hosts: [ [], [], [] ], links: []}; |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 48 | |
| 49 | beforeEach(async(() => { |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 50 | const logSpy = jasmine.createSpyObj('LogService', ['info', 'debug', 'warn', 'error']); |
| 51 | ar = new MockActivatedRoute({ 'debug': 'txrx' }); |
| 52 | |
| 53 | windowMock = <any>{ |
| 54 | location: <any>{ |
| 55 | hostname: 'foo', |
| 56 | host: 'foo', |
| 57 | port: '80', |
| 58 | protocol: 'http', |
| 59 | search: { debug: 'true' }, |
| 60 | href: 'ws://foo:123/onos/ui/websock/path', |
| 61 | absUrl: 'ws://foo:123/onos/ui/websock/path' |
| 62 | } |
| 63 | }; |
| 64 | |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 65 | fs = new FnService(ar, logSpy, windowMock); |
| 66 | |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 67 | TestBed.configureTestingModule({ |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 68 | declarations: [ |
| 69 | ForceSvgComponent, |
| 70 | DeviceNodeSvgComponent, |
| 71 | HostNodeSvgComponent, |
| 72 | SubRegionNodeSvgComponent, |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 73 | LinkSvgComponent, |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 74 | DraggableDirective, |
| 75 | MapSvgComponent |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 76 | ], |
| 77 | providers: [ |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 78 | { provide: FnService, useValue: fs }, |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 79 | { provide: LogService, useValue: logSpy }, |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 80 | { provide: 'Window', useValue: windowMock }, |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 81 | ] |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 82 | }) |
| 83 | .compileComponents(); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 84 | logServiceSpy = TestBed.get(LogService); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 85 | })); |
| 86 | |
| 87 | beforeEach(() => { |
| 88 | fixture = TestBed.createComponent(ForceSvgComponent); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 89 | component = fixture.debugElement.componentInstance; |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 90 | fixture.detectChanges(); |
| 91 | }); |
| 92 | |
| 93 | it('should create', () => { |
| 94 | expect(component).toBeTruthy(); |
| 95 | }); |
Sean Condon | c13d956 | 2019-04-18 13:24:42 +0100 | [diff] [blame] | 96 | |
| 97 | it('load sample file', () => { |
| 98 | expect(sampledata).toBeTruthy(); |
| 99 | expect(sampledata.payload).toBeTruthy(); |
| 100 | expect(sampledata.payload.id).toBe('(root)'); |
| 101 | }); |
| 102 | |
| 103 | it('should read sample data payload as Region', () => { |
| 104 | expect(regionData).toBeTruthy(); |
| 105 | // console.log(regionData); |
| 106 | expect(regionData.id).toBe('(root)'); |
| 107 | expect(regionData.devices).toBeTruthy(); |
| 108 | expect(regionData.devices.length).toBe(3); |
| 109 | expect(regionData.devices[2].length).toBe(10); |
| 110 | expect(regionData.hosts.length).toBe(3); |
| 111 | expect(regionData.hosts[2].length).toBe(20); |
| 112 | expect(regionData.links.length).toBe(44); |
| 113 | }); |
| 114 | |
| 115 | it('should read device246 correctly', () => { |
| 116 | const device246: Device = regionData.devices[2][0]; |
| 117 | expect(device246.id).toBe('of:0000000000000246'); |
| 118 | expect(device246.nodeType).toBe('device'); |
| 119 | expect(device246.type).toBe('switch'); |
| 120 | expect(device246.online).toBe(true); |
| 121 | expect(device246.master).toBe('10.192.19.68'); |
| 122 | expect(device246.layer).toBe('def'); |
| 123 | |
| 124 | expect(device246.props.managementAddress).toBe('10.192.19.69'); |
| 125 | expect(device246.props.protocol).toBe('OF_13'); |
| 126 | expect(device246.props.driver).toBe('ofdpa-ovs'); |
| 127 | expect(device246.props.latitude).toBe('40.15'); |
| 128 | expect(device246.props.name).toBe('s246'); |
| 129 | expect(device246.props.locType).toBe('geo'); |
| 130 | expect(device246.props.channelId).toBe('10.192.19.69:59980'); |
| 131 | expect(device246.props.longitude).toBe('-121.679'); |
| 132 | |
| 133 | expect(device246.location.locType).toBe('geo'); |
| 134 | expect(device246.location.latOrY).toBe(40.15); |
| 135 | expect(device246.location.longOrX).toBe(-121.679); |
| 136 | }); |
| 137 | |
| 138 | it('should read host 3 correctly', () => { |
| 139 | const host3: Host = regionData.hosts[2][0]; |
| 140 | expect(host3.id).toBe('00:88:00:00:00:03/110'); |
| 141 | expect(host3.nodeType).toBe('host'); |
| 142 | expect(host3.layer).toBe('def'); |
| 143 | expect(host3.configured).toBe(false); |
| 144 | expect(host3.ips.length).toBe(3); |
| 145 | expect(host3.ips[0]).toBe('fe80::288:ff:fe00:3'); |
| 146 | expect(host3.ips[1]).toBe('2000::102'); |
| 147 | expect(host3.ips[2]).toBe('10.0.1.2'); |
| 148 | }); |
| 149 | |
| 150 | it('should read link 3-205 correctly', () => { |
| 151 | const link3_205: Link = regionData.links[0]; |
| 152 | expect(link3_205.id).toBe('00:AA:00:00:00:03/None~of:0000000000000205/6'); |
| 153 | expect(link3_205.epA).toBe('00:AA:00:00:00:03/None'); |
| 154 | expect(link3_205.epB).toBe('of:0000000000000205'); |
| 155 | expect(String(LinkType[link3_205.type])).toBe('2'); |
| 156 | expect(link3_205.portA).toBe(undefined); |
| 157 | expect(link3_205.portB).toBe('6'); |
| 158 | |
| 159 | expect(link3_205.rollup).toBeTruthy(); |
| 160 | expect(link3_205.rollup.length).toBe(1); |
| 161 | expect(link3_205.rollup[0].id).toBe('00:AA:00:00:00:03/None~of:0000000000000205/6'); |
| 162 | expect(link3_205.rollup[0].epA).toBe('00:AA:00:00:00:03/None'); |
| 163 | expect(link3_205.rollup[0].epB).toBe('of:0000000000000205'); |
| 164 | expect(String(LinkType[link3_205.rollup[0].type])).toBe('2'); |
| 165 | expect(link3_205.rollup[0].portA).toBe(undefined); |
| 166 | expect(link3_205.rollup[0].portB).toBe('6'); |
| 167 | |
| 168 | }); |
| 169 | |
| 170 | it('should handle regionData change - empty Region', () => { |
| 171 | component.ngOnChanges( |
| 172 | {'regionData' : new SimpleChange(<Region>{}, emptyRegion, true)}); |
| 173 | |
| 174 | expect(component.graph.nodes.length).toBe(0); |
| 175 | }); |
| 176 | |
| 177 | it('should know hwo to format names', () => { |
| 178 | expect(ForceSvgComponent.extractNodeName('00:AA:00:00:00:03/None')) |
| 179 | .toEqual('00:AA:00:00:00:03/None'); |
| 180 | |
| 181 | expect(ForceSvgComponent.extractNodeName('00:AA:00:00:00:03/161')) |
| 182 | .toEqual('00:AA:00:00:00:03/161'); |
| 183 | |
| 184 | expect(ForceSvgComponent.extractNodeName('of:0000000000000206/6')) |
| 185 | .toEqual('of:0000000000000206'); |
| 186 | }); |
| 187 | |
| 188 | it('should handle regionData change - sample Region', () => { |
| 189 | component.regionData = regionData; |
| 190 | component.ngOnChanges( |
| 191 | {'regionData' : new SimpleChange(<Region>{}, regionData, true)}); |
| 192 | |
| 193 | expect(component.graph.nodes.length).toBe(30); |
| 194 | |
| 195 | expect(component.graph.links.length).toBe(44); |
| 196 | |
| 197 | }); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 198 | }); |