Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [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 | */ |
| 16 | package org.onosproject.net.flowobjective; |
| 17 | |
Ray Milkey | 3004c7d | 2017-12-13 09:43:20 -0800 | [diff] [blame] | 18 | import com.google.common.testing.EqualsTester; |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 19 | import org.junit.Test; |
Ray Milkey | 3004c7d | 2017-12-13 09:43:20 -0800 | [diff] [blame] | 20 | import org.onosproject.TestApplicationId; |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 21 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 22 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 23 | import org.onosproject.net.flow.TrafficSelector; |
| 24 | import org.onosproject.net.flow.TrafficTreatment; |
| 25 | import org.onosproject.net.flow.criteria.Criteria; |
| 26 | import org.onosproject.net.flow.criteria.Criterion; |
| 27 | |
Ray Milkey | 3004c7d | 2017-12-13 09:43:20 -0800 | [diff] [blame] | 28 | import static org.hamcrest.CoreMatchers.equalTo; |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 29 | import static org.hamcrest.CoreMatchers.hasItem; |
| 30 | import static org.hamcrest.CoreMatchers.is; |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 31 | import static org.hamcrest.CoreMatchers.nullValue; |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 32 | import static org.hamcrest.MatcherAssert.assertThat; |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 33 | import static org.hamcrest.Matchers.not; |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 34 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 35 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 36 | import static org.onosproject.net.flowobjective.FilteringObjective.Type.DENY; |
| 37 | import static org.onosproject.net.flowobjective.ForwardingObjective.Flag.SPECIFIC; |
Ray Milkey | 3004c7d | 2017-12-13 09:43:20 -0800 | [diff] [blame] | 38 | import static org.onosproject.net.flowobjective.ForwardingObjective.Flag.VERSATILE; |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 39 | import static org.onosproject.net.flowobjective.NextObjective.Type.HASHED; |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 40 | import static org.onosproject.net.flowobjective.Objective.Operation.ADD; |
Ray Milkey | 3004c7d | 2017-12-13 09:43:20 -0800 | [diff] [blame] | 41 | import static org.onosproject.net.flowobjective.Objective.Operation.ADD_TO_EXISTING; |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 42 | import static org.onosproject.net.flowobjective.Objective.Operation.REMOVE; |
Ray Milkey | 3004c7d | 2017-12-13 09:43:20 -0800 | [diff] [blame] | 43 | import static org.onosproject.net.flowobjective.Objective.Operation.REMOVE_FROM_EXISTING; |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Unit tests for forwarding objective class. |
| 47 | */ |
| 48 | public class ObjectiveTest { |
| 49 | |
| 50 | private final TrafficTreatment treatment = |
| 51 | DefaultTrafficTreatment.emptyTreatment(); |
| 52 | private final TrafficSelector selector = |
| 53 | DefaultTrafficSelector.emptySelector(); |
| 54 | private final Criterion criterion = Criteria.dummy(); |
| 55 | private final Criterion key = Criteria.dummy(); |
| 56 | |
| 57 | /** |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 58 | * Checks immutability of objective classes. |
| 59 | */ |
| 60 | @Test |
| 61 | public void testImmutability() { |
| 62 | assertThatClassIsImmutable(DefaultFilteringObjective.class); |
| 63 | assertThatClassIsImmutable(DefaultForwardingObjective.class); |
| 64 | assertThatClassIsImmutable(DefaultNextObjective.class); |
| 65 | } |
| 66 | |
| 67 | // Forwarding Objectives |
| 68 | |
| 69 | /** |
| 70 | * Makes a forwarding objective builder with a set of default values. |
| 71 | * |
| 72 | * @return forwarding objective builder |
| 73 | */ |
| 74 | private ForwardingObjective.Builder baseForwardingBuilder() { |
| 75 | return DefaultForwardingObjective.builder() |
| 76 | .withSelector(selector) |
| 77 | .withTreatment(treatment) |
| 78 | .withFlag(SPECIFIC) |
Ray Milkey | 3004c7d | 2017-12-13 09:43:20 -0800 | [diff] [blame] | 79 | .withMeta(selector) |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 80 | .fromApp(APP_ID) |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 81 | .withPriority(22) |
| 82 | .makeTemporary(5) |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 83 | .nextStep(33); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Checks the default values of a forwarding objective object. |
| 88 | * |
| 89 | * @param objective forwarding objective to check |
| 90 | */ |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 91 | private void checkForwardingBase(ForwardingObjective objective, |
| 92 | Objective.Operation op, |
| 93 | ObjectiveContext expectedContext) { |
| 94 | assertThat(objective.permanent(), is(false)); |
| 95 | assertThat(objective.timeout(), is(5)); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 96 | assertThat(objective.selector(), is(selector)); |
Ray Milkey | 3004c7d | 2017-12-13 09:43:20 -0800 | [diff] [blame] | 97 | assertThat(objective.meta(), is(selector)); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 98 | assertThat(objective.treatment(), is(treatment)); |
| 99 | assertThat(objective.flag(), is(SPECIFIC)); |
| 100 | assertThat(objective.appId(), is(APP_ID)); |
| 101 | assertThat(objective.nextId(), is(33)); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 102 | assertThat(objective.id(), is(not(0))); |
| 103 | assertThat(objective.priority(), is(22)); |
| 104 | assertThat(objective.op(), is(op)); |
| 105 | if (objective.context().isPresent()) { |
| 106 | assertThat(objective.context().get(), is(expectedContext)); |
| 107 | } else { |
| 108 | assertThat(expectedContext, nullValue()); |
| 109 | } |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Tests that forwarding objective objects are built correctly using the |
| 114 | * add() method. |
| 115 | */ |
| 116 | @Test |
| 117 | public void testForwardingAdd() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 118 | checkForwardingBase(baseForwardingBuilder().add(), ADD, null); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Tests that forwarding objective objects are built correctly using the |
| 123 | * add(context) method. |
| 124 | */ |
| 125 | @Test |
| 126 | public void testForwardingAddWithContext() { |
Ray Milkey | 3004c7d | 2017-12-13 09:43:20 -0800 | [diff] [blame] | 127 | ObjectiveContext context = new DefaultObjectiveContext(null, null); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 128 | checkForwardingBase(baseForwardingBuilder().add(context), ADD, context); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Tests that forwarding objective objects are built correctly using the |
| 133 | * remove() method. |
| 134 | */ |
| 135 | @Test |
| 136 | public void testForwardingRemove() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 137 | checkForwardingBase(baseForwardingBuilder().remove(), REMOVE, null); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Tests that forwarding objective objects are built correctly using the |
| 142 | * remove(context) method. |
| 143 | */ |
| 144 | @Test |
| 145 | public void testForwardingRemoveWithContext() { |
Ray Milkey | 3004c7d | 2017-12-13 09:43:20 -0800 | [diff] [blame] | 146 | ObjectiveContext context = new DefaultObjectiveContext(null, null); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 147 | checkForwardingBase(baseForwardingBuilder().remove(context), REMOVE, context); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // Filtering objectives |
| 151 | |
| 152 | /** |
| 153 | * Makes a filtering objective builder with a set of default values. |
| 154 | * |
| 155 | * @return filtering objective builder |
| 156 | */ |
| 157 | private FilteringObjective.Builder baseFilteringBuilder() { |
| 158 | return DefaultFilteringObjective.builder() |
| 159 | .withKey(key) |
| 160 | .withPriority(5) |
Ray Milkey | 3004c7d | 2017-12-13 09:43:20 -0800 | [diff] [blame] | 161 | .withMeta(treatment) |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 162 | .addCondition(criterion) |
| 163 | .fromApp(APP_ID) |
| 164 | .makeTemporary(2) |
| 165 | .deny(); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Checks the default values of a filtering objective object. |
| 170 | * |
| 171 | * @param objective filtering objective to check |
| 172 | */ |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 173 | private void checkFilteringBase(FilteringObjective objective, |
| 174 | Objective.Operation op, |
| 175 | ObjectiveContext expectedContext) { |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 176 | assertThat(objective.key(), is(key)); |
| 177 | assertThat(objective.conditions(), hasItem(criterion)); |
Ray Milkey | 3004c7d | 2017-12-13 09:43:20 -0800 | [diff] [blame] | 178 | assertThat(objective.meta(), is(treatment)); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 179 | assertThat(objective.permanent(), is(false)); |
| 180 | assertThat(objective.timeout(), is(2)); |
| 181 | assertThat(objective.priority(), is(5)); |
| 182 | assertThat(objective.appId(), is(APP_ID)); |
| 183 | assertThat(objective.type(), is(DENY)); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 184 | assertThat(objective.id(), is(not(0))); |
| 185 | assertThat(objective.op(), is(op)); |
| 186 | if (objective.context().isPresent()) { |
| 187 | assertThat(objective.context().get(), is(expectedContext)); |
| 188 | } else { |
| 189 | assertThat(expectedContext, nullValue()); |
| 190 | } |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Tests that forwarding objective objects are built correctly using the |
| 195 | * add() method. |
| 196 | */ |
| 197 | @Test |
| 198 | public void testFilteringAdd() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 199 | checkFilteringBase(baseFilteringBuilder().add(), ADD, null); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Tests that forwarding objective objects are built correctly using the |
| 204 | * add(context) method. |
| 205 | */ |
| 206 | @Test |
| 207 | public void testFilteringAddWithContext() { |
Ray Milkey | 3004c7d | 2017-12-13 09:43:20 -0800 | [diff] [blame] | 208 | ObjectiveContext context = new DefaultObjectiveContext(null, null); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 209 | checkFilteringBase(baseFilteringBuilder().add(context), ADD, context); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Tests that forwarding objective objects are built correctly using the |
| 214 | * remove() method. |
| 215 | */ |
| 216 | @Test |
| 217 | public void testFilteringRemove() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 218 | checkFilteringBase(baseFilteringBuilder().remove(), REMOVE, null); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Tests that forwarding objective objects are built correctly using the |
| 223 | * remove(context) method. |
| 224 | */ |
| 225 | @Test |
| 226 | public void testFilteringRemoveWithContext() { |
Ray Milkey | 3004c7d | 2017-12-13 09:43:20 -0800 | [diff] [blame] | 227 | ObjectiveContext context = new DefaultObjectiveContext(null, null); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 228 | checkFilteringBase(baseFilteringBuilder().remove(context), REMOVE, context); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | // Next objectives |
| 232 | |
| 233 | /** |
| 234 | * Makes a next objective builder with a set of default values. |
| 235 | * |
| 236 | * @return next objective builder |
| 237 | */ |
| 238 | private NextObjective.Builder baseNextBuilder() { |
| 239 | return DefaultNextObjective.builder() |
| 240 | .addTreatment(treatment) |
| 241 | .withId(12) |
| 242 | .withType(HASHED) |
Ray Milkey | 3004c7d | 2017-12-13 09:43:20 -0800 | [diff] [blame] | 243 | .withMeta(selector) |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 244 | .makeTemporary(777) |
| 245 | .withPriority(33) |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 246 | .fromApp(APP_ID); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Checks the default values of a next objective object. |
| 251 | * |
| 252 | * @param objective next objective to check |
| 253 | */ |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 254 | private void checkNextBase(NextObjective objective, |
| 255 | Objective.Operation op, |
| 256 | ObjectiveContext expectedContext) { |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 257 | assertThat(objective.id(), is(12)); |
| 258 | assertThat(objective.appId(), is(APP_ID)); |
| 259 | assertThat(objective.type(), is(HASHED)); |
| 260 | assertThat(objective.next(), hasItem(treatment)); |
Ray Milkey | 3004c7d | 2017-12-13 09:43:20 -0800 | [diff] [blame] | 261 | assertThat(objective.meta(), is(selector)); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 262 | assertThat(objective.permanent(), is(false)); |
| 263 | assertThat(objective.timeout(), is(0)); |
| 264 | assertThat(objective.priority(), is(0)); |
| 265 | assertThat(objective.op(), is(op)); |
| 266 | if (objective.context().isPresent()) { |
| 267 | assertThat(objective.context().get(), is(expectedContext)); |
| 268 | } else { |
| 269 | assertThat(expectedContext, nullValue()); |
| 270 | } |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Tests that forwarding objective objects are built correctly using the |
| 275 | * add() method. |
| 276 | */ |
| 277 | @Test |
| 278 | public void testNextAdd() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 279 | checkNextBase(baseNextBuilder().add(), ADD, null); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Tests that forwarding objective objects are built correctly using the |
| 284 | * add(context) method. |
| 285 | */ |
| 286 | @Test |
| 287 | public void testNextAddWithContext() { |
Ray Milkey | 3004c7d | 2017-12-13 09:43:20 -0800 | [diff] [blame] | 288 | ObjectiveContext context = new DefaultObjectiveContext(null, null); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 289 | checkNextBase(baseNextBuilder().add(context), ADD, context); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Tests that forwarding objective objects are built correctly using the |
| 294 | * remove() method. |
| 295 | */ |
| 296 | @Test |
| 297 | public void testNextRemove() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 298 | checkNextBase(baseNextBuilder().remove(), REMOVE, null); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Tests that forwarding objective objects are built correctly using the |
| 303 | * remove(context) method. |
| 304 | */ |
| 305 | @Test |
| 306 | public void testNextRemoveWithContext() { |
Ray Milkey | 3004c7d | 2017-12-13 09:43:20 -0800 | [diff] [blame] | 307 | ObjectiveContext context = new DefaultObjectiveContext(null, null); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 308 | checkNextBase(baseNextBuilder().remove(context), REMOVE, context); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 309 | } |
Ray Milkey | 3004c7d | 2017-12-13 09:43:20 -0800 | [diff] [blame] | 310 | |
| 311 | /** |
| 312 | * Tests equality, hash, and toString operations on next objectives. |
| 313 | */ |
| 314 | @Test |
| 315 | public void testEqualsNextObjective() { |
| 316 | NextObjective next1 = baseNextBuilder().add(); |
| 317 | NextObjective sameAsNext1 = next1.copy().add(); |
| 318 | NextObjective next2 = baseNextBuilder().verify(); |
| 319 | NextObjective next3 = baseNextBuilder().fromApp(new TestApplicationId("foo2")).verify(); |
| 320 | NextObjective next4 = baseNextBuilder().withType(NextObjective.Type.FAILOVER).verify(); |
| 321 | NextObjective next5 = baseNextBuilder().addTreatment(DefaultTrafficTreatment.emptyTreatment()).verify(); |
| 322 | new EqualsTester() |
| 323 | .addEqualityGroup(next1, sameAsNext1) |
| 324 | .addEqualityGroup(next2) |
| 325 | .addEqualityGroup(next3) |
| 326 | .addEqualityGroup(next4) |
| 327 | .addEqualityGroup(next5) |
| 328 | .testEquals(); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Tests add to an existing next objective. |
| 333 | */ |
| 334 | @Test |
| 335 | public void testToFromExistingNextObjective() { |
| 336 | NextObjective next1 = baseNextBuilder().addToExisting(); |
| 337 | |
| 338 | checkNextBase(next1, ADD_TO_EXISTING, null); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Tests remove from an existing next objective. |
| 343 | */ |
| 344 | @Test |
| 345 | public void testRemoveFromExistingNextObjective() { |
| 346 | NextObjective next1 = baseNextBuilder().removeFromExisting(); |
| 347 | |
| 348 | checkNextBase(next1, REMOVE_FROM_EXISTING, null); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Tests equality, hash, and toString operations on filtering objectives. |
| 353 | */ |
| 354 | @Test |
| 355 | public void testEqualsFilteringObjective() { |
| 356 | FilteringObjective filter1 = baseFilteringBuilder().add(); |
| 357 | FilteringObjective sameAsFilter1 = filter1.copy().add(); |
| 358 | FilteringObjective filter2 = baseFilteringBuilder().permit().add(); |
| 359 | FilteringObjective filter3 = baseFilteringBuilder().fromApp(new TestApplicationId("foo2")).add(); |
| 360 | FilteringObjective filter4 = baseFilteringBuilder().permit().remove(); |
| 361 | FilteringObjective filter5 = baseFilteringBuilder().withPriority(55).add(); |
| 362 | FilteringObjective filter6 = baseFilteringBuilder().makePermanent().add(); |
| 363 | new EqualsTester() |
| 364 | .addEqualityGroup(filter1, sameAsFilter1) |
| 365 | .addEqualityGroup(filter2) |
| 366 | .addEqualityGroup(filter3) |
| 367 | .addEqualityGroup(filter4) |
| 368 | .addEqualityGroup(filter5) |
| 369 | .addEqualityGroup(filter6) |
| 370 | .testEquals(); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Tests add to an existing filtering objective. |
| 375 | */ |
| 376 | @Test |
| 377 | public void testToFromExistingFilteringObjective() { |
| 378 | FilteringObjective filter1 = baseFilteringBuilder().add(null); |
| 379 | |
| 380 | checkFilteringBase(filter1, ADD, null); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Tests remove from an existing filtering objective. |
| 385 | */ |
| 386 | @Test |
| 387 | public void testRemoveFromExistingFilteringObjective() { |
| 388 | FilteringObjective filter1 = baseFilteringBuilder().remove(null); |
| 389 | |
| 390 | checkFilteringBase(filter1, REMOVE, null); |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Tests equality, hash, and toString operations on filtering objectives. |
| 395 | */ |
| 396 | @Test |
| 397 | public void testEqualsForwardingObjective() { |
| 398 | ForwardingObjective forward1 = baseForwardingBuilder().add(); |
| 399 | ForwardingObjective sameAsForward1 = forward1.copy().add(); |
| 400 | ForwardingObjective forward2 = baseForwardingBuilder().withFlag(VERSATILE).add(); |
| 401 | ForwardingObjective forward3 = baseForwardingBuilder().fromApp(new TestApplicationId("foo2")).add(); |
| 402 | ForwardingObjective forward4 = baseForwardingBuilder().remove(); |
| 403 | ForwardingObjective forward5 = baseForwardingBuilder().withPriority(55).add(); |
| 404 | ForwardingObjective forward6 = baseForwardingBuilder().makePermanent().add(); |
| 405 | new EqualsTester() |
| 406 | .addEqualityGroup(forward1, sameAsForward1) |
| 407 | .addEqualityGroup(forward2) |
| 408 | .addEqualityGroup(forward3) |
| 409 | .addEqualityGroup(forward4) |
| 410 | .addEqualityGroup(forward5) |
| 411 | .addEqualityGroup(forward6) |
| 412 | .testEquals(); |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Tests add to an existing forwarding objective. |
| 417 | */ |
| 418 | @Test |
| 419 | public void testToFromExistingForwardingObjective() { |
| 420 | ForwardingObjective forward1 = baseForwardingBuilder().add(null); |
| 421 | |
| 422 | checkForwardingBase(forward1, ADD, null); |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Tests remove from an existing forwarding objective. |
| 427 | */ |
| 428 | @Test |
| 429 | public void testRemoveFromExistingForwardingObjective() { |
| 430 | ForwardingObjective forward1 = baseForwardingBuilder().remove(null); |
| 431 | |
| 432 | checkForwardingBase(forward1, REMOVE, null); |
| 433 | } |
| 434 | |
| 435 | |
| 436 | enum ContextType { |
| 437 | BOTH, |
| 438 | ERROR_ONLY, |
| 439 | SUCCESS_ONLY |
| 440 | } |
| 441 | |
| 442 | class ObjectiveContextAdapter implements ObjectiveContext { |
| 443 | DefaultObjectiveContext context; |
| 444 | int successCount = 0; |
| 445 | int errorCount = 0; |
| 446 | ObjectiveError objectiveError = ObjectiveError.UNKNOWN; |
| 447 | Objective objectiveInError = null; |
| 448 | |
| 449 | ObjectiveContextAdapter(ContextType type) { |
| 450 | switch (type) { |
| 451 | |
| 452 | case ERROR_ONLY: |
| 453 | context = new DefaultObjectiveContext( |
| 454 | (failedObjective, error) -> { |
| 455 | errorCount++; |
| 456 | objectiveInError = failedObjective; |
| 457 | objectiveError = error; |
| 458 | }); |
| 459 | break; |
| 460 | |
| 461 | case SUCCESS_ONLY: |
| 462 | context = new DefaultObjectiveContext( |
| 463 | (successfulObjective) -> successCount++); |
| 464 | break; |
| 465 | |
| 466 | case BOTH: |
| 467 | default: |
| 468 | context = new DefaultObjectiveContext( |
| 469 | (successfulObjective) -> successCount++, |
| 470 | (failedObjective, error) -> { |
| 471 | errorCount++; |
| 472 | objectiveInError = failedObjective; |
| 473 | objectiveError = error; |
| 474 | }); |
| 475 | break; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | @Override |
| 480 | public void onSuccess(Objective objective) { |
| 481 | context.onSuccess(objective); |
| 482 | } |
| 483 | |
| 484 | @Override |
| 485 | public void onError(Objective objective, ObjectiveError error) { |
| 486 | context.onError(objective, error); |
| 487 | } |
| 488 | |
| 489 | int successCount() { |
| 490 | return successCount; |
| 491 | } |
| 492 | |
| 493 | int errorCount() { |
| 494 | return errorCount; |
| 495 | } |
| 496 | |
| 497 | ObjectiveError objectiveError() { |
| 498 | return objectiveError; |
| 499 | } |
| 500 | |
| 501 | Objective objectiveInError() { |
| 502 | return objectiveInError; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Tests default objective context. |
| 508 | */ |
| 509 | @Test |
| 510 | public void testDefaultContext() { |
| 511 | Objective objective = baseFilteringBuilder().add(); |
| 512 | ObjectiveContextAdapter context; |
| 513 | |
| 514 | context = new ObjectiveContextAdapter(ContextType.BOTH); |
| 515 | context.onSuccess(objective); |
| 516 | assertThat(context.successCount(), is(1)); |
| 517 | assertThat(context.errorCount(), is(0)); |
| 518 | assertThat(context.objectiveError(), is(ObjectiveError.UNKNOWN)); |
| 519 | assertThat(context.objectiveInError(), nullValue()); |
| 520 | |
| 521 | context = new ObjectiveContextAdapter(ContextType.BOTH); |
| 522 | context.onError(objective, ObjectiveError.UNSUPPORTED); |
| 523 | assertThat(context.successCount(), is(0)); |
| 524 | assertThat(context.errorCount(), is(1)); |
| 525 | assertThat(context.objectiveError(), is(ObjectiveError.UNSUPPORTED)); |
| 526 | assertThat(context.objectiveInError(), equalTo(objective)); |
| 527 | |
| 528 | context = new ObjectiveContextAdapter(ContextType.SUCCESS_ONLY); |
| 529 | context.onSuccess(objective); |
| 530 | assertThat(context.successCount(), is(1)); |
| 531 | assertThat(context.errorCount(), is(0)); |
| 532 | assertThat(context.objectiveError(), is(ObjectiveError.UNKNOWN)); |
| 533 | assertThat(context.objectiveInError(), nullValue()); |
| 534 | |
| 535 | context = new ObjectiveContextAdapter(ContextType.ERROR_ONLY); |
| 536 | context.onError(objective, ObjectiveError.UNSUPPORTED); |
| 537 | assertThat(context.successCount(), is(0)); |
| 538 | assertThat(context.errorCount(), is(1)); |
| 539 | assertThat(context.objectiveError(), is(ObjectiveError.UNSUPPORTED)); |
| 540 | assertThat(context.objectiveInError(), equalTo(objective)); |
| 541 | } |
| 542 | |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 543 | } |