blob: 5db51e7bd92d14dbb70fb94681bb658c4aaba79e [file] [log] [blame]
Jian Licb42c312017-03-30 16:20:33 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Licb42c312017-03-30 16:20:33 +09003 *
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 Li2e818b02017-04-12 19:28:30 +090016package org.onosproject.mapping.codec;
Jian Licb42c312017-03-30 16:20:33 +090017
18import com.fasterxml.jackson.databind.JsonNode;
Jian Licb42c312017-03-30 16:20:33 +090019import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.ImmutableSet;
21import org.hamcrest.Description;
22import org.hamcrest.TypeSafeDiagnosingMatcher;
23import org.junit.After;
24import org.junit.Before;
25import org.junit.Test;
26import org.onosproject.codec.CodecContext;
Jian Licb42c312017-03-30 16:20:33 +090027import org.onosproject.codec.JsonCodec;
28import org.onosproject.codec.impl.CodecManager;
29import org.onosproject.mapping.DefaultMappingTreatment;
30import org.onosproject.mapping.MappingTreatment;
31import org.onosproject.mapping.addresses.MappingAddress;
32import org.onosproject.mapping.addresses.MappingAddresses;
33import org.onosproject.mapping.instructions.MappingInstruction;
34import org.onosproject.mapping.instructions.MappingInstructions;
Jian Li2e818b02017-04-12 19:28:30 +090035import org.onosproject.mapping.MappingCodecRegistrator;
Jian Licb42c312017-03-30 16:20:33 +090036
37import java.io.IOException;
38import java.io.InputStream;
39import java.util.List;
40
41import static org.hamcrest.MatcherAssert.assertThat;
42import static org.hamcrest.Matchers.is;
43import static org.hamcrest.Matchers.notNullValue;
44
45/**
46 * Unit tests for MappingTreatmentCodec.
47 */
48public 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 Lifa69be62017-04-05 16:00:10 +090074 context = new MappingCodecContextAdapter(registrator.codecService);
Jian Licb42c312017-03-30 16:20:33 +090075 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 Li5a577a32017-04-04 12:37:53 +0900171 // check address
172 final JsonNode jsonAddressNode = jsonNode.get(MappingKeyCodec.ADDRESS);
173
174 assertThat(jsonAddressNode,
175 MappingAddressJsonMatcher.matchesMappingAddress(mappingTreatment.address()));
176
Jian Licb42c312017-03-30 16:20:33 +0900177 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 Licb42c312017-03-30 16:20:33 +0900197 * 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}