blob: 1b0b0911da38d91889a96fae9026b38347760c01 [file] [log] [blame]
Jian Liee65a232017-03-29 14:15:30 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Liee65a232017-03-29 14:15:30 +09003 *
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 */
Jian Li2e818b02017-04-12 19:28:30 +090016package org.onosproject.mapping.codec;
Jian Liee65a232017-03-29 14:15:30 +090017
Jian Li124ecf02017-04-05 16:38:34 +090018import com.fasterxml.jackson.databind.JsonNode;
Jian Liee65a232017-03-29 14:15:30 +090019import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onosproject.codec.CodecContext;
Jian Liee65a232017-03-29 14:15:30 +090024import org.onosproject.codec.JsonCodec;
25import org.onosproject.codec.impl.CodecManager;
26import org.onosproject.mapping.actions.MappingAction;
27import org.onosproject.mapping.actions.MappingActions;
28import org.onosproject.mapping.actions.NoMappingAction;
29import org.onosproject.mapping.actions.DropMappingAction;
30import org.onosproject.mapping.actions.ForwardMappingAction;
31import org.onosproject.mapping.actions.NativeForwardMappingAction;
Jian Li2e818b02017-04-12 19:28:30 +090032import org.onosproject.mapping.MappingCodecRegistrator;
Jian Liee65a232017-03-29 14:15:30 +090033
Jian Li124ecf02017-04-05 16:38:34 +090034import java.io.IOException;
35import java.io.InputStream;
36
Jian Liee65a232017-03-29 14:15:30 +090037import static org.hamcrest.MatcherAssert.assertThat;
Jian Li124ecf02017-04-05 16:38:34 +090038import static org.hamcrest.Matchers.is;
Jian Liee65a232017-03-29 14:15:30 +090039import static org.hamcrest.Matchers.notNullValue;
Jian Li2e818b02017-04-12 19:28:30 +090040import static org.onosproject.mapping.codec.MappingActionJsonMatcher.matchesAction;
Jian Liee65a232017-03-29 14:15:30 +090041
42/**
43 * Unit tests for MappingActionCodec.
44 */
45public class MappingActionCodecTest {
46
Jian Li124ecf02017-04-05 16:38:34 +090047 private static final String NO_ACTION_STRING = "NO_ACTION";
48
Jian Liee65a232017-03-29 14:15:30 +090049 private CodecContext context;
50 private JsonCodec<MappingAction> actionCodec;
51 private MappingCodecRegistrator registrator;
52
53 /**
54 * Sets up for each test.
55 * Creates a context and fetches the mapping action codec.
56 */
57 @Before
58 public void setUp() {
59 CodecManager manager = new CodecManager();
60 registrator = new MappingCodecRegistrator();
61 registrator.codecService = manager;
62 registrator.activate();
63
Jian Lifa69be62017-04-05 16:00:10 +090064 context = new MappingCodecContextAdapter(registrator.codecService);
Jian Liee65a232017-03-29 14:15:30 +090065 actionCodec = context.codec(MappingAction.class);
66 assertThat(actionCodec, notNullValue());
67 }
68
69 /**
70 * Deactivates the codec registrator.
71 */
72 @After
73 public void tearDown() {
74 registrator.deactivate();
75 }
76
77 /**
78 * Tests the encoding of no mapping action.
79 */
80 @Test
81 public void noActionTest() {
82 final NoMappingAction action = MappingActions.noAction();
83 final ObjectNode actionJson = actionCodec.encode(action, context);
84 assertThat(actionJson, matchesAction(action));
85 }
86
87 /**
88 * Tests the encoding of drop mapping action.
89 */
90 @Test
91 public void dropActionTest() {
92 final DropMappingAction action = MappingActions.drop();
93 final ObjectNode actionJson = actionCodec.encode(action, context);
94 assertThat(actionJson, matchesAction(action));
95 }
96
97 /**
98 * Tests the encoding of forward mapping action.
99 */
100 @Test
101 public void forwardActionTest() {
102 final ForwardMappingAction action = MappingActions.forward();
103 final ObjectNode actionJson = actionCodec.encode(action, context);
104 assertThat(actionJson, matchesAction(action));
105 }
106
107 /**
108 * Tests the encoding of native forwarding mapping action.
109 */
110 @Test
111 public void nativeForwardActionTest() {
112 final NativeForwardMappingAction action = MappingActions.nativeForward();
113 final ObjectNode actionJson = actionCodec.encode(action, context);
114 assertThat(actionJson, matchesAction(action));
115 }
Jian Li124ecf02017-04-05 16:38:34 +0900116
117 /**
118 * Tests decoding of a mapping key JSON object.
119 *
120 * @throws IOException if processing the resource fails
121 */
122 @Test
123 public void testMappingActionDecode() throws IOException {
124 MappingAction action = getAction("MappingAction.json");
125 assertThat(action.toString(), is(NO_ACTION_STRING));
126 }
127
128 /**
129 * Reads in a mapping action from the given resource and decodes it.
130 *
131 * @param resourceName resource to use to read the JSON for the rule
132 * @return decoded mappingAction
133 * @throws IOException if processing the resource fails
134 */
135 private MappingAction getAction(String resourceName) throws IOException {
136 InputStream jsonStream = MappingActionCodecTest.class.getResourceAsStream(resourceName);
137 JsonNode json = context.mapper().readTree(jsonStream);
138 assertThat(json, notNullValue());
139 MappingAction action = actionCodec.decode((ObjectNode) json, context);
140 assertThat(action, notNullValue());
141 return action;
142 }
143 }