Charles Chan | a01d57c | 2019-07-19 16:25:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 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.segmentrouting.xconnect.api; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.collect.Sets; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.VlanId; |
| 25 | import org.onosproject.codec.CodecContext; |
| 26 | import org.onosproject.codec.JsonCodec; |
| 27 | import org.onosproject.codec.impl.MockCodecContext; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | |
| 30 | import java.io.InputStream; |
| 31 | import java.util.Set; |
| 32 | |
| 33 | import static org.junit.Assert.*; |
| 34 | |
| 35 | public class XconnectCodecTest { |
| 36 | private static final DeviceId DEVICE_ID = DeviceId.deviceId("of:1"); |
| 37 | private static final VlanId VLAN_VID = VlanId.vlanId((short) 10); |
| 38 | private static final XconnectKey KEY = new XconnectKey(DEVICE_ID, VLAN_VID); |
| 39 | private static final XconnectEndpoint EP1 = XconnectEndpoint.fromString("1"); |
| 40 | private static final XconnectEndpoint EP2 = XconnectEndpoint.fromString("2"); |
| 41 | private static final XconnectEndpoint EP3 = XconnectEndpoint.fromString("LB:5"); |
| 42 | |
| 43 | private CodecContext context; |
| 44 | private JsonCodec<XconnectDesc> codec; |
| 45 | |
| 46 | @Before |
| 47 | public void setUp() throws Exception { |
| 48 | context = new MockCodecContext(); |
| 49 | codec = new XconnectCodec(); |
| 50 | } |
| 51 | |
| 52 | @Test |
| 53 | public void testEncodePort() throws Exception { |
| 54 | Set<XconnectEndpoint> endpoints1 = Sets.newHashSet(EP1, EP2); |
| 55 | XconnectDesc desc1 = new XconnectDesc(KEY, endpoints1); |
| 56 | |
| 57 | ObjectMapper mapper = new ObjectMapper(); |
| 58 | InputStream jsonStream1 = XconnectCodecTest.class.getResourceAsStream("/xconnect1.json"); |
| 59 | JsonNode expected = mapper.readTree(jsonStream1); |
| 60 | |
| 61 | JsonNode actual = codec.encode(desc1, context); |
| 62 | |
| 63 | assertEquals(expected.get(XconnectCodec.DEVICE_ID), actual.get(XconnectCodec.DEVICE_ID)); |
| 64 | assertEquals(expected.get(XconnectCodec.VLAN_ID).asInt(), actual.get(XconnectCodec.VLAN_ID).asInt()); |
| 65 | assertEquals(expected.get(XconnectCodec.ENDPOINTS), actual.get(XconnectCodec.ENDPOINTS)); |
| 66 | } |
| 67 | |
| 68 | @Test |
| 69 | public void testDecodePort() throws Exception { |
| 70 | Set<XconnectEndpoint> endpoints1 = Sets.newHashSet(EP1, EP2); |
| 71 | XconnectDesc expected = new XconnectDesc(KEY, endpoints1); |
| 72 | |
| 73 | ObjectMapper mapper = new ObjectMapper(); |
| 74 | InputStream jsonStream1 = XconnectCodecTest.class.getResourceAsStream("/xconnect1.json"); |
| 75 | ObjectNode objectNode = mapper.readTree(jsonStream1).deepCopy(); |
| 76 | |
| 77 | XconnectDesc actual = codec.decode(objectNode, context); |
| 78 | |
| 79 | assertEquals(expected, actual); |
| 80 | } |
| 81 | |
| 82 | @Test |
| 83 | public void testEncodeLb() throws Exception { |
| 84 | Set<XconnectEndpoint> endpoints1 = Sets.newHashSet(EP1, EP3); |
| 85 | XconnectDesc desc1 = new XconnectDesc(KEY, endpoints1); |
| 86 | |
| 87 | ObjectMapper mapper = new ObjectMapper(); |
| 88 | InputStream jsonStream1 = XconnectCodecTest.class.getResourceAsStream("/xconnect2.json"); |
| 89 | JsonNode expected = mapper.readTree(jsonStream1); |
| 90 | |
| 91 | JsonNode actual = codec.encode(desc1, context); |
| 92 | |
| 93 | assertEquals(expected.get(XconnectCodec.DEVICE_ID), actual.get(XconnectCodec.DEVICE_ID)); |
| 94 | assertEquals(expected.get(XconnectCodec.VLAN_ID).asInt(), actual.get(XconnectCodec.VLAN_ID).asInt()); |
| 95 | assertEquals(expected.get(XconnectCodec.ENDPOINTS), actual.get(XconnectCodec.ENDPOINTS)); |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void testDecodeLb() throws Exception { |
| 100 | Set<XconnectEndpoint> endpoints1 = Sets.newHashSet(EP1, EP3); |
| 101 | XconnectDesc expected = new XconnectDesc(KEY, endpoints1); |
| 102 | |
| 103 | ObjectMapper mapper = new ObjectMapper(); |
| 104 | InputStream jsonStream1 = XconnectCodecTest.class.getResourceAsStream("/xconnect2.json"); |
| 105 | ObjectNode objectNode = mapper.readTree(jsonStream1).deepCopy(); |
| 106 | |
| 107 | XconnectDesc actual = codec.decode(objectNode, context); |
| 108 | |
| 109 | assertEquals(expected, actual); |
| 110 | } |
| 111 | } |