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 85a8768e61..52178ecc01 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 @@ -1,18 +1,5 @@ package io.codearte.accurest.stubrunner.junit; -import java.net.URL; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.junit.rules.TestRule; -import org.junit.runner.Description; -import org.junit.runners.model.Statement; - import io.codearte.accurest.dsl.GroovyDsl; import io.codearte.accurest.stubrunner.BatchStubRunner; import io.codearte.accurest.stubrunner.BatchStubRunnerFactory; @@ -22,6 +9,18 @@ import io.codearte.accurest.stubrunner.StubFinder; import io.codearte.accurest.stubrunner.StubRunnerOptions; import io.codearte.accurest.stubrunner.util.StringUtils; import io.codearte.accurest.stubrunner.util.StubsParser; +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +import java.net.URL; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; /** * JUnit class rule that allows you to download the provided stubs. @@ -170,17 +169,17 @@ public class AccurestRule implements TestRule, StubFinder { } @Override - public void trigger(String ivyNotation, String labelName) { - stubFinder.trigger(ivyNotation, labelName); + public boolean trigger(String ivyNotation, String labelName) { + return stubFinder.trigger(ivyNotation, labelName); } @Override - public void trigger(String labelName) { - stubFinder.trigger(labelName); + public boolean trigger(String labelName) { + return stubFinder.trigger(labelName); } @Override - public void trigger() { - stubFinder.trigger(); + public boolean trigger() { + return stubFinder.trigger(); } } 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 fecb9fa70a..28e8053387 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 @@ -93,22 +93,18 @@ class CamelStubRunnerSpec extends Specification { receivedMessage.in.headers.get('BOOK-NAME') == 'foo' } - def 'should not run any wrong trigger when missing label is passed'() { + def 'should throw an exception when missing label is passed'() { when: stubFinder.trigger('missing label') then: - Exchange receivedMessage = camelContext.createConsumerTemplate().receive('jms:output', 100) - and: - receivedMessage == null + thrown(IllegalArgumentException) } - def 'should not run any wrong trigger when missing label and artifactid is passed'() { + def 'should throw an exception when missing label and artifactid is passed'() { when: stubFinder.trigger('some:service', 'return_book_1') then: - Exchange receivedMessage = camelContext.createConsumerTemplate().receive('jms:output', 100) - and: - receivedMessage == null + thrown(IllegalArgumentException) } def 'should trigger messages by running all triggers'() { diff --git a/stub-runner/stub-runner-messaging/stub-runner-messaging-integration/src/test/groovy/io/codearte/accurest/stubrunner/messaging/integration/IntegrationStubRunnerSpec.groovy b/stub-runner/stub-runner-messaging/stub-runner-messaging-integration/src/test/groovy/io/codearte/accurest/stubrunner/messaging/integration/IntegrationStubRunnerSpec.groovy index 8f071f5bb1..6c69f17cc4 100644 --- a/stub-runner/stub-runner-messaging/stub-runner-messaging-integration/src/test/groovy/io/codearte/accurest/stubrunner/messaging/integration/IntegrationStubRunnerSpec.groovy +++ b/stub-runner/stub-runner-messaging/stub-runner-messaging-integration/src/test/groovy/io/codearte/accurest/stubrunner/messaging/integration/IntegrationStubRunnerSpec.groovy @@ -6,7 +6,6 @@ import io.codearte.accurest.dsl.GroovyDsl import io.codearte.accurest.messaging.AccurestMessage import io.codearte.accurest.messaging.AccurestMessaging import io.codearte.accurest.stubrunner.StubFinder -import org.springframework.beans.factory.BeanNotOfRequiredTypeException import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.autoconfigure.EnableAutoConfiguration import org.springframework.boot.test.SpringApplicationContextLoader @@ -95,24 +94,18 @@ class IntegrationStubRunnerSpec extends Specification { receivedMessage.headers.get('BOOK-NAME') == 'foo' } - def 'should not run any wrong trigger when missing label is passed'() { - given: - stubFinder.trigger('missing label') + def 'should throw exception when missing label is passed'() { when: - messaging.receiveMessage('output', 100, TimeUnit.MILLISECONDS) + stubFinder.trigger('missing label') then: - Exception e = thrown(RuntimeException) - e.cause.class == BeanNotOfRequiredTypeException + thrown(IllegalArgumentException) } - def 'should not run any wrong trigger when missing label and artifactid is passed'() { - given: - stubFinder.trigger('some:service', 'return_book_1') + def 'should throw exception when missing label and artifactid is passed'() { when: - messaging.receiveMessage('output', 100, TimeUnit.MILLISECONDS) + stubFinder.trigger('some:service', 'return_book_1') then: - Exception e = thrown(RuntimeException) - e.cause.class == BeanNotOfRequiredTypeException + thrown(IllegalArgumentException) } def 'should trigger messages by running all triggers'() { diff --git a/stub-runner/stub-runner-messaging/stub-runner-messaging-stream/src/test/groovy/io/codearte/accurest/stubrunner/messaging/stream/StreamStubRunnerSpec.groovy b/stub-runner/stub-runner-messaging/stub-runner-messaging-stream/src/test/groovy/io/codearte/accurest/stubrunner/messaging/stream/StreamStubRunnerSpec.groovy index 72177a855e..1090f1c5ea 100644 --- a/stub-runner/stub-runner-messaging/stub-runner-messaging-stream/src/test/groovy/io/codearte/accurest/stubrunner/messaging/stream/StreamStubRunnerSpec.groovy +++ b/stub-runner/stub-runner-messaging/stub-runner-messaging-stream/src/test/groovy/io/codearte/accurest/stubrunner/messaging/stream/StreamStubRunnerSpec.groovy @@ -92,22 +92,18 @@ class StreamStubRunnerSpec extends Specification { receivedMessage.headers.get('BOOK-NAME') == 'foo' } - def 'should not run any wrong trigger when missing label is passed'() { - given: - stubFinder.trigger('missing label') + def 'should throw exception when missing label is passed'() { when: - AccurestMessage message = messaging.receiveMessage('output', 100, TimeUnit.MILLISECONDS) + stubFinder.trigger('missing label') then: - message == null + thrown(IllegalArgumentException) } - def 'should not run any wrong trigger when missing label and artifactid is passed'() { - given: - stubFinder.trigger('some:service', 'return_book_1') + def 'should throw exception when missing label and artifactid is passed'() { when: - AccurestMessage message = messaging.receiveMessage('output', 100, TimeUnit.MILLISECONDS) + stubFinder.trigger('some:service', 'return_book_1') then: - message == null + thrown(IllegalArgumentException) } def 'should trigger messages by running all triggers'() { diff --git a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/BatchStubRunner.groovy b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/BatchStubRunner.groovy index 7aff5d3a3d..16ef82b2fe 100644 --- a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/BatchStubRunner.groovy +++ b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/BatchStubRunner.groovy @@ -57,18 +57,55 @@ class BatchStubRunner implements StubRunning { } @Override - void trigger(String ivyNotation, String labelName) { - stubRunners.each { it.trigger(ivyNotation, labelName) } + boolean trigger(String ivyNotation, String labelName) { + boolean triggered = stubRunners.inject(false) { boolean acc, StubRunner stubRunner -> + boolean success = stubRunner.trigger(ivyNotation, labelName) + if (acc) { + return true + } + return success + } + if (!triggered) { + throw new IllegalArgumentException("No label with name [$labelName] for " + + "dependency [$ivyNotation] was found. Here you have the list of dependencies " + + "and their labels [${ivyToLabels()}") + } + return triggered + } + + private String ivyToLabels() { + return (getAccurestContracts().collectEntries { + [(it.key.toColonSeparatedDependencyNotation()) : it.value.collect { it.label }] + } as Map>).entrySet().collect { + "Dependency [${it.key}] has labels ${it.value}]" + }.join('\n') } @Override - void trigger(String labelName) { - stubRunners.each { it.trigger(labelName) } + boolean trigger(String labelName) { + boolean triggered = stubRunners.inject(false) { boolean acc, StubRunner stubRunner -> + boolean success = stubRunner.trigger(labelName) + if (acc) { + return true + } + return success + } + if (!triggered) { + throw new IllegalArgumentException("No label with name [$labelName] was found. " + + "Here you have the list of dependencies and their labels [${ivyToLabels()}") + } + return triggered } @Override - void trigger() { - stubRunners.each { it.trigger() } + boolean trigger() { + return stubRunners.inject(false) { boolean acc, StubRunner stubRunner -> + boolean success = stubRunner.trigger() + if (acc) { + return true + } + return success + } } @Override diff --git a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/StubRunner.groovy b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/StubRunner.groovy index 69d4940c4f..91a5174775 100644 --- a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/StubRunner.groovy +++ b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/StubRunner.groovy @@ -75,18 +75,18 @@ class StubRunner implements StubRunning { } @Override - void trigger(String ivyNotation, String labelName) { - localStubRunner.trigger(ivyNotation, labelName) + boolean trigger(String ivyNotation, String labelName) { + return localStubRunner.trigger(ivyNotation, labelName) } @Override - void trigger(String labelName) { - localStubRunner.trigger(labelName) + boolean trigger(String labelName) { + return localStubRunner.trigger(labelName) } @Override - void trigger() { - localStubRunner.trigger() + boolean trigger() { + return localStubRunner.trigger() } private void registerShutdownHook() { diff --git a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/StubRunnerExecutor.groovy b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/StubRunnerExecutor.groovy index 92cb78b7ad..20649d03ae 100644 --- a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/StubRunnerExecutor.groovy +++ b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/StubRunnerExecutor.groovy @@ -69,29 +69,35 @@ class StubRunnerExecutor implements StubFinder { } @Override - void trigger(String ivyNotationAsString, String labelName) { + boolean trigger(String ivyNotationAsString, String labelName) { Collection matchingContracts = getAccurestContracts().findAll { it.key.matches(ivyNotationAsString) }.values().flatten() as Collection - triggerForDsls(matchingContracts, labelName) + return triggerForDsls(matchingContracts, labelName) } @Override - void trigger(String labelName) { - triggerForDsls(getAccurestContracts().values().flatten() as Collection, labelName) + boolean trigger(String labelName) { + return triggerForDsls(getAccurestContracts().values().flatten() as Collection, labelName) } - private void triggerForDsls(Collection dsls, String labelName) { - dsls.findAll { it.label == labelName}.each { + private boolean triggerForDsls(Collection dsls, String labelName) { + Collection matchingDsls = dsls.findAll { it.label == labelName} + if (matchingDsls.empty) { + return false + } + matchingDsls.each { sendMessageIfApplicable(it) } + return true } @Override - void trigger() { + boolean trigger() { (getAccurestContracts().values().flatten() as Collection).each { GroovyDsl groovyDsl -> sendMessageIfApplicable(groovyDsl) } + return true } private void sendMessageIfApplicable(GroovyDsl groovyDsl) { diff --git a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/StubTrigger.groovy b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/StubTrigger.groovy index 94b900bc6d..cee1acebec 100644 --- a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/StubTrigger.groovy +++ b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/StubTrigger.groovy @@ -6,20 +6,26 @@ interface StubTrigger { * Triggers an event by a given label for a given {@code groupid:artifactid} notation. You can use only {@code artifactId} too. * * Feature related to messaging. + * + * @return true - if managed to run a trigger */ - void trigger(String ivyNotation, String labelName) + boolean trigger(String ivyNotation, String labelName) /** * Triggers an event by a given label. * * Feature related to messaging. + * + * @return true - if managed to run a trigger */ - void trigger(String labelName) + boolean trigger(String labelName) /** * Triggers all possible events. * * Feature related to messaging. + * + * @return true - if managed to run a trigger */ - void trigger() + boolean trigger() } \ No newline at end of file diff --git a/stub-runner/stub-runner/src/test/groovy/io/codearte/accurest/stubrunner/BatchStubRunnerSpec.groovy b/stub-runner/stub-runner/src/test/groovy/io/codearte/accurest/stubrunner/BatchStubRunnerSpec.groovy index 71935f62f9..19fbb1227e 100644 --- a/stub-runner/stub-runner/src/test/groovy/io/codearte/accurest/stubrunner/BatchStubRunnerSpec.groovy +++ b/stub-runner/stub-runner/src/test/groovy/io/codearte/accurest/stubrunner/BatchStubRunnerSpec.groovy @@ -1,5 +1,6 @@ package io.codearte.accurest.stubrunner +import io.codearte.accurest.dsl.GroovyDsl import spock.lang.Specification class BatchStubRunnerSpec extends Specification { @@ -22,10 +23,21 @@ class BatchStubRunnerSpec extends Specification { !batchStubRunner.findStubUrl(UNKNOWN_STUB_PATH) } + def 'should throw exception if trying to execute not available trigger'() { + given: + BatchStubRunner batchStubRunner = new BatchStubRunner(runners()) + when: + batchStubRunner.trigger('non existing label') + then: + IllegalArgumentException exception = thrown(IllegalArgumentException) + exception.message == "No label with name [non existing label] was found. Here you have the list of dependencies and their labels [Dependency [a:b:c] has labels [foo]]" + } + Collection runners() { StubRunner runner = Mock(StubRunner) runner.findStubUrl("group", "knownArtifact") >> KNOWN_STUB_URL runner.findStubUrl("group", "unknownArtifact") >> null + runner.accurestContracts >> [(new StubConfiguration('a', 'b', 'c')): [GroovyDsl.make { outputMessage { label 'foo' } }]] return [runner] }