Jian Li | b54d14b | 2017-03-28 21:34:34 +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 | */ |
Jian Li | 2e818b0 | 2017-04-12 19:28:30 +0900 | [diff] [blame] | 16 | package org.onosproject.mapping.codec; |
Jian Li | b54d14b | 2017-03-28 21:34:34 +0900 | [diff] [blame] | 17 | |
Jian Li | 124ecf0 | 2017-04-05 16:38:34 +0900 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Jian Li | b54d14b | 2017-03-28 21:34:34 +0900 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onosproject.codec.CodecContext; |
Jian Li | b54d14b | 2017-03-28 21:34:34 +0900 | [diff] [blame] | 24 | import org.onosproject.codec.JsonCodec; |
| 25 | import org.onosproject.codec.impl.CodecManager; |
| 26 | import org.onosproject.mapping.instructions.MappingInstruction; |
| 27 | import org.onosproject.mapping.instructions.MappingInstructions; |
| 28 | import org.onosproject.mapping.instructions.MulticastMappingInstruction; |
| 29 | import org.onosproject.mapping.instructions.UnicastMappingInstruction; |
Jian Li | 2e818b0 | 2017-04-12 19:28:30 +0900 | [diff] [blame] | 30 | import org.onosproject.mapping.MappingCodecRegistrator; |
Jian Li | b54d14b | 2017-03-28 21:34:34 +0900 | [diff] [blame] | 31 | |
Jian Li | 124ecf0 | 2017-04-05 16:38:34 +0900 | [diff] [blame] | 32 | import java.io.IOException; |
| 33 | import java.io.InputStream; |
| 34 | |
Jian Li | b54d14b | 2017-03-28 21:34:34 +0900 | [diff] [blame] | 35 | import static org.hamcrest.MatcherAssert.assertThat; |
Jian Li | 124ecf0 | 2017-04-05 16:38:34 +0900 | [diff] [blame] | 36 | import static org.hamcrest.Matchers.is; |
Jian Li | b54d14b | 2017-03-28 21:34:34 +0900 | [diff] [blame] | 37 | import static org.hamcrest.Matchers.notNullValue; |
Jian Li | 2e818b0 | 2017-04-12 19:28:30 +0900 | [diff] [blame] | 38 | import static org.onosproject.mapping.codec.MappingInstructionJsonMatcher.matchesInstruction; |
Jian Li | b54d14b | 2017-03-28 21:34:34 +0900 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Unit tests for MappingInstructionCodec. |
| 42 | */ |
| 43 | public class MappingInstructionCodecTest { |
| 44 | |
| 45 | private CodecContext context; |
| 46 | private JsonCodec<MappingInstruction> instructionCodec; |
| 47 | private MappingCodecRegistrator registrator; |
| 48 | |
| 49 | private static final int UNICAST_WEIGHT = 1; |
| 50 | private static final int UNICAST_PRIORITY = 1; |
| 51 | private static final int MULTICAST_WEIGHT = 2; |
| 52 | private static final int MULTICAST_PRIORITY = 2; |
| 53 | |
Jian Li | 124ecf0 | 2017-04-05 16:38:34 +0900 | [diff] [blame] | 54 | private static final String UNICAST_TYPE_STRING = "UNICAST"; |
| 55 | private static final String WEIGHT_SUBTYPE_STRING = "WEIGHT"; |
| 56 | |
Jian Li | b54d14b | 2017-03-28 21:34:34 +0900 | [diff] [blame] | 57 | /** |
| 58 | * Sets up for each test. |
| 59 | * Creates a context and fetches the mapping instruction codec. |
| 60 | */ |
| 61 | @Before |
| 62 | public void setUp() { |
| 63 | CodecManager manager = new CodecManager(); |
| 64 | registrator = new MappingCodecRegistrator(); |
| 65 | registrator.codecService = manager; |
| 66 | registrator.activate(); |
| 67 | |
Jian Li | fa69be6 | 2017-04-05 16:00:10 +0900 | [diff] [blame] | 68 | context = new MappingCodecContextAdapter(registrator.codecService); |
Jian Li | b54d14b | 2017-03-28 21:34:34 +0900 | [diff] [blame] | 69 | |
| 70 | instructionCodec = context.codec(MappingInstruction.class); |
| 71 | assertThat(instructionCodec, notNullValue()); |
| 72 | } |
| 73 | |
| 74 | @After |
| 75 | public void tearDown() { |
| 76 | registrator.deactivate(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Tests the encoding of unicast weight instruction. |
| 81 | */ |
| 82 | @Test |
| 83 | public void unicastWeightInstrutionTest() { |
| 84 | final UnicastMappingInstruction.WeightMappingInstruction instruction = |
| 85 | (UnicastMappingInstruction.WeightMappingInstruction) |
| 86 | MappingInstructions.unicastWeight(UNICAST_WEIGHT); |
| 87 | final ObjectNode instructionJson = |
| 88 | instructionCodec.encode(instruction, context); |
| 89 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Tests the encoding of unicast priority instruction. |
| 94 | */ |
| 95 | @Test |
| 96 | public void unicastPriorityInstructionTest() { |
| 97 | final UnicastMappingInstruction.PriorityMappingInstruction instruction = |
| 98 | (UnicastMappingInstruction.PriorityMappingInstruction) |
| 99 | MappingInstructions.unicastPriority(UNICAST_PRIORITY); |
| 100 | final ObjectNode instructionJson = |
| 101 | instructionCodec.encode(instruction, context); |
| 102 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Tests the encoding of multicast weight instruction. |
| 107 | */ |
| 108 | @Test |
| 109 | public void multicastWeightInstructionTest() { |
| 110 | final MulticastMappingInstruction.WeightMappingInstruction instruction = |
| 111 | (MulticastMappingInstruction.WeightMappingInstruction) |
| 112 | MappingInstructions.multicastWeight(MULTICAST_WEIGHT); |
| 113 | final ObjectNode instructionJson = |
| 114 | instructionCodec.encode(instruction, context); |
| 115 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Tests the encoding of multicast priority instruction. |
| 120 | */ |
| 121 | @Test |
| 122 | public void multicastPriorityInstructionTest() { |
| 123 | final MulticastMappingInstruction.PriorityMappingInstruction instruction = |
| 124 | (MulticastMappingInstruction.PriorityMappingInstruction) |
| 125 | MappingInstructions.multicastPriority(MULTICAST_PRIORITY); |
| 126 | final ObjectNode instructionJson = |
| 127 | instructionCodec.encode(instruction, context); |
Jian Li | fa69be6 | 2017-04-05 16:00:10 +0900 | [diff] [blame] | 128 | assertThat(instructionJson, matchesInstruction(instruction)); |
Jian Li | b54d14b | 2017-03-28 21:34:34 +0900 | [diff] [blame] | 129 | } |
Jian Li | 124ecf0 | 2017-04-05 16:38:34 +0900 | [diff] [blame] | 130 | |
| 131 | /** |
| 132 | * Tests the decoding of mapping instruction from JSON object. |
| 133 | * |
| 134 | * @throws IOException if processing the resource fails |
| 135 | */ |
| 136 | @Test |
| 137 | public void testMappingInstructionDecode() throws IOException { |
| 138 | UnicastMappingInstruction instruction = (UnicastMappingInstruction) getInstruction("MappingInstruction.json"); |
| 139 | assertThat(instruction.type().toString(), is(UNICAST_TYPE_STRING)); |
| 140 | assertThat(instruction.subtype().toString(), is(WEIGHT_SUBTYPE_STRING)); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Reads in a mapping instruction from the given resource and decodes it. |
| 145 | * |
| 146 | * @param resourceName resource to use to read the JSON for the rule |
| 147 | * @return decoded mappingInstruction |
| 148 | * @throws IOException if processing the resource fails |
| 149 | */ |
| 150 | private MappingInstruction getInstruction(String resourceName) throws IOException { |
| 151 | InputStream jsonStream = MappingInstructionCodecTest.class.getResourceAsStream(resourceName); |
| 152 | JsonNode json = context.mapper().readTree(jsonStream); |
| 153 | assertThat(json, notNullValue()); |
| 154 | MappingInstruction instruction = instructionCodec.decode((ObjectNode) json, context); |
| 155 | assertThat(instruction, notNullValue()); |
| 156 | return instruction; |
| 157 | } |
Jian Li | b54d14b | 2017-03-28 21:34:34 +0900 | [diff] [blame] | 158 | } |