Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present 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 | package org.onosproject.ui.impl.topo; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import org.junit.Test; |
| 22 | import org.onosproject.ui.impl.AbstractUiImplTest; |
| 23 | import org.onosproject.ui.model.topo.UiNode; |
| 24 | |
| 25 | import java.util.List; |
| 26 | import java.util.Set; |
| 27 | |
| 28 | import static org.junit.Assert.assertEquals; |
| 29 | import static org.onosproject.ui.model.topo.UiNode.LAYER_DEFAULT; |
| 30 | import static org.onosproject.ui.model.topo.UiNode.LAYER_OPTICAL; |
| 31 | import static org.onosproject.ui.model.topo.UiNode.LAYER_PACKET; |
| 32 | |
| 33 | /** |
| 34 | * Unit tests for {@link Topo2ViewMessageHandler}. |
| 35 | */ |
| 36 | public class Topo2JsonifierTest extends AbstractUiImplTest { |
| 37 | |
| 38 | // mock node class for testing |
| 39 | private static class MockNode extends UiNode { |
| 40 | private final String id; |
| 41 | |
| 42 | MockNode(String id, String layer) { |
| 43 | this.id = id; |
| 44 | setLayer(layer); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public String idAsString() { |
| 49 | return id; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public String toString() { |
| 54 | return id; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | private static final List<String> ALL_TAGS = ImmutableList.of( |
| 59 | LAYER_OPTICAL, LAYER_PACKET, LAYER_DEFAULT |
| 60 | ); |
| 61 | |
| 62 | private static final List<String> PKT_DEF_TAGS = ImmutableList.of( |
| 63 | LAYER_PACKET, LAYER_DEFAULT |
| 64 | ); |
| 65 | |
| 66 | private static final List<String> DEF_TAG_ONLY = ImmutableList.of( |
| 67 | LAYER_DEFAULT |
| 68 | ); |
| 69 | |
| 70 | private static final MockNode NODE_A = new MockNode("A-O", LAYER_OPTICAL); |
| 71 | private static final MockNode NODE_B = new MockNode("B-P", LAYER_PACKET); |
| 72 | private static final MockNode NODE_C = new MockNode("C-O", LAYER_OPTICAL); |
| 73 | private static final MockNode NODE_D = new MockNode("D-D", LAYER_DEFAULT); |
| 74 | private static final MockNode NODE_E = new MockNode("E-P", LAYER_PACKET); |
| 75 | private static final MockNode NODE_F = new MockNode("F-r", "random"); |
| 76 | |
| 77 | private static final Set<MockNode> NODES = ImmutableSet.of( |
| 78 | NODE_A, NODE_B, NODE_C, NODE_D, NODE_E, NODE_F |
| 79 | ); |
| 80 | |
| 81 | private Topo2Jsonifier t2 = new Topo2Jsonifier(); |
| 82 | |
| 83 | @Test |
| 84 | public void threeLayers() { |
| 85 | print("threeLayers()"); |
| 86 | |
| 87 | List<Set<UiNode>> result = t2.splitByLayer(ALL_TAGS, NODES); |
| 88 | print(result); |
| 89 | |
| 90 | assertEquals("wrong split size", 3, result.size()); |
| 91 | Set<UiNode> opt = result.get(0); |
| 92 | Set<UiNode> pkt = result.get(1); |
| 93 | Set<UiNode> def = result.get(2); |
| 94 | |
| 95 | assertEquals("opt bad size", 2, opt.size()); |
| 96 | assertEquals("missing node A", true, opt.contains(NODE_A)); |
| 97 | assertEquals("missing node C", true, opt.contains(NODE_C)); |
| 98 | |
| 99 | assertEquals("pkt bad size", 2, pkt.size()); |
| 100 | assertEquals("missing node B", true, pkt.contains(NODE_B)); |
| 101 | assertEquals("missing node E", true, pkt.contains(NODE_E)); |
| 102 | |
| 103 | assertEquals("def bad size", 2, def.size()); |
| 104 | assertEquals("missing node D", true, def.contains(NODE_D)); |
| 105 | assertEquals("missing node F", true, def.contains(NODE_F)); |
| 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void twoLayers() { |
| 110 | print("twoLayers()"); |
| 111 | |
| 112 | List<Set<UiNode>> result = t2.splitByLayer(PKT_DEF_TAGS, NODES); |
| 113 | print(result); |
| 114 | |
| 115 | assertEquals("wrong split size", 2, result.size()); |
| 116 | Set<UiNode> pkt = result.get(0); |
| 117 | Set<UiNode> def = result.get(1); |
| 118 | |
| 119 | assertEquals("pkt bad size", 2, pkt.size()); |
| 120 | assertEquals("missing node B", true, pkt.contains(NODE_B)); |
| 121 | assertEquals("missing node E", true, pkt.contains(NODE_E)); |
| 122 | |
| 123 | assertEquals("def bad size", 4, def.size()); |
| 124 | assertEquals("missing node D", true, def.contains(NODE_D)); |
| 125 | assertEquals("missing node F", true, def.contains(NODE_F)); |
| 126 | assertEquals("missing node A", true, def.contains(NODE_A)); |
| 127 | assertEquals("missing node C", true, def.contains(NODE_C)); |
| 128 | } |
| 129 | |
| 130 | @Test |
| 131 | public void oneLayer() { |
| 132 | print("oneLayer()"); |
| 133 | |
| 134 | List<Set<UiNode>> result = t2.splitByLayer(DEF_TAG_ONLY, NODES); |
| 135 | print(result); |
| 136 | |
| 137 | assertEquals("wrong split size", 1, result.size()); |
| 138 | Set<UiNode> def = result.get(0); |
| 139 | |
| 140 | assertEquals("def bad size", 6, def.size()); |
| 141 | assertEquals("missing node D", true, def.contains(NODE_D)); |
| 142 | assertEquals("missing node F", true, def.contains(NODE_F)); |
| 143 | assertEquals("missing node A", true, def.contains(NODE_A)); |
| 144 | assertEquals("missing node C", true, def.contains(NODE_C)); |
| 145 | assertEquals("missing node B", true, def.contains(NODE_B)); |
| 146 | assertEquals("missing node E", true, def.contains(NODE_E)); |
| 147 | } |
| 148 | } |