Merge junit features into main stub runner module

This commit is contained in:
Dave Syer
2016-07-15 22:03:17 +01:00
parent bbec38fa74
commit 664082d843
19 changed files with 89 additions and 176 deletions

View File

@@ -44,7 +44,6 @@ Spring Cloud Contract Stub Runner comes with a new structure of modules
└── spring-cloud-contract-stub-runner
├── spring-cloud-contract-stub-runner
├── spring-cloud-contract-stub-runner-boot
├── spring-cloud-contract-stub-runner-junit
├── spring-cloud-contract-stub-runner-spring
└── spring-cloud-contract-stub-runner-spring-cloud
----
@@ -55,8 +54,6 @@ include::{stubrunner_core_path}/spring-cloud-contract-stub-runner-boot/README.ad
include::{stubrunner_core_path}/spring-cloud-contract-stub-runner-jetty/README.adoc[]
include::{stubrunner_core_path}/spring-cloud-contract-stub-runner-junit/README.adoc[]
include::{stubrunner_core_path}/spring-cloud-contract-stub-runner-spring/README.adoc[]
include::{stubrunner_core_path}/spring-cloud-contract-stub-runner-spring-cloud/README.adoc[]

View File

@@ -20,7 +20,6 @@
<modules>
<module>spring-cloud-contract-stub-runner</module>
<module>spring-cloud-contract-stub-runner-jetty</module>
<module>spring-cloud-contract-stub-runner-junit</module>
<module>spring-cloud-contract-stub-runner-spring</module>
<module>spring-cloud-contract-stub-runner-boot</module>
<module>spring-cloud-contract-stub-runner-messaging</module>

View File

@@ -1,65 +0,0 @@
=== Stub Runner JUnit Rule
Stub Runner comes with a JUnit rule thanks to which you can very easily download and run stubs for given group and artifact id:
[source,java,indent=0]
----
include::src/test/groovy/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRuleJUnitTest.java[tags=classrule]
----
After that rule gets executed Stub Runner connects to your Maven repository and for the given list of dependencies tries to:
- download them
- cache them locally
- unzip them to a temporary folder
- start a WireMock server for each Maven dependency on a random port from the provided range of ports / provided port
- feed the WireMock server with all JSON files that are valid WireMock definitions
Stub Runner uses https://wiki.eclipse.org/Aether[Eclipse Aether] mechanism to download the Maven dependencies.
Check their https://wiki.eclipse.org/Aether[docs] for more information.
Since the `StubRunnerRule` implements the `StubFinder` it allows you to find the started stubs:
[source,groovy,indent=0]
----
include::../spring-cloud-contract-stub-runner/src/main/groovy/org/springframework/cloud/contract/stubrunner/StubFinder.groovy[]
----
Example of usage in Spock tests:
[source,groovy,indent=0]
----
include::src/test/groovy/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRuleSpec.groovy[tags=classrule]
----
Example of usage in JUnit tests:
[source,java,indent=0]
----
include::src/test/groovy/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRuleJUnitTest.java[tags=test]
----
Check the *Common properties for JUnit and Spring* for more information on how to apply global configuration of Stub Runner.
==== Providing fixed ports
You can also run your stubs on fixed ports. You can do it in two different ways. One is to pass it in the properties, and the other via fluent API of
JUnit rule.
==== Fluent API
When using the `StubRunnerRule` you can add a stub to download and then pass the port for the last downloaded stub.
[source,java,indent=0]
----
include::src/test/groovy/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRuleCustomPortJUnitTest.java[tags=classrule_with_port]
----
You can see that for this example the following test is valid:
[source,java,indent=0]
----
include::src/test/groovy/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRuleCustomPortJUnitTest.java[tags=test_with_port]
----

View File

@@ -1,64 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-stub-runner-build</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>spring-cloud-contract-stub-runner-junit</artifactId>
<packaging>jar</packaging>
<name>Spring Cloud Contract Stub Runner JUnit</name>
<description>Spring Cloud Contract Stub Runner JUnit</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-stub-runner-jetty</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.solidsoft.spock</groupId>
<artifactId>spock-global-unroll</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -1,30 +0,0 @@
<!--
~ Copyright 2013-2016 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@@ -41,17 +41,22 @@ public class StubRunnerCamelConfiguration {
return new SpringRouteBuilder() {
@Override
public void configure() throws Exception {
Map<StubConfiguration, Collection<Contract>> contracts = batchStubRunner.getContracts();
for (Collection<Contract> list : contracts.values()) {
for (Contract it : list) {
if (it.getInput()!=null && it.getInput().getMessageFrom()!=null && it.getOutputMessage()!=null && it.getOutputMessage().getSentTo()!=null) {
from(it.getInput().getMessageFrom().getClientValue())
Map<StubConfiguration, Collection<Contract>> contracts = batchStubRunner
.getContracts();
for (Collection<Contract> list : contracts.values()) {
for (Contract it : list) {
if (it.getInput() != null
&& it.getInput().getMessageFrom() != null
&& it.getOutputMessage() != null
&& it.getOutputMessage().getSentTo() != null) {
from(it.getInput().getMessageFrom().getClientValue())
.filter(new StubRunnerCamelPredicate(it))
.process(new StubRunnerCamelProcessor(it))
.to(it.getOutputMessage().getSentTo().getClientValue());
}
}
.to(it.getOutputMessage().getSentTo()
.getClientValue());
}
}
}
}
};
}

View File

@@ -63,4 +63,69 @@ Every stubbed collaborator exposes list of defined mappings under `__/admin/` en
===== Messaging Stubs
Depending on the provided Stub Runner dependency and the DSL the messaging routes are automatically set up.
Depending on the provided Stub Runner dependency and the DSL the messaging routes are automatically set up.
=== Stub Runner JUnit Rule
Stub Runner comes with a JUnit rule thanks to which you can very easily download and run stubs for given group and artifact id:
[source,java,indent=0]
----
include::src/test/groovy/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRuleJUnitTest.java[tags=classrule]
----
After that rule gets executed Stub Runner connects to your Maven repository and for the given list of dependencies tries to:
- download them
- cache them locally
- unzip them to a temporary folder
- start a WireMock server for each Maven dependency on a random port from the provided range of ports / provided port
- feed the WireMock server with all JSON files that are valid WireMock definitions
Stub Runner uses https://wiki.eclipse.org/Aether[Eclipse Aether] mechanism to download the Maven dependencies.
Check their https://wiki.eclipse.org/Aether[docs] for more information.
Since the `StubRunnerRule` implements the `StubFinder` it allows you to find the started stubs:
[source,groovy,indent=0]
----
include::../spring-cloud-contract-stub-runner/src/main/groovy/org/springframework/cloud/contract/stubrunner/StubFinder.groovy[]
----
Example of usage in Spock tests:
[source,groovy,indent=0]
----
include::src/test/groovy/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRuleSpec.groovy[tags=classrule]
----
Example of usage in JUnit tests:
[source,java,indent=0]
----
include::src/test/groovy/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRuleJUnitTest.java[tags=test]
----
Check the *Common properties for JUnit and Spring* for more information on how to apply global configuration of Stub Runner.
==== Providing fixed ports
You can also run your stubs on fixed ports. You can do it in two different ways. One is to pass it in the properties, and the other via fluent API of
JUnit rule.
==== Fluent API
When using the `StubRunnerRule` you can add a stub to download and then pass the port for the last downloaded stub.
[source,java,indent=0]
----
include::src/test/groovy/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRuleCustomPortJUnitTest.java[tags=classrule_with_port]
----
You can see that for this example the following test is valid:
[source,java,indent=0]
----
include::src/test/groovy/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRuleCustomPortJUnitTest.java[tags=test_with_port]
----

View File

@@ -34,10 +34,6 @@
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
</dependency>
<dependency>
<groupId>args4j</groupId>
<artifactId>args4j</artifactId>
@@ -66,6 +62,10 @@
<groupId>org.apache.maven</groupId>
<artifactId>maven-aether-provider</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>

View File

@@ -63,6 +63,8 @@ class StubRunnerExecutorSpec extends Specification {
executor.runStubs(stubRunnerOptions, repository, stub)
then:
!executor.findStubUrl("unkowngroup", "unknownartifact")
cleanup:
executor.shutdown()
}
def 'should start a stub on a given port'() {
@@ -74,6 +76,8 @@ class StubRunnerExecutorSpec extends Specification {
executor.runStubs(stubRunnerOptions, repository, stub)
then:
executor.findStubUrl("group", "artifact") == 'http://localhost:12345'.toURL()
cleanup:
executor.shutdown()
}
def 'should ensure that triggered contracts have properly parsed message body when a message is sent'() {
@@ -84,6 +88,8 @@ class StubRunnerExecutorSpec extends Specification {
executor.trigger('send_order')
then:
noExceptionThrown()
cleanup:
executor.shutdown()
}
Map<StubConfiguration, Integer> stubIdsWithPortsFromString(String stubIdsToPortMapping) {