Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [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. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.net.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 | |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 24 | import static junit.framework.TestCase.assertFalse; |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 25 | import static org.junit.Assert.assertTrue; |
| 26 | import static org.onosproject.net.config.Config.FieldPresence.MANDATORY; |
| 27 | import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL; |
| 28 | |
| 29 | /** |
| 30 | * Test of the base network config class. |
| 31 | */ |
| 32 | public class ConfigTest { |
| 33 | |
| 34 | private static final String SUBJECT = "subject"; |
| 35 | private static final String KEY = "key"; |
| 36 | |
| 37 | private static final String TEXT = "text"; |
| 38 | private static final String LONG = "long"; |
| 39 | private static final String DOUBLE = "double"; |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 40 | private static final String BOOLEAN = "boolean"; |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 41 | private static final String MAC = "mac"; |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 42 | private static final String BAD_MAC = "badMac"; |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 43 | private static final String IP = "ip"; |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 44 | private static final String BAD_IP = "badIp"; |
| 45 | private static final String PREFIX = "prefix"; |
| 46 | private static final String BAD_PREFIX = "badPrefix"; |
| 47 | private static final String CONNECT_POINT = "connectPoint"; |
| 48 | private static final String BAD_CONNECT_POINT = "badConnectPoint"; |
Hyunsun Moon | 61b73e9 | 2016-05-10 18:05:57 -0700 | [diff] [blame] | 49 | private static final String TP_PORT = "tpPort"; |
| 50 | private static final String BAD_TP_PORT = "badTpPort"; |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 51 | |
| 52 | private final ObjectMapper mapper = new ObjectMapper(); |
| 53 | private final ConfigApplyDelegate delegate = new TestDelegate(); |
| 54 | |
| 55 | private Config<String> cfg; |
| 56 | private JsonNode json; |
| 57 | |
| 58 | @Before |
| 59 | public void setUp() { |
| 60 | json = new ObjectMapper().createObjectNode() |
| 61 | .put(TEXT, "foo").put(LONG, 5).put(DOUBLE, 0.5) |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 62 | .put(BOOLEAN, "true") |
| 63 | .put(MAC, "ab:cd:ef:ca:fe:ed").put(BAD_MAC, "ab:cd:ef:ca:fe.ed") |
| 64 | .put(IP, "12.34.56.78").put(BAD_IP, "12.34-56.78") |
| 65 | .put(PREFIX, "12.34.56.78/18").put(BAD_PREFIX, "12.34.56.78-18") |
| 66 | .put(CONNECT_POINT, "of:0000000000000001/1") |
| 67 | .put(BAD_CONNECT_POINT, "of:0000000000000001-1") |
Hyunsun Moon | 61b73e9 | 2016-05-10 18:05:57 -0700 | [diff] [blame] | 68 | .put(TP_PORT, 65535).put(BAD_TP_PORT, 65536); |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 69 | cfg = new TestConfig(); |
| 70 | cfg.init(SUBJECT, KEY, json, mapper, delegate); |
| 71 | } |
| 72 | |
| 73 | @Test |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 74 | public void hasField() { |
| 75 | assertTrue("missing field", cfg.hasField(MAC)); |
| 76 | assertFalse("unexpected field", cfg.hasField("non-existent")); |
| 77 | } |
| 78 | |
| 79 | @Test |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 80 | public void hasOnlyFields() { |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 81 | assertTrue("has unexpected fields", |
| 82 | cfg.hasOnlyFields(TEXT, LONG, DOUBLE, BOOLEAN, MAC, BAD_MAC, |
| 83 | IP, BAD_IP, PREFIX, BAD_PREFIX, |
| 84 | CONNECT_POINT, BAD_CONNECT_POINT, TP_PORT, BAD_TP_PORT)); |
| 85 | assertTrue("did not detect unexpected fields", |
| 86 | expectInvalidField(() -> cfg.hasOnlyFields(TEXT, LONG, DOUBLE, MAC))); |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Test |
Hyunsun Moon | 61b73e9 | 2016-05-10 18:05:57 -0700 | [diff] [blame] | 90 | public void hasFields() { |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 91 | assertTrue("does not have mandatory field", |
| 92 | cfg.hasFields(TEXT, LONG, DOUBLE, MAC)); |
| 93 | assertTrue("did not detect missing field", |
| 94 | expectInvalidField(() -> cfg.hasFields("none"))); |
Hyunsun Moon | 61b73e9 | 2016-05-10 18:05:57 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Test |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 98 | public void isString() { |
| 99 | assertTrue("is not proper text", cfg.isString(TEXT, MANDATORY)); |
| 100 | assertTrue("is not proper text", cfg.isString(TEXT, MANDATORY, "^f.*")); |
| 101 | assertTrue("is not proper text", cfg.isString(TEXT, OPTIONAL, "^f.*")); |
| 102 | assertTrue("is not proper text", cfg.isString(TEXT, OPTIONAL)); |
| 103 | assertTrue("is not proper text", cfg.isString("none", OPTIONAL)); |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 104 | assertTrue("did not detect missing field", |
| 105 | expectInvalidField(() -> cfg.isString("none", MANDATORY))); |
| 106 | assertTrue("did not detect bad text", |
| 107 | expectInvalidField(() -> cfg.isString(TEXT, OPTIONAL, "^b.*"))); |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Test |
| 111 | public void isNumber() { |
| 112 | assertTrue("is not proper number", cfg.isNumber(LONG, MANDATORY)); |
| 113 | assertTrue("is not proper number", cfg.isNumber(LONG, MANDATORY, 0)); |
| 114 | assertTrue("is not proper number", cfg.isNumber(LONG, MANDATORY, 0, 10)); |
| 115 | assertTrue("is not proper number", cfg.isNumber(LONG, MANDATORY, 5, 6)); |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 116 | assertTrue("is not in range", |
| 117 | expectInvalidField(() -> cfg.isNumber(LONG, MANDATORY, 6, 10))); |
| 118 | assertTrue("is not in range", cfg.isNumber(LONG, MANDATORY, 4, 5)); |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 119 | assertTrue("is not proper number", cfg.isNumber(LONG, OPTIONAL, 0, 10)); |
| 120 | assertTrue("is not proper number", cfg.isNumber(LONG, OPTIONAL)); |
| 121 | assertTrue("is not proper number", cfg.isNumber("none", OPTIONAL)); |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 122 | assertTrue("did not detect missing field", |
| 123 | expectInvalidField(() -> cfg.isNumber("none", MANDATORY))); |
| 124 | assertTrue("is not proper number", |
| 125 | expectInvalidField(() -> cfg.isNumber(TEXT, MANDATORY))); |
| 126 | |
| 127 | assertTrue("is not proper number", cfg.isNumber(DOUBLE, MANDATORY, 0, 1)); |
| 128 | assertTrue("is not in range", |
| 129 | expectInvalidField(() -> cfg.isNumber(DOUBLE, MANDATORY, 1, 2))); |
| 130 | } |
| 131 | |
| 132 | @Test |
| 133 | public void isIntegralNumber() { |
| 134 | assertTrue("is not proper number", cfg.isIntegralNumber(LONG, MANDATORY)); |
| 135 | assertTrue("is not proper number", cfg.isIntegralNumber(LONG, MANDATORY, 0)); |
| 136 | assertTrue("is not proper number", cfg.isIntegralNumber(LONG, MANDATORY, 0, 10)); |
| 137 | assertTrue("is not proper number", cfg.isIntegralNumber(LONG, MANDATORY, 5, 6)); |
| 138 | assertTrue("is not in range", |
| 139 | expectInvalidField(() -> cfg.isIntegralNumber(LONG, MANDATORY, 6, 10))); |
| 140 | assertTrue("is not in range", cfg.isIntegralNumber(LONG, MANDATORY, 4, 5)); |
| 141 | assertTrue("is not proper number", cfg.isIntegralNumber(LONG, OPTIONAL, 0, 10)); |
| 142 | assertTrue("is not proper number", cfg.isIntegralNumber(LONG, OPTIONAL)); |
| 143 | assertTrue("is not proper number", cfg.isIntegralNumber("none", OPTIONAL)); |
| 144 | assertTrue("did not detect missing field", |
| 145 | expectInvalidField(() -> cfg.isIntegralNumber("none", MANDATORY))); |
| 146 | assertTrue("is not proper number", |
| 147 | expectInvalidField(() -> cfg.isIntegralNumber(TEXT, MANDATORY))); |
| 148 | |
| 149 | assertTrue("is not in range", |
| 150 | expectInvalidField(() -> cfg.isIntegralNumber(DOUBLE, MANDATORY, 0, 10))); |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Test |
| 154 | public void isDecimal() { |
| 155 | assertTrue("is not proper decimal", cfg.isDecimal(DOUBLE, MANDATORY)); |
| 156 | assertTrue("is not proper decimal", cfg.isDecimal(DOUBLE, MANDATORY, 0.0)); |
| 157 | assertTrue("is not proper decimal", cfg.isDecimal(DOUBLE, MANDATORY, 0.0, 1.0)); |
| 158 | assertTrue("is not proper decimal", cfg.isDecimal(DOUBLE, MANDATORY, 0.5, 0.6)); |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 159 | assertTrue("is not proper decimal", cfg.isDecimal(DOUBLE, MANDATORY, 0.4, 0.5)); |
| 160 | assertTrue("is not in range", |
| 161 | expectInvalidField(() -> cfg.isDecimal(DOUBLE, MANDATORY, 0.6, 1.0))); |
| 162 | assertTrue("is not in range", |
| 163 | expectInvalidField(() -> cfg.isDecimal(DOUBLE, MANDATORY, 0.3, 0.4))); |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 164 | assertTrue("is not proper decimal", cfg.isDecimal(DOUBLE, OPTIONAL, 0.0, 1.0)); |
| 165 | assertTrue("is not proper decimal", cfg.isDecimal(DOUBLE, OPTIONAL)); |
| 166 | assertTrue("is not proper decimal", cfg.isDecimal("none", OPTIONAL)); |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 167 | assertTrue("did not detect missing field", |
| 168 | expectInvalidField(() -> cfg.isDecimal("none", MANDATORY))); |
| 169 | } |
| 170 | |
| 171 | @Test |
| 172 | public void isBoolean() { |
| 173 | assertTrue("is not proper boolean", cfg.isBoolean(BOOLEAN, MANDATORY)); |
| 174 | assertTrue("did not detect missing field", |
| 175 | expectInvalidField(() -> cfg.isBoolean("none", MANDATORY))); |
| 176 | assertTrue("is not proper boolean", cfg.isBoolean("none", OPTIONAL)); |
| 177 | assertTrue("did not detect bad boolean", |
| 178 | expectInvalidField(() -> cfg.isBoolean(TEXT, MANDATORY))); |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Test |
| 182 | public void isMacAddress() { |
| 183 | assertTrue("is not proper mac", cfg.isMacAddress(MAC, MANDATORY)); |
| 184 | assertTrue("is not proper mac", cfg.isMacAddress(MAC, OPTIONAL)); |
| 185 | assertTrue("is not proper mac", cfg.isMacAddress("none", OPTIONAL)); |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 186 | assertTrue("did not detect missing field", |
| 187 | expectInvalidField(() -> cfg.isMacAddress("none", MANDATORY))); |
| 188 | assertTrue("did not detect bad ip", |
| 189 | expectInvalidField(() -> cfg.isMacAddress(BAD_MAC, MANDATORY))); |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 192 | @Test |
| 193 | public void isIpAddress() { |
| 194 | assertTrue("is not proper ip", cfg.isIpAddress(IP, MANDATORY)); |
| 195 | assertTrue("is not proper ip", cfg.isIpAddress(IP, OPTIONAL)); |
| 196 | assertTrue("is not proper ip", cfg.isIpAddress("none", OPTIONAL)); |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 197 | assertTrue("did not detect missing ip", |
| 198 | expectInvalidField(() -> cfg.isIpAddress("none", MANDATORY))); |
| 199 | assertTrue("did not detect bad ip", |
| 200 | expectInvalidField(() -> cfg.isIpAddress(BAD_IP, MANDATORY))); |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 203 | @Test |
| 204 | public void isIpPrefix() { |
| 205 | assertTrue("is not proper prefix", cfg.isIpPrefix(PREFIX, MANDATORY)); |
| 206 | assertTrue("is not proper prefix", cfg.isIpPrefix(PREFIX, OPTIONAL)); |
| 207 | assertTrue("is not proper prefix", cfg.isIpPrefix("none", OPTIONAL)); |
| 208 | assertTrue("did not detect missing prefix", |
| 209 | expectInvalidField(() -> cfg.isIpPrefix("none", MANDATORY))); |
| 210 | assertTrue("did not detect bad prefix", |
| 211 | expectInvalidField(() -> cfg.isIpPrefix(BAD_PREFIX, MANDATORY))); |
| 212 | } |
| 213 | |
| 214 | @Test |
| 215 | public void isConnectPoint() { |
| 216 | assertTrue("is not proper connectPoint", cfg.isConnectPoint(CONNECT_POINT, MANDATORY)); |
| 217 | assertTrue("is not proper connectPoint", cfg.isConnectPoint(CONNECT_POINT, OPTIONAL)); |
| 218 | assertTrue("is not proper connectPoint", cfg.isConnectPoint("none", OPTIONAL)); |
| 219 | assertTrue("did not detect missing connectPoint", |
| 220 | expectInvalidField(() -> cfg.isConnectPoint("none", MANDATORY))); |
| 221 | assertTrue("did not detect bad connectPoint", |
| 222 | expectInvalidField(() -> cfg.isConnectPoint(BAD_CONNECT_POINT, MANDATORY))); |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Hyunsun Moon | 61b73e9 | 2016-05-10 18:05:57 -0700 | [diff] [blame] | 225 | @Test |
| 226 | public void isTpPort() { |
| 227 | assertTrue("is not proper transport port", cfg.isTpPort(TP_PORT, MANDATORY)); |
| 228 | assertTrue("is not proper transport port", cfg.isTpPort(TP_PORT, OPTIONAL)); |
| 229 | assertTrue("is not proper transport port", cfg.isTpPort("none", OPTIONAL)); |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 230 | assertTrue("did not detect missing field", |
| 231 | expectInvalidField(() -> cfg.isTpPort("none", MANDATORY))); |
| 232 | assertTrue("is not proper transport port", |
| 233 | expectInvalidField(() -> cfg.isTpPort(BAD_TP_PORT, MANDATORY))); |
Hyunsun Moon | 61b73e9 | 2016-05-10 18:05:57 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 236 | /** |
| 237 | * Expects an InvalidFieldException to be thrown when the given runnable is |
| 238 | * run. |
| 239 | * |
| 240 | * @param runnable runnable to run |
| 241 | * @return true if an InvalidFieldException was thrown, otherwise false |
| 242 | */ |
| 243 | private boolean expectInvalidField(Runnable runnable) { |
| 244 | try { |
| 245 | runnable.run(); |
| 246 | return false; |
| 247 | } catch (InvalidFieldException e) { |
| 248 | return true; |
| 249 | } |
Hyunsun Moon | 61b73e9 | 2016-05-10 18:05:57 -0700 | [diff] [blame] | 250 | } |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 251 | |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 252 | private class TestConfig extends Config<String> { |
| 253 | } |
| 254 | |
| 255 | private class TestDelegate implements ConfigApplyDelegate { |
| 256 | @Override |
| 257 | public void onApply(Config config) { |
| 258 | } |
| 259 | } |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 260 | } |