This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch mock in repository https://gitbox.apache.org/repos/asf/camel.git
commit 1f05f8389b8e0f94386bf50bff3b05f2cf16ed23 Author: Claus Ibsen <[email protected]> AuthorDate: Mon Apr 15 17:08:09 2019 +0200 Move mock component out of camel-core. Work in progress. --- MIGRATION.md | 1 + .../org/apache/camel/builder/NotifyBuilder.java | 222 +-------------------- .../apache/camel/builder/NotifyBuilderTest.java | 155 -------------- 3 files changed, 5 insertions(+), 373 deletions(-) diff --git a/MIGRATION.md b/MIGRATION.md index 578ee48..f60ae99 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -42,6 +42,7 @@ We have also modularized many of the core components and moved them out of `came - camel-file - camel-language - camel-log +- camel-mock - camel-properties - camel-ref - camel-rest 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 6ebe1a8..1a6153c 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 @@ -31,7 +31,6 @@ import org.apache.camel.Endpoint; import org.apache.camel.Exchange; import org.apache.camel.Expression; import org.apache.camel.Predicate; -import org.apache.camel.Producer; import org.apache.camel.RuntimeCamelException; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.spi.CamelEvent; @@ -799,204 +798,6 @@ public class NotifyBuilder { } /** - * Sets a condition when the provided mock 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 for setting fine grained expectations - * and then use that together with this builder. The mock provided does <b>NOT</b> - * have to already exist in the route. You can just create a new pseudo mock - * and this builder will send the done {@link Exchange} to it. So its like - * adding the mock to the end of your route(s). - * - * @param mock the mock - * @return the builder - */ - public NotifyBuilder whenDoneSatisfied(final MockEndpoint mock) { - return doWhenSatisfied(mock, false); - } - - /** - * Sets a condition when the provided mock 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 for setting fine grained expectations - * and then use that together with this builder. The mock provided does <b>NOT</b> - * have to already exist in the route. You can just create a new pseudo mock - * and this builder will send the done {@link Exchange} to it. So its like - * adding the mock to the end of your route(s). - * - * @param mock the mock - * @return the builder - */ - public NotifyBuilder whenReceivedSatisfied(final MockEndpoint mock) { - return doWhenSatisfied(mock, true); - } - - private NotifyBuilder doWhenSatisfied(final MockEndpoint mock, final boolean received) { - stack.add(new EventPredicateSupport() { - private Producer producer; - - @Override - public boolean onExchangeCreated(Exchange exchange) { - if (received) { - sendToMock(exchange); - } - return true; - } - - @Override - public boolean onExchangeFailed(Exchange exchange) { - if (!received) { - sendToMock(exchange); - } - return true; - } - - @Override - public boolean onExchangeCompleted(Exchange exchange) { - if (!received) { - sendToMock(exchange); - } - return true; - } - - private void sendToMock(Exchange exchange) { - // send the exchange when its completed to the mock - try { - if (producer == null) { - producer = mock.createProducer(); - } - producer.process(exchange); - } catch (Exception e) { - throw RuntimeCamelException.wrapRuntimeCamelException(e); - } - } - - public boolean matches() { - try { - return mock.await(0, TimeUnit.SECONDS); - } catch (InterruptedException e) { - throw RuntimeCamelException.wrapRuntimeCamelException(e); - } - } - - @Override - public void reset() { - mock.reset(); - } - - @Override - public String toString() { - if (received) { - return "whenReceivedSatisfied(" + mock + ")"; - } else { - return "whenDoneSatisfied(" + mock + ")"; - } - } - }); - return this; - } - - /** - * Sets a condition when the provided mock 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 for setting fine grained expectations - * and then use that together with this builder. The mock provided does <b>NOT</b> - * have to already exist in the route. You can just create a new pseudo mock - * and this builder will send the done {@link Exchange} to it. So its like - * adding the mock to the end of your route(s). - * - * @param mock the mock - * @return the builder - */ - public NotifyBuilder whenReceivedNotSatisfied(final MockEndpoint mock) { - return doWhenNotSatisfied(mock, true); - } - - /** - * Sets a condition when the provided mock 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 for setting fine grained expectations - * and then use that together with this builder. The mock provided does <b>NOT</b> - * have to already exist in the route. You can just create a new pseudo mock - * and this builder will send the done {@link Exchange} to it. So its like - * adding the mock to the end of your route(s). - * - * @param mock the mock - * @return the builder - */ - public NotifyBuilder whenDoneNotSatisfied(final MockEndpoint mock) { - return doWhenNotSatisfied(mock, false); - } - - private NotifyBuilder doWhenNotSatisfied(final MockEndpoint mock, final boolean received) { - stack.add(new EventPredicateSupport() { - private Producer producer; - - @Override - public boolean onExchangeCreated(Exchange exchange) { - if (received) { - sendToMock(exchange); - } - return true; - } - - @Override - public boolean onExchangeFailed(Exchange exchange) { - if (!received) { - sendToMock(exchange); - } - return true; - } - - @Override - public boolean onExchangeCompleted(Exchange exchange) { - if (!received) { - sendToMock(exchange); - } - return true; - } - - private void sendToMock(Exchange exchange) { - // send the exchange when its completed to the mock - try { - if (producer == null) { - producer = mock.createProducer(); - } - producer.process(exchange); - } catch (Exception e) { - throw RuntimeCamelException.wrapRuntimeCamelException(e); - } - } - - public boolean matches() { - try { - return !mock.await(0, TimeUnit.SECONDS); - } catch (InterruptedException e) { - throw RuntimeCamelException.wrapRuntimeCamelException(e); - } - } - - @Override - public void reset() { - mock.reset(); - } - - @Override - public String toString() { - if (received) { - return "whenReceivedNotSatisfied(" + mock + ")"; - } else { - return "whenDoneNotSatisfied(" + mock + ")"; - } - } - }); - return this; - } - - /** * Sets a condition that the bodies is expected to be <b>received</b> in the order as well. * <p/> * This condition will discard any additional messages. If you need a more strict condition @@ -1227,34 +1028,19 @@ public class NotifyBuilder { * This operation will wait until the match is <tt>true</tt> or otherwise a timeout occur * which means <tt>false</tt> will be returned. * <p/> - * The timeout value is by default 10 seconds. But it will use the highest <i>maximum result wait time</i> - * from the configured mocks, if such a value has been configured. - * <p/> - * This method is convenient to use in unit tests to have it adhere and wait - * as long as the mock endpoints. + * The timeout value is by default 10 seconds. * * @return <tt>true</tt> if matching, <tt>false</tt> otherwise due to timeout + * @deprecated use {@link #matches(long, TimeUnit)} instead */ + @Deprecated public boolean matchesMockWaitTime() { if (!created) { throw new IllegalStateException("NotifyBuilder has not been created. Invoke the create() method before matching."); } - long timeout = 0; - for (Endpoint endpoint : context.getEndpoints()) { - if (endpoint instanceof MockEndpoint) { - long waitTime = ((MockEndpoint) endpoint).getResultWaitTime(); - if (waitTime > 0) { - timeout = Math.max(timeout, waitTime); - } - } - } // use 10 sec as default - if (timeout == 0) { - timeout = 10000; - } - - return matches(timeout, TimeUnit.MILLISECONDS); + return matches(10000, TimeUnit.MILLISECONDS); } /** 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 87c080d..b3fefe3 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,7 +18,6 @@ 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 { @@ -747,160 +746,6 @@ 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)
