Hyunsun Moon | af3822c | 2017-04-04 15:56:27 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | 80dff97 | 2017-08-03 22:46:30 -0700 | [diff] [blame^] | 2 | * Copyright 2017-present Open Networking Foundation |
Hyunsun Moon | af3822c | 2017-04-04 15:56:27 +0900 | [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 | package org.opencord.cordvtn.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.NullNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onlab.packet.MacAddress; |
| 26 | import org.onlab.packet.VlanId; |
| 27 | import org.onosproject.codec.JsonCodec; |
| 28 | import org.opencord.cordvtn.api.net.AddressPair; |
| 29 | import org.opencord.cordvtn.api.net.NetworkId; |
| 30 | import org.opencord.cordvtn.api.net.PortId; |
| 31 | import org.opencord.cordvtn.api.net.ServicePort; |
| 32 | import org.opencord.cordvtn.impl.DefaultServicePort; |
| 33 | |
| 34 | import java.io.IOException; |
| 35 | import java.io.InputStream; |
| 36 | import java.util.HashSet; |
| 37 | import java.util.Set; |
| 38 | |
| 39 | import static org.hamcrest.MatcherAssert.assertThat; |
| 40 | import static org.hamcrest.Matchers.is; |
| 41 | import static org.hamcrest.Matchers.notNullValue; |
| 42 | import static org.opencord.cordvtn.codec.ServicePortJsonMatcher.matchesServicePort; |
| 43 | |
| 44 | /** |
| 45 | * Unit tests for ServiceNetwork codec. |
| 46 | */ |
| 47 | public final class ServicePortCodecTest { |
| 48 | |
| 49 | private static final String SERVICE_PORT = "servicePort"; |
| 50 | private static final String ID = "id"; |
| 51 | private static final String NETWORK_ID = "network_id"; |
| 52 | private static final String NAME = "name"; |
| 53 | private static final String IP_ADDRESS = "ip_address"; |
| 54 | private static final String MAC_ADDRESS = "mac_address"; |
| 55 | private static final String FLOATING_ADDRESS_PAIRS = "floating_address_pairs"; |
| 56 | private static final String VLAN_ID = "vlan_id"; |
| 57 | |
| 58 | private static final PortId PORT_ID_1 = PortId.of("port-1"); |
| 59 | private static final NetworkId NETWORK_ID_1 = NetworkId.of("network-1"); |
| 60 | private static final String PORT_NAME_1 = "tap1"; |
| 61 | private static final MacAddress MAC_ADDRESS_1 = MacAddress.valueOf("00:00:00:00:00:01"); |
| 62 | private static final IpAddress IP_ADDRESS_1 = IpAddress.valueOf("10.0.0.1"); |
| 63 | private static final VlanId VLAN_ID_1 = VlanId.vlanId("222"); |
| 64 | |
| 65 | private static final AddressPair ADDRESS_PAIR_1 = AddressPair.of( |
| 66 | IpAddress.valueOf("192.168.0.1"), |
| 67 | MacAddress.valueOf("02:42:0a:06:01:01")); |
| 68 | private static final AddressPair ADDRESS_PAIR_2 = AddressPair.of( |
| 69 | IpAddress.valueOf("192.168.0.2"), |
| 70 | MacAddress.valueOf("02:42:0a:06:01:02")); |
| 71 | |
| 72 | private static final Set<AddressPair> ADDRESS_PAIRS_1 = |
| 73 | new HashSet<AddressPair>() { |
| 74 | { |
| 75 | add(ADDRESS_PAIR_1); |
| 76 | add(ADDRESS_PAIR_2); |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | private static final ServicePort PORT_1 = DefaultServicePort.builder() |
| 81 | .id(PORT_ID_1) |
| 82 | .networkId(NETWORK_ID_1) |
| 83 | .name(PORT_NAME_1) |
| 84 | .ip(IP_ADDRESS_1) |
| 85 | .mac(MAC_ADDRESS_1) |
| 86 | .addressPairs(ADDRESS_PAIRS_1) |
| 87 | .vlanId(VLAN_ID_1) |
| 88 | .build(); |
| 89 | |
| 90 | private JsonCodec<ServicePort> codec; |
| 91 | private MockCodecContext context; |
| 92 | |
| 93 | /** |
| 94 | * Creates a context and gets the servicePort codec for each test. |
| 95 | */ |
| 96 | @Before |
| 97 | public void setUp() { |
| 98 | context = new MockCodecContext(); |
| 99 | codec = context.codec(ServicePort.class); |
| 100 | assertThat(codec, notNullValue()); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Checks if encoding service port works properly. |
| 105 | */ |
| 106 | @Test |
| 107 | public void testServicePortEncode() { |
| 108 | ObjectNode jsonPort = codec.encode(PORT_1, context); |
| 109 | assertThat(jsonPort, notNullValue()); |
| 110 | assertThat(jsonPort, matchesServicePort(PORT_1)); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Checks if decoding service port works properly. |
| 115 | */ |
| 116 | @Test |
| 117 | public void testServicePortDecode() throws IOException { |
| 118 | ServicePort sPort = getServicePort("service-port.json"); |
| 119 | assertThat(sPort.id(), is(PORT_ID_1)); |
| 120 | assertThat(sPort.networkId(), is(NETWORK_ID_1)); |
| 121 | assertThat(sPort.name(), is(PORT_NAME_1)); |
| 122 | assertThat(sPort.ip(), is(IP_ADDRESS_1)); |
| 123 | assertThat(sPort.mac(), is(MAC_ADDRESS_1)); |
| 124 | assertThat(sPort.addressPairs(), is(ADDRESS_PAIRS_1)); |
| 125 | assertThat(sPort.vlanId(), is(VLAN_ID_1)); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Checks if decoding service port without ID fails. |
| 130 | */ |
| 131 | @Test(expected = IllegalArgumentException.class) |
| 132 | public void testServicePortDecodeMissingId() { |
| 133 | final JsonNode jsonInvalidId = context.mapper().createObjectNode() |
| 134 | .put(NETWORK_ID, NETWORK_ID_1.id()) |
| 135 | .put(NAME, PORT_NAME_1) |
| 136 | .put(IP_ADDRESS, IP_ADDRESS_1.toString()) |
| 137 | .put(MAC_ADDRESS, MAC_ADDRESS_1.toString()) |
| 138 | .put(VLAN_ID, VLAN_ID_1.toShort()); |
| 139 | codec.decode((ObjectNode) jsonInvalidId, context); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Checks if decoding service port with empty ID fails. |
| 144 | */ |
| 145 | @Test(expected = IllegalArgumentException.class) |
| 146 | public void testServicePortDecodeEmptyId() { |
| 147 | final JsonNode jsonInvalidId = context.mapper().createObjectNode() |
| 148 | .put(ID, "") |
| 149 | .put(NETWORK_ID, NETWORK_ID_1.id()) |
| 150 | .put(NAME, PORT_NAME_1) |
| 151 | .put(IP_ADDRESS, IP_ADDRESS_1.toString()) |
| 152 | .put(MAC_ADDRESS, MAC_ADDRESS_1.toString()) |
| 153 | .put(VLAN_ID, VLAN_ID_1.toShort()); |
| 154 | codec.decode((ObjectNode) jsonInvalidId, context); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Checks if decoding service port with null ID fails. |
| 159 | */ |
| 160 | @Test(expected = IllegalArgumentException.class) |
| 161 | public void testServicePortDecodeNullId() { |
| 162 | final JsonNode jsonInvalidId = context.mapper().createObjectNode() |
| 163 | .put(NETWORK_ID, NETWORK_ID_1.id()) |
| 164 | .put(NAME, PORT_NAME_1) |
| 165 | .put(IP_ADDRESS, IP_ADDRESS_1.toString()) |
| 166 | .put(MAC_ADDRESS, MAC_ADDRESS_1.toString()) |
| 167 | .put(VLAN_ID, VLAN_ID_1.toShort()) |
| 168 | .set(ID, NullNode.getInstance()); |
| 169 | codec.decode((ObjectNode) jsonInvalidId, context); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Checks if empty string is not allowed for network ID. |
| 174 | */ |
| 175 | @Test(expected = IllegalArgumentException.class) |
| 176 | public void testServicePortDecodeEmptyNetworkId() { |
| 177 | final JsonNode jsonInvalidId = context.mapper().createObjectNode() |
| 178 | .put(ID, PORT_ID_1.id()) |
| 179 | .put(NETWORK_ID, "") |
| 180 | .put(NAME, PORT_NAME_1) |
| 181 | .put(IP_ADDRESS, IP_ADDRESS_1.toString()) |
| 182 | .put(MAC_ADDRESS, MAC_ADDRESS_1.toString()) |
| 183 | .put(VLAN_ID, VLAN_ID_1.toShort()); |
| 184 | codec.decode((ObjectNode) jsonInvalidId, context); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Checks if null is not allowed for network ID. |
| 189 | */ |
| 190 | @Test(expected = IllegalArgumentException.class) |
| 191 | public void testServicePortDecodeNullNetworkId() { |
| 192 | final JsonNode jsonInvalidId = context.mapper().createObjectNode() |
| 193 | .put(ID, PORT_ID_1.id()) |
| 194 | .put(NAME, PORT_NAME_1) |
| 195 | .put(IP_ADDRESS, IP_ADDRESS_1.toString()) |
| 196 | .put(MAC_ADDRESS, MAC_ADDRESS_1.toString()) |
| 197 | .put(VLAN_ID, VLAN_ID_1.toShort()) |
| 198 | .set(NETWORK_ID, NullNode.getInstance()); |
| 199 | codec.decode((ObjectNode) jsonInvalidId, context); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Checks if empty string is not allowed for port name. |
| 204 | */ |
| 205 | @Test(expected = IllegalArgumentException.class) |
| 206 | public void testServicePortDecodeEmptyName() { |
| 207 | JsonNode jsonInvalidName = context.mapper().createObjectNode() |
| 208 | .put(ID, PORT_ID_1.id()) |
| 209 | .put(NETWORK_ID, NETWORK_ID_1.id()) |
| 210 | .put(NAME, "") |
| 211 | .put(IP_ADDRESS, IP_ADDRESS_1.toString()) |
| 212 | .put(MAC_ADDRESS, MAC_ADDRESS_1.toString()) |
| 213 | .put(VLAN_ID, VLAN_ID_1.toShort()); |
| 214 | codec.decode((ObjectNode) jsonInvalidName, context); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Checks if null is not allowed for port name. |
| 219 | */ |
| 220 | @Test(expected = IllegalArgumentException.class) |
| 221 | public void testServicePortDecodeNullName() { |
| 222 | final JsonNode jsonInvalidName = context.mapper().createObjectNode() |
| 223 | .put(ID, PORT_ID_1.id()) |
| 224 | .put(NETWORK_ID, NETWORK_ID_1.id()) |
| 225 | .put(IP_ADDRESS, IP_ADDRESS_1.toString()) |
| 226 | .put(MAC_ADDRESS, MAC_ADDRESS_1.toString()) |
| 227 | .put(VLAN_ID, VLAN_ID_1.toShort()) |
| 228 | .set(NAME, NullNode.getInstance()); |
| 229 | codec.decode((ObjectNode) jsonInvalidName, context); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Checks if invalid IP address string is not allowed. |
| 234 | */ |
| 235 | @Test(expected = IllegalArgumentException.class) |
| 236 | public void testServicePortDecodeInvalidIpAddress() { |
| 237 | final JsonNode jsonInvalidIp = context.mapper().createObjectNode() |
| 238 | .put(ID, PORT_ID_1.id()) |
| 239 | .put(NETWORK_ID, NETWORK_ID_1.id()) |
| 240 | .put(NAME, PORT_NAME_1) |
| 241 | .put(IP_ADDRESS, "ipAddress") |
| 242 | .put(MAC_ADDRESS, MAC_ADDRESS_1.toString()) |
| 243 | .put(VLAN_ID, VLAN_ID_1.toShort()); |
| 244 | codec.decode((ObjectNode) jsonInvalidIp, context); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Checks if invalid IP address string is not allowed. |
| 249 | */ |
| 250 | @Test(expected = IllegalArgumentException.class) |
| 251 | public void testServicePortDecodeNullIpAddress() { |
| 252 | final JsonNode jsonInvalidIp = context.mapper().createObjectNode() |
| 253 | .put(ID, PORT_ID_1.id()) |
| 254 | .put(NETWORK_ID, NETWORK_ID_1.id()) |
| 255 | .put(NAME, PORT_NAME_1) |
| 256 | .put(MAC_ADDRESS, MAC_ADDRESS_1.toString()) |
| 257 | .put(VLAN_ID, VLAN_ID_1.toShort()) |
| 258 | .set(IP_ADDRESS, NullNode.getInstance()); |
| 259 | codec.decode((ObjectNode) jsonInvalidIp, context); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Checks if invalid MAC address string is not allowed. |
| 264 | */ |
| 265 | @Test(expected = IllegalArgumentException.class) |
| 266 | public void testServicePortDecodeInvalidMacAddress() { |
| 267 | final JsonNode jsonInvalidMac = context.mapper().createObjectNode() |
| 268 | .put(ID, PORT_ID_1.id()) |
| 269 | .put(NETWORK_ID, NETWORK_ID_1.id()) |
| 270 | .put(NAME, PORT_NAME_1) |
| 271 | .put(IP_ADDRESS, IP_ADDRESS_1.toString()) |
| 272 | .put(MAC_ADDRESS, "macAddress") |
| 273 | .put(VLAN_ID, VLAN_ID_1.toShort()); |
| 274 | codec.decode((ObjectNode) jsonInvalidMac, context); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Checks if null is not allowed for MAC address. |
| 279 | */ |
| 280 | @Test(expected = IllegalArgumentException.class) |
| 281 | public void testServicePortDecodeNullMacAddress() { |
| 282 | final JsonNode jsonInvalidMac = context.mapper().createObjectNode() |
| 283 | .put(ID, PORT_ID_1.id()) |
| 284 | .put(NETWORK_ID, NETWORK_ID_1.id()) |
| 285 | .put(NAME, PORT_NAME_1) |
| 286 | .put(IP_ADDRESS, IP_ADDRESS_1.toString()) |
| 287 | .put(VLAN_ID, VLAN_ID_1.toShort()) |
| 288 | .set(MAC_ADDRESS, NullNode.getInstance()); |
| 289 | codec.decode((ObjectNode) jsonInvalidMac, context); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Checks if invalid VLAN ID is not allowed. |
| 294 | */ |
| 295 | @Test(expected = IllegalArgumentException.class) |
| 296 | public void testServicePortDecodeInvalidVlanId() { |
| 297 | final JsonNode jsonInvalidVlan = context.mapper().createObjectNode() |
| 298 | .put(ID, PORT_ID_1.id()) |
| 299 | .put(NETWORK_ID, NETWORK_ID_1.id()) |
| 300 | .put(NAME, PORT_NAME_1) |
| 301 | .put(IP_ADDRESS, IP_ADDRESS_1.toString()) |
| 302 | .put(MAC_ADDRESS, MAC_ADDRESS_1.toString()) |
| 303 | .put(VLAN_ID, "vlanId"); |
| 304 | codec.decode((ObjectNode) jsonInvalidVlan, context); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Checks if null is not allowed for VLAN ID. |
| 309 | */ |
| 310 | @Test(expected = IllegalArgumentException.class) |
| 311 | public void testServicePortDecodeNullVlanId() { |
| 312 | final JsonNode jsonInvalidVlan = context.mapper().createObjectNode() |
| 313 | .put(ID, PORT_ID_1.id()) |
| 314 | .put(NETWORK_ID, NETWORK_ID_1.id()) |
| 315 | .put(NAME, PORT_NAME_1) |
| 316 | .put(IP_ADDRESS, IP_ADDRESS_1.toString()) |
| 317 | .put(MAC_ADDRESS, MAC_ADDRESS_1.toString()) |
| 318 | .set(VLAN_ID, NullNode.getInstance()); |
| 319 | codec.decode((ObjectNode) jsonInvalidVlan, context); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Checks if only array node is allowed for floating address pairs. |
| 324 | */ |
| 325 | @Test(expected = IllegalArgumentException.class) |
| 326 | public void testServicePortDecodeNonArrayAddressPairs() { |
| 327 | final JsonNode jsonAddrPair = context.mapper().createObjectNode() |
| 328 | .put(IP_ADDRESS, ADDRESS_PAIR_1.ip().toString()) |
| 329 | .put(MAC_ADDRESS, ADDRESS_PAIR_1.mac().toString()); |
| 330 | final JsonNode jsonInvalidAddrPair = context.mapper().createObjectNode() |
| 331 | .put(ID, PORT_ID_1.id()) |
| 332 | .put(NETWORK_ID, NETWORK_ID_1.id()) |
| 333 | .put(NAME, PORT_NAME_1) |
| 334 | .put(IP_ADDRESS, IP_ADDRESS_1.toString()) |
| 335 | .put(MAC_ADDRESS, MAC_ADDRESS_1.toString()) |
| 336 | .put(VLAN_ID, VLAN_ID_1.toShort()) |
| 337 | .set(FLOATING_ADDRESS_PAIRS, jsonAddrPair); |
| 338 | codec.decode((ObjectNode) jsonInvalidAddrPair, context); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Checks if null and empty array node is allowed for floating address pairs. |
| 343 | */ |
| 344 | @Test |
| 345 | public void testServicePortDecodeNullAndEmptyAddressPairs() { |
| 346 | JsonNode jsonInvalidAddrPair = context.mapper().createObjectNode() |
| 347 | .put(ID, PORT_ID_1.id()) |
| 348 | .put(NETWORK_ID, NETWORK_ID_1.id()) |
| 349 | .put(NAME, PORT_NAME_1) |
| 350 | .put(IP_ADDRESS, IP_ADDRESS_1.toString()) |
| 351 | .put(MAC_ADDRESS, MAC_ADDRESS_1.toString()) |
| 352 | .put(VLAN_ID, VLAN_ID_1.toShort()) |
| 353 | .set(FLOATING_ADDRESS_PAIRS, NullNode.getInstance()); |
| 354 | codec.decode((ObjectNode) jsonInvalidAddrPair, context); |
| 355 | |
| 356 | jsonInvalidAddrPair = context.mapper().createObjectNode() |
| 357 | .put(ID, PORT_ID_1.id()) |
| 358 | .put(NETWORK_ID, NETWORK_ID_1.id()) |
| 359 | .put(NAME, PORT_NAME_1) |
| 360 | .put(IP_ADDRESS, IP_ADDRESS_1.toString()) |
| 361 | .put(MAC_ADDRESS, MAC_ADDRESS_1.toString()) |
| 362 | .put(VLAN_ID, VLAN_ID_1.toShort()) |
| 363 | .set(FLOATING_ADDRESS_PAIRS, context.mapper().createArrayNode()); |
| 364 | codec.decode((ObjectNode) jsonInvalidAddrPair, context); |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Checks if floating address pair without IP address field fails. |
| 369 | */ |
| 370 | @Test(expected = IllegalArgumentException.class) |
| 371 | public void testServicePortDecodeMissingFloatingAddrPairIp() { |
| 372 | final ArrayNode jsonAddrPairs = context.mapper().createArrayNode(); |
| 373 | final JsonNode jsonAddrPair = context.mapper().createObjectNode() |
| 374 | .put(MAC_ADDRESS, ADDRESS_PAIR_1.mac().toString()); |
| 375 | jsonAddrPairs.add(jsonAddrPair); |
| 376 | final JsonNode jsonInvalidAddrPair = context.mapper().createObjectNode() |
| 377 | .put(ID, PORT_ID_1.id()) |
| 378 | .put(NETWORK_ID, NETWORK_ID_1.id()) |
| 379 | .put(NAME, PORT_NAME_1) |
| 380 | .put(IP_ADDRESS, IP_ADDRESS_1.toString()) |
| 381 | .put(MAC_ADDRESS, MAC_ADDRESS_1.toString()) |
| 382 | .put(VLAN_ID, VLAN_ID_1.toShort()) |
| 383 | .set(FLOATING_ADDRESS_PAIRS, jsonAddrPairs); |
| 384 | codec.decode((ObjectNode) jsonInvalidAddrPair, context); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Checks if floating address pair without MAC address field fails. |
| 389 | */ |
| 390 | @Test(expected = IllegalArgumentException.class) |
| 391 | public void testServicePortDecodeMissingFloatingAddrPairMac() { |
| 392 | final ArrayNode jsonAddrPairs = context.mapper().createArrayNode(); |
| 393 | final JsonNode jsonAddrPair = context.mapper().createObjectNode() |
| 394 | .put(IP_ADDRESS, ADDRESS_PAIR_1.ip().toString()); |
| 395 | jsonAddrPairs.add(jsonAddrPair); |
| 396 | final JsonNode jsonInvalidAddrPair = context.mapper().createObjectNode() |
| 397 | .put(ID, PORT_ID_1.id()) |
| 398 | .put(NETWORK_ID, NETWORK_ID_1.id()) |
| 399 | .put(NAME, PORT_NAME_1) |
| 400 | .put(IP_ADDRESS, IP_ADDRESS_1.toString()) |
| 401 | .put(MAC_ADDRESS, MAC_ADDRESS_1.toString()) |
| 402 | .put(VLAN_ID, VLAN_ID_1.toShort()) |
| 403 | .set(FLOATING_ADDRESS_PAIRS, jsonAddrPairs); |
| 404 | codec.decode((ObjectNode) jsonInvalidAddrPair, context); |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Checks if null IP address for floating address pair fails. |
| 409 | */ |
| 410 | @Test(expected = IllegalArgumentException.class) |
| 411 | public void testServicePortDecodeNullFloatingAddrPairIp() { |
| 412 | final ArrayNode jsonAddrPairs = context.mapper().createArrayNode(); |
| 413 | final JsonNode jsonAddrPair = context.mapper().createObjectNode() |
| 414 | .put(MAC_ADDRESS, ADDRESS_PAIR_1.mac().toString()) |
| 415 | .set(IP_ADDRESS, NullNode.getInstance()); |
| 416 | jsonAddrPairs.add(jsonAddrPair); |
| 417 | final JsonNode jsonInvalidAddrPair = context.mapper().createObjectNode() |
| 418 | .put(ID, PORT_ID_1.id()) |
| 419 | .put(NETWORK_ID, NETWORK_ID_1.id()) |
| 420 | .put(NAME, PORT_NAME_1) |
| 421 | .put(IP_ADDRESS, IP_ADDRESS_1.toString()) |
| 422 | .put(MAC_ADDRESS, MAC_ADDRESS_1.toString()) |
| 423 | .put(VLAN_ID, VLAN_ID_1.toShort()) |
| 424 | .set(FLOATING_ADDRESS_PAIRS, jsonAddrPairs); |
| 425 | codec.decode((ObjectNode) jsonInvalidAddrPair, context); |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Checks if null MAC address for floating address pair fails. |
| 430 | */ |
| 431 | @Test(expected = IllegalArgumentException.class) |
| 432 | public void testServicePortDecodeNullFloatingAddrPairMac() { |
| 433 | final ArrayNode jsonAddrPairs = context.mapper().createArrayNode(); |
| 434 | final JsonNode jsonAddrPair = context.mapper().createObjectNode() |
| 435 | .put(IP_ADDRESS, ADDRESS_PAIR_1.ip().toString()) |
| 436 | .set(MAC_ADDRESS, NullNode.getInstance()); |
| 437 | jsonAddrPairs.add(jsonAddrPair); |
| 438 | final JsonNode jsonInvalidAddrPair = context.mapper().createObjectNode() |
| 439 | .put(ID, PORT_ID_1.id()) |
| 440 | .put(NETWORK_ID, NETWORK_ID_1.id()) |
| 441 | .put(NAME, PORT_NAME_1) |
| 442 | .put(IP_ADDRESS, IP_ADDRESS_1.toString()) |
| 443 | .put(MAC_ADDRESS, MAC_ADDRESS_1.toString()) |
| 444 | .put(VLAN_ID, VLAN_ID_1.toShort()) |
| 445 | .set(FLOATING_ADDRESS_PAIRS, jsonAddrPairs); |
| 446 | codec.decode((ObjectNode) jsonInvalidAddrPair, context); |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Checks if invalid IP address for floating address pair fails. |
| 451 | */ |
| 452 | @Test(expected = IllegalArgumentException.class) |
| 453 | public void testServicePortDecodeInvalidFloatingAddrPairIp() { |
| 454 | final ArrayNode jsonAddrPairs = context.mapper().createArrayNode(); |
| 455 | final JsonNode jsonAddrPair = context.mapper().createObjectNode() |
| 456 | .put(IP_ADDRESS, "ipAddress") |
| 457 | .put(MAC_ADDRESS, ADDRESS_PAIR_1.mac().toString()); |
| 458 | jsonAddrPairs.add(jsonAddrPair); |
| 459 | final JsonNode jsonInvalidAddrPair = context.mapper().createObjectNode() |
| 460 | .put(ID, PORT_ID_1.id()) |
| 461 | .put(NETWORK_ID, NETWORK_ID_1.id()) |
| 462 | .put(NAME, PORT_NAME_1) |
| 463 | .put(IP_ADDRESS, IP_ADDRESS_1.toString()) |
| 464 | .put(MAC_ADDRESS, MAC_ADDRESS_1.toString()) |
| 465 | .put(VLAN_ID, VLAN_ID_1.toShort()) |
| 466 | .set(FLOATING_ADDRESS_PAIRS, jsonAddrPairs); |
| 467 | codec.decode((ObjectNode) jsonInvalidAddrPair, context); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Checks if invalid MAC address for floating address pair fails. |
| 472 | */ |
| 473 | @Test(expected = IllegalArgumentException.class) |
| 474 | public void testServicePortDecodeInvalidFloatingAddrPairMac() { |
| 475 | final ArrayNode jsonAddrPairs = context.mapper().createArrayNode(); |
| 476 | final JsonNode jsonAddrPair = context.mapper().createObjectNode() |
| 477 | .put(IP_ADDRESS, ADDRESS_PAIR_1.ip().toString()) |
| 478 | .put(MAC_ADDRESS, "macAddress"); |
| 479 | jsonAddrPairs.add(jsonAddrPair); |
| 480 | final JsonNode jsonInvalidAddrPair = context.mapper().createObjectNode() |
| 481 | .put(ID, PORT_ID_1.id()) |
| 482 | .put(NETWORK_ID, NETWORK_ID_1.id()) |
| 483 | .put(NAME, PORT_NAME_1) |
| 484 | .put(IP_ADDRESS, IP_ADDRESS_1.toString()) |
| 485 | .put(MAC_ADDRESS, MAC_ADDRESS_1.toString()) |
| 486 | .put(VLAN_ID, VLAN_ID_1.toShort()) |
| 487 | .set(FLOATING_ADDRESS_PAIRS, jsonAddrPairs); |
| 488 | codec.decode((ObjectNode) jsonInvalidAddrPair, context); |
| 489 | } |
| 490 | |
| 491 | private ServicePort getServicePort(String resource) throws IOException { |
| 492 | InputStream jsonStream = ServicePortCodecTest.class.getResourceAsStream(resource); |
| 493 | JsonNode jsonNode = context.mapper().readTree(jsonStream).get(SERVICE_PORT); |
| 494 | assertThat(jsonNode, notNullValue()); |
| 495 | |
| 496 | ServicePort sPort = codec.decode((ObjectNode) jsonNode, context); |
| 497 | assertThat(sPort, notNullValue()); |
| 498 | return sPort; |
| 499 | } |
| 500 | } |