diff --git a/docs/src/docs/asciidoc/stubrunner_msg.adoc b/docs/src/docs/asciidoc/stubrunner_msg.adoc index 2a027ba005..788cba9990 100644 --- a/docs/src/docs/asciidoc/stubrunner_msg.adoc +++ b/docs/src/docs/asciidoc/stubrunner_msg.adoc @@ -50,6 +50,36 @@ include::../../../../stub-runner/stub-runner/src/main/groovy/io/codearte/accures For convenience the `StubFinder` interface extends `StubTrigger` so it's enough to use only one in your tests. +`StubTrigger` gives you the following options to trigger a message: + +===== Trigger by label + +[source,groovy] +---- +include::../../../../stub-runner/stub-runner-messaging/stub-runner-messaging-camel/src/test/groovy/io/codearte/accurest/stubrunner/messaging/camel/CamelStubRunnerSpec.groovy[tags=client_trigger,indent=0] +---- + +===== Trigger by group and artifact ids + +[source,groovy] +---- +include::../../../../stub-runner/stub-runner-messaging/stub-runner-messaging-camel/src/test/groovy/io/codearte/accurest/stubrunner/messaging/camel/CamelStubRunnerSpec.groovy[tags=trigger_group_artifact,indent=0] +---- + +===== Trigger by artifact ids + +[source,groovy] +---- +include::../../../../stub-runner/stub-runner-messaging/stub-runner-messaging-camel/src/test/groovy/io/codearte/accurest/stubrunner/messaging/camel/CamelStubRunnerSpec.groovy[tags=trigger_artifact,indent=0] +---- + +===== Trigger all messages + +[source,groovy] +---- +include::../../../../stub-runner/stub-runner-messaging/stub-runner-messaging-camel/src/test/groovy/io/codearte/accurest/stubrunner/messaging/camel/CamelStubRunnerSpec.groovy[tags=trigger_all,indent=0] +---- + include::../../../../stub-runner/stub-runner-messaging/stub-runner-messaging-camel/README.adoc[] include::../../../../stub-runner/stub-runner-messaging/stub-runner-messaging-integration/README.adoc[] diff --git a/stub-runner/stub-runner-junit/src/main/groovy/io/codearte/accurest/stubrunner/junit/AccurestRule.java b/stub-runner/stub-runner-junit/src/main/groovy/io/codearte/accurest/stubrunner/junit/AccurestRule.java index c4ecac01bf..85a8768e61 100644 --- a/stub-runner/stub-runner-junit/src/main/groovy/io/codearte/accurest/stubrunner/junit/AccurestRule.java +++ b/stub-runner/stub-runner-junit/src/main/groovy/io/codearte/accurest/stubrunner/junit/AccurestRule.java @@ -171,16 +171,16 @@ public class AccurestRule implements TestRule, StubFinder { @Override public void trigger(String ivyNotation, String labelName) { - throw new UnsupportedOperationException("Feature not yet supported"); + stubFinder.trigger(ivyNotation, labelName); } @Override public void trigger(String labelName) { - throw new UnsupportedOperationException("Feature not yet supported"); + stubFinder.trigger(labelName); } @Override public void trigger() { - throw new UnsupportedOperationException("Feature not yet supported"); + stubFinder.trigger(); } } diff --git a/stub-runner/stub-runner-messaging/stub-runner-messaging-camel/README.adoc b/stub-runner/stub-runner-messaging/stub-runner-messaging-camel/README.adoc index 7fdfc0011d..fb579dc507 100644 --- a/stub-runner/stub-runner-messaging/stub-runner-messaging-camel/README.adoc +++ b/stub-runner/stub-runner-messaging/stub-runner-messaging-camel/README.adoc @@ -113,34 +113,11 @@ And the received message would pass the following assertions include::src/test/groovy/io/codearte/accurest/stubrunner/messaging/camel/CamelStubRunnerSpec.groovy[tags=client_receive_message,indent=0] ---- -===== Custom triggers +===== Scenario 3 (input with no output) -`StubTrigger` gives you the following options to trigger a message: - -===== Trigger by label +Since the route is set for you it's enough to just send a message to the `{output_name}` destination. [source,groovy] ---- -include::src/test/groovy/io/codearte/accurest/stubrunner/messaging/camel/CamelStubRunnerSpec.groovy[tags=client_trigger,indent=0] +include::src/test/groovy/io/codearte/accurest/stubrunner/messaging/camel/CamelStubRunnerSpec.groovy[tags=trigger_no_output,indent=0] ---- - -===== Trigger by group and artifact ids - -[source,groovy] ----- -include::src/test/groovy/io/codearte/accurest/stubrunner/messaging/camel/CamelStubRunnerSpec.groovy[tags=trigger_group_artifact,indent=0] ----- - -===== Trigger by artifact ids - -[source,groovy] ----- -include::src/test/groovy/io/codearte/accurest/stubrunner/messaging/camel/CamelStubRunnerSpec.groovy[tags=trigger_artifact,indent=0] ----- - -===== Trigger all messages - -[source,groovy] ----- -include::src/test/groovy/io/codearte/accurest/stubrunner/messaging/camel/CamelStubRunnerSpec.groovy[tags=trigger_all,indent=0] ----- \ No newline at end of file diff --git a/stub-runner/stub-runner-messaging/stub-runner-messaging-camel/src/test/groovy/io/codearte/accurest/stubrunner/messaging/camel/CamelStubRunnerSpec.groovy b/stub-runner/stub-runner-messaging/stub-runner-messaging-camel/src/test/groovy/io/codearte/accurest/stubrunner/messaging/camel/CamelStubRunnerSpec.groovy index ef0aae52db..dee3c93b00 100644 --- a/stub-runner/stub-runner-messaging/stub-runner-messaging-camel/src/test/groovy/io/codearte/accurest/stubrunner/messaging/camel/CamelStubRunnerSpec.groovy +++ b/stub-runner/stub-runner-messaging/stub-runner-messaging-camel/src/test/groovy/io/codearte/accurest/stubrunner/messaging/camel/CamelStubRunnerSpec.groovy @@ -119,6 +119,24 @@ class CamelStubRunnerSpec extends Specification { receivedMessage.in.headers.get('BOOK-NAME') == 'foo' } + def 'should trigger a label with no output message'() { + when: + // tag::trigger_no_output[] + camelContext.createProducerTemplate().sendBodyAndHeaders('jms:delete', new BookReturned('foo'), [sample: 'header']) + // end::trigger_no_output[] + then: + noExceptionThrown() + } + + def 'should not trigger a message that does not match input'() { + when: + camelContext.createProducerTemplate().sendBodyAndHeaders('jms:input', new BookReturned('notmatching'), [wrong: 'header_value']) + then: + Exchange receivedMessage = camelContext.createConsumerTemplate().receive('jms:output', 100) + and: + receivedMessage == null + } + private boolean assertThatBodyContainsBookNameFoo(Object payload) { String objectAsString = payload instanceof String ? payload : JsonOutput.toJson(payload) @@ -126,6 +144,11 @@ class CamelStubRunnerSpec extends Specification { return json.bookName == 'foo' } + def setup() { + // ensure that message were taken from the queue + camelContext.createConsumerTemplate().receive('jms:output', 100) + } + @Bean ActiveMQComponent activeMQComponent(@Value('${activemq.url:vm://localhost?broker.persistent=false}') String url) { return new ActiveMQComponent(brokerURL: url) @@ -172,4 +195,21 @@ class CamelStubRunnerSpec extends Specification { } } // end::sample_dsl_2[] + + GroovyDsl dsl3 = + // tag::sample_dsl_3[] + io.codearte.accurest.dsl.GroovyDsl.make { + label 'delete_book' + input { + messageFrom('jms:delete') + messageBody([ + bookName: 'foo' + ]) + messageHeaders { + header('sample', 'header') + } + assertThat('bookWasDeleted()') + } + } + // end::sample_dsl_3[] } diff --git a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/MessageNotMatchingException.groovy b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/MessageNotMatchingException.groovy new file mode 100644 index 0000000000..62368d75bf --- /dev/null +++ b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/MessageNotMatchingException.groovy @@ -0,0 +1,12 @@ +package io.codearte.accurest.stubrunner + +import groovy.transform.InheritConstructors + +/** + * Exception thrown when message is not matched + * + * @author Marcin Grzejszczak + */ +@InheritConstructors +class MessageNotMatchingException extends RuntimeException { +}