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