Yuta HIGUCHI | 776f074 | 2016-11-03 15:51:14 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Yuta HIGUCHI | 776f074 | 2016-11-03 15:51:14 -0700 | [diff] [blame] | 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.codec.impl; |
| 17 | |
| 18 | import static org.hamcrest.MatcherAssert.assertThat; |
| 19 | import static org.hamcrest.Matchers.*; |
| 20 | import static org.junit.Assert.*; |
| 21 | |
| 22 | import java.io.IOException; |
| 23 | |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.onlab.packet.VlanId; |
| 27 | import org.onosproject.codec.CodecContext; |
| 28 | import org.onosproject.codec.JsonCodec; |
| 29 | import org.onosproject.net.ConnectPoint; |
| 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.net.FilteredConnectPoint; |
| 32 | import org.onosproject.net.PortNumber; |
| 33 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 34 | import org.onosproject.net.flow.TrafficSelector; |
| 35 | |
| 36 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 37 | import com.fasterxml.jackson.databind.DeserializationFeature; |
| 38 | import com.fasterxml.jackson.databind.JsonNode; |
| 39 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 40 | import com.fasterxml.jackson.databind.node.NumericNode; |
| 41 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 42 | |
| 43 | /** |
| 44 | * Unit tests for {@link FilteredConnectPointCodec}. |
| 45 | */ |
| 46 | public 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 | } |