Jian Li | 5a577a3 | 2017-04-04 12:37:53 +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 | 5a577a3 | 2017-04-04 12:37:53 +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 | 5a577a3 | 2017-04-04 12:37:53 +0900 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
Jian Li | 5a577a3 | 2017-04-04 12:37:53 +0900 | [diff] [blame] | 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; |
Jian Li | 5a577a3 | 2017-04-04 12:37:53 +0900 | [diff] [blame] | 27 | import org.onosproject.codec.JsonCodec; |
| 28 | import org.onosproject.codec.impl.CodecManager; |
| 29 | import org.onosproject.mapping.DefaultMappingKey; |
| 30 | import org.onosproject.mapping.MappingKey; |
| 31 | import org.onosproject.mapping.addresses.MappingAddress; |
| 32 | import org.onosproject.mapping.addresses.MappingAddresses; |
Jian Li | 2e818b0 | 2017-04-12 19:28:30 +0900 | [diff] [blame] | 33 | import org.onosproject.mapping.MappingCodecRegistrator; |
Jian Li | 5a577a3 | 2017-04-04 12:37:53 +0900 | [diff] [blame] | 34 | |
| 35 | import java.io.IOException; |
| 36 | import java.io.InputStream; |
| 37 | |
| 38 | import static org.hamcrest.MatcherAssert.assertThat; |
| 39 | import static org.hamcrest.Matchers.is; |
| 40 | import static org.hamcrest.Matchers.notNullValue; |
| 41 | |
| 42 | /** |
| 43 | * Unit tests for MappingKeyCodec. |
| 44 | */ |
| 45 | public class MappingKeyCodecTest { |
| 46 | |
| 47 | private static final String IPV4_STRING = "1.2.3.4"; |
| 48 | private static final String PORT_STRING = "32"; |
| 49 | private static final IpPrefix IPV4_PREFIX = |
| 50 | IpPrefix.valueOf(IPV4_STRING + "/" + PORT_STRING); |
| 51 | |
| 52 | private CodecContext context; |
| 53 | private JsonCodec<MappingKey> keyCodec; |
| 54 | private MappingCodecRegistrator registrator; |
| 55 | |
| 56 | /** |
| 57 | * Sets up for each test. |
| 58 | * Creates a context and fetches the mapping key codec. |
| 59 | */ |
| 60 | @Before |
| 61 | public void setUp() { |
| 62 | CodecManager manager = new CodecManager(); |
| 63 | registrator = new MappingCodecRegistrator(); |
| 64 | registrator.codecService = manager; |
| 65 | registrator.activate(); |
| 66 | |
Jian Li | fa69be6 | 2017-04-05 16:00:10 +0900 | [diff] [blame] | 67 | context = new MappingCodecContextAdapter(registrator.codecService); |
Jian Li | 5a577a3 | 2017-04-04 12:37:53 +0900 | [diff] [blame] | 68 | keyCodec = context.codec(MappingKey.class); |
| 69 | assertThat(keyCodec, notNullValue()); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Deactivates the codec registrator. |
| 74 | */ |
| 75 | @After |
| 76 | public void tearDown() { |
| 77 | registrator.deactivate(); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Tests encoding of a mapping key object. |
| 82 | */ |
| 83 | @Test |
| 84 | public void testMappingKeyEncode() { |
| 85 | |
| 86 | MappingAddress address = MappingAddresses.ipv4MappingAddress(IPV4_PREFIX); |
| 87 | |
| 88 | MappingKey key = DefaultMappingKey.builder() |
| 89 | .withAddress(address) |
| 90 | .build(); |
| 91 | |
| 92 | ObjectNode keyJson = keyCodec.encode(key, context); |
| 93 | assertThat(keyJson, MappingKeyJsonMatcher.matchesMappingKey(key)); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Tests decoding of a mapping key JSON object. |
| 98 | */ |
| 99 | @Test |
| 100 | public void testMappingKeyDecode() throws IOException { |
| 101 | MappingKey key = getKey("MappingKey.json"); |
| 102 | assertThat(key.address().toString(), |
| 103 | is("IPV4:" + IPV4_STRING + "/" + PORT_STRING)); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Hamcrest matcher for mapping key. |
| 108 | */ |
| 109 | public static final class MappingKeyJsonMatcher |
| 110 | extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 111 | |
| 112 | private final MappingKey mappingKey; |
| 113 | |
| 114 | /** |
| 115 | * A default constructor. |
| 116 | * |
| 117 | * @param mappingKey mapping key |
| 118 | */ |
| 119 | private MappingKeyJsonMatcher(MappingKey mappingKey) { |
| 120 | this.mappingKey = mappingKey; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 125 | |
| 126 | // check address |
| 127 | final JsonNode jsonAddressNode = jsonNode.get(MappingKeyCodec.ADDRESS); |
| 128 | |
| 129 | assertThat(jsonAddressNode, |
| 130 | MappingAddressJsonMatcher.matchesMappingAddress(mappingKey.address())); |
| 131 | |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void describeTo(Description description) { |
| 137 | description.appendText(mappingKey.toString()); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Factory to allocate a mapping treatment. |
| 142 | * |
| 143 | * @param mappingKey mapping treatment object we are looking for |
| 144 | * @return matcher |
| 145 | */ |
| 146 | static MappingKeyJsonMatcher matchesMappingKey(MappingKey mappingKey) { |
| 147 | return new MappingKeyJsonMatcher(mappingKey); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
Jian Li | 5a577a3 | 2017-04-04 12:37:53 +0900 | [diff] [blame] | 152 | * Reads in a mapping key from the given resource and decodes it. |
| 153 | * |
| 154 | * @param resourceName resource to use to read the JSON for the rule |
| 155 | * @return decoded mappingKey |
| 156 | * @throws IOException if processing the resource fails |
| 157 | */ |
| 158 | private MappingKey getKey(String resourceName) throws IOException { |
| 159 | InputStream jsonStream = MappingKeyCodecTest.class.getResourceAsStream(resourceName); |
| 160 | JsonNode json = context.mapper().readTree(jsonStream); |
| 161 | assertThat(json, notNullValue()); |
| 162 | MappingKey key = keyCodec.decode((ObjectNode) json, context); |
| 163 | assertThat(key, notNullValue()); |
| 164 | return key; |
| 165 | } |
| 166 | } |