Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.intent; |
| 2 | |
| 3 | import org.junit.Test; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 4 | import org.onlab.onos.ApplicationId; |
| 5 | import org.onlab.onos.TestApplicationId; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 6 | import org.onlab.onos.net.HostId; |
| 7 | import org.onlab.onos.net.flow.TrafficSelector; |
| 8 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 9 | |
| 10 | import static org.hamcrest.MatcherAssert.assertThat; |
| 11 | import static org.hamcrest.Matchers.equalTo; |
| 12 | import static org.hamcrest.Matchers.is; |
| 13 | import static org.hamcrest.Matchers.not; |
| 14 | import static org.onlab.onos.net.NetTestTools.hid; |
| 15 | |
| 16 | /** |
| 17 | * Unit tests for the HostToHostIntent class. |
| 18 | */ |
| 19 | public class TestHostToHostIntent { |
| 20 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 21 | private static final ApplicationId APPID = new TestApplicationId("foo"); |
| 22 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 23 | private TrafficSelector selector = new IntentTestsMocks.MockSelector(); |
| 24 | private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment(); |
| 25 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 26 | private HostToHostIntent makeHostToHost(HostId one, HostId two) { |
| 27 | return new HostToHostIntent(APPID, one, two, selector, treatment); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Tests the equals() method where two HostToHostIntents have references |
| 32 | * to the same hosts. These should compare equal. |
| 33 | */ |
| 34 | @Test |
| 35 | public void testSameEquals() { |
| 36 | |
| 37 | HostId one = hid("00:00:00:00:00:01/-1"); |
| 38 | HostId two = hid("00:00:00:00:00:02/-1"); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 39 | HostToHostIntent i1 = makeHostToHost(one, two); |
| 40 | HostToHostIntent i2 = makeHostToHost(one, two); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 41 | |
| 42 | assertThat(i1, is(equalTo(i2))); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Tests the equals() method where two HostToHostIntents have references |
| 47 | * to different Hosts. These should compare not equal. |
| 48 | */ |
| 49 | @Test |
Thomas Vachuska | a12fdf2 | 2014-10-21 01:33:48 -0700 | [diff] [blame] | 50 | public void testSameEquals2() { |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 51 | HostId one = hid("00:00:00:00:00:01/-1"); |
| 52 | HostId two = hid("00:00:00:00:00:02/-1"); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 53 | HostToHostIntent i1 = makeHostToHost(one, two); |
| 54 | HostToHostIntent i2 = makeHostToHost(two, one); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 55 | |
Thomas Vachuska | a12fdf2 | 2014-10-21 01:33:48 -0700 | [diff] [blame] | 56 | assertThat(i1, is(equalTo(i2))); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Tests that the hashCode() values for two equivalent HostToHostIntent |
| 61 | * objects are the same. |
| 62 | */ |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 63 | @Test |
| 64 | public void testHashCodeEquals() { |
| 65 | HostId one = hid("00:00:00:00:00:01/-1"); |
| 66 | HostId two = hid("00:00:00:00:00:02/-1"); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 67 | HostToHostIntent i1 = makeHostToHost(one, two); |
| 68 | HostToHostIntent i2 = makeHostToHost(one, two); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 69 | |
| 70 | assertThat(i1.hashCode(), is(equalTo(i2.hashCode()))); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Tests that the hashCode() values for two distinct LinkCollectionIntent |
| 75 | * objects are different. |
| 76 | */ |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 77 | @Test |
Thomas Vachuska | a12fdf2 | 2014-10-21 01:33:48 -0700 | [diff] [blame] | 78 | public void testHashCodeEquals2() { |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 79 | HostId one = hid("00:00:00:00:00:01/-1"); |
| 80 | HostId two = hid("00:00:00:00:00:02/-1"); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 81 | HostToHostIntent i1 = makeHostToHost(one, two); |
| 82 | HostToHostIntent i2 = makeHostToHost(two, one); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 83 | |
Thomas Vachuska | a12fdf2 | 2014-10-21 01:33:48 -0700 | [diff] [blame] | 84 | assertThat(i1.hashCode(), is(equalTo(i2.hashCode()))); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Tests that the hashCode() values for two distinct LinkCollectionIntent |
| 89 | * objects are different. |
| 90 | */ |
| 91 | @Test |
| 92 | public void testHashCodeDifferent() { |
| 93 | HostId one = hid("00:00:00:00:00:01/-1"); |
| 94 | HostId two = hid("00:00:00:00:00:02/-1"); |
| 95 | HostId three = hid("00:00:00:00:00:32/-1"); |
| 96 | HostToHostIntent i1 = makeHostToHost(one, two); |
| 97 | HostToHostIntent i2 = makeHostToHost(one, three); |
| 98 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 99 | assertThat(i1.hashCode(), is(not(equalTo(i2.hashCode())))); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Checks that the HostToHostIntent class is immutable. |
| 104 | */ |
| 105 | @Test |
| 106 | public void checkImmutability() { |
| 107 | ImmutableClassChecker.assertThatClassIsImmutable(HostToHostIntent.class); |
| 108 | } |
| 109 | } |