Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent; |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 17 | |
| 18 | import org.junit.Test; |
HIGUCHI Yuta | 501c465 | 2015-10-29 14:21:48 -0700 | [diff] [blame] | 19 | import org.onlab.util.DataRateUnit; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.TestApplicationId; |
| 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.net.HostId; |
| 23 | import org.onosproject.net.flow.TrafficSelector; |
HIGUCHI Yuta | 584107a | 2015-10-27 18:12:44 -0700 | [diff] [blame] | 24 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
HIGUCHI Yuta | 584107a | 2015-10-27 18:12:44 -0700 | [diff] [blame] | 25 | import com.google.common.collect.ImmutableList; |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 26 | import com.google.common.testing.EqualsTester; |
| 27 | |
Ray Milkey | 37f6a38 | 2014-11-25 14:54:42 -0800 | [diff] [blame] | 28 | import static org.hamcrest.MatcherAssert.assertThat; |
| 29 | import static org.hamcrest.Matchers.equalTo; |
HIGUCHI Yuta | 584107a | 2015-10-27 18:12:44 -0700 | [diff] [blame] | 30 | import static org.hamcrest.Matchers.hasItem; |
Ray Milkey | 37f6a38 | 2014-11-25 14:54:42 -0800 | [diff] [blame] | 31 | import static org.hamcrest.Matchers.is; |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 32 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import static org.onosproject.net.NetTestTools.hid; |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Unit tests for the HostToHostIntent class. |
| 37 | */ |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 38 | public class HostToHostIntentTest extends IntentTest { |
| 39 | private final TrafficSelector selector = new IntentTestsMocks.MockSelector(); |
| 40 | private final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment(); |
| 41 | private final HostId id1 = hid("12:34:56:78:91:ab/1"); |
| 42 | private final HostId id2 = hid("12:34:56:78:92:ab/1"); |
| 43 | private final HostId id3 = hid("12:34:56:78:93:ab/1"); |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 44 | |
Ray Milkey | 37f6a38 | 2014-11-25 14:54:42 -0800 | [diff] [blame] | 45 | private static final ApplicationId APPID = new TestApplicationId("foo"); |
| 46 | |
| 47 | private HostToHostIntent makeHostToHost(HostId one, HostId two) { |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 48 | return HostToHostIntent.builder() |
| 49 | .appId(APPID) |
| 50 | .one(one) |
| 51 | .two(two) |
| 52 | .selector(selector) |
| 53 | .treatment(treatment) |
| 54 | .build(); |
Ray Milkey | 37f6a38 | 2014-11-25 14:54:42 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Tests the equals() method where two HostToHostIntents have references |
| 59 | * to the same hosts. These should compare equal. |
| 60 | */ |
| 61 | @Test |
| 62 | public void testSameEquals() { |
| 63 | |
| 64 | HostId one = hid("00:00:00:00:00:01/-1"); |
| 65 | HostId two = hid("00:00:00:00:00:02/-1"); |
| 66 | HostToHostIntent i1 = makeHostToHost(one, two); |
| 67 | HostToHostIntent i2 = makeHostToHost(one, two); |
| 68 | |
| 69 | assertThat(i1.one(), is(equalTo(i2.one()))); |
| 70 | assertThat(i1.two(), is(equalTo(i2.two()))); |
| 71 | } |
| 72 | |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 73 | /** |
| 74 | * Checks that the HostToHostIntent class is immutable. |
| 75 | */ |
| 76 | @Test |
| 77 | public void testImmutability() { |
| 78 | assertThatClassIsImmutable(HostToHostIntent.class); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Tests equals(), hashCode() and toString() methods. |
| 83 | */ |
Ray Milkey | 37f6a38 | 2014-11-25 14:54:42 -0800 | [diff] [blame] | 84 | @Test |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 85 | public void testEquals() { |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 86 | final HostToHostIntent intent1 = HostToHostIntent.builder() |
| 87 | .appId(APPID) |
| 88 | .one(id1) |
| 89 | .two(id2) |
| 90 | .selector(selector) |
| 91 | .treatment(treatment) |
| 92 | .build(); |
Ray Milkey | 37f6a38 | 2014-11-25 14:54:42 -0800 | [diff] [blame] | 93 | |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 94 | final HostToHostIntent intent2 = HostToHostIntent.builder() |
| 95 | .appId(APPID) |
| 96 | .one(id2) |
| 97 | .two(id3) |
| 98 | .selector(selector) |
| 99 | .treatment(treatment) |
| 100 | .build(); |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 101 | |
| 102 | new EqualsTester() |
Ray Milkey | 37f6a38 | 2014-11-25 14:54:42 -0800 | [diff] [blame] | 103 | .addEqualityGroup(intent1) |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 104 | .addEqualityGroup(intent2) |
| 105 | .testEquals(); |
| 106 | } |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 107 | |
HIGUCHI Yuta | 584107a | 2015-10-27 18:12:44 -0700 | [diff] [blame] | 108 | @Test |
| 109 | public void testImplicitConstraintsAreAdded() { |
HIGUCHI Yuta | 501c465 | 2015-10-29 14:21:48 -0700 | [diff] [blame] | 110 | final Constraint other = BandwidthConstraint.of(1, DataRateUnit.GBPS); |
HIGUCHI Yuta | 584107a | 2015-10-27 18:12:44 -0700 | [diff] [blame] | 111 | final HostToHostIntent intent = HostToHostIntent.builder() |
| 112 | .appId(APPID) |
| 113 | .one(id1) |
| 114 | .two(id2) |
| 115 | .selector(selector) |
| 116 | .treatment(treatment) |
| 117 | .constraints(ImmutableList.of(other)) |
| 118 | .build(); |
| 119 | |
| 120 | assertThat(intent.constraints(), hasItem(HostToHostIntent.NOT_OPTICAL)); |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void testImplicitConstraints() { |
| 125 | final HostToHostIntent implicit = HostToHostIntent.builder() |
| 126 | .appId(APPID) |
| 127 | .one(id1) |
| 128 | .two(id2) |
| 129 | .selector(selector) |
| 130 | .treatment(treatment) |
| 131 | .build(); |
| 132 | final HostToHostIntent empty = HostToHostIntent.builder() |
| 133 | .appId(APPID) |
| 134 | .one(id1) |
| 135 | .two(id2) |
| 136 | .selector(selector) |
| 137 | .treatment(treatment) |
| 138 | .constraints(ImmutableList.of()) |
| 139 | .build(); |
| 140 | final HostToHostIntent exact = HostToHostIntent.builder() |
| 141 | .appId(APPID) |
| 142 | .one(id1) |
| 143 | .two(id2) |
| 144 | .selector(selector) |
| 145 | .treatment(treatment) |
| 146 | .constraints(ImmutableList.of(HostToHostIntent.NOT_OPTICAL)) |
| 147 | .build(); |
| 148 | |
| 149 | new EqualsTester() |
| 150 | .addEqualityGroup(implicit.constraints(), |
| 151 | empty.constraints(), |
| 152 | exact.constraints()) |
| 153 | .testEquals(); |
| 154 | |
| 155 | } |
| 156 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 157 | @Override |
| 158 | protected Intent createOne() { |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 159 | return HostToHostIntent.builder() |
| 160 | .appId(APPID) |
| 161 | .one(id1) |
| 162 | .two(id2) |
| 163 | .selector(selector) |
| 164 | .treatment(treatment) |
| 165 | .build(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Override |
| 169 | protected Intent createAnother() { |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 170 | return HostToHostIntent.builder() |
| 171 | .appId(APPID) |
| 172 | .one(id1) |
| 173 | .two(id3) |
| 174 | .selector(selector) |
| 175 | .treatment(treatment) |
| 176 | .build(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 177 | } |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 178 | } |