Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.intent; |
| 17 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 18 | import org.junit.Before; |
| 19 | import org.junit.Test; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 20 | import org.onlab.onos.core.ApplicationId; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 21 | import org.onlab.onos.TestApplicationId; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 22 | import org.onlab.onos.net.ConnectPoint; |
| 23 | import org.onlab.onos.net.flow.TrafficSelector; |
| 24 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 25 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 26 | import java.util.HashSet; |
| 27 | import java.util.Set; |
| 28 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 29 | import static org.hamcrest.CoreMatchers.not; |
| 30 | import static org.hamcrest.MatcherAssert.assertThat; |
| 31 | import static org.hamcrest.Matchers.equalTo; |
| 32 | import static org.hamcrest.Matchers.is; |
Pavlin Radoslavov | d26f57a | 2014-10-23 17:19:45 -0700 | [diff] [blame] | 33 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 34 | import static org.onlab.onos.net.NetTestTools.connectPoint; |
| 35 | |
| 36 | /** |
| 37 | * Unit tests for the MultiPointToSinglePointIntent class. |
| 38 | */ |
| 39 | public class TestMultiPointToSinglePointIntent { |
| 40 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 41 | private static final ApplicationId APPID = new TestApplicationId("foo"); |
| 42 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 43 | private ConnectPoint point1 = connectPoint("dev1", 1); |
| 44 | private ConnectPoint point2 = connectPoint("dev2", 1); |
| 45 | private ConnectPoint point3 = connectPoint("dev3", 1); |
| 46 | |
| 47 | private TrafficSelector selector = new IntentTestsMocks.MockSelector(); |
| 48 | private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment(); |
| 49 | |
| 50 | Set<ConnectPoint> ingress1; |
| 51 | Set<ConnectPoint> ingress2; |
| 52 | |
| 53 | /** |
| 54 | * Creates a MultiPointToSinglePointIntent object. |
| 55 | * |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 56 | * @param ingress set of ingress points |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 57 | * @param egress egress point |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 58 | * @return MultiPointToSinglePoint intent |
| 59 | */ |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 60 | private MultiPointToSinglePointIntent makeIntent(Set<ConnectPoint> ingress, |
| 61 | ConnectPoint egress) { |
| 62 | return new MultiPointToSinglePointIntent(APPID, selector, treatment, |
| 63 | ingress, egress); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Initializes the ingress sets. |
| 68 | */ |
| 69 | @Before |
| 70 | public void setup() { |
| 71 | ingress1 = new HashSet<>(); |
| 72 | ingress2 = new HashSet<>(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Tests the equals() method where two MultiPointToSinglePoint have references |
| 77 | * to the same Links in different orders. These should compare equal. |
| 78 | */ |
| 79 | @Test |
| 80 | public void testSameEquals() { |
| 81 | |
| 82 | Set<ConnectPoint> ingress1 = new HashSet<>(); |
| 83 | ingress1.add(point2); |
| 84 | ingress1.add(point3); |
| 85 | |
| 86 | Set<ConnectPoint> ingress2 = new HashSet<>(); |
| 87 | ingress2.add(point3); |
| 88 | ingress2.add(point2); |
| 89 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 90 | Intent i1 = makeIntent(ingress1, point1); |
| 91 | Intent i2 = makeIntent(ingress2, point1); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 92 | |
| 93 | assertThat(i1, is(equalTo(i2))); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Tests the equals() method where two MultiPointToSinglePoint have references |
| 98 | * to different Links. These should compare not equal. |
| 99 | */ |
| 100 | @Test |
| 101 | public void testLinksDifferentEquals() { |
| 102 | ingress1.add(point3); |
| 103 | |
| 104 | ingress2.add(point3); |
| 105 | ingress2.add(point2); |
| 106 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 107 | Intent i1 = makeIntent(ingress1, point1); |
| 108 | Intent i2 = makeIntent(ingress2, point1); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 109 | |
| 110 | assertThat(i1, is(not(equalTo(i2)))); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Tests that the hashCode() values for two equivalent MultiPointToSinglePoint |
| 115 | * objects are the same. |
| 116 | */ |
| 117 | @Test |
| 118 | public void testHashCodeEquals() { |
| 119 | ingress1.add(point2); |
| 120 | ingress1.add(point3); |
| 121 | |
| 122 | ingress2.add(point3); |
| 123 | ingress2.add(point2); |
| 124 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 125 | Intent i1 = makeIntent(ingress1, point1); |
| 126 | Intent i2 = makeIntent(ingress2, point1); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 127 | |
| 128 | assertThat(i1.hashCode(), is(equalTo(i2.hashCode()))); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Tests that the hashCode() values for two distinct MultiPointToSinglePoint |
| 133 | * objects are different. |
| 134 | */ |
| 135 | @Test |
| 136 | public void testHashCodeDifferent() { |
| 137 | ingress1.add(point2); |
| 138 | |
| 139 | ingress2.add(point3); |
| 140 | ingress2.add(point2); |
| 141 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 142 | Intent i1 = makeIntent(ingress1, point1); |
| 143 | Intent i2 = makeIntent(ingress2, point1); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 144 | |
| 145 | |
| 146 | assertThat(i1.hashCode(), is(not(equalTo(i2.hashCode())))); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Checks that the MultiPointToSinglePointIntent class is immutable. |
| 151 | */ |
| 152 | @Test |
| 153 | public void checkImmutability() { |
Pavlin Radoslavov | d26f57a | 2014-10-23 17:19:45 -0700 | [diff] [blame] | 154 | assertThatClassIsImmutable(MultiPointToSinglePointIntent.class); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 155 | } |
| 156 | } |