Repository: camel Updated Branches: refs/heads/master cf9cd31e7 -> 3d80e6512
CAMEL-11604: Added new method expectedPropertyValuesReceivedInAnyOrder Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c896f443 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c896f443 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c896f443 Branch: refs/heads/master Commit: c896f44305e86e20ceb3b8d5929bb371cd2304d0 Parents: cf9cd31 Author: Rajani-K <rajani....@gmail.com> Authored: Thu Jul 27 00:50:15 2017 +0530 Committer: Claus Ibsen <davscl...@apache.org> Committed: Fri Jul 28 16:53:30 2017 +0200 ---------------------------------------------------------------------- .../camel/component/mock/MockEndpoint.java | 53 ++++++++++++++++++-- .../camel/component/mock/MockEndpointTest.java | 43 ++++++++++++++++ 2 files changed, 93 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/c896f443/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java ---------------------------------------------------------------------- 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 ff5b19e..b147adb 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 class MockEndpoint extends DefaultEndpoint implements BrowsableEndpoint { * You can set multiple expectations for different header names. * If you set a value of <tt>null</tt> that means we accept either the header is absent, or its value is <tt>null</tt> * <p/> - * <b>Important:</b> The number of values must match the expected number of messages, so if you expect 3 messages, then - * there must be 3 values. - * <p/> * <b>Important:</b> This overrides any previous set value using {@link #expectedMessageCount(int)} */ public void expectedHeaderReceived(final String name, final Object value) { @@ -638,6 +635,56 @@ public class MockEndpoint extends DefaultEndpoint implements BrowsableEndpoint { } /** + * Adds an expectation that the given property values are received by this + * endpoint in any order. + * <p/> + * <b>Important:</b> The number of values must match the expected number of messages, so if you expect 3 messages, then + * there must be 3 values. + * <p/> + * <b>Important:</b> 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<Object> actualPropertyValues = new CopyOnWriteArraySet<Object>(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 + * <p/> + * <b>Important:</b> The number of values must match the expected number of messages, so if you expect 3 messages, then + * there must be 3 values. + * <p/> + * <b>Important:</b> This overrides any previous set value using {@link #expectedMessageCount(int)} + */ + public void expectedPropertyValuesReceivedInAnyOrder(String name, Object... values) { + List<Object> valueList = new ArrayList<Object>(); + 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 * <p/> http://git-wip-us.apache.org/repos/asf/camel/blob/c896f443/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java ---------------------------------------------------------------------- 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 e6074f6..459df76 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 class MockEndpointTest extends ContextTestSupport { } } + 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"));