blob: 0492858dc53c0670b2cb28dae925b088398c9416 [file] [log] [blame]
Sean Condonf4f54a12018-10-10 23:25:46 +01001/*
Sean Condonb2c483c2019-01-16 20:28:55 +00002 * Copyright 2019-present Open Networking Foundation
Sean Condonf4f54a12018-10-10 23:25:46 +01003 *
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 */
16import { async, ComponentFixture, TestBed } from '@angular/core/testing';
17import { ActivatedRoute, Params } from '@angular/router';
18import { of } from 'rxjs';
19import { HttpClient } from '@angular/common/http';
20import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
Sean Condon0d064ec2019-02-04 21:53:53 +000021import * as d3 from 'd3';
Sean Condonf4f54a12018-10-10 23:25:46 +010022import { TopologyComponent } from './topology.component';
Sean Condonaa4366d2018-11-02 14:29:01 +000023import {
24 Instance,
25 InstanceComponent
26} from '../panel/instance/instance.component';
Sean Condonf4f54a12018-10-10 23:25:46 +010027import { SummaryComponent } from '../panel/summary/summary.component';
28import { ToolbarComponent } from '../panel/toolbar/toolbar.component';
29import { DetailsComponent } from '../panel/details/details.component';
Sean Condonaa4366d2018-11-02 14:29:01 +000030import { TopologyService } from '../topology.service';
Sean Condonf4f54a12018-10-10 23:25:46 +010031
32import {
33 FlashComponent,
Sean Condonb2c483c2019-01-16 20:28:55 +000034 QuickhelpComponent,
Sean Condonf4f54a12018-10-10 23:25:46 +010035 FnService,
Sean Condon91481822019-01-01 13:56:14 +000036 LogService,
Sean Condonb2c483c2019-01-16 20:28:55 +000037 IconService, IconComponent, PrefsService, KeysService, LionService
Sean Condonf4f54a12018-10-10 23:25:46 +010038} from 'gui2-fw-lib';
Sean Condon0c577f62018-11-18 22:40:05 +000039import {ZoomableDirective} from '../layer/zoomable.directive';
Sean Condon91481822019-01-01 13:56:14 +000040import {RouterTestingModule} from '@angular/router/testing';
Sean Condonb2c483c2019-01-16 20:28:55 +000041import {TrafficService} from '../traffic.service';
42import {ForceSvgComponent} from '../layer/forcesvg/forcesvg.component';
Sean Condonb2c483c2019-01-16 20:28:55 +000043import {DraggableDirective} from '../layer/forcesvg/draggable/draggable.directive';
Sean Condon0d064ec2019-02-04 21:53:53 +000044import {MapSelectorComponent} from '../panel/mapselector/mapselector.component';
45import {BackgroundSvgComponent} from '../layer/backgroundsvg/backgroundsvg.component';
46import {FormsModule, ReactiveFormsModule} from '@angular/forms';
47import {MapSvgComponent} from '../layer/mapsvg/mapsvg.component';
Sean Condon71910542019-02-16 18:16:42 +000048import {GridsvgComponent} from '../layer/gridsvg/gridsvg.component';
Sean Condonff85fbe2019-03-16 14:28:46 +000049import {LinkSvgComponent} from '../layer/forcesvg/visuals/linksvg/linksvg.component';
50import {DeviceNodeSvgComponent} from '../layer/forcesvg/visuals/devicenodesvg/devicenodesvg.component';
51import {SubRegionNodeSvgComponent} from '../layer/forcesvg/visuals/subregionnodesvg/subregionnodesvg.component';
52import {HostNodeSvgComponent} from '../layer/forcesvg/visuals/hostnodesvg/hostnodesvg.component';
Sean Condon28884332019-03-21 14:07:00 +000053import {LayoutService} from '../layout.service';
Sean Condonf4f54a12018-10-10 23:25:46 +010054
55
56class MockActivatedRoute extends ActivatedRoute {
57 constructor(params: Params) {
58 super();
59 this.queryParams = of(params);
60 }
61}
62
63class MockHttpClient {}
64
Sean Condonaa4366d2018-11-02 14:29:01 +000065class MockTopologyService {
66 init(instance: InstanceComponent) {
67 instance.onosInstances = [
68 <Instance>{
69 'id': 'inst1',
70 'ip': '127.0.0.1',
71 'reachable': true,
72 'online': true,
73 'ready': true,
74 'switches': 4,
75 'uiAttached': true
76 },
77 <Instance>{
78 'id': 'inst1',
79 'ip': '127.0.0.2',
80 'reachable': true,
81 'online': true,
82 'ready': true,
83 'switches': 3,
84 'uiAttached': false
85 }
86 ];
87 }
88 destroy() {}
89}
90
Sean Condon91481822019-01-01 13:56:14 +000091class MockIconService {
92 loadIconDef() { }
93}
94
Sean Condonb2c483c2019-01-16 20:28:55 +000095class MockKeysService {
96 quickHelpShown: boolean = true;
97
Sean Condonbed2e032019-04-17 22:22:49 +010098 keyHandler: {
99 viewKeys: any[],
100 globalKeys: any[]
101 };
102
103 mockViewKeys: Object[];
104 constructor() {
105 this.mockViewKeys = [];
106 this.keyHandler = {
107 viewKeys: this.mockViewKeys,
108 globalKeys: this.mockViewKeys
109 };
110 }
111
Sean Condonb2c483c2019-01-16 20:28:55 +0000112 keyBindings(x) {
113 return {};
114 }
115
116 gestureNotes() {
117 return {};
118 }
119}
120
Sean Condon19e83672019-04-13 16:21:52 +0100121class MockTrafficService {
122 init(force: ForceSvgComponent) {}
123}
Sean Condonb2c483c2019-01-16 20:28:55 +0000124
Sean Condon28884332019-03-21 14:07:00 +0000125class MockLayoutService {}
126
Sean Condonb2c483c2019-01-16 20:28:55 +0000127class MockPrefsService {
128 listeners: ((data) => void)[] = [];
129
130 getPrefs() {
131 return { 'topo2_prefs': ''};
132 }
133
134 addListener(listener: (data) => void): void {
135 this.listeners.push(listener);
136 }
137
138 removeListener(listener: (data) => void) {
139 this.listeners = this.listeners.filter((obj) => obj !== listener);
140 }
141
Sean Condon71910542019-02-16 18:16:42 +0000142 setPrefs(name: string, obj: Object) {
143
144 }
145
Sean Condonb2c483c2019-01-16 20:28:55 +0000146}
147
Sean Condonf4f54a12018-10-10 23:25:46 +0100148/**
149 * ONOS GUI -- Topology View -- Unit Tests
150 */
151describe('TopologyComponent', () => {
152 let fs: FnService;
153 let ar: MockActivatedRoute;
154 let windowMock: Window;
155 let logServiceSpy: jasmine.SpyObj<LogService>;
156 let component: TopologyComponent;
157 let fixture: ComponentFixture<TopologyComponent>;
158
Sean Condonb2c483c2019-01-16 20:28:55 +0000159 const bundleObj = {
160 'core.fw.QuickHelp': {
161 test: 'test1',
162 tt_help: 'Help!'
163 }
164 };
165 const mockLion = (key) => {
166 return bundleObj[key] || '%' + key + '%';
167 };
168
Sean Condonf4f54a12018-10-10 23:25:46 +0100169 beforeEach(async(() => {
170 const logSpy = jasmine.createSpyObj('LogService', ['info', 'debug', 'warn', 'error']);
171 ar = new MockActivatedRoute({ 'debug': 'txrx' });
172
173 windowMock = <any>{
174 location: <any>{
175 hostname: 'foo',
176 host: 'foo',
177 port: '80',
178 protocol: 'http',
179 search: { debug: 'true' },
180 href: 'ws://foo:123/onos/ui/websock/path',
181 absUrl: 'ws://foo:123/onos/ui/websock/path'
182 }
183 };
184 fs = new FnService(ar, logSpy, windowMock);
185
186 TestBed.configureTestingModule({
Sean Condon0d064ec2019-02-04 21:53:53 +0000187 imports: [
188 BrowserAnimationsModule,
189 RouterTestingModule,
190 FormsModule,
191 ReactiveFormsModule
192 ],
Sean Condonf4f54a12018-10-10 23:25:46 +0100193 declarations: [
194 TopologyComponent,
195 InstanceComponent,
196 SummaryComponent,
197 ToolbarComponent,
198 DetailsComponent,
Sean Condon0c577f62018-11-18 22:40:05 +0000199 FlashComponent,
Sean Condon91481822019-01-01 13:56:14 +0000200 ZoomableDirective,
Sean Condonb2c483c2019-01-16 20:28:55 +0000201 IconComponent,
202 QuickhelpComponent,
203 ForceSvgComponent,
204 LinkSvgComponent,
205 DeviceNodeSvgComponent,
206 HostNodeSvgComponent,
207 DraggableDirective,
208 ZoomableDirective,
Sean Condon0d064ec2019-02-04 21:53:53 +0000209 SubRegionNodeSvgComponent,
210 MapSelectorComponent,
211 BackgroundSvgComponent,
Sean Condon71910542019-02-16 18:16:42 +0000212 MapSvgComponent,
213 GridsvgComponent
Sean Condonf4f54a12018-10-10 23:25:46 +0100214 ],
215 providers: [
216 { provide: FnService, useValue: fs },
217 { provide: LogService, useValue: logSpy },
218 { provide: 'Window', useValue: windowMock },
219 { provide: HttpClient, useClass: MockHttpClient },
Sean Condon0c577f62018-11-18 22:40:05 +0000220 { provide: TopologyService, useClass: MockTopologyService },
Sean Condonb2c483c2019-01-16 20:28:55 +0000221 { provide: TrafficService, useClass: MockTrafficService },
Sean Condon28884332019-03-21 14:07:00 +0000222 { provide: LayoutService, useClass: MockLayoutService },
Sean Condon91481822019-01-01 13:56:14 +0000223 { provide: IconService, useClass: MockIconService },
Sean Condonb2c483c2019-01-16 20:28:55 +0000224 { provide: PrefsService, useClass: MockPrefsService },
225 { provide: KeysService, useClass: MockKeysService },
226 { provide: LionService, useFactory: (() => {
227 return {
228 bundle: ((bundleId) => mockLion),
229 ubercache: new Array(),
230 loadCbs: new Map<string, () => void>([])
231 };
232 })
233 },
Sean Condonf4f54a12018-10-10 23:25:46 +0100234 ]
235 }).compileComponents();
236 logServiceSpy = TestBed.get(LogService);
237 }));
238
239 beforeEach(() => {
240 fixture = TestBed.createComponent(TopologyComponent);
241 component = fixture.componentInstance;
Sean Condon0d064ec2019-02-04 21:53:53 +0000242
Sean Condonf4f54a12018-10-10 23:25:46 +0100243 fixture.detectChanges();
244 });
245
246 it('should create', () => {
247 expect(component).toBeTruthy();
248 });
249});