Jian Li | cb42c31 | 2017-03-30 16:20:33 +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 | cb42c31 | 2017-03-30 16:20:33 +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 | cb42c31 | 2017-03-30 16:20:33 +0900 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
Jian Li | cb42c31 | 2017-03-30 16:20:33 +0900 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import org.hamcrest.Description; |
| 22 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 23 | import org.junit.After; |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.onosproject.codec.CodecContext; |
Jian Li | cb42c31 | 2017-03-30 16:20:33 +0900 | [diff] [blame] | 27 | import org.onosproject.codec.JsonCodec; |
| 28 | import org.onosproject.codec.impl.CodecManager; |
| 29 | import org.onosproject.mapping.DefaultMappingTreatment; |
| 30 | import org.onosproject.mapping.MappingTreatment; |
| 31 | import org.onosproject.mapping.addresses.MappingAddress; |
| 32 | import org.onosproject.mapping.addresses.MappingAddresses; |
| 33 | import org.onosproject.mapping.instructions.MappingInstruction; |
| 34 | import org.onosproject.mapping.instructions.MappingInstructions; |
Jian Li | 2e818b0 | 2017-04-12 19:28:30 +0900 | [diff] [blame] | 35 | import org.onosproject.mapping.MappingCodecRegistrator; |
Jian Li | cb42c31 | 2017-03-30 16:20:33 +0900 | [diff] [blame] | 36 | |
| 37 | import java.io.IOException; |
| 38 | import java.io.InputStream; |
| 39 | import java.util.List; |
| 40 | |
| 41 | import static org.hamcrest.MatcherAssert.assertThat; |
| 42 | import static org.hamcrest.Matchers.is; |
| 43 | import static org.hamcrest.Matchers.notNullValue; |
| 44 | |
| 45 | /** |
| 46 | * Unit tests for MappingTreatmentCodec. |
| 47 | */ |
| 48 | public class MappingTreatmentCodecTest { |
| 49 | |
| 50 | private static final String INSTRUCTIONS = "instructions"; |
| 51 | private static final String TYPE = "type"; |
| 52 | |
| 53 | private static final int UNICAST_WEIGHT = 1; |
| 54 | private static final int UNICAST_PRIORITY = 1; |
| 55 | private static final int MULTICAST_WEIGHT = 2; |
| 56 | private static final int MULTICAST_PRIORITY = 2; |
| 57 | private static final String ASN_NUMBER = "ASN2000"; |
| 58 | |
| 59 | private CodecContext context; |
| 60 | private JsonCodec<MappingTreatment> treatmentCodec; |
| 61 | private MappingCodecRegistrator registrator; |
| 62 | |
| 63 | /** |
| 64 | * Sets up for each test. |
| 65 | * Creates a context and fetches the mapping action codec. |
| 66 | */ |
| 67 | @Before |
| 68 | public void setUp() { |
| 69 | CodecManager manager = new CodecManager(); |
| 70 | registrator = new MappingCodecRegistrator(); |
| 71 | registrator.codecService = manager; |
| 72 | registrator.activate(); |
| 73 | |
Jian Li | fa69be6 | 2017-04-05 16:00:10 +0900 | [diff] [blame] | 74 | context = new MappingCodecContextAdapter(registrator.codecService); |
Jian Li | cb42c31 | 2017-03-30 16:20:33 +0900 | [diff] [blame] | 75 | treatmentCodec = context.codec(MappingTreatment.class); |
| 76 | assertThat(treatmentCodec, notNullValue()); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Deactivates the codec registrator. |
| 81 | */ |
| 82 | @After |
| 83 | public void tearDown() { |
| 84 | registrator.deactivate(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Tests encoding of a mapping treatment object. |
| 89 | */ |
| 90 | @Test |
| 91 | public void testMappingTreatmentEncode() { |
| 92 | MappingInstruction unicastWeight = MappingInstructions.unicastWeight(UNICAST_WEIGHT); |
| 93 | MappingInstruction unicastPriority = MappingInstructions.unicastPriority(UNICAST_PRIORITY); |
| 94 | MappingInstruction multicastWeight = MappingInstructions.multicastWeight(MULTICAST_WEIGHT); |
| 95 | MappingInstruction multicastPriority = MappingInstructions.multicastPriority(MULTICAST_PRIORITY); |
| 96 | |
| 97 | MappingAddress address = MappingAddresses.asMappingAddress(ASN_NUMBER); |
| 98 | |
| 99 | MappingTreatment treatment = DefaultMappingTreatment.builder() |
| 100 | .add(unicastWeight) |
| 101 | .add(unicastPriority) |
| 102 | .add(multicastWeight) |
| 103 | .add(multicastPriority) |
| 104 | .withAddress(address) |
| 105 | .build(); |
| 106 | |
| 107 | ObjectNode treatmentJson = treatmentCodec.encode(treatment, context); |
| 108 | assertThat(treatmentJson, MappingTreatmentJsonMatcher.matchesMappingTreatment(treatment)); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Tests decoding of a mapping treatment JSON object. |
| 113 | */ |
| 114 | @Test |
| 115 | public void testMappingTreatmentDecode() throws IOException { |
| 116 | MappingTreatment treatment = getTreatment("MappingTreatment.json"); |
| 117 | |
| 118 | List<MappingInstruction> insts = treatment.instructions(); |
| 119 | assertThat(insts.size(), is(2)); |
| 120 | |
| 121 | ImmutableSet<String> types = ImmutableSet.of("UNICAST", "MULTICAST"); |
| 122 | assertThat(types.contains(insts.get(0).type().name()), is(true)); |
| 123 | assertThat(types.contains(insts.get(1).type().name()), is(true)); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Hamcrest matcher for mapping treatment. |
| 128 | */ |
| 129 | public static final class MappingTreatmentJsonMatcher |
| 130 | extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 131 | |
| 132 | private final MappingTreatment mappingTreatment; |
| 133 | |
| 134 | /** |
| 135 | * A default constructor. |
| 136 | * |
| 137 | * @param mappingTreatment mapping treatment |
| 138 | */ |
| 139 | private MappingTreatmentJsonMatcher(MappingTreatment mappingTreatment) { |
| 140 | this.mappingTreatment = mappingTreatment; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 145 | |
| 146 | // check mapping instruction |
| 147 | final JsonNode jsonInstructions = jsonNode.get(INSTRUCTIONS); |
| 148 | if (mappingTreatment.instructions().size() != jsonInstructions.size()) { |
| 149 | description.appendText("mapping instructions array size of " + |
| 150 | Integer.toString(mappingTreatment.instructions().size())); |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | for (final MappingInstruction instruction : mappingTreatment.instructions()) { |
| 155 | boolean instructionFound = false; |
| 156 | for (int instructionIdx = 0; instructionIdx < jsonInstructions.size(); |
| 157 | instructionIdx++) { |
| 158 | final String jsonType = |
| 159 | jsonInstructions.get(instructionIdx).get(TYPE).asText(); |
| 160 | final String instructionType = instruction.type().name(); |
| 161 | if (jsonType.equals(instructionType)) { |
| 162 | instructionFound = true; |
| 163 | } |
| 164 | } |
| 165 | if (!instructionFound) { |
| 166 | description.appendText("mapping instruction " + instruction.toString()); |
| 167 | return false; |
| 168 | } |
| 169 | } |
| 170 | |
Jian Li | 5a577a3 | 2017-04-04 12:37:53 +0900 | [diff] [blame] | 171 | // check address |
| 172 | final JsonNode jsonAddressNode = jsonNode.get(MappingKeyCodec.ADDRESS); |
| 173 | |
| 174 | assertThat(jsonAddressNode, |
| 175 | MappingAddressJsonMatcher.matchesMappingAddress(mappingTreatment.address())); |
| 176 | |
Jian Li | cb42c31 | 2017-03-30 16:20:33 +0900 | [diff] [blame] | 177 | return true; |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public void describeTo(Description description) { |
| 182 | description.appendText(mappingTreatment.toString()); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Factory to allocate a mapping treatment. |
| 187 | * |
| 188 | * @param mappingTreatment mapping treatment object we are looking for |
| 189 | * @return matcher |
| 190 | */ |
| 191 | static MappingTreatmentJsonMatcher matchesMappingTreatment(MappingTreatment mappingTreatment) { |
| 192 | return new MappingTreatmentJsonMatcher(mappingTreatment); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /** |
Jian Li | cb42c31 | 2017-03-30 16:20:33 +0900 | [diff] [blame] | 197 | * Reads in a mapping treatment from the given resource and decodes it. |
| 198 | * |
| 199 | * @param resourceName resource to use to read the JSON for the rule |
| 200 | * @return decoded mappingTreatment |
| 201 | * @throws IOException if processing the resource fails |
| 202 | */ |
| 203 | private MappingTreatment getTreatment(String resourceName) throws IOException { |
| 204 | InputStream jsonStream = MappingTreatmentCodecTest.class.getResourceAsStream(resourceName); |
| 205 | JsonNode json = context.mapper().readTree(jsonStream); |
| 206 | assertThat(json, notNullValue()); |
| 207 | MappingTreatment treatment = treatmentCodec.decode((ObjectNode) json, context); |
| 208 | assertThat(treatment, notNullValue()); |
| 209 | return treatment; |
| 210 | } |
| 211 | } |