blob: d55bba76916ee5a52823755d337079c430ec83ca [file] [log] [blame]
Jian Li0e09eaa2017-02-14 02:01:18 +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.addresses;
17
18import com.google.common.testing.EqualsTester;
19import org.junit.Test;
20import org.onlab.packet.IpPrefix;
21import org.onlab.packet.MacAddress;
22
23import static org.hamcrest.MatcherAssert.assertThat;
24import static org.hamcrest.Matchers.equalTo;
25import static org.hamcrest.Matchers.instanceOf;
26import static org.hamcrest.Matchers.is;
27import static org.hamcrest.Matchers.notNullValue;
28import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
29import static org.onlab.junit.UtilityClassChecker.assertThatClassIsUtility;
30
31/**
32 * Unit tests for various mapping address implementation classes.
33 */
34public class MappingAddressesTest {
35
36 private final String asn1 = "1";
37 private final String asn2 = "2";
38 private MappingAddress asMa1 = MappingAddresses.asMappingAddress(asn1);
39 private MappingAddress sameAsAsMa1 = MappingAddresses.asMappingAddress(asn1);
40 private MappingAddress asMa2 = MappingAddresses.asMappingAddress(asn2);
41
42 private final String dn1 = "1";
43 private final String dn2 = "2";
44 private MappingAddress dnMa1 = MappingAddresses.dnMappingAddress(dn1);
45 private MappingAddress sameAsDnMa1 = MappingAddresses.dnMappingAddress(dn1);
46 private MappingAddress dnMa2 = MappingAddresses.dnMappingAddress(dn2);
47
48 private static final String MAC1 = "00:00:00:00:00:01";
49 private static final String MAC2 = "00:00:00:00:00:02";
50 private MacAddress mac1 = MacAddress.valueOf(MAC1);
51 private MacAddress mac2 = MacAddress.valueOf(MAC2);
52 private MappingAddress ethMa1 = MappingAddresses.ethMappingAddress(mac1);
53 private MappingAddress sameAsEthMa1 = MappingAddresses.ethMappingAddress(mac1);
54 private MappingAddress ethMa2 = MappingAddresses.ethMappingAddress(mac2);
55
56 private static final String IP1 = "1.2.3.4/24";
57 private static final String IP2 = "5.6.7.8/24";
58 private static final String IPV61 = "fe80::1/64";
59 private static final String IPV62 = "fc80::2/64";
60 private IpPrefix ip1 = IpPrefix.valueOf(IP1);
61 private IpPrefix ip2 = IpPrefix.valueOf(IP2);
62 private IpPrefix ipv61 = IpPrefix.valueOf(IPV61);
63 private IpPrefix ipv62 = IpPrefix.valueOf(IPV62);
64 private MappingAddress ipMa1 = MappingAddresses.ipv4MappingAddress(ip1);
65 private MappingAddress sameAsIpMa1 = MappingAddresses.ipv4MappingAddress(ip1);
66 private MappingAddress ipMa2 = MappingAddresses.ipv4MappingAddress(ip2);
67 private MappingAddress ipv6Ma1 = MappingAddresses.ipv6MappingAddress(ipv61);
68 private MappingAddress sameAsIpv6Ma1 = MappingAddresses.ipv6MappingAddress(ipv61);
69 private MappingAddress ipv6Ma2 = MappingAddresses.ipv6MappingAddress(ipv62);
70
71 /**
72 * Checks that a MappingAddress object has the proper type, and then converts
73 * it to the proper type.
74 *
75 * @param address MappingAddress object to convert
76 * @param type Enumerated type value for the MappingAddress class
77 * @param clazz Desired MappingAddress class
78 * @param <T> The type the caller wants returned
79 * @return converted object
80 */
81 @SuppressWarnings("unchecked")
82 private <T> T checkAndConvert(MappingAddress address, MappingAddress.Type type, Class clazz) {
83 assertThat(address, is(notNullValue()));
84 assertThat(address.type(), is(equalTo(type)));
85 assertThat(address, instanceOf(clazz));
86 return (T) address;
87 }
88
89 /**
90 * Checks the equals() and toString() methods of a MappingAddress class.
91 *
92 * @param c1 first object to compare
93 * @param c1match object that should be equal to the first
94 * @param c2 object that should not be equal to the first
95 * @param <T> type of the arguments
96 */
97 private <T extends MappingAddress> void checkEqualsAndToString(T c1,
98 T c1match,
99 T c2) {
100 new EqualsTester()
101 .addEqualityGroup(c1, c1match)
102 .addEqualityGroup(c2)
103 .testEquals();
104 }
105
106 /**
107 * Checks that the MappingAddresses class is a valid utility class.
108 */
109 @Test
110 public void testMappingAddressesUtility() {
111 assertThatClassIsUtility(MappingAddresses.class);
112 }
113
114 /**
115 * Checks that the mapping address implementations are immutable.
116 */
117 @Test
118 public void testMappingAddressesImmutability() {
119 assertThatClassIsImmutable(ASMappingAddress.class);
120 assertThatClassIsImmutable(DNMappingAddress.class);
121 assertThatClassIsImmutable(EthMappingAddress.class);
122 assertThatClassIsImmutable(ExtensionMappingAddress.class);
123 assertThatClassIsImmutable(IPMappingAddress.class);
124 }
125
126 /**
127 * Tests the asMappingAddress method.
128 */
129 @Test
130 public void testAsMappingAddressMethod() {
131 String asn = "1";
132 MappingAddress mappingAddress = MappingAddresses.asMappingAddress(asn);
133 ASMappingAddress asMappingAddress =
134 checkAndConvert(mappingAddress,
135 MappingAddress.Type.AS,
136 ASMappingAddress.class);
137 assertThat(asMappingAddress.asNumber(), is(equalTo(asn)));
138 }
139
140 /**
141 * Tests the equals() method of the ASMappingAddress class.
142 */
143 @Test
144 public void testAsMappingAddressEquals() {
145 checkEqualsAndToString(asMa1, sameAsAsMa1, asMa2);
146 }
147
148 /**
149 * Tests the dnMappingAddress method.
150 */
151 @Test
152 public void testDnMappingAddressMethod() {
153 String dn = "1";
154 MappingAddress mappingAddress = MappingAddresses.dnMappingAddress(dn);
155 DNMappingAddress dnMappingAddress =
156 checkAndConvert(mappingAddress,
157 MappingAddress.Type.DN,
158 DNMappingAddress.class);
159 assertThat(dnMappingAddress.name(), is(equalTo(dn)));
160 }
161
162 /**
163 * Tests the equals() method of the DNMappingAddress class.
164 */
165 @Test
166 public void testDnMappingAddressEquals() {
167 checkEqualsAndToString(dnMa1, sameAsDnMa1, dnMa2);
168 }
169
170 /**
171 * Tests the ethMappingAddress method.
172 */
173 @Test
174 public void testEthMappingAddressMethod() {
175 MacAddress mac = MacAddress.valueOf("00:00:00:00:00:01");
176 MappingAddress mappingAddress = MappingAddresses.ethMappingAddress(mac);
177 EthMappingAddress ethMappingAddress =
178 checkAndConvert(mappingAddress,
179 MappingAddress.Type.ETH,
180 EthMappingAddress.class);
181 assertThat(ethMappingAddress.mac(), is(equalTo(mac)));
182 }
183
184 /**
185 * Tests the equals() method of the EthMappingAddress class.
186 */
187 @Test
188 public void testEthMappingAddressEquals() {
189 checkEqualsAndToString(ethMa1, sameAsEthMa1, ethMa2);
190 }
191
192 /**
193 * Tests the ipv4MappingAddress method.
194 */
195 @Test
196 public void testIpv4MappingAddressMethod() {
197 IpPrefix ip = IpPrefix.valueOf("1.2.3.4/24");
198 MappingAddress mappingAddress = MappingAddresses.ipv4MappingAddress(ip);
199 IPMappingAddress ipMappingAddress =
200 checkAndConvert(mappingAddress,
201 MappingAddress.Type.IPV4,
202 IPMappingAddress.class);
203 assertThat(ipMappingAddress.ip(), is(equalTo(ip)));
204 }
205
206 /**
207 * Tests the equals() method of the IPMappingAddress class.
208 */
209 @Test
210 public void testIpv4MappingAddressEquals() {
211 checkEqualsAndToString(ipMa1, sameAsIpMa1, ipMa2);
212 }
213
214 /**
215 * Tests the ipv6MappingAddress method.
216 */
217 @Test
218 public void testIpv6MappingAddressMethod() {
219 IpPrefix ipv6 = IpPrefix.valueOf("fe80::1/64");
220 MappingAddress mappingAddress = MappingAddresses.ipv6MappingAddress(ipv6);
221 IPMappingAddress ipMappingAddress =
222 checkAndConvert(mappingAddress,
223 MappingAddress.Type.IPV6,
224 IPMappingAddress.class);
225 assertThat(ipMappingAddress.ip(), is(equalTo(ipv6)));
226 }
227
228 /**
229 * Tests the equals() method of the IPMappingAddress class.
230 */
231 @Test
232 public void testIpv6MappingAddressEquals() {
233 checkEqualsAndToString(ipv6Ma1, sameAsIpv6Ma1, ipv6Ma2);
234 }
235}