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