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.onosproject.codec.CodecContext; |
| 25 | import org.onosproject.codec.JsonCodec; |
| 26 | import org.onosproject.codec.impl.MockCodecContext; |
| 27 | import org.onosproject.net.DeviceId; |
pierventre | b37a11a | 2021-03-18 16:50:04 +0100 | [diff] [blame^] | 28 | import org.onosproject.segmentrouting.policy.api.RedirectPolicy; |
Wailok Shum | ee90c13 | 2021-03-11 21:00:11 +0800 | [diff] [blame] | 29 | |
| 30 | import java.io.InputStream; |
| 31 | import java.util.LinkedList; |
| 32 | import java.util.List; |
| 33 | import java.util.Set; |
| 34 | |
| 35 | public 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 | } |