blob: 5d95625697a4e502bcdaeb461b3c93fc36daf1b9 [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.onosproject.codec.CodecContext;
25import org.onosproject.codec.JsonCodec;
26import org.onosproject.codec.impl.MockCodecContext;
27
28import java.io.InputStream;
29
30public class DropPolicyCodecTest extends TestCase {
31 private DropPolicy dropPolicy;
32
33 private CodecContext context;
34 private JsonCodec<DropPolicy> codec;
35
36 @Before
37 public void setUp() throws Exception {
38 context = new MockCodecContext();
39 codec = new DropPolicyCodec();
40
41 dropPolicy = new DropPolicy();
42 }
43
44 @Test
45 public void testEncode() throws Exception {
46 ObjectMapper mapper = new ObjectMapper();
47
48 InputStream jsonStream1 = RedirectPolicyCodecTest.class.getResourceAsStream("/droppolicy.json");
49 JsonNode expected = mapper.readTree(jsonStream1);
50
51 JsonNode actual = codec.encode(dropPolicy, context);
52
53 assertEquals(expected.get(RedirectPolicyCodec.POLICY_ID), actual.get(RedirectPolicyCodec.POLICY_ID));
54 assertEquals(expected.get(RedirectPolicyCodec.POLICY_TYPE), actual.get(RedirectPolicyCodec.POLICY_TYPE));
55 }
56
57 @Test
58 public void testDecode() throws Exception {
59 ObjectMapper mapper = new ObjectMapper();
60
61 InputStream jsonStream1 = RedirectPolicyCodecTest.class.getResourceAsStream("/droppolicy.json");
62 ObjectNode json = mapper.readTree(jsonStream1).deepCopy();
63
64 DropPolicy actual = codec.decode(json, context);
65
66 assertEquals(dropPolicy, actual);
67 }
68}