blob: ff757693d3a355265846020739ede93e22e9de1c [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 */
pierventreb37a11a2021-03-18 16:50:04 +010016package org.onosproject.segmentrouting.policy.impl;
Wailok Shumee90c132021-03-11 21:00:11 +080017
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.onosproject.codec.CodecContext;
25import org.onosproject.codec.JsonCodec;
26import org.onosproject.codec.impl.MockCodecContext;
27import org.onosproject.net.DeviceId;
pierventreb37a11a2021-03-18 16:50:04 +010028import org.onosproject.segmentrouting.policy.api.RedirectPolicy;
Wailok Shumee90c132021-03-11 21:00:11 +080029
30import java.io.InputStream;
31import java.util.LinkedList;
32import java.util.List;
33import java.util.Set;
34
35public class RedirectPolicyCodecTest extends TestCase {
36 private RedirectPolicy redirectPolicy;
37
38 private CodecContext context;
39 private JsonCodec<RedirectPolicy> codec;
40
41 @Before
42 public void setUp() throws Exception {
43 context = new MockCodecContext();
44 codec = new RedirectPolicyCodec();
45
46 List<DeviceId> deviceIds = new LinkedList<>();
47 deviceIds.add(DeviceId.deviceId("of:0000000000000001"));
48 deviceIds.add(DeviceId.deviceId("of:0000000000000002"));
49 deviceIds.add(DeviceId.deviceId("of:0000000000000003"));
50 redirectPolicy = new RedirectPolicy(Set.copyOf(deviceIds));
51 }
52
53 @Test
54 public void testEncode() throws Exception {
55 ObjectMapper mapper = new ObjectMapper();
56
57 InputStream jsonStream1 = RedirectPolicyCodecTest.class.getResourceAsStream("/redirectpolicy.json");
58 JsonNode expected = mapper.readTree(jsonStream1);
59
60 JsonNode actual = codec.encode(redirectPolicy, context);
61
62 assertEquals(expected.get(RedirectPolicyCodec.POLICY_ID), actual.get(RedirectPolicyCodec.POLICY_ID));
63 assertEquals(expected.get(RedirectPolicyCodec.POLICY_TYPE), actual.get(RedirectPolicyCodec.POLICY_TYPE));
64 assertEquals(expected.get(RedirectPolicyCodec.DEVICE_ID), actual.get(RedirectPolicyCodec.DEVICE_ID));
65 }
66
67 @Test
68 public void testDecode() throws Exception {
69 ObjectMapper mapper = new ObjectMapper();
70
71 InputStream jsonStream1 = RedirectPolicyCodecTest.class.getResourceAsStream("/redirectpolicy.json");
72 ObjectNode json = mapper.readTree(jsonStream1).deepCopy();
73
74 RedirectPolicy actual = codec.decode(json, context);
75
76 assertEquals(redirectPolicy, actual);
77 }
78}