Jian Li | 136fd6c | 2017-02-10 14:54:59 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jian Li | 136fd6c | 2017-02-10 14:54:59 +0900 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.mapping.instructions; |
| 17 | |
Jian Li | 24ad4e4 | 2017-02-14 01:34:50 +0900 | [diff] [blame] | 18 | import com.google.common.testing.EqualsTester; |
| 19 | import org.junit.Test; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.flow.instructions.ExtensionPropertyException; |
| 22 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
| 23 | import org.onosproject.net.flow.instructions.ExtensionTreatmentType; |
| 24 | |
| 25 | import java.util.List; |
| 26 | |
| 27 | import static org.hamcrest.MatcherAssert.assertThat; |
| 28 | import static org.hamcrest.Matchers.equalTo; |
| 29 | import static org.hamcrest.Matchers.instanceOf; |
| 30 | import static org.hamcrest.Matchers.is; |
| 31 | import static org.hamcrest.Matchers.not; |
| 32 | import static org.hamcrest.Matchers.notNullValue; |
| 33 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 34 | import static org.onlab.junit.UtilityClassChecker.assertThatClassIsUtility; |
| 35 | |
Jian Li | 136fd6c | 2017-02-10 14:54:59 +0900 | [diff] [blame] | 36 | /** |
| 37 | * Unit tests for the MappingInstructions class. |
| 38 | */ |
| 39 | public class MappingInstructionsTest { |
Jian Li | 24ad4e4 | 2017-02-14 01:34:50 +0900 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Checks that a MappingInstruction object has the proper type, ad then |
| 43 | * converts it to the proper type. |
| 44 | * |
| 45 | * @param instruction MappingInstruction object to convert |
| 46 | * @param type Enumerated type value for the Criterion class |
| 47 | * @param clazz Desired Criterion class |
| 48 | * @param <T> The type the caller wants returned |
| 49 | * @return converted object |
| 50 | */ |
| 51 | @SuppressWarnings("unchecked") |
| 52 | private <T> T checkAndConvert(MappingInstruction instruction, |
| 53 | MappingInstruction.Type type, Class clazz) { |
| 54 | assertThat(instruction, is(notNullValue())); |
| 55 | assertThat(instruction.type(), is(equalTo(type))); |
| 56 | assertThat(instruction, instanceOf(clazz)); |
| 57 | return (T) instruction; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Checks the equals() and toString() methods of a MappingInstruction class. |
| 62 | * |
| 63 | * @param c1 first object to compare |
| 64 | * @param c1match object that should be equal to the first |
| 65 | * @param c2 object that should not be equal to the first |
| 66 | * @param <T> type of the arguments |
| 67 | */ |
| 68 | private <T extends MappingInstruction> void checkEqualsAndToString(T c1, |
| 69 | T c1match, |
| 70 | T c2) { |
| 71 | new EqualsTester() |
| 72 | .addEqualityGroup(c1, c1match) |
| 73 | .addEqualityGroup(c2) |
| 74 | .testEquals(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Checks that MappingInstructions is a proper utility class. |
| 79 | */ |
| 80 | @Test |
| 81 | public void testMappingInstructionsUtilityClass() { |
| 82 | assertThatClassIsUtility(MappingInstructions.class); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Checks that the MappingInstruction class implementation are immutable. |
| 87 | */ |
| 88 | @Test |
| 89 | public void testImmutabilityOfMappingInstructions() { |
Jian Li | 24ad4e4 | 2017-02-14 01:34:50 +0900 | [diff] [blame] | 90 | assertThatClassIsImmutable(MulticastMappingInstruction.WeightMappingInstruction.class); |
| 91 | assertThatClassIsImmutable(MulticastMappingInstruction.PriorityMappingInstruction.class); |
| 92 | assertThatClassIsImmutable(UnicastMappingInstruction.WeightMappingInstruction.class); |
| 93 | assertThatClassIsImmutable(UnicastMappingInstruction.PriorityMappingInstruction.class); |
| 94 | } |
| 95 | |
Jian Li | 24ad4e4 | 2017-02-14 01:34:50 +0900 | [diff] [blame] | 96 | private final UnicastMappingInstruction uniWeight1 = |
| 97 | MappingInstructions.unicastWeight(1); |
| 98 | private final UnicastMappingInstruction sameAsUniWeight1 = |
| 99 | MappingInstructions.unicastWeight(1); |
| 100 | private final UnicastMappingInstruction uniWeight2 = |
| 101 | MappingInstructions.unicastWeight(2); |
| 102 | |
| 103 | /** |
| 104 | * Tests the unicastWeight method. |
| 105 | */ |
| 106 | @Test |
| 107 | public void testUnicastWeightMethod() { |
| 108 | final MappingInstruction instruction = MappingInstructions.unicastWeight(2); |
| 109 | final UnicastMappingInstruction.WeightMappingInstruction weightInstruction = |
| 110 | checkAndConvert(instruction, |
| 111 | UnicastMappingInstruction.Type.UNICAST, |
| 112 | UnicastMappingInstruction.WeightMappingInstruction.class); |
| 113 | assertThat(weightInstruction.weight(), is(equalTo(2))); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Test the equals() method of the UnicastWeightInstruction class. |
| 118 | */ |
| 119 | @Test |
| 120 | public void testUnicastWeightInstructionEquals() { |
| 121 | checkEqualsAndToString(uniWeight1, sameAsUniWeight1, uniWeight2); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Tests the hashCode() method of the UnicastWeightInstruction class. |
| 126 | */ |
| 127 | @Test |
| 128 | public void testUnicastWeightInstructionHashCode() { |
| 129 | assertThat(uniWeight1.hashCode(), is(equalTo(sameAsUniWeight1.hashCode()))); |
| 130 | assertThat(uniWeight1.hashCode(), is(not(equalTo(uniWeight2.hashCode())))); |
| 131 | } |
| 132 | |
| 133 | private final UnicastMappingInstruction uniPriority1 = |
| 134 | MappingInstructions.unicastPriority(1); |
| 135 | private final UnicastMappingInstruction sameAsUniPriority1 = |
| 136 | MappingInstructions.unicastPriority(1); |
| 137 | private final UnicastMappingInstruction uniPriority2 = |
| 138 | MappingInstructions.unicastPriority(2); |
| 139 | |
| 140 | /** |
| 141 | * Tests the unicastPriority method. |
| 142 | */ |
| 143 | @Test |
| 144 | public void testUnicastPriorityMethod() { |
| 145 | final MappingInstruction instruction = MappingInstructions.unicastPriority(2); |
| 146 | final UnicastMappingInstruction.PriorityMappingInstruction priorityMappingInstruction = |
| 147 | checkAndConvert(instruction, |
| 148 | UnicastMappingInstruction.Type.UNICAST, |
| 149 | UnicastMappingInstruction.PriorityMappingInstruction.class); |
| 150 | assertThat(priorityMappingInstruction.priority(), is(equalTo(2))); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Tests the equals() method of the UnicastPriorityInstruction class. |
| 155 | */ |
| 156 | @Test |
| 157 | public void testUnicastPriorityInstructionEquals() { |
| 158 | checkEqualsAndToString(uniPriority1, sameAsUniPriority1, uniPriority2); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Test the hashCode() method of the UnicastPriorityInstruction class. |
| 163 | */ |
| 164 | @Test |
| 165 | public void testUnicastPriorityInstructionHashCode() { |
| 166 | assertThat(uniPriority1.hashCode(), is(equalTo(sameAsUniPriority1.hashCode()))); |
| 167 | assertThat(uniPriority1.hashCode(), is(not(equalTo(uniPriority2.hashCode())))); |
| 168 | } |
| 169 | |
| 170 | private final MulticastMappingInstruction multiWeight1 = |
| 171 | MappingInstructions.multicastWeight(1); |
| 172 | private final MulticastMappingInstruction sameAsMultiWeight1 = |
| 173 | MappingInstructions.multicastWeight(1); |
| 174 | private final MulticastMappingInstruction multiWeight2 = |
| 175 | MappingInstructions.multicastWeight(2); |
| 176 | |
| 177 | /** |
| 178 | * Tests the multicastWeight method. |
| 179 | */ |
| 180 | @Test |
| 181 | public void testMulticastWeightMethod() { |
| 182 | final MappingInstruction instruction = MappingInstructions.multicastWeight(2); |
| 183 | final MulticastMappingInstruction.WeightMappingInstruction weightMappingInstruction = |
| 184 | checkAndConvert(instruction, |
| 185 | MulticastMappingInstruction.Type.MULTICAST, |
| 186 | MulticastMappingInstruction.WeightMappingInstruction.class); |
| 187 | assertThat(weightMappingInstruction.weight(), is(equalTo(2))); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Tests the equals() method of the MulticastWeightInstruction class. |
| 192 | */ |
| 193 | @Test |
| 194 | public void testMulticastWeightInstructionEquals() { |
| 195 | checkEqualsAndToString(multiWeight1, sameAsMultiWeight1, multiWeight2); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Tests the hashCode() method of the MulticastWeightInstruction class. |
| 200 | */ |
| 201 | @Test |
| 202 | public void testMulticastWeightInstructionHashCode() { |
| 203 | assertThat(multiWeight1.hashCode(), is(equalTo(sameAsMultiWeight1.hashCode()))); |
| 204 | assertThat(multiWeight1.hashCode(), is(not(equalTo(multiWeight2.hashCode())))); |
| 205 | } |
| 206 | |
| 207 | private final MulticastMappingInstruction multiPriority1 = |
| 208 | MappingInstructions.multicastPriority(1); |
| 209 | private final MulticastMappingInstruction sameAsMultiPriority1 = |
| 210 | MappingInstructions.multicastPriority(1); |
| 211 | private final MulticastMappingInstruction multiPriority2 = |
| 212 | MappingInstructions.multicastPriority(2); |
| 213 | |
| 214 | /** |
| 215 | * Tests the multicastPriority method. |
| 216 | */ |
| 217 | @Test |
| 218 | public void testMulticastPriorityMethod() { |
| 219 | final MappingInstruction instruction = MappingInstructions.multicastPriority(2); |
| 220 | final MulticastMappingInstruction.PriorityMappingInstruction priorityMappingInstruction = |
| 221 | checkAndConvert(instruction, |
| 222 | MulticastMappingInstruction.Type.MULTICAST, |
| 223 | MulticastMappingInstruction.PriorityMappingInstruction.class); |
| 224 | assertThat(priorityMappingInstruction.priority(), is(equalTo(2))); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Tests the equals() method of the MulticastPriorityInstruction class. |
| 229 | */ |
| 230 | @Test |
| 231 | public void testMulticastPriorityInstructionEquals() { |
| 232 | checkEqualsAndToString(multiPriority1, sameAsMultiPriority1, multiPriority2); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Tests the hashCode() method of the MulticastPriorityInstruction class. |
| 237 | */ |
| 238 | @Test |
| 239 | public void testMulticastPriorityInstructionHashCode() { |
| 240 | assertThat(multiPriority1.hashCode(), is(equalTo(sameAsMultiPriority1.hashCode()))); |
| 241 | assertThat(multiPriority1.hashCode(), is(not(equalTo(multiPriority2.hashCode())))); |
| 242 | } |
| 243 | |
| 244 | // ExtensionMappingInstructionWrapper |
| 245 | |
| 246 | class MockExtensionTreatment implements ExtensionTreatment { |
| 247 | int type; |
| 248 | |
| 249 | MockExtensionTreatment(int type) { |
| 250 | this.type = type; |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public ExtensionTreatmentType type() { |
| 255 | return new ExtensionTreatmentType(type); |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public <T> void setPropertyValue(String key, T value) throws ExtensionPropertyException { |
| 260 | |
| 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public <T> T getPropertyValue(String key) throws ExtensionPropertyException { |
| 265 | return null; |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public List<String> getProperties() { |
| 270 | return null; |
| 271 | } |
| 272 | |
| 273 | @Override |
| 274 | public byte[] serialize() { |
| 275 | return new byte[0]; |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public void deserialize(byte[] data) { |
| 280 | |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | private final ExtensionTreatment extensionTreatment1 = new MockExtensionTreatment(111); |
| 285 | private final ExtensionTreatment extensionTreatment2 = new MockExtensionTreatment(222); |
| 286 | |
| 287 | private final DeviceId deviceId1 = DeviceId.deviceId("of:1"); |
| 288 | private final DeviceId deviceId2 = DeviceId.deviceId("of:2"); |
| 289 | |
| 290 | private final MappingInstruction extensionInstruction1 = |
| 291 | MappingInstructions.extension(extensionTreatment1, deviceId1); |
| 292 | private final MappingInstruction sameAsExtensionInstruction1 = |
| 293 | MappingInstructions.extension(extensionTreatment1, deviceId1); |
| 294 | private final MappingInstruction extensionInstruction2 = |
| 295 | MappingInstructions.extension(extensionTreatment2, deviceId2); |
| 296 | |
| 297 | /** |
| 298 | * Tests the extension method. |
| 299 | */ |
| 300 | @Test |
| 301 | public void testExtensionMethod() { |
| 302 | final MappingInstruction instruction = |
| 303 | MappingInstructions.extension(extensionTreatment1, deviceId1); |
| 304 | final MappingInstructions.ExtensionMappingInstructionWrapper wrapper = |
| 305 | checkAndConvert(instruction, |
| 306 | MappingInstruction.Type.EXTENSION, |
| 307 | MappingInstructions.ExtensionMappingInstructionWrapper.class); |
| 308 | assertThat(wrapper.deviceId(), is(deviceId1)); |
| 309 | assertThat(wrapper.extensionMappingInstruction(), is(extensionTreatment1)); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Tests the equals() method of the ExtensionMappingInstructionWrapper class. |
| 314 | */ |
| 315 | @Test |
| 316 | public void testExtensionMappingInstructionWrapperEquals() { |
| 317 | checkEqualsAndToString(extensionInstruction1, |
| 318 | sameAsExtensionInstruction1, |
| 319 | extensionInstruction2); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Tests the hashCode() method of the ExtensionMappingInstructionWrapper class. |
| 324 | */ |
| 325 | @Test |
| 326 | public void testExtensionMappingInstructionWrapperHashCode() { |
| 327 | assertThat(extensionInstruction1.hashCode(), |
| 328 | is(equalTo(sameAsExtensionInstruction1.hashCode()))); |
| 329 | assertThat(extensionInstruction1.hashCode(), |
| 330 | is(not(equalTo(extensionInstruction2.hashCode())))); |
| 331 | } |
Jian Li | 136fd6c | 2017-02-10 14:54:59 +0900 | [diff] [blame] | 332 | } |