Jian Li | 4508352 | 2017-03-20 18:46:07 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jian Li | 4508352 | 2017-03-20 18:46:07 +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 | */ |
Jian Li | 2e818b0 | 2017-04-12 19:28:30 +0900 | [diff] [blame] | 16 | package org.onosproject.mapping.codec; |
Jian Li | 4508352 | 2017-03-20 18:46:07 +0900 | [diff] [blame] | 17 | |
Jian Li | 124ecf0 | 2017-04-05 16:38:34 +0900 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Jian Li | 4508352 | 2017-03-20 18:46:07 +0900 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.IpPrefix; |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | import org.onosproject.codec.CodecContext; |
Jian Li | 4508352 | 2017-03-20 18:46:07 +0900 | [diff] [blame] | 26 | import org.onosproject.codec.JsonCodec; |
| 27 | import org.onosproject.codec.impl.CodecManager; |
| 28 | import org.onosproject.mapping.addresses.MappingAddress; |
| 29 | import org.onosproject.mapping.addresses.MappingAddresses; |
Jian Li | 2e818b0 | 2017-04-12 19:28:30 +0900 | [diff] [blame] | 30 | import org.onosproject.mapping.MappingCodecRegistrator; |
Jian Li | 4508352 | 2017-03-20 18:46:07 +0900 | [diff] [blame] | 31 | |
Jian Li | 124ecf0 | 2017-04-05 16:38:34 +0900 | [diff] [blame] | 32 | import java.io.IOException; |
| 33 | import java.io.InputStream; |
| 34 | |
Jian Li | 4508352 | 2017-03-20 18:46:07 +0900 | [diff] [blame] | 35 | import static org.hamcrest.MatcherAssert.assertThat; |
Jian Li | 124ecf0 | 2017-04-05 16:38:34 +0900 | [diff] [blame] | 36 | import static org.hamcrest.Matchers.is; |
Jian Li | 4508352 | 2017-03-20 18:46:07 +0900 | [diff] [blame] | 37 | import static org.hamcrest.Matchers.notNullValue; |
Jian Li | 2e818b0 | 2017-04-12 19:28:30 +0900 | [diff] [blame] | 38 | import static org.onosproject.mapping.codec.MappingAddressJsonMatcher.matchesMappingAddress; |
Jian Li | 4508352 | 2017-03-20 18:46:07 +0900 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Unit tests for MappingAddressCodec. |
| 42 | */ |
| 43 | public class MappingAddressCodecTest { |
| 44 | |
| 45 | private CodecContext context; |
| 46 | private JsonCodec<MappingAddress> addressCodec; |
Jian Li | b54d14b | 2017-03-28 21:34:34 +0900 | [diff] [blame] | 47 | private MappingCodecRegistrator registrator; |
Jian Li | 124ecf0 | 2017-04-05 16:38:34 +0900 | [diff] [blame] | 48 | private static final String IPV4_STRING = "1.2.3.4"; |
| 49 | private static final String PORT_STRING = "32"; |
| 50 | private static final IpPrefix IPV4_PREFIX = |
| 51 | IpPrefix.valueOf(IPV4_STRING + "/" + PORT_STRING); |
Jian Li | 4508352 | 2017-03-20 18:46:07 +0900 | [diff] [blame] | 52 | private static final IpPrefix IPV6_PREFIX = IpPrefix.valueOf("fe80::/64"); |
| 53 | private static final MacAddress MAC = MacAddress.valueOf("00:00:11:00:00:01"); |
| 54 | private static final String DN = "onos"; |
| 55 | private static final String AS = "AS1000"; |
| 56 | |
| 57 | /** |
| 58 | * Sets up for each test. |
| 59 | * Creates a context and fetches the mapping address codec. |
| 60 | */ |
| 61 | @Before |
| 62 | public void setUp() { |
| 63 | CodecManager manager = new CodecManager(); |
| 64 | registrator = new MappingCodecRegistrator(); |
| 65 | registrator.codecService = manager; |
| 66 | registrator.activate(); |
| 67 | |
Jian Li | fa69be6 | 2017-04-05 16:00:10 +0900 | [diff] [blame] | 68 | context = new MappingCodecContextAdapter(registrator.codecService); |
Jian Li | 4508352 | 2017-03-20 18:46:07 +0900 | [diff] [blame] | 69 | |
| 70 | addressCodec = context.codec(MappingAddress.class); |
| 71 | assertThat(addressCodec, notNullValue()); |
| 72 | } |
| 73 | |
| 74 | @After |
| 75 | public void tearDown() { |
| 76 | registrator.deactivate(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Tests AS mapping address. |
| 81 | */ |
| 82 | @Test |
| 83 | public void asMappingAddressTest() { |
| 84 | MappingAddress address = MappingAddresses.asMappingAddress(AS); |
| 85 | ObjectNode result = addressCodec.encode(address, context); |
| 86 | assertThat(result, matchesMappingAddress(address)); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Tests DN mapping address. |
| 91 | */ |
| 92 | @Test |
| 93 | public void dnMappingAddressTest() { |
| 94 | MappingAddress address = MappingAddresses.dnMappingAddress(DN); |
| 95 | ObjectNode result = addressCodec.encode(address, context); |
| 96 | assertThat(result, matchesMappingAddress(address)); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Tests IPv4 mapping address. |
| 101 | */ |
| 102 | @Test |
| 103 | public void ipv4MappingAddressTest() { |
| 104 | MappingAddress address = MappingAddresses.ipv4MappingAddress(IPV4_PREFIX); |
| 105 | ObjectNode result = addressCodec.encode(address, context); |
| 106 | assertThat(result, matchesMappingAddress(address)); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Tests IPv6 mapping address. |
| 111 | */ |
| 112 | @Test |
| 113 | public void ipv6MappingAddressTest() { |
| 114 | MappingAddress address = MappingAddresses.ipv6MappingAddress(IPV6_PREFIX); |
| 115 | ObjectNode result = addressCodec.encode(address, context); |
| 116 | assertThat(result, matchesMappingAddress(address)); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Tests Ethernet mapping address. |
| 121 | */ |
| 122 | @Test |
| 123 | public void ethMappingAddressTest() { |
| 124 | MappingAddress address = MappingAddresses.ethMappingAddress(MAC); |
| 125 | ObjectNode result = addressCodec.encode(address, context); |
| 126 | assertThat(result, matchesMappingAddress(address)); |
| 127 | } |
Jian Li | 124ecf0 | 2017-04-05 16:38:34 +0900 | [diff] [blame] | 128 | |
| 129 | /** |
| 130 | * Tests the decoding of mapping address from JSON object. |
| 131 | * |
| 132 | * @throws IOException if processing the resource fails |
| 133 | */ |
| 134 | @Test |
| 135 | public void testMappingAddressDecode() throws IOException { |
| 136 | MappingAddress address = getAddress("MappingAddress.json"); |
| 137 | assertThat(address.toString(), |
| 138 | is("IPV4:" + IPV4_STRING + "/" + PORT_STRING)); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Reads in a mapping address from the given resource and decodes it. |
| 143 | * |
| 144 | * @param resourceName resource to use to read the JSON for the rule |
| 145 | * @return decoded mappingAddress |
| 146 | * @throws IOException if processing the resource fails |
| 147 | */ |
| 148 | private MappingAddress getAddress(String resourceName) throws IOException { |
| 149 | InputStream jsonStream = MappingAddressCodecTest.class.getResourceAsStream(resourceName); |
| 150 | JsonNode json = context.mapper().readTree(jsonStream); |
| 151 | assertThat(json, notNullValue()); |
| 152 | MappingAddress address = addressCodec.decode((ObjectNode) json, context); |
| 153 | assertThat(address, notNullValue()); |
| 154 | return address; |
| 155 | } |
Jian Li | 4508352 | 2017-03-20 18:46:07 +0900 | [diff] [blame] | 156 | } |