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 | 0742ec9 | 2014-10-13 08:39:55 -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 static org.hamcrest.CoreMatchers.not; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 19 | import static org.hamcrest.MatcherAssert.assertThat; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 20 | import static org.hamcrest.Matchers.equalTo; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 21 | import static org.hamcrest.Matchers.is; |
Pavlin Radoslavov | d26f57a | 2014-10-23 17:19:45 -0700 | [diff] [blame] | 22 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 23 | import static org.onlab.onos.net.NetTestTools.link; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 24 | |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 25 | import java.util.HashSet; |
| 26 | import java.util.Set; |
| 27 | |
| 28 | import org.junit.Before; |
| 29 | import org.junit.Test; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 30 | import org.onlab.onos.core.ApplicationId; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 31 | import org.onlab.onos.TestApplicationId; |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.ConnectPoint; |
| 33 | import org.onlab.onos.net.DeviceId; |
| 34 | import org.onlab.onos.net.Link; |
| 35 | import org.onlab.onos.net.PortNumber; |
| 36 | import org.onlab.onos.net.flow.TrafficSelector; |
| 37 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 38 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Unit tests for the LinkCollectionIntent class. |
| 41 | */ |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 42 | public class TestLinkCollectionIntent { |
| 43 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 44 | private static final ApplicationId APPID = new TestApplicationId("foo"); |
| 45 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 46 | private Link link1 = link("dev1", 1, "dev2", 2); |
| 47 | private Link link2 = link("dev1", 1, "dev3", 2); |
| 48 | private Link link3 = link("dev2", 1, "dev3", 2); |
| 49 | |
| 50 | private Set<Link> links1; |
| 51 | private Set<Link> links2; |
| 52 | |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 53 | private ConnectPoint egress1 = new ConnectPoint(DeviceId.deviceId("dev1"), |
| 54 | PortNumber.portNumber(3)); |
| 55 | private ConnectPoint egress2 = new ConnectPoint(DeviceId.deviceId("dev2"), |
| 56 | PortNumber.portNumber(3)); |
| 57 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 58 | private TrafficSelector selector = new IntentTestsMocks.MockSelector(); |
| 59 | private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment(); |
| 60 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 61 | private LinkCollectionIntent makeLinkCollection(Set<Link> links, |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 62 | ConnectPoint egress) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 63 | return new LinkCollectionIntent(APPID, selector, treatment, links, egress); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 66 | @Before |
| 67 | public void setup() { |
| 68 | links1 = new HashSet<>(); |
| 69 | links2 = new HashSet<>(); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 72 | /** |
| 73 | * Tests the equals() method where two LinkCollectionIntents have references |
| 74 | * to the same Links in different orders. These should compare equal. |
| 75 | */ |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 76 | @Test |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 77 | public void testSameEquals() { |
| 78 | links1.add(link1); |
| 79 | links1.add(link2); |
| 80 | links1.add(link3); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 81 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 82 | links2.add(link3); |
| 83 | links2.add(link2); |
| 84 | links2.add(link1); |
| 85 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 86 | LinkCollectionIntent i1 = makeLinkCollection(links1, egress1); |
| 87 | LinkCollectionIntent i2 = makeLinkCollection(links2, egress1); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 88 | |
| 89 | assertThat(i1, is(equalTo(i2))); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 92 | /** |
| 93 | * Tests the equals() method where two LinkCollectionIntents have references |
| 94 | * to different Links. These should compare not equal. |
| 95 | */ |
| 96 | @Test |
| 97 | public void testLinksDifferentEquals() { |
| 98 | links1.add(link1); |
| 99 | links1.add(link2); |
| 100 | |
| 101 | links2.add(link3); |
| 102 | links2.add(link1); |
| 103 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 104 | LinkCollectionIntent i1 = makeLinkCollection(links1, egress1); |
| 105 | LinkCollectionIntent i2 = makeLinkCollection(links2, egress1); |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 106 | |
| 107 | assertThat(i1, is(not(equalTo(i2)))); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Tests the equals() method where two LinkCollectionIntents have references |
| 112 | * to the same Links but different egress points. These should compare not equal. |
| 113 | */ |
| 114 | @Test |
| 115 | public void testEgressDifferentEquals() { |
| 116 | links1.add(link1); |
| 117 | links1.add(link2); |
| 118 | links1.add(link3); |
| 119 | |
| 120 | links2.add(link3); |
| 121 | links2.add(link2); |
| 122 | links2.add(link1); |
| 123 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 124 | LinkCollectionIntent i1 = makeLinkCollection(links1, egress1); |
| 125 | LinkCollectionIntent i2 = makeLinkCollection(links2, egress2); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 126 | |
| 127 | assertThat(i1, is(not(equalTo(i2)))); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Tests that the hashCode() values for two equivalent LinkCollectionIntent |
| 132 | * objects are the same. |
| 133 | */ |
| 134 | @Test |
| 135 | public void testHashCodeEquals() { |
| 136 | links1.add(link1); |
| 137 | links1.add(link2); |
| 138 | links1.add(link3); |
| 139 | |
| 140 | links2.add(link3); |
| 141 | links2.add(link2); |
| 142 | links2.add(link1); |
| 143 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 144 | LinkCollectionIntent i1 = makeLinkCollection(links1, egress1); |
| 145 | LinkCollectionIntent i2 = makeLinkCollection(links2, egress1); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 146 | |
| 147 | assertThat(i1.hashCode(), is(equalTo(i2.hashCode()))); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Tests that the hashCode() values for two distinct LinkCollectionIntent |
| 152 | * objects are different. |
| 153 | */ |
| 154 | @Test |
| 155 | public void testHashCodeDifferent() { |
| 156 | links1.add(link1); |
| 157 | links1.add(link2); |
| 158 | |
| 159 | links2.add(link1); |
| 160 | links2.add(link3); |
| 161 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 162 | LinkCollectionIntent i1 = makeLinkCollection(links1, egress1); |
| 163 | LinkCollectionIntent i2 = makeLinkCollection(links2, egress2); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 164 | |
| 165 | assertThat(i1.hashCode(), is(not(equalTo(i2.hashCode())))); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Checks that the HostToHostIntent class is immutable. |
| 170 | */ |
| 171 | @Test |
| 172 | public void checkImmutability() { |
Pavlin Radoslavov | d26f57a | 2014-10-23 17:19:45 -0700 | [diff] [blame] | 173 | assertThatClassIsImmutable(LinkCollectionIntent.class); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 174 | } |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 175 | } |