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