Jian Li | 944e3a9 | 2017-04-07 23:53:36 +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.LispAsAddress; |
| 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 LispAsAddressCodec. |
| 43 | */ |
| 44 | public class LispAsAddressCodecTest { |
| 45 | |
| 46 | private static final int AS_NUMBER = 1; |
| 47 | private static final IpPrefix IPV4_PREFIX = IpPrefix.valueOf("10.1.1.0/24"); |
| 48 | |
| 49 | private CodecContext context; |
| 50 | private JsonCodec<LispAsAddress> asAddressCodec; |
| 51 | private LispMappingExtensionCodecRegistrator registrator; |
| 52 | |
| 53 | /** |
| 54 | * Sets up for each test. |
| 55 | * Creates a context and fetches the LispAsAddress codec. |
| 56 | */ |
| 57 | @Before |
| 58 | public void setUp() { |
| 59 | CodecManager manager = new CodecManager(); |
| 60 | registrator = new LispMappingExtensionCodecRegistrator(); |
| 61 | registrator.codecService = manager; |
| 62 | registrator.activate(); |
| 63 | |
| 64 | context = new LispMappingExtensionCodecContextAdapter(registrator.codecService); |
| 65 | asAddressCodec = context.codec(LispAsAddress.class); |
| 66 | assertThat("AS address codec should not be null", asAddressCodec, notNullValue()); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Deactivates the codec registrator. |
| 71 | */ |
| 72 | @After |
| 73 | public void tearDown() { |
| 74 | registrator.deactivate(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Tests encoding of a LispAsAddress object. |
| 79 | */ |
| 80 | @Test |
| 81 | public void testLispAsAddressEncode() { |
| 82 | LispAsAddress address = new LispAsAddress.Builder() |
| 83 | .withAsNumber(AS_NUMBER) |
| 84 | .withAddress(MappingAddresses.ipv4MappingAddress(IPV4_PREFIX)) |
| 85 | .build(); |
| 86 | ObjectNode addressJson = asAddressCodec.encode(address, context); |
| 87 | assertThat("errors in encoding AS address JSON", |
| 88 | addressJson, LispAsAddressJsonMatcher.matchesAsAddress(address)); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Tests decoding of a LispAsAddress JSON object. |
| 93 | */ |
| 94 | @Test |
| 95 | public void testLispAsAddressDecode() throws IOException { |
| 96 | LispAsAddress address = getLispAsAddress("LispAsAddress.json"); |
| 97 | |
| 98 | assertThat("incorrect AS number", address.getAsNumber(), is(AS_NUMBER)); |
| 99 | assertThat("incorrect mapping address", address.getAddress(), |
| 100 | is(MappingAddresses.ipv4MappingAddress(IPV4_PREFIX))); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Hamcrest matcher for LispAsAddress. |
| 105 | */ |
| 106 | public static final class LispAsAddressJsonMatcher |
| 107 | extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 108 | |
| 109 | private final LispAsAddress address; |
| 110 | |
| 111 | /** |
| 112 | * Default constructor. |
| 113 | * |
| 114 | * @param address LispAsAddress object |
| 115 | */ |
| 116 | private LispAsAddressJsonMatcher(LispAsAddress address) { |
| 117 | this.address = address; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 122 | |
| 123 | // check protocol |
| 124 | int jsonAsNumber = jsonNode.get(LispAsAddressCodec.AS_NUMBER).asInt(); |
| 125 | int asNumber = address.getAsNumber(); |
| 126 | if (jsonAsNumber != asNumber) { |
| 127 | description.appendText("AsNumber was " + jsonAsNumber); |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | // check address |
| 132 | MappingAddressJsonMatcher addressMatcher = |
| 133 | MappingAddressJsonMatcher.matchesMappingAddress(address.getAddress()); |
| 134 | return addressMatcher.matches(jsonNode.get(LispAppDataAddressCodec.ADDRESS)); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void describeTo(Description description) { |
| 139 | description.appendText(address.toString()); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Factory to allocate a LispAsAddress matcher. |
| 144 | * |
| 145 | * @param address LispAsAddress object we are looking for |
| 146 | * @return matcher |
| 147 | */ |
| 148 | public static LispAsAddressJsonMatcher matchesAsAddress(LispAsAddress address) { |
| 149 | return new LispAsAddressJsonMatcher(address); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Reads in a LispAsAddress from the given resource and decodes it. |
| 155 | * |
| 156 | * @param resourceName resource to use to read the JSON for the rule |
| 157 | * @return decoded LispAsAddress |
| 158 | * @throws IOException if processing the resource fails |
| 159 | */ |
| 160 | private LispAsAddress getLispAsAddress(String resourceName) throws IOException { |
| 161 | InputStream jsonStream = LispAsAddressCodecTest.class.getResourceAsStream(resourceName); |
| 162 | JsonNode json = context.mapper().readTree(jsonStream); |
| 163 | assertThat("JSON string should not be null", json, notNullValue()); |
| 164 | LispAsAddress asAddress = asAddressCodec.decode((ObjectNode) json, context); |
| 165 | assertThat("Decoded address should not be null", asAddress, notNullValue()); |
| 166 | return asAddress; |
| 167 | } |
| 168 | } |