Jian Li | 136fd6c | 2017-02-10 14:54:59 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 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() { |
| 90 | assertThatClassIsImmutable(ActionMappingInstruction.NoActionMappingInstruction.class); |
| 91 | assertThatClassIsImmutable(ActionMappingInstruction.NativeForwardMappingInstruction.class); |
| 92 | assertThatClassIsImmutable(ActionMappingInstruction.ForwardMappingInstruction.class); |
| 93 | assertThatClassIsImmutable(ActionMappingInstruction.DropMappingInstruction.class); |
| 94 | assertThatClassIsImmutable(MulticastMappingInstruction.WeightMappingInstruction.class); |
| 95 | assertThatClassIsImmutable(MulticastMappingInstruction.PriorityMappingInstruction.class); |
| 96 | assertThatClassIsImmutable(UnicastMappingInstruction.WeightMappingInstruction.class); |
| 97 | assertThatClassIsImmutable(UnicastMappingInstruction.PriorityMappingInstruction.class); |
| 98 | } |
| 99 | |
| 100 | private final ActionMappingInstruction noAction1 = MappingInstructions.noAction(); |
| 101 | private final ActionMappingInstruction noAction2 = MappingInstructions.noAction(); |
| 102 | |
| 103 | /** |
| 104 | * Tests the noAction method. |
| 105 | */ |
| 106 | @Test |
| 107 | public void testNoActionMethod() { |
| 108 | ActionMappingInstruction instruction = MappingInstructions.noAction(); |
| 109 | checkAndConvert(instruction, |
| 110 | ActionMappingInstruction.Type.ACTION, |
| 111 | ActionMappingInstruction.NoActionMappingInstruction.class); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Tests the equals() method of the NoActionMappingInstruction class. |
| 116 | */ |
| 117 | @Test |
| 118 | public void testNoActionMappingInstructionEquals() { |
| 119 | new EqualsTester() |
| 120 | .addEqualityGroup(noAction1, noAction2) |
| 121 | .testEquals(); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Tests the hashCode() method of the NoActionMappingInstruction class. |
| 126 | */ |
| 127 | @Test |
| 128 | public void testNoActionMappingInstructionHashCode() { |
| 129 | assertThat(noAction1.hashCode(), is(equalTo(noAction2.hashCode()))); |
| 130 | } |
| 131 | |
| 132 | private final ActionMappingInstruction fwdAction1 = MappingInstructions.forwardAction(); |
| 133 | private final ActionMappingInstruction fwdAction2 = MappingInstructions.forwardAction(); |
| 134 | |
| 135 | /** |
| 136 | * Tests the forwardAction method. |
| 137 | */ |
| 138 | @Test |
| 139 | public void testForwardActionMethod() { |
| 140 | ActionMappingInstruction instruction = MappingInstructions.forwardAction(); |
| 141 | checkAndConvert(instruction, |
| 142 | ActionMappingInstruction.Type.ACTION, |
| 143 | ActionMappingInstruction.ForwardMappingInstruction.class); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Tests the equals() method of the ForwardActionMappingInstruction class. |
| 148 | */ |
| 149 | @Test |
| 150 | public void testForwardActionMappingInstructionEquals() { |
| 151 | new EqualsTester() |
| 152 | .addEqualityGroup(fwdAction1, fwdAction2) |
| 153 | .testEquals(); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Tests the hashCode() method of the ForwardActionMappingInstruction class. |
| 158 | */ |
| 159 | @Test |
| 160 | public void testForwardActionMappingInstructionHashCode() { |
| 161 | assertThat(fwdAction1.hashCode(), is(equalTo(fwdAction2.hashCode()))); |
| 162 | } |
| 163 | |
| 164 | private final ActionMappingInstruction nativeFwdAction1 = |
| 165 | MappingInstructions.nativeForwardAction(); |
| 166 | private final ActionMappingInstruction nativeFwdAction2 = |
| 167 | MappingInstructions.nativeForwardAction(); |
| 168 | |
| 169 | /** |
| 170 | * Tests the nativeForwardAction method. |
| 171 | */ |
| 172 | @Test |
| 173 | public void testNativeForwardActionMethod() { |
| 174 | ActionMappingInstruction instruction = MappingInstructions.nativeForwardAction(); |
| 175 | checkAndConvert(instruction, |
| 176 | ActionMappingInstruction.Type.ACTION, |
| 177 | ActionMappingInstruction.NativeForwardMappingInstruction.class); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Tests the equals() method of the NativeForwardActionMappingInstruction class. |
| 182 | */ |
| 183 | @Test |
| 184 | public void testNativeForwardActionMappingInstructionEquals() { |
| 185 | new EqualsTester() |
| 186 | .addEqualityGroup(nativeFwdAction1, nativeFwdAction2) |
| 187 | .testEquals(); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Tests the hashCode() method of the NativeForwardActionMappingInstruction class. |
| 192 | */ |
| 193 | @Test |
| 194 | public void testNativeForwardActionMappingInstructionHashCode() { |
| 195 | assertThat(nativeFwdAction1.hashCode(), is(equalTo(nativeFwdAction2.hashCode()))); |
| 196 | } |
| 197 | |
| 198 | private final ActionMappingInstruction dropAction1 = MappingInstructions.dropAction(); |
| 199 | private final ActionMappingInstruction dropAction2 = MappingInstructions.dropAction(); |
| 200 | |
| 201 | /** |
| 202 | * Tests the dropAction method. |
| 203 | */ |
| 204 | @Test |
| 205 | public void testDropActionMethod() { |
| 206 | ActionMappingInstruction instruction = MappingInstructions.dropAction(); |
| 207 | checkAndConvert(instruction, |
| 208 | ActionMappingInstruction.Type.ACTION, |
| 209 | ActionMappingInstruction.DropMappingInstruction.class); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Tests the equals() method of the DropActionMappingInstruction class. |
| 214 | */ |
| 215 | @Test |
| 216 | public void testDropActionMappingInstructionEquals() { |
| 217 | new EqualsTester() |
| 218 | .addEqualityGroup(dropAction1, dropAction2) |
| 219 | .testEquals(); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Tests the hashCode() method of the DropActionMappingInstruction class. |
| 224 | */ |
| 225 | @Test |
| 226 | public void testDropActionMappingInstructionHashCode() { |
| 227 | assertThat(dropAction1.hashCode(), is(equalTo(dropAction2.hashCode()))); |
| 228 | } |
| 229 | |
| 230 | private final UnicastMappingInstruction uniWeight1 = |
| 231 | MappingInstructions.unicastWeight(1); |
| 232 | private final UnicastMappingInstruction sameAsUniWeight1 = |
| 233 | MappingInstructions.unicastWeight(1); |
| 234 | private final UnicastMappingInstruction uniWeight2 = |
| 235 | MappingInstructions.unicastWeight(2); |
| 236 | |
| 237 | /** |
| 238 | * Tests the unicastWeight method. |
| 239 | */ |
| 240 | @Test |
| 241 | public void testUnicastWeightMethod() { |
| 242 | final MappingInstruction instruction = MappingInstructions.unicastWeight(2); |
| 243 | final UnicastMappingInstruction.WeightMappingInstruction weightInstruction = |
| 244 | checkAndConvert(instruction, |
| 245 | UnicastMappingInstruction.Type.UNICAST, |
| 246 | UnicastMappingInstruction.WeightMappingInstruction.class); |
| 247 | assertThat(weightInstruction.weight(), is(equalTo(2))); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Test the equals() method of the UnicastWeightInstruction class. |
| 252 | */ |
| 253 | @Test |
| 254 | public void testUnicastWeightInstructionEquals() { |
| 255 | checkEqualsAndToString(uniWeight1, sameAsUniWeight1, uniWeight2); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Tests the hashCode() method of the UnicastWeightInstruction class. |
| 260 | */ |
| 261 | @Test |
| 262 | public void testUnicastWeightInstructionHashCode() { |
| 263 | assertThat(uniWeight1.hashCode(), is(equalTo(sameAsUniWeight1.hashCode()))); |
| 264 | assertThat(uniWeight1.hashCode(), is(not(equalTo(uniWeight2.hashCode())))); |
| 265 | } |
| 266 | |
| 267 | private final UnicastMappingInstruction uniPriority1 = |
| 268 | MappingInstructions.unicastPriority(1); |
| 269 | private final UnicastMappingInstruction sameAsUniPriority1 = |
| 270 | MappingInstructions.unicastPriority(1); |
| 271 | private final UnicastMappingInstruction uniPriority2 = |
| 272 | MappingInstructions.unicastPriority(2); |
| 273 | |
| 274 | /** |
| 275 | * Tests the unicastPriority method. |
| 276 | */ |
| 277 | @Test |
| 278 | public void testUnicastPriorityMethod() { |
| 279 | final MappingInstruction instruction = MappingInstructions.unicastPriority(2); |
| 280 | final UnicastMappingInstruction.PriorityMappingInstruction priorityMappingInstruction = |
| 281 | checkAndConvert(instruction, |
| 282 | UnicastMappingInstruction.Type.UNICAST, |
| 283 | UnicastMappingInstruction.PriorityMappingInstruction.class); |
| 284 | assertThat(priorityMappingInstruction.priority(), is(equalTo(2))); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Tests the equals() method of the UnicastPriorityInstruction class. |
| 289 | */ |
| 290 | @Test |
| 291 | public void testUnicastPriorityInstructionEquals() { |
| 292 | checkEqualsAndToString(uniPriority1, sameAsUniPriority1, uniPriority2); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Test the hashCode() method of the UnicastPriorityInstruction class. |
| 297 | */ |
| 298 | @Test |
| 299 | public void testUnicastPriorityInstructionHashCode() { |
| 300 | assertThat(uniPriority1.hashCode(), is(equalTo(sameAsUniPriority1.hashCode()))); |
| 301 | assertThat(uniPriority1.hashCode(), is(not(equalTo(uniPriority2.hashCode())))); |
| 302 | } |
| 303 | |
| 304 | private final MulticastMappingInstruction multiWeight1 = |
| 305 | MappingInstructions.multicastWeight(1); |
| 306 | private final MulticastMappingInstruction sameAsMultiWeight1 = |
| 307 | MappingInstructions.multicastWeight(1); |
| 308 | private final MulticastMappingInstruction multiWeight2 = |
| 309 | MappingInstructions.multicastWeight(2); |
| 310 | |
| 311 | /** |
| 312 | * Tests the multicastWeight method. |
| 313 | */ |
| 314 | @Test |
| 315 | public void testMulticastWeightMethod() { |
| 316 | final MappingInstruction instruction = MappingInstructions.multicastWeight(2); |
| 317 | final MulticastMappingInstruction.WeightMappingInstruction weightMappingInstruction = |
| 318 | checkAndConvert(instruction, |
| 319 | MulticastMappingInstruction.Type.MULTICAST, |
| 320 | MulticastMappingInstruction.WeightMappingInstruction.class); |
| 321 | assertThat(weightMappingInstruction.weight(), is(equalTo(2))); |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Tests the equals() method of the MulticastWeightInstruction class. |
| 326 | */ |
| 327 | @Test |
| 328 | public void testMulticastWeightInstructionEquals() { |
| 329 | checkEqualsAndToString(multiWeight1, sameAsMultiWeight1, multiWeight2); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Tests the hashCode() method of the MulticastWeightInstruction class. |
| 334 | */ |
| 335 | @Test |
| 336 | public void testMulticastWeightInstructionHashCode() { |
| 337 | assertThat(multiWeight1.hashCode(), is(equalTo(sameAsMultiWeight1.hashCode()))); |
| 338 | assertThat(multiWeight1.hashCode(), is(not(equalTo(multiWeight2.hashCode())))); |
| 339 | } |
| 340 | |
| 341 | private final MulticastMappingInstruction multiPriority1 = |
| 342 | MappingInstructions.multicastPriority(1); |
| 343 | private final MulticastMappingInstruction sameAsMultiPriority1 = |
| 344 | MappingInstructions.multicastPriority(1); |
| 345 | private final MulticastMappingInstruction multiPriority2 = |
| 346 | MappingInstructions.multicastPriority(2); |
| 347 | |
| 348 | /** |
| 349 | * Tests the multicastPriority method. |
| 350 | */ |
| 351 | @Test |
| 352 | public void testMulticastPriorityMethod() { |
| 353 | final MappingInstruction instruction = MappingInstructions.multicastPriority(2); |
| 354 | final MulticastMappingInstruction.PriorityMappingInstruction priorityMappingInstruction = |
| 355 | checkAndConvert(instruction, |
| 356 | MulticastMappingInstruction.Type.MULTICAST, |
| 357 | MulticastMappingInstruction.PriorityMappingInstruction.class); |
| 358 | assertThat(priorityMappingInstruction.priority(), is(equalTo(2))); |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Tests the equals() method of the MulticastPriorityInstruction class. |
| 363 | */ |
| 364 | @Test |
| 365 | public void testMulticastPriorityInstructionEquals() { |
| 366 | checkEqualsAndToString(multiPriority1, sameAsMultiPriority1, multiPriority2); |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Tests the hashCode() method of the MulticastPriorityInstruction class. |
| 371 | */ |
| 372 | @Test |
| 373 | public void testMulticastPriorityInstructionHashCode() { |
| 374 | assertThat(multiPriority1.hashCode(), is(equalTo(sameAsMultiPriority1.hashCode()))); |
| 375 | assertThat(multiPriority1.hashCode(), is(not(equalTo(multiPriority2.hashCode())))); |
| 376 | } |
| 377 | |
| 378 | // ExtensionMappingInstructionWrapper |
| 379 | |
| 380 | class MockExtensionTreatment implements ExtensionTreatment { |
| 381 | int type; |
| 382 | |
| 383 | MockExtensionTreatment(int type) { |
| 384 | this.type = type; |
| 385 | } |
| 386 | |
| 387 | @Override |
| 388 | public ExtensionTreatmentType type() { |
| 389 | return new ExtensionTreatmentType(type); |
| 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public <T> void setPropertyValue(String key, T value) throws ExtensionPropertyException { |
| 394 | |
| 395 | } |
| 396 | |
| 397 | @Override |
| 398 | public <T> T getPropertyValue(String key) throws ExtensionPropertyException { |
| 399 | return null; |
| 400 | } |
| 401 | |
| 402 | @Override |
| 403 | public List<String> getProperties() { |
| 404 | return null; |
| 405 | } |
| 406 | |
| 407 | @Override |
| 408 | public byte[] serialize() { |
| 409 | return new byte[0]; |
| 410 | } |
| 411 | |
| 412 | @Override |
| 413 | public void deserialize(byte[] data) { |
| 414 | |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | private final ExtensionTreatment extensionTreatment1 = new MockExtensionTreatment(111); |
| 419 | private final ExtensionTreatment extensionTreatment2 = new MockExtensionTreatment(222); |
| 420 | |
| 421 | private final DeviceId deviceId1 = DeviceId.deviceId("of:1"); |
| 422 | private final DeviceId deviceId2 = DeviceId.deviceId("of:2"); |
| 423 | |
| 424 | private final MappingInstruction extensionInstruction1 = |
| 425 | MappingInstructions.extension(extensionTreatment1, deviceId1); |
| 426 | private final MappingInstruction sameAsExtensionInstruction1 = |
| 427 | MappingInstructions.extension(extensionTreatment1, deviceId1); |
| 428 | private final MappingInstruction extensionInstruction2 = |
| 429 | MappingInstructions.extension(extensionTreatment2, deviceId2); |
| 430 | |
| 431 | /** |
| 432 | * Tests the extension method. |
| 433 | */ |
| 434 | @Test |
| 435 | public void testExtensionMethod() { |
| 436 | final MappingInstruction instruction = |
| 437 | MappingInstructions.extension(extensionTreatment1, deviceId1); |
| 438 | final MappingInstructions.ExtensionMappingInstructionWrapper wrapper = |
| 439 | checkAndConvert(instruction, |
| 440 | MappingInstruction.Type.EXTENSION, |
| 441 | MappingInstructions.ExtensionMappingInstructionWrapper.class); |
| 442 | assertThat(wrapper.deviceId(), is(deviceId1)); |
| 443 | assertThat(wrapper.extensionMappingInstruction(), is(extensionTreatment1)); |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Tests the equals() method of the ExtensionMappingInstructionWrapper class. |
| 448 | */ |
| 449 | @Test |
| 450 | public void testExtensionMappingInstructionWrapperEquals() { |
| 451 | checkEqualsAndToString(extensionInstruction1, |
| 452 | sameAsExtensionInstruction1, |
| 453 | extensionInstruction2); |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Tests the hashCode() method of the ExtensionMappingInstructionWrapper class. |
| 458 | */ |
| 459 | @Test |
| 460 | public void testExtensionMappingInstructionWrapperHashCode() { |
| 461 | assertThat(extensionInstruction1.hashCode(), |
| 462 | is(equalTo(sameAsExtensionInstruction1.hashCode()))); |
| 463 | assertThat(extensionInstruction1.hashCode(), |
| 464 | is(not(equalTo(extensionInstruction2.hashCode())))); |
| 465 | } |
Jian Li | 136fd6c | 2017-02-10 14:54:59 +0900 | [diff] [blame] | 466 | } |