blob: 27579de14a062d2ac7f8f0b7f3b4745d53b1ebf9 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Jonathan Hart20d8e512014-10-16 11:05:52 -070016package org.onlab.onos.sdnip.bgp;
17
18import static org.hamcrest.Matchers.is;
19import static org.hamcrest.Matchers.not;
20import static org.junit.Assert.assertThat;
21
22import java.util.ArrayList;
23
24import org.junit.Test;
25
26/**
27 * Unit tests for the BgpRouteEntry.PathSegment class.
28 */
29public 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 Radoslavove6015262014-11-07 13:08:53 -080055 "PathSegment{type=AS_SEQUENCE, segmentAsNumbers=[1, 2, 3]}";
Jonathan Hart20d8e512014-10-16 11:05:52 -070056 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 Radoslavove6015262014-11-07 13:08:53 -0800127 "PathSegment{type=AS_SEQUENCE, segmentAsNumbers=[1, 2, 3]}";
Jonathan Hart20d8e512014-10-16 11:05:52 -0700128 assertThat(pathSegment.toString(), is(expectedString));
129 }
130}