JAVA-4493: Add missing live test for Consumer Driven Contracts article (#12362)
* JAVA-4493: Add missing live test for Consumer Driven Contracts article * JAVA-4493: Add missing live test for Consumer Driven Contracts article
This commit is contained in:
parent
263f0b1add
commit
3e0c28d1ed
@ -6,16 +6,16 @@
|
|||||||
<artifactId>libraries-5</artifactId>
|
<artifactId>libraries-5</artifactId>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>parent-modules</artifactId>
|
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<artifactId>parent-boot-2</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../parent-boot-2</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-web</artifactId>
|
<artifactId>spring-web</artifactId>
|
||||||
<version>${spring.version}</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jooq</groupId>
|
<groupId>org.jooq</groupId>
|
||||||
@ -24,10 +24,19 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>au.com.dius</groupId>
|
<groupId>au.com.dius</groupId>
|
||||||
<artifactId>pact-jvm-consumer-junit_2.11</artifactId>
|
<artifactId>pact-jvm-provider-junit5_2.12</artifactId>
|
||||||
|
<version>${pact.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>au.com.dius</groupId>
|
||||||
|
<artifactId>pact-jvm-consumer-junit5_2.12</artifactId>
|
||||||
<version>${pact.version}</version>
|
<version>${pact.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
<!-- https://mvnrepository.com/artifact/com.typesafe.akka/akka-actor -->
|
<!-- https://mvnrepository.com/artifact/com.typesafe.akka/akka-actor -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.typesafe.akka</groupId>
|
<groupId>com.typesafe.akka</groupId>
|
||||||
@ -111,10 +120,10 @@
|
|||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<pact.version>3.5.0</pact.version>
|
<pact.version>3.6.3</pact.version>
|
||||||
<jool.version>0.9.12</jool.version>
|
<jool.version>0.9.12</jool.version>
|
||||||
<spring.version>4.3.8.RELEASE</spring.version>
|
<spring.version>4.3.8.RELEASE</spring.version>
|
||||||
<scala.version>2.11</scala.version>
|
<scala.version>2.12</scala.version>
|
||||||
<typesafe-akka.version>2.5.11</typesafe-akka.version>
|
<typesafe-akka.version>2.5.11</typesafe-akka.version>
|
||||||
<streamex.version>0.6.5</streamex.version>
|
<streamex.version>0.6.5</streamex.version>
|
||||||
<docker.version>3.0.14</docker.version>
|
<docker.version>3.0.14</docker.version>
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.pact.config;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
@ComponentScan("com.baeldung.pact")
|
||||||
|
public class MainApplication implements WebMvcConfigurer {
|
||||||
|
|
||||||
|
public static void main(final String[] args) {
|
||||||
|
SpringApplication.run(MainApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.sampleapp.web.controller;
|
package com.baeldung.pact.web.controller;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -11,7 +11,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import com.baeldung.sampleapp.web.dto.PactDto;
|
import com.baeldung.pact.web.dto.PactDto;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class PactController {
|
public class PactController {
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.sampleapp.web.dto;
|
package com.baeldung.pact.web.dto;
|
||||||
|
|
||||||
public class PactDto {
|
public class PactDto {
|
||||||
|
|
2
libraries-5/src/main/resources/application.properties
Normal file
2
libraries-5/src/main/resources/application.properties
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
server.port=8082
|
||||||
|
server.servlet.context-path=/spring-rest
|
@ -1,46 +1,31 @@
|
|||||||
package com.baeldung.pact;
|
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.io.IOException;
|
|
||||||
import java.net.ServerSocket;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.springframework.http.HttpEntity;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import au.com.dius.pact.consumer.MockServer;
|
||||||
|
import au.com.dius.pact.consumer.Pact;
|
||||||
|
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
|
||||||
|
import au.com.dius.pact.consumer.junit5.PactConsumerTestExt;
|
||||||
|
import au.com.dius.pact.consumer.junit5.PactTestFor;
|
||||||
|
import au.com.dius.pact.model.RequestResponsePact;
|
||||||
|
|
||||||
|
@ExtendWith(PactConsumerTestExt.class)
|
||||||
|
@PactTestFor(providerName = "test_provider", hostInterface="localhost")
|
||||||
public class PactConsumerDrivenContractUnitTest {
|
public class PactConsumerDrivenContractUnitTest {
|
||||||
|
|
||||||
private static int getAvailablePort() {
|
@Pact(provider="test_provider", consumer = "test_consumer")
|
||||||
return new Random()
|
|
||||||
.ints(6000, 9000)
|
|
||||||
.filter(PactConsumerDrivenContractUnitTest::isFree)
|
|
||||||
.findFirst()
|
|
||||||
.orElse(8080);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean isFree(int port) {
|
|
||||||
try {
|
|
||||||
new ServerSocket(port).close();
|
|
||||||
return true;
|
|
||||||
} catch (IOException e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Rule
|
|
||||||
public PactProviderRuleMk2 mockProvider = new PactProviderRuleMk2("test_provider", "localhost", getAvailablePort(), this);
|
|
||||||
|
|
||||||
@Pact(consumer = "test_consumer")
|
|
||||||
public RequestResponsePact createPact(PactDslWithProvider builder) {
|
public RequestResponsePact createPact(PactDslWithProvider builder) {
|
||||||
Map<String, String> headers = new HashMap<>();
|
Map<String, String> headers = new HashMap<>();
|
||||||
headers.put("Content-Type", "application/json");
|
headers.put("Content-Type", "application/json");
|
||||||
@ -66,10 +51,10 @@ public class PactConsumerDrivenContractUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@PactVerification()
|
@PactTestFor
|
||||||
public void givenGet_whenSendRequest_shouldReturn200WithProperHeaderAndBody() {
|
void givenGet_whenSendRequest_shouldReturn200WithProperHeaderAndBody(MockServer mockServer) {
|
||||||
// when
|
// when
|
||||||
ResponseEntity<String> response = new RestTemplate().getForEntity(mockProvider.getUrl() + "/pact", String.class);
|
ResponseEntity<String> response = new RestTemplate().getForEntity(mockServer.getUrl() + "/pact", String.class);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
||||||
@ -82,7 +67,7 @@ public class PactConsumerDrivenContractUnitTest {
|
|||||||
String jsonBody = "{\"name\": \"Michael\"}";
|
String jsonBody = "{\"name\": \"Michael\"}";
|
||||||
|
|
||||||
// when
|
// when
|
||||||
ResponseEntity<String> postResponse = new RestTemplate().exchange(mockProvider.getUrl() + "/pact", HttpMethod.POST, new HttpEntity<>(jsonBody, httpHeaders), String.class);
|
ResponseEntity<String> postResponse = new RestTemplate().exchange(mockServer.getUrl() + "/pact", HttpMethod.POST, new HttpEntity<>(jsonBody, httpHeaders), String.class);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertThat(postResponse.getStatusCode().value()).isEqualTo(201);
|
assertThat(postResponse.getStatusCode().value()).isEqualTo(201);
|
||||||
|
@ -7,7 +7,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
|
|||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.web.context.ConfigurableWebApplicationContext;
|
import org.springframework.web.context.ConfigurableWebApplicationContext;
|
||||||
|
|
||||||
import com.baeldung.sampleapp.config.MainApplication;
|
import com.baeldung.pact.config.MainApplication;
|
||||||
|
|
||||||
import au.com.dius.pact.provider.junit.Provider;
|
import au.com.dius.pact.provider.junit.Provider;
|
||||||
import au.com.dius.pact.provider.junit.State;
|
import au.com.dius.pact.provider.junit.State;
|
@ -1,76 +0,0 @@
|
|||||||
package com.baeldung.pact;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
|
||||||
import org.springframework.http.HttpEntity;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import org.springframework.http.HttpMethod;
|
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
|
|
||||||
import au.com.dius.pact.consumer.MockServer;
|
|
||||||
import au.com.dius.pact.consumer.Pact;
|
|
||||||
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
|
|
||||||
import au.com.dius.pact.consumer.junit5.PactConsumerTestExt;
|
|
||||||
import au.com.dius.pact.consumer.junit5.PactTestFor;
|
|
||||||
import au.com.dius.pact.model.RequestResponsePact;
|
|
||||||
|
|
||||||
@ExtendWith(PactConsumerTestExt.class)
|
|
||||||
@PactTestFor(providerName = "test_provider", hostInterface="localhost")
|
|
||||||
public class PactConsumerDrivenContractUnitTest {
|
|
||||||
|
|
||||||
@Pact(provider="test_provider", consumer = "test_consumer")
|
|
||||||
public RequestResponsePact createPact(PactDslWithProvider builder) {
|
|
||||||
Map<String, String> headers = new HashMap<>();
|
|
||||||
headers.put("Content-Type", "application/json");
|
|
||||||
|
|
||||||
return builder
|
|
||||||
.given("test GET")
|
|
||||||
.uponReceiving("GET REQUEST")
|
|
||||||
.path("/pact")
|
|
||||||
.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("/pact")
|
|
||||||
.willRespondWith()
|
|
||||||
.status(201)
|
|
||||||
.toPact();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@PactTestFor
|
|
||||||
void givenGet_whenSendRequest_shouldReturn200WithProperHeaderAndBody(MockServer mockServer) {
|
|
||||||
// when
|
|
||||||
ResponseEntity<String> response = new RestTemplate().getForEntity(mockServer.getUrl() + "/pact", 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<String> postResponse = new RestTemplate().exchange(mockServer.getUrl() + "/pact", HttpMethod.POST, new HttpEntity<>(jsonBody, httpHeaders), String.class);
|
|
||||||
|
|
||||||
// then
|
|
||||||
assertThat(postResponse.getStatusCode().value()).isEqualTo(201);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user