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