blob: 446d41c44419ff2ca9e0dda247b128aeabcadf7b [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';
21
22import { 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';
43import {
44 DeviceNodeSvgComponent, HostNodeSvgComponent,
45 LinkSvgComponent, SubRegionNodeSvgComponent
46} from '../layer/forcesvg/visuals';
47import {DraggableDirective} from '../layer/forcesvg/draggable/draggable.directive';
Sean Condonf4f54a12018-10-10 23:25:46 +010048
49
50class MockActivatedRoute extends ActivatedRoute {
51 constructor(params: Params) {
52 super();
53 this.queryParams = of(params);
54 }
55}
56
57class MockHttpClient {}
58
Sean Condonaa4366d2018-11-02 14:29:01 +000059class MockTopologyService {
60 init(instance: InstanceComponent) {
61 instance.onosInstances = [
62 <Instance>{
63 'id': 'inst1',
64 'ip': '127.0.0.1',
65 'reachable': true,
66 'online': true,
67 'ready': true,
68 'switches': 4,
69 'uiAttached': true
70 },
71 <Instance>{
72 'id': 'inst1',
73 'ip': '127.0.0.2',
74 'reachable': true,
75 'online': true,
76 'ready': true,
77 'switches': 3,
78 'uiAttached': false
79 }
80 ];
81 }
82 destroy() {}
83}
84
Sean Condon91481822019-01-01 13:56:14 +000085class MockIconService {
86 loadIconDef() { }
87}
88
Sean Condonb2c483c2019-01-16 20:28:55 +000089class MockKeysService {
90 quickHelpShown: boolean = true;
91
92 keyBindings(x) {
93 return {};
94 }
95
96 gestureNotes() {
97 return {};
98 }
99}
100
101class MockTrafficService {}
102
103class MockPrefsService {
104 listeners: ((data) => void)[] = [];
105
106 getPrefs() {
107 return { 'topo2_prefs': ''};
108 }
109
110 addListener(listener: (data) => void): void {
111 this.listeners.push(listener);
112 }
113
114 removeListener(listener: (data) => void) {
115 this.listeners = this.listeners.filter((obj) => obj !== listener);
116 }
117
118}
119
Sean Condonf4f54a12018-10-10 23:25:46 +0100120/**
121 * ONOS GUI -- Topology View -- Unit Tests
122 */
123describe('TopologyComponent', () => {
124 let fs: FnService;
125 let ar: MockActivatedRoute;
126 let windowMock: Window;
127 let logServiceSpy: jasmine.SpyObj<LogService>;
128 let component: TopologyComponent;
129 let fixture: ComponentFixture<TopologyComponent>;
130
Sean Condonb2c483c2019-01-16 20:28:55 +0000131 const bundleObj = {
132 'core.fw.QuickHelp': {
133 test: 'test1',
134 tt_help: 'Help!'
135 }
136 };
137 const mockLion = (key) => {
138 return bundleObj[key] || '%' + key + '%';
139 };
140
Sean Condonf4f54a12018-10-10 23:25:46 +0100141 beforeEach(async(() => {
142 const logSpy = jasmine.createSpyObj('LogService', ['info', 'debug', 'warn', 'error']);
143 ar = new MockActivatedRoute({ 'debug': 'txrx' });
144
145 windowMock = <any>{
146 location: <any>{
147 hostname: 'foo',
148 host: 'foo',
149 port: '80',
150 protocol: 'http',
151 search: { debug: 'true' },
152 href: 'ws://foo:123/onos/ui/websock/path',
153 absUrl: 'ws://foo:123/onos/ui/websock/path'
154 }
155 };
156 fs = new FnService(ar, logSpy, windowMock);
157
158 TestBed.configureTestingModule({
Sean Condon91481822019-01-01 13:56:14 +0000159 imports: [ BrowserAnimationsModule, RouterTestingModule ],
Sean Condonf4f54a12018-10-10 23:25:46 +0100160 declarations: [
161 TopologyComponent,
162 InstanceComponent,
163 SummaryComponent,
164 ToolbarComponent,
165 DetailsComponent,
Sean Condon0c577f62018-11-18 22:40:05 +0000166 FlashComponent,
Sean Condon91481822019-01-01 13:56:14 +0000167 ZoomableDirective,
Sean Condonb2c483c2019-01-16 20:28:55 +0000168 IconComponent,
169 QuickhelpComponent,
170 ForceSvgComponent,
171 LinkSvgComponent,
172 DeviceNodeSvgComponent,
173 HostNodeSvgComponent,
174 DraggableDirective,
175 ZoomableDirective,
176 SubRegionNodeSvgComponent
Sean Condonf4f54a12018-10-10 23:25:46 +0100177 ],
178 providers: [
179 { provide: FnService, useValue: fs },
180 { provide: LogService, useValue: logSpy },
181 { provide: 'Window', useValue: windowMock },
182 { provide: HttpClient, useClass: MockHttpClient },
Sean Condon0c577f62018-11-18 22:40:05 +0000183 { provide: TopologyService, useClass: MockTopologyService },
Sean Condonb2c483c2019-01-16 20:28:55 +0000184 { provide: TrafficService, useClass: MockTrafficService },
Sean Condon91481822019-01-01 13:56:14 +0000185 { provide: IconService, useClass: MockIconService },
Sean Condonb2c483c2019-01-16 20:28:55 +0000186 { provide: PrefsService, useClass: MockPrefsService },
187 { provide: KeysService, useClass: MockKeysService },
188 { provide: LionService, useFactory: (() => {
189 return {
190 bundle: ((bundleId) => mockLion),
191 ubercache: new Array(),
192 loadCbs: new Map<string, () => void>([])
193 };
194 })
195 },
Sean Condonf4f54a12018-10-10 23:25:46 +0100196 ]
197 }).compileComponents();
198 logServiceSpy = TestBed.get(LogService);
199 }));
200
201 beforeEach(() => {
202 fixture = TestBed.createComponent(TopologyComponent);
203 component = fixture.componentInstance;
204 fixture.detectChanges();
205 });
206
207 it('should create', () => {
208 expect(component).toBeTruthy();
209 });
210});