blob: e9af8658997b0d0ac9be52238124161928ad62bc [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;
pierventreb37a11a2021-03-18 16:50:04 +010027import org.onosproject.segmentrouting.policy.api.DropPolicy;
Wailok Shumee90c132021-03-11 21:00:11 +080028
29import java.io.InputStream;
30
31public 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}