From 14742aed277b7edbe48e1baf6d08d04cd7d5b5ce Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Sun, 27 Mar 2016 01:47:09 +0100 Subject: [PATCH] Updated the tests, extended StubFinding contract --- .travis.yml | 2 +- stub-runner/stub-runner-junit/README.md | 4 ++ .../stubrunner/junit/AccurestRule.java | 6 ++ .../stubrunner/junit/AccurestRuleSpec.groovy | 11 +++- .../junit/AccurestRuleSysPropsSpec.groovy | 5 +- stub-runner/stub-runner-spring/README.md | 20 ++++--- .../spring/StubRunnerConfiguration.java | 11 ++-- .../spring/StubRunnerConfigurationSpec.groovy | 8 ++- .../stubrunner/BatchStubRunner.groovy | 5 ++ .../accurest/stubrunner/RunningStubs.groovy | 58 ++++++++++++++++++- .../accurest/stubrunner/StubFinder.groovy | 5 ++ .../accurest/stubrunner/StubRunner.groovy | 5 ++ .../stubrunner/StubRunnerExecutor.groovy | 5 ++ .../stubrunner/StubRunnerExecutorSpec.groovy | 4 ++ 14 files changed, 128 insertions(+), 21 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2e6e565f5f..14f8d62652 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,4 +18,4 @@ cache: - $HOME/.m2 install: ./gradlew assemble -script: ./gradlew clean check funcTest --stacktrace --info --continue +script: ./gradlew clean check funcTest --stacktrace --info --continue --parallel diff --git a/stub-runner/stub-runner-junit/README.md b/stub-runner/stub-runner-junit/README.md index 985bd8d743..83a6a51adf 100644 --- a/stub-runner/stub-runner-junit/README.md +++ b/stub-runner/stub-runner-junit/README.md @@ -19,6 +19,10 @@ class AccurestRuleSpec extends Specification { rule.findStubUrl('loanIssuance') != null rule.findStubUrl('loanIssuance') == rule.findStubUrl('io.codearte.accurest.stubs', 'loanIssuance') rule.findStubUrl('io.codearte.accurest.stubs:fraudDetectionServer') != null + and: + rule.findAllRunningStubs().isPresent('loanIssuance') + rule.findAllRunningStubs().isPresent('io.codearte.accurest.stubs', 'fraudDetectionServer') + rule.findAllRunningStubs().isPresent('io.codearte.accurest.stubs:fraudDetectionServer') and: 'Stubs were registered' "${rule.findStubUrl('loanIssuance').toString()}/name".toURL().text == 'loanIssuance' "${rule.findStubUrl('fraudDetectionServer').toString()}/name".toURL().text == 'fraudDetectionServer' 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 b584b8ca7f..6236e5d710 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 @@ -14,6 +14,7 @@ import org.junit.runners.model.Statement; import io.codearte.accurest.stubrunner.BatchStubRunner; import io.codearte.accurest.stubrunner.BatchStubRunnerFactory; +import io.codearte.accurest.stubrunner.RunningStubs; import io.codearte.accurest.stubrunner.StubConfiguration; import io.codearte.accurest.stubrunner.StubFinder; import io.codearte.accurest.stubrunner.StubRunnerOptions; @@ -155,4 +156,9 @@ public class AccurestRule implements TestRule, StubFinder { public URL findStubUrl(String ivyNotation) { return stubFinder.findStubUrl(ivyNotation); } + + @Override + public RunningStubs findAllRunningStubs() { + return stubFinder.findAllRunningStubs(); + } } diff --git a/stub-runner/stub-runner-junit/src/test/groovy/io/codearte/accurest/stubrunner/junit/AccurestRuleSpec.groovy b/stub-runner/stub-runner-junit/src/test/groovy/io/codearte/accurest/stubrunner/junit/AccurestRuleSpec.groovy index 755f8857c7..a5dbf056e1 100644 --- a/stub-runner/stub-runner-junit/src/test/groovy/io/codearte/accurest/stubrunner/junit/AccurestRuleSpec.groovy +++ b/stub-runner/stub-runner-junit/src/test/groovy/io/codearte/accurest/stubrunner/junit/AccurestRuleSpec.groovy @@ -9,8 +9,13 @@ import spock.lang.Specification */ class AccurestRuleSpec extends Specification { + static { + System.properties.setProperty("stubrunner.stubs.repository.root", "") + System.properties.setProperty("stubrunner.stubs.classifier", 'stubs') + } + @ClassRule @Shared AccurestRule rule = new AccurestRule() - .repoRoot(AccurestRuleSpec.getResource("/m2repo").path) + .repoRoot(AccurestRuleSpec.getResource("/m2repo").toURI().toString()) .downloadStub("io.codearte.accurest.stubs", "loanIssuance") .downloadStub("io.codearte.accurest.stubs:fraudDetectionServer") @@ -20,6 +25,10 @@ class AccurestRuleSpec extends Specification { rule.findStubUrl('loanIssuance') != null rule.findStubUrl('loanIssuance') == rule.findStubUrl('io.codearte.accurest.stubs', 'loanIssuance') rule.findStubUrl('io.codearte.accurest.stubs:fraudDetectionServer') != null + and: + rule.findAllRunningStubs().isPresent('loanIssuance') + rule.findAllRunningStubs().isPresent('io.codearte.accurest.stubs', 'fraudDetectionServer') + rule.findAllRunningStubs().isPresent('io.codearte.accurest.stubs:fraudDetectionServer') and: 'Stubs were registered' "${rule.findStubUrl('loanIssuance').toString()}/name".toURL().text == 'loanIssuance' "${rule.findStubUrl('fraudDetectionServer').toString()}/name".toURL().text == 'fraudDetectionServer' diff --git a/stub-runner/stub-runner-junit/src/test/groovy/io/codearte/accurest/stubrunner/junit/AccurestRuleSysPropsSpec.groovy b/stub-runner/stub-runner-junit/src/test/groovy/io/codearte/accurest/stubrunner/junit/AccurestRuleSysPropsSpec.groovy index 77ab729acc..2dc4fe0939 100644 --- a/stub-runner/stub-runner-junit/src/test/groovy/io/codearte/accurest/stubrunner/junit/AccurestRuleSysPropsSpec.groovy +++ b/stub-runner/stub-runner-junit/src/test/groovy/io/codearte/accurest/stubrunner/junit/AccurestRuleSysPropsSpec.groovy @@ -12,7 +12,7 @@ import spock.util.environment.RestoreSystemProperties class AccurestRuleSysPropsSpec extends Specification { static { - System.properties.setProperty("stubrunner.stubs.repository.root", AccurestRuleSysPropsSpec.getResource("/m2repo").path) + System.properties.setProperty("stubrunner.stubs.repository.root", AccurestRuleSysPropsSpec.getResource("/m2repo").toURI().toString()) System.properties.setProperty("stubrunner.stubs.classifier", 'classifier that will be overridden') } @@ -29,5 +29,8 @@ class AccurestRuleSysPropsSpec extends Specification { and: 'Stubs were registered' "${rule.findStubUrl('loanIssuance').toString()}/name".toURL().text == 'loanIssuance' "${rule.findStubUrl('fraudDetectionServer').toString()}/name".toURL().text == 'fraudDetectionServer' + cleanup: + System.properties.setProperty("stubrunner.stubs.repository.root", "") + System.properties.setProperty("stubrunner.stubs.classifier", 'stubs') } } diff --git a/stub-runner/stub-runner-spring/README.md b/stub-runner/stub-runner-spring/README.md index 2229814c9e..c6d26a1386 100644 --- a/stub-runner/stub-runner-spring/README.md +++ b/stub-runner/stub-runner-spring/README.md @@ -13,14 +13,18 @@ its methods as presented below: @Autowired StubFinder stubFinder def 'should start WireMock servers'() { - expect: 'WireMocks are running' - stubFinder.findStubUrl('io.codearte.accurest.stubs', 'loanIssuance') != null - stubFinder.findStubUrl('loanIssuance') != null - stubFinder.findStubUrl('loanIssuance') == stubFinder.findStubUrl('io.codearte.accurest.stubs', 'loanIssuance') - stubFinder.findStubUrl('io.codearte.accurest.stubs:fraudDetectionServer') != null - and: 'Stubs were registered' - "${stubFinder.findStubUrl('loanIssuance').toString()}/name".toURL().text == 'loanIssuance' - "${stubFinder.findStubUrl('fraudDetectionServer').toString()}/name".toURL().text == 'fraudDetectionServer' + expect: 'WireMocks are running' + stubFinder.findStubUrl('io.codearte.accurest.stubs', 'loanIssuance') != null + stubFinder.findStubUrl('loanIssuance') != null + stubFinder.findStubUrl('loanIssuance') == stubFinder.findStubUrl('io.codearte.accurest.stubs', 'loanIssuance') + stubFinder.findStubUrl('io.codearte.accurest.stubs:fraudDetectionServer') != null + and: + stubFinder.findAllRunningStubs().isPresent('loanIssuance') + stubFinder.findAllRunningStubs().isPresent('io.codearte.accurest.stubs', 'fraudDetectionServer') + stubFinder.findAllRunningStubs().isPresent('io.codearte.accurest.stubs:fraudDetectionServer') + and: 'Stubs were registered' + "${stubFinder.findStubUrl('loanIssuance').toString()}/name".toURL().text == 'loanIssuance' + "${stubFinder.findStubUrl('fraudDetectionServer').toString()}/name".toURL().text == 'fraudDetectionServer' } ``` diff --git a/stub-runner/stub-runner-spring/src/main/groovy/io/codearte/accurest/stubrunner/spring/StubRunnerConfiguration.java b/stub-runner/stub-runner-spring/src/main/groovy/io/codearte/accurest/stubrunner/spring/StubRunnerConfiguration.java index 0f1c8d6939..ebc373b762 100644 --- a/stub-runner/stub-runner-spring/src/main/groovy/io/codearte/accurest/stubrunner/spring/StubRunnerConfiguration.java +++ b/stub-runner/stub-runner-spring/src/main/groovy/io/codearte/accurest/stubrunner/spring/StubRunnerConfiguration.java @@ -34,7 +34,7 @@ public class StubRunnerConfiguration { * @param workOffline forces offline work * @param stubs comma separated list of stubs presented in Ivy notation */ - @Bean(initMethod = "runStubs", destroyMethod = "close") + @Bean(destroyMethod = "close") public StubRunning batchStubRunner( @Value("${stubrunner.port.range.min:10000}") Integer minPortValue, @Value("${stubrunner.port.range.max:15000}") Integer maxPortValue, @@ -42,12 +42,13 @@ public class StubRunnerConfiguration { @Value("${stubrunner.stubs.classifier:stubs}") String stubsSuffix, @Value("${stubrunner.work-offline:false}") boolean workOffline, @Value("${stubrunner.stubs:}") String stubs) throws IOException { - StubRunnerOptions stubRunnerOptions = new StubRunnerOptions(minPortValue, - maxPortValue, uriStringOrEmpty(stubRepositoryRoot), + StubRunnerOptions stubRunnerOptions = new StubRunnerOptions(minPortValue, maxPortValue, uriStringOrEmpty(stubRepositoryRoot), stubRepositoryRoot == null || workOffline, stubsSuffix); Set dependencies = StubsParser.fromString(stubs, stubsSuffix); - return new BatchStubRunnerFactory(stubRunnerOptions, dependencies) - .buildBatchStubRunner(); + BatchStubRunner batchStubRunner = new BatchStubRunnerFactory(stubRunnerOptions, dependencies).buildBatchStubRunner(); + // TODO: Consider running it in a separate thread + batchStubRunner.runStubs(); + return batchStubRunner; } private String uriStringOrEmpty(Resource stubRepositoryRoot) throws IOException { diff --git a/stub-runner/stub-runner-spring/src/test/groovy/io/codearte/accurest/stubrunner/spring/StubRunnerConfigurationSpec.groovy b/stub-runner/stub-runner-spring/src/test/groovy/io/codearte/accurest/stubrunner/spring/StubRunnerConfigurationSpec.groovy index 2e3a749ecd..b43cd14143 100644 --- a/stub-runner/stub-runner-spring/src/test/groovy/io/codearte/accurest/stubrunner/spring/StubRunnerConfigurationSpec.groovy +++ b/stub-runner/stub-runner-spring/src/test/groovy/io/codearte/accurest/stubrunner/spring/StubRunnerConfigurationSpec.groovy @@ -23,6 +23,10 @@ class StubRunnerConfigurationSpec extends Specification { stubFinder.findStubUrl('loanIssuance') != null stubFinder.findStubUrl('loanIssuance') == stubFinder.findStubUrl('io.codearte.accurest.stubs', 'loanIssuance') stubFinder.findStubUrl('io.codearte.accurest.stubs:fraudDetectionServer') != null + and: + stubFinder.findAllRunningStubs().isPresent('loanIssuance') + stubFinder.findAllRunningStubs().isPresent('io.codearte.accurest.stubs', 'fraudDetectionServer') + stubFinder.findAllRunningStubs().isPresent('io.codearte.accurest.stubs:fraudDetectionServer') and: 'Stubs were registered' "${stubFinder.findStubUrl('loanIssuance').toString()}/name".toURL().text == 'loanIssuance' "${stubFinder.findStubUrl('fraudDetectionServer').toString()}/name".toURL().text == 'fraudDetectionServer' @@ -31,7 +35,5 @@ class StubRunnerConfigurationSpec extends Specification { @Configuration @Import(StubRunnerConfiguration) @EnableAutoConfiguration - static class Config { - - } + static class Config {} } 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 3281ca38a7..a29a799eaf 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 @@ -43,6 +43,11 @@ class BatchStubRunner implements StubRunning { return findStubUrl(null, splitString[0]) } + @Override + RunningStubs findAllRunningStubs() { + return new RunningStubs(stubRunners.collect { StubRunner runner -> runner.findAllRunningStubs() }) + } + @Override void close() throws IOException { stubRunners.each { diff --git a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/RunningStubs.groovy b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/RunningStubs.groovy index c2f0572d3f..c856dd9dd2 100644 --- a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/RunningStubs.groovy +++ b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/RunningStubs.groovy @@ -10,10 +10,64 @@ import groovy.transform.EqualsAndHashCode @EqualsAndHashCode @CompileStatic class RunningStubs { - final Map namesAndPorts + final Map namesAndPorts = [:] RunningStubs(Map map) { - this.namesAndPorts = map + this.namesAndPorts.putAll(map) + } + + RunningStubs(Collection runningStubs) { + runningStubs.each { + this.namesAndPorts.putAll(it.namesAndPorts) + } + } + + Integer getPort(String artifactId) { + def strings = artifactId.split(':') + if (strings.length == 1) { + return namesAndPorts.entrySet().find { + it.key.artifactId == artifactId + }?.value + } else if(strings.length == 2) { + return namesAndPorts.entrySet().find { + it.key.groupId == strings[0] && it.key.artifactId == strings[1] + }?.value + } + return namesAndPorts.entrySet().find { + it.key.groupId == strings[0] && + it.key.artifactId == strings[1] && + it.key.classifier == strings[2] + }?.value + } + + Integer getPort(String groupId, String artifactId) { + return namesAndPorts.entrySet().find { + it.key.artifactId == artifactId && it.key.groupId == groupId + }?.value + } + + boolean isPresent(String artifactId) { + def strings = artifactId.split(':') + if (strings.length == 1) { + return namesAndPorts.entrySet().find { + it.key.artifactId == artifactId + } + } else if(strings.length == 2) { + return namesAndPorts.entrySet().find { + it.key.groupId == strings[0] && it.key.artifactId == strings[1] + } + } + return namesAndPorts.entrySet().find { + it.key.groupId == strings[0] && + it.key.artifactId == strings[1] && + it.key.classifier == strings[2] + } + } + + boolean isPresent(String groupId, String artifactId) { + return namesAndPorts.entrySet().find { + it.key.artifactId == artifactId && it.key.groupId == groupId + } } @Override diff --git a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/StubFinder.groovy b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/StubFinder.groovy index f608436c2a..f67825bcef 100644 --- a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/StubFinder.groovy +++ b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/StubFinder.groovy @@ -18,4 +18,9 @@ interface StubFinder { * @return URL of a running stub or null if not found */ URL findStubUrl(String ivyNotation) + + /** + * Returns all running stubs + */ + RunningStubs findAllRunningStubs() } \ No newline at end of file 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 fd580d3e99..0777d7d56a 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 @@ -46,6 +46,11 @@ class StubRunner implements StubRunning { return findStubUrl(splitString[0], splitString[1]) } + @Override + RunningStubs findAllRunningStubs() { + return localStubRunner.findAllRunningStubs() + } + private void registerShutdownHook() { Runnable stopAllServers = { this.close() } Runtime.runtime.addShutdownHook(new Thread(stopAllServers)) 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 a12707c12b..a398a3ec4b 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 @@ -47,6 +47,11 @@ class StubRunnerExecutor implements StubFinder { return findStubUrl(splitString[0], splitString[1]) } + @Override + RunningStubs findAllRunningStubs() { + return new RunningStubs([(stubServer.stubConfiguration) : stubServer.port]) + } + private URL returnStubUrlIfMatches(boolean condition) { return condition ? stubServer.stubUrl : null } diff --git a/stub-runner/stub-runner/src/test/groovy/io/codearte/accurest/stubrunner/StubRunnerExecutorSpec.groovy b/stub-runner/stub-runner/src/test/groovy/io/codearte/accurest/stubrunner/StubRunnerExecutorSpec.groovy index 7b086f7458..ce378588a5 100644 --- a/stub-runner/stub-runner/src/test/groovy/io/codearte/accurest/stubrunner/StubRunnerExecutorSpec.groovy +++ b/stub-runner/stub-runner/src/test/groovy/io/codearte/accurest/stubrunner/StubRunnerExecutorSpec.groovy @@ -24,6 +24,10 @@ class StubRunnerExecutorSpec extends Specification { executor.runStubs(repository, stub) then: executor.findStubUrl("group", "artifact") == EXPECTED_STUB_URL + and: + executor.findAllRunningStubs().isPresent('artifact') + executor.findAllRunningStubs().isPresent('group', 'artifact') + executor.findAllRunningStubs().isPresent('group:artifact') cleanup: executor.shutdown() }