Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 1 | package org.onlab.onos.sdnip.bgp; |
| 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 java.util.ArrayList; |
| 8 | |
| 9 | import org.junit.Test; |
| 10 | |
| 11 | /** |
| 12 | * Unit tests for the BgpRouteEntry.AsPath class. |
| 13 | */ |
| 14 | public class AsPathTest { |
| 15 | /** |
| 16 | * Generates an AS Path. |
| 17 | * |
| 18 | * @return a generated AS Path |
| 19 | */ |
| 20 | private BgpRouteEntry.AsPath generateAsPath() { |
| 21 | ArrayList<BgpRouteEntry.PathSegment> pathSegments = new ArrayList<>(); |
| 22 | byte pathSegmentType1 = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE; |
| 23 | ArrayList<Long> segmentAsNumbers1 = new ArrayList<>(); |
| 24 | segmentAsNumbers1.add((long) 1); |
| 25 | segmentAsNumbers1.add((long) 2); |
| 26 | segmentAsNumbers1.add((long) 3); |
| 27 | BgpRouteEntry.PathSegment pathSegment1 = |
| 28 | new BgpRouteEntry.PathSegment(pathSegmentType1, segmentAsNumbers1); |
| 29 | pathSegments.add(pathSegment1); |
| 30 | // |
| 31 | byte pathSegmentType2 = (byte) BgpConstants.Update.AsPath.AS_SET; |
| 32 | ArrayList<Long> segmentAsNumbers2 = new ArrayList<>(); |
| 33 | segmentAsNumbers2.add((long) 4); |
| 34 | segmentAsNumbers2.add((long) 5); |
| 35 | segmentAsNumbers2.add((long) 6); |
| 36 | BgpRouteEntry.PathSegment pathSegment2 = |
| 37 | new BgpRouteEntry.PathSegment(pathSegmentType2, segmentAsNumbers2); |
| 38 | pathSegments.add(pathSegment2); |
| 39 | // |
| 40 | BgpRouteEntry.AsPath asPath = new BgpRouteEntry.AsPath(pathSegments); |
| 41 | |
| 42 | return asPath; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Tests valid class constructor. |
| 47 | */ |
| 48 | @Test |
| 49 | public void testConstructor() { |
| 50 | BgpRouteEntry.AsPath asPath = generateAsPath(); |
| 51 | |
| 52 | String expectedString = |
| 53 | "AsPath{pathSegments=" + |
| 54 | "[PathSegment{type=2, segmentAsNumbers=[1, 2, 3]}, " + |
| 55 | "PathSegment{type=1, segmentAsNumbers=[4, 5, 6]}]}"; |
| 56 | assertThat(asPath.toString(), is(expectedString)); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Tests invalid class constructor for null Path Segments. |
| 61 | */ |
| 62 | @Test(expected = NullPointerException.class) |
| 63 | public void testInvalidConstructorNullPathSegments() { |
| 64 | ArrayList<BgpRouteEntry.PathSegment> pathSegments = null; |
| 65 | new BgpRouteEntry.AsPath(pathSegments); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Tests getting the fields of an AS Path. |
| 70 | */ |
| 71 | @Test |
| 72 | public void testGetFields() { |
| 73 | // Create the fields to compare against |
| 74 | ArrayList<BgpRouteEntry.PathSegment> pathSegments = new ArrayList<>(); |
| 75 | byte pathSegmentType1 = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE; |
| 76 | ArrayList<Long> segmentAsNumbers1 = new ArrayList<>(); |
| 77 | segmentAsNumbers1.add((long) 1); |
| 78 | segmentAsNumbers1.add((long) 2); |
| 79 | segmentAsNumbers1.add((long) 3); |
| 80 | BgpRouteEntry.PathSegment pathSegment1 = |
| 81 | new BgpRouteEntry.PathSegment(pathSegmentType1, segmentAsNumbers1); |
| 82 | pathSegments.add(pathSegment1); |
| 83 | // |
| 84 | byte pathSegmentType2 = (byte) BgpConstants.Update.AsPath.AS_SET; |
| 85 | ArrayList<Long> segmentAsNumbers2 = new ArrayList<>(); |
| 86 | segmentAsNumbers2.add((long) 4); |
| 87 | segmentAsNumbers2.add((long) 5); |
| 88 | segmentAsNumbers2.add((long) 6); |
| 89 | BgpRouteEntry.PathSegment pathSegment2 = |
| 90 | new BgpRouteEntry.PathSegment(pathSegmentType2, segmentAsNumbers2); |
| 91 | pathSegments.add(pathSegment2); |
| 92 | |
| 93 | // Generate the entry to test |
| 94 | BgpRouteEntry.AsPath asPath = generateAsPath(); |
| 95 | |
| 96 | assertThat(asPath.getPathSegments(), is(pathSegments)); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Tests getting the AS Path Length. |
| 101 | */ |
| 102 | @Test |
| 103 | public void testGetAsPathLength() { |
| 104 | BgpRouteEntry.AsPath asPath = generateAsPath(); |
| 105 | assertThat(asPath.getAsPathLength(), is(4)); |
| 106 | |
| 107 | // Create an empty AS Path |
| 108 | ArrayList<BgpRouteEntry.PathSegment> pathSegments = new ArrayList<>(); |
| 109 | asPath = new BgpRouteEntry.AsPath(pathSegments); |
| 110 | assertThat(asPath.getAsPathLength(), is(0)); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Tests equality of {@link BgpRouteEntry.AsPath}. |
| 115 | */ |
| 116 | @Test |
| 117 | public void testEquality() { |
| 118 | BgpRouteEntry.AsPath asPath1 = generateAsPath(); |
| 119 | BgpRouteEntry.AsPath asPath2 = generateAsPath(); |
| 120 | |
| 121 | assertThat(asPath1, is(asPath2)); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Tests non-equality of {@link BgpRouteEntry.AsPath}. |
| 126 | */ |
| 127 | @Test |
| 128 | public void testNonEquality() { |
| 129 | BgpRouteEntry.AsPath asPath1 = generateAsPath(); |
| 130 | |
| 131 | // Setup AS Path 2 |
| 132 | ArrayList<BgpRouteEntry.PathSegment> pathSegments = new ArrayList<>(); |
| 133 | byte pathSegmentType1 = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE; |
| 134 | ArrayList<Long> segmentAsNumbers1 = new ArrayList<>(); |
| 135 | segmentAsNumbers1.add((long) 1); |
| 136 | segmentAsNumbers1.add((long) 2); |
| 137 | segmentAsNumbers1.add((long) 3); |
| 138 | BgpRouteEntry.PathSegment pathSegment1 = |
| 139 | new BgpRouteEntry.PathSegment(pathSegmentType1, segmentAsNumbers1); |
| 140 | pathSegments.add(pathSegment1); |
| 141 | // |
| 142 | byte pathSegmentType2 = (byte) BgpConstants.Update.AsPath.AS_SET; |
| 143 | ArrayList<Long> segmentAsNumbers2 = new ArrayList<>(); |
| 144 | segmentAsNumbers2.add((long) 4); |
| 145 | segmentAsNumbers2.add((long) 55); // Different |
| 146 | segmentAsNumbers2.add((long) 6); |
| 147 | BgpRouteEntry.PathSegment pathSegment2 = |
| 148 | new BgpRouteEntry.PathSegment(pathSegmentType2, segmentAsNumbers2); |
| 149 | pathSegments.add(pathSegment2); |
| 150 | // |
| 151 | BgpRouteEntry.AsPath asPath2 = new BgpRouteEntry.AsPath(pathSegments); |
| 152 | |
| 153 | assertThat(asPath1, is(not(asPath2))); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Tests object string representation. |
| 158 | */ |
| 159 | @Test |
| 160 | public void testToString() { |
| 161 | BgpRouteEntry.AsPath asPath = generateAsPath(); |
| 162 | |
| 163 | String expectedString = |
| 164 | "AsPath{pathSegments=" + |
| 165 | "[PathSegment{type=2, segmentAsNumbers=[1, 2, 3]}, " + |
| 166 | "PathSegment{type=1, segmentAsNumbers=[4, 5, 6]}]}"; |
| 167 | assertThat(asPath.toString(), is(expectedString)); |
| 168 | } |
| 169 | } |