blob: 67291f3a3d821c58c72c9e9e28a6d9fc9a43f102 [file] [log] [blame]
Simon Hunt3a6eec02015-02-09 21:16:43 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16
17/*
18 ONOS GUI -- Topo View -- Topo Model Service - Unit Tests
19 */
20describe('factory: view/topo/topoModel.js', function() {
21 var $log, fs, rnd, tms;
22
23 // stop random numbers from being quite so random
24 var mockRandom = {
25 // mock spread returns s + 1
26 spread: function (s) {
27 return s + 1;
28 },
29 // mock random dimension returns d / 2 - 1
30 randDim: function (d) {
31 return d/2 - 1;
32 },
33 mock: 'yup'
34 };
35
36 // to mock out the [lng,lat] <=> [x,y] transformations, we will
37 // add/subtract 2000, 3000 respectively:
38 // lng:2005 === x:5, lat:3004 === y:4
39
40 var mockProjection = function (lnglat) {
41 return [lnglat[0] - 2000, lnglat[1] - 3000];
42 };
43
44 mockProjection.invert = function (xy) {
45 return [xy[0] + 2000, xy[1] + 3000];
46 };
47
Simon Huntdc6adea2015-02-09 22:29:36 -080048 // our test devices and hosts:
49 var dev1 = {
Simon Hunt3a6eec02015-02-09 21:16:43 -080050 'class': 'device',
51 id: 'dev1',
52 x: 17,
53 y: 27,
54 online: true
55 },
Simon Huntdc6adea2015-02-09 22:29:36 -080056 dev2 = {
Simon Hunt3a6eec02015-02-09 21:16:43 -080057 'class': 'device',
58 id: 'dev2',
59 x: 18,
60 y: 28,
61 online: true
62 },
Simon Huntdc6adea2015-02-09 22:29:36 -080063 host1 = {
Simon Hunt3a6eec02015-02-09 21:16:43 -080064 'class': 'host',
65 id: 'host1',
66 x: 23,
67 y: 33,
68 cp: {
69 device: 'dev1',
70 port: 7
71 },
72 ingress: 'dev1/7-host1'
73 },
Simon Huntdc6adea2015-02-09 22:29:36 -080074 host2 = {
Simon Hunt3a6eec02015-02-09 21:16:43 -080075 'class': 'host',
76 id: 'host2',
77 x: 24,
78 y: 34,
79 cp: {
80 device: 'dev0',
81 port: 0
82 },
83 ingress: 'dev0/0-host2'
Simon Huntdc6adea2015-02-09 22:29:36 -080084 };
85
Simon Hunt3a6eec02015-02-09 21:16:43 -080086
87 // our test api
88 var api = {
89 projection: function () { return mockProjection; },
Simon Huntdc6adea2015-02-09 22:29:36 -080090 network: {
91 nodes: [dev1, dev2, host1, host2],
92 links: [],
93 lookup: {dev1: dev1, dev2: dev2, host1: host1, host2: host2},
94 revLinkToKey: {}
95 },
96 restyleLinkElement: function () {},
97 removeLinkElement: function () {}
Simon Hunt3a6eec02015-02-09 21:16:43 -080098 };
99
100 // our test dimensions and well known locations..
101 var dim = [20, 40],
102 randLoc = [9, 19], // random location using randDim(): d/2-1
103 randHostLoc = [40, 50], // host "near" random location
104 // given that 'nearDist' = 15
105 // and spread(15) = 16
106 // 9 + 15 + 16 = 40; 19 + 15 + 16 = 50
107 nearDev1 = [48,58], // [17+15+16, 27+15+16]
108 dev1Loc = [17,27],
109 dev2Loc = [18,28],
110 host1Loc = [23,33],
111 host2Loc = [24,34];
112
113 // implement some custom matchers...
114 beforeEach(function () {
115 jasmine.addMatchers({
116 toBePositionedAt: function () {
117 return {
118 compare: function (actual, xy) {
119 var result = {},
120 actCoord = [actual.x, actual.y];
121
122 result.pass = (actual.x === xy[0]) && (actual.y === xy[1]);
123
124 if (result.pass) {
125 // for negation with ".not"
126 result.message = 'Expected [' + actCoord +
127 '] NOT to be positioned at [' + xy + ']';
128 } else {
129 result.message = 'Expected [' + actCoord +
130 '] to be positioned at [' + xy + ']';
131 }
132 return result;
133 }
134 }
135 },
136 toHaveEndPoints: function () {
137 return {
138 compare: function (actual, xy1, xy2) {
139 var result = {};
140
141 result.pass = (actual.x1 === xy1[0]) && (actual.y1 === xy1[1]) &&
142 (actual.x2 === xy2[0]) && (actual.y2 === xy2[1]);
143
144 if (result.pass) {
145 // for negation with ".not"
146 result.message = 'Expected ' + actual +
147 ' NOT to have endpoints [' + xy1 + ']-[' + xy2 + ']';
148 } else {
149 result.message = 'Expected ' + actual +
150 ' to have endpoints [' + xy1 + ']-[' + xy2 + ']';
151 }
152 return result;
153 }
154 }
155 },
156 toBeFixed: function () {
157 return {
158 compare: function (actual) {
159 var result = {
160 pass: actual.fixed
161 };
162 if (result.pass) {
163 result.message = 'Expected ' + actual +
164 ' NOT to be fixed!';
165 } else {
166 result.message = 'Expected ' + actual +
167 ' to be fixed!';
168 }
169 return result;
170 }
171 }
172 }
173 });
174 });
175
176 beforeEach(module('ovTopo', 'onosUtil'));
177
178 beforeEach(function () {
179 module(function ($provide) {
180 $provide.value('RandomService', mockRandom);
181 });
182 });
183
184 beforeEach(inject(function (_$log_, FnService, RandomService, TopoModelService) {
185 $log = _$log_;
186 fs = FnService;
187 rnd = RandomService;
188 tms = TopoModelService;
189 tms.initModel(api, dim);
190 }));
191
192
193 it('should install the mock random service', function () {
194 expect(rnd.mock).toBe('yup');
195 expect(rnd.spread(4)).toBe(5);
196 expect(rnd.randDim(8)).toBe(3);
197 });
198
199 it('should install the mock projection', function () {
200 expect(tms.coordFromLngLat({lng: 2005, lat: 3004})).toEqual([5,4]);
201 expect(tms.lngLatFromCoord([5,4])).toEqual([2005,3004]);
202 });
203
204 it('should define TopoModelService', function () {
205 expect(tms).toBeDefined();
206 });
207
208 it('should define api functions', function () {
209 expect(fs.areFunctions(tms, [
210 'initModel', 'newDim',
211 'positionNode', 'createDeviceNode', 'createHostNode',
212 'createHostLink', 'createLink',
Simon Huntdc6adea2015-02-09 22:29:36 -0800213 'coordFromLngLat', 'lngLatFromCoord',
214 'findLink', 'findLinkById', 'findDevices',
215 'findAttachedHosts', 'findAttachedLinks'
Simon Hunt3a6eec02015-02-09 21:16:43 -0800216 ])).toBeTruthy();
217 });
218
219 // === unit tests for positionNode()
220
221 it('should position a node using meta x/y', function () {
222 var node = {
223 metaUi: { x:37, y:48 }
224 };
225 tms.positionNode(node);
226 expect(node).toBePositionedAt([37,48]);
227 expect(node).toBeFixed();
228 });
229
230 it('should position a node by translating lng/lat', function () {
231 var node = {
232 location: {
233 type: 'latlng',
234 lng: 2008,
235 lat: 3009
236 }
237 };
238 tms.positionNode(node);
239 expect(node).toBePositionedAt([8,9]);
240 expect(node).toBeFixed();
241 });
242
243 it('should position a device with no location randomly', function () {
244 var node = { 'class': 'device' };
245 tms.positionNode(node);
246 expect(node).toBePositionedAt(randLoc);
247 expect(node).not.toBeFixed();
248 });
249
250 it('should position a device randomly even if x/y set', function () {
251 var node = { 'class': 'device', x: 1, y: 2 };
252 tms.positionNode(node);
253 expect(node).toBePositionedAt(randLoc);
254 expect(node).not.toBeFixed();
255 });
256
257 it('should NOT reposition a device randomly on update', function () {
258 var node = { 'class': 'device', x: 1, y: 2 };
259 tms.positionNode(node, true);
260 expect(node).toBePositionedAt([1,2]);
261 expect(node).not.toBeFixed();
262 });
263
264 it('should position a host close to its device', function () {
265 var node = { 'class': 'host', cp: { device: 'dev1' } };
266 tms.positionNode(node);
267
268 // note: nearDist is 15; spread(15) adds 16; dev1 at [17,27]
269
270 expect(node).toBePositionedAt(nearDev1);
271 expect(node).not.toBeFixed();
272 });
273
274 it('should randomize host with no assoc device', function () {
275 var node = { 'class': 'host', cp: { device: 'dev0' } };
276 tms.positionNode(node);
277
278 // note: no device gives 'rand loc' [9,19]
279 // nearDist is 15; spread(15) adds 16
280
281 expect(node).toBePositionedAt(randHostLoc);
282 expect(node).not.toBeFixed();
283 });
284
285 // === unit tests for createDeviceNode()
286
287 it('should create a basic device node', function () {
288 var node = tms.createDeviceNode({ id: 'foo' });
289 expect(node).toBePositionedAt(randLoc);
290 expect(node).not.toBeFixed();
291 expect(node.class).toEqual('device');
292 expect(node.svgClass).toEqual('node device');
293 expect(node.id).toEqual('foo');
294 });
295
296 it('should create device node with type', function () {
297 var node = tms.createDeviceNode({ id: 'foo', type: 'cool' });
298 expect(node).toBePositionedAt(randLoc);
299 expect(node).not.toBeFixed();
300 expect(node.class).toEqual('device');
301 expect(node.svgClass).toEqual('node device cool');
302 expect(node.id).toEqual('foo');
303 });
304
305 it('should create online device node with type', function () {
306 var node = tms.createDeviceNode({ id: 'foo', type: 'cool', online: true });
307 expect(node).toBePositionedAt(randLoc);
308 expect(node).not.toBeFixed();
309 expect(node.class).toEqual('device');
310 expect(node.svgClass).toEqual('node device cool online');
311 expect(node.id).toEqual('foo');
312 });
313
314 it('should create online device node with type and lng/lat', function () {
315 var node = tms.createDeviceNode({
316 id: 'foo',
317 type: 'yowser',
318 online: true,
319 location: {
320 type: 'latlng',
321 lng: 2048,
322 lat: 3096
323 }
324 });
325 expect(node).toBePositionedAt([48,96]);
326 expect(node).toBeFixed();
327 expect(node.class).toEqual('device');
328 expect(node.svgClass).toEqual('node device yowser online');
329 expect(node.id).toEqual('foo');
330 });
331
332 // === unit tests for createHostNode()
333
334 it('should create a basic host node', function () {
335 var node = tms.createHostNode({ id: 'bar', cp: { device: 'dev0' } });
336 expect(node).toBePositionedAt(randHostLoc);
337 expect(node).not.toBeFixed();
338 expect(node.class).toEqual('host');
339 expect(node.svgClass).toEqual('node host endstation');
340 expect(node.id).toEqual('bar');
341 });
342
343 it('should create a host with type', function () {
344 var node = tms.createHostNode({
345 id: 'bar',
346 type: 'classic',
347 cp: { device: 'dev1' }
348 });
349 expect(node).toBePositionedAt(nearDev1);
350 expect(node).not.toBeFixed();
351 expect(node.class).toEqual('host');
352 expect(node.svgClass).toEqual('node host classic');
353 expect(node.id).toEqual('bar');
354 });
355
356 // === unit tests for createHostLink()
357
358 it('should create a basic host link', function () {
Simon Huntdc6adea2015-02-09 22:29:36 -0800359 var link = tms.createHostLink(host1);
360 expect(link.source).toEqual(host1);
361 expect(link.target).toEqual(dev1);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800362 expect(link).toHaveEndPoints(host1Loc, dev1Loc);
363 expect(link.key).toEqual('dev1/7-host1');
364 expect(link.class).toEqual('link');
365 expect(link.type()).toEqual('hostLink');
366 expect(link.linkWidth()).toEqual(1);
367 expect(link.online()).toEqual(true);
368 });
369
370 it('should return null for failed endpoint lookup', function () {
371 spyOn($log, 'error');
Simon Huntdc6adea2015-02-09 22:29:36 -0800372 var link = tms.createHostLink(host2);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800373 expect(link).toBeNull();
374 expect($log.error).toHaveBeenCalledWith(
375 'Node(s) not on map for link:\n[dst] "dev0" missing'
376 );
377 });
378
379 // === unit tests for createLink()
380
381 it('should return null for missing endpoints', function () {
382 spyOn($log, 'error');
383 var link = tms.createLink({src: 'dev0', dst: 'dev00'});
384 expect(link).toBeNull();
385 expect($log.error).toHaveBeenCalledWith(
386 'Node(s) not on map for link:\n[src] "dev0" missing\n[dst] "dev00" missing'
387 );
388 });
389
390 it('should create a basic link', function () {
391 var linkData = {
392 src: 'dev1',
393 dst: 'dev2',
394 id: 'baz',
395 type: 'zoo',
396 online: true,
397 linkWidth: 1.5
398 },
399 link = tms.createLink(linkData);
Simon Huntdc6adea2015-02-09 22:29:36 -0800400 expect(link.source).toEqual(dev1);
401 expect(link.target).toEqual(dev2);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800402 expect(link).toHaveEndPoints(dev1Loc, dev2Loc);
403 expect(link.key).toEqual('baz');
404 expect(link.class).toEqual('link');
405 expect(link.fromSource).toBe(linkData);
406 expect(link.type()).toEqual('zoo');
407 expect(link.online()).toEqual(true);
408 expect(link.linkWidth()).toEqual(1.5);
409 });
410
Simon Huntdc6adea2015-02-09 22:29:36 -0800411 // TODO: more unit tests for additional functions....
Simon Hunt3a6eec02015-02-09 21:16:43 -0800412});