From a0acc40f556bffc7878de99d85bc815f4219971f Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Mon, 26 Jun 2017 20:04:13 +0200 Subject: [PATCH] BAEL-981 pact article code --- libraries/pom.xml | 12 +++ .../PactConsumerDrivenContractUnitTest.java | 82 +++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 libraries/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java diff --git a/libraries/pom.xml b/libraries/pom.xml index b7a33abbfe..de001ef622 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -381,6 +381,17 @@ java-lsh ${java-lsh.version} + + au.com.dius + pact-jvm-consumer-junit_2.11 + ${pact.version} + test + + + org.codehaus.groovy + groovy-all + 2.4.10 + @@ -410,6 +421,7 @@ 1.1.0 4.1.10.Final 0.10 + 3.5.0 diff --git a/libraries/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java b/libraries/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java new file mode 100644 index 0000000000..2952938cd9 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java @@ -0,0 +1,82 @@ +package com.baeldung.pact; + + +import au.com.dius.pact.consumer.Pact; +import au.com.dius.pact.consumer.PactProviderRuleMk2; +import au.com.dius.pact.consumer.PactVerification; +import au.com.dius.pact.consumer.dsl.PactDslWithProvider; +import au.com.dius.pact.model.RequestResponsePact; +import org.junit.Rule; +import org.junit.Test; +import org.springframework.http.*; +import org.springframework.web.client.RestTemplate; + +import java.util.HashMap; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +public class PactConsumerDrivenContractUnitTest { + + @Rule + public PactProviderRuleMk2 mockProvider + = new PactProviderRuleMk2("test_provider", "localhost", 8080, this); + + @Pact(consumer = "test_consumer") + public RequestResponsePact createPact(PactDslWithProvider builder) { + Map headers = new HashMap(); + headers.put("Content-Type", "application/json"); + + return builder + .given("test GET ") + .uponReceiving("GET REQUEST") + .path("/") + .method("GET") + .willRespondWith() + .status(200) + .headers(headers) + .body("{\"condition\": true, \"name\": \"tom\"}") + .given("test POST") + .uponReceiving("POST REQUEST") + .method("POST") + .headers(headers) + .body("{\"name\": \"Michael\"}") + .path("/create") + .willRespondWith() + .status(201) + .headers(headers) + .body("") + .toPact(); + } + + + @Test + @PactVerification() + public void givenGet_whenSendRequest_shouldReturn200WithProperHeaderAndBody() { + //when + ResponseEntity response + = new RestTemplate().getForEntity(mockProvider.getUrl(), String.class); + + //then + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThat(response.getHeaders().get("Content-Type").contains("application/json")).isTrue(); + assertThat(response.getBody()).contains("condition", "true", "name", "tom"); + + //and + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.APPLICATION_JSON); + String jsonBody = "{\"name\": \"Michael\"}"; + + //when + ResponseEntity postResponse = new RestTemplate().exchange( + mockProvider.getUrl() + "/create", + HttpMethod.POST, + new HttpEntity<>(jsonBody, httpHeaders), + String.class + ); + + //then + assertThat(postResponse.getStatusCode().value()).isEqualTo(201); + } + +}