Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 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 Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Simon Hunt | 5256066 | 2015-08-27 22:46:44 -0700 | [diff] [blame] | 17 | package org.onosproject.ui.topo; |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 18 | |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.junit.Test; |
| 22 | import org.onosproject.ui.JsonUtils; |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 23 | import org.onosproject.ui.topo.Highlights.Amount; |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 24 | import org.onosproject.ui.topo.NodeBadge.Status; |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 25 | |
| 26 | import static org.junit.Assert.assertEquals; |
Simon Hunt | 8589757 | 2015-10-20 16:29:51 -0700 | [diff] [blame] | 27 | import static org.junit.Assert.assertNotNull; |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 28 | import static org.junit.Assert.assertNull; |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Unit tests for {@link TopoJson}. |
| 32 | */ |
| 33 | public class TopoJsonTest { |
| 34 | |
Simon Hunt | 8589757 | 2015-10-20 16:29:51 -0700 | [diff] [blame] | 35 | private static final String DEV1 = "device-1"; |
| 36 | private static final String DEV2 = "device-2"; |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 37 | private static final String SOME_MSG = "Hello there"; |
| 38 | private static final String GID = "glyph-ID"; |
Simon Hunt | 8589757 | 2015-10-20 16:29:51 -0700 | [diff] [blame] | 39 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 40 | 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 Hunt | 8589757 | 2015-10-20 16:29:51 -0700 | [diff] [blame] | 79 | |
| 80 | @Test |
| 81 | public void badgedDevice() { |
| 82 | Highlights h = new Highlights(); |
| 83 | DeviceHighlight dh = new DeviceHighlight(DEV1); |
Simon Hunt | 8589757 | 2015-10-20 16:29:51 -0700 | [diff] [blame] | 84 | dh.setBadge(NodeBadge.number(7)); |
| 85 | h.add(dh); |
| 86 | |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 87 | dh = new DeviceHighlight(DEV2); |
| 88 | dh.setBadge(NodeBadge.glyph(Status.WARN, GID, SOME_MSG)); |
| 89 | h.add(dh); |
| 90 | |
Simon Hunt | 8589757 | 2015-10-20 16:29:51 -0700 | [diff] [blame] | 91 | payload = TopoJson.json(h); |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 92 | // System.out.println(payload); |
Simon Hunt | 8589757 | 2015-10-20 16:29:51 -0700 | [diff] [blame] | 93 | |
| 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 Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 102 | 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 Hunt | 8589757 | 2015-10-20 16:29:51 -0700 | [diff] [blame] | 106 | |
| 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 Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 112 | 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 Hunt | 8589757 | 2015-10-20 16:29:51 -0700 | [diff] [blame] | 116 | } |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 117 | } |