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