Skip to content

Commit 692f126

Browse files
lor6pivovarit
authored andcommitted
add pact provider test (eugenp#3475)
1 parent b1266c8 commit 692f126

6 files changed

Lines changed: 179 additions & 7 deletions

File tree

‎libraries/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public RequestResponsePact createPact(PactDslWithProvider builder) {
3232
headers.put("Content-Type", "application/json");
3333

3434
return builder
35-
.given("test GET ")
35+
.given("test GET")
3636
.uponReceiving("GET REQUEST")
37-
.path("/")
37+
.path("/pact")
3838
.method("GET")
3939
.willRespondWith()
4040
.status(200)
@@ -45,11 +45,9 @@ public RequestResponsePact createPact(PactDslWithProvider builder) {
4545
.method("POST")
4646
.headers(headers)
4747
.body("{\"name\": \"Michael\"}")
48-
.path("/create")
48+
.path("/pact")
4949
.willRespondWith()
5050
.status(201)
51-
.headers(headers)
52-
.body("")
5351
.toPact();
5452
}
5553

@@ -59,7 +57,7 @@ public RequestResponsePact createPact(PactDslWithProvider builder) {
5957
public void givenGet_whenSendRequest_shouldReturn200WithProperHeaderAndBody() {
6058
//when
6159
ResponseEntity<String> response
62-
= new RestTemplate().getForEntity(mockProvider.getUrl(), String.class);
60+
= new RestTemplate().getForEntity(mockProvider.getUrl() + "/pact", String.class);
6361

6462
//then
6563
assertThat(response.getStatusCode().value()).isEqualTo(200);
@@ -73,7 +71,7 @@ public void givenGet_whenSendRequest_shouldReturn200WithProperHeaderAndBody() {
7371

7472
//when
7573
ResponseEntity<String> postResponse = new RestTemplate().exchange(
76-
mockProvider.getUrl() + "/create",
74+
mockProvider.getUrl() + "/pact",
7775
HttpMethod.POST,
7876
new HttpEntity<>(jsonBody, httpHeaders),
7977
String.class

‎spring-rest/pom.xml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@
169169
<artifactId>commons-io</artifactId>
170170
<version>2.4</version>
171171
</dependency>
172+
173+
<dependency>
174+
<groupId>au.com.dius</groupId>
175+
<artifactId>pact-jvm-provider-junit_2.11</artifactId>
176+
<version>${pact.version}</version>
177+
</dependency>
172178

173179
</dependencies>
174180

@@ -324,6 +330,8 @@
324330
<com.squareup.okhttp3.version>3.4.1</com.squareup.okhttp3.version>
325331

326332
<json.path.version>2.2.0</json.path.version>
333+
334+
<pact.version>3.5.11</pact.version>
327335
</properties>
328336

329337
</project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.baeldung.web.controller;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.baeldung.web.dto.PactDto;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.PostMapping;
11+
import org.springframework.web.bind.annotation.ResponseBody;
12+
import org.springframework.web.bind.annotation.ResponseStatus;
13+
import org.springframework.web.bind.annotation.RestController;
14+
15+
@RestController
16+
public class PactController {
17+
18+
List<PactDto> pacts = new ArrayList<>();
19+
20+
@GetMapping(value = "/pact", produces = MediaType.APPLICATION_JSON_VALUE)
21+
@ResponseBody
22+
public PactDto getPact() {
23+
return new PactDto(true, "tom");
24+
}
25+
26+
@PostMapping("/pact")
27+
@ResponseStatus(HttpStatus.CREATED)
28+
public void createPact(PactDto pact) {
29+
pacts.add(pact);
30+
}
31+
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.baeldung.web.dto;
2+
3+
public class PactDto {
4+
5+
private boolean condition;
6+
private String name;
7+
8+
public PactDto() {
9+
}
10+
11+
public PactDto(boolean condition, String name) {
12+
super();
13+
this.condition = condition;
14+
this.name = name;
15+
}
16+
17+
public boolean isCondition() {
18+
return condition;
19+
}
20+
21+
public void setCondition(boolean condition) {
22+
this.condition = condition;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"provider": {
3+
"name": "test_provider"
4+
},
5+
"consumer": {
6+
"name": "test_consumer"
7+
},
8+
"interactions": [
9+
{
10+
"description": "GET REQUEST",
11+
"request": {
12+
"method": "GET",
13+
"path": "/pact"
14+
},
15+
"response": {
16+
"status": 200,
17+
"headers": {
18+
"Content-Type": "application/json"
19+
},
20+
"body": {
21+
"condition": true,
22+
"name": "tom"
23+
}
24+
},
25+
"providerStates": [
26+
{
27+
"name": "test GET"
28+
}
29+
]
30+
},
31+
{
32+
"description": "POST REQUEST",
33+
"request": {
34+
"method": "POST",
35+
"path": "/pact",
36+
"headers": {
37+
"Content-Type": "application/json"
38+
},
39+
"body": {
40+
"name": "Michael"
41+
}
42+
},
43+
"response": {
44+
"status": 201
45+
},
46+
"providerStates": [
47+
{
48+
"name": "test POST"
49+
}
50+
]
51+
}
52+
],
53+
"metadata": {
54+
"pact-specification": {
55+
"version": "3.0.0"
56+
},
57+
"pact-jvm": {
58+
"version": "3.5.0"
59+
}
60+
}
61+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.baeldung.pact;
2+
3+
import org.baeldung.config.MainApplication;
4+
import org.junit.BeforeClass;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.boot.SpringApplication;
7+
import org.springframework.web.context.ConfigurableWebApplicationContext;
8+
9+
import au.com.dius.pact.provider.junit.PactRunner;
10+
import au.com.dius.pact.provider.junit.Provider;
11+
import au.com.dius.pact.provider.junit.State;
12+
import au.com.dius.pact.provider.junit.loader.PactFolder;
13+
import au.com.dius.pact.provider.junit.target.HttpTarget;
14+
import au.com.dius.pact.provider.junit.target.Target;
15+
import au.com.dius.pact.provider.junit.target.TestTarget;
16+
17+
@RunWith(PactRunner.class)
18+
@Provider("test_provider")
19+
@PactFolder("pacts")
20+
public class PactProviderTest {
21+
22+
@TestTarget
23+
public final Target target = new HttpTarget("http", "localhost", 8082, "/spring-rest");
24+
25+
private static ConfigurableWebApplicationContext application;
26+
27+
@BeforeClass
28+
public static void start() {
29+
application = (ConfigurableWebApplicationContext) SpringApplication.run(MainApplication.class);
30+
}
31+
32+
@State("test GET")
33+
public void toGetState() {
34+
}
35+
36+
@State("test POST")
37+
public void toPostState() {
38+
}
39+
40+
}

0 commit comments

Comments
 (0)