Committed by
Gerrit Code Review
[ONOS-3164] UT for port chain codec
Change-Id: If2347f4862a548bc12b37df7ee878a922d48568a
Showing
2 changed files
with
109 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.PortChain; | ||
| 29 | +import org.onosproject.vtnrsc.PortChainId; | ||
| 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 | + * Flow rule codec unit tests. | ||
| 38 | + */ | ||
| 39 | +public class PortChainCodecTest { | ||
| 40 | + | ||
| 41 | + SfcCodecContext context; | ||
| 42 | + JsonCodec<PortChain> portChainCodec; | ||
| 43 | + /** | ||
| 44 | + * Sets up for each test. Creates a context and fetches the flow rule | ||
| 45 | + * codec. | ||
| 46 | + */ | ||
| 47 | + @Before | ||
| 48 | + public void setUp() { | ||
| 49 | + context = new SfcCodecContext(); | ||
| 50 | + portChainCodec = context.codec(PortChain.class); | ||
| 51 | + assertThat(portChainCodec, notNullValue()); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + /** | ||
| 55 | + * Reads in a rule from the given resource and decodes it. | ||
| 56 | + * | ||
| 57 | + * @param resourceName resource to use to read the JSON for the rule | ||
| 58 | + * @return decoded flow rule | ||
| 59 | + * @throws IOException if processing the resource fails | ||
| 60 | + */ | ||
| 61 | + private PortChain getPortChain(String resourceName) throws IOException { | ||
| 62 | + InputStream jsonStream = PortChainCodecTest.class | ||
| 63 | + .getResourceAsStream(resourceName); | ||
| 64 | + ObjectMapper mapper = new ObjectMapper(); | ||
| 65 | + JsonNode json = mapper.readTree(jsonStream); | ||
| 66 | + assertThat(json, notNullValue()); | ||
| 67 | + PortChain portChain = portChainCodec.decode((ObjectNode) json, context); | ||
| 68 | + assertThat(portChain, notNullValue()); | ||
| 69 | + return portChain; | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + /** | ||
| 73 | + * Checks that a simple rule decodes properly. | ||
| 74 | + * | ||
| 75 | + * @throws IOException if the resource cannot be processed | ||
| 76 | + */ | ||
| 77 | + @Test | ||
| 78 | + public void codecPortChainTest() throws IOException { | ||
| 79 | + | ||
| 80 | + PortChain portChain = getPortChain("portChain.json"); | ||
| 81 | + | ||
| 82 | + assertThat(portChain, notNullValue()); | ||
| 83 | + | ||
| 84 | + PortChainId portChainId = PortChainId.of("1278dcd4-459f-62ed-754b-87fc5e4a6751"); | ||
| 85 | + TenantId tenantId = TenantId.tenantId("d382007aa9904763a801f68ecf065cf5"); | ||
| 86 | + | ||
| 87 | + assertThat(portChain.portChainId().toString(), is(portChainId.toString())); | ||
| 88 | + assertThat(portChain.name(), is("PC2")); | ||
| 89 | + assertThat(portChain.tenantId().toString(), is(tenantId.toString())); | ||
| 90 | + assertThat(portChain.description(), is("Two flows and two port-pair-groups")); | ||
| 91 | + | ||
| 92 | + assertThat(portChain.flowClassifiers(), notNullValue()); | ||
| 93 | + assertThat(portChain.portPairGroups(), notNullValue()); | ||
| 94 | + } | ||
| 95 | +} |
| 1 | +{ | ||
| 2 | + "id": "1278dcd4-459f-62ed-754b-87fc5e4a6751", | ||
| 3 | + "name": "PC2", | ||
| 4 | + "tenant_id": "d382007aa9904763a801f68ecf065cf5", | ||
| 5 | + "description": "Two flows and two port-pair-groups", | ||
| 6 | + "flow_classifiers": [ | ||
| 7 | + "456a4a34-2e9c-14ae-37fb-765feae2eb05", | ||
| 8 | + "4a334cd4-fe9c-4fae-af4b-321c5e2eb051" | ||
| 9 | + ], | ||
| 10 | + "port_pair_groups": [ | ||
| 11 | + "4512d643-24fc-4fae-af4b-321c5e2eb3d1", | ||
| 12 | + "4a634d49-76dc-4fae-af4b-321c5e23d651" | ||
| 13 | + ] | ||
| 14 | +} |
-
Please register or login to post a comment