From 1923e6b8902af9dad91cff1e585775a32dd0a352 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Wed, 7 Dec 2016 13:41:39 +0100 Subject: [PATCH] Added a test with Moco as a stub provider --- pom.xml | 5 ++ spring-cloud-contract-stub-runner/pom.xml | 1 - tests/pom.xml | 1 + .../pom.xml | 78 ++++++++++++++++++ .../provider/moco/MocoHttpServerStub.groovy | 64 ++++++++++++++ .../moco/MocoHttpServerStubSpec.groovy | 36 ++++++++ .../test/resources/META-INF/spring.factories | 2 + .../test/resources/application.yml | 5 ++ .../test/resources/logback.xml | 25 ++++++ ...tectionServerMoco-0.0.1-SNAPSHOT-stubs.jar | Bin 0 -> 906 bytes ...raudDetectionServerMoco-0.0.1-SNAPSHOT.pom | 25 ++++++ .../maven-metadata.xml | 28 +++++++ 12 files changed, 269 insertions(+), 1 deletion(-) create mode 100644 tests/spring-cloud-contract-stub-runner-moco/pom.xml create mode 100644 tests/spring-cloud-contract-stub-runner-moco/test/groovy/org/springframework/cloud/contract/stubrunner/provider/moco/MocoHttpServerStub.groovy create mode 100644 tests/spring-cloud-contract-stub-runner-moco/test/groovy/org/springframework/cloud/contract/stubrunner/provider/moco/MocoHttpServerStubSpec.groovy create mode 100644 tests/spring-cloud-contract-stub-runner-moco/test/resources/META-INF/spring.factories create mode 100644 tests/spring-cloud-contract-stub-runner-moco/test/resources/application.yml create mode 100644 tests/spring-cloud-contract-stub-runner-moco/test/resources/logback.xml create mode 100644 tests/spring-cloud-contract-stub-runner-moco/test/resources/m2repo/repository/org/springframework/cloud/contract/verifier/stubs/fraudDetectionServerMoco/0.0.1-SNAPSHOT/fraudDetectionServerMoco-0.0.1-SNAPSHOT-stubs.jar create mode 100644 tests/spring-cloud-contract-stub-runner-moco/test/resources/m2repo/repository/org/springframework/cloud/contract/verifier/stubs/fraudDetectionServerMoco/0.0.1-SNAPSHOT/fraudDetectionServerMoco-0.0.1-SNAPSHOT.pom create mode 100644 tests/spring-cloud-contract-stub-runner-moco/test/resources/m2repo/repository/org/springframework/cloud/contract/verifier/stubs/fraudDetectionServerMoco/maven-metadata.xml diff --git a/pom.xml b/pom.xml index 2556a94a19..d61f83f098 100644 --- a/pom.xml +++ b/pom.xml @@ -122,6 +122,11 @@ mockito-core 1.10.19 + + io.specto + hoverfly-junit + 0.1.8 + org.springframework.cloud spring-cloud-contract-dependencies diff --git a/spring-cloud-contract-stub-runner/pom.xml b/spring-cloud-contract-stub-runner/pom.xml index 7e836a5d7c..baf3c86c5f 100644 --- a/spring-cloud-contract-stub-runner/pom.xml +++ b/spring-cloud-contract-stub-runner/pom.xml @@ -97,7 +97,6 @@ io.specto hoverfly-junit - 0.1.8 test diff --git a/tests/pom.xml b/tests/pom.xml index 416a35d922..5bbd020ba0 100644 --- a/tests/pom.xml +++ b/tests/pom.xml @@ -27,6 +27,7 @@ spring-cloud-contract-stub-runner-boot-zookeeper spring-cloud-contract-stub-runner-camel spring-cloud-contract-stub-runner-context-path + spring-cloud-contract-stub-runner-moco spring-cloud-contract-stub-runner-integration spring-cloud-contract-stub-runner-stream spring-cloud-contract-stub-runner-amqp diff --git a/tests/spring-cloud-contract-stub-runner-moco/pom.xml b/tests/spring-cloud-contract-stub-runner-moco/pom.xml new file mode 100644 index 0000000000..64cdd42817 --- /dev/null +++ b/tests/spring-cloud-contract-stub-runner-moco/pom.xml @@ -0,0 +1,78 @@ + + + 4.0.0 + + org.springframework.cloud + spring-cloud-contract-tests + 1.1.0.BUILD-SNAPSHOT + .. + + spring-cloud-contract-stub-runner-moco + jar + Spring Cloud Contract Stub Runner Moco + Spring Cloud Contract Stub Runner Moco + + 1.8 + + + + org.springframework.cloud + spring-cloud-contract-stub-runner + + + com.github.dreamhead + moco-runner + 0.11.0 + + + org.springframework.cloud + spring-cloud-contract-verifier + + + junit + junit + test + + + org.spockframework + spock-core + test + + + org.spockframework + spock-spring + test + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-starter-web + test + + + info.solidsoft.spock + spock-global-unroll + test + + + + + + org.codehaus.gmavenplus + gmavenplus-plugin + + + + testCompile + + + + + + + diff --git a/tests/spring-cloud-contract-stub-runner-moco/test/groovy/org/springframework/cloud/contract/stubrunner/provider/moco/MocoHttpServerStub.groovy b/tests/spring-cloud-contract-stub-runner-moco/test/groovy/org/springframework/cloud/contract/stubrunner/provider/moco/MocoHttpServerStub.groovy new file mode 100644 index 0000000000..2ea0a8f77f --- /dev/null +++ b/tests/spring-cloud-contract-stub-runner-moco/test/groovy/org/springframework/cloud/contract/stubrunner/provider/moco/MocoHttpServerStub.groovy @@ -0,0 +1,64 @@ +package org.springframework.cloud.contract.stubrunner.provider.moco + +import com.github.dreamhead.moco.bootstrap.arg.HttpArgs +import com.github.dreamhead.moco.runner.JsonRunner +import org.springframework.cloud.contract.stubrunner.HttpServerStub +import org.springframework.util.SocketUtils + +/** + * @author Marcin Grzejszczak + */ +class MocoHttpServerStub implements HttpServerStub { + + private boolean started + private JsonRunner runner + private int port + + @Override + int port() { + if (!isRunning()) { + return -1 + } + return port + } + + @Override + boolean isRunning() { + return started + } + + @Override + HttpServerStub start() { + return start(SocketUtils.findAvailableTcpPort()) + } + + @Override + HttpServerStub start(int port) { + this.port = port + return this + } + + @Override + HttpServerStub stop() { + if (!isRunning()) { + return this + } + this.runner.stop() + return this + } + + @Override + HttpServerStub registerMappings(Collection stubFiles) { + List streams = stubFiles.collect { it.newInputStream() } + this.runner = JsonRunner.newJsonRunnerWithStreams(streams, + HttpArgs.httpArgs().withPort(this.port).build()) + this.runner.run() + this.started = true + return this + } + + @Override + boolean isAccepted(File file) { + return file.name.endsWith(".json") + } +} diff --git a/tests/spring-cloud-contract-stub-runner-moco/test/groovy/org/springframework/cloud/contract/stubrunner/provider/moco/MocoHttpServerStubSpec.groovy b/tests/spring-cloud-contract-stub-runner-moco/test/groovy/org/springframework/cloud/contract/stubrunner/provider/moco/MocoHttpServerStubSpec.groovy new file mode 100644 index 0000000000..951ac9a055 --- /dev/null +++ b/tests/spring-cloud-contract-stub-runner-moco/test/groovy/org/springframework/cloud/contract/stubrunner/provider/moco/MocoHttpServerStubSpec.groovy @@ -0,0 +1,36 @@ +package org.springframework.cloud.contract.stubrunner.provider.moco + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.autoconfigure.EnableAutoConfiguration +import org.springframework.boot.test.context.SpringBootContextLoader +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.cloud.contract.stubrunner.StubFinder +import org.springframework.cloud.contract.stubrunner.spring.AutoConfigureStubRunner +import org.springframework.context.annotation.Configuration +import org.springframework.test.annotation.DirtiesContext +import org.springframework.test.context.ContextConfiguration +import spock.lang.Specification + +/** + * @author Marcin Grzejszczak + */ +@ContextConfiguration(classes = Config, loader = SpringBootContextLoader) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) +@AutoConfigureStubRunner( ids = + ["org.springframework.cloud.contract.verifier.stubs:fraudDetectionServerMoco"], + repositoryRoot = "classpath:m2repo/repository/") +@DirtiesContext +class MocoHttpServerStubSpec extends Specification { + + @Autowired StubFinder stubFinder + + def 'should successfully receive a response from a stub'() { + expect: + "${stubFinder.findStubUrl('fraudDetectionServerMoco').toString()}/name".toURL().text == 'fraudDetectionServerMoco' + } + + @Configuration + @EnableAutoConfiguration + static class Config { + } +} diff --git a/tests/spring-cloud-contract-stub-runner-moco/test/resources/META-INF/spring.factories b/tests/spring-cloud-contract-stub-runner-moco/test/resources/META-INF/spring.factories new file mode 100644 index 0000000000..a48f1e4078 --- /dev/null +++ b/tests/spring-cloud-contract-stub-runner-moco/test/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.cloud.contract.stubrunner.HttpServerStub=\ +org.springframework.cloud.contract.stubrunner.provider.moco.MocoHttpServerStub \ No newline at end of file diff --git a/tests/spring-cloud-contract-stub-runner-moco/test/resources/application.yml b/tests/spring-cloud-contract-stub-runner-moco/test/resources/application.yml new file mode 100644 index 0000000000..9752093711 --- /dev/null +++ b/tests/spring-cloud-contract-stub-runner-moco/test/resources/application.yml @@ -0,0 +1,5 @@ +stubrunner: + idsToServiceIds: + fraudDetectionServerMoco: someNameThatShouldMapFraudDetectionServer +server: + port: 0 \ No newline at end of file diff --git a/tests/spring-cloud-contract-stub-runner-moco/test/resources/logback.xml b/tests/spring-cloud-contract-stub-runner-moco/test/resources/logback.xml new file mode 100644 index 0000000000..df6a2daa72 --- /dev/null +++ b/tests/spring-cloud-contract-stub-runner-moco/test/resources/logback.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/tests/spring-cloud-contract-stub-runner-moco/test/resources/m2repo/repository/org/springframework/cloud/contract/verifier/stubs/fraudDetectionServerMoco/0.0.1-SNAPSHOT/fraudDetectionServerMoco-0.0.1-SNAPSHOT-stubs.jar b/tests/spring-cloud-contract-stub-runner-moco/test/resources/m2repo/repository/org/springframework/cloud/contract/verifier/stubs/fraudDetectionServerMoco/0.0.1-SNAPSHOT/fraudDetectionServerMoco-0.0.1-SNAPSHOT-stubs.jar new file mode 100644 index 0000000000000000000000000000000000000000..f2153d4cc94b54e441810bb941e6d272fc56e94f GIT binary patch literal 906 zcmWIWW@h1HVBp|jSmxB}!2kqIAOZ+Df!NnI#8KDN&rP41Apk|;rh2A#(m(~0KrDi+ z(AUw=)6F$FM9YJaOuOAx1$-rFw z%{>ByODnh;7+JnEGBB`!91Zdkh~{9h&2IP9UGe*WF_5UZp(TE1s zM-UB4N40bcC)g9e8LKZQ-PM; I2U)}b0FXHFq5uE@ literal 0 HcmV?d00001 diff --git a/tests/spring-cloud-contract-stub-runner-moco/test/resources/m2repo/repository/org/springframework/cloud/contract/verifier/stubs/fraudDetectionServerMoco/0.0.1-SNAPSHOT/fraudDetectionServerMoco-0.0.1-SNAPSHOT.pom b/tests/spring-cloud-contract-stub-runner-moco/test/resources/m2repo/repository/org/springframework/cloud/contract/verifier/stubs/fraudDetectionServerMoco/0.0.1-SNAPSHOT/fraudDetectionServerMoco-0.0.1-SNAPSHOT.pom new file mode 100644 index 0000000000..4ac46b5839 --- /dev/null +++ b/tests/spring-cloud-contract-stub-runner-moco/test/resources/m2repo/repository/org/springframework/cloud/contract/verifier/stubs/fraudDetectionServerMoco/0.0.1-SNAPSHOT/fraudDetectionServerMoco-0.0.1-SNAPSHOT.pom @@ -0,0 +1,25 @@ + + + + + 4.0.0 + org.springframework.cloud.contract.verifier.stubs + fraudDetectionServerMoco + 0.0.1-SNAPSHOT + pom + diff --git a/tests/spring-cloud-contract-stub-runner-moco/test/resources/m2repo/repository/org/springframework/cloud/contract/verifier/stubs/fraudDetectionServerMoco/maven-metadata.xml b/tests/spring-cloud-contract-stub-runner-moco/test/resources/m2repo/repository/org/springframework/cloud/contract/verifier/stubs/fraudDetectionServerMoco/maven-metadata.xml new file mode 100644 index 0000000000..84801f46e2 --- /dev/null +++ b/tests/spring-cloud-contract-stub-runner-moco/test/resources/m2repo/repository/org/springframework/cloud/contract/verifier/stubs/fraudDetectionServerMoco/maven-metadata.xml @@ -0,0 +1,28 @@ + + + + + org.springframework.cloud.contract.verifier.stubs + fraudDetectionServerMoco + 0.0.1-SNAPSHOT + + + 0.0.1-SNAPSHOT + + 20160409062112 + +