Wailok Shum | ee90c13 | 2021-03-11 21:00:11 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-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 | */ |
pierventre | b37a11a | 2021-03-18 16:50:04 +0100 | [diff] [blame^] | 16 | package org.onosproject.segmentrouting.policy.impl; |
Wailok Shum | ee90c13 | 2021-03-11 21:00:11 +0800 | [diff] [blame] | 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 junit.framework.TestCase; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.Ip4Address; |
| 25 | import org.onlab.packet.TpPort; |
| 26 | import org.onosproject.codec.CodecContext; |
| 27 | import org.onosproject.codec.JsonCodec; |
| 28 | import org.onosproject.codec.impl.MockCodecContext; |
| 29 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 30 | import org.onosproject.net.flow.TrafficSelector; |
pierventre | b37a11a | 2021-03-18 16:50:04 +0100 | [diff] [blame^] | 31 | import org.onosproject.segmentrouting.policy.api.PolicyId; |
| 32 | import org.onosproject.segmentrouting.policy.api.TrafficMatch; |
Wailok Shum | ee90c13 | 2021-03-11 21:00:11 +0800 | [diff] [blame] | 33 | |
| 34 | import java.io.InputStream; |
| 35 | |
| 36 | public class TrafficMatchCodecTest extends TestCase { |
| 37 | private TrafficMatch trafficMatch; |
| 38 | private TrafficSelector trafficSelector; |
| 39 | private PolicyId policyId; |
| 40 | |
| 41 | private CodecContext context; |
| 42 | private JsonCodec<TrafficMatch> codec; |
| 43 | |
| 44 | @Before |
| 45 | public void setUp() throws Exception { |
| 46 | context = new MockCodecContext(); |
| 47 | codec = new TrafficMatchCodec(); |
| 48 | |
| 49 | trafficSelector = DefaultTrafficSelector.builder() |
| 50 | .matchIPProtocol((byte) 0x06) |
| 51 | .matchIPSrc(Ip4Address.valueOf("10.0.0.1").toIpPrefix()) |
| 52 | .matchIPDst(Ip4Address.valueOf("10.0.0.2").toIpPrefix()) |
| 53 | .matchTcpSrc(TpPort.tpPort(80)) |
| 54 | .matchTcpDst(TpPort.tpPort(81)) |
| 55 | .build(); |
| 56 | policyId = PolicyId.of("DROP"); |
| 57 | |
| 58 | trafficMatch = new TrafficMatch(trafficSelector, policyId); |
| 59 | } |
| 60 | |
| 61 | @Test |
| 62 | public void testEncode() throws Exception { |
| 63 | ObjectMapper mapper = new ObjectMapper(); |
| 64 | |
| 65 | InputStream jsonStream1 = RedirectPolicyCodecTest.class.getResourceAsStream("/trafficmatch.json"); |
| 66 | JsonNode expected = mapper.readTree(jsonStream1); |
| 67 | |
| 68 | JsonNode actual = codec.encode(trafficMatch, context); |
| 69 | |
| 70 | assertEquals(expected.get(TrafficMatchCodec.TRAFFIC_MATCH_ID), actual.get(TrafficMatchCodec.TRAFFIC_MATCH_ID)); |
| 71 | assertEquals(expected.get(TrafficMatchCodec.POLICY_ID), actual.get(TrafficMatchCodec.POLICY_ID)); |
| 72 | for (int i = 0; i < trafficMatch.trafficSelector().criteria().size(); i++) { |
| 73 | assertEquals(expected.get(TrafficMatchCodec.TRAFFIC_SELECTOR).get(i), |
| 74 | actual.get(TrafficMatchCodec.TRAFFIC_SELECTOR).get(i)); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | @Test |
| 79 | public void testDecode() throws Exception { |
| 80 | ObjectMapper mapper = new ObjectMapper(); |
| 81 | |
| 82 | InputStream jsonStream1 = RedirectPolicyCodecTest.class.getResourceAsStream("/trafficmatch.json"); |
| 83 | ObjectNode json = mapper.readTree(jsonStream1).deepCopy(); |
| 84 | |
| 85 | TrafficMatch actual = codec.decode(json, context); |
| 86 | |
| 87 | assertEquals(trafficMatch, actual); |
| 88 | } |
| 89 | } |