Ayaka Koshibe

added Host-related unit tests

Change-Id: Ia3c0610f5bdd91ddd554dd477af7ad3640a6cef3
1 +package org.onlab.onos.net;
2 +
3 +import static org.junit.Assert.assertEquals;
4 +
5 +import org.junit.Test;
6 +
7 +import com.google.common.testing.EqualsTester;
8 +
9 +public class DefaultHostTest extends TestDeviceParams {
10 +
11 + @Test
12 + public void testEquality() {
13 + Host h1 = new DefaultHost(PID, HID1, MAC1, VLAN1, LOC1, IPSET1);
14 + Host h2 = new DefaultHost(PID, HID1, MAC1, VLAN1, LOC1, IPSET1);
15 + Host h3 = new DefaultHost(PID, HID2, MAC2, VLAN2, LOC2, IPSET2);
16 + Host h4 = new DefaultHost(PID, HID2, MAC2, VLAN2, LOC2, IPSET2);
17 + Host h5 = new DefaultHost(PID, HID2, MAC2, VLAN1, LOC2, IPSET1);
18 +
19 + new EqualsTester().addEqualityGroup(h1, h2)
20 + .addEqualityGroup(h3, h4)
21 + .addEqualityGroup(h5)
22 + .testEquals();
23 + }
24 +
25 + @Test
26 + public void basics() {
27 + Host host = new DefaultHost(PID, HID1, MAC1, VLAN1, LOC1, IPSET1);
28 + assertEquals("incorrect provider", PID, host.providerId());
29 + assertEquals("incorrect id", HID1, host.id());
30 + assertEquals("incorrect type", MAC1, host.mac());
31 + assertEquals("incorrect VLAN", VLAN1, host.vlan());
32 + assertEquals("incorrect location", LOC1, host.location());
33 + assertEquals("incorrect IP's", IPSET1, host.ipAddresses());
34 + }
35 +
36 +}
1 package org.onlab.onos.net; 1 package org.onlab.onos.net;
2 2
3 import com.google.common.testing.EqualsTester; 3 import com.google.common.testing.EqualsTester;
4 +
4 import org.junit.Test; 5 import org.junit.Test;
6 +import org.onlab.packet.MACAddress;
7 +import org.onlab.packet.VLANID;
5 8
6 import static org.onlab.onos.net.HostId.hostId; 9 import static org.onlab.onos.net.HostId.hostId;
7 10
8 /** 11 /**
9 - * Test of the host identifier. 12 + * Test for the host identifier.
10 */ 13 */
11 public class HostIdTest extends ElementIdTest { 14 public class HostIdTest extends ElementIdTest {
12 15
16 + private static final MACAddress MAC1 = MACAddress.valueOf("00:11:00:00:00:01");
17 + private static final MACAddress MAC2 = MACAddress.valueOf("00:22:00:00:00:02");
18 + private static final VLANID VLAN1 = VLANID.vlanId((short) 11);
19 + private static final VLANID VLAN2 = VLANID.vlanId((short) 22);
20 +
21 + @Override
13 @Test 22 @Test
14 public void basics() { 23 public void basics() {
15 new EqualsTester() 24 new EqualsTester()
16 - .addEqualityGroup(hostId("nic:foo"), 25 + .addEqualityGroup(hostId("nic:00:11:00:00:00:01/11"),
17 - hostId("nic:foo")) 26 + hostId(MAC1, VLAN1))
18 - .addEqualityGroup(hostId("nic:bar")) 27 + .addEqualityGroup(hostId(MAC2, VLAN2))
19 .testEquals(); 28 .testEquals();
20 } 29 }
21 30
......
1 +package org.onlab.onos.net;
2 +
3 +import static org.onlab.onos.net.DeviceId.deviceId;
4 +
5 +import java.util.Set;
6 +
7 +import org.onlab.onos.net.provider.ProviderId;
8 +import org.onlab.packet.IPAddress;
9 +import org.onlab.packet.MACAddress;
10 +import org.onlab.packet.VLANID;
11 +
12 +import com.google.common.collect.Sets;
13 +
14 +/**
15 + * Provides a set of test DefaultDevice parameters for use with Host-
16 + * related tests.
17 + */
18 +public abstract class TestDeviceParams {
19 +
20 + protected static final ProviderId PID = new ProviderId("foo");
21 + protected static final DeviceId DID1 = deviceId("of:foo");
22 + protected static final DeviceId DID2 = deviceId("of:bar");
23 + protected static final MACAddress MAC1 = MACAddress.valueOf("00:11:00:00:00:01");
24 + protected static final MACAddress MAC2 = MACAddress.valueOf("00:22:00:00:00:02");
25 + protected static final VLANID VLAN1 = VLANID.vlanId((short) 11);
26 + protected static final VLANID VLAN2 = VLANID.vlanId((short) 22);
27 + protected static final IPAddress IP1 = IPAddress.valueOf("10.0.0.1");
28 + protected static final IPAddress IP2 = IPAddress.valueOf("10.0.0.2");
29 + protected static final IPAddress IP3 = IPAddress.valueOf("10.0.0.3");
30 +
31 + protected static final PortNumber P1 = PortNumber.portNumber(100);
32 + protected static final PortNumber P2 = PortNumber.portNumber(200);
33 + protected static final HostId HID1 = HostId.hostId(MAC1, VLAN1);
34 + protected static final HostId HID2 = HostId.hostId(MAC2, VLAN2);
35 + protected static final HostLocation LOC1 = new HostLocation(DID1, P1, 123L);
36 + protected static final HostLocation LOC2 = new HostLocation(DID2, P2, 123L);
37 + protected static final Set<IPAddress> IPSET1 = Sets.newHashSet(IP1, IP2);
38 + protected static final Set<IPAddress> IPSET2 = Sets.newHashSet(IP1, IP3);
39 +
40 +}
...@@ -57,7 +57,7 @@ public class IPAddress { ...@@ -57,7 +57,7 @@ public class IPAddress {
57 * @return an IP address 57 * @return an IP address
58 */ 58 */
59 public static IPAddress valueOf(String address) { 59 public static IPAddress valueOf(String address) {
60 - final String [] parts = address.split("."); 60 + final String [] parts = address.split("\\.");
61 if (parts.length != INET_LEN) { 61 if (parts.length != INET_LEN) {
62 throw new IllegalArgumentException("Malformed IP address string; " 62 throw new IllegalArgumentException("Malformed IP address string; "
63 + "Addres must have four decimal values separated by dots (.)"); 63 + "Addres must have four decimal values separated by dots (.)");
...@@ -119,7 +119,9 @@ public class IPAddress { ...@@ -119,7 +119,9 @@ public class IPAddress {
119 return true; 119 return true;
120 } 120 }
121 if (obj instanceof IPAddress) { 121 if (obj instanceof IPAddress) {
122 +
122 IPAddress other = (IPAddress) obj; 123 IPAddress other = (IPAddress) obj;
124 +
123 if (!(this.version.equals(other.version))) { 125 if (!(this.version.equals(other.version))) {
124 return false; 126 return false;
125 } 127 }
......
...@@ -37,12 +37,12 @@ public class VLANID { ...@@ -37,12 +37,12 @@ public class VLANID {
37 } 37 }
38 38
39 if (obj instanceof VLANID) { 39 if (obj instanceof VLANID) {
40 - return true;
41 - }
42 40
43 - VLANID other = (VLANID) obj; 41 + VLANID other = (VLANID) obj;
44 - if (this.value == other.value) { 42 +
45 - return true; 43 + if (this.value == other.value) {
44 + return true;
45 + }
46 } 46 }
47 47
48 return false; 48 return false;
......