|
| 1 | +package com.baeldung.pact; |
| 2 | + |
| 3 | + |
| 4 | +import au.com.dius.pact.consumer.Pact; |
| 5 | +import au.com.dius.pact.consumer.PactProviderRuleMk2; |
| 6 | +import au.com.dius.pact.consumer.PactVerification; |
| 7 | +import au.com.dius.pact.consumer.dsl.PactDslWithProvider; |
| 8 | +import au.com.dius.pact.model.RequestResponsePact; |
| 9 | +import org.junit.Rule; |
| 10 | +import org.junit.Test; |
| 11 | +import org.springframework.http.*; |
| 12 | +import org.springframework.web.client.RestTemplate; |
| 13 | + |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | + |
| 19 | +public class PactConsumerDrivenContractUnitTest { |
| 20 | + |
| 21 | + @Rule |
| 22 | + public PactProviderRuleMk2 mockProvider |
| 23 | + = new PactProviderRuleMk2("test_provider", "localhost", 8080, this); |
| 24 | + |
| 25 | + @Pact(consumer = "test_consumer") |
| 26 | + public RequestResponsePact createPact(PactDslWithProvider builder) { |
| 27 | + Map<String, String> headers = new HashMap<String, String>(); |
| 28 | + headers.put("Content-Type", "application/json"); |
| 29 | + |
| 30 | + return builder |
| 31 | + .given("test GET ") |
| 32 | + .uponReceiving("GET REQUEST") |
| 33 | + .path("/") |
| 34 | + .method("GET") |
| 35 | + .willRespondWith() |
| 36 | + .status(200) |
| 37 | + .headers(headers) |
| 38 | + .body("{\"condition\": true, \"name\": \"tom\"}") |
| 39 | + .given("test POST") |
| 40 | + .uponReceiving("POST REQUEST") |
| 41 | + .method("POST") |
| 42 | + .headers(headers) |
| 43 | + .body("{\"name\": \"Michael\"}") |
| 44 | + .path("/create") |
| 45 | + .willRespondWith() |
| 46 | + .status(201) |
| 47 | + .headers(headers) |
| 48 | + .body("") |
| 49 | + .toPact(); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + @Test |
| 54 | + @PactVerification() |
| 55 | + public void givenGet_whenSendRequest_shouldReturn200WithProperHeaderAndBody() { |
| 56 | + //when |
| 57 | + ResponseEntity<String> response |
| 58 | + = new RestTemplate().getForEntity(mockProvider.getUrl(), String.class); |
| 59 | + |
| 60 | + //then |
| 61 | + assertThat(response.getStatusCode().value()).isEqualTo(200); |
| 62 | + assertThat(response.getHeaders().get("Content-Type").contains("application/json")).isTrue(); |
| 63 | + assertThat(response.getBody()).contains("condition", "true", "name", "tom"); |
| 64 | + |
| 65 | + //and |
| 66 | + HttpHeaders httpHeaders = new HttpHeaders(); |
| 67 | + httpHeaders.setContentType(MediaType.APPLICATION_JSON); |
| 68 | + String jsonBody = "{\"name\": \"Michael\"}"; |
| 69 | + |
| 70 | + //when |
| 71 | + ResponseEntity<String> postResponse = new RestTemplate().exchange( |
| 72 | + mockProvider.getUrl() + "/create", |
| 73 | + HttpMethod.POST, |
| 74 | + new HttpEntity<>(jsonBody, httpHeaders), |
| 75 | + String.class |
| 76 | + ); |
| 77 | + |
| 78 | + //then |
| 79 | + assertThat(postResponse.getStatusCode().value()).isEqualTo(201); |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments