Jian Li | 5baef51 | 2017-04-10 20:25:18 +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.LispSegmentAddress; |
| 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 LispSegmentAddressCodec. |
| 43 | */ |
| 44 | public class LispSegmentAddressCodecTest { |
| 45 | |
| 46 | private static final int INSTANCE_ID = 1; |
| 47 | private static final IpPrefix IPV4_PREFIX = IpPrefix.valueOf("10.1.1.0/24"); |
| 48 | |
| 49 | private CodecContext context; |
| 50 | private JsonCodec<LispSegmentAddress> segmentAddressCodec; |
| 51 | private LispMappingExtensionCodecRegistrator registrator; |
| 52 | |
| 53 | /** |
| 54 | * Sets up for each test. |
| 55 | * Creates a context and fetches the LispSegmentAddress 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 | segmentAddressCodec = context.codec(LispSegmentAddress.class); |
| 66 | assertThat("segment address codec should not be null", |
| 67 | segmentAddressCodec, notNullValue()); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Deactivates the codec registrator. |
| 72 | */ |
| 73 | @After |
| 74 | public void tearDown() { |
| 75 | registrator.deactivate(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Tests encoding of a LispSegmentAddress object. |
| 80 | */ |
| 81 | @Test |
| 82 | public void testLispSegmentAddressEncode() { |
| 83 | LispSegmentAddress address = new LispSegmentAddress.Builder() |
| 84 | .withInstanceId(INSTANCE_ID) |
| 85 | .withAddress(MappingAddresses.ipv4MappingAddress(IPV4_PREFIX)) |
| 86 | .build(); |
| 87 | ObjectNode addressJson = segmentAddressCodec.encode(address, context); |
| 88 | assertThat("errors in encoding segment address JSON", |
| 89 | addressJson, LispSegmentAddressJsonMatcher.matchesSegmentAddress(address)); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Tests decoding of a LispSegmentAddress JSON object. |
| 94 | */ |
| 95 | @Test |
| 96 | public void testLispSegmentAddressDecode() throws IOException { |
| 97 | LispSegmentAddress address = getLispSegmentAddress("LispSegmentAddress.json"); |
| 98 | |
| 99 | assertThat("incorrect instance ID", address.getInstanceId(), is(INSTANCE_ID)); |
| 100 | assertThat("incorrect mapping address", address.getAddress(), |
| 101 | is(MappingAddresses.ipv4MappingAddress(IPV4_PREFIX))); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Hamcrest matcher for LispSegmentAddress. |
| 106 | */ |
| 107 | public static final class LispSegmentAddressJsonMatcher |
| 108 | extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 109 | |
| 110 | private final LispSegmentAddress address; |
| 111 | |
| 112 | /** |
| 113 | * Default constructor. |
| 114 | * |
| 115 | * @param address LispSegmentAddress object |
| 116 | */ |
| 117 | private LispSegmentAddressJsonMatcher(LispSegmentAddress address) { |
| 118 | this.address = address; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 123 | |
| 124 | // check instance ID |
| 125 | int jsonInstanceId = jsonNode.get(LispSegmentAddressCodec.INSTANCE_ID).asInt(); |
| 126 | int instanceId = address.getInstanceId(); |
| 127 | if (jsonInstanceId != instanceId) { |
| 128 | description.appendText("Instance ID was " + jsonInstanceId); |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | // check address |
| 133 | MappingAddressJsonMatcher addressMatcher = |
| 134 | MappingAddressJsonMatcher.matchesMappingAddress(address.getAddress()); |
| 135 | |
| 136 | return addressMatcher.matches(jsonNode.get(LispSegmentAddressCodec.ADDRESS)); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public void describeTo(Description description) { |
| 141 | description.appendText(address.toString()); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Factory to allocate a LispSegmentAddress matcher. |
| 146 | * |
| 147 | * @param address LispSegmentAddress object we are looking for |
| 148 | * @return matcher |
| 149 | */ |
| 150 | public static LispSegmentAddressJsonMatcher matchesSegmentAddress(LispSegmentAddress address) { |
| 151 | return new LispSegmentAddressJsonMatcher(address); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Reads in a LispSegmentAddress from the given resource and decodes it. |
| 157 | * |
| 158 | * @param resourceName resource to use to read the JSON for the rule |
| 159 | * @return decoded LispSegmentAddress |
| 160 | * @throws IOException if processing the resource fails |
| 161 | */ |
| 162 | private LispSegmentAddress getLispSegmentAddress(String resourceName) throws IOException { |
| 163 | InputStream jsonStream = LispSegmentAddressCodecTest.class.getResourceAsStream(resourceName); |
| 164 | JsonNode json = context.mapper().readTree(jsonStream); |
| 165 | assertThat("JSON string should not be null", json, notNullValue()); |
| 166 | LispSegmentAddress segmentAddress = segmentAddressCodec.decode((ObjectNode) json, context); |
| 167 | assertThat("decoded address should not be null", segmentAddress, notNullValue()); |
| 168 | return segmentAddress; |
| 169 | } |
| 170 | } |