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.PathSegment class. |
| 13 | */ |
| 14 | public class PathSegmentTest { |
| 15 | /** |
| 16 | * Generates a Path Segment. |
| 17 | * |
| 18 | * @return a generated PathSegment |
| 19 | */ |
| 20 | private BgpRouteEntry.PathSegment generatePathSegment() { |
| 21 | byte pathSegmentType = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE; |
| 22 | ArrayList<Long> segmentAsNumbers = new ArrayList<>(); |
| 23 | segmentAsNumbers.add((long) 1); |
| 24 | segmentAsNumbers.add((long) 2); |
| 25 | segmentAsNumbers.add((long) 3); |
| 26 | BgpRouteEntry.PathSegment pathSegment = |
| 27 | new BgpRouteEntry.PathSegment(pathSegmentType, segmentAsNumbers); |
| 28 | |
| 29 | return pathSegment; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Tests valid class constructor. |
| 34 | */ |
| 35 | @Test |
| 36 | public void testConstructor() { |
| 37 | BgpRouteEntry.PathSegment pathSegment = generatePathSegment(); |
| 38 | |
| 39 | String expectedString = |
| 40 | "PathSegment{type=2, segmentAsNumbers=[1, 2, 3]}"; |
| 41 | assertThat(pathSegment.toString(), is(expectedString)); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Tests invalid class constructor for null Segment AS Numbers. |
| 46 | */ |
| 47 | @Test(expected = NullPointerException.class) |
| 48 | public void testInvalidConstructorNullSegmentAsNumbers() { |
| 49 | byte pathSegmentType = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE; |
| 50 | ArrayList<Long> segmentAsNumbers = null; |
| 51 | new BgpRouteEntry.PathSegment(pathSegmentType, segmentAsNumbers); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Tests getting the fields of a Path Segment. |
| 56 | */ |
| 57 | @Test |
| 58 | public void testGetFields() { |
| 59 | // Create the fields to compare against |
| 60 | byte pathSegmentType = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE; |
| 61 | ArrayList<Long> segmentAsNumbers = new ArrayList<>(); |
| 62 | segmentAsNumbers.add((long) 1); |
| 63 | segmentAsNumbers.add((long) 2); |
| 64 | segmentAsNumbers.add((long) 3); |
| 65 | |
| 66 | // Generate the entry to test |
| 67 | BgpRouteEntry.PathSegment pathSegment = generatePathSegment(); |
| 68 | |
| 69 | assertThat(pathSegment.getType(), is(pathSegmentType)); |
| 70 | assertThat(pathSegment.getSegmentAsNumbers(), is(segmentAsNumbers)); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Tests equality of {@link BgpRouteEntry.PathSegment}. |
| 75 | */ |
| 76 | @Test |
| 77 | public void testEquality() { |
| 78 | BgpRouteEntry.PathSegment pathSegment1 = generatePathSegment(); |
| 79 | BgpRouteEntry.PathSegment pathSegment2 = generatePathSegment(); |
| 80 | |
| 81 | assertThat(pathSegment1, is(pathSegment2)); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Tests non-equality of {@link BgpRouteEntry.PathSegment}. |
| 86 | */ |
| 87 | @Test |
| 88 | public void testNonEquality() { |
| 89 | BgpRouteEntry.PathSegment pathSegment1 = generatePathSegment(); |
| 90 | |
| 91 | // Setup Path Segment 2 |
| 92 | byte pathSegmentType = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE; |
| 93 | ArrayList<Long> segmentAsNumbers = new ArrayList<>(); |
| 94 | segmentAsNumbers.add((long) 1); |
| 95 | segmentAsNumbers.add((long) 22); // Different |
| 96 | segmentAsNumbers.add((long) 3); |
| 97 | // |
| 98 | BgpRouteEntry.PathSegment pathSegment2 = |
| 99 | new BgpRouteEntry.PathSegment(pathSegmentType, segmentAsNumbers); |
| 100 | |
| 101 | assertThat(pathSegment1, is(not(pathSegment2))); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Tests object string representation. |
| 106 | */ |
| 107 | @Test |
| 108 | public void testToString() { |
| 109 | BgpRouteEntry.PathSegment pathSegment = generatePathSegment(); |
| 110 | |
| 111 | String expectedString = |
| 112 | "PathSegment{type=2, segmentAsNumbers=[1, 2, 3]}"; |
| 113 | assertThat(pathSegment.toString(), is(expectedString)); |
| 114 | } |
| 115 | } |