470 lines
19 KiB
Plaintext
470 lines
19 KiB
Plaintext
// Do not edit this file (e.g. go instead to src/main/asciidoc)
|
|
|
|
image::https://badges.gitter.im/Join%20Chat.svg[Gitter, link="https://gitter.im/spring-cloud/spring-cloud-contract?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge"]
|
|
image::https://codecov.io/gh/spring-cloud/spring-cloud-contract/branch/master/graph/badge.svg["codecov", link="https://codecov.io/gh/spring-cloud/spring-cloud-contract"]
|
|
image::https://circleci.com/gh/spring-cloud/spring-cloud-contract.svg?style=svg["CircleCI", link="https://circleci.com/gh/spring-cloud/spring-cloud-contract"]
|
|
|
|
== Spring Cloud Contract
|
|
|
|
What you always need it confidence in pushing new features into a new application or service in a distributed system.
|
|
This project provides support for Consumer Driven Contracts and service schemas in Spring applications, covering a
|
|
range of options for writing tests, publishing them as assets, asserting that a contract is kept by producers
|
|
and consumers, for HTTP and message-based interactions.
|
|
|
|
=== Spring Cloud Contract WireMock
|
|
|
|
Modules giving you the possibility to use http://wiremock.org[WireMock] with different servers. Check out the
|
|
https://github.com/spring-cloud/spring-cloud-contract/tree/master/samples[samples] for more information.
|
|
|
|
Currently we support Jetty, Native WireMock server, Tomcat and Undertow.
|
|
|
|
=== Spring Cloud Contract Verifier
|
|
|
|
=== Introduction
|
|
|
|
IMPORTANT: http://codearte.github.io/accurest[The documentation to the deprecated Accurest project in version 1.1.0 is available here.]
|
|
|
|
TIP: The Accurest project was initially started by Marcin Grzejszczak and Jakub Kubrynski (http://codearte.io[codearte.io])
|
|
|
|
Just to make long story short - Spring Cloud Contract Verifier is a tool that enables Consumer Driven Contract (CDC) development of JVM-based applications. It is shipped
|
|
with __Contract Definition Language__ (DSL). Contract definitions are used to produce following resources:
|
|
|
|
* JSON stub definitions to be used by Wiremock when doing integration testing on the client code (__client tests__).
|
|
Test code must still be written by hand, test data is produced by Spring Cloud Contract Verifier.
|
|
* Messaging routes if you're using one. We're integrating with Spring Integration, Spring Cloud Stream and Apache Camel. You can however set your own integrations if you want to
|
|
* Acceptance tests (in JUnit or Spock) used to verify if server-side implementation of the API is compliant with the contract (__server tests__).
|
|
Full test is generated by Spring Cloud Contract Verifier.
|
|
|
|
Spring Cloud Contract Verifier moves TDD to the level of software architecture.
|
|
|
|
==== Why?
|
|
|
|
Let us assume that we have a system comprising of multiple microservices:
|
|
|
|
image::https://raw.githubusercontent.com/spring-cloud/spring-cloud-contract/master/docs/src/main/asciidoc/images/Deps.png[Microservices Architecture]
|
|
|
|
===== Testing issues
|
|
|
|
If we wanted to test the application in top left corner if it can communicate with other services then we could do one of two things:
|
|
|
|
- deploy all microservices and perform end to end tests
|
|
- mock other microservices in unit / integration tests
|
|
|
|
Both have their advantages but also a lot of disadvantages. Let's focus on the latter.
|
|
|
|
*Deploy all microservices and perform end to end tests*
|
|
|
|
Advantages:
|
|
|
|
- simulates production
|
|
- tests real communication between services
|
|
|
|
Disadvantages:
|
|
|
|
- to test one microservice we would have to deploy 6 microservices, a couple of databases etc.
|
|
- the environment where the tests would be conducted would be locked for a single suite of tests (i.e. nobody else would be able to run the tests in the meantime).
|
|
- long to run
|
|
- very late feedback
|
|
- extremely hard to debug
|
|
|
|
*Mock other microservices in unit / integration tests*
|
|
|
|
Advantages:
|
|
|
|
- very fast feedback
|
|
- no infrastructure requirements
|
|
|
|
Disadvantages:
|
|
|
|
- the implementor of the service creates stubs thus they might have nothing to do with the reality
|
|
- you can go to production with passing tests and failing production
|
|
|
|
To solve the aforementioned issues Spring Cloud Contract Verifier with Stub Runner were created. Their main idea is to give you very fast feedback, without the need
|
|
to set up the whole world of microservices.
|
|
|
|
image::https://raw.githubusercontent.com/spring-cloud/spring-cloud-contract/master/docs/src/main/asciidoc/images/Stubs1.png[Stubbed Services]
|
|
|
|
If you work on stubs then the only applications you need are those that your application is using directly.
|
|
|
|
image::https://raw.githubusercontent.com/spring-cloud/spring-cloud-contract/master/docs/src/main/asciidoc/images/Stubs2.png[Stubbed Services]
|
|
|
|
Spring Cloud Contract Verifier gives you the certainty that the stubs that you're using were created by the service that you're calling. Also if you can use them it means that they were
|
|
tested against the producer's side. In other words - you can trust those stubs.
|
|
|
|
|
|
==== Purposes
|
|
|
|
The main purposes of Spring Cloud Contract Verifier with Stub Runner are:
|
|
|
|
- to ensure that WireMock / Messaging stubs (used when developing the client) are doing exactly what actual server-side implementation will do,
|
|
- to promote ATDD method and Microservices architectural style,
|
|
- to provide a way to publish changes in contracts that are immediately visible on both sides,
|
|
- to generate boilerplate test code used on the server side.
|
|
|
|
==== Client Side
|
|
|
|
During the tests you want to have a Wiremock instance / Messaging route up and running that simulates the service Y.
|
|
You would like to feed that instance with a proper stub definition. That stub definition would need
|
|
to be valid and should also be reusable on the server side.
|
|
|
|
__Summing it up:__ On this side, in the stub definition, you can use patterns for request stubbing and you need exact
|
|
values for responses.
|
|
|
|
==== Server Side
|
|
|
|
Being a service Y since you are developing your stub, you need to be sure that it's actually resembling your
|
|
concrete implementation. You can't have a situation where your stub acts in one way and your application on
|
|
production behaves in a different way.
|
|
|
|
That's why from the provided stub acceptance tests will be generated that will ensure
|
|
that your application behaves in the same way as you define in your stub.
|
|
|
|
__Summing it up:__ On this side, in the stub definition, you need exact values as request and can use patterns/methods
|
|
for response verification.
|
|
|
|
==== Step by step guide to CDC
|
|
|
|
Let's take an example of Fraud Detection and Loan Issuance process. Let's assume that the Loan Issuance is a client to the
|
|
Fraud Detection server. Let's assume that we have to write a new feature (if a client wants too borrow too much money then
|
|
we mark him as fraud). The current implementation grants loan to everybody.
|
|
|
|
Of course both client and server development teams need to communicate directly and discuss changes while
|
|
going through the process. CDC is all about communication.
|
|
|
|
The code is available under the `samples/samples-standalone/http-server` and `samples/samples-standalone/http-client` folders.
|
|
|
|
As a developer of the Loan Issuance service (a consumer to the Fraud Detection server):
|
|
|
|
- start doing TDD by writing a test to your feature
|
|
- at some point in time you need to send a request to the Fraud Detection service
|
|
- you clone the Fraud Detection service repository locally
|
|
- you define the contract locally in the repo of Fraud Detection service
|
|
- provide the group id and artifact id for the Spring Cloud Contract Stub Runner to download stubs of your collaborators
|
|
- also provide the offline work switch since you don't want to work offline
|
|
- when you need to install the stubs of the server side you execute e.g. `./mvnw clean install -DskipTests` in their repo
|
|
- when you're happy with the contract and your test passes publish a PR to the server side
|
|
|
|
As a developer of the Fraud Detection server (a server to the Loan Issuance service):
|
|
|
|
- take over the PR (if you try to build it your app will break cause you have a missing implementation)
|
|
- write the missing implementation
|
|
- deploy your app with the stubs e.g. `./mvnw clean deploy` you'll publish both the application fat jar and the stub jar
|
|
|
|
As a developer of the Loan Issuance service (a consumer to the Fraud Detection server):
|
|
|
|
- since the server side work was done you can merge the branch to master
|
|
- disable the offline work for Spring Cloud Contract Stub Runner
|
|
- at this moment the stubs of the server side will be automatically downloaded from Nexus / Artifactory
|
|
|
|
Code examples:
|
|
|
|
*CLIENT*
|
|
|
|
*start doing TDD*
|
|
|
|
[source,groovy,indent=0]
|
|
----
|
|
def 'should be rejected due to abnormal loan amount'() {
|
|
given:
|
|
LoanApplication application =
|
|
new LoanApplication(client: new Client(pesel: '1234567890'), amount: 99_999)
|
|
when:
|
|
LoanApplicationResult loanApplication = sut.loanApplication(application)
|
|
then:
|
|
loanApplication.loanApplicationStatus == LoanApplicationStatus.LOAN_APPLICATION_REJECTED
|
|
loanApplication.rejectionReason == 'Amount too high'
|
|
}
|
|
----
|
|
|
|
*at some point in time you need to send a request to the Fraud Detection service*
|
|
|
|
[source,groovy,indent=0]
|
|
----
|
|
ResponseEntity<FraudServiceResponse> response =
|
|
restTemplate.exchange("http://localhost:" + port + "/fraudcheck", HttpMethod.PUT,
|
|
new HttpEntity<>(request, httpHeaders),
|
|
FraudServiceResponse.class);
|
|
----
|
|
|
|
*you define the contract locally in the repo of Fraud Detection service*
|
|
|
|
[source,groovy,indent=0]
|
|
----
|
|
package contracts
|
|
|
|
org.springframework.cloud.contract.spec.Contract.make {
|
|
request {
|
|
method 'PUT'
|
|
url '/fraudcheck'
|
|
body("""
|
|
{
|
|
"clientPesel":"${value(client(regex('[0-9]{10}')), server('1234567890'))}",
|
|
"loanAmount":99999}
|
|
"""
|
|
)
|
|
headers {
|
|
header('Content-Type', 'application/vnd.fraud.v1+json')
|
|
}
|
|
|
|
}
|
|
response {
|
|
status 200
|
|
body( """{
|
|
"fraudCheckStatus": "${value(client('FRAUD'), server(regex('[A-Z]{5}')))}",
|
|
"rejectionReason": "Amount too high"
|
|
}""")
|
|
headers {
|
|
header('Content-Type': value(server(regex('application/vnd.fraud.v1.json.*')), client('application/vnd.fraud.v1+json')))
|
|
}
|
|
}
|
|
|
|
}
|
|
----
|
|
|
|
*provide the group id and artifact id for the Spring Cloud Contract Stub Runner to download stubs of your collaborators*
|
|
*also provide the offline work switch since you don't want to work offline*
|
|
|
|
[source,groovy,indent=0]
|
|
----
|
|
stubrunner:
|
|
work-offline: true
|
|
stubs.ids: 'com.example:http-server:+:stubs:8080'
|
|
----
|
|
|
|
*SERVER*
|
|
|
|
*take over the PR (if you try to build it your app will break cause you have a missing implementation)*
|
|
|
|
the API
|
|
|
|
[source,java,indent=0]
|
|
----
|
|
@RequestMapping(
|
|
value = "/fraudcheck",
|
|
method = PUT,
|
|
consumes = FRAUD_SERVICE_JSON_VERSION_1,
|
|
produces = FRAUD_SERVICE_JSON_VERSION_1)
|
|
public FraudCheckResult fraudCheck(@RequestBody FraudCheck fraudCheck) {
|
|
}
|
|
----
|
|
|
|
the initial impl
|
|
|
|
[source,java,indent=0]
|
|
----
|
|
@RequestMapping(
|
|
value = "/fraudcheck",
|
|
method = PUT,
|
|
consumes = FRAUD_SERVICE_JSON_VERSION_1,
|
|
produces = FRAUD_SERVICE_JSON_VERSION_1)
|
|
public FraudCheckResult fraudCheck(@RequestBody FraudCheck fraudCheck) {
|
|
return new FraudCheckResult(FraudCheckStatus.OK, NO_REASON);
|
|
}
|
|
----
|
|
|
|
*write the missing implementation*
|
|
|
|
[source,java,indent=0]
|
|
----
|
|
@RequestMapping(
|
|
value = "/fraudcheck",
|
|
method = PUT,
|
|
consumes = FRAUD_SERVICE_JSON_VERSION_1,
|
|
produces = FRAUD_SERVICE_JSON_VERSION_1)
|
|
public FraudCheckResult fraudCheck(@RequestBody FraudCheck fraudCheck) {
|
|
if (amountGreaterThanThreshold(fraudCheck)) {
|
|
return new FraudCheckResult(FraudCheckStatus.FRAUD, AMOUNT_TOO_HIGH);
|
|
}
|
|
return new FraudCheckResult(FraudCheckStatus.OK, NO_REASON);
|
|
}
|
|
----
|
|
|
|
==== Dependencies
|
|
|
|
Spring Cloud Contract Verifier and Stub Runner are using the following libraries
|
|
|
|
- http://wiremock.org/[WireMock]
|
|
- https://github.com/jayway/JsonPath[Jayway JSONPath]
|
|
- https://github.com/marcingrzejszczak/jsonassert[JSONAssert from Marcin Grzejszczak]
|
|
|
|
==== Additional links
|
|
|
|
Below you can find some resources related to Spring Cloud Contract Verifier and Stub Runner. Note that some can be outdated since the Spring Cloud Contract Verifier project
|
|
is under constant development.
|
|
|
|
===== Videos
|
|
|
|
*Marcin Grzejszczak and Jakub Kubryński talking about Spring Cloud Contract Verifier*
|
|
|
|
video::msRFcQM07-Y[youtube]
|
|
|
|
https://www.youtube.com/watch?v=msRFcQM07-Y[click here to see the video]
|
|
|
|
*Olga Maciaszek-Sharma talking about Accurest (Spring Cloud Contract Verifier predecessor)*
|
|
|
|
video::daafmTYFoDU[youtube]
|
|
|
|
https://www.youtube.com/watch?v=daafmTYFoDU[click here to see the video]
|
|
|
|
*Marcin Grzejszczak and Jakub Kubryński talking about Accurest (Spring Cloud Contract Verifier predecessor)*
|
|
|
|
video::130779882[vimeo]
|
|
|
|
https://vimeo.com/130779882[click here to see the video]
|
|
|
|
===== Readings
|
|
|
|
- http://www.slideshare.net/MarcinGrzejszczak/stick-to-the-rules-consumer-driven-contracts-201507-confitura[Slides from Marcin Grzejszczak's talk about Accurest]
|
|
- http://toomuchcoding.com/blog/categories/accurest/[Accurest related articles from Marcin Grzejszczak's blog]
|
|
- http://toomuchcoding.com/blog/categories/spring-cloud-contract/[Spring Cloud Contract related articles from Marcin Grzejszczak's blog]
|
|
|
|
==== Samples
|
|
|
|
Here you can find some https://github.com/spring-cloud/spring-cloud-contract/tree/master/samples/[samples].
|
|
|
|
==== Why use Spring Cloud Contract Verifier and not X ?
|
|
|
|
For the time being Spring Cloud Contract Verifier is a JVM based tool. So it could be your first pick when you're already creating
|
|
software for the JVM. This project has a lot of really interesting features but especially quite a few of them definitely make
|
|
Spring Cloud Contract Verifier stand out on the "market" of Consumer Driven Contract (CDC) tooling. Out of many the most interesting are:
|
|
|
|
- Possibility to do CDC with messaging
|
|
- Clear and easy to use, statically typed DSL
|
|
- Possibility to copy paste your current JSON file to the contract and only edit its elements
|
|
- Automatic generation of tests from the defined Contract
|
|
- Stub Runner functionality - the stubs are automatically downloaded at runtime from Nexus / Artifactory
|
|
- Spring Cloud integration - no discovery service is needed for integration tests
|
|
|
|
=== Links
|
|
|
|
Here you can find interesting links related to Spring Cloud Contract Verifier:
|
|
|
|
- https://github.com/spring-cloud/spring-cloud-contract/[Spring Cloud Contract Verifier Github Repository]
|
|
- https://cloud.spring.io/spring-cloud-contract/spring-cloud-contract.html[Spring Cloud Contract Verifier Documentation]
|
|
- https://cloud.spring.io/spring-cloud-contract/spring-cloud-contract.html/deprecated[Accurest Legacy Documentation]
|
|
- https://cloud.spring.io/spring-cloud-contract/spring-cloud-contract.html/#spring-cloud-contract-stub-runner[Spring Cloud Contract Stub Runner Documentation]
|
|
- https://cloud.spring.io/spring-cloud-contract/spring-cloud-contract.html/#stub-runner-for-messaging[Spring Cloud Contract Stub Runner Messaging Documentation]
|
|
- https://gitter.im/spring-cloud/spring-cloud-contract[Spring Cloud Contract Verifier Gitter]
|
|
- https://github.com/spring-cloud/spring-cloud-contract[Spring Cloud Contract Verifier Maven Plugin]
|
|
|
|
== Documentation
|
|
|
|
You can read more about Spring Cloud Contract Verifier by reading the {documentation_url}[docs]
|
|
|
|
== Contributing
|
|
|
|
Spring Cloud is released under the non-restrictive Apache 2.0 license,
|
|
and follows a very standard Github development process, using Github
|
|
tracker for issues and merging pull requests into master. If you want
|
|
to contribute even something trivial please do not hesitate, but
|
|
follow the guidelines below.
|
|
|
|
=== Sign the Contributor License Agreement
|
|
Before we accept a non-trivial patch or pull request we will need you to sign the
|
|
https://cla.pivotal.io/sign/spring[Contributor License Agreement].
|
|
Signing the contributor's agreement does not grant anyone commit rights to the main
|
|
repository, but it does mean that we can accept your contributions, and you will get an
|
|
author credit if we do. Active contributors might be asked to join the core team, and
|
|
given the ability to merge pull requests.
|
|
|
|
=== Code of Conduct
|
|
This project adheres to the Contributor Covenant https://github.com/spring-cloud/spring-cloud-build/blob/master/docs/src/main/asciidoc/code-of-conduct.adoc[code of
|
|
conduct]. By participating, you are expected to uphold this code. Please report
|
|
unacceptable behavior to spring-code-of-conduct@pivotal.io.
|
|
|
|
=== Code Conventions and Housekeeping
|
|
None of these is essential for a pull request, but they will all help. They can also be
|
|
added after the original pull request but before a merge.
|
|
|
|
* Use the Spring Framework code format conventions. If you use Eclipse
|
|
you can import formatter settings using the
|
|
`eclipse-code-formatter.xml` file from the
|
|
https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-dependencies-parent/eclipse-code-formatter.xml[Spring
|
|
Cloud Build] project. If using IntelliJ, you can use the
|
|
http://plugins.jetbrains.com/plugin/6546[Eclipse Code Formatter
|
|
Plugin] to import the same file.
|
|
* Make sure all new `.java` files to have a simple Javadoc class comment with at least an
|
|
`@author` tag identifying you, and preferably at least a paragraph on what the class is
|
|
for.
|
|
* Add the ASF license header comment to all new `.java` files (copy from existing files
|
|
in the project)
|
|
* Add yourself as an `@author` to the .java files that you modify substantially (more
|
|
than cosmetic changes).
|
|
* Add some Javadocs and, if you change the namespace, some XSD doc elements.
|
|
* A few unit tests would help a lot as well -- someone has to do it.
|
|
* If no-one else is using your branch, please rebase it against the current master (or
|
|
other target branch in the main project).
|
|
* When writing a commit message please follow http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html[these conventions],
|
|
if you are fixing an existing issue please add `Fixes gh-XXXX` at the end of the commit
|
|
message (where XXXX is the issue number).
|
|
|
|
== How to build it
|
|
|
|
=== Project structure
|
|
|
|
Here you can find the Spring Cloud Contract folder structure
|
|
|
|
```
|
|
├── samples
|
|
├── scripts
|
|
├── spring-cloud-contract-spec
|
|
├── spring-cloud-contract-stub-runner
|
|
├── spring-cloud-contract-tools
|
|
└── spring-cloud-contract-verifier
|
|
```
|
|
|
|
- `samples` - folder contains test samples together with standalone ones used also to build documentation
|
|
- `scripts` - contains scripts to build and test `Spring Cloud Contract` with Maven, Gradle and standalone projects
|
|
- `spring-cloud-contract-spec` - contains specification modules (contains concept of a Contract)
|
|
- `spring-cloud-contract-stub-runner` - contains Stub Runner related modules
|
|
- `spring-cloud-contract-tools` - Gradle and Maven plugin for `Spring Cloud Contract Verifier`
|
|
- `spring-cloud-contract-verifier` - core of the `Spring Cloud Contract Verifier` functionality
|
|
|
|
=== Commands
|
|
|
|
To build the core functionality together with Maven Plugin you can run
|
|
|
|
```
|
|
./mvnw clean install
|
|
```
|
|
|
|
To build the Gradle Plugin
|
|
|
|
```
|
|
cd spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin
|
|
./gradlew clean build
|
|
```
|
|
|
|
=== Scripts
|
|
|
|
For your convenience we have created a bunch of scripts to build and test the whole functionality.
|
|
|
|
Build both Maven and Gradle project
|
|
|
|
```
|
|
./scripts/build.sh
|
|
```
|
|
|
|
Run tests on the standalone projects
|
|
|
|
```
|
|
./scripts/runTests.sh
|
|
```
|
|
|
|
Build both Maven and Gradle projects and run tests on standalone projects
|
|
|
|
```
|
|
./scripts/buildAndTest.sh
|
|
```
|
|
|
|
Generate documentation
|
|
|
|
```
|
|
./scripts/generateDocs.sh
|
|
```
|
|
|
|
Publish documentation to `gh-pages`
|
|
|
|
```
|
|
./docs/src/main/asciidoc/ghpages.sh
|
|
``` |