Jian Li | 44155b0 | 2017-02-15 17:03:38 +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 | 44155b0 | 2017-02-15 17:03:38 +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; |
| 17 | |
| 18 | import com.google.common.testing.EqualsTester; |
| 19 | import org.junit.Test; |
| 20 | import org.onlab.packet.IpPrefix; |
| 21 | import org.onosproject.mapping.addresses.MappingAddress; |
| 22 | import org.onosproject.mapping.addresses.MappingAddresses; |
| 23 | |
| 24 | import static org.hamcrest.MatcherAssert.assertThat; |
| 25 | import static org.hamcrest.Matchers.hasSize; |
| 26 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 27 | |
| 28 | /** |
| 29 | * Unit tests for the default mapping treatment class. |
| 30 | */ |
| 31 | public class DefaultMappingTreatmentTest { |
| 32 | |
| 33 | private static final String IP_ADDRESS_1 = "1.2.3.4/24"; |
| 34 | private static final String IP_ADDRESS_2 = "5.6.7.8/24"; |
| 35 | |
| 36 | /** |
| 37 | * Checks that the DefaultMappingTreatment class is immutable. |
| 38 | */ |
| 39 | @Test |
| 40 | public void testImmutability() { |
| 41 | assertThatClassIsImmutable(DefaultMappingTreatment.class); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Tests method defined on the Builder. |
| 46 | */ |
| 47 | @Test |
| 48 | public void testBuilderMethods() { |
| 49 | IpPrefix ip = IpPrefix.valueOf(IP_ADDRESS_1); |
| 50 | MappingAddress address = MappingAddresses.ipv4MappingAddress(ip); |
| 51 | |
| 52 | MappingTreatment.Builder builder = |
| 53 | DefaultMappingTreatment.builder() |
| 54 | .withAddress(address) |
| 55 | .setUnicastPriority(10) |
| 56 | .setUnicastWeight(10); |
| 57 | MappingTreatment treatment = builder.build(); |
| 58 | assertThat(treatment.instructions(), hasSize(2)); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Tests illegal unicast type instruction construction. |
| 63 | */ |
| 64 | @Test(expected = IllegalArgumentException.class) |
| 65 | public void testIllegalUnicastTypeConstruction() { |
| 66 | IpPrefix ip = IpPrefix.valueOf(IP_ADDRESS_1); |
| 67 | MappingAddress address = MappingAddresses.ipv4MappingAddress(ip); |
| 68 | |
| 69 | DefaultMappingTreatment.builder() |
| 70 | .withAddress(address) |
| 71 | .setUnicastPriority(10) |
| 72 | .setUnicastWeight(10) |
| 73 | .setUnicastPriority(20) |
| 74 | .build(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Tests illegal multicast type instruction construction. |
| 79 | */ |
| 80 | @Test(expected = IllegalArgumentException.class) |
| 81 | public void testIllegalMulticastTypeConstruction() { |
| 82 | IpPrefix ip = IpPrefix.valueOf(IP_ADDRESS_1); |
| 83 | MappingAddress address = MappingAddresses.ipv4MappingAddress(ip); |
| 84 | |
| 85 | DefaultMappingTreatment.builder() |
| 86 | .withAddress(address) |
| 87 | .setMulticastPriority(10) |
| 88 | .setMulticastWeight(10) |
| 89 | .setMulticastPriority(20) |
| 90 | .build(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Tests equals() method. |
| 95 | */ |
| 96 | @Test |
| 97 | public void testEquals() { |
| 98 | IpPrefix ip1 = IpPrefix.valueOf(IP_ADDRESS_1); |
| 99 | MappingAddress address1 = MappingAddresses.ipv4MappingAddress(ip1); |
| 100 | |
| 101 | MappingTreatment treatment1 = DefaultMappingTreatment.builder() |
| 102 | .withAddress(address1) |
| 103 | .setUnicastPriority(10) |
| 104 | .setUnicastWeight(10) |
| 105 | .build(); |
| 106 | |
| 107 | MappingTreatment sameAsTreatment1 = DefaultMappingTreatment.builder() |
| 108 | .withAddress(address1) |
| 109 | .setUnicastPriority(10) |
| 110 | .setUnicastWeight(10) |
| 111 | .build(); |
| 112 | |
| 113 | IpPrefix ip2 = IpPrefix.valueOf(IP_ADDRESS_2); |
| 114 | MappingAddress address2 = MappingAddresses.ipv4MappingAddress(ip2); |
| 115 | |
| 116 | MappingTreatment treatment2 = DefaultMappingTreatment.builder() |
| 117 | .withAddress(address2) |
| 118 | .setMulticastPriority(20) |
| 119 | .setMulticastWeight(20) |
| 120 | .build(); |
| 121 | |
| 122 | new EqualsTester() |
| 123 | .addEqualityGroup(treatment1, sameAsTreatment1) |
| 124 | .addEqualityGroup(treatment2) |
| 125 | .testEquals(); |
| 126 | } |
| 127 | } |