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