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