Simon Hunt | 0c3a7bb | 2017-10-05 17:00:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.segmentrouting.config; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.core.DefaultApplicationId; |
| 25 | |
| 26 | import java.io.IOException; |
| 27 | import java.io.InputStream; |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.Iterator; |
| 30 | import java.util.List; |
| 31 | import java.util.NoSuchElementException; |
| 32 | |
| 33 | import static org.junit.Assert.assertEquals; |
| 34 | import static org.junit.Assert.fail; |
| 35 | |
| 36 | /** |
| 37 | * Unit tests for {@link BlockedPortsConfig}. |
| 38 | */ |
| 39 | public class BlockedPortsConfigTest { |
| 40 | |
| 41 | private static final ApplicationId APP_ID = new DefaultApplicationId(1, "foo"); |
| 42 | private static final String KEY = "blocked"; |
| 43 | private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 44 | |
| 45 | private static final String DEV1 = "of:0000000000000001"; |
| 46 | private static final String DEV2 = "of:0000000000000002"; |
| 47 | private static final String DEV3 = "of:0000000000000003"; |
| 48 | private static final String DEV4 = "of:0000000000000004"; |
| 49 | private static final String RANGE_14 = "1-4"; |
| 50 | private static final String RANGE_79 = "7-9"; |
| 51 | private static final String P1 = "1"; |
| 52 | private static final String P5 = "5"; |
| 53 | private static final String P9 = "9"; |
| 54 | |
| 55 | private BlockedPortsConfig cfg; |
| 56 | private BlockedPortsConfig.Range range; |
| 57 | |
| 58 | private void print(String s) { |
| 59 | System.out.println(s); |
| 60 | } |
| 61 | |
| 62 | private void print(Object o) { |
| 63 | print(o.toString()); |
| 64 | } |
| 65 | |
| 66 | @Before |
| 67 | public void setUp() throws IOException { |
| 68 | InputStream blockedPortsJson = BlockedPortsConfigTest.class |
| 69 | .getResourceAsStream("/blocked-ports.json"); |
| 70 | JsonNode node = MAPPER.readTree(blockedPortsJson); |
| 71 | cfg = new BlockedPortsConfig(); |
| 72 | cfg.init(APP_ID, KEY, node, MAPPER, null); |
| 73 | } |
| 74 | |
| 75 | @Test |
| 76 | public void basic() { |
| 77 | cfg = new BlockedPortsConfig(); |
| 78 | print(cfg); |
| 79 | |
| 80 | assertEquals("non-empty devices list", 0, cfg.deviceIds().size()); |
| 81 | assertEquals("non-empty port-ranges list", 0, cfg.portRanges("non-exist").size()); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | @Test |
| 86 | public void overIteratePort() { |
| 87 | Iterator<Long> iterator = cfg.portIterator(DEV3); |
| 88 | while (iterator.hasNext()) { |
| 89 | print(iterator.next()); |
| 90 | } |
| 91 | |
| 92 | try { |
| 93 | print(iterator.next()); |
| 94 | fail("NoSuchElement exception NOT thrown"); |
| 95 | } catch (NoSuchElementException e) { |
| 96 | print("<good> " + e); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void overIterateRange() { |
| 102 | range = new BlockedPortsConfig.Range("4-6"); |
| 103 | |
| 104 | Iterator<Long> iterator = range.iterator(); |
| 105 | while (iterator.hasNext()) { |
| 106 | print(iterator.next()); |
| 107 | } |
| 108 | |
| 109 | try { |
| 110 | print(iterator.next()); |
| 111 | fail("NoSuchElement exception NOT thrown"); |
| 112 | } catch (NoSuchElementException e) { |
| 113 | print("<good> " + e); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | |
| 118 | @Test |
| 119 | public void simple() { |
| 120 | List<String> devIds = cfg.deviceIds(); |
| 121 | print(devIds); |
| 122 | assertEquals("wrong dev id count", 3, devIds.size()); |
| 123 | assertEquals("missing dev 1", true, devIds.contains(DEV1)); |
| 124 | assertEquals("dev 2??", false, devIds.contains(DEV2)); |
| 125 | assertEquals("missing dev 3", true, devIds.contains(DEV3)); |
| 126 | |
| 127 | List<String> d1ranges = cfg.portRanges(DEV1); |
| 128 | print(d1ranges); |
| 129 | assertEquals("wrong d1 range count", 2, d1ranges.size()); |
| 130 | assertEquals("missing 1-4", true, d1ranges.contains(RANGE_14)); |
| 131 | assertEquals("missing 7-9", true, d1ranges.contains(RANGE_79)); |
| 132 | |
| 133 | List<String> d2ranges = cfg.portRanges(DEV2); |
| 134 | print(d2ranges); |
| 135 | assertEquals("wrong d2 range count", 0, d2ranges.size()); |
| 136 | |
| 137 | List<String> d3ranges = cfg.portRanges(DEV3); |
| 138 | print(d3ranges); |
| 139 | assertEquals("wrong d3 range count", 1, d3ranges.size()); |
| 140 | assertEquals("range 1-4?", false, d3ranges.contains(RANGE_14)); |
| 141 | assertEquals("missing 7-9", true, d3ranges.contains(RANGE_79)); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | private void verifyPorts(List<Long> ports, long... exp) { |
| 146 | assertEquals("Wrong port count", exp.length, ports.size()); |
| 147 | for (long e : exp) { |
| 148 | assertEquals("missing port", true, ports.contains(e)); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | private void verifyPortIterator(String devid, long... exp) { |
| 153 | List<Long> ports = new ArrayList<>(); |
| 154 | Iterator<Long> iter = cfg.portIterator(devid); |
| 155 | iter.forEachRemaining(ports::add); |
| 156 | print(ports); |
| 157 | verifyPorts(ports, exp); |
| 158 | } |
| 159 | |
| 160 | @Test |
| 161 | public void rangeIterators() { |
| 162 | verifyPortIterator(DEV1, 1, 2, 3, 4, 7, 8, 9); |
| 163 | verifyPortIterator(DEV2); |
| 164 | verifyPortIterator(DEV3, 7, 8, 9); |
| 165 | } |
| 166 | |
| 167 | @Test |
| 168 | public void singlePorts() { |
| 169 | List<String> devIds = cfg.deviceIds(); |
| 170 | print(devIds); |
| 171 | assertEquals("wrong dev id count", 3, devIds.size()); |
| 172 | assertEquals("missing dev 4", true, devIds.contains(DEV4)); |
| 173 | |
| 174 | List<String> d1ranges = cfg.portRanges(DEV4); |
| 175 | print(d1ranges); |
| 176 | assertEquals("wrong d4 range count", 3, d1ranges.size()); |
| 177 | assertEquals("missing 1", true, d1ranges.contains(P1)); |
| 178 | assertEquals("missing 5", true, d1ranges.contains(P5)); |
| 179 | assertEquals("missing 9", true, d1ranges.contains(P9)); |
| 180 | |
| 181 | verifyPortIterator(DEV4, 1, 5, 9); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | // test Range inner class |
| 186 | |
| 187 | @Test |
| 188 | public void rangeBadFormat() { |
| 189 | try { |
| 190 | range = new BlockedPortsConfig.Range("not-a-range-format"); |
| 191 | fail("no exception thrown"); |
| 192 | } catch (IllegalArgumentException iar) { |
| 193 | print(iar); |
| 194 | assertEquals("wrong msg", "Bad Range Format not-a-range-format", iar.getMessage()); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | @Test |
| 199 | public void rangeBadHi() { |
| 200 | try { |
| 201 | range = new BlockedPortsConfig.Range("2-nine"); |
| 202 | fail("no exception thrown"); |
| 203 | } catch (IllegalArgumentException iar) { |
| 204 | print(iar); |
| 205 | assertEquals("wrong msg", "Bad Range Format 2-nine", iar.getMessage()); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | @Test |
| 210 | public void rangeHiLessThanLo() { |
| 211 | try { |
| 212 | range = new BlockedPortsConfig.Range("9-5"); |
| 213 | fail("no exception thrown"); |
| 214 | } catch (IllegalArgumentException iar) { |
| 215 | print(iar); |
| 216 | assertEquals("wrong msg", "Bad Range Format 9-5", iar.getMessage()); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | @Test |
| 221 | public void rangeNegative() { |
| 222 | try { |
| 223 | range = new BlockedPortsConfig.Range("-2-4"); |
| 224 | fail("no exception thrown"); |
| 225 | } catch (IllegalArgumentException iar) { |
| 226 | print(iar); |
| 227 | assertEquals("wrong msg", "Bad Range Format -2-4", iar.getMessage()); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | @Test |
| 232 | public void rangeGood() { |
| 233 | range = new BlockedPortsConfig.Range("100-104"); |
| 234 | List<Long> values = new ArrayList<>(); |
| 235 | range.iterator().forEachRemaining(values::add); |
| 236 | print(values); |
| 237 | verifyPorts(values, 100, 101, 102, 103, 104); |
| 238 | } |
| 239 | } |