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