From 3e76b0ab23690788092cdd086105bc8356615eee Mon Sep 17 00:00:00 2001 From: Rajani-K Date: Thu, 27 Jul 2017 00:50:15 +0530 Subject: [PATCH] CAMEL-11604: Added new method expectedPropertyValuesReceivedInAnyOrder --- .../camel/component/mock/MockEndpoint.java | 53 +++++++++++++++++-- .../component/mock/MockEndpointTest.java | 43 +++++++++++++++ 2 files changed, 93 insertions(+), 3 deletions(-) diff --git a/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java b/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java index ff5b19e0beda3..b147adb963b95 100644 --- a/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java +++ b/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java @@ -514,9 +514,6 @@ public void expectedMinimumMessageCount(int expectedCount) { * You can set multiple expectations for different header names. * If you set a value of null that means we accept either the header is absent, or its value is null *

- * Important: The number of values must match the expected number of messages, so if you expect 3 messages, then - * there must be 3 values. - *

* Important: This overrides any previous set value using {@link #expectedMessageCount(int)} */ public void expectedHeaderReceived(final String name, final Object value) { @@ -637,6 +634,56 @@ public void run() { }); } + /** + * Adds an expectation that the given property values are received by this + * endpoint in any order. + *

+ * Important: The number of values must match the expected number of messages, so if you expect 3 messages, then + * there must be 3 values. + *

+ * Important: This overrides any previous set value using {@link #expectedMessageCount(int)} + */ + public void expectedPropertyValuesReceivedInAnyOrder(final String name, final List values) { + expectedMessageCount(values.size()); + + expects(new Runnable() { + public void run() { + // these are the expected values to find + final Set actualPropertyValues = new CopyOnWriteArraySet(values); + + for (int i = 0; i < getReceivedExchanges().size(); i++) { + Exchange exchange = getReceivedExchange(i); + + Object actualValue = exchange.getProperty(name); + for (Object expectedValue : actualPropertyValues) { + actualValue = extractActualValue(exchange, actualValue, expectedValue); + // remove any found values + actualPropertyValues.remove(actualValue); + } + } + + // should be empty, as we should find all the values + assertTrue("Expected " + values.size() + " properties with key[" + name + "], received " + (values.size() - actualPropertyValues.size()) + + " properties. Expected property values: " + actualPropertyValues, actualPropertyValues.isEmpty()); + } + }); + } + + /** + * Adds an expectation that the given property values are received by this + * endpoint in any order + *

+ * Important: The number of values must match the expected number of messages, so if you expect 3 messages, then + * there must be 3 values. + *

+ * Important: This overrides any previous set value using {@link #expectedMessageCount(int)} + */ + public void expectedPropertyValuesReceivedInAnyOrder(String name, Object... values) { + List valueList = new ArrayList(); + valueList.addAll(Arrays.asList(values)); + expectedPropertyValuesReceivedInAnyOrder(name, valueList); + } + /** * Adds an expectation that the given body values are received by this * endpoint in the specified order diff --git a/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java b/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java index e6074f6aa5935..459df765a60d7 100644 --- a/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java +++ b/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java @@ -125,6 +125,49 @@ public void testExpectsHeadersInAnyOrderFail() throws Exception { } } + public void testExpectsPropertiesInAnyOrder() throws Exception { + MockEndpoint resultEndpoint = getMockEndpoint("mock:result"); + resultEndpoint.expectedPropertyValuesReceivedInAnyOrder("foo", 123, 456); + + template.send("direct:a", new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.setProperty("foo", 456); + } + }); + + template.send("direct:a", new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.setProperty("foo", 123); + } + }); + + resultEndpoint.assertIsSatisfied(); + } + + public void testExpectsPropertiesInAnyOrderFail() throws Exception { + MockEndpoint resultEndpoint = getMockEndpoint("mock:result"); + resultEndpoint.expectedPropertyValuesReceivedInAnyOrder("foo", 123, 456); + + template.send("direct:a", new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.setProperty("foo", 123); + } + }); + + template.send("direct:a", new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.setProperty("foo", 789); + } + }); + + try { + resultEndpoint.assertIsNotSatisfied(); + fail("Should fail"); + } catch (AssertionError e) { + assertEquals("mock://result Expected 2 properties with key[bar], received 1 properties. Expected property values: [456]", e.getMessage()); + } + } + public void testNoDuplicateMessagesPass() throws Exception { MockEndpoint resultEndpoint = getMockEndpoint("mock:result"); resultEndpoint.expectsNoDuplicates(header("counter"));