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