blob: 26504c3580497b1cc687ec71210634b479de1173 [file] [log] [blame]
Simon Hunt743a8492015-08-25 16:18:19 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Hunt743a8492015-08-25 16:18:19 -07003 *
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.
Simon Hunt743a8492015-08-25 16:18:19 -070015 */
16
Simon Hunt52560662015-08-27 22:46:44 -070017package org.onosproject.ui.topo;
Simon Hunt743a8492015-08-25 16:18:19 -070018
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.junit.Test;
22import org.onosproject.ui.JsonUtils;
Simon Hunt743a8492015-08-25 16:18:19 -070023import org.onosproject.ui.topo.Highlights.Amount;
Simon Hunte9343f32015-10-21 18:07:46 -070024import org.onosproject.ui.topo.NodeBadge.Status;
Simon Hunt743a8492015-08-25 16:18:19 -070025
26import static org.junit.Assert.assertEquals;
Simon Hunt85897572015-10-20 16:29:51 -070027import static org.junit.Assert.assertNotNull;
Simon Hunte9343f32015-10-21 18:07:46 -070028import static org.junit.Assert.assertNull;
Simon Hunt743a8492015-08-25 16:18:19 -070029
30/**
31 * Unit tests for {@link TopoJson}.
32 */
33public class TopoJsonTest {
34
Simon Hunt85897572015-10-20 16:29:51 -070035 private static final String DEV1 = "device-1";
36 private static final String DEV2 = "device-2";
Simon Hunte9343f32015-10-21 18:07:46 -070037 private static final String SOME_MSG = "Hello there";
38 private static final String GID = "glyph-ID";
Simon Hunt85897572015-10-20 16:29:51 -070039
Simon Hunt743a8492015-08-25 16:18:19 -070040 private ObjectNode payload;
41
42 private void checkArrayLength(String key, int expLen) {
43 ArrayNode a = (ArrayNode) payload.get(key);
44 assertEquals("wrong size: " + key, expLen, a.size());
45 }
46
47 private void checkEmptyArrays() {
48 checkArrayLength(TopoJson.DEVICES, 0);
49 checkArrayLength(TopoJson.HOSTS, 0);
50 checkArrayLength(TopoJson.LINKS, 0);
51 }
52
53 @Test
54 public void basicHighlights() {
55 Highlights h = new Highlights();
56 payload = TopoJson.json(h);
57 checkEmptyArrays();
58 String subdue = JsonUtils.string(payload, TopoJson.SUBDUE);
59 assertEquals("subdue", "", subdue);
60 }
61
62 @Test
63 public void subdueMinimalHighlights() {
64 Highlights h = new Highlights().subdueAllElse(Amount.MINIMALLY);
65 payload = TopoJson.json(h);
66 checkEmptyArrays();
67 String subdue = JsonUtils.string(payload, TopoJson.SUBDUE);
68 assertEquals("not min", "min", subdue);
69 }
70
71 @Test
72 public void subdueMaximalHighlights() {
73 Highlights h = new Highlights().subdueAllElse(Amount.MAXIMALLY);
74 payload = TopoJson.json(h);
75 checkEmptyArrays();
76 String subdue = JsonUtils.string(payload, TopoJson.SUBDUE);
77 assertEquals("not max", "max", subdue);
78 }
Simon Hunt85897572015-10-20 16:29:51 -070079
80 @Test
81 public void badgedDevice() {
82 Highlights h = new Highlights();
83 DeviceHighlight dh = new DeviceHighlight(DEV1);
Simon Hunt85897572015-10-20 16:29:51 -070084 dh.setBadge(NodeBadge.number(7));
85 h.add(dh);
86
Simon Hunte9343f32015-10-21 18:07:46 -070087 dh = new DeviceHighlight(DEV2);
88 dh.setBadge(NodeBadge.glyph(Status.WARN, GID, SOME_MSG));
89 h.add(dh);
90
Simon Hunt85897572015-10-20 16:29:51 -070091 payload = TopoJson.json(h);
Simon Hunte9343f32015-10-21 18:07:46 -070092// System.out.println(payload);
Simon Hunt85897572015-10-20 16:29:51 -070093
94 // dig into the payload, and verify the badges are set on the devices
95 ArrayNode a = (ArrayNode) payload.get(TopoJson.DEVICES);
96
97 ObjectNode d = (ObjectNode) a.get(0);
98 assertEquals("wrong device id", DEV1, d.get(TopoJson.ID).asText());
99
100 ObjectNode b = (ObjectNode) d.get(TopoJson.BADGE);
101 assertNotNull("missing badge", b);
Simon Hunte9343f32015-10-21 18:07:46 -0700102 assertEquals("wrong status code", "i", b.get(TopoJson.STATUS).asText());
103 assertEquals("wrong text", "7", b.get(TopoJson.TXT).asText());
104 assertNull("glyph?", b.get(TopoJson.GID));
105 assertNull("msg?", b.get(TopoJson.MSG));
Simon Hunt85897572015-10-20 16:29:51 -0700106
107 d = (ObjectNode) a.get(1);
108 assertEquals("wrong device id", DEV2, d.get(TopoJson.ID).asText());
109
110 b = (ObjectNode) d.get(TopoJson.BADGE);
111 assertNotNull("missing badge", b);
Simon Hunte9343f32015-10-21 18:07:46 -0700112 assertEquals("wrong status code", "w", b.get(TopoJson.STATUS).asText());
113 assertNull("text?", b.get(TopoJson.TXT));
114 assertEquals("wrong text", GID, b.get(TopoJson.GID).asText());
115 assertEquals("wrong message", SOME_MSG, b.get(TopoJson.MSG).asText());
Simon Hunt85897572015-10-20 16:29:51 -0700116 }
Simon Hunt743a8492015-08-25 16:18:19 -0700117}