This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit ef4f2c6768caa5b1424b9bdd4e6dff176bbda28c Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sat Jul 6 10:13:05 2019 +0200 CAMEL-13730: Add NotifyBuilderMatcher in SPI so we can have mock endpoints back with notify builder as predicates. --- MIGRATION.md | 2 +- .../org/apache/camel/builder/NotifyBuilder.java | 9 +- .../apache/camel/builder/NotifyBuilderTest.java | 155 +++++++++++++++++++++ 3 files changed, 160 insertions(+), 6 deletions(-) diff --git a/MIGRATION.md b/MIGRATION.md index a13dfd2..24d68ef 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -132,7 +132,7 @@ The `camel-script` component has been removed and there is no support for javax. ### Mock component -The `mock` component has been moved out of `camel-core` and as part of this work, we had to remove a number of methods on its _assertion clause builder_ that were seldom in use. Also we had to remove a few methods on `NotifyBuilder` that were using the `mock` component. +The `mock` component has been moved out of `camel-core` and as part of this work, we had to remove a number of methods on its _assertion clause builder_ that were seldom in use. ### ActiveMQ diff --git a/core/camel-core/src/main/java/org/apache/camel/builder/NotifyBuilder.java b/core/camel-core/src/main/java/org/apache/camel/builder/NotifyBuilder.java index c569a79..ee329dd 100644 --- a/core/camel-core/src/main/java/org/apache/camel/builder/NotifyBuilder.java +++ b/core/camel-core/src/main/java/org/apache/camel/builder/NotifyBuilder.java @@ -976,7 +976,7 @@ public class NotifyBuilder { } /** - * Sets a condition when the provided mock is satisfied based on {@link Exchange} + * Sets a condition when the provided matcher (such as mock endpoint) is satisfied based on {@link Exchange} * being sent to it when they are <b>done</b>. * <p/> * The idea is that you can use mock endpoints (or other matchers) for setting fine grained expectations @@ -993,7 +993,7 @@ public class NotifyBuilder { } /** - * Sets a condition when the provided mock endpoint (or other matchers) is satisfied based on {@link Exchange} + * Sets a condition when the provided matcher (such as mock endpoint) is satisfied based on {@link Exchange} * being sent to it when they are <b>received</b>. * <p/> * The idea is that you can use mock endpoints (or other matchers) for setting fine grained expectations @@ -1058,7 +1058,7 @@ public class NotifyBuilder { } /** - * Sets a condition when the provided mock (or other matchers) is <b>not</b> satisfied based on {@link Exchange} + * Sets a condition when the provided matcher (such as mock endpoint) is <b>not</b> satisfied based on {@link Exchange} * being sent to it when they are <b>received</b>. * <p/> * The idea is that you can use mock endpoints (or other matchers) for setting fine grained expectations @@ -1070,13 +1070,12 @@ public class NotifyBuilder { * @param matcher the matcher such as mock endpoint * @return the builder */ - @Deprecated public NotifyBuilder whenReceivedNotSatisfied(final NotifyBuilderMatcher matcher) { return doWhenNotSatisfied(matcher, true); } /** - * Sets a condition when the provided mock (or other matchers) is <b>not</b> satisfied based on {@link Exchange} + * Sets a condition when the provided matcher (such as mock endpoint) is <b>not</b> satisfied based on {@link Exchange} * being sent to it when they are <b>done</b>. * <p/> * The idea is that you can use mock endpoints (or other matchers) for setting fine grained expectations diff --git a/core/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderTest.java b/core/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderTest.java index b3fefe3..87c080d 100644 --- a/core/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderTest.java @@ -18,6 +18,7 @@ package org.apache.camel.builder; import org.apache.camel.CamelExecutionException; import org.apache.camel.ContextTestSupport; +import org.apache.camel.component.mock.MockEndpoint; import org.junit.Test; public class NotifyBuilderTest extends ContextTestSupport { @@ -746,6 +747,160 @@ public class NotifyBuilderTest extends ContextTestSupport { } @Test + public void testWhenReceivedSatisfied() throws Exception { + // lets use a mock to set the expressions as it got many great assertions for that + // notice we use mock:assert which does NOT exist in the route, its just a pseudo name + MockEndpoint mock = getMockEndpoint("mock:assert"); + mock.expectedBodiesReceivedInAnyOrder("Hello World", "Bye World", "Hi World"); + + NotifyBuilder notify = new NotifyBuilder(context) + .from("direct:foo").whenDoneSatisfied(mock) + .create(); + + assertEquals(false, notify.matches()); + + template.sendBody("direct:foo", "Bye World"); + assertEquals(false, notify.matches()); + + template.sendBody("direct:foo", "Hello World"); + assertEquals(false, notify.matches()); + + // the notify is based on direct:foo so sending to bar should not trigger match + template.sendBody("direct:bar", "Hi World"); + assertEquals(false, notify.matches()); + + template.sendBody("direct:foo", "Hi World"); + assertEquals(true, notify.matches()); + } + + @Test + public void testWhenReceivedNotSatisfied() throws Exception { + // lets use a mock to set the expressions as it got many great assertions for that + // notice we use mock:assert which does NOT exist in the route, its just a pseudo name + MockEndpoint mock = getMockEndpoint("mock:assert"); + mock.expectedMessageCount(2); + mock.message(1).body().contains("Camel"); + + NotifyBuilder notify = new NotifyBuilder(context) + .from("direct:foo").whenReceivedNotSatisfied(mock) + .create(); + + // is always false to start with + assertEquals(false, notify.matches()); + + template.sendBody("direct:foo", "Bye World"); + assertEquals(true, notify.matches()); + + template.sendBody("direct:foo", "Hello Camel"); + assertEquals(false, notify.matches()); + } + + @Test + public void testWhenNotSatisfiedUsingSatisfied() throws Exception { + // lets use a mock to set the expressions as it got many great assertions for that + // notice we use mock:assert which does NOT exist in the route, its just a pseudo name + MockEndpoint mock = getMockEndpoint("mock:assert"); + mock.expectedMessageCount(2); + mock.message(1).body().contains("Camel"); + + NotifyBuilder notify = new NotifyBuilder(context) + .from("direct:foo").whenReceivedSatisfied(mock) + .create(); + + assertEquals(false, notify.matches()); + + template.sendBody("direct:foo", "Bye World"); + assertEquals(false, notify.matches()); + + template.sendBody("direct:foo", "Hello Camel"); + assertEquals(true, notify.matches()); + } + + @Test + public void testComplexOrCamel() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:assert"); + mock.expectedBodiesReceivedInAnyOrder("Hello World", "Bye World", "Hi World"); + + NotifyBuilder notify = new NotifyBuilder(context) + .from("direct:foo").whenReceivedSatisfied(mock) + .and().from("direct:bar").whenExactlyDone(5).whenAnyReceivedMatches(body().contains("Camel")) + .create(); + + assertEquals(false, notify.matches()); + + template.sendBody("direct:foo", "Bye World"); + assertEquals(false, notify.matches()); + + template.sendBody("direct:foo", "Hello World"); + assertEquals(false, notify.matches()); + + // the notify is based on direct:foo so sending to bar should not trigger match + template.sendBody("direct:bar", "Hi World"); + assertEquals(false, notify.matches()); + + template.sendBody("direct:foo", "Hi World"); + assertEquals(false, notify.matches()); + + template.sendBody("direct:bar", "Hi Camel"); + assertEquals(false, notify.matches()); + + template.sendBody("direct:bar", "A"); + template.sendBody("direct:bar", "B"); + template.sendBody("direct:bar", "C"); + assertEquals(true, notify.matches()); + } + + @Test + public void testWhenDoneSatisfied() throws Exception { + // lets use a mock to set the expressions as it got many great assertions for that + // notice we use mock:assert which does NOT exist in the route, its just a pseudo name + MockEndpoint mock = getMockEndpoint("mock:assert"); + mock.expectedBodiesReceived("Bye World", "Bye Camel"); + + NotifyBuilder notify = new NotifyBuilder(context) + .whenDoneSatisfied(mock) + .create(); + + // is always false to start with + assertEquals(false, notify.matches()); + + template.requestBody("direct:cake", "World"); + assertEquals(false, notify.matches()); + + template.requestBody("direct:cake", "Camel"); + assertEquals(true, notify.matches()); + + template.requestBody("direct:cake", "Damn"); + // will still be true as the mock has been completed + assertEquals(true, notify.matches()); + } + + @Test + public void testWhenDoneNotSatisfied() throws Exception { + // lets use a mock to set the expressions as it got many great assertions for that + // notice we use mock:assert which does NOT exist in the route, its just a pseudo name + MockEndpoint mock = getMockEndpoint("mock:assert"); + mock.expectedBodiesReceived("Bye World", "Bye Camel"); + + NotifyBuilder notify = new NotifyBuilder(context) + .whenDoneNotSatisfied(mock) + .create(); + + // is always false to start with + assertEquals(false, notify.matches()); + + template.requestBody("direct:cake", "World"); + assertEquals(true, notify.matches()); + + template.requestBody("direct:cake", "Camel"); + assertEquals(false, notify.matches()); + + template.requestBody("direct:cake", "Damn"); + // will still be false as the mock has been completed + assertEquals(false, notify.matches()); + } + + @Test public void testReset() throws Exception { NotifyBuilder notify = new NotifyBuilder(context) .whenExactlyDone(1)