Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 16 | package org.onlab.onos.sdnip.bgp; |
| 17 | |
| 18 | import static org.hamcrest.Matchers.is; |
| 19 | import static org.hamcrest.Matchers.not; |
| 20 | import static org.junit.Assert.assertThat; |
| 21 | |
| 22 | import java.util.ArrayList; |
| 23 | |
| 24 | import org.junit.Test; |
| 25 | |
| 26 | /** |
| 27 | * Unit tests for the BgpRouteEntry.PathSegment class. |
| 28 | */ |
| 29 | public class PathSegmentTest { |
| 30 | /** |
| 31 | * Generates a Path Segment. |
| 32 | * |
| 33 | * @return a generated PathSegment |
| 34 | */ |
| 35 | private BgpRouteEntry.PathSegment generatePathSegment() { |
| 36 | byte pathSegmentType = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE; |
| 37 | ArrayList<Long> segmentAsNumbers = new ArrayList<>(); |
| 38 | segmentAsNumbers.add((long) 1); |
| 39 | segmentAsNumbers.add((long) 2); |
| 40 | segmentAsNumbers.add((long) 3); |
| 41 | BgpRouteEntry.PathSegment pathSegment = |
| 42 | new BgpRouteEntry.PathSegment(pathSegmentType, segmentAsNumbers); |
| 43 | |
| 44 | return pathSegment; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Tests valid class constructor. |
| 49 | */ |
| 50 | @Test |
| 51 | public void testConstructor() { |
| 52 | BgpRouteEntry.PathSegment pathSegment = generatePathSegment(); |
| 53 | |
| 54 | String expectedString = |
Pavlin Radoslavov | e601526 | 2014-11-07 13:08:53 -0800 | [diff] [blame] | 55 | "PathSegment{type=AS_SEQUENCE, segmentAsNumbers=[1, 2, 3]}"; |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 56 | assertThat(pathSegment.toString(), is(expectedString)); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Tests invalid class constructor for null Segment AS Numbers. |
| 61 | */ |
| 62 | @Test(expected = NullPointerException.class) |
| 63 | public void testInvalidConstructorNullSegmentAsNumbers() { |
| 64 | byte pathSegmentType = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE; |
| 65 | ArrayList<Long> segmentAsNumbers = null; |
| 66 | new BgpRouteEntry.PathSegment(pathSegmentType, segmentAsNumbers); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Tests getting the fields of a Path Segment. |
| 71 | */ |
| 72 | @Test |
| 73 | public void testGetFields() { |
| 74 | // Create the fields to compare against |
| 75 | byte pathSegmentType = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE; |
| 76 | ArrayList<Long> segmentAsNumbers = new ArrayList<>(); |
| 77 | segmentAsNumbers.add((long) 1); |
| 78 | segmentAsNumbers.add((long) 2); |
| 79 | segmentAsNumbers.add((long) 3); |
| 80 | |
| 81 | // Generate the entry to test |
| 82 | BgpRouteEntry.PathSegment pathSegment = generatePathSegment(); |
| 83 | |
| 84 | assertThat(pathSegment.getType(), is(pathSegmentType)); |
| 85 | assertThat(pathSegment.getSegmentAsNumbers(), is(segmentAsNumbers)); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Tests equality of {@link BgpRouteEntry.PathSegment}. |
| 90 | */ |
| 91 | @Test |
| 92 | public void testEquality() { |
| 93 | BgpRouteEntry.PathSegment pathSegment1 = generatePathSegment(); |
| 94 | BgpRouteEntry.PathSegment pathSegment2 = generatePathSegment(); |
| 95 | |
| 96 | assertThat(pathSegment1, is(pathSegment2)); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Tests non-equality of {@link BgpRouteEntry.PathSegment}. |
| 101 | */ |
| 102 | @Test |
| 103 | public void testNonEquality() { |
| 104 | BgpRouteEntry.PathSegment pathSegment1 = generatePathSegment(); |
| 105 | |
| 106 | // Setup Path Segment 2 |
| 107 | byte pathSegmentType = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE; |
| 108 | ArrayList<Long> segmentAsNumbers = new ArrayList<>(); |
| 109 | segmentAsNumbers.add((long) 1); |
| 110 | segmentAsNumbers.add((long) 22); // Different |
| 111 | segmentAsNumbers.add((long) 3); |
| 112 | // |
| 113 | BgpRouteEntry.PathSegment pathSegment2 = |
| 114 | new BgpRouteEntry.PathSegment(pathSegmentType, segmentAsNumbers); |
| 115 | |
| 116 | assertThat(pathSegment1, is(not(pathSegment2))); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Tests object string representation. |
| 121 | */ |
| 122 | @Test |
| 123 | public void testToString() { |
| 124 | BgpRouteEntry.PathSegment pathSegment = generatePathSegment(); |
| 125 | |
| 126 | String expectedString = |
Pavlin Radoslavov | e601526 | 2014-11-07 13:08:53 -0800 | [diff] [blame] | 127 | "PathSegment{type=AS_SEQUENCE, segmentAsNumbers=[1, 2, 3]}"; |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 128 | assertThat(pathSegment.toString(), is(expectedString)); |
| 129 | } |
| 130 | } |