Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame^] | 1 | package org.onlab.onos.sdnip; |
| 2 | |
| 3 | import static org.hamcrest.Matchers.is; |
| 4 | import static org.hamcrest.Matchers.not; |
| 5 | import static org.junit.Assert.assertThat; |
| 6 | |
| 7 | import org.junit.Test; |
| 8 | import org.onlab.packet.IpAddress; |
| 9 | import org.onlab.packet.IpPrefix; |
| 10 | |
| 11 | /** |
| 12 | * Unit tests for the RouteEntry class. |
| 13 | */ |
| 14 | public class RouteEntryTest { |
| 15 | /** |
| 16 | * Tests valid class constructor. |
| 17 | */ |
| 18 | @Test |
| 19 | public void testConstructor() { |
| 20 | IpPrefix prefix = IpPrefix.valueOf("1.2.3.0/24"); |
| 21 | IpAddress nextHop = IpAddress.valueOf("5.6.7.8"); |
| 22 | |
| 23 | RouteEntry routeEntry = new RouteEntry(prefix, nextHop); |
| 24 | assertThat(routeEntry.toString(), |
| 25 | is("RouteEntry{prefix=1.2.3.0/24, nextHop=5.6.7.8}")); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Tests invalid class constructor for null IPv4 prefix. |
| 30 | */ |
| 31 | @Test(expected = NullPointerException.class) |
| 32 | public void testInvalidConstructorNullPrefix() { |
| 33 | IpPrefix prefix = null; |
| 34 | IpAddress nextHop = IpAddress.valueOf("5.6.7.8"); |
| 35 | |
| 36 | new RouteEntry(prefix, nextHop); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Tests invalid class constructor for null IPv4 next-hop. |
| 41 | */ |
| 42 | @Test(expected = NullPointerException.class) |
| 43 | public void testInvalidConstructorNullNextHop() { |
| 44 | IpPrefix prefix = IpPrefix.valueOf("1.2.3.0/24"); |
| 45 | IpAddress nextHop = null; |
| 46 | |
| 47 | new RouteEntry(prefix, nextHop); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Tests getting the fields of a route entry. |
| 52 | */ |
| 53 | @Test |
| 54 | public void testGetFields() { |
| 55 | IpPrefix prefix = IpPrefix.valueOf("1.2.3.0/24"); |
| 56 | IpAddress nextHop = IpAddress.valueOf("5.6.7.8"); |
| 57 | |
| 58 | RouteEntry routeEntry = new RouteEntry(prefix, nextHop); |
| 59 | assertThat(routeEntry.prefix(), is(prefix)); |
| 60 | assertThat(routeEntry.nextHop(), is(nextHop)); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Tests creating a binary string from IPv4 prefix. |
| 65 | */ |
| 66 | @Test |
| 67 | public void testCreateBinaryString() { |
| 68 | IpPrefix prefix; |
| 69 | |
| 70 | prefix = IpPrefix.valueOf("0.0.0.0/0"); |
| 71 | assertThat(RouteEntry.createBinaryString(prefix), is("")); |
| 72 | |
| 73 | prefix = IpPrefix.valueOf("192.168.166.0/22"); |
| 74 | assertThat(RouteEntry.createBinaryString(prefix), |
| 75 | is("1100000010101000101001")); |
| 76 | |
| 77 | prefix = IpPrefix.valueOf("192.168.166.0/23"); |
| 78 | assertThat(RouteEntry.createBinaryString(prefix), |
| 79 | is("11000000101010001010011")); |
| 80 | |
| 81 | prefix = IpPrefix.valueOf("192.168.166.0/24"); |
| 82 | assertThat(RouteEntry.createBinaryString(prefix), |
| 83 | is("110000001010100010100110")); |
| 84 | |
| 85 | prefix = IpPrefix.valueOf("130.162.10.1/25"); |
| 86 | assertThat(RouteEntry.createBinaryString(prefix), |
| 87 | is("1000001010100010000010100")); |
| 88 | |
| 89 | prefix = IpPrefix.valueOf("255.255.255.255/32"); |
| 90 | assertThat(RouteEntry.createBinaryString(prefix), |
| 91 | is("11111111111111111111111111111111")); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Tests equality of {@link RouteEntry}. |
| 96 | */ |
| 97 | @Test |
| 98 | public void testEquality() { |
| 99 | IpPrefix prefix1 = IpPrefix.valueOf("1.2.3.0/24"); |
| 100 | IpAddress nextHop1 = IpAddress.valueOf("5.6.7.8"); |
| 101 | RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1); |
| 102 | |
| 103 | IpPrefix prefix2 = IpPrefix.valueOf("1.2.3.0/24"); |
| 104 | IpAddress nextHop2 = IpAddress.valueOf("5.6.7.8"); |
| 105 | RouteEntry routeEntry2 = new RouteEntry(prefix2, nextHop2); |
| 106 | |
| 107 | assertThat(routeEntry1, is(routeEntry2)); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Tests non-equality of {@link RouteEntry}. |
| 112 | */ |
| 113 | @Test |
| 114 | public void testNonEquality() { |
| 115 | IpPrefix prefix1 = IpPrefix.valueOf("1.2.3.0/24"); |
| 116 | IpAddress nextHop1 = IpAddress.valueOf("5.6.7.8"); |
| 117 | RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1); |
| 118 | |
| 119 | IpPrefix prefix2 = IpPrefix.valueOf("1.2.3.0/25"); // Different |
| 120 | IpAddress nextHop2 = IpAddress.valueOf("5.6.7.8"); |
| 121 | RouteEntry routeEntry2 = new RouteEntry(prefix2, nextHop2); |
| 122 | |
| 123 | IpPrefix prefix3 = IpPrefix.valueOf("1.2.3.0/24"); |
| 124 | IpAddress nextHop3 = IpAddress.valueOf("5.6.7.9"); // Different |
| 125 | RouteEntry routeEntry3 = new RouteEntry(prefix3, nextHop3); |
| 126 | |
| 127 | assertThat(routeEntry1, is(not(routeEntry2))); |
| 128 | assertThat(routeEntry1, is(not(routeEntry3))); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Tests object string representation. |
| 133 | */ |
| 134 | @Test |
| 135 | public void testToString() { |
| 136 | IpPrefix prefix = IpPrefix.valueOf("1.2.3.0/24"); |
| 137 | IpAddress nextHop = IpAddress.valueOf("5.6.7.8"); |
| 138 | RouteEntry routeEntry = new RouteEntry(prefix, nextHop); |
| 139 | |
| 140 | assertThat(routeEntry.toString(), |
| 141 | is("RouteEntry{prefix=1.2.3.0/24, nextHop=5.6.7.8}")); |
| 142 | } |
| 143 | } |