blob: c80837f14c38976d587477538cfe6f83825c92e1 [file] [log] [blame]
Jian Liee65a232017-03-29 14:15:30 +09001/*
2 * Copyright 2017-present Open Networking Laboratory
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.mapping.web.codec;
17
Jian Liee65a232017-03-29 14:15:30 +090018import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
22import org.onosproject.codec.CodecContext;
Jian Liee65a232017-03-29 14:15:30 +090023import org.onosproject.codec.JsonCodec;
24import org.onosproject.codec.impl.CodecManager;
25import org.onosproject.mapping.actions.MappingAction;
26import org.onosproject.mapping.actions.MappingActions;
27import org.onosproject.mapping.actions.NoMappingAction;
28import org.onosproject.mapping.actions.DropMappingAction;
29import org.onosproject.mapping.actions.ForwardMappingAction;
30import org.onosproject.mapping.actions.NativeForwardMappingAction;
31import org.onosproject.mapping.web.MappingCodecRegistrator;
32
33import static org.hamcrest.MatcherAssert.assertThat;
34import static org.hamcrest.Matchers.notNullValue;
35import static org.onosproject.mapping.web.codec.MappingActionJsonMatcher.matchesAction;
36
37/**
38 * Unit tests for MappingActionCodec.
39 */
40public class MappingActionCodecTest {
41
42 private CodecContext context;
43 private JsonCodec<MappingAction> actionCodec;
44 private MappingCodecRegistrator registrator;
45
46 /**
47 * Sets up for each test.
48 * Creates a context and fetches the mapping action codec.
49 */
50 @Before
51 public void setUp() {
52 CodecManager manager = new CodecManager();
53 registrator = new MappingCodecRegistrator();
54 registrator.codecService = manager;
55 registrator.activate();
56
Jian Lifa69be62017-04-05 16:00:10 +090057 context = new MappingCodecContextAdapter(registrator.codecService);
Jian Liee65a232017-03-29 14:15:30 +090058 actionCodec = context.codec(MappingAction.class);
59 assertThat(actionCodec, notNullValue());
60 }
61
62 /**
63 * Deactivates the codec registrator.
64 */
65 @After
66 public void tearDown() {
67 registrator.deactivate();
68 }
69
70 /**
71 * Tests the encoding of no mapping action.
72 */
73 @Test
74 public void noActionTest() {
75 final NoMappingAction action = MappingActions.noAction();
76 final ObjectNode actionJson = actionCodec.encode(action, context);
77 assertThat(actionJson, matchesAction(action));
78 }
79
80 /**
81 * Tests the encoding of drop mapping action.
82 */
83 @Test
84 public void dropActionTest() {
85 final DropMappingAction action = MappingActions.drop();
86 final ObjectNode actionJson = actionCodec.encode(action, context);
87 assertThat(actionJson, matchesAction(action));
88 }
89
90 /**
91 * Tests the encoding of forward mapping action.
92 */
93 @Test
94 public void forwardActionTest() {
95 final ForwardMappingAction action = MappingActions.forward();
96 final ObjectNode actionJson = actionCodec.encode(action, context);
97 assertThat(actionJson, matchesAction(action));
98 }
99
100 /**
101 * Tests the encoding of native forwarding mapping action.
102 */
103 @Test
104 public void nativeForwardActionTest() {
105 final NativeForwardMappingAction action = MappingActions.nativeForward();
106 final ObjectNode actionJson = actionCodec.encode(action, context);
107 assertThat(actionJson, matchesAction(action));
108 }
Jian Liee65a232017-03-29 14:15:30 +0900109}