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.actions.MappingAction; |
| 22 | import org.onosproject.mapping.actions.MappingActions; |
| 23 | import org.onosproject.mapping.addresses.MappingAddress; |
| 24 | import org.onosproject.mapping.addresses.MappingAddresses; |
| 25 | |
| 26 | import static org.hamcrest.MatcherAssert.assertThat; |
| 27 | import static org.hamcrest.Matchers.hasSize; |
| 28 | import static org.hamcrest.Matchers.is; |
| 29 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 30 | |
| 31 | /** |
| 32 | * Unit tests for the default mapping value class. |
| 33 | */ |
| 34 | public class DefaultMappingValueTest { |
| 35 | |
| 36 | private static final String IP_ADDRESS_1 = "1.2.3.4/24"; |
| 37 | private static final String IP_ADDRESS_2 = "5.6.7.8/24"; |
| 38 | private final IpPrefix ip1 = IpPrefix.valueOf(IP_ADDRESS_1); |
| 39 | private final IpPrefix ip2 = IpPrefix.valueOf(IP_ADDRESS_2); |
| 40 | private final MappingAddress ma1 = MappingAddresses.ipv4MappingAddress(ip1); |
| 41 | private final MappingAddress ma2 = MappingAddresses.ipv4MappingAddress(ip2); |
| 42 | |
| 43 | /** |
| 44 | * Checks that the DefaultMappingValue class is immutable. |
| 45 | */ |
| 46 | @Test |
| 47 | public void testImmutability() { |
| 48 | assertThatClassIsImmutable(DefaultMappingValue.class); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Tests methods defined on the Builder. |
| 53 | */ |
| 54 | @Test |
| 55 | public void testBuilderMethods() { |
| 56 | MappingAction action = MappingActions.noAction(); |
| 57 | MappingTreatment treatment = DefaultMappingTreatment.builder() |
| 58 | .withAddress(ma1) |
| 59 | .setUnicastPriority(10) |
| 60 | .setUnicastWeight(10) |
| 61 | .build(); |
| 62 | |
| 63 | MappingValue value = DefaultMappingValue.builder() |
| 64 | .withAction(action) |
| 65 | .add(treatment) |
| 66 | .build(); |
| 67 | |
| 68 | assertThat(value.action(), is(action)); |
| 69 | assertThat(value.treatments(), hasSize(1)); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Tests equals() method. |
| 74 | */ |
| 75 | @Test |
| 76 | public void testEquals() { |
| 77 | MappingTreatment treatment1 = DefaultMappingTreatment.builder() |
| 78 | .withAddress(ma1) |
| 79 | .setUnicastPriority(10) |
| 80 | .setUnicastWeight(10) |
| 81 | .build(); |
| 82 | |
| 83 | MappingTreatment treatment2 = DefaultMappingTreatment.builder() |
| 84 | .withAddress(ma2) |
| 85 | .setUnicastPriority(20) |
| 86 | .setUnicastWeight(20) |
| 87 | .build(); |
| 88 | |
| 89 | MappingAction noAction = MappingActions.noAction(); |
| 90 | MappingAction forward = MappingActions.forward(); |
| 91 | |
| 92 | MappingValue value1 = DefaultMappingValue.builder() |
| 93 | .withAction(noAction) |
| 94 | .add(treatment1) |
| 95 | .build(); |
| 96 | |
| 97 | MappingValue sameAsValue1 = DefaultMappingValue.builder() |
| 98 | .withAction(noAction) |
| 99 | .add(treatment1) |
| 100 | .build(); |
| 101 | |
| 102 | MappingValue value2 = DefaultMappingValue.builder() |
| 103 | .withAction(forward) |
| 104 | .add(treatment2) |
| 105 | .build(); |
| 106 | |
| 107 | new EqualsTester() |
| 108 | .addEqualityGroup(value1, sameAsValue1) |
| 109 | .addEqualityGroup(value2) |
| 110 | .testEquals(); |
| 111 | } |
| 112 | } |