20. Standards are required for things like monitoring,
logging, backward compatibility, and API contracts.
API Contracts
21. Lean principle:
A service is of value to the business only to the
extent it is consumed.
https://martinfowler.com/articles/consumerDrivenContracts.html
22. Make sure the Contracts can be imposed
Set of rules and
technologies that should be
implemented and adopted
on both sides.
29. Postel's law
“Be conservative in what you do, be liberal in
what you accept from others
(often reworded as "Be conservative in what you
send, be liberal in what you accept").”
41. Some parts of the APIs might not be used and
will not impact anyone. They might either be in a
state of active development or about to be
decommissioned
42. Demystification
These tests are not component tests. They do not
test the behaviour of the service deeply but that
the inputs and outputs of service calls contain
required attributes.
45. There are 2 main options:
1. Spring Cloud Contracts;
2. Pact.
46. Spring Cloud Contracts
● Spring Cloud Contract Verifier - the
producer side;
● Spring Cloud Contract Stub Runner - the
consumer side
47. Spring Cloud Contracts: Contract Verifier
Shipped with Contract Definition Language
(DSL) written in Groovy or YAML.
48. Spring Cloud Contract Verifier provides:
● Alignment between stubs and actual
implementation;
● Acceptance test driven development;
● Immediate visibility of contracts on both sides;
● Autogenerated boilerplate tests.
49. Spring Cloud Contracts: Contract Stub Runner
● Fetches the stubs of the producer;
● Runs tests against the stubs.
51. There’s more if you read into specs:
● Groovy DSL
● YAML
Spring Cloud Contracts: Contract Definition
Language
52. There’s more if you read into specs:
● Groovy DSL
● YAML
● Pact JSON
● Spring Rest Docs
Spring Cloud Contracts: Contract Definition
Language
53. Spring Cloud Contracts - catchline
“Spring Cloud Contract Verifier moves TDD to
the level of software architecture.”
54. SCC guarantees:
● Stubs reliability: generated on successful test
execution.
● Stubs reusability: packed in a separate artifact
with a stub classifier.
58. Server / Producer side: base class
<baseClassForTests>....api.controllers.BookingControllerImplTest</...>
59. Server / Producer side: base class
The responsibility to initialising your MockMvc is on you:
java.lang.IllegalStateException: You haven't configured a MockMVC
instance. You can do this statically
RestAssuredMockMvc.mockMvc(..)
RestAssuredMockMvc.standaloneSetup(..);
RestAssuredMockMvc.webAppContextSetup(..);
or using the DSL:
given().
mockMvc(..). ..
60. Server / Producer side: producer stubs
Spring Cloud Contract Verifier will convert the contracts into an HTTP
server stub definitions(Wiremock mapping jsons).
61. Demo time!
● Run Producer tests;
● Spring-cloud-contract-maven-plugin options;
● Run Consumer tests;
● Groovy DSL vs YAML;
● Add stateful behaviour(real MySql instance) for visibility;
● Non-binary storage(git);
● Explain stubrunner yaml property and how it impacts tests;
● Wiremock scenarios;
● produce(), consume() and matchers;
● execute() with custom utility methods;
● Integration with the rest of Spring Cloud.
63. Pact: language support
● Ruby Pact
● JVM Pact and Scala-Pact
● .NET Pact
● JS Pact
● Go Pact (there is also a v1.1 native Pact Go)
● Swift / Objective-C Pact
● Python
● PHP
66. Pact: diagram
Consumer
● Specifies the contracts;
● Generates the pact files;
● Push them into Pact Broker
Pact Broker
● Stores pact contracts;
● Integrates into CI tools.
67. Pact: diagram
Consumer
● Specifies the contracts;
● Generates the pact files;
● Push them into Pact Broker
Pact Broker
● Stores pact contracts;
● Integrates into CI tools.
Producer
● Implement a service
according to Consumer
specs.
68. Demo time!
● Pact core concepts: State, Broker, Verification;
● Versioning;
● Pact extension for Spring Cloud Contracts;
You can create very robust,
scalable microservices with an expectation that
it will get you closer to CD and bring true value to Business...
CD
Hotfixes. etc.
You can create very robust,
scalable microservices with an expectation that
it will get you closer to CD and bring true value to Business...
...and completely screw it up by
not adopting contracts between them,
which will get you further from CD than you’ve ever been before.
It has a lot to do with communitation.
Runtime attributes(Scalability, Performance, Availability)
Design attributes(Reusability, Maintanability, Integrity)
If your ultimate goal is to build a distributed, microservice based system.
and collaboration between services and people.
As a result, providers expose a lean contract that is clearly aligned with the business goals that underpin their consumers.
The Consumer-Driven Contract pattern is applicable in a single enterprise or a closed community.
If you delay with API contracts, it will be a lot more challenging to get a buy-in from all development teams to adopt it when you are already running 10s/100s of services.
Let’s say we don’t use any tools and decided to test our microservices together as an end to end flow
Do you see a contraption like this very often in a real life?
Contracts enable service independence; paradoxically, they can also couple service providers and consumers in undesirable ways.
That’s why there are frameworks for that!
and they can be quite different and only partially overlap or not overlap at all.
As consumers of services, we need to define what exactly we want to achieve. We need to formulate our expectations. That is why we write contracts.
Contract tests assert that the integration between the producer and the consumer meets the requirements defined in the contract.
Spring Cloud Contract Verifier features:
Ensure that HTTP / Messaging stubs (for client) are doing exactly what actual server-side implementation will do
Promote acceptance test driven development method and Microservices architectural style
Provide a way to publish changes in contracts that are immediately visible on both sides of the communication
Autogenerated boilerplate test code used on the server side
Very cool feature, because it allows you you use a webenvironment of your choosing.
Wiremock output: logging by default
Base classes:
About Wiremock scenarios: TestRestTemplate is required. Otherwise 404 issue.
About Wiremock scenarios: Content-type: very important to have. Otherwise, stubs will fail when called with RestTemplate.
Disable FixedMethodOrder and show the results.
https://github.com/spring-cloud-samples/spring-cloud-contract-nodejs
https://spring.io/blog/2018/02/13/spring-cloud-contract-in-a-polyglot-world
Something similar can be attempted by using Spring Cloud:
In order to enable working with contracts while creating applications in non-JVM technologies, the springcloud/spring-cloud-contract Docker image has been created. It contains a project that automatically generates tests for HTTP contracts and executes them in EXPLICIT test mode. Then, if the tests pass, it generates Wiremock stubs and, optionally, publishes them to an artifact manager. In order to use the image, you can mount the contracts into the /contracts directory and set a few environment variables.
Something similar can be attempted by using Spring Cloud:
In order to enable working with contracts while creating applications in non-JVM technologies, the springcloud/spring-cloud-contract Docker image has been created. It contains a project that automatically generates tests for HTTP contracts and executes them in EXPLICIT test mode. Then, if the tests pass, it generates Wiremock stubs and, optionally, publishes them to an artifact manager. In order to use the image, you can mount the contracts into the /contracts directory and set a few environment variables.
Something similar can be attempted by using Spring Cloud:
In order to enable working with contracts while creating applications in non-JVM technologies, the springcloud/spring-cloud-contract Docker image has been created. It contains a project that automatically generates tests for HTTP contracts and executes them in EXPLICIT test mode. Then, if the tests pass, it generates Wiremock stubs and, optionally, publishes them to an artifact manager. In order to use the image, you can mount the contracts into the /contracts directory and set a few environment variables.
Something similar can be attempted by using Spring Cloud:
In order to enable working with contracts while creating applications in non-JVM technologies, the springcloud/spring-cloud-contract Docker image has been created. It contains a project that automatically generates tests for HTTP contracts and executes them in EXPLICIT test mode. Then, if the tests pass, it generates Wiremock stubs and, optionally, publishes them to an artifact manager. In order to use the image, you can mount the contracts into the /contracts directory and set a few environment variables.
Something similar can be attempted by using Spring Cloud:
In order to enable working with contracts while creating applications in non-JVM technologies, the springcloud/spring-cloud-contract Docker image has been created. It contains a project that automatically generates tests for HTTP contracts and executes them in EXPLICIT test mode. Then, if the tests pass, it generates Wiremock stubs and, optionally, publishes them to an artifact manager. In order to use the image, you can mount the contracts into the /contracts directory and set a few environment variables.
Native Pact Broker only supports real HTTP transport.
https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-provider-junit
https://unsplash.com/collections/239831/typography - slogans
https://unsplash.com/collections/203782/in-transit - travelling, transportations, on the road
https://unsplash.com/collections/261936/technology - mostly phones and laptops
https://unsplash.com/collections/881815/au-naturel - desktop wallpapers
https://unsplash.com/collections/345744/urban-architecture
https://unsplash.com/collections/589374/textures - abstract patterns
https://unsplash.com/collections/227/shapes-patterns-textures - abstract patterns
https://unsplash.com/collections/416021/abstract - abstract patterns
https://unsplash.com/collections/1240111/its-simple-but-very-complex - patterns(nature, architecture)
https://unsplash.com/collections/420324/blurrrr - nice pics of different blurred sceneries
https://unsplash.com/collections/770996/creative-spaces - office environment
https://unsplash.com/collections/385548/work-and-collaboration
https://unsplash.com/collections/361687/architectural-lines - architecture, buildings
https://unsplash.com/collections/1439704/facade - building facades
https://unsplash.com/collections/1136512/%E2%98%85-textures-colors - slogans and sceneries
https://unsplash.com/collections/345761/sport - sports