Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.intent; |
| 17 | |
| 18 | import org.junit.Test; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 19 | import org.onlab.onos.core.ApplicationId; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 20 | import org.onlab.onos.TestApplicationId; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 21 | import org.onlab.onos.net.HostId; |
| 22 | import org.onlab.onos.net.flow.TrafficSelector; |
| 23 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 24 | |
| 25 | import static org.hamcrest.MatcherAssert.assertThat; |
| 26 | import static org.hamcrest.Matchers.equalTo; |
| 27 | import static org.hamcrest.Matchers.is; |
| 28 | import static org.hamcrest.Matchers.not; |
Pavlin Radoslavov | d26f57a | 2014-10-23 17:19:45 -0700 | [diff] [blame] | 29 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 30 | import static org.onlab.onos.net.NetTestTools.hid; |
| 31 | |
| 32 | /** |
| 33 | * Unit tests for the HostToHostIntent class. |
| 34 | */ |
| 35 | public class TestHostToHostIntent { |
| 36 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 37 | private static final ApplicationId APPID = new TestApplicationId("foo"); |
| 38 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 39 | private TrafficSelector selector = new IntentTestsMocks.MockSelector(); |
| 40 | private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment(); |
| 41 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 42 | private HostToHostIntent makeHostToHost(HostId one, HostId two) { |
| 43 | return new HostToHostIntent(APPID, one, two, selector, treatment); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Tests the equals() method where two HostToHostIntents have references |
| 48 | * to the same hosts. These should compare equal. |
| 49 | */ |
| 50 | @Test |
| 51 | public void testSameEquals() { |
| 52 | |
| 53 | HostId one = hid("00:00:00:00:00:01/-1"); |
| 54 | HostId two = hid("00:00:00:00:00:02/-1"); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 55 | HostToHostIntent i1 = makeHostToHost(one, two); |
| 56 | HostToHostIntent i2 = makeHostToHost(one, two); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 57 | |
| 58 | assertThat(i1, is(equalTo(i2))); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Tests the equals() method where two HostToHostIntents have references |
| 63 | * to different Hosts. These should compare not equal. |
| 64 | */ |
| 65 | @Test |
Thomas Vachuska | a12fdf2 | 2014-10-21 01:33:48 -0700 | [diff] [blame] | 66 | public void testSameEquals2() { |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 67 | HostId one = hid("00:00:00:00:00:01/-1"); |
| 68 | HostId two = hid("00:00:00:00:00:02/-1"); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 69 | HostToHostIntent i1 = makeHostToHost(one, two); |
| 70 | HostToHostIntent i2 = makeHostToHost(two, one); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 71 | |
Thomas Vachuska | a12fdf2 | 2014-10-21 01:33:48 -0700 | [diff] [blame] | 72 | assertThat(i1, is(equalTo(i2))); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Tests that the hashCode() values for two equivalent HostToHostIntent |
| 77 | * objects are the same. |
| 78 | */ |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 79 | @Test |
| 80 | public void testHashCodeEquals() { |
| 81 | HostId one = hid("00:00:00:00:00:01/-1"); |
| 82 | HostId two = hid("00:00:00:00:00:02/-1"); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 83 | HostToHostIntent i1 = makeHostToHost(one, two); |
| 84 | HostToHostIntent i2 = makeHostToHost(one, two); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 85 | |
| 86 | assertThat(i1.hashCode(), is(equalTo(i2.hashCode()))); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Tests that the hashCode() values for two distinct LinkCollectionIntent |
| 91 | * objects are different. |
| 92 | */ |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 93 | @Test |
Thomas Vachuska | a12fdf2 | 2014-10-21 01:33:48 -0700 | [diff] [blame] | 94 | public void testHashCodeEquals2() { |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 95 | HostId one = hid("00:00:00:00:00:01/-1"); |
| 96 | HostId two = hid("00:00:00:00:00:02/-1"); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 97 | HostToHostIntent i1 = makeHostToHost(one, two); |
| 98 | HostToHostIntent i2 = makeHostToHost(two, one); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 99 | |
Thomas Vachuska | a12fdf2 | 2014-10-21 01:33:48 -0700 | [diff] [blame] | 100 | assertThat(i1.hashCode(), is(equalTo(i2.hashCode()))); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Tests that the hashCode() values for two distinct LinkCollectionIntent |
| 105 | * objects are different. |
| 106 | */ |
| 107 | @Test |
| 108 | public void testHashCodeDifferent() { |
| 109 | HostId one = hid("00:00:00:00:00:01/-1"); |
| 110 | HostId two = hid("00:00:00:00:00:02/-1"); |
| 111 | HostId three = hid("00:00:00:00:00:32/-1"); |
| 112 | HostToHostIntent i1 = makeHostToHost(one, two); |
| 113 | HostToHostIntent i2 = makeHostToHost(one, three); |
| 114 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 115 | assertThat(i1.hashCode(), is(not(equalTo(i2.hashCode())))); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Checks that the HostToHostIntent class is immutable. |
| 120 | */ |
| 121 | @Test |
| 122 | public void checkImmutability() { |
Pavlin Radoslavov | d26f57a | 2014-10-23 17:19:45 -0700 | [diff] [blame] | 123 | assertThatClassIsImmutable(HostToHostIntent.class); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 124 | } |
| 125 | } |