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