Jian Li | f2acb66 | 2017-04-06 02:06:16 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | package org.onosproject.drivers.lisp.extensions.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.hamcrest.Description; |
| 21 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 22 | import org.junit.After; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onlab.packet.IpPrefix; |
| 26 | import org.onosproject.codec.CodecContext; |
| 27 | import org.onosproject.codec.JsonCodec; |
| 28 | import org.onosproject.codec.impl.CodecManager; |
| 29 | import org.onosproject.drivers.lisp.extensions.LispAppDataAddress; |
| 30 | import org.onosproject.drivers.lisp.extensions.LispMappingExtensionCodecRegistrator; |
| 31 | import org.onosproject.mapping.addresses.MappingAddresses; |
| 32 | import org.onosproject.mapping.web.codec.MappingAddressJsonMatcher; |
| 33 | |
| 34 | import java.io.IOException; |
| 35 | import java.io.InputStream; |
| 36 | |
| 37 | import static org.hamcrest.MatcherAssert.assertThat; |
| 38 | import static org.hamcrest.Matchers.is; |
| 39 | import static org.hamcrest.Matchers.notNullValue; |
| 40 | |
| 41 | /** |
| 42 | * Unit tests for LispAppDataAddressCodec. |
| 43 | */ |
| 44 | public class LispAppDataAddressCodecTest { |
| 45 | |
| 46 | private static final byte PROTOCOL = (byte) 1; |
| 47 | private static final int IP_TOS = 1; |
| 48 | private static final short LOCAL_PORT_LOW = (short) 1; |
| 49 | private static final short LOCAL_PORT_HIGH = (short) 1; |
| 50 | private static final short REMOTE_PORT_LOW = (short) 1; |
| 51 | private static final short REMOTE_PORT_HIGH = (short) 1; |
| 52 | private static final IpPrefix IPV4_PREFIX = IpPrefix.valueOf("10.1.1.0/24"); |
| 53 | |
| 54 | private CodecContext context; |
| 55 | private JsonCodec<LispAppDataAddress> appDataAddressCodec; |
| 56 | private LispMappingExtensionCodecRegistrator registrator; |
| 57 | |
| 58 | /** |
| 59 | * Sets up for each test. |
| 60 | * Creates a context and fetches the LispAppDataAddress codec. |
| 61 | */ |
| 62 | @Before |
| 63 | public void setUp() { |
| 64 | CodecManager manager = new CodecManager(); |
| 65 | registrator = new LispMappingExtensionCodecRegistrator(); |
| 66 | registrator.codecService = manager; |
| 67 | registrator.activate(); |
| 68 | |
| 69 | context = new LispMappingExtensionCodecContextAdapter(registrator.codecService); |
| 70 | appDataAddressCodec = context.codec(LispAppDataAddress.class); |
| 71 | assertThat(appDataAddressCodec, notNullValue()); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Deactivates the codec registrator. |
| 76 | */ |
| 77 | @After |
| 78 | public void tearDown() { |
| 79 | registrator.deactivate(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Tests encoding of a LispAppDataAddress object. |
| 84 | */ |
| 85 | @Test |
| 86 | public void testLispAppDataAddressEncode() { |
| 87 | |
| 88 | LispAppDataAddress address = new LispAppDataAddress.Builder() |
| 89 | .withProtocol(PROTOCOL) |
| 90 | .withIpTos(IP_TOS) |
| 91 | .withLocalPortLow(LOCAL_PORT_LOW) |
| 92 | .withLocalPortHigh(LOCAL_PORT_HIGH) |
| 93 | .withRemotePortLow(REMOTE_PORT_LOW) |
| 94 | .withRemotePortHigh(REMOTE_PORT_HIGH) |
| 95 | .withAddress(MappingAddresses.ipv4MappingAddress(IPV4_PREFIX)) |
| 96 | .build(); |
| 97 | ObjectNode addressJson = appDataAddressCodec.encode(address, context); |
| 98 | assertThat(addressJson, LispAppDataAddressJsonMatcher.matchesAppDataAddress(address)); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Tests decoding of a LispAppDataAddress JSON object. |
| 103 | */ |
| 104 | @Test |
| 105 | public void testLispAppDataAddressDecode() throws IOException { |
| 106 | LispAppDataAddress appDataAddress = |
| 107 | getLispAppDataAddress("LispAppDataAddress.json"); |
| 108 | |
| 109 | assertThat("incorrect protocol value", |
| 110 | appDataAddress.getProtocol(), is(PROTOCOL)); |
| 111 | assertThat("incorrect IP ToS value", |
| 112 | appDataAddress.getIpTos(), is(IP_TOS)); |
| 113 | assertThat("incorrect local port low value", |
| 114 | appDataAddress.getLocalPortLow(), is(LOCAL_PORT_LOW)); |
| 115 | assertThat("incorrect local port high value", |
| 116 | appDataAddress.getLocalPortHigh(), is(LOCAL_PORT_HIGH)); |
| 117 | assertThat("incorrect remote port low value", |
| 118 | appDataAddress.getRemotePortLow(), is(REMOTE_PORT_LOW)); |
| 119 | assertThat("incorrect remote port high value", |
| 120 | appDataAddress.getRemotePortHigh(), is(REMOTE_PORT_HIGH)); |
| 121 | assertThat("incorrect mapping address", |
| 122 | appDataAddress.getAddress(), |
| 123 | is(MappingAddresses.ipv4MappingAddress(IPV4_PREFIX))); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Hamcrest matcher for LispAppDataAddress. |
| 128 | */ |
| 129 | public static final class LispAppDataAddressJsonMatcher |
| 130 | extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 131 | |
| 132 | private final LispAppDataAddress address; |
| 133 | |
| 134 | /** |
| 135 | * Default constructor. |
| 136 | * |
| 137 | * @param address LispAppDataAddress object |
| 138 | */ |
| 139 | private LispAppDataAddressJsonMatcher(LispAppDataAddress address) { |
| 140 | this.address = address; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 145 | |
| 146 | // check protocol |
| 147 | byte jsonProtocol = (byte) jsonNode.get(LispAppDataAddressCodec.PROTOCOL).asInt(); |
| 148 | byte protocol = address.getProtocol(); |
| 149 | if (jsonProtocol != protocol) { |
| 150 | description.appendText("protocol was " + jsonProtocol); |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | // check IP type of service |
| 155 | int jsonIpTos = jsonNode.get(LispAppDataAddressCodec.IP_TOS).asInt(); |
| 156 | int ipTos = address.getIpTos(); |
| 157 | if (jsonIpTos != ipTos) { |
| 158 | description.appendText("IP ToS was " + jsonIpTos); |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | // check local port low |
| 163 | short jsonLocalPortLow = (short) |
| 164 | jsonNode.get(LispAppDataAddressCodec.LOCAL_PORT_LOW).asInt(); |
| 165 | short localPortLow = address.getLocalPortLow(); |
| 166 | if (jsonLocalPortLow != localPortLow) { |
| 167 | description.appendText("Local port low was " + jsonLocalPortLow); |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | // check local port high |
| 172 | short jsonLocalPortHigh = (short) |
| 173 | jsonNode.get(LispAppDataAddressCodec.LOCAL_PORT_HIGH).asInt(); |
| 174 | short localPortHigh = address.getLocalPortHigh(); |
| 175 | if (jsonLocalPortHigh != localPortHigh) { |
| 176 | description.appendText("Local port high was " + jsonLocalPortHigh); |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | // check remote port low |
| 181 | short jsonRemotePortLow = (short) |
| 182 | jsonNode.get(LispAppDataAddressCodec.REMOTE_PORT_LOW).asInt(); |
| 183 | short remotePortLow = address.getRemotePortLow(); |
| 184 | if (jsonRemotePortLow != remotePortLow) { |
| 185 | description.appendText("Remote port low was " + jsonRemotePortLow); |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | // check remote port high |
| 190 | short jsonRemotePortHigh = (short) |
| 191 | jsonNode.get(LispAppDataAddressCodec.REMOTE_PORT_HIGH).asInt(); |
| 192 | short remotePortHigh = address.getRemotePortHigh(); |
| 193 | if (jsonRemotePortHigh != remotePortHigh) { |
| 194 | description.appendText("Remote port high was " + jsonRemotePortHigh); |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | // check address |
| 199 | MappingAddressJsonMatcher addressMatcher = |
| 200 | MappingAddressJsonMatcher.matchesMappingAddress(address.getAddress()); |
| 201 | if (!addressMatcher.matches(jsonNode.get(LispAppDataAddressCodec.ADDRESS))) { |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public void describeTo(Description description) { |
| 210 | description.appendText(address.toString()); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Factory to allocate a LispAppDataAddress matcher. |
| 215 | * |
| 216 | * @param address LispAppDataAddress object we are looking for |
| 217 | * @return matcher |
| 218 | */ |
| 219 | public static LispAppDataAddressJsonMatcher matchesAppDataAddress( |
| 220 | LispAppDataAddress address) { |
| 221 | return new LispAppDataAddressJsonMatcher(address); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Reads in a LispAppDataAddress from the given resource and decodes it. |
| 227 | * |
| 228 | * @param resourceName resource to use to read the JSON for the rule |
| 229 | * @return decoded LispAppDataAddress |
| 230 | * @throws IOException if processing the resource fails |
| 231 | */ |
| 232 | private LispAppDataAddress getLispAppDataAddress(String resourceName) throws IOException { |
| 233 | InputStream jsonStream = LispAppDataAddressCodecTest.class.getResourceAsStream(resourceName); |
| 234 | JsonNode json = context.mapper().readTree(jsonStream); |
| 235 | assertThat(json, notNullValue()); |
Jian Li | 944e3a9 | 2017-04-07 23:53:36 +0900 | [diff] [blame^] | 236 | LispAppDataAddress appDataAddress = appDataAddressCodec.decode((ObjectNode) json, context); |
| 237 | assertThat(appDataAddress, notNullValue()); |
| 238 | return appDataAddress; |
Jian Li | f2acb66 | 2017-04-06 02:06:16 +0900 | [diff] [blame] | 239 | } |
| 240 | } |