refactor to allow access logic related to stubs ports from external libraries (#259)

This commit is contained in:
Mariusz Smykuła
2016-05-01 16:47:09 +02:00
parent a272e3b1c6
commit 5a5c79b922
10 changed files with 229 additions and 150 deletions

View File

@@ -1,17 +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.LinkedList;
import java.util.List;
import java.util.Map;
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;
@@ -19,8 +7,16 @@ import io.codearte.accurest.stubrunner.RunningStubs;
import io.codearte.accurest.stubrunner.StubConfiguration;
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 io.codearte.accurest.stubrunner.StubRunnerOptionsBuilder;
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.List;
import java.util.Map;
/**
* JUnit class rule that allows you to download the provided stubs.
@@ -29,10 +25,9 @@ import io.codearte.accurest.stubrunner.util.StubsParser;
*/
public class AccurestRule implements TestRule, StubFinder {
private static final String DELIMITER = ":";
public static final String LATEST_VERSION = "+";
private static final String LATEST_VERSION = "+";
private LinkedList<String> stubs = new LinkedList<>();
private StubRunnerOptions stubRunnerOptions = defaultStubRunnerOptions();
private StubRunnerOptionsBuilder stubRunnerOptionsBuilder = new StubRunnerOptionsBuilder(defaultStubRunnerOptions());
private BatchStubRunner stubFinder;
@Override
@@ -46,25 +41,21 @@ public class AccurestRule implements TestRule, StubFinder {
}
private void before() {
Collection<StubConfiguration> dependencies = StubsParser.fromString(stubs, stubRunnerOptions.getStubsClassifier());
stubFinder = new BatchStubRunnerFactory(stubRunnerOptions, dependencies)
.buildBatchStubRunner();
stubFinder = new BatchStubRunnerFactory(stubRunnerOptionsBuilder.build()).buildBatchStubRunner();
stubFinder.runStubs();
}
};
}
private StubRunnerOptions defaultStubRunnerOptions() {
Integer minPort = Integer.valueOf(System.getProperty("stubrunner.port.range.min", "10000"));
Integer maxPort = Integer.valueOf(System.getProperty("stubrunner.port.range.max", "15000"));
String repoRoot = System.getProperty("stubrunner.stubs.repository.root", "");
String stubSuffix = System.getProperty("stubrunner.stubs.classifier", "stubs");
Boolean workOffline = Boolean.parseBoolean(System.getProperty("stubrunner.work-offline", "false"));
String stubsToDownload = System.getProperty("stubrunner.stubs.ids", "");
if (StringUtils.hasText(stubsToDownload)) {
Collections.addAll(stubs, stubsToDownload.split(","));
}
return new StubRunnerOptions(minPort, maxPort, repoRoot, workOffline, stubSuffix, stubsToDownload);
return new StubRunnerOptionsBuilder()
.withMinPort(Integer.valueOf(System.getProperty("stubrunner.port.range.min", "10000")))
.withMaxPort(Integer.valueOf(System.getProperty("stubrunner.port.range.max", "15000")))
.withStubRepositoryRoot(System.getProperty("stubrunner.stubs.repository.root", ""))
.withWorkOffline(Boolean.parseBoolean(System.getProperty("stubrunner.work-offline", "false")))
.withStubsClassifier(System.getProperty("stubrunner.stubs.classifier", "stubs"))
.withStubs(System.getProperty("stubrunner.stubs.ids", ""))
.build();
}
/**
@@ -73,7 +64,7 @@ public class AccurestRule implements TestRule, StubFinder {
* @see StubRunnerOptions
*/
public AccurestRule options(StubRunnerOptions stubRunnerOptions) {
this.stubRunnerOptions = stubRunnerOptions;
stubRunnerOptionsBuilder.withOptions(stubRunnerOptions);
return this;
}
@@ -81,7 +72,7 @@ public class AccurestRule implements TestRule, StubFinder {
* Min value of port for WireMock server
*/
public AccurestRule minPort(int minPort) {
this.stubRunnerOptions.setMinPortValue(minPort);
stubRunnerOptionsBuilder.withMinPort(minPort);
return this;
}
@@ -89,7 +80,7 @@ public class AccurestRule implements TestRule, StubFinder {
* Max value of port for WireMock server
*/
public AccurestRule maxPort(int maxPort) {
this.stubRunnerOptions.setMaxPortValue(maxPort);
stubRunnerOptionsBuilder.withMaxPort(maxPort);
return this;
}
@@ -97,7 +88,7 @@ public class AccurestRule implements TestRule, StubFinder {
* String URI of repository containing stubs
*/
public AccurestRule repoRoot(String repoRoot) {
this.stubRunnerOptions.setStubRepositoryRoot(repoRoot);
stubRunnerOptionsBuilder.withStubRepositoryRoot(repoRoot);
return this;
}
@@ -105,7 +96,7 @@ public class AccurestRule implements TestRule, StubFinder {
* Should download stubs or use only the local repository
*/
public AccurestRule workOffline(boolean workOffline) {
this.stubRunnerOptions.setWorkOffline(workOffline);
stubRunnerOptionsBuilder.withWorkOffline(workOffline);
return this;
}
@@ -113,7 +104,7 @@ public class AccurestRule implements TestRule, StubFinder {
* Group Id, artifact Id, version and classifier of a single stub to download
*/
public AccurestRule downloadStub(String groupId, String artifactId, String version, String classifier) {
addStub(groupId + DELIMITER + artifactId + DELIMITER + version + DELIMITER + classifier);
stubRunnerOptionsBuilder.withStubs(groupId + DELIMITER + artifactId + DELIMITER + version + DELIMITER + classifier);
return this;
}
@@ -121,7 +112,7 @@ public class AccurestRule implements TestRule, StubFinder {
* Group Id, artifact Id and classifier of a single stub to download in the latest version
*/
public AccurestRule downloadLatestStub(String groupId, String artifactId, String classifier) {
addStub(groupId + DELIMITER + artifactId + DELIMITER + LATEST_VERSION + DELIMITER + classifier);
stubRunnerOptionsBuilder.withStubs(groupId + DELIMITER + artifactId + DELIMITER + LATEST_VERSION + DELIMITER + classifier);
return this;
}
@@ -129,7 +120,7 @@ public class AccurestRule implements TestRule, StubFinder {
* Group Id, artifact Id and version of a single stub to download
*/
public AccurestRule downloadStub(String groupId, String artifactId, String version) {
addStub(groupId + DELIMITER + artifactId + DELIMITER + version);
stubRunnerOptionsBuilder.withStubs(groupId + DELIMITER + artifactId + DELIMITER + version);
return this;
}
@@ -137,7 +128,7 @@ public class AccurestRule implements TestRule, StubFinder {
* Group Id, artifact Id of a single stub to download. Default classifier will be picked.
*/
public AccurestRule downloadStub(String groupId, String artifactId) {
addStub(groupId + DELIMITER + artifactId);
stubRunnerOptionsBuilder.withStubs(groupId + DELIMITER + artifactId);
return this;
}
@@ -145,7 +136,7 @@ public class AccurestRule implements TestRule, StubFinder {
* Ivy notation of a single stub to download.
*/
public AccurestRule downloadStub(String ivyNotation) {
addStub(ivyNotation);
stubRunnerOptionsBuilder.withStubs(ivyNotation);
return this;
}
@@ -153,7 +144,7 @@ public class AccurestRule implements TestRule, StubFinder {
* Stubs to download in Ivy notations
*/
public AccurestRule downloadStubs(String... ivyNotations) {
addStub(Arrays.asList(ivyNotations));
stubRunnerOptionsBuilder.withStubs(Arrays.asList(ivyNotations));
return this;
}
@@ -161,7 +152,7 @@ public class AccurestRule implements TestRule, StubFinder {
* Stubs to download in Ivy notations
*/
public AccurestRule downloadStubs(List<String> ivyNotations) {
addStub(ivyNotations);
stubRunnerOptionsBuilder.withStubs(ivyNotations);
return this;
}
@@ -169,8 +160,7 @@ public class AccurestRule implements TestRule, StubFinder {
* Appends port to last added stub
*/
public AccurestRule withPort(Integer port) {
String lastStub = stubs.peekLast();
addPort(lastStub + DELIMITER + port);
stubRunnerOptionsBuilder.withPort(port);
return this;
}
@@ -214,22 +204,4 @@ public class AccurestRule implements TestRule, StubFinder {
return stubFinder.labels();
}
private void addStub(String notation) {
if(StubsParser.hasPort(notation)) {
addPort(notation);
stubs.add(StubsParser.ivyFromStringWithPort(notation));
} else {
stubs.add(notation);
}
}
private void addStub(List<String> notations) {
for (String notation : notations) {
addStub(notation);
}
}
private void addPort(String notation) {
stubRunnerOptions.putStubIdsToPortMapping(StubsParser.fromStringWithPort(notation));
}
}

View File

@@ -5,12 +5,11 @@ import io.codearte.accurest.messaging.noop.NoOpAccurestMessaging;
import io.codearte.accurest.stubrunner.AetherStubDownloader;
import io.codearte.accurest.stubrunner.BatchStubRunner;
import io.codearte.accurest.stubrunner.BatchStubRunnerFactory;
import io.codearte.accurest.stubrunner.StubConfiguration;
import io.codearte.accurest.stubrunner.StubDownloader;
import io.codearte.accurest.stubrunner.StubRunner;
import io.codearte.accurest.stubrunner.StubRunnerOptions;
import io.codearte.accurest.stubrunner.StubRunnerOptionsBuilder;
import io.codearte.accurest.stubrunner.StubRunning;
import io.codearte.accurest.stubrunner.util.StubsParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
@@ -18,7 +17,6 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import java.io.IOException;
import java.util.Set;
/**
* Configuration that initializes a {@link BatchStubRunner} that runs {@link StubRunner} instance for each stub
@@ -49,10 +47,14 @@ public class StubRunnerConfiguration {
@Value("${stubrunner.stubs.classifier:stubs}") String stubsSuffix,
@Value("${stubrunner.work-offline:false}") boolean workOffline,
@Value("${stubrunner.stubs.ids:}") String stubs) throws IOException {
StubRunnerOptions stubRunnerOptions = new StubRunnerOptions(minPortValue, maxPortValue, uriStringOrEmpty(stubRepositoryRoot),
stubRepositoryRoot == null || workOffline, stubsSuffix, stubs);
Set<StubConfiguration> dependencies = StubsParser.fromString(stubs, stubsSuffix);
BatchStubRunner batchStubRunner = new BatchStubRunnerFactory(stubRunnerOptions, dependencies,
StubRunnerOptions stubRunnerOptions = new StubRunnerOptionsBuilder()
.withMinMaxPort(minPortValue, maxPortValue)
.withStubRepositoryRoot(uriStringOrEmpty(stubRepositoryRoot))
.withWorkOffline(stubRepositoryRoot == null || workOffline)
.withStubsClassifier(stubsSuffix)
.withStubs(stubs)
.build();
BatchStubRunner batchStubRunner = new BatchStubRunnerFactory(stubRunnerOptions,
stubDownloader != null ? stubDownloader : new AetherStubDownloader(stubRunnerOptions),
accurestMessaging != null ? accurestMessaging : new NoOpAccurestMessaging()).buildBatchStubRunner();
// TODO: Consider running it in a separate thread

View File

@@ -14,37 +14,29 @@ import io.codearte.accurest.messaging.noop.NoOpAccurestMessaging
class BatchStubRunnerFactory {
private final StubRunnerOptions stubRunnerOptions
private final Collection<StubConfiguration> dependencies
private final StubDownloader stubDownloader
private final AccurestMessaging accurestMessaging
BatchStubRunnerFactory(StubRunnerOptions stubRunnerOptions, Collection<StubConfiguration> dependencies) {
this(stubRunnerOptions, dependencies, new AetherStubDownloader(stubRunnerOptions), new NoOpAccurestMessaging())
BatchStubRunnerFactory(StubRunnerOptions stubRunnerOptions) {
this(stubRunnerOptions, new AetherStubDownloader(stubRunnerOptions), new NoOpAccurestMessaging())
}
BatchStubRunnerFactory(StubRunnerOptions stubRunnerOptions, Collection<StubConfiguration> dependencies,
AccurestMessaging accurestMessaging) {
this(stubRunnerOptions, dependencies, new AetherStubDownloader(stubRunnerOptions), accurestMessaging)
BatchStubRunnerFactory(StubRunnerOptions stubRunnerOptions, AccurestMessaging accurestMessaging) {
this(stubRunnerOptions, new AetherStubDownloader(stubRunnerOptions), accurestMessaging)
}
BatchStubRunnerFactory(StubRunnerOptions stubRunnerOptions,
Collection<StubConfiguration> dependencies,
StubDownloader stubDownloader) {
this(stubRunnerOptions, dependencies, stubDownloader, new NoOpAccurestMessaging())
BatchStubRunnerFactory(StubRunnerOptions stubRunnerOptions, StubDownloader stubDownloader) {
this(stubRunnerOptions, stubDownloader, new NoOpAccurestMessaging())
}
BatchStubRunnerFactory(StubRunnerOptions stubRunnerOptions,
Collection<StubConfiguration> dependencies,
StubDownloader stubDownloader,
AccurestMessaging accurestMessaging) {
BatchStubRunnerFactory(StubRunnerOptions stubRunnerOptions, StubDownloader stubDownloader, AccurestMessaging accurestMessaging) {
this.stubRunnerOptions = stubRunnerOptions
this.dependencies = dependencies
this.stubDownloader = stubDownloader
this.accurestMessaging = accurestMessaging
}
BatchStubRunner buildBatchStubRunner() {
StubRunnerFactory stubRunnerFactory = new StubRunnerFactory(stubRunnerOptions, dependencies, stubDownloader, accurestMessaging)
StubRunnerFactory stubRunnerFactory = new StubRunnerFactory(stubRunnerOptions, stubDownloader, accurestMessaging)
return new BatchStubRunner(stubRunnerFactory.createStubsFromServiceConfiguration())
}

View File

@@ -3,6 +3,7 @@ package io.codearte.accurest.stubrunner
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import io.codearte.accurest.messaging.AccurestMessaging
/**
* Factory of StubRunners. Basing on the options and passed collaborators
* downloads the stubs and returns a list of corresponding stub runners.
@@ -12,21 +13,17 @@ import io.codearte.accurest.messaging.AccurestMessaging
class StubRunnerFactory {
private final StubRunnerOptions stubRunnerOptions
private final Collection<StubConfiguration> collaborators
private final StubDownloader stubDownloader
private final AccurestMessaging accurestMessaging
StubRunnerFactory(StubRunnerOptions stubRunnerOptions,
Collection<StubConfiguration> collaborators,
StubDownloader stubDownloader, AccurestMessaging accurestMessaging) {
StubRunnerFactory(StubRunnerOptions stubRunnerOptions, StubDownloader stubDownloader, AccurestMessaging accurestMessaging) {
this.stubRunnerOptions = stubRunnerOptions
this.collaborators = collaborators
this.stubDownloader = stubDownloader
this.accurestMessaging = accurestMessaging
}
Collection<StubRunner> createStubsFromServiceConfiguration() {
return collaborators.collect { StubConfiguration stubsConfiguration ->
return stubRunnerOptions.getDependencies().collect { StubConfiguration stubsConfiguration ->
Map.Entry<StubConfiguration, File> entry = stubDownloader.downloadAndUnpackStubJar(stubRunnerOptions, stubsConfiguration)
if (!entry) {
return null
@@ -43,7 +40,7 @@ class StubRunnerFactory {
}
private StubRunner createStubRunner(File unzippedStubsDir, StubConfiguration stubsConfiguration,
StubRunnerOptions stubRunnerOptions) {
StubRunnerOptions stubRunnerOptions) {
return new StubRunner(stubRunnerOptions, unzippedStubsDir.path, stubsConfiguration, accurestMessaging)
}

View File

@@ -2,7 +2,6 @@ package io.codearte.accurest.stubrunner
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import io.codearte.accurest.stubrunner.util.StubsParser
import org.kohsuke.args4j.CmdLineException
import org.kohsuke.args4j.CmdLineParser
import org.kohsuke.args4j.Option
@@ -37,8 +36,14 @@ class StubRunnerMain {
CmdLineParser parser = new CmdLineParser(this)
try {
parser.parseArgument(args)
this.arguments = new Arguments(new StubRunnerOptions(minPortValue, maxPortValue, stubRepositoryRoot,
workOffline, stubsSuffix))
StubRunnerOptions stubRunnerOptions = new StubRunnerOptionsBuilder()
.withMinMaxPort(minPortValue, maxPortValue)
.withStubRepositoryRoot(stubRepositoryRoot)
.withWorkOffline(workOffline)
.withStubsClassifier(stubsSuffix)
.withStubs(stubs)
.build()
this.arguments = new Arguments(stubRunnerOptions)
} catch (CmdLineException e) {
printErrorMessage(e, parser)
throw e
@@ -61,8 +66,7 @@ class StubRunnerMain {
try {
log.debug("Launching StubRunner with args: $arguments")
// TODO: Pass StubsToRun either from String or File
Collection<StubConfiguration> collaborators = StubsParser.fromString(stubs, stubsSuffix)
BatchStubRunner stubRunner = new BatchStubRunnerFactory(arguments.stubRunnerOptions, collaborators).buildBatchStubRunner()
BatchStubRunner stubRunner = new BatchStubRunnerFactory(arguments.stubRunnerOptions).buildBatchStubRunner()
RunningStubs runningCollaborators = stubRunner.runStubs()
log.info(runningCollaborators.toString())
} catch (Exception e) {

View File

@@ -1,11 +1,15 @@
package io.codearte.accurest.stubrunner
import groovy.transform.CompileStatic
import groovy.transform.PackageScope
import groovy.transform.ToString
import io.codearte.accurest.stubrunner.util.StubsParser
/**
* Technical options related to running StubRunner
*
* Use {@class StubRunnerOptionsBuilder} to build this object.
*
* @see StubRunnerOptionsBuilder
*/
@ToString(includeNames = true)
@CompileStatic
@@ -14,77 +18,53 @@ class StubRunnerOptions {
/**
* min port value of the WireMock instance for the given collaborator
*/
Integer minPortValue = 10000
final Integer minPortValue
/**
* max port value of the WireMock instance for the given collaborator
*/
Integer maxPortValue = 15000
final Integer maxPortValue
/**
* root URL from where the JAR with stub mappings will be downloaded
*/
String stubRepositoryRoot
final String stubRepositoryRoot
/**
* avoids local repository in dependency resolution
*/
boolean workOffline = false
final boolean workOffline
/**
* stub definition classifier
*/
final String stubsClassifier
final Collection<StubConfiguration> dependencies
/**
* colon separated list of ids to the desired port
*/
Map<StubConfiguration, Integer> stubIdsToPortMapping = [:]
/**
* stub definition suffix
*/
String stubsClassifier = "stubs"
final Map<StubConfiguration, Integer> stubIdsToPortMapping
@PackageScope
StubRunnerOptions(Integer minPortValue, Integer maxPortValue, String stubRepositoryRoot,
boolean workOffline, String stubsClassifier, String stubIdsToPortMapping) {
boolean workOffline, String stubsClassifier, Collection<StubConfiguration> dependencies, Map<StubConfiguration, Integer> stubIdsToPortMapping) {
this.minPortValue = minPortValue
this.maxPortValue = maxPortValue
this.stubRepositoryRoot = stubRepositoryRoot
this.workOffline = workOffline
this.stubsClassifier = stubsClassifier
this.stubIdsToPortMapping = stubIdsWithPortsFromString(stubIdsToPortMapping)
}
StubRunnerOptions(Integer minPortValue, Integer maxPortValue, String stubRepositoryRoot,
boolean workOffline, String stubsClassifier) {
this.minPortValue = minPortValue
this.maxPortValue = maxPortValue
this.stubRepositoryRoot = stubRepositoryRoot
this.workOffline = workOffline
this.stubsClassifier = stubsClassifier
}
StubRunnerOptions(String stubRepositoryRoot) {
this.stubRepositoryRoot = stubRepositoryRoot
}
StubRunnerOptions() {}
Map<StubConfiguration, Integer> stubIdsWithPortsFromString(String stubIdsToPortMapping) {
return stubIdsToPortMapping.split(',').collectEntries { String entry ->
return StubsParser.fromStringWithPort(entry)
}
}
Integer port(StubConfiguration stubConfiguration) {
return stubIdsToPortMapping[stubConfiguration]
}
void setStubIdsToPortMapping(Map<StubConfiguration, Integer> stubIdsToPortMapping) {
this.dependencies = dependencies
this.stubIdsToPortMapping = stubIdsToPortMapping
}
void putStubIdsToPortMapping(Map<StubConfiguration, Integer> stubIdsToPortMapping) {
this.stubIdsToPortMapping.putAll(stubIdsToPortMapping)
Integer port(StubConfiguration stubConfiguration) {
if (stubIdsToPortMapping) {
return stubIdsToPortMapping[stubConfiguration]
} else {
return null
}
}
void setStubIdsToPortMapping(String stubIdsToPortMapping) {
this.stubIdsToPortMapping = stubIdsWithPortsFromString(stubIdsToPortMapping)
}
}

View File

@@ -0,0 +1,118 @@
package io.codearte.accurest.stubrunner
import groovy.transform.CompileStatic
import io.codearte.accurest.stubrunner.util.StubsParser
@CompileStatic
class StubRunnerOptionsBuilder {
private static final String DELIMITER = ":";
private LinkedList<String> stubs = new LinkedList<>()
private Map<StubConfiguration, Integer> stubIdsToPortMapping = [:]
private Integer minPortValue = 10000
private Integer maxPortValue = 15000
private String stubRepositoryRoot
private boolean workOffline = false
private String stubsClassifier = "stubs"
StubRunnerOptionsBuilder() {
}
StubRunnerOptionsBuilder(StubRunnerOptions options) {
withOptions(options)
}
StubRunnerOptionsBuilder withStubs(String stubs) {
addStub(stubsToList(stubs))
return this
}
StubRunnerOptionsBuilder withStubs(List<String> stubs) {
for (String stub : stubs) {
withStubs(stub);
}
return this
}
StubRunnerOptionsBuilder withMinMaxPort(Integer minPortValue, Integer maxPortValue) {
this.minPortValue = minPortValue
this.maxPortValue = maxPortValue
return this
}
StubRunnerOptionsBuilder withMinPort(int minPortValue) {
this.minPortValue = minPortValue
return this
}
StubRunnerOptionsBuilder withMaxPort(int maxPortValue) {
this.maxPortValue = maxPortValue
return this
}
StubRunnerOptionsBuilder withStubRepositoryRoot(String stubRepositoryRoot) {
this.stubRepositoryRoot = stubRepositoryRoot
return this
}
StubRunnerOptionsBuilder withWorkOffline(boolean workOffline) {
this.workOffline = workOffline
return this
}
StubRunnerOptionsBuilder withStubsClassifier(String stubsClassifier) {
this.stubsClassifier = stubsClassifier
return this
}
StubRunnerOptionsBuilder withPort(Integer port) {
String lastStub = stubs.peekLast();
println "PORT $lastStub -> $port"
addPort(lastStub + DELIMITER + port);
return this;
}
StubRunnerOptions build() {
return new StubRunnerOptions(minPortValue, maxPortValue, stubRepositoryRoot, workOffline, stubsClassifier, buildDependencies(), stubIdsToPortMapping)
}
void withOptions(StubRunnerOptions options) {
this.minPortValue = options.minPortValue
this.maxPortValue = options.maxPortValue
this.stubRepositoryRoot = options.stubRepositoryRoot
this.workOffline = options.workOffline
this.stubsClassifier = options.stubsClassifier
}
private Collection<StubConfiguration> buildDependencies() {
return StubsParser.fromString(stubs, stubsClassifier);
}
private static List<String> stubsToList(String stubIdsToPortMapping) {
return stubIdsToPortMapping.split(',').collect { it }
}
private void addStub(List<String> notations) {
for (String notation : notations) {
addStub(notation);
}
}
private void addStub(String notation) {
if (StubsParser.hasPort(notation)) {
addPort(notation);
stubs.add(StubsParser.ivyFromStringWithPort(notation));
} else {
stubs.add(notation);
}
}
private void addPort(String notation) {
putStubIdsToPortMapping(StubsParser.fromStringWithPort(notation));
}
private void putStubIdsToPortMapping(Map<StubConfiguration, Integer> stubIdsToPortMapping) {
this.stubIdsToPortMapping.putAll(stubIdsToPortMapping)
}
}

View File

@@ -1,5 +1,6 @@
package io.codearte.accurest.stubrunner
import io.codearte.accurest.stubrunner.util.StubsParser
import spock.lang.Specification
class StubRunnerExecutorSpec extends Specification {
@@ -10,7 +11,7 @@ class StubRunnerExecutorSpec extends Specification {
private AvailablePortScanner portScanner
private StubRepository repository
private StubConfiguration stub = new StubConfiguration("group:artifact", "stubs")
private StubRunnerOptions stubRunnerOptions = new StubRunnerOptions()
private StubRunnerOptions stubRunnerOptions = new StubRunnerOptionsBuilder().build()
def setup() {
portScanner = new AvailablePortScanner(MIN_PORT, MAX_PORT)
@@ -46,11 +47,18 @@ class StubRunnerExecutorSpec extends Specification {
def 'should start a stub on a given port'() {
given:
StubRunnerExecutor executor = new StubRunnerExecutor(portScanner)
stubRunnerOptions.setStubIdsToPortMapping('group:artifact:12345,someotherartifact:123')
stubRunnerOptions = new StubRunnerOptionsBuilder(stubIdsToPortMapping: stubIdsWithPortsFromString('group:artifact:12345,someotherartifact:123'))
.build()
when:
executor.runStubs(stubRunnerOptions, repository, stub)
then:
executor.findStubUrl("group", "artifact") == 'http://localhost:12345'.toURL()
}
Map<StubConfiguration, Integer> stubIdsWithPortsFromString(String stubIdsToPortMapping) {
return stubIdsToPortMapping.split(',').collectEntries { String entry ->
return StubsParser.fromStringWithPort(entry)
}
}
}

View File

@@ -10,17 +10,23 @@ class StubRunnerFactorySpec extends Specification {
@Rule
TemporaryFolder folder = new TemporaryFolder()
Collection<StubConfiguration> collaborators = [new StubConfiguration("a:b"), new StubConfiguration("c:d")]
String stubs = "a:b,c:d"
StubDownloader downloader = Mock(StubDownloader)
StubRunnerOptions stubRunnerOptions = new StubRunnerOptions(stubRepositoryRoot: 'http://sth.net')
StubRunnerFactory factory = new StubRunnerFactory(stubRunnerOptions, collaborators, downloader, new NoOpAccurestMessaging())
StubRunnerOptions stubRunnerOptions
StubRunnerFactory factory
void setup() {
stubRunnerOptions = new StubRunnerOptionsBuilder()
.withStubRepositoryRoot(folder.root.absolutePath) // FIXME: not used
.withStubs(stubs).build()
factory = new StubRunnerFactory(stubRunnerOptions, downloader, new NoOpAccurestMessaging())
}
def "Should download stub definitions many times"() {
given:
folder.newFolder("mappings")
1 * downloader.downloadAndUnpackStubJar(_, _) >> new AbstractMap.SimpleEntry(new StubConfiguration('a:b'), folder.root)
1 * downloader.downloadAndUnpackStubJar(_, _) >> new AbstractMap.SimpleEntry(new StubConfiguration('c:d'), folder.root)
stubRunnerOptions.stubRepositoryRoot = folder.root.absolutePath
when:
Collection<StubRunner> stubRunners = collectOnlyPresentValues(factory.createStubsFromServiceConfiguration())
then:

View File

@@ -32,7 +32,7 @@ class StubRunnerSpec extends Specification {
Arguments argumentsWithProjectDefinition() {
StubConfiguration stubConfiguration = new StubConfiguration("groupId", "artifactId", "classifier")
StubRunnerOptions stubRunnerOptions = new StubRunnerOptions(minPortValue: MIN_PORT, maxPortValue: MAX_PORT)
StubRunnerOptions stubRunnerOptions = new StubRunnerOptionsBuilder().withMinMaxPort(MIN_PORT, MAX_PORT).build()
return new Arguments(stubRunnerOptions, 'src/test/resources/repository', stubConfiguration)
}