blob: 20b2e23800782ce1cd201363e4f492c97567ed2e [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Jonathan Hart20d8e512014-10-16 11:05:52 -070019package org.onlab.onos.sdnip.bgp;
20
21import static org.hamcrest.Matchers.is;
22import static org.hamcrest.Matchers.not;
23import static org.junit.Assert.assertThat;
24
25import java.util.ArrayList;
26
27import org.junit.Test;
28
29/**
30 * Unit tests for the BgpRouteEntry.AsPath class.
31 */
32public class AsPathTest {
33 /**
34 * Generates an AS Path.
35 *
36 * @return a generated AS Path
37 */
38 private BgpRouteEntry.AsPath generateAsPath() {
39 ArrayList<BgpRouteEntry.PathSegment> pathSegments = new ArrayList<>();
40 byte pathSegmentType1 = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE;
41 ArrayList<Long> segmentAsNumbers1 = new ArrayList<>();
42 segmentAsNumbers1.add((long) 1);
43 segmentAsNumbers1.add((long) 2);
44 segmentAsNumbers1.add((long) 3);
45 BgpRouteEntry.PathSegment pathSegment1 =
46 new BgpRouteEntry.PathSegment(pathSegmentType1, segmentAsNumbers1);
47 pathSegments.add(pathSegment1);
48 //
49 byte pathSegmentType2 = (byte) BgpConstants.Update.AsPath.AS_SET;
50 ArrayList<Long> segmentAsNumbers2 = new ArrayList<>();
51 segmentAsNumbers2.add((long) 4);
52 segmentAsNumbers2.add((long) 5);
53 segmentAsNumbers2.add((long) 6);
54 BgpRouteEntry.PathSegment pathSegment2 =
55 new BgpRouteEntry.PathSegment(pathSegmentType2, segmentAsNumbers2);
56 pathSegments.add(pathSegment2);
57 //
58 BgpRouteEntry.AsPath asPath = new BgpRouteEntry.AsPath(pathSegments);
59
60 return asPath;
61 }
62
63 /**
64 * Tests valid class constructor.
65 */
66 @Test
67 public void testConstructor() {
68 BgpRouteEntry.AsPath asPath = generateAsPath();
69
70 String expectedString =
71 "AsPath{pathSegments=" +
72 "[PathSegment{type=2, segmentAsNumbers=[1, 2, 3]}, " +
73 "PathSegment{type=1, segmentAsNumbers=[4, 5, 6]}]}";
74 assertThat(asPath.toString(), is(expectedString));
75 }
76
77 /**
78 * Tests invalid class constructor for null Path Segments.
79 */
80 @Test(expected = NullPointerException.class)
81 public void testInvalidConstructorNullPathSegments() {
82 ArrayList<BgpRouteEntry.PathSegment> pathSegments = null;
83 new BgpRouteEntry.AsPath(pathSegments);
84 }
85
86 /**
87 * Tests getting the fields of an AS Path.
88 */
89 @Test
90 public void testGetFields() {
91 // Create the fields to compare against
92 ArrayList<BgpRouteEntry.PathSegment> pathSegments = new ArrayList<>();
93 byte pathSegmentType1 = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE;
94 ArrayList<Long> segmentAsNumbers1 = new ArrayList<>();
95 segmentAsNumbers1.add((long) 1);
96 segmentAsNumbers1.add((long) 2);
97 segmentAsNumbers1.add((long) 3);
98 BgpRouteEntry.PathSegment pathSegment1 =
99 new BgpRouteEntry.PathSegment(pathSegmentType1, segmentAsNumbers1);
100 pathSegments.add(pathSegment1);
101 //
102 byte pathSegmentType2 = (byte) BgpConstants.Update.AsPath.AS_SET;
103 ArrayList<Long> segmentAsNumbers2 = new ArrayList<>();
104 segmentAsNumbers2.add((long) 4);
105 segmentAsNumbers2.add((long) 5);
106 segmentAsNumbers2.add((long) 6);
107 BgpRouteEntry.PathSegment pathSegment2 =
108 new BgpRouteEntry.PathSegment(pathSegmentType2, segmentAsNumbers2);
109 pathSegments.add(pathSegment2);
110
111 // Generate the entry to test
112 BgpRouteEntry.AsPath asPath = generateAsPath();
113
114 assertThat(asPath.getPathSegments(), is(pathSegments));
115 }
116
117 /**
118 * Tests getting the AS Path Length.
119 */
120 @Test
121 public void testGetAsPathLength() {
122 BgpRouteEntry.AsPath asPath = generateAsPath();
123 assertThat(asPath.getAsPathLength(), is(4));
124
125 // Create an empty AS Path
126 ArrayList<BgpRouteEntry.PathSegment> pathSegments = new ArrayList<>();
127 asPath = new BgpRouteEntry.AsPath(pathSegments);
128 assertThat(asPath.getAsPathLength(), is(0));
129 }
130
131 /**
132 * Tests equality of {@link BgpRouteEntry.AsPath}.
133 */
134 @Test
135 public void testEquality() {
136 BgpRouteEntry.AsPath asPath1 = generateAsPath();
137 BgpRouteEntry.AsPath asPath2 = generateAsPath();
138
139 assertThat(asPath1, is(asPath2));
140 }
141
142 /**
143 * Tests non-equality of {@link BgpRouteEntry.AsPath}.
144 */
145 @Test
146 public void testNonEquality() {
147 BgpRouteEntry.AsPath asPath1 = generateAsPath();
148
149 // Setup AS Path 2
150 ArrayList<BgpRouteEntry.PathSegment> pathSegments = new ArrayList<>();
151 byte pathSegmentType1 = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE;
152 ArrayList<Long> segmentAsNumbers1 = new ArrayList<>();
153 segmentAsNumbers1.add((long) 1);
154 segmentAsNumbers1.add((long) 2);
155 segmentAsNumbers1.add((long) 3);
156 BgpRouteEntry.PathSegment pathSegment1 =
157 new BgpRouteEntry.PathSegment(pathSegmentType1, segmentAsNumbers1);
158 pathSegments.add(pathSegment1);
159 //
160 byte pathSegmentType2 = (byte) BgpConstants.Update.AsPath.AS_SET;
161 ArrayList<Long> segmentAsNumbers2 = new ArrayList<>();
162 segmentAsNumbers2.add((long) 4);
163 segmentAsNumbers2.add((long) 55); // Different
164 segmentAsNumbers2.add((long) 6);
165 BgpRouteEntry.PathSegment pathSegment2 =
166 new BgpRouteEntry.PathSegment(pathSegmentType2, segmentAsNumbers2);
167 pathSegments.add(pathSegment2);
168 //
169 BgpRouteEntry.AsPath asPath2 = new BgpRouteEntry.AsPath(pathSegments);
170
171 assertThat(asPath1, is(not(asPath2)));
172 }
173
174 /**
175 * Tests object string representation.
176 */
177 @Test
178 public void testToString() {
179 BgpRouteEntry.AsPath asPath = generateAsPath();
180
181 String expectedString =
182 "AsPath{pathSegments=" +
183 "[PathSegment{type=2, segmentAsNumbers=[1, 2, 3]}, " +
184 "PathSegment{type=1, segmentAsNumbers=[4, 5, 6]}]}";
185 assertThat(asPath.toString(), is(expectedString));
186 }
187}