Git Product home page Git Product logo

wiremock-placeholders's Introduction

wiremock-cdc

Wiremock for Consumer Driven Contracts

Introduction

We would like to use the Wiremock's JSON stub definitions as a point of entry to our approach of Consumer Driven Contracts.

Notations

  • Collaborator - a service whom your service can contact
  • Client test - since your service is a client of your collaborator then the tests of your service are client tests
  • Server test - from a your service's (client) perspective your collaborator is a server that provides a functionality thus tests of your collaborators are server tests

Why?

The main purpose of this approach is to:

  • ensure that our stubs are doing exactly what the actual implementation does
  • generate acceptance test cases from stub definitions (ATDD)
  • make the stub definitions reusable

Below we depict the idea behind client and server side testing. Let's assume that for the sake of this description that there is a service X calling a service Y.

Client side (service X)

During the tests you want to have a Wiremock instance 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 from the Wiremock's perspective but 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 (service Y)

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.

Description

To achieve that we needed to tweak the standard Wiremock stub definitions by providing a possibility of entering two values for one field. This is done via the following pattern:

${VALUE_FOR_CLIENT_TESTS:VALUE_FOR_SERVER_TESTS}

That means that depending on the need you can take either value for the client test or server test.

Example

Let's take a look at the following example

{
    "request": {
        "method": "GET",
        "urlPattern": "${/[0-9]{2}}:${/12}"
    },
    "response": {
        "status": 200,
        "body": "${{\"date\":\"2015-01-14\"}:$isCurrentDate($it)}",
        "headers": {
            "Content-Type": "text/plain"
        }
    }
}

Client side

From the client's perspective, in this particular scenario, you want a regexp matching a URL on which you send a request ([/0-9]{2}) and a concrete value returned (2015-01-14). That way we will change your stub definition to:

{
    "request": {
        "method": "GET",
        "urlPattern": "/[0-9]{2}"
    },
    "response": {
        "status": 200,
        "body": {
            "date" : "2015-01-14"
        },
        "headers": {
            "Content-Type": "text/plain"
        }
    }
}

Server side

On the server side we need to generate acceptance tests. So we need to check that for given input (request) we receive some matching output (response).

In this case we want to send a request to an endpoint /12 and check if in the body of the response we will receive the current date (isCurrentDate(it.date)).

The latter value will be checked if it's a method (if it contains parentheses) and then a method will be called with the corresponding field's (status, body, headers etc.) value as input.

VALUE_FOR_SERVER_TESTS can use $it notation to pass body as method argument. The method in the generated Spec can be access also with $ prefix - if you want to call a method isPersonalIdValid(String requestBody) you can do it as follows:

${123456789:$isPersonalIdValid($it)}
class AcceptanceSpec extends AssurestSpec {

    // TODO: Define how it's going to look like

}

wiremock-placeholders's People

Contributors

marcingrzejszczak avatar

Stargazers

Simon Collins avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.