blob: 35cdbb67c1b01b06fc80679a145fa2fa915fa142 [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 */
Jian Li2e818b02017-04-12 19:28:30 +090016package org.onosproject.mapping.codec;
Jian Lib54d14b2017-03-28 21:34:34 +090017
Jian Li124ecf02017-04-05 16:38:34 +090018import com.fasterxml.jackson.databind.JsonNode;
Jian Lib54d14b2017-03-28 21:34:34 +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 Lib54d14b2017-03-28 21:34:34 +090024import org.onosproject.codec.JsonCodec;
25import org.onosproject.codec.impl.CodecManager;
26import org.onosproject.mapping.instructions.MappingInstruction;
27import org.onosproject.mapping.instructions.MappingInstructions;
28import org.onosproject.mapping.instructions.MulticastMappingInstruction;
29import org.onosproject.mapping.instructions.UnicastMappingInstruction;
Jian Li2e818b02017-04-12 19:28:30 +090030import org.onosproject.mapping.MappingCodecRegistrator;
Jian Lib54d14b2017-03-28 21:34:34 +090031
Jian Li124ecf02017-04-05 16:38:34 +090032import java.io.IOException;
33import java.io.InputStream;
34
Jian Lib54d14b2017-03-28 21:34:34 +090035import static org.hamcrest.MatcherAssert.assertThat;
Jian Li124ecf02017-04-05 16:38:34 +090036import static org.hamcrest.Matchers.is;
Jian Lib54d14b2017-03-28 21:34:34 +090037import static org.hamcrest.Matchers.notNullValue;
Jian Li2e818b02017-04-12 19:28:30 +090038import static org.onosproject.mapping.codec.MappingInstructionJsonMatcher.matchesInstruction;
Jian Lib54d14b2017-03-28 21:34:34 +090039
40/**
41 * Unit tests for MappingInstructionCodec.
42 */
43public class MappingInstructionCodecTest {
44
45 private CodecContext context;
46 private JsonCodec<MappingInstruction> instructionCodec;
47 private MappingCodecRegistrator registrator;
48
49 private static final int UNICAST_WEIGHT = 1;
50 private static final int UNICAST_PRIORITY = 1;
51 private static final int MULTICAST_WEIGHT = 2;
52 private static final int MULTICAST_PRIORITY = 2;
53
Jian Li124ecf02017-04-05 16:38:34 +090054 private static final String UNICAST_TYPE_STRING = "UNICAST";
55 private static final String WEIGHT_SUBTYPE_STRING = "WEIGHT";
56
Jian Lib54d14b2017-03-28 21:34:34 +090057 /**
58 * Sets up for each test.
59 * Creates a context and fetches the mapping instruction codec.
60 */
61 @Before
62 public void setUp() {
63 CodecManager manager = new CodecManager();
64 registrator = new MappingCodecRegistrator();
65 registrator.codecService = manager;
66 registrator.activate();
67
Jian Lifa69be62017-04-05 16:00:10 +090068 context = new MappingCodecContextAdapter(registrator.codecService);
Jian Lib54d14b2017-03-28 21:34:34 +090069
70 instructionCodec = context.codec(MappingInstruction.class);
71 assertThat(instructionCodec, notNullValue());
72 }
73
74 @After
75 public void tearDown() {
76 registrator.deactivate();
77 }
78
79 /**
80 * Tests the encoding of unicast weight instruction.
81 */
82 @Test
83 public void unicastWeightInstrutionTest() {
84 final UnicastMappingInstruction.WeightMappingInstruction instruction =
85 (UnicastMappingInstruction.WeightMappingInstruction)
86 MappingInstructions.unicastWeight(UNICAST_WEIGHT);
87 final ObjectNode instructionJson =
88 instructionCodec.encode(instruction, context);
89 assertThat(instructionJson, matchesInstruction(instruction));
90 }
91
92 /**
93 * Tests the encoding of unicast priority instruction.
94 */
95 @Test
96 public void unicastPriorityInstructionTest() {
97 final UnicastMappingInstruction.PriorityMappingInstruction instruction =
98 (UnicastMappingInstruction.PriorityMappingInstruction)
99 MappingInstructions.unicastPriority(UNICAST_PRIORITY);
100 final ObjectNode instructionJson =
101 instructionCodec.encode(instruction, context);
102 assertThat(instructionJson, matchesInstruction(instruction));
103 }
104
105 /**
106 * Tests the encoding of multicast weight instruction.
107 */
108 @Test
109 public void multicastWeightInstructionTest() {
110 final MulticastMappingInstruction.WeightMappingInstruction instruction =
111 (MulticastMappingInstruction.WeightMappingInstruction)
112 MappingInstructions.multicastWeight(MULTICAST_WEIGHT);
113 final ObjectNode instructionJson =
114 instructionCodec.encode(instruction, context);
115 assertThat(instructionJson, matchesInstruction(instruction));
116 }
117
118 /**
119 * Tests the encoding of multicast priority instruction.
120 */
121 @Test
122 public void multicastPriorityInstructionTest() {
123 final MulticastMappingInstruction.PriorityMappingInstruction instruction =
124 (MulticastMappingInstruction.PriorityMappingInstruction)
125 MappingInstructions.multicastPriority(MULTICAST_PRIORITY);
126 final ObjectNode instructionJson =
127 instructionCodec.encode(instruction, context);
Jian Lifa69be62017-04-05 16:00:10 +0900128 assertThat(instructionJson, matchesInstruction(instruction));
Jian Lib54d14b2017-03-28 21:34:34 +0900129 }
Jian Li124ecf02017-04-05 16:38:34 +0900130
131 /**
132 * Tests the decoding of mapping instruction from JSON object.
133 *
134 * @throws IOException if processing the resource fails
135 */
136 @Test
137 public void testMappingInstructionDecode() throws IOException {
138 UnicastMappingInstruction instruction = (UnicastMappingInstruction) getInstruction("MappingInstruction.json");
139 assertThat(instruction.type().toString(), is(UNICAST_TYPE_STRING));
140 assertThat(instruction.subtype().toString(), is(WEIGHT_SUBTYPE_STRING));
141 }
142
143 /**
144 * Reads in a mapping instruction from the given resource and decodes it.
145 *
146 * @param resourceName resource to use to read the JSON for the rule
147 * @return decoded mappingInstruction
148 * @throws IOException if processing the resource fails
149 */
150 private MappingInstruction getInstruction(String resourceName) throws IOException {
151 InputStream jsonStream = MappingInstructionCodecTest.class.getResourceAsStream(resourceName);
152 JsonNode json = context.mapper().readTree(jsonStream);
153 assertThat(json, notNullValue());
154 MappingInstruction instruction = instructionCodec.decode((ObjectNode) json, context);
155 assertThat(instruction, notNullValue());
156 return instruction;
157 }
Jian Lib54d14b2017-03-28 21:34:34 +0900158}