March 2, 2017

Ratpacked: Using Spring Cloud Contract As Client

In a previous post we learned about Spring Cloud Contract. We saw how we can use contracts to implement the server side of the contract. But Spring Cloud Contract also creates a stub based on the contract. The stub server is implemented with Wiremock and Spring Boot. The server can match incoming requests with the contracts and send back the response as defined in the contract. Let's write an application that is invoking HTTP requests on the server application we wrote before. In the tests that we write for this client application we use the stub that is generated by Spring Cloud Contract. We know the stub is following the contract of the actual server.

First we create the stub in our server project with the Gradle task verifierStubsJar. The tests in the client application need these stub and will fetch it as dependency from a Maven repository or the local Maven repository. For our example we use the local Maven repository. We add the maven-publish plugin to the server project and run the task publishToMavenLocal.

We create a new Gradle project for our Ratpack application that is invoking requests on the pirate service. The following Gradle build file sets all dependencies for the application and plugins to run and test the application:

plugins {
    id 'groovy'
    id 'project-report'
    id 'io.ratpack.ratpack-java' version '1.4.5'
    id 'com.github.johnrengelman.shadow' version '1.2.4'
    id 'io.spring.dependency-management' version '1.0.0.RELEASE'
}

group = 'mrhaki.ratpack.pirate.client'
version = '0.0.1'

repositories {
    jcenter()
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:Camden.SR5"
        mavenBom "org.springframework.boot:spring-boot-starter-parent:1.5.1.RELEASE"
    }
    dependencies {
        dependency 'com.google.guava:guava:19.0'
    }
}

dependencies {
    runtime 'org.slf4j:slf4j-simple:1.7.24'
    
    testCompile 'org.codehaus.groovy:groovy-all:2.4.9'
    testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
    testCompile 'org.spockframework:spock-spring:1.0-groovy-2.4'

    testCompile 'org.springframework.boot:spring-boot-starter-web', {
        exclude module: 'logback-classic'
    }
    testCompile 'org.springframework.cloud:spring-cloud-starter-contract-stub-runner', {
        exclude module: 'logback-classic'
    }
    testRuntime 'javax.servlet:javax.servlet-api:3.1.0'
}

mainClassName = 'mrhaki.sample.TavernApp'

assemble.dependsOn shadowJar

The code for the application can be found in Github. We write a test for our application and use the Spring Cloud Contract generated stub server. In our application we use the HttpClient class from Ratpack to invoke the pirate service. These calls will be send to the stub server in the specification. To start the stub we use the JUnit rule StubRunnerRule. We configure it to use the Maven local repository and define the dependency details. The stub server starts using a random port and we can get the address with the method findStubUrl. In our Ratpack application we have the address of the pirate service in the registry. We use impositions in our tests to replace that address with the stub server address:

package mrhaki.sample

import org.junit.ClassRule
import org.springframework.cloud.contract.stubrunner.junit.StubRunnerRule
import ratpack.impose.ImpositionsSpec
import ratpack.impose.UserRegistryImposition
import ratpack.registry.Registry
import ratpack.test.MainClassApplicationUnderTest
import spock.lang.AutoCleanup
import spock.lang.Shared
import spock.lang.Specification

class BartenderSpec extends Specification {

    @ClassRule
    @Shared
    private StubRunnerRule mockServer =
            new StubRunnerRule()
                    .downloadStub('mrhaki.ratpack.pirate.service', 'pirate-service', '0.0.2', 'stubs')
                    .workOffline(true)  // Use Maven local repo

    @AutoCleanup
    @Shared
    private app = new MainClassApplicationUnderTest(TavernApp) {
        @Override
        protected void addImpositions(final ImpositionsSpec impositions) {
            final mockUrl = mockServer.findStubUrl('mrhaki.ratpack.pirate.service', 'pirate-service')
            impositions.add(UserRegistryImposition.of(Registry.of { registry -> registry.add(URL, mockUrl)}))
        }
    }

    void 'ask for a drink'() {
        when:
        def response = app.httpClient.post('bartender/ask')

        then:
        response.statusCode == 200
        response.body.text == 'Hi-ho, mrhaki, ye like to drink some spiced rum!'
    }
    
    void 'tell story'() {
        when:
        def response = app.httpClient.get('bartender/story')

        then:
        response.statusCode == 200
        response.body.text == 'Ay, matey, mrhaki, walk the plank!'
    }
}

Spring Cloud Contract gives us a stub server that is compliant with the contract and even gives back responses based on matched requests from the contracts.

Written with Ratpack 1.4.5 and Spring Cloud Contract 1.0.3.RELEASE.