Committed by
Ray Milkey
[ONOS-3161]Unit test for port pair codec
Change-Id: Id4fefe31c182cc076f616da1fceb9662522433e3
Showing
3 changed files
with
180 additions
and
0 deletions
1 | +/* | ||
2 | + * Copyright 2015 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 | + */ | ||
16 | +package org.onosproject.vtnweb.web; | ||
17 | + | ||
18 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
19 | +import static org.hamcrest.Matchers.is; | ||
20 | +import static org.hamcrest.Matchers.notNullValue; | ||
21 | + | ||
22 | +import java.io.IOException; | ||
23 | +import java.io.InputStream; | ||
24 | + | ||
25 | +import org.junit.Before; | ||
26 | +import org.junit.Test; | ||
27 | +import org.onosproject.codec.JsonCodec; | ||
28 | +import org.onosproject.vtnrsc.PortPair; | ||
29 | +import org.onosproject.vtnrsc.PortPairId; | ||
30 | +import org.onosproject.vtnrsc.TenantId; | ||
31 | + | ||
32 | +import com.fasterxml.jackson.databind.JsonNode; | ||
33 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
34 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
35 | + | ||
36 | +/** | ||
37 | + * Port pair codec unit tests. | ||
38 | + */ | ||
39 | +public class PortPairCodecTest { | ||
40 | + | ||
41 | + SfcCodecContext context; | ||
42 | + JsonCodec<PortPair> portPairCodec; | ||
43 | + /** | ||
44 | + * Sets up for each test. Creates a context and fetches the port pair | ||
45 | + * codec. | ||
46 | + */ | ||
47 | + @Before | ||
48 | + public void setUp() { | ||
49 | + context = new SfcCodecContext(); | ||
50 | + portPairCodec = context.codec(PortPair.class); | ||
51 | + assertThat(portPairCodec, notNullValue()); | ||
52 | + } | ||
53 | + | ||
54 | + /** | ||
55 | + * Reads in a port pair from the given resource and decodes it. | ||
56 | + * | ||
57 | + * @param resourceName resource to use to read the JSON for the port pair | ||
58 | + * @return decoded port pair | ||
59 | + * @throws IOException if processing the resource fails | ||
60 | + */ | ||
61 | + private PortPair getPortPair(String resourceName) throws IOException { | ||
62 | + InputStream jsonStream = PortPairCodecTest.class | ||
63 | + .getResourceAsStream(resourceName); | ||
64 | + ObjectMapper mapper = new ObjectMapper(); | ||
65 | + JsonNode json = mapper.readTree(jsonStream); | ||
66 | + assertThat(json, notNullValue()); | ||
67 | + PortPair portPair = portPairCodec.decode((ObjectNode) json, context); | ||
68 | + assertThat(portPair, notNullValue()); | ||
69 | + return portPair; | ||
70 | + } | ||
71 | + | ||
72 | + /** | ||
73 | + * Checks that a simple port pair decodes properly. | ||
74 | + * | ||
75 | + * @throws IOException if the resource cannot be processed | ||
76 | + */ | ||
77 | + @Test | ||
78 | + public void codecPortPairTest() throws IOException { | ||
79 | + | ||
80 | + PortPair portPair = getPortPair("portPair.json"); | ||
81 | + | ||
82 | + assertThat(portPair, notNullValue()); | ||
83 | + | ||
84 | + PortPairId portPairId = PortPairId.of("78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae"); | ||
85 | + TenantId tenantId = TenantId.tenantId("d382007aa9904763a801f68ecf065cf5"); | ||
86 | + | ||
87 | + assertThat(portPair.portPairId().toString(), is(portPairId.toString())); | ||
88 | + assertThat(portPair.name(), is("PP1")); | ||
89 | + assertThat(portPair.tenantId().toString(), is(tenantId.toString())); | ||
90 | + assertThat(portPair.description(), is("SF-A")); | ||
91 | + assertThat(portPair.ingress().toString(), is("dace4513-24fc-4fae-af4b-321c5e2eb3d1")); | ||
92 | + assertThat(portPair.egress().toString(), is("aef3478a-4a56-2a6e-cd3a-9dee4e2ec345")); | ||
93 | + } | ||
94 | +} |
1 | +/* | ||
2 | + * Copyright 2015 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 | + */ | ||
16 | +package org.onosproject.vtnweb.web; | ||
17 | + | ||
18 | +import java.util.Map; | ||
19 | +import java.util.concurrent.ConcurrentHashMap; | ||
20 | + | ||
21 | +import org.onosproject.codec.CodecContext; | ||
22 | +import org.onosproject.codec.JsonCodec; | ||
23 | +import org.onosproject.vtnrsc.FlowClassifier; | ||
24 | +import org.onosproject.vtnrsc.PortChain; | ||
25 | +import org.onosproject.vtnrsc.PortPair; | ||
26 | +import org.onosproject.vtnrsc.PortPairGroup; | ||
27 | + | ||
28 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
29 | + | ||
30 | +/** | ||
31 | + * Mock codec context for use in codec unit tests. | ||
32 | + */ | ||
33 | +public class SfcCodecContext implements CodecContext { | ||
34 | + | ||
35 | + private final ObjectMapper mapper = new ObjectMapper(); | ||
36 | + private final Map<Class<?>, JsonCodec> codecs = new ConcurrentHashMap<>(); | ||
37 | + | ||
38 | + /** | ||
39 | + * Constructs a new mock codec context. | ||
40 | + */ | ||
41 | + public SfcCodecContext() { | ||
42 | + codecs.clear(); | ||
43 | + registerCodec(PortPair.class, new PortPairCodec()); | ||
44 | + registerCodec(PortChain.class, new PortChainCodec()); | ||
45 | + registerCodec(PortPairGroup.class, new PortPairGroupCodec()); | ||
46 | + registerCodec(FlowClassifier.class, new FlowClassifierCodec()); | ||
47 | + } | ||
48 | + | ||
49 | + @Override | ||
50 | + public ObjectMapper mapper() { | ||
51 | + return mapper; | ||
52 | + } | ||
53 | + | ||
54 | + @SuppressWarnings("unchecked") | ||
55 | + @Override | ||
56 | + public <T> T getService(Class<T> serviceClass) { | ||
57 | + // TODO | ||
58 | + return null; | ||
59 | + } | ||
60 | + | ||
61 | + /** | ||
62 | + * Registers the specified JSON codec for the given entity class. | ||
63 | + * | ||
64 | + * @param entityClass entity class | ||
65 | + * @param codec JSON codec | ||
66 | + * @param <T> entity type | ||
67 | + */ | ||
68 | + public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) { | ||
69 | + codecs.putIfAbsent(entityClass, codec); | ||
70 | + } | ||
71 | + | ||
72 | + @SuppressWarnings("unchecked") | ||
73 | + @Override | ||
74 | + public <T> JsonCodec<T> codec(Class<T> entityClass) { | ||
75 | + return codecs.get(entityClass); | ||
76 | + } | ||
77 | +} |
-
Please register or login to post a comment