blob: 6c857d6a24da224ed2d7ae51ee2290eae2f95090 [file] [log] [blame]
Jian Li44155b02017-02-15 17:03:38 +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 */
16package org.onosproject.mapping;
17
18import com.google.common.testing.EqualsTester;
19import org.junit.Test;
20import org.onlab.packet.IpPrefix;
21import org.onosproject.mapping.actions.MappingAction;
22import org.onosproject.mapping.actions.MappingActions;
23import org.onosproject.mapping.addresses.MappingAddress;
24import org.onosproject.mapping.addresses.MappingAddresses;
25
26import static org.hamcrest.MatcherAssert.assertThat;
27import static org.hamcrest.Matchers.hasSize;
28import static org.hamcrest.Matchers.is;
29import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
30
31/**
32 * Unit tests for the default mapping value class.
33 */
34public 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}