blob: 48e862f7eb0c9e80fc6ccf9c9b9bfbe616eceeff [file] [log] [blame]
Jian Li2d3a1d12017-04-05 00:56:54 +09001/*
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 */
16package org.onosproject.mapping.web.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.hamcrest.Description;
22import org.hamcrest.TypeSafeDiagnosingMatcher;
23import org.junit.After;
24import org.junit.Before;
25import org.junit.Test;
26import org.onlab.packet.IpPrefix;
27import org.onosproject.codec.CodecContext;
28import org.onosproject.codec.CodecService;
29import org.onosproject.codec.JsonCodec;
30import org.onosproject.codec.impl.CodecManager;
31import org.onosproject.mapping.DefaultMappingTreatment;
32import org.onosproject.mapping.DefaultMappingValue;
33import org.onosproject.mapping.MappingTreatment;
34import org.onosproject.mapping.MappingValue;
35import org.onosproject.mapping.actions.MappingAction;
36import org.onosproject.mapping.actions.MappingActions;
37import org.onosproject.mapping.addresses.MappingAddress;
38import org.onosproject.mapping.addresses.MappingAddresses;
39import org.onosproject.mapping.instructions.MappingInstruction;
40import org.onosproject.mapping.instructions.MappingInstructions;
41import org.onosproject.mapping.web.MappingCodecRegistrator;
42
43import java.io.IOException;
44import java.io.InputStream;
45
46import static org.hamcrest.MatcherAssert.assertThat;
47import static org.hamcrest.Matchers.is;
48import static org.hamcrest.Matchers.notNullValue;
49
50/**
51 * Unit tests for MappingValueCodec.
52 */
53public class MappingValueCodecTest {
54
55 private static final String TREATMENTS = "treatments";
56 private static final String ADDRESS = "address";
57 private static final String IPV4_STRING = "1.2.3.4";
58 private static final String PORT_STRING = "32";
59 private static final IpPrefix IPV4_PREFIX =
60 IpPrefix.valueOf(IPV4_STRING + "/" + PORT_STRING);
61
62 private static final int UNICAST_WEIGHT = 1;
63 private static final int UNICAST_PRIORITY = 1;
64 private static final int MULTICAST_WEIGHT = 2;
65 private static final int MULTICAST_PRIORITY = 2;
66
67 private CodecContext context;
68 private JsonCodec<MappingValue> valueCodec;
69 private MappingCodecRegistrator registrator;
70
71 /**
72 * Sets up for each test.
73 * Creates a context and fetches the mapping value codec.
74 */
75 @Before
76 public void setUp() {
77 CodecManager manager = new CodecManager();
78 registrator = new MappingCodecRegistrator();
79 registrator.codecService = manager;
80 registrator.activate();
81
82 context = new MappingValueCodecTest.MappingTestContext(registrator.codecService);
83 valueCodec = context.codec(MappingValue.class);
84 assertThat(valueCodec, notNullValue());
85 }
86
87 /**
88 * Deactivates the codec registrator.
89 */
90 @After
91 public void tearDown() {
92 registrator.deactivate();
93 }
94
95 /**
96 * Tests encoding of a mapping value object.
97 */
98 @Test
99 public void testMapingValueEncode() {
100 MappingInstruction unicastWeight = MappingInstructions.unicastWeight(UNICAST_WEIGHT);
101 MappingInstruction unicastPriority = MappingInstructions.unicastPriority(UNICAST_PRIORITY);
102 MappingInstruction multicastWeight = MappingInstructions.multicastWeight(MULTICAST_WEIGHT);
103 MappingInstruction multicastPriority = MappingInstructions.multicastPriority(MULTICAST_PRIORITY);
104
105 MappingAddress address = MappingAddresses.ipv4MappingAddress(IPV4_PREFIX);
106
107 MappingTreatment treatment = DefaultMappingTreatment.builder()
108 .add(unicastWeight)
109 .add(unicastPriority)
110 .add(multicastWeight)
111 .add(multicastPriority)
112 .withAddress(address)
113 .build();
114
115 MappingAction action = MappingActions.noAction();
116
117 MappingValue value = DefaultMappingValue.builder()
118 .add(treatment)
119 .withAction(action)
120 .build();
121
122 ObjectNode valueJson = valueCodec.encode(value, context);
123 assertThat(valueJson, MappingValueJsonMatcher.matchesMappingValue(value));
124 }
125
126 /**
127 * Tests decoding of a mapping value JSON object.
128 */
129 @Test
130 public void testMappingValueDecode() throws IOException {
131 MappingValue value = getValue("MappingValue.json");
132 assertThat(value.treatments().get(0).address().toString(),
133 is("IPV4:" + IPV4_STRING + "/" + PORT_STRING));
134 }
135
136
137 /**
138 * Hamcrest matcher for mapping value.
139 */
140 public static final class MappingValueJsonMatcher
141 extends TypeSafeDiagnosingMatcher<JsonNode> {
142
143 private final MappingValue mappingValue;
144
145 /**
146 * A default constructor.
147 *
148 * @param mappingValue mapping value
149 */
150 private MappingValueJsonMatcher(MappingValue mappingValue) {
151 this.mappingValue = mappingValue;
152 }
153
154 @Override
155 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
156
157 // check mapping treatments
158 final JsonNode jsonTreatments = jsonNode.get(TREATMENTS);
159 if (mappingValue.treatments().size() != jsonTreatments.size()) {
160 description.appendText("mapping treatments array size of " +
161 Integer.toString(mappingValue.treatments().size()));
162 return false;
163 }
164
165 for (final MappingTreatment treatment : mappingValue.treatments()) {
166 boolean treatmentFound = false;
167 for (int treatmentIdx = 0; treatmentIdx < jsonTreatments.size();
168 treatmentIdx++) {
169 final String jsonAddress =
170 jsonTreatments.get(treatmentIdx).get(ADDRESS)
171 .get(MappingAddressCodec.IPV4).textValue();
172 final String address = treatment.address().toString();
173 if (address.contains(jsonAddress)) {
174 treatmentFound = true;
175 }
176 }
177
178 if (!treatmentFound) {
179 description.appendText("mapping treatment " + treatment.toString());
180 return false;
181 }
182 }
183
184 // check mapping action
185 final JsonNode jsonActionNode = jsonNode.get(MappingValueCodec.ACTION);
186
187 assertThat(jsonActionNode, MappingActionJsonMatcher.matchesAction(mappingValue.action()));
188
189 return true;
190 }
191
192 @Override
193 public void describeTo(Description description) {
194 description.appendText(mappingValue.toString());
195 }
196
197 /**
198 * Factory to allocate a mapping value.
199 *
200 * @param mappingValue mapping value object we are looking for
201 * @return matcher
202 */
203 static MappingValueJsonMatcher matchesMappingValue(MappingValue mappingValue) {
204 return new MappingValueJsonMatcher(mappingValue);
205 }
206 }
207
208 /**
209 * Test mapping codec context.
210 */
211 private class MappingTestContext implements CodecContext {
212 private final ObjectMapper mapper = new ObjectMapper();
213 private final CodecService manager;
214
215 /**
216 * Constructs a new mock codec context.
217 */
218 public MappingTestContext(CodecService manager) {
219 this.manager = manager;
220 }
221
222 @Override
223 public ObjectMapper mapper() {
224 return mapper;
225 }
226
227 @Override
228 public <T> JsonCodec<T> codec(Class<T> entityClass) {
229 return manager.getCodec(entityClass);
230 }
231
232 @Override
233 public <T> T getService(Class<T> serviceClass) {
234 return null;
235 }
236 }
237
238 /**
239 * Reads in a mapping value from the given resource and decodes it.
240 *
241 * @param resourceName resource to use to read the JSON for the rule
242 * @return decoded mappingKey
243 * @throws IOException if processing the resource fails
244 */
245 private MappingValue getValue(String resourceName) throws IOException {
246 InputStream jsonStream = MappingValueCodecTest.class.getResourceAsStream(resourceName);
247 JsonNode json = context.mapper().readTree(jsonStream);
248 assertThat(json, notNullValue());
249 MappingValue value = valueCodec.decode((ObjectNode) json, context);
250 assertThat(value, notNullValue());
251 return value;
252 }
253}