Jian Li | d144501 | 2017-04-09 02:12:07 +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.LispMappingExtensionCodecRegistrator; |
| 30 | import org.onosproject.drivers.lisp.extensions.LispMulticastAddress; |
| 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 LispMulticastAddressCodec. |
| 43 | */ |
| 44 | public class LispMulticastAddressCodecTest { |
| 45 | |
| 46 | private static final int INSTANCE_ID = 1; |
| 47 | private static final byte SRC_MASK_LENGTH = 2; |
| 48 | private static final byte GRP_MASK_LENGTH = 3; |
| 49 | |
| 50 | private static final IpPrefix SRC_ADDRESS_PREFIX = IpPrefix.valueOf("10.1.1.0/24"); |
| 51 | private static final IpPrefix GRP_ADDRESS_PREFIX = IpPrefix.valueOf("10.1.1.0/24"); |
| 52 | |
| 53 | private CodecContext context; |
| 54 | private JsonCodec<LispMulticastAddress> multicastAddressCodec; |
| 55 | private LispMappingExtensionCodecRegistrator registrator; |
| 56 | |
| 57 | /** |
| 58 | * Sets up for each test. |
| 59 | * Creates a context and fetches the LispMulticastAddress codec. |
| 60 | */ |
| 61 | @Before |
| 62 | public void setUp() { |
| 63 | CodecManager manager = new CodecManager(); |
| 64 | registrator = new LispMappingExtensionCodecRegistrator(); |
| 65 | registrator.codecService = manager; |
| 66 | registrator.activate(); |
| 67 | |
| 68 | context = new LispMappingExtensionCodecContextAdapter(registrator.codecService); |
| 69 | multicastAddressCodec = context.codec(LispMulticastAddress.class); |
| 70 | assertThat("Multicast address codec should not be null", |
| 71 | multicastAddressCodec, 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 LispMulticastAddress object. |
| 84 | */ |
| 85 | @Test |
| 86 | public void testLispMulticastAddressEncode() { |
| 87 | LispMulticastAddress address = new LispMulticastAddress.Builder() |
| 88 | .withInstanceId(INSTANCE_ID) |
| 89 | .withSrcMaskLength(SRC_MASK_LENGTH) |
| 90 | .withGrpMaskLength(GRP_MASK_LENGTH) |
| 91 | .withSrcAddress(MappingAddresses.ipv4MappingAddress(SRC_ADDRESS_PREFIX)) |
| 92 | .withGrpAddress(MappingAddresses.ipv4MappingAddress(GRP_ADDRESS_PREFIX)) |
| 93 | .build(); |
| 94 | ObjectNode addressJson = multicastAddressCodec.encode(address, context); |
| 95 | assertThat("errors in encoding Multicast address JSON", |
| 96 | addressJson, LispMulticastAddressJsonMatcher.matchesMulticastAddress(address)); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Tests decoding of a LispMulticastAddress JSON object. |
| 101 | */ |
| 102 | @Test |
| 103 | public void testLispMulticastAddressDecode() throws IOException { |
| 104 | LispMulticastAddress address = |
| 105 | getLispMulticastAddress("LispMulticastAddress.json"); |
| 106 | |
| 107 | assertThat("incorrect instance id", |
| 108 | address.getInstanceId(), is(INSTANCE_ID)); |
| 109 | assertThat("incorrect srcMaskLength", |
| 110 | address.getSrcMaskLength(), is(SRC_MASK_LENGTH)); |
| 111 | assertThat("incorrect srcAddress", address.getSrcAddress(), |
| 112 | is(MappingAddresses.ipv4MappingAddress(SRC_ADDRESS_PREFIX))); |
| 113 | assertThat("incorrect grpMaskLength", |
| 114 | address.getGrpMaskLength(), is(GRP_MASK_LENGTH)); |
| 115 | assertThat("incorrect grpAddress", address.getGrpAddress(), |
| 116 | is(MappingAddresses.ipv4MappingAddress(GRP_ADDRESS_PREFIX))); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Hamcrest matcher for LispMulticastAddress. |
| 121 | */ |
| 122 | public static final class LispMulticastAddressJsonMatcher |
| 123 | extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 124 | |
| 125 | private final LispMulticastAddress address; |
| 126 | |
| 127 | /** |
| 128 | * Default constructor. |
| 129 | * |
| 130 | * @param address LispMulticastAddres object |
| 131 | */ |
| 132 | private LispMulticastAddressJsonMatcher(LispMulticastAddress address) { |
| 133 | this.address = address; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 138 | |
| 139 | // check instance id |
| 140 | int jsonInstanceId = jsonNode.get(LispMulticastAddressCodec.INSTANCE_ID).asInt(); |
| 141 | int instanceId = address.getInstanceId(); |
| 142 | if (jsonInstanceId != instanceId) { |
| 143 | description.appendText("Instance id was " + jsonInstanceId); |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | // check source mask length |
| 148 | byte jsonSrcMaskLength = (byte) jsonNode.get( |
| 149 | LispMulticastAddressCodec.SRC_MASK_LENGTH).asInt(); |
| 150 | byte srcMaskLength = address.getSrcMaskLength(); |
| 151 | if (jsonSrcMaskLength != srcMaskLength) { |
| 152 | description.appendText("SrcMaskLength was " + jsonSrcMaskLength); |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | // check group mask length |
| 157 | byte jsonGrpMaskLength = (byte) jsonNode.get( |
| 158 | LispMulticastAddressCodec.GRP_MASK_LENGTH).asInt(); |
| 159 | byte grpMaskLength = address.getGrpMaskLength(); |
| 160 | if (jsonGrpMaskLength != grpMaskLength) { |
| 161 | description.appendText("GrpMaskLength was " + jsonGrpMaskLength); |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | // check source address |
| 166 | MappingAddressJsonMatcher srcAddressMatcher = |
| 167 | MappingAddressJsonMatcher.matchesMappingAddress(address.getSrcAddress()); |
| 168 | |
| 169 | // check group address |
| 170 | MappingAddressJsonMatcher grpAddressMatcher = |
| 171 | MappingAddressJsonMatcher.matchesMappingAddress(address.getGrpAddress()); |
| 172 | |
| 173 | return srcAddressMatcher.matches(jsonNode.get(LispMulticastAddressCodec.SRC_ADDRESS)) || |
| 174 | grpAddressMatcher.matches(jsonNode.get(LispMulticastAddressCodec.GRP_ADDRESS)); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void describeTo(Description description) { |
| 179 | description.appendText(address.toString()); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Factory to allocate a LispMulticastAddress matcher. |
| 184 | * |
| 185 | * @param address LispMulticastAddress object we are looking for |
| 186 | * @return matcher |
| 187 | */ |
| 188 | public static LispMulticastAddressJsonMatcher matchesMulticastAddress( |
| 189 | LispMulticastAddress address) { |
| 190 | return new LispMulticastAddressJsonMatcher(address); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Reads in a LispMulticastAddress from the given resource and decodes it. |
| 196 | * |
| 197 | * @param resourceName resource to use to read the JSON for the rule |
| 198 | * @return decoded LispMulticastAddress |
| 199 | * @throws IOException if processing the resource fails |
| 200 | */ |
| 201 | private LispMulticastAddress getLispMulticastAddress(String resourceName) throws IOException { |
| 202 | InputStream jsonStream = LispMulticastAddressCodecTest.class.getResourceAsStream(resourceName); |
| 203 | JsonNode json = context.mapper().readTree(jsonStream); |
| 204 | assertThat("JSON string should not be null", json, notNullValue()); |
| 205 | LispMulticastAddress multicastAddress = multicastAddressCodec.decode((ObjectNode) json, context); |
| 206 | assertThat("decoded address should not be null", multicastAddress, notNullValue()); |
| 207 | return multicastAddress; |
| 208 | } |
| 209 | } |