Updates to use new TestSocketUtils from spring-cloud-test-support

This commit is contained in:
spencergibb
2022-03-11 13:27:45 -05:00
parent 0930e1d6cb
commit 21a60b8f0d
18 changed files with 51 additions and 26 deletions

View File

@@ -577,7 +577,7 @@ import io.javalin.Javalin;
import io.restassured.RestAssured;
import org.junit.After;
import org.junit.Before;
import org.springframework.util.SocketUtils;
import org.springframework.cloud.test.TestSocketUtils;
public class BaseClass {
@@ -586,7 +586,7 @@ public class BaseClass {
@Before
public void setup() {
// pick a random port
int port = SocketUtils.findAvailableTcpPort();
int port = TestSocketUtils.findAvailableTcpPort();
// start the application at a random port
this.app = start(port);
// tell Rest Assured where the started application is

View File

@@ -201,6 +201,11 @@
<artifactId>spring-boot-starter-web</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-test-support</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>

View File

@@ -24,7 +24,7 @@ import spock.lang.Specification
import org.springframework.cloud.contract.stubrunner.util.StubsParser
import org.springframework.cloud.contract.verifier.converter.YamlContract
import org.springframework.cloud.contract.verifier.messaging.MessageVerifier
import org.springframework.util.SocketUtils
import org.springframework.cloud.test.TestSocketUtils
class StubRunnerExecutorSpec extends Specification {
@@ -74,7 +74,7 @@ class StubRunnerExecutorSpec extends Specification {
def 'should start a stub on a given port'() {
given:
int port = SocketUtils.findAvailableTcpPort()
int port = TestSocketUtils.findAvailableTcpPort()
StubRunnerExecutor executor = new StubRunnerExecutor(portScanner)
stubRunnerOptions = new StubRunnerOptionsBuilder(stubIdsToPortMapping:
stubIdsWithPortsFromString("group:artifact:${port},someotherartifact:${SocketUtils.findAvailableTcpPort()}"))

View File

@@ -21,11 +21,11 @@ import spock.lang.Specification
import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties
import org.springframework.cloud.contract.verifier.messaging.noop.NoOpStubMessages
import org.springframework.util.SocketUtils
import org.springframework.cloud.test.TestSocketUtils
class StubRunnerSpec extends Specification {
private static final int MIN_PORT = SocketUtils.findAvailableTcpPort()
private static final int MIN_PORT = TestSocketUtils.findAvailableTcpPort()
private static final int MAX_PORT = MIN_PORT
private static final URL EXPECTED_STUB_URL = new URL("http://localhost:$MIN_PORT")

View File

@@ -19,10 +19,10 @@ package org.springframework.cloud.contract.stubrunner
import spock.lang.Specification
import org.springframework.cloud.contract.stubrunner.provider.wiremock.WireMockHttpServerStub
import org.springframework.util.SocketUtils
import org.springframework.cloud.test.TestSocketUtils
class StubServerSpec extends Specification {
static final int STUB_SERVER_PORT = SocketUtils.findAvailableTcpPort()
static final int STUB_SERVER_PORT = TestSocketUtils.findAvailableTcpPort()
static final URL EXPECTED_URL = new URL("http://localhost:$STUB_SERVER_PORT")
File repository = new File('src/test/resources/repository/mappings/spring/cloud/bye')

View File

@@ -25,9 +25,9 @@ import org.springframework.boot.test.system.OutputCaptureRule
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.cloud.contract.stubrunner.HttpServerStubConfiguration
import org.springframework.cloud.contract.stubrunner.HttpServerStubConfigurer
import org.springframework.cloud.test.TestSocketUtils
import org.springframework.http.HttpEntity
import org.springframework.http.HttpMethod
import org.springframework.util.SocketUtils
import org.springframework.web.client.RestTemplate
class WireMockHttpServerStubSpec extends Specification {
@@ -43,7 +43,7 @@ class WireMockHttpServerStubSpec extends Specification {
def 'should describe stub mapping'() {
given:
WireMockHttpServerStub mappingDescriptor = new WireMockHttpServerStub().start(new HttpServerStubConfiguration(HttpServerStubConfigurer.NoOpHttpServerStubConfigurer.INSTANCE, null,
null, SocketUtils.findAvailableTcpPort())) as WireMockHttpServerStub
null, TestSocketUtils.findAvailableTcpPort())) as WireMockHttpServerStub
when:
StubMapping mapping = mappingDescriptor.getMapping(MAPPING_DESCRIPTOR)
then:

View File

@@ -32,10 +32,10 @@ import org.springframework.cloud.contract.stubrunner.StubFinder
import org.springframework.cloud.contract.stubrunner.StubNotFoundException
import org.springframework.cloud.contract.stubrunner.provider.wiremock.WireMockHttpServerStubAccessor
import org.springframework.cloud.contract.stubrunner.provider.wiremock.WireMockHttpServerStubConfigurer
import org.springframework.cloud.test.TestSocketUtils
import org.springframework.context.annotation.Configuration
import org.springframework.core.env.Environment
import org.springframework.test.context.ActiveProfiles
import org.springframework.util.SocketUtils
/**
* @author Marcin Grzejszczak
@@ -161,7 +161,7 @@ class StubRunnerConfigurationSpec extends Specification {
@Override
WireMockConfiguration configure(WireMockConfiguration httpStubConfiguration, HttpServerStubConfiguration httpServerStubConfiguration) {
if (httpServerStubConfiguration.stubConfiguration.artifactId == "fraudDetectionServer") {
int httpsPort = SocketUtils.findAvailableTcpPort()
int httpsPort = TestSocketUtils.findAvailableTcpPort()
log.info("Will set HTTPs port [" + httpsPort + "] for fraud detection server")
return httpStubConfiguration
.httpsPort(httpsPort)

View File

@@ -66,6 +66,11 @@
<artifactId>spring-cloud-contract-wiremock</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-test-support</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>

View File

@@ -33,16 +33,16 @@ import org.springframework.cloud.contract.spec.Contract
import org.springframework.cloud.contract.verifier.dsl.wiremock.WireMockStubMapping
import org.springframework.cloud.contract.verifier.file.ContractMetadata
import org.springframework.cloud.contract.verifier.util.ContractVerifierDslConverter
import org.springframework.cloud.test.TestSocketUtils
import org.springframework.core.io.ByteArrayResource
import org.springframework.http.HttpEntity
import org.springframework.http.RequestEntity
import org.springframework.util.LinkedMultiValueMap
import org.springframework.util.MultiValueMap
import org.springframework.util.SocketUtils
class DslToWireMockClientConverterSpec extends Specification {
int port = SocketUtils.findAvailableTcpPort()
int port = TestSocketUtils.findAvailableTcpPort()
@Rule
public WireMockRule wireMockRule = new WireMockRule(port)
@Rule

View File

@@ -117,6 +117,7 @@ configure([project(':fraudDetectionService'), project(':loanApplicationService')
testImplementation "org.mockito:mockito-core"
testImplementation "org.springframework:spring-test"
testImplementation "org.springframework.boot:spring-boot-test"
testImplementation "org.springframework.cloud:spring-cloud-test-support"
testImplementation "io.rest-assured:rest-assured:$restAssuredVersion"
testImplementation "io.rest-assured:spring-mock-mvc:$restAssuredVersion"
}

View File

@@ -36,7 +36,7 @@ import org.springframework.test.context.ContextConfiguration
@Stepwise
class LoanApplicationServiceSpec extends Specification {
public static int port = org.springframework.util.SocketUtils.findAvailableTcpPort()
public static int port = org.springframework.cloud.test.TestSocketUtils.findAvailableTcpPort()
@ClassRule
@Shared

View File

@@ -36,7 +36,7 @@ import org.springframework.test.context.ContextConfiguration
@Stepwise
class LoanApplicationServiceSpec extends Specification {
public static int port = org.springframework.util.SocketUtils.findAvailableTcpPort()
public static int port = org.springframework.cloud.test.TestSocketUtils.findAvailableTcpPort()
@ClassRule
@Shared

View File

@@ -174,6 +174,11 @@
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-test-support</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>

View File

@@ -36,7 +36,7 @@ import org.springframework.cloud.contract.verifier.util.AssertionUtil
import org.springframework.cloud.contract.verifier.util.ContractVerifierDslConverter
import org.springframework.http.RequestEntity
import org.springframework.http.ResponseEntity
import org.springframework.util.SocketUtils
import org.springframework.cloud.test.TestSocketUtils
class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifier {
@@ -2075,7 +2075,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
}
'''
and:
int port = SocketUtils.findAvailableTcpPort()
int port = TestSocketUtils.findAvailableTcpPort()
WireMockServer server = new WireMockServer(config().port(port))
server.start()
and:
@@ -2118,7 +2118,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
and:
def json = toWireMockClientJsonStub(contract)
and:
int port = SocketUtils.findAvailableTcpPort()
int port = TestSocketUtils.findAvailableTcpPort()
WireMockServer server = new WireMockServer(config().port(port))
server.start()
and:
@@ -2229,7 +2229,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
and:
stubMappingIsValidWireMockStub(json)
and:
int port = SocketUtils.findAvailableTcpPort()
int port = TestSocketUtils.findAvailableTcpPort()
WireMockServer server = new WireMockServer(config().port(port))
server.start()
server.addStubMapping(WireMockStubMapping.buildFrom(json))
@@ -2397,7 +2397,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
and:
stubMappingIsValidWireMockStub(json)
and:
int port = SocketUtils.findAvailableTcpPort()
int port = TestSocketUtils.findAvailableTcpPort()
WireMockServer server = new WireMockServer(config().port(port))
server.start()
server.addStubMapping(WireMockStubMapping.buildFrom(json))
@@ -2947,7 +2947,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
wireMockStub.contains('Entity2')
stubMappingIsValidWireMockStub(wireMockStub)
and:
int port = SocketUtils.findAvailableTcpPort()
int port = TestSocketUtils.findAvailableTcpPort()
WireMockServer server = new WireMockServer(config().port(port))
server.start()
and:
@@ -3048,7 +3048,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
.toWireMockClientStub()
and:
int port = SocketUtils.findAvailableTcpPort()
int port = TestSocketUtils.findAvailableTcpPort()
WireMockServer server = new WireMockServer(config().port(port))
server.start()
and:

View File

@@ -106,6 +106,10 @@
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-test-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-verifier</artifactId>

View File

@@ -23,6 +23,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.cloud.test.TestSocketUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
@@ -30,7 +31,6 @@ import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.util.SocketUtils;
/**
* Listener that prepares the environment so that WireMock will work when it is
@@ -94,7 +94,7 @@ public class WireMockApplicationListener implements ApplicationListener<Applicat
MutablePropertySources propertySources = environment.getPropertySources();
addPropertySource(propertySources);
Map<String, Object> source = ((MapPropertySource) propertySources.get("wiremock")).getSource();
int port = SocketUtils.findAvailableTcpPort(minPort, maxPort);
int port = TestSocketUtils.findAvailableTcpPort(minPort, maxPort);
source.put(portProperty, port);
if (log.isDebugEnabled()) {
log.debug("Registered property source for property [" + portProperty + "] with value [" + port + "]");

View File

@@ -22,6 +22,11 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-stub-runner</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-test-support</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-stub-runner-jetty</artifactId>

View File

@@ -32,7 +32,7 @@ import org.springframework.cloud.zookeeper.ZookeeperProperties
import org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryClient
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.util.SocketUtils
import org.springframework.cloud.test.TestSocketUtils
import org.springframework.web.client.RestTemplate
/**