Updated the tests, extended StubFinding contract

This commit is contained in:
Marcin Grzejszczak
2016-03-27 01:47:09 +01:00
parent 940f79185b
commit 14742aed27
14 changed files with 128 additions and 21 deletions

View File

@@ -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

View File

@@ -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'

View File

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

View File

@@ -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'

View File

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

View File

@@ -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'
}
```

View File

@@ -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<StubConfiguration> 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 {

View File

@@ -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 {}
}

View File

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

View File

@@ -10,10 +10,64 @@ import groovy.transform.EqualsAndHashCode
@EqualsAndHashCode
@CompileStatic
class RunningStubs {
final Map<StubConfiguration, Integer> namesAndPorts
final Map<StubConfiguration, Integer> namesAndPorts = [:]
RunningStubs(Map<StubConfiguration, Integer> map) {
this.namesAndPorts = map
this.namesAndPorts.putAll(map)
}
RunningStubs(Collection<RunningStubs> 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

View File

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

View File

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

View File

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

View File

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