blob: 3b572a970333c360c7d0fc9c55314b39f03639c8 [file] [log] [blame]
Simon Hunt743a8492015-08-25 16:18:19 -07001/*
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.
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;
24
25import static org.junit.Assert.assertEquals;
Simon Hunt85897572015-10-20 16:29:51 -070026import static org.junit.Assert.assertNotNull;
Simon Hunt743a8492015-08-25 16:18:19 -070027
28/**
29 * Unit tests for {@link TopoJson}.
30 */
31public class TopoJsonTest {
32
Simon Hunt85897572015-10-20 16:29:51 -070033 private static final String DEV1 = "device-1";
34 private static final String DEV2 = "device-2";
35 private static final String BADGE_MSG = "Hello there";
36
Simon Hunt743a8492015-08-25 16:18:19 -070037 private ObjectNode payload;
38
39 private void checkArrayLength(String key, int expLen) {
40 ArrayNode a = (ArrayNode) payload.get(key);
41 assertEquals("wrong size: " + key, expLen, a.size());
42 }
43
44 private void checkEmptyArrays() {
45 checkArrayLength(TopoJson.DEVICES, 0);
46 checkArrayLength(TopoJson.HOSTS, 0);
47 checkArrayLength(TopoJson.LINKS, 0);
48 }
49
50 @Test
51 public void basicHighlights() {
52 Highlights h = new Highlights();
53 payload = TopoJson.json(h);
54 checkEmptyArrays();
55 String subdue = JsonUtils.string(payload, TopoJson.SUBDUE);
56 assertEquals("subdue", "", subdue);
57 }
58
59 @Test
60 public void subdueMinimalHighlights() {
61 Highlights h = new Highlights().subdueAllElse(Amount.MINIMALLY);
62 payload = TopoJson.json(h);
63 checkEmptyArrays();
64 String subdue = JsonUtils.string(payload, TopoJson.SUBDUE);
65 assertEquals("not min", "min", subdue);
66 }
67
68 @Test
69 public void subdueMaximalHighlights() {
70 Highlights h = new Highlights().subdueAllElse(Amount.MAXIMALLY);
71 payload = TopoJson.json(h);
72 checkEmptyArrays();
73 String subdue = JsonUtils.string(payload, TopoJson.SUBDUE);
74 assertEquals("not max", "max", subdue);
75 }
Simon Hunt85897572015-10-20 16:29:51 -070076
77 @Test
78 public void badgedDevice() {
79 Highlights h = new Highlights();
80 DeviceHighlight dh = new DeviceHighlight(DEV1);
81 dh.setBadge(NodeBadge.info(BADGE_MSG));
82 h.add(dh);
83
84 dh = new DeviceHighlight(DEV2);
85 dh.setBadge(NodeBadge.number(7));
86 h.add(dh);
87
88 payload = TopoJson.json(h);
89 System.out.println(payload);
90
91 // dig into the payload, and verify the badges are set on the devices
92 ArrayNode a = (ArrayNode) payload.get(TopoJson.DEVICES);
93
94 ObjectNode d = (ObjectNode) a.get(0);
95 assertEquals("wrong device id", DEV1, d.get(TopoJson.ID).asText());
96
97 ObjectNode b = (ObjectNode) d.get(TopoJson.BADGE);
98 assertNotNull("missing badge", b);
99 assertEquals("wrong type code", "i", b.get(TopoJson.TYPE).asText());
100 assertEquals("wrong message", BADGE_MSG, b.get(TopoJson.MSG).asText());
101
102 d = (ObjectNode) a.get(1);
103 assertEquals("wrong device id", DEV2, d.get(TopoJson.ID).asText());
104
105 b = (ObjectNode) d.get(TopoJson.BADGE);
106 assertNotNull("missing badge", b);
107 assertEquals("wrong type code", "n", b.get(TopoJson.TYPE).asText());
108 assertEquals("wrong message", "7", b.get(TopoJson.MSG).asText());
109 }
Simon Hunt743a8492015-08-25 16:18:19 -0700110}