blob: bc3fd76143d92fe00e0bda219657dbd44eb9b0d3 [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) {}
Sean Condonc13d9562019-04-18 13:24:42 +0100123 destroy() {}
Sean Condon19e83672019-04-13 16:21:52 +0100124}
Sean Condonb2c483c2019-01-16 20:28:55 +0000125
Sean Condon28884332019-03-21 14:07:00 +0000126class MockLayoutService {}
127
Sean Condonb2c483c2019-01-16 20:28:55 +0000128class MockPrefsService {
129 listeners: ((data) => void)[] = [];
130
131 getPrefs() {
132 return { 'topo2_prefs': ''};
133 }
134
135 addListener(listener: (data) => void): void {
136 this.listeners.push(listener);
137 }
138
139 removeListener(listener: (data) => void) {
140 this.listeners = this.listeners.filter((obj) => obj !== listener);
141 }
142
Sean Condon71910542019-02-16 18:16:42 +0000143 setPrefs(name: string, obj: Object) {
144
145 }
146
Sean Condonb2c483c2019-01-16 20:28:55 +0000147}
148
Sean Condonf4f54a12018-10-10 23:25:46 +0100149/**
150 * ONOS GUI -- Topology View -- Unit Tests
151 */
152describe('TopologyComponent', () => {
153 let fs: FnService;
154 let ar: MockActivatedRoute;
155 let windowMock: Window;
156 let logServiceSpy: jasmine.SpyObj<LogService>;
157 let component: TopologyComponent;
158 let fixture: ComponentFixture<TopologyComponent>;
159
Sean Condonb2c483c2019-01-16 20:28:55 +0000160 const bundleObj = {
161 'core.fw.QuickHelp': {
162 test: 'test1',
163 tt_help: 'Help!'
164 }
165 };
166 const mockLion = (key) => {
167 return bundleObj[key] || '%' + key + '%';
168 };
169
Sean Condonf4f54a12018-10-10 23:25:46 +0100170 beforeEach(async(() => {
171 const logSpy = jasmine.createSpyObj('LogService', ['info', 'debug', 'warn', 'error']);
172 ar = new MockActivatedRoute({ 'debug': 'txrx' });
173
174 windowMock = <any>{
175 location: <any>{
176 hostname: 'foo',
177 host: 'foo',
178 port: '80',
179 protocol: 'http',
180 search: { debug: 'true' },
181 href: 'ws://foo:123/onos/ui/websock/path',
182 absUrl: 'ws://foo:123/onos/ui/websock/path'
183 }
184 };
185 fs = new FnService(ar, logSpy, windowMock);
186
187 TestBed.configureTestingModule({
Sean Condon0d064ec2019-02-04 21:53:53 +0000188 imports: [
189 BrowserAnimationsModule,
190 RouterTestingModule,
191 FormsModule,
192 ReactiveFormsModule
193 ],
Sean Condonf4f54a12018-10-10 23:25:46 +0100194 declarations: [
195 TopologyComponent,
196 InstanceComponent,
197 SummaryComponent,
198 ToolbarComponent,
199 DetailsComponent,
Sean Condon0c577f62018-11-18 22:40:05 +0000200 FlashComponent,
Sean Condon91481822019-01-01 13:56:14 +0000201 ZoomableDirective,
Sean Condonb2c483c2019-01-16 20:28:55 +0000202 IconComponent,
203 QuickhelpComponent,
204 ForceSvgComponent,
205 LinkSvgComponent,
206 DeviceNodeSvgComponent,
207 HostNodeSvgComponent,
208 DraggableDirective,
209 ZoomableDirective,
Sean Condon0d064ec2019-02-04 21:53:53 +0000210 SubRegionNodeSvgComponent,
211 MapSelectorComponent,
212 BackgroundSvgComponent,
Sean Condon71910542019-02-16 18:16:42 +0000213 MapSvgComponent,
214 GridsvgComponent
Sean Condonf4f54a12018-10-10 23:25:46 +0100215 ],
216 providers: [
217 { provide: FnService, useValue: fs },
218 { provide: LogService, useValue: logSpy },
219 { provide: 'Window', useValue: windowMock },
220 { provide: HttpClient, useClass: MockHttpClient },
Sean Condon0c577f62018-11-18 22:40:05 +0000221 { provide: TopologyService, useClass: MockTopologyService },
Sean Condonb2c483c2019-01-16 20:28:55 +0000222 { provide: TrafficService, useClass: MockTrafficService },
Sean Condon28884332019-03-21 14:07:00 +0000223 { provide: LayoutService, useClass: MockLayoutService },
Sean Condon91481822019-01-01 13:56:14 +0000224 { provide: IconService, useClass: MockIconService },
Sean Condonb2c483c2019-01-16 20:28:55 +0000225 { provide: PrefsService, useClass: MockPrefsService },
226 { provide: KeysService, useClass: MockKeysService },
227 { provide: LionService, useFactory: (() => {
228 return {
229 bundle: ((bundleId) => mockLion),
230 ubercache: new Array(),
231 loadCbs: new Map<string, () => void>([])
232 };
233 })
234 },
Sean Condonf4f54a12018-10-10 23:25:46 +0100235 ]
236 }).compileComponents();
237 logServiceSpy = TestBed.get(LogService);
238 }));
239
240 beforeEach(() => {
241 fixture = TestBed.createComponent(TopologyComponent);
242 component = fixture.componentInstance;
Sean Condon0d064ec2019-02-04 21:53:53 +0000243
Sean Condonf4f54a12018-10-10 23:25:46 +0100244 fixture.detectChanges();
245 });
246
247 it('should create', () => {
248 expect(component).toBeTruthy();
249 });
250});