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