blob: 86051616b3c0fef33a9b3c7eaaa8f43f436ee0b7 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Jonathan Hartf4bd0482017-01-27 15:11:18 -08002 * Copyright 2017-present 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 Hartf4bd0482017-01-27 15:11:18 -080016
Jonathan Hart41349e92015-02-09 14:14:02 -080017package org.onosproject.routing.bgp;
18
19import org.hamcrest.Matchers;
20import org.junit.Test;
21
22import java.util.ArrayList;
Jonathan Hart20d8e512014-10-16 11:05:52 -070023
24import static org.hamcrest.Matchers.is;
Jonathan Hart20d8e512014-10-16 11:05:52 -070025import static org.junit.Assert.assertThat;
26
Jonathan Hart20d8e512014-10-16 11:05:52 -070027/**
28 * Unit tests for the BgpRouteEntry.PathSegment class.
29 */
30public 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 SHIMIZUc14b2a22015-05-05 18:15:13 -070039 segmentAsNumbers.add(1L);
40 segmentAsNumbers.add(2L);
41 segmentAsNumbers.add(3L);
Jonathan Hart20d8e512014-10-16 11:05:52 -070042 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 Radoslavove6015262014-11-07 13:08:53 -080056 "PathSegment{type=AS_SEQUENCE, segmentAsNumbers=[1, 2, 3]}";
Jonathan Hart20d8e512014-10-16 11:05:52 -070057 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 SHIMIZUc14b2a22015-05-05 18:15:13 -070078 segmentAsNumbers.add(1L);
79 segmentAsNumbers.add(2L);
80 segmentAsNumbers.add(3L);
Jonathan Hart20d8e512014-10-16 11:05:52 -070081
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 SHIMIZUc14b2a22015-05-05 18:15:13 -0700110 segmentAsNumbers.add(1L);
111 segmentAsNumbers.add(22L); // Different
112 segmentAsNumbers.add(3L);
Jonathan Hart20d8e512014-10-16 11:05:52 -0700113 //
114 BgpRouteEntry.PathSegment pathSegment2 =
115 new BgpRouteEntry.PathSegment(pathSegmentType, segmentAsNumbers);
116
Jonathan Hartf4bd0482017-01-27 15:11:18 -0800117 assertThat(pathSegment1, Matchers.is(Matchers.not(pathSegment2)));
Jonathan Hart20d8e512014-10-16 11:05:52 -0700118 }
119
120 /**
121 * Tests object string representation.
122 */
123 @Test
124 public void testToString() {
125 BgpRouteEntry.PathSegment pathSegment = generatePathSegment();
126
127 String expectedString =
Pavlin Radoslavove6015262014-11-07 13:08:53 -0800128 "PathSegment{type=AS_SEQUENCE, segmentAsNumbers=[1, 2, 3]}";
Jonathan Hart20d8e512014-10-16 11:05:52 -0700129 assertThat(pathSegment.toString(), is(expectedString));
130 }
131}