Throws exception on missing label

also will print a list of available labels for dependencies

fixes #244
This commit is contained in:
Marcin Grzejszczak
2016-04-26 14:18:12 +02:00
parent a05acfa0be
commit afbde780d3
9 changed files with 117 additions and 72 deletions

View File

@@ -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();
}
}

View File

@@ -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'() {

View File

@@ -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'() {

View File

@@ -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'() {

View File

@@ -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<String, List<String>>).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

View File

@@ -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() {

View File

@@ -69,29 +69,35 @@ class StubRunnerExecutor implements StubFinder {
}
@Override
void trigger(String ivyNotationAsString, String labelName) {
boolean trigger(String ivyNotationAsString, String labelName) {
Collection<GroovyDsl> matchingContracts = getAccurestContracts().findAll {
it.key.matches(ivyNotationAsString)
}.values().flatten() as Collection<GroovyDsl>
triggerForDsls(matchingContracts, labelName)
return triggerForDsls(matchingContracts, labelName)
}
@Override
void trigger(String labelName) {
triggerForDsls(getAccurestContracts().values().flatten() as Collection<GroovyDsl>, labelName)
boolean trigger(String labelName) {
return triggerForDsls(getAccurestContracts().values().flatten() as Collection<GroovyDsl>, labelName)
}
private void triggerForDsls(Collection<GroovyDsl> dsls, String labelName) {
dsls.findAll { it.label == labelName}.each {
private boolean triggerForDsls(Collection<GroovyDsl> dsls, String labelName) {
Collection<GroovyDsl> 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<GroovyDsl>).each { GroovyDsl groovyDsl ->
sendMessageIfApplicable(groovyDsl)
}
return true
}
private void sendMessageIfApplicable(GroovyDsl groovyDsl) {

View File

@@ -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()
}

View File

@@ -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<StubRunner> 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]
}