Updates related to SDN-IP submitting intent operations:
* Updated IntentSynchronizer.submitPeerIntents() to use intent batch
operation
* Refactored the SDN-IP unit tests:
The verification for intent service operations is moved to a new class
TestIntentServiceHeler
Change-Id: Id6766ebb4d6bf492e8f9787c2c2247ac38d42964
Showing
4 changed files
with
234 additions
and
205 deletions
| ... | @@ -210,10 +210,11 @@ public class IntentSynchronizer { | ... | @@ -210,10 +210,11 @@ public class IntentSynchronizer { |
| 210 | // Push the intents | 210 | // Push the intents |
| 211 | if (isElectedLeader && isActivatedLeader) { | 211 | if (isElectedLeader && isActivatedLeader) { |
| 212 | log.debug("Submitting all SDN-IP Peer Intents..."); | 212 | log.debug("Submitting all SDN-IP Peer Intents..."); |
| 213 | - // TODO: We should use a single Intent batch operation | 213 | + IntentOperations.Builder builder = IntentOperations.builder(); |
| 214 | for (Intent intent : intents) { | 214 | for (Intent intent : intents) { |
| 215 | - intentService.submit(intent); | 215 | + builder.addSubmitOperation(intent); |
| 216 | } | 216 | } |
| 217 | + intentService.execute(builder.build()); | ||
| 217 | } | 218 | } |
| 218 | } | 219 | } |
| 219 | } | 220 | } | ... | ... |
| ... | @@ -5,7 +5,6 @@ import static org.easymock.EasyMock.createMock; | ... | @@ -5,7 +5,6 @@ import static org.easymock.EasyMock.createMock; |
| 5 | import static org.easymock.EasyMock.expect; | 5 | import static org.easymock.EasyMock.expect; |
| 6 | import static org.easymock.EasyMock.expectLastCall; | 6 | import static org.easymock.EasyMock.expectLastCall; |
| 7 | import static org.easymock.EasyMock.replay; | 7 | import static org.easymock.EasyMock.replay; |
| 8 | -import static org.easymock.EasyMock.reportMatcher; | ||
| 9 | import static org.easymock.EasyMock.reset; | 8 | import static org.easymock.EasyMock.reset; |
| 10 | import static org.easymock.EasyMock.verify; | 9 | import static org.easymock.EasyMock.verify; |
| 11 | import static org.hamcrest.Matchers.is; | 10 | import static org.hamcrest.Matchers.is; |
| ... | @@ -15,13 +14,9 @@ import static org.junit.Assert.assertThat; | ... | @@ -15,13 +14,9 @@ import static org.junit.Assert.assertThat; |
| 15 | import static org.junit.Assert.assertTrue; | 14 | import static org.junit.Assert.assertTrue; |
| 16 | 15 | ||
| 17 | import java.util.HashSet; | 16 | import java.util.HashSet; |
| 18 | -import java.util.LinkedList; | ||
| 19 | -import java.util.List; | ||
| 20 | import java.util.Set; | 17 | import java.util.Set; |
| 21 | import java.util.concurrent.ConcurrentHashMap; | 18 | import java.util.concurrent.ConcurrentHashMap; |
| 22 | 19 | ||
| 23 | -import org.apache.commons.collections4.CollectionUtils; | ||
| 24 | -import org.easymock.IArgumentMatcher; | ||
| 25 | import org.junit.Before; | 20 | import org.junit.Before; |
| 26 | import org.junit.Test; | 21 | import org.junit.Test; |
| 27 | import org.onlab.junit.TestUtils; | 22 | import org.onlab.junit.TestUtils; |
| ... | @@ -42,14 +37,11 @@ import org.onlab.onos.net.host.HostListener; | ... | @@ -42,14 +37,11 @@ import org.onlab.onos.net.host.HostListener; |
| 42 | import org.onlab.onos.net.host.HostService; | 37 | import org.onlab.onos.net.host.HostService; |
| 43 | import org.onlab.onos.net.host.InterfaceIpAddress; | 38 | import org.onlab.onos.net.host.InterfaceIpAddress; |
| 44 | import org.onlab.onos.net.intent.Intent; | 39 | import org.onlab.onos.net.intent.Intent; |
| 45 | -import org.onlab.onos.net.intent.IntentId; | ||
| 46 | -import org.onlab.onos.net.intent.IntentOperation; | ||
| 47 | import org.onlab.onos.net.intent.IntentOperations; | 40 | import org.onlab.onos.net.intent.IntentOperations; |
| 48 | import org.onlab.onos.net.intent.IntentService; | 41 | import org.onlab.onos.net.intent.IntentService; |
| 49 | import org.onlab.onos.net.intent.IntentState; | 42 | import org.onlab.onos.net.intent.IntentState; |
| 50 | import org.onlab.onos.net.intent.MultiPointToSinglePointIntent; | 43 | import org.onlab.onos.net.intent.MultiPointToSinglePointIntent; |
| 51 | import org.onlab.onos.net.provider.ProviderId; | 44 | import org.onlab.onos.net.provider.ProviderId; |
| 52 | -import org.onlab.onos.sdnip.IntentSynchronizer.IntentKey; | ||
| 53 | import org.onlab.onos.sdnip.config.Interface; | 45 | import org.onlab.onos.sdnip.config.Interface; |
| 54 | import org.onlab.packet.Ethernet; | 46 | import org.onlab.packet.Ethernet; |
| 55 | import org.onlab.packet.IpAddress; | 47 | import org.onlab.packet.IpAddress; |
| ... | @@ -334,14 +326,16 @@ public class IntentSyncTest { | ... | @@ -334,14 +326,16 @@ public class IntentSyncTest { |
| 334 | IntentOperations.Builder builder = IntentOperations.builder(); | 326 | IntentOperations.Builder builder = IntentOperations.builder(); |
| 335 | builder.addWithdrawOperation(intent2.id()); | 327 | builder.addWithdrawOperation(intent2.id()); |
| 336 | builder.addWithdrawOperation(intent4.id()); | 328 | builder.addWithdrawOperation(intent4.id()); |
| 337 | - intentService.execute(eqExceptId(builder.build())); | 329 | + intentService.execute(TestIntentServiceHelper.eqExceptId( |
| 330 | + builder.build())); | ||
| 338 | 331 | ||
| 339 | builder = IntentOperations.builder(); | 332 | builder = IntentOperations.builder(); |
| 340 | builder.addSubmitOperation(intent3); | 333 | builder.addSubmitOperation(intent3); |
| 341 | builder.addSubmitOperation(intent4Update); | 334 | builder.addSubmitOperation(intent4Update); |
| 342 | builder.addSubmitOperation(intent6); | 335 | builder.addSubmitOperation(intent6); |
| 343 | builder.addSubmitOperation(intent7); | 336 | builder.addSubmitOperation(intent7); |
| 344 | - intentService.execute(eqExceptId(builder.build())); | 337 | + intentService.execute(TestIntentServiceHelper.eqExceptId( |
| 338 | + builder.build())); | ||
| 345 | replay(intentService); | 339 | replay(intentService); |
| 346 | 340 | ||
| 347 | // Start the test | 341 | // Start the test |
| ... | @@ -427,129 +421,4 @@ public class IntentSyncTest { | ... | @@ -427,129 +421,4 @@ public class IntentSyncTest { |
| 427 | "ingressPoints", intent.ingressPoints()); | 421 | "ingressPoints", intent.ingressPoints()); |
| 428 | return intentNew; | 422 | return intentNew; |
| 429 | } | 423 | } |
| 430 | - | ||
| 431 | - /* | ||
| 432 | - * EasyMock matcher that matches {@link IntenOperations} but | ||
| 433 | - * ignores the {@link IntentId} when matching. | ||
| 434 | - * <p/> | ||
| 435 | - * The normal intent equals method tests that the intent IDs are equal, | ||
| 436 | - * however in these tests we can't know what the intent IDs will be in | ||
| 437 | - * advance, so we can't set up expected intents with the correct IDs. Thus, | ||
| 438 | - * the solution is to use an EasyMock matcher that verifies that all the | ||
| 439 | - * value properties of the provided intent match the expected values, but | ||
| 440 | - * ignores the intent ID when testing equality. | ||
| 441 | - */ | ||
| 442 | - private static final class IdAgnosticIntentOperationsMatcher implements | ||
| 443 | - IArgumentMatcher { | ||
| 444 | - | ||
| 445 | - private final IntentOperations intentOperations; | ||
| 446 | - private String providedString; | ||
| 447 | - | ||
| 448 | - /** | ||
| 449 | - * Constructor taking the expected intent operations to match against. | ||
| 450 | - * | ||
| 451 | - * @param intentOperations the expected intent operations | ||
| 452 | - */ | ||
| 453 | - public IdAgnosticIntentOperationsMatcher( | ||
| 454 | - IntentOperations intentOperations) { | ||
| 455 | - this.intentOperations = intentOperations; | ||
| 456 | - } | ||
| 457 | - | ||
| 458 | - @Override | ||
| 459 | - public void appendTo(StringBuffer strBuffer) { | ||
| 460 | - strBuffer.append("IntentOperationsMatcher unable to match: " | ||
| 461 | - + providedString); | ||
| 462 | - } | ||
| 463 | - | ||
| 464 | - @Override | ||
| 465 | - public boolean matches(Object object) { | ||
| 466 | - if (!(object instanceof IntentOperations)) { | ||
| 467 | - return false; | ||
| 468 | - } | ||
| 469 | - | ||
| 470 | - IntentOperations providedIntentOperations = | ||
| 471 | - (IntentOperations) object; | ||
| 472 | - providedString = providedIntentOperations.toString(); | ||
| 473 | - | ||
| 474 | - List<IntentKey> thisSubmitIntents = new LinkedList<>(); | ||
| 475 | - List<IntentId> thisWithdrawIntentIds = new LinkedList<>(); | ||
| 476 | - List<IntentKey> thisReplaceIntents = new LinkedList<>(); | ||
| 477 | - List<IntentKey> thisUpdateIntents = new LinkedList<>(); | ||
| 478 | - List<IntentKey> providedSubmitIntents = new LinkedList<>(); | ||
| 479 | - List<IntentId> providedWithdrawIntentIds = new LinkedList<>(); | ||
| 480 | - List<IntentKey> providedReplaceIntents = new LinkedList<>(); | ||
| 481 | - List<IntentKey> providedUpdateIntents = new LinkedList<>(); | ||
| 482 | - | ||
| 483 | - extractIntents(intentOperations, thisSubmitIntents, | ||
| 484 | - thisWithdrawIntentIds, thisReplaceIntents, | ||
| 485 | - thisUpdateIntents); | ||
| 486 | - extractIntents(providedIntentOperations, providedSubmitIntents, | ||
| 487 | - providedWithdrawIntentIds, providedReplaceIntents, | ||
| 488 | - providedUpdateIntents); | ||
| 489 | - | ||
| 490 | - return CollectionUtils.isEqualCollection(thisSubmitIntents, | ||
| 491 | - providedSubmitIntents) && | ||
| 492 | - CollectionUtils.isEqualCollection(thisWithdrawIntentIds, | ||
| 493 | - providedWithdrawIntentIds) && | ||
| 494 | - CollectionUtils.isEqualCollection(thisUpdateIntents, | ||
| 495 | - providedUpdateIntents) && | ||
| 496 | - CollectionUtils.isEqualCollection(thisReplaceIntents, | ||
| 497 | - providedReplaceIntents); | ||
| 498 | - } | ||
| 499 | - | ||
| 500 | - /** | ||
| 501 | - * Extracts the intents per operation type. Each intent is encapsulated | ||
| 502 | - * in IntentKey so it can be compared by excluding the Intent ID. | ||
| 503 | - * | ||
| 504 | - * @param intentOperations the container with the intent operations | ||
| 505 | - * to extract the intents from | ||
| 506 | - * @param submitIntents the SUBMIT intents | ||
| 507 | - * @param withdrawIntentIds the WITHDRAW intents IDs | ||
| 508 | - * @param replaceIntents the REPLACE intents | ||
| 509 | - * @param updateIntents the UPDATE intens | ||
| 510 | - */ | ||
| 511 | - private void extractIntents(IntentOperations intentOperations, | ||
| 512 | - List<IntentKey> submitIntents, | ||
| 513 | - List<IntentId> withdrawIntentIds, | ||
| 514 | - List<IntentKey> replaceIntents, | ||
| 515 | - List<IntentKey> updateIntents) { | ||
| 516 | - for (IntentOperation oper : intentOperations.operations()) { | ||
| 517 | - IntentId intentId; | ||
| 518 | - IntentKey intentKey; | ||
| 519 | - switch (oper.type()) { | ||
| 520 | - case SUBMIT: | ||
| 521 | - intentKey = new IntentKey(oper.intent()); | ||
| 522 | - submitIntents.add(intentKey); | ||
| 523 | - break; | ||
| 524 | - case WITHDRAW: | ||
| 525 | - intentId = oper.intentId(); | ||
| 526 | - withdrawIntentIds.add(intentId); | ||
| 527 | - break; | ||
| 528 | - case REPLACE: | ||
| 529 | - intentKey = new IntentKey(oper.intent()); | ||
| 530 | - replaceIntents.add(intentKey); | ||
| 531 | - break; | ||
| 532 | - case UPDATE: | ||
| 533 | - intentKey = new IntentKey(oper.intent()); | ||
| 534 | - updateIntents.add(intentKey); | ||
| 535 | - break; | ||
| 536 | - default: | ||
| 537 | - break; | ||
| 538 | - } | ||
| 539 | - } | ||
| 540 | - } | ||
| 541 | - } | ||
| 542 | - | ||
| 543 | - /** | ||
| 544 | - * Matcher method to set an expected intent to match against (ignoring the | ||
| 545 | - * the intent ID). | ||
| 546 | - * | ||
| 547 | - * @param intent the expected intent | ||
| 548 | - * @return something of type IntentOperations | ||
| 549 | - */ | ||
| 550 | - private static IntentOperations eqExceptId( | ||
| 551 | - IntentOperations intentOperations) { | ||
| 552 | - reportMatcher(new IdAgnosticIntentOperationsMatcher(intentOperations)); | ||
| 553 | - return intentOperations; | ||
| 554 | - } | ||
| 555 | } | 424 | } | ... | ... |
| ... | @@ -16,7 +16,6 @@ | ... | @@ -16,7 +16,6 @@ |
| 16 | package org.onlab.onos.sdnip; | 16 | package org.onlab.onos.sdnip; |
| 17 | 17 | ||
| 18 | import com.google.common.collect.Sets; | 18 | import com.google.common.collect.Sets; |
| 19 | -import org.easymock.IArgumentMatcher; | ||
| 20 | import org.junit.Before; | 19 | import org.junit.Before; |
| 21 | import org.junit.Ignore; | 20 | import org.junit.Ignore; |
| 22 | import org.junit.Test; | 21 | import org.junit.Test; |
| ... | @@ -31,6 +30,8 @@ import org.onlab.onos.net.flow.DefaultTrafficTreatment; | ... | @@ -31,6 +30,8 @@ import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 31 | import org.onlab.onos.net.flow.TrafficSelector; | 30 | import org.onlab.onos.net.flow.TrafficSelector; |
| 32 | import org.onlab.onos.net.flow.TrafficTreatment; | 31 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 33 | import org.onlab.onos.net.host.InterfaceIpAddress; | 32 | import org.onlab.onos.net.host.InterfaceIpAddress; |
| 33 | +import org.onlab.onos.net.intent.Intent; | ||
| 34 | +import org.onlab.onos.net.intent.IntentOperations; | ||
| 34 | import org.onlab.onos.net.intent.IntentService; | 35 | import org.onlab.onos.net.intent.IntentService; |
| 35 | import org.onlab.onos.net.intent.PointToPointIntent; | 36 | import org.onlab.onos.net.intent.PointToPointIntent; |
| 36 | import org.onlab.onos.sdnip.bgp.BgpConstants; | 37 | import org.onlab.onos.sdnip.bgp.BgpConstants; |
| ... | @@ -550,69 +551,6 @@ public class PeerConnectivityManagerTest { | ... | @@ -550,69 +551,6 @@ public class PeerConnectivityManagerTest { |
| 550 | configInfoService, interfaceService); | 551 | configInfoService, interfaceService); |
| 551 | } | 552 | } |
| 552 | 553 | ||
| 553 | - /* | ||
| 554 | - * EasyMock matcher that matches {@link PointToPointIntent}s but | ||
| 555 | - * ignores the {@link IntentId} when matching. | ||
| 556 | - * <p/> | ||
| 557 | - * The normal intent equals method tests that the intent IDs are equal, | ||
| 558 | - * however in these tests we can't know what the intent IDs will be in | ||
| 559 | - * advance, so we can't set up expected intents with the correct IDs. Thus, | ||
| 560 | - * the solution is to use an EasyMock matcher that verifies that all the | ||
| 561 | - * value properties of the provided intent match the expected values, but | ||
| 562 | - * ignores the intent ID when testing equality. | ||
| 563 | - */ | ||
| 564 | - private static final class IdAgnosticPointToPointIntentMatcher implements | ||
| 565 | - IArgumentMatcher { | ||
| 566 | - | ||
| 567 | - private final PointToPointIntent intent; | ||
| 568 | - private String providedIntentString; | ||
| 569 | - | ||
| 570 | - /** | ||
| 571 | - * Constructor taking the expected intent to match against. | ||
| 572 | - * | ||
| 573 | - * @param intent the expected intent | ||
| 574 | - */ | ||
| 575 | - public IdAgnosticPointToPointIntentMatcher(PointToPointIntent intent) { | ||
| 576 | - this.intent = intent; | ||
| 577 | - } | ||
| 578 | - | ||
| 579 | - @Override | ||
| 580 | - public void appendTo(StringBuffer strBuffer) { | ||
| 581 | - strBuffer.append("PointToPointIntentMatcher unable to match: " | ||
| 582 | - + providedIntentString); | ||
| 583 | - } | ||
| 584 | - | ||
| 585 | - @Override | ||
| 586 | - public boolean matches(Object object) { | ||
| 587 | - if (!(object instanceof PointToPointIntent)) { | ||
| 588 | - return false; | ||
| 589 | - } | ||
| 590 | - | ||
| 591 | - PointToPointIntent providedIntent = (PointToPointIntent) object; | ||
| 592 | - providedIntentString = providedIntent.toString(); | ||
| 593 | - | ||
| 594 | - PointToPointIntent matchIntent = | ||
| 595 | - new PointToPointIntent(providedIntent.appId(), | ||
| 596 | - intent.selector(), intent.treatment(), | ||
| 597 | - intent.ingressPoint(), intent.egressPoint()); | ||
| 598 | - | ||
| 599 | - return matchIntent.equals(providedIntent); | ||
| 600 | - } | ||
| 601 | - } | ||
| 602 | - | ||
| 603 | - /** | ||
| 604 | - * Matcher method to set an expected intent to match against (ignoring the | ||
| 605 | - * the intent ID). | ||
| 606 | - * | ||
| 607 | - * @param intent the expected intent | ||
| 608 | - * @return something of type PointToPointIntent | ||
| 609 | - */ | ||
| 610 | - private static PointToPointIntent eqExceptId( | ||
| 611 | - PointToPointIntent intent) { | ||
| 612 | - reportMatcher(new IdAgnosticPointToPointIntentMatcher(intent)); | ||
| 613 | - return null; | ||
| 614 | - } | ||
| 615 | - | ||
| 616 | /** | 554 | /** |
| 617 | * Tests whether peer connectivity manager can set up correct BGP and | 555 | * Tests whether peer connectivity manager can set up correct BGP and |
| 618 | * ICMP intents according to specific configuration. | 556 | * ICMP intents according to specific configuration. |
| ... | @@ -625,11 +563,13 @@ public class PeerConnectivityManagerTest { | ... | @@ -625,11 +563,13 @@ public class PeerConnectivityManagerTest { |
| 625 | 563 | ||
| 626 | reset(intentService); | 564 | reset(intentService); |
| 627 | 565 | ||
| 628 | - // Sets up the expected PointToPoint intents. | 566 | + // Setup the expected intents |
| 629 | - for (int i = 0; i < intentList.size(); i++) { | 567 | + IntentOperations.Builder builder = IntentOperations.builder(); |
| 630 | - intentService.submit(eqExceptId(intentList.get(i))); | 568 | + for (Intent intent : intentList) { |
| 569 | + builder.addSubmitOperation(intent); | ||
| 631 | } | 570 | } |
| 632 | - | 571 | + intentService.execute(TestIntentServiceHelper.eqExceptId( |
| 572 | + builder.build())); | ||
| 633 | replay(intentService); | 573 | replay(intentService); |
| 634 | 574 | ||
| 635 | // Running the interface to be tested. | 575 | // Running the interface to be tested. |
| ... | @@ -659,6 +599,9 @@ public class PeerConnectivityManagerTest { | ... | @@ -659,6 +599,9 @@ public class PeerConnectivityManagerTest { |
| 659 | replay(configInfoService); | 599 | replay(configInfoService); |
| 660 | 600 | ||
| 661 | reset(intentService); | 601 | reset(intentService); |
| 602 | + IntentOperations.Builder builder = IntentOperations.builder(); | ||
| 603 | + intentService.execute(TestIntentServiceHelper.eqExceptId( | ||
| 604 | + builder.build())); | ||
| 662 | replay(intentService); | 605 | replay(intentService); |
| 663 | peerConnectivityManager.start(); | 606 | peerConnectivityManager.start(); |
| 664 | verify(intentService); | 607 | verify(intentService); |
| ... | @@ -682,6 +625,9 @@ public class PeerConnectivityManagerTest { | ... | @@ -682,6 +625,9 @@ public class PeerConnectivityManagerTest { |
| 682 | replay(configInfoService); | 625 | replay(configInfoService); |
| 683 | 626 | ||
| 684 | reset(intentService); | 627 | reset(intentService); |
| 628 | + IntentOperations.Builder builder = IntentOperations.builder(); | ||
| 629 | + intentService.execute(TestIntentServiceHelper.eqExceptId( | ||
| 630 | + builder.build())); | ||
| 685 | replay(intentService); | 631 | replay(intentService); |
| 686 | peerConnectivityManager.start(); | 632 | peerConnectivityManager.start(); |
| 687 | verify(intentService); | 633 | verify(intentService); | ... | ... |
| 1 | +package org.onlab.onos.sdnip; | ||
| 2 | + | ||
| 3 | +import java.util.LinkedList; | ||
| 4 | +import java.util.List; | ||
| 5 | + | ||
| 6 | +import org.apache.commons.collections4.CollectionUtils; | ||
| 7 | +import org.easymock.IArgumentMatcher; | ||
| 8 | +import org.onlab.onos.net.intent.IntentId; | ||
| 9 | +import org.onlab.onos.net.intent.IntentOperation; | ||
| 10 | +import org.onlab.onos.net.intent.IntentOperations; | ||
| 11 | +import org.onlab.onos.net.intent.PointToPointIntent; | ||
| 12 | +import org.onlab.onos.sdnip.IntentSynchronizer.IntentKey; | ||
| 13 | + | ||
| 14 | +import static org.easymock.EasyMock.reportMatcher; | ||
| 15 | + | ||
| 16 | +/** | ||
| 17 | + * Helper class for testing operations submitted to the IntentService. | ||
| 18 | + */ | ||
| 19 | +public final class TestIntentServiceHelper { | ||
| 20 | + /** | ||
| 21 | + * Default constructor to prevent instantiation. | ||
| 22 | + */ | ||
| 23 | + private TestIntentServiceHelper() { | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + /** | ||
| 27 | + * Matcher method to set the expected intent operations to match against | ||
| 28 | + * (ignoring the intent ID for each intent). | ||
| 29 | + * | ||
| 30 | + * @param intentOperations the expected Intent Operations | ||
| 31 | + * @return the submitted Intent Operations | ||
| 32 | + */ | ||
| 33 | + static IntentOperations eqExceptId(IntentOperations intentOperations) { | ||
| 34 | + reportMatcher(new IdAgnosticIntentOperationsMatcher(intentOperations)); | ||
| 35 | + return intentOperations; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + | ||
| 39 | + /** | ||
| 40 | + * Matcher method to set an expected point-to-point intent to match | ||
| 41 | + * against (ignoring the intent ID). | ||
| 42 | + * | ||
| 43 | + * @param intent the expected point-to-point intent | ||
| 44 | + * @return the submitted point-to-point intent | ||
| 45 | + */ | ||
| 46 | + private static PointToPointIntent eqExceptId( | ||
| 47 | + PointToPointIntent intent) { | ||
| 48 | + reportMatcher(new IdAgnosticPointToPointIntentMatcher(intent)); | ||
| 49 | + return intent; | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + /* | ||
| 53 | + * EasyMock matcher that matches {@link IntenOperations} but | ||
| 54 | + * ignores the {@link IntentId} when matching. | ||
| 55 | + * <p/> | ||
| 56 | + * The normal intent equals method tests that the intent IDs are equal, | ||
| 57 | + * however in these tests we can't know what the intent IDs will be in | ||
| 58 | + * advance, so we can't set up expected intents with the correct IDs. Thus, | ||
| 59 | + * the solution is to use an EasyMock matcher that verifies that all the | ||
| 60 | + * value properties of the provided intent match the expected values, but | ||
| 61 | + * ignores the intent ID when testing equality. | ||
| 62 | + */ | ||
| 63 | + private static final class IdAgnosticIntentOperationsMatcher implements | ||
| 64 | + IArgumentMatcher { | ||
| 65 | + | ||
| 66 | + private final IntentOperations intentOperations; | ||
| 67 | + private String providedString; | ||
| 68 | + | ||
| 69 | + /** | ||
| 70 | + * Constructor taking the expected intent operations to match against. | ||
| 71 | + * | ||
| 72 | + * @param intentOperations the expected intent operations | ||
| 73 | + */ | ||
| 74 | + public IdAgnosticIntentOperationsMatcher( | ||
| 75 | + IntentOperations intentOperations) { | ||
| 76 | + this.intentOperations = intentOperations; | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + @Override | ||
| 80 | + public void appendTo(StringBuffer strBuffer) { | ||
| 81 | + strBuffer.append("IntentOperationsMatcher unable to match: " | ||
| 82 | + + providedString); | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + @Override | ||
| 86 | + public boolean matches(Object object) { | ||
| 87 | + if (!(object instanceof IntentOperations)) { | ||
| 88 | + return false; | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + IntentOperations providedIntentOperations = | ||
| 92 | + (IntentOperations) object; | ||
| 93 | + providedString = providedIntentOperations.toString(); | ||
| 94 | + | ||
| 95 | + List<IntentKey> thisSubmitIntents = new LinkedList<>(); | ||
| 96 | + List<IntentId> thisWithdrawIntentIds = new LinkedList<>(); | ||
| 97 | + List<IntentKey> thisReplaceIntents = new LinkedList<>(); | ||
| 98 | + List<IntentKey> thisUpdateIntents = new LinkedList<>(); | ||
| 99 | + List<IntentKey> providedSubmitIntents = new LinkedList<>(); | ||
| 100 | + List<IntentId> providedWithdrawIntentIds = new LinkedList<>(); | ||
| 101 | + List<IntentKey> providedReplaceIntents = new LinkedList<>(); | ||
| 102 | + List<IntentKey> providedUpdateIntents = new LinkedList<>(); | ||
| 103 | + | ||
| 104 | + extractIntents(intentOperations, thisSubmitIntents, | ||
| 105 | + thisWithdrawIntentIds, thisReplaceIntents, | ||
| 106 | + thisUpdateIntents); | ||
| 107 | + extractIntents(providedIntentOperations, providedSubmitIntents, | ||
| 108 | + providedWithdrawIntentIds, providedReplaceIntents, | ||
| 109 | + providedUpdateIntents); | ||
| 110 | + | ||
| 111 | + return CollectionUtils.isEqualCollection(thisSubmitIntents, | ||
| 112 | + providedSubmitIntents) && | ||
| 113 | + CollectionUtils.isEqualCollection(thisWithdrawIntentIds, | ||
| 114 | + providedWithdrawIntentIds) && | ||
| 115 | + CollectionUtils.isEqualCollection(thisUpdateIntents, | ||
| 116 | + providedUpdateIntents) && | ||
| 117 | + CollectionUtils.isEqualCollection(thisReplaceIntents, | ||
| 118 | + providedReplaceIntents); | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + /** | ||
| 122 | + * Extracts the intents per operation type. Each intent is encapsulated | ||
| 123 | + * in IntentKey so it can be compared by excluding the Intent ID. | ||
| 124 | + * | ||
| 125 | + * @param intentOperations the container with the intent operations | ||
| 126 | + * to extract the intents from | ||
| 127 | + * @param submitIntents the SUBMIT intents | ||
| 128 | + * @param withdrawIntentIds the WITHDRAW intents IDs | ||
| 129 | + * @param replaceIntents the REPLACE intents | ||
| 130 | + * @param updateIntents the UPDATE intens | ||
| 131 | + */ | ||
| 132 | + private void extractIntents(IntentOperations intentOperations, | ||
| 133 | + List<IntentKey> submitIntents, | ||
| 134 | + List<IntentId> withdrawIntentIds, | ||
| 135 | + List<IntentKey> replaceIntents, | ||
| 136 | + List<IntentKey> updateIntents) { | ||
| 137 | + for (IntentOperation oper : intentOperations.operations()) { | ||
| 138 | + IntentId intentId; | ||
| 139 | + IntentKey intentKey; | ||
| 140 | + switch (oper.type()) { | ||
| 141 | + case SUBMIT: | ||
| 142 | + intentKey = new IntentKey(oper.intent()); | ||
| 143 | + submitIntents.add(intentKey); | ||
| 144 | + break; | ||
| 145 | + case WITHDRAW: | ||
| 146 | + intentId = oper.intentId(); | ||
| 147 | + withdrawIntentIds.add(intentId); | ||
| 148 | + break; | ||
| 149 | + case REPLACE: | ||
| 150 | + intentKey = new IntentKey(oper.intent()); | ||
| 151 | + replaceIntents.add(intentKey); | ||
| 152 | + break; | ||
| 153 | + case UPDATE: | ||
| 154 | + intentKey = new IntentKey(oper.intent()); | ||
| 155 | + updateIntents.add(intentKey); | ||
| 156 | + break; | ||
| 157 | + default: | ||
| 158 | + break; | ||
| 159 | + } | ||
| 160 | + } | ||
| 161 | + } | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | + /* | ||
| 165 | + * EasyMock matcher that matches {@link PointToPointIntent}s but | ||
| 166 | + * ignores the {@link IntentId} when matching. | ||
| 167 | + * <p/> | ||
| 168 | + * The normal intent equals method tests that the intent IDs are equal, | ||
| 169 | + * however in these tests we can't know what the intent IDs will be in | ||
| 170 | + * advance, so we can't set up expected intents with the correct IDs. Thus, | ||
| 171 | + * the solution is to use an EasyMock matcher that verifies that all the | ||
| 172 | + * value properties of the provided intent match the expected values, but | ||
| 173 | + * ignores the intent ID when testing equality. | ||
| 174 | + */ | ||
| 175 | + private static final class IdAgnosticPointToPointIntentMatcher implements | ||
| 176 | + IArgumentMatcher { | ||
| 177 | + | ||
| 178 | + private final PointToPointIntent intent; | ||
| 179 | + private String providedIntentString; | ||
| 180 | + | ||
| 181 | + /** | ||
| 182 | + * Constructor taking the expected intent to match against. | ||
| 183 | + * | ||
| 184 | + * @param intent the expected intent | ||
| 185 | + */ | ||
| 186 | + public IdAgnosticPointToPointIntentMatcher(PointToPointIntent intent) { | ||
| 187 | + this.intent = intent; | ||
| 188 | + } | ||
| 189 | + | ||
| 190 | + @Override | ||
| 191 | + public void appendTo(StringBuffer strBuffer) { | ||
| 192 | + strBuffer.append("PointToPointIntentMatcher unable to match: " | ||
| 193 | + + providedIntentString); | ||
| 194 | + } | ||
| 195 | + | ||
| 196 | + @Override | ||
| 197 | + public boolean matches(Object object) { | ||
| 198 | + if (!(object instanceof PointToPointIntent)) { | ||
| 199 | + return false; | ||
| 200 | + } | ||
| 201 | + | ||
| 202 | + PointToPointIntent providedIntent = (PointToPointIntent) object; | ||
| 203 | + providedIntentString = providedIntent.toString(); | ||
| 204 | + | ||
| 205 | + PointToPointIntent matchIntent = | ||
| 206 | + new PointToPointIntent(providedIntent.appId(), | ||
| 207 | + intent.selector(), intent.treatment(), | ||
| 208 | + intent.ingressPoint(), intent.egressPoint()); | ||
| 209 | + | ||
| 210 | + return matchIntent.equals(providedIntent); | ||
| 211 | + } | ||
| 212 | + } | ||
| 213 | +} |
-
Please register or login to post a comment