blob: 2aaa796e9babdff1a62a45afd6bd623b067de6aa [file] [log] [blame]
Yuta HIGUCHI776f0742016-11-03 15:51:14 -07001/*
2 * Copyright 2016-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.codec.impl;
17
18import static org.hamcrest.MatcherAssert.assertThat;
19import static org.hamcrest.Matchers.*;
20import static org.junit.Assert.*;
21
22import java.io.IOException;
23
24import org.junit.Before;
25import org.junit.Test;
26import org.onlab.packet.VlanId;
27import org.onosproject.codec.CodecContext;
28import org.onosproject.codec.JsonCodec;
29import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.DeviceId;
31import org.onosproject.net.FilteredConnectPoint;
32import org.onosproject.net.PortNumber;
33import org.onosproject.net.flow.DefaultTrafficSelector;
34import org.onosproject.net.flow.TrafficSelector;
35
36import com.fasterxml.jackson.core.JsonProcessingException;
37import com.fasterxml.jackson.databind.DeserializationFeature;
38import com.fasterxml.jackson.databind.JsonNode;
39import com.fasterxml.jackson.databind.node.JsonNodeFactory;
40import com.fasterxml.jackson.databind.node.NumericNode;
41import com.fasterxml.jackson.databind.node.ObjectNode;
42
43/**
44 * Unit tests for {@link FilteredConnectPointCodec}.
45 */
46public class FilteredConnectPointCodecTest {
47
48 private static final VlanId VID_56 = VlanId.vlanId((short) 56);
49 private static final TrafficSelector VLAN_SELECTOR
50 = DefaultTrafficSelector.builder().matchVlanId(VID_56).build();
51 private static final PortNumber PN_42 = PortNumber.portNumber(42);
52 private static final DeviceId DID = DeviceId.deviceId("test:device");
53 private static final ConnectPoint CP = new ConnectPoint(DID, PN_42);
54
55 private static final String JSON
56 = "{"
57 + "\"connectPoint\":{\"port\":\"42\",\"device\":\"test:device\"},"
58 + "\"trafficSelector\":{\"criteria\":[{\"type\":\"VLAN_VID\",\"vlanId\":56}]}"
59 + "}";
60
61 private FilteredConnectPointCodec sut;
62 private CodecContext context;
63
64 @Before
65 public void setUp() {
66 context = new MockCodecContext();
67 JsonCodec<FilteredConnectPoint> codec = context.codec(FilteredConnectPoint.class);
68 assertThat(codec, instanceOf(FilteredConnectPointCodec.class));
69 sut = (FilteredConnectPointCodec) codec;
70 }
71
72
73 @Test
74 public void testNoInformationLoss() {
75 FilteredConnectPoint original = new FilteredConnectPoint(CP, VLAN_SELECTOR);
76 ObjectNode json = sut.encode(original, context);
77 assertNotNull(json);
78
79 FilteredConnectPoint decoded = sut.decode(json, context);
80 assertThat(decoded, is(equalTo(original)));
81 }
82
83 @Test
84 public void testJsonFormat() throws JsonProcessingException, IOException {
85 FilteredConnectPoint original = new FilteredConnectPoint(CP, VLAN_SELECTOR);
86
87 // Jackson configuration for ease of Numeric node comparison
88 // - treat integral number node as long node
89 context.mapper().enable(DeserializationFeature.USE_LONG_FOR_INTS);
90 context.mapper().setNodeFactory(new JsonNodeFactory(false) {
91 @Override
92 public NumericNode numberNode(int v) {
93 return super.numberNode((long) v);
94 }
95 @Override
96 public NumericNode numberNode(short v) {
97 return super.numberNode((long) v);
98 }
99 });
100
101 ObjectNode json = sut.encode(original, context);
102 JsonNode expected = context.mapper().readTree(JSON);
103
104 assertEquals(expected, json);
105 }
106
107 @Test
108 public void testEmptySelector() {
109 FilteredConnectPoint original = new FilteredConnectPoint(CP);
110 ObjectNode json = sut.encode(original, context);
111 assertNotNull(json);
112
113 FilteredConnectPoint decoded = sut.decode(json, context);
114 assertThat(decoded, is(equalTo(original)));
115 }
116
117}