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; |
pierventre | b37a11a | 2021-03-18 16:50:04 +0100 | [diff] [blame^] | 27 | import org.onosproject.segmentrouting.policy.api.DropPolicy; |
Wailok Shum | ee90c13 | 2021-03-11 21:00:11 +0800 | [diff] [blame] | 28 | |
| 29 | import java.io.InputStream; |
| 30 | |
| 31 | public class DropPolicyCodecTest extends TestCase { |
| 32 | private DropPolicy dropPolicy; |
| 33 | |
| 34 | private CodecContext context; |
| 35 | private JsonCodec<DropPolicy> codec; |
| 36 | |
| 37 | @Before |
| 38 | public void setUp() throws Exception { |
| 39 | context = new MockCodecContext(); |
| 40 | codec = new DropPolicyCodec(); |
| 41 | |
| 42 | dropPolicy = new DropPolicy(); |
| 43 | } |
| 44 | |
| 45 | @Test |
| 46 | public void testEncode() throws Exception { |
| 47 | ObjectMapper mapper = new ObjectMapper(); |
| 48 | |
| 49 | InputStream jsonStream1 = RedirectPolicyCodecTest.class.getResourceAsStream("/droppolicy.json"); |
| 50 | JsonNode expected = mapper.readTree(jsonStream1); |
| 51 | |
| 52 | JsonNode actual = codec.encode(dropPolicy, context); |
| 53 | |
| 54 | assertEquals(expected.get(RedirectPolicyCodec.POLICY_ID), actual.get(RedirectPolicyCodec.POLICY_ID)); |
| 55 | assertEquals(expected.get(RedirectPolicyCodec.POLICY_TYPE), actual.get(RedirectPolicyCodec.POLICY_TYPE)); |
| 56 | } |
| 57 | |
| 58 | @Test |
| 59 | public void testDecode() throws Exception { |
| 60 | ObjectMapper mapper = new ObjectMapper(); |
| 61 | |
| 62 | InputStream jsonStream1 = RedirectPolicyCodecTest.class.getResourceAsStream("/droppolicy.json"); |
| 63 | ObjectNode json = mapper.readTree(jsonStream1).deepCopy(); |
| 64 | |
| 65 | DropPolicy actual = codec.decode(json, context); |
| 66 | |
| 67 | assertEquals(dropPolicy, actual); |
| 68 | } |
| 69 | } |