Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.intent; |
| 2 | |
| 3 | import java.util.HashSet; |
| 4 | import java.util.Set; |
| 5 | |
| 6 | import org.junit.Before; |
| 7 | import org.junit.Test; |
| 8 | import org.onlab.onos.net.ConnectPoint; |
| 9 | import org.onlab.onos.net.flow.TrafficSelector; |
| 10 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 11 | |
| 12 | import static org.hamcrest.CoreMatchers.not; |
| 13 | import static org.hamcrest.MatcherAssert.assertThat; |
| 14 | import static org.hamcrest.Matchers.equalTo; |
| 15 | import static org.hamcrest.Matchers.is; |
| 16 | import static org.onlab.onos.net.NetTestTools.connectPoint; |
| 17 | |
| 18 | /** |
| 19 | * Unit tests for the MultiPointToSinglePointIntent class. |
| 20 | */ |
| 21 | public class TestMultiPointToSinglePointIntent { |
| 22 | |
| 23 | private ConnectPoint point1 = connectPoint("dev1", 1); |
| 24 | private ConnectPoint point2 = connectPoint("dev2", 1); |
| 25 | private ConnectPoint point3 = connectPoint("dev3", 1); |
| 26 | |
| 27 | private TrafficSelector selector = new IntentTestsMocks.MockSelector(); |
| 28 | private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment(); |
| 29 | |
| 30 | Set<ConnectPoint> ingress1; |
| 31 | Set<ConnectPoint> ingress2; |
| 32 | |
| 33 | /** |
| 34 | * Creates a MultiPointToSinglePointIntent object. |
| 35 | * |
| 36 | * @param id identifier to use for the new intent |
| 37 | * @param ingress set of ingress points |
| 38 | * @param egress egress point |
| 39 | * @return MultiPointToSinglePoint intent |
| 40 | */ |
| 41 | private MultiPointToSinglePointIntent makeIntent(long id, |
| 42 | Set<ConnectPoint> ingress, |
| 43 | ConnectPoint egress) { |
| 44 | return new MultiPointToSinglePointIntent(new IntentId(id), |
| 45 | selector, |
| 46 | treatment, |
| 47 | ingress, |
| 48 | egress); |
| 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 | |
| 75 | Intent i1 = makeIntent(12, ingress1, point1); |
| 76 | Intent i2 = makeIntent(12, ingress2, point1); |
| 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 | |
| 92 | Intent i1 = makeIntent(12, ingress1, point1); |
| 93 | Intent i2 = makeIntent(12, ingress2, point1); |
| 94 | |
| 95 | assertThat(i1, is(not(equalTo(i2)))); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Tests the equals() method where two MultiPointToSinglePoint have different |
| 100 | * ids. These should compare not equal. |
| 101 | */ |
| 102 | @Test |
| 103 | public void testBaseDifferentEquals() { |
| 104 | ingress1.add(point3); |
| 105 | ingress2.add(point3); |
| 106 | |
| 107 | Intent i1 = makeIntent(12, ingress1, point1); |
| 108 | Intent i2 = makeIntent(11, ingress2, point1); |
| 109 | |
| 110 | assertThat(i1, is(not(equalTo(i2)))); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Tests that the hashCode() values for two equivalent MultiPointToSinglePoint |
| 115 | * objects are the same. |
| 116 | */ |
| 117 | @Test |
| 118 | public void testHashCodeEquals() { |
| 119 | ingress1.add(point2); |
| 120 | ingress1.add(point3); |
| 121 | |
| 122 | ingress2.add(point3); |
| 123 | ingress2.add(point2); |
| 124 | |
| 125 | Intent i1 = makeIntent(12, ingress1, point1); |
| 126 | Intent i2 = makeIntent(12, ingress2, point1); |
| 127 | |
| 128 | assertThat(i1.hashCode(), is(equalTo(i2.hashCode()))); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Tests that the hashCode() values for two distinct MultiPointToSinglePoint |
| 133 | * objects are different. |
| 134 | */ |
| 135 | @Test |
| 136 | public void testHashCodeDifferent() { |
| 137 | ingress1.add(point2); |
| 138 | |
| 139 | ingress2.add(point3); |
| 140 | ingress2.add(point2); |
| 141 | |
| 142 | Intent i1 = makeIntent(12, ingress1, point1); |
| 143 | Intent i2 = makeIntent(12, ingress2, point1); |
| 144 | |
| 145 | |
| 146 | assertThat(i1.hashCode(), is(not(equalTo(i2.hashCode())))); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Checks that the MultiPointToSinglePointIntent class is immutable. |
| 151 | */ |
| 152 | @Test |
| 153 | public void checkImmutability() { |
| 154 | ImmutableClassChecker. |
| 155 | assertThatClassIsImmutable(MultiPointToSinglePointIntent.class); |
| 156 | } |
| 157 | } |