From 692f12636f79be0be58fd86b6d527ba28be690f6 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Mon, 22 Jan 2018 18:46:11 +0200 Subject: [PATCH] add pact provider test (#3475) --- .../PactConsumerDrivenContractUnitTest.java | 12 ++-- spring-rest/pom.xml | 8 +++ .../web/controller/PactController.java | 32 ++++++++++ .../java/org/baeldung/web/dto/PactDto.java | 33 ++++++++++ .../pacts/test_consumer-test_provider.json | 61 +++++++++++++++++++ .../org/baeldung/pact/PactProviderTest.java | 40 ++++++++++++ 6 files changed, 179 insertions(+), 7 deletions(-) create mode 100644 spring-rest/src/main/java/org/baeldung/web/controller/PactController.java create mode 100644 spring-rest/src/main/java/org/baeldung/web/dto/PactDto.java create mode 100644 spring-rest/src/main/resources/pacts/test_consumer-test_provider.json create mode 100644 spring-rest/src/test/java/org/baeldung/pact/PactProviderTest.java diff --git a/libraries/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java b/libraries/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java index b152b22964..cc3b441aa4 100644 --- a/libraries/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java +++ b/libraries/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java @@ -32,9 +32,9 @@ public class PactConsumerDrivenContractUnitTest { headers.put("Content-Type", "application/json"); return builder - .given("test GET ") + .given("test GET") .uponReceiving("GET REQUEST") - .path("/") + .path("/pact") .method("GET") .willRespondWith() .status(200) @@ -45,11 +45,9 @@ public class PactConsumerDrivenContractUnitTest { .method("POST") .headers(headers) .body("{\"name\": \"Michael\"}") - .path("/create") + .path("/pact") .willRespondWith() .status(201) - .headers(headers) - .body("") .toPact(); } @@ -59,7 +57,7 @@ public class PactConsumerDrivenContractUnitTest { public void givenGet_whenSendRequest_shouldReturn200WithProperHeaderAndBody() { //when ResponseEntity response - = new RestTemplate().getForEntity(mockProvider.getUrl(), String.class); + = new RestTemplate().getForEntity(mockProvider.getUrl() + "/pact", String.class); //then assertThat(response.getStatusCode().value()).isEqualTo(200); @@ -73,7 +71,7 @@ public class PactConsumerDrivenContractUnitTest { //when ResponseEntity postResponse = new RestTemplate().exchange( - mockProvider.getUrl() + "/create", + mockProvider.getUrl() + "/pact", HttpMethod.POST, new HttpEntity<>(jsonBody, httpHeaders), String.class diff --git a/spring-rest/pom.xml b/spring-rest/pom.xml index 6da891b054..50ac00e1c3 100644 --- a/spring-rest/pom.xml +++ b/spring-rest/pom.xml @@ -169,6 +169,12 @@ commons-io 2.4 + + + au.com.dius + pact-jvm-provider-junit_2.11 + ${pact.version} + @@ -324,6 +330,8 @@ 3.4.1 2.2.0 + + 3.5.11 diff --git a/spring-rest/src/main/java/org/baeldung/web/controller/PactController.java b/spring-rest/src/main/java/org/baeldung/web/controller/PactController.java new file mode 100644 index 0000000000..4f586479ef --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/web/controller/PactController.java @@ -0,0 +1,32 @@ +package org.baeldung.web.controller; + +import java.util.ArrayList; +import java.util.List; + +import org.baeldung.web.dto.PactDto; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class PactController { + + List pacts = new ArrayList<>(); + + @GetMapping(value = "/pact", produces = MediaType.APPLICATION_JSON_VALUE) + @ResponseBody + public PactDto getPact() { + return new PactDto(true, "tom"); + } + + @PostMapping("/pact") + @ResponseStatus(HttpStatus.CREATED) + public void createPact(PactDto pact) { + pacts.add(pact); + } + +} diff --git a/spring-rest/src/main/java/org/baeldung/web/dto/PactDto.java b/spring-rest/src/main/java/org/baeldung/web/dto/PactDto.java new file mode 100644 index 0000000000..e34e2bdf3b --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/web/dto/PactDto.java @@ -0,0 +1,33 @@ +package org.baeldung.web.dto; + +public class PactDto { + + private boolean condition; + private String name; + + public PactDto() { + } + + public PactDto(boolean condition, String name) { + super(); + this.condition = condition; + this.name = name; + } + + public boolean isCondition() { + return condition; + } + + public void setCondition(boolean condition) { + this.condition = condition; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/spring-rest/src/main/resources/pacts/test_consumer-test_provider.json b/spring-rest/src/main/resources/pacts/test_consumer-test_provider.json new file mode 100644 index 0000000000..c8082798f4 --- /dev/null +++ b/spring-rest/src/main/resources/pacts/test_consumer-test_provider.json @@ -0,0 +1,61 @@ +{ + "provider": { + "name": "test_provider" + }, + "consumer": { + "name": "test_consumer" + }, + "interactions": [ + { + "description": "GET REQUEST", + "request": { + "method": "GET", + "path": "/pact" + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "application/json" + }, + "body": { + "condition": true, + "name": "tom" + } + }, + "providerStates": [ + { + "name": "test GET" + } + ] + }, + { + "description": "POST REQUEST", + "request": { + "method": "POST", + "path": "/pact", + "headers": { + "Content-Type": "application/json" + }, + "body": { + "name": "Michael" + } + }, + "response": { + "status": 201 + }, + "providerStates": [ + { + "name": "test POST" + } + ] + } + ], + "metadata": { + "pact-specification": { + "version": "3.0.0" + }, + "pact-jvm": { + "version": "3.5.0" + } + } +} \ No newline at end of file diff --git a/spring-rest/src/test/java/org/baeldung/pact/PactProviderTest.java b/spring-rest/src/test/java/org/baeldung/pact/PactProviderTest.java new file mode 100644 index 0000000000..0456b0d3e7 --- /dev/null +++ b/spring-rest/src/test/java/org/baeldung/pact/PactProviderTest.java @@ -0,0 +1,40 @@ +package org.baeldung.pact; + +import org.baeldung.config.MainApplication; +import org.junit.BeforeClass; +import org.junit.runner.RunWith; +import org.springframework.boot.SpringApplication; +import org.springframework.web.context.ConfigurableWebApplicationContext; + +import au.com.dius.pact.provider.junit.PactRunner; +import au.com.dius.pact.provider.junit.Provider; +import au.com.dius.pact.provider.junit.State; +import au.com.dius.pact.provider.junit.loader.PactFolder; +import au.com.dius.pact.provider.junit.target.HttpTarget; +import au.com.dius.pact.provider.junit.target.Target; +import au.com.dius.pact.provider.junit.target.TestTarget; + +@RunWith(PactRunner.class) +@Provider("test_provider") +@PactFolder("pacts") +public class PactProviderTest { + + @TestTarget + public final Target target = new HttpTarget("http", "localhost", 8082, "/spring-rest"); + + private static ConfigurableWebApplicationContext application; + + @BeforeClass + public static void start() { + application = (ConfigurableWebApplicationContext) SpringApplication.run(MainApplication.class); + } + + @State("test GET") + public void toGetState() { + } + + @State("test POST") + public void toPostState() { + } + +}