From 8f838a8a28d51727d43ddd10d1716fd5195e818f Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Wed, 26 Apr 2017 14:36:48 +0200 Subject: [PATCH] Not running HTTP server if there are no mappings / contracts without this change we're forcing the users to turn on web components even they don't use them with this change if there are no mappings or contracts then we do not start the web server fixes #279 --- .../contract/stubrunner/StubRunnerExecutor.java | 10 ++++++---- .../stubrunner/StubRunnerExecutorSpec.groovy | 13 +++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/StubRunnerExecutor.java b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/StubRunnerExecutor.java index 1eb0c80ea7..455a4aff0d 100644 --- a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/StubRunnerExecutor.java +++ b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/StubRunnerExecutor.java @@ -231,7 +231,7 @@ class StubRunnerExecutor implements StubFinder { final List mappings = repository.getProjectDescriptors(); final Collection contracts = repository.contracts; Integer port = stubRunnerOptions.port(stubConfiguration); - if (!contracts.isEmpty() && !hasRequest(contracts)) { + if (!hasRequest(contracts) && mappings.isEmpty()) { if (log.isDebugEnabled()) { log.debug("There are no HTTP related contracts. Won't start any servers"); } @@ -239,7 +239,7 @@ class StubRunnerExecutor implements StubFinder { return; } if (port != null && port >= 0) { - this.stubServer = new StubServer(stubConfiguration, mappings, contracts, new WireMockHttpServerStub(port)); + this.stubServer = new StubServer(stubConfiguration, mappings, contracts, new WireMockHttpServerStub(port)).start(); } else { this.stubServer = this.portScanner.tryToExecuteWithFreePort(new PortCallback() { @@ -248,12 +248,14 @@ class StubRunnerExecutor implements StubFinder { return new StubServer(stubConfiguration, mappings, contracts, new WireMockHttpServerStub(availablePort)); } - }); + }).start(); } - this.stubServer = this.stubServer.start(); } private boolean hasRequest(Collection contracts) { + if (contracts.isEmpty()) { + return false; + } for (Contract contract : contracts) { if (contract.getRequest() != null) { return true; diff --git a/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/StubRunnerExecutorSpec.groovy b/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/StubRunnerExecutorSpec.groovy index 4ca3d6c9ed..466ac0e531 100644 --- a/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/StubRunnerExecutorSpec.groovy +++ b/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/StubRunnerExecutorSpec.groovy @@ -131,6 +131,19 @@ class StubRunnerExecutorSpec extends Specification { executor.shutdown() } + def 'should not start http server if no contracts or mappings are found'() { + given: + def stubConf = new StubConfiguration('asd', 'asd', 'asd', '') + StubRunnerExecutor executor = new StubRunnerExecutor(portScanner) + when: + RunningStubs stubs = executor.runStubs(stubRunnerOptions, + new StubRepository(new File('src/test/resources/emptyrepo')), stubConf) + then: + stubs.getPort('asd') == -1 + cleanup: + executor.shutdown() + } + Map stubIdsWithPortsFromString(String stubIdsToPortMapping) { return stubIdsToPortMapping.split(',').collectEntries { String entry -> return StubsParser.fromStringWithPort(entry)