blob: 6b55fcf7307ba1185a1068f0d506382c7bca1e02 [file] [log] [blame]
Bharat saraswalf215b0c2015-11-14 15:58:46 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Bharat saraswalf215b0c2015-11-14 15:58:46 +05303 *
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.vtnweb.web;
17
18import static org.hamcrest.MatcherAssert.assertThat;
19import static org.hamcrest.Matchers.is;
20import static org.hamcrest.Matchers.notNullValue;
21
22import java.io.IOException;
23import java.io.InputStream;
24
25import org.junit.Before;
26import org.junit.Test;
27import org.onosproject.codec.JsonCodec;
28import org.onosproject.vtnrsc.FlowClassifier;
29import org.onosproject.vtnrsc.FlowClassifierId;
30import org.onosproject.vtnrsc.TenantId;
31
32import com.fasterxml.jackson.databind.JsonNode;
33import com.fasterxml.jackson.databind.ObjectMapper;
34import com.fasterxml.jackson.databind.node.ObjectNode;
35
36/**
37 * Flow classifier codec unit tests.
38 */
39public class FlowClassifierCodecTest {
40
41 SfcCodecContext context;
42 JsonCodec<FlowClassifier> flowClassifierCodec;
43 /**
44 * Sets up for each test. Creates a context and fetches the flow classifier
45 * codec.
46 */
47 @Before
48 public void setUp() {
49 context = new SfcCodecContext();
50 flowClassifierCodec = context.codec(FlowClassifier.class);
51 assertThat(flowClassifierCodec, notNullValue());
52 }
53
54 /**
55 * Reads in a flow classifier from the given resource and decodes it.
56 *
57 * @param resourceName resource to use to read the JSON for the flow classifier
58 * @return decoded flow classifier
59 * @throws IOException if processing the resource fails
60 */
61 private FlowClassifier getFlowClassifier(String resourceName) throws IOException {
62 InputStream jsonStream = FlowClassifierCodecTest.class
63 .getResourceAsStream(resourceName);
64 ObjectMapper mapper = new ObjectMapper();
65 JsonNode json = mapper.readTree(jsonStream);
66 assertThat(json, notNullValue());
67 FlowClassifier flowClassifier = flowClassifierCodec.decode((ObjectNode) json, context);
68 assertThat(flowClassifier, notNullValue());
69 return flowClassifier;
70 }
71
72 /**
73 * Checks that a simple flow classifier decodes properly.
74 *
75 * @throws IOException if the resource cannot be processed
76 */
77 @Test
78 public void codecFlowClassifierTest() throws IOException {
79
80 FlowClassifier flowClassifier = getFlowClassifier("flowClassifier.json");
81
82 assertThat(flowClassifier, notNullValue());
83
84 FlowClassifierId flowClassifierId = FlowClassifierId.of("4a334cd4-fe9c-4fae-af4b-321c5e2eb051");
85 TenantId tenantId = TenantId.tenantId("1814726e2d22407b8ca76db5e567dcf1");
86
87 assertThat(flowClassifier.flowClassifierId().toString(), is(flowClassifierId.toString()));
88 assertThat(flowClassifier.name(), is("flow1"));
89 assertThat(flowClassifier.tenantId().toString(), is(tenantId.toString()));
90 assertThat(flowClassifier.description(), is("flow classifier"));
91 assertThat(flowClassifier.protocol(), is("tcp"));
Phaneendra Manda299877f2016-04-13 23:28:03 +053092 assertThat(flowClassifier.priority(), is(65535));
Bharat saraswalf215b0c2015-11-14 15:58:46 +053093 assertThat(flowClassifier.minSrcPortRange(), is(22));
94 assertThat(flowClassifier.maxSrcPortRange(), is(4000));
95 assertThat(flowClassifier.minDstPortRange(), is(80));
96 assertThat(flowClassifier.maxDstPortRange(), is(80));
97
98 }
99}