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