Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 1 | /* |
| 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 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; |
| 24 | |
| 25 | import static org.junit.Assert.assertEquals; |
Simon Hunt | 8589757 | 2015-10-20 16:29:51 -0700 | [diff] [blame^] | 26 | import static org.junit.Assert.assertNotNull; |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Unit tests for {@link TopoJson}. |
| 30 | */ |
| 31 | public class TopoJsonTest { |
| 32 | |
Simon Hunt | 8589757 | 2015-10-20 16:29:51 -0700 | [diff] [blame^] | 33 | 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 Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 37 | 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 Hunt | 8589757 | 2015-10-20 16:29:51 -0700 | [diff] [blame^] | 76 | |
| 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 Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 110 | } |