blob: 715af50d5b10902795a92f719550aedbaa9bf227 [file] [log] [blame]
Jian Lib54d14b2017-03-28 21:34:34 +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
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onosproject.codec.CodecContext;
24import org.onosproject.codec.CodecService;
25import org.onosproject.codec.JsonCodec;
26import org.onosproject.codec.impl.CodecManager;
27import org.onosproject.mapping.instructions.MappingInstruction;
28import org.onosproject.mapping.instructions.MappingInstructions;
29import org.onosproject.mapping.instructions.MulticastMappingInstruction;
30import org.onosproject.mapping.instructions.UnicastMappingInstruction;
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.MappingInstructionJsonMatcher.matchesInstruction;
36
37/**
38 * Unit tests for MappingInstructionCodec.
39 */
40public class MappingInstructionCodecTest {
41
42 private CodecContext context;
43 private JsonCodec<MappingInstruction> instructionCodec;
44 private MappingCodecRegistrator registrator;
45
46 private static final int UNICAST_WEIGHT = 1;
47 private static final int UNICAST_PRIORITY = 1;
48 private static final int MULTICAST_WEIGHT = 2;
49 private static final int MULTICAST_PRIORITY = 2;
50
51 /**
52 * Sets up for each test.
53 * Creates a context and fetches the mapping instruction codec.
54 */
55 @Before
56 public void setUp() {
57 CodecManager manager = new CodecManager();
58 registrator = new MappingCodecRegistrator();
59 registrator.codecService = manager;
60 registrator.activate();
61
62 context = new MappingInstructionCodecTest.MappingTestContext(registrator.codecService);
63
64 instructionCodec = context.codec(MappingInstruction.class);
65 assertThat(instructionCodec, notNullValue());
66 }
67
68 @After
69 public void tearDown() {
70 registrator.deactivate();
71 }
72
73 /**
74 * Tests the encoding of unicast weight instruction.
75 */
76 @Test
77 public void unicastWeightInstrutionTest() {
78 final UnicastMappingInstruction.WeightMappingInstruction instruction =
79 (UnicastMappingInstruction.WeightMappingInstruction)
80 MappingInstructions.unicastWeight(UNICAST_WEIGHT);
81 final ObjectNode instructionJson =
82 instructionCodec.encode(instruction, context);
83 assertThat(instructionJson, matchesInstruction(instruction));
84 }
85
86 /**
87 * Tests the encoding of unicast priority instruction.
88 */
89 @Test
90 public void unicastPriorityInstructionTest() {
91 final UnicastMappingInstruction.PriorityMappingInstruction instruction =
92 (UnicastMappingInstruction.PriorityMappingInstruction)
93 MappingInstructions.unicastPriority(UNICAST_PRIORITY);
94 final ObjectNode instructionJson =
95 instructionCodec.encode(instruction, context);
96 assertThat(instructionJson, matchesInstruction(instruction));
97 }
98
99 /**
100 * Tests the encoding of multicast weight instruction.
101 */
102 @Test
103 public void multicastWeightInstructionTest() {
104 final MulticastMappingInstruction.WeightMappingInstruction instruction =
105 (MulticastMappingInstruction.WeightMappingInstruction)
106 MappingInstructions.multicastWeight(MULTICAST_WEIGHT);
107 final ObjectNode instructionJson =
108 instructionCodec.encode(instruction, context);
109 assertThat(instructionJson, matchesInstruction(instruction));
110 }
111
112 /**
113 * Tests the encoding of multicast priority instruction.
114 */
115 @Test
116 public void multicastPriorityInstructionTest() {
117 final MulticastMappingInstruction.PriorityMappingInstruction instruction =
118 (MulticastMappingInstruction.PriorityMappingInstruction)
119 MappingInstructions.multicastPriority(MULTICAST_PRIORITY);
120 final ObjectNode instructionJson =
121 instructionCodec.encode(instruction, context);
122 assertThat(instructionJson, matchesInstruction(instruction)); }
123
124 /**
125 * Test mapping codec context.
126 */
127 private class MappingTestContext implements CodecContext {
128 private final ObjectMapper mapper = new ObjectMapper();
129 private final CodecService manager;
130
131 /**
132 * Constructs a new mock codec context.
133 */
134 public MappingTestContext(CodecService manager) {
135 this.manager = manager;
136 }
137
138 @Override
139 public ObjectMapper mapper() {
140 return mapper;
141 }
142
143 @Override
144 public <T> JsonCodec<T> codec(Class<T> entityClass) {
145 return manager.getCodec(entityClass);
146 }
147
148 @Override
149 public <T> T getService(Class<T> serviceClass) {
150 return null;
151 }
152 }
153}