Ray Milkey | e668408 | 2014-10-16 16:59:47 -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 org.junit.Before; |
| 4 | import org.junit.Test; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 5 | import org.onlab.onos.ApplicationId; |
| 6 | import org.onlab.onos.TestApplicationId; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 7 | import org.onlab.onos.net.ConnectPoint; |
| 8 | import org.onlab.onos.net.flow.TrafficSelector; |
| 9 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 10 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 11 | import java.util.HashSet; |
| 12 | import java.util.Set; |
| 13 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 14 | import static org.hamcrest.CoreMatchers.not; |
| 15 | import static org.hamcrest.MatcherAssert.assertThat; |
| 16 | import static org.hamcrest.Matchers.equalTo; |
| 17 | import static org.hamcrest.Matchers.is; |
Pavlin Radoslavov | d26f57a | 2014-10-23 17:19:45 -0700 | [diff] [blame^] | 18 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 19 | import static org.onlab.onos.net.NetTestTools.connectPoint; |
| 20 | |
| 21 | /** |
| 22 | * Unit tests for the MultiPointToSinglePointIntent class. |
| 23 | */ |
| 24 | public class TestMultiPointToSinglePointIntent { |
| 25 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 26 | private static final ApplicationId APPID = new TestApplicationId("foo"); |
| 27 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 28 | private ConnectPoint point1 = connectPoint("dev1", 1); |
| 29 | private ConnectPoint point2 = connectPoint("dev2", 1); |
| 30 | private ConnectPoint point3 = connectPoint("dev3", 1); |
| 31 | |
| 32 | private TrafficSelector selector = new IntentTestsMocks.MockSelector(); |
| 33 | private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment(); |
| 34 | |
| 35 | Set<ConnectPoint> ingress1; |
| 36 | Set<ConnectPoint> ingress2; |
| 37 | |
| 38 | /** |
| 39 | * Creates a MultiPointToSinglePointIntent object. |
| 40 | * |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 41 | * @param ingress set of ingress points |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 42 | * @param egress egress point |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 43 | * @return MultiPointToSinglePoint intent |
| 44 | */ |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 45 | private MultiPointToSinglePointIntent makeIntent(Set<ConnectPoint> ingress, |
| 46 | ConnectPoint egress) { |
| 47 | return new MultiPointToSinglePointIntent(APPID, selector, treatment, |
| 48 | ingress, egress); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Initializes the ingress sets. |
| 53 | */ |
| 54 | @Before |
| 55 | public void setup() { |
| 56 | ingress1 = new HashSet<>(); |
| 57 | ingress2 = new HashSet<>(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Tests the equals() method where two MultiPointToSinglePoint have references |
| 62 | * to the same Links in different orders. These should compare equal. |
| 63 | */ |
| 64 | @Test |
| 65 | public void testSameEquals() { |
| 66 | |
| 67 | Set<ConnectPoint> ingress1 = new HashSet<>(); |
| 68 | ingress1.add(point2); |
| 69 | ingress1.add(point3); |
| 70 | |
| 71 | Set<ConnectPoint> ingress2 = new HashSet<>(); |
| 72 | ingress2.add(point3); |
| 73 | ingress2.add(point2); |
| 74 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 75 | Intent i1 = makeIntent(ingress1, point1); |
| 76 | Intent i2 = makeIntent(ingress2, point1); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 77 | |
| 78 | assertThat(i1, is(equalTo(i2))); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Tests the equals() method where two MultiPointToSinglePoint have references |
| 83 | * to different Links. These should compare not equal. |
| 84 | */ |
| 85 | @Test |
| 86 | public void testLinksDifferentEquals() { |
| 87 | ingress1.add(point3); |
| 88 | |
| 89 | ingress2.add(point3); |
| 90 | ingress2.add(point2); |
| 91 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 92 | Intent i1 = makeIntent(ingress1, point1); |
| 93 | Intent i2 = makeIntent(ingress2, point1); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 94 | |
| 95 | assertThat(i1, is(not(equalTo(i2)))); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Tests that the hashCode() values for two equivalent MultiPointToSinglePoint |
| 100 | * objects are the same. |
| 101 | */ |
| 102 | @Test |
| 103 | public void testHashCodeEquals() { |
| 104 | ingress1.add(point2); |
| 105 | ingress1.add(point3); |
| 106 | |
| 107 | ingress2.add(point3); |
| 108 | ingress2.add(point2); |
| 109 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 110 | Intent i1 = makeIntent(ingress1, point1); |
| 111 | Intent i2 = makeIntent(ingress2, point1); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 112 | |
| 113 | assertThat(i1.hashCode(), is(equalTo(i2.hashCode()))); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Tests that the hashCode() values for two distinct MultiPointToSinglePoint |
| 118 | * objects are different. |
| 119 | */ |
| 120 | @Test |
| 121 | public void testHashCodeDifferent() { |
| 122 | ingress1.add(point2); |
| 123 | |
| 124 | ingress2.add(point3); |
| 125 | ingress2.add(point2); |
| 126 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 127 | Intent i1 = makeIntent(ingress1, point1); |
| 128 | Intent i2 = makeIntent(ingress2, point1); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 129 | |
| 130 | |
| 131 | assertThat(i1.hashCode(), is(not(equalTo(i2.hashCode())))); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Checks that the MultiPointToSinglePointIntent class is immutable. |
| 136 | */ |
| 137 | @Test |
| 138 | public void checkImmutability() { |
Pavlin Radoslavov | d26f57a | 2014-10-23 17:19:45 -0700 | [diff] [blame^] | 139 | assertThatClassIsImmutable(MultiPointToSinglePointIntent.class); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 140 | } |
| 141 | } |