add pact provider test (#3475)
This commit is contained in:
parent
b1266c833b
commit
692f12636f
|
@ -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<String> 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<String> postResponse = new RestTemplate().exchange(
|
||||
mockProvider.getUrl() + "/create",
|
||||
mockProvider.getUrl() + "/pact",
|
||||
HttpMethod.POST,
|
||||
new HttpEntity<>(jsonBody, httpHeaders),
|
||||
String.class
|
||||
|
|
|
@ -169,6 +169,12 @@
|
|||
<artifactId>commons-io</artifactId>
|
||||
<version>2.4</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>au.com.dius</groupId>
|
||||
<artifactId>pact-jvm-provider-junit_2.11</artifactId>
|
||||
<version>${pact.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
@ -324,6 +330,8 @@
|
|||
<com.squareup.okhttp3.version>3.4.1</com.squareup.okhttp3.version>
|
||||
|
||||
<json.path.version>2.2.0</json.path.version>
|
||||
|
||||
<pact.version>3.5.11</pact.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -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<PactDto> 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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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() {
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue