Skip to content

Commit 3ba169f

Browse files
author
Ronald Holshausen
committed
chore: update to support JDK 8
1 parent 47546e4 commit 3ba169f

File tree

2 files changed

+73
-57
lines changed

2 files changed

+73
-57
lines changed

README.md

+61-53
Original file line numberDiff line numberDiff line change
@@ -231,75 +231,81 @@ In `consumer/src/test/java/au/com/dius/pactworkshop/consumer/ProductConsumerPact
231231
```java
232232
@ExtendWith(PactConsumerTestExt.class)
233233
public class ProductConsumerPactTest {
234-
235-
@Pact(consumer = "FrontendApplication", provider = "ProductService")
236-
RequestResponsePact getAllProducts(PactDslWithProvider builder) {
234+
235+
@Pact(consumer = "FrontendApplication", provider = "ProductService")
236+
RequestResponsePact getAllProducts(PactDslWithProvider builder) {
237237
return builder.given("products exist")
238-
.uponReceiving("get all products")
239-
.method("GET")
240-
.path("/products")
241-
.willRespondWith()
242-
.status(200)
243-
.headers(Map.of("Content-Type", "application/json; charset=utf-8"))
244-
.body(newJsonArrayMinLike(2, array -> {
245-
array.object(object -> {
246-
object.stringType("id", "09");
247-
object.stringType("type", "CREDIT_CARD");
248-
object.stringType("name", "Gem Visa");
249-
});
250-
}).build())
251-
.toPact();
252-
}
253-
254-
@Pact(consumer = "FrontendApplication", provider = "ProductService")
255-
RequestResponsePact getOneProduct(PactDslWithProvider builder) {
238+
.uponReceiving("get all products")
239+
.method("GET")
240+
.path("/products")
241+
.willRespondWith()
242+
.status(200)
243+
.headers(headers())
244+
.body(newJsonArrayMinLike(2, array ->
245+
array.object(object -> {
246+
object.stringType("id", "09");
247+
object.stringType("type", "CREDIT_CARD");
248+
object.stringType("name", "Gem Visa");
249+
})
250+
).build())
251+
.toPact();
252+
}
253+
254+
@Pact(consumer = "FrontendApplication", provider = "ProductService")
255+
RequestResponsePact getOneProduct(PactDslWithProvider builder) {
256256
return builder.given("product with ID 10 exists")
257-
.uponReceiving("get product with ID 10")
258-
.method("GET")
259-
.path("/products/10")
260-
.willRespondWith()
261-
.status(200)
262-
.headers(Map.of("Content-Type", "application/json; charset=utf-8"))
263-
.body(newJsonBody(object -> {
264-
object.stringType("id", "10");
265-
object.stringType("type", "CREDIT_CARD");
266-
object.stringType("name", "28 Degrees");
267-
}).build())
268-
.toPact();
269-
}
270-
271-
@Test
272-
@PactTestFor(pactMethod = "getAllProducts")
273-
void getAllProducts_whenProductsExist(MockServer mockServer) {
257+
.uponReceiving("get product with ID 10")
258+
.method("GET")
259+
.path("/products/10")
260+
.willRespondWith()
261+
.status(200)
262+
.headers(headers())
263+
.body(newJsonBody(object -> {
264+
object.stringType("id", "10");
265+
object.stringType("type", "CREDIT_CARD");
266+
object.stringType("name", "28 Degrees");
267+
}).build())
268+
.toPact();
269+
}
270+
271+
@Test
272+
@PactTestFor(pactMethod = "getAllProducts")
273+
void getAllProducts_whenProductsExist(MockServer mockServer) {
274274
Product product = new Product();
275275
product.setId("09");
276276
product.setType("CREDIT_CARD");
277277
product.setName("Gem Visa");
278-
List<Product> expected = List.of(product, product);
279-
278+
List<Product> expected = Arrays.asList(product, product);
279+
280280
RestTemplate restTemplate = new RestTemplateBuilder()
281-
.rootUri(mockServer.getUrl())
282-
.build();
281+
.rootUri(mockServer.getUrl())
282+
.build();
283283
List<Product> products = new ProductService(restTemplate).getAllProducts();
284-
284+
285285
assertEquals(expected, products);
286-
}
287-
288-
@Test
289-
@PactTestFor(pactMethod = "getOneProduct")
290-
void getProductById_whenProductWithId10Exists(MockServer mockServer) {
286+
}
287+
288+
@Test
289+
@PactTestFor(pactMethod = "getOneProduct")
290+
void getProductById_whenProductWithId10Exists(MockServer mockServer) {
291291
Product expected = new Product();
292292
expected.setId("10");
293293
expected.setType("CREDIT_CARD");
294294
expected.setName("28 Degrees");
295-
295+
296296
RestTemplate restTemplate = new RestTemplateBuilder()
297-
.rootUri(mockServer.getUrl())
298-
.build();
297+
.rootUri(mockServer.getUrl())
298+
.build();
299299
Product product = new ProductService(restTemplate).getProduct("10");
300-
300+
301301
assertEquals(expected, product);
302-
}
302+
}
303+
304+
private Map<String, String> headers() {
305+
Map<String, String> headers = new HashMap<>();
306+
headers.put("Content-Type", "application/json; charset=utf-8");
307+
return headers;
308+
}
303309
}
304310
```
305311

@@ -327,6 +333,8 @@ A pact file should have been generated in *consumer/build/pacts/FrontendApplicat
327333

328334
*NOTE*: even if the API client had been graciously provided for us by our Provider Team, it doesn't mean that we shouldn't write contract tests - because the version of the client we have may not always be in sync with the deployed API - and also because we will write tests on the output appropriate to our specific needs.
329335

336+
Move on to [step 4](https://github.com/pact-foundation/pact-workshop-jvm-spring/tree/step4#step-4---verify-the-provider)
337+
330338
## Step 4 - Verify the provider
331339

332340
We will need to copy the Pact contract file that was produced from the consumer test into the Provider module. This will help us verify that the provider can meet the requirements as set out in the contract.

consumer/src/test/java/au/com/dius/pactworkshop/consumer/ProductConsumerPactTest.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import org.springframework.web.client.RestTemplate;
1414

1515
import java.util.Collections;
16+
import java.util.Arrays;
17+
import java.util.HashMap;
1618
import java.util.List;
1719
import java.util.Map;
1820

@@ -32,7 +34,7 @@ RequestResponsePact getAllProducts(PactDslWithProvider builder) {
3234
.path("/products")
3335
.willRespondWith()
3436
.status(200)
35-
.headers(Map.of("Content-Type", "application/json; charset=utf-8"))
37+
.headers(headers())
3638
.body(newJsonArrayMinLike(2, array ->
3739
array.object(object -> {
3840
object.stringType("id", "09");
@@ -51,7 +53,7 @@ RequestResponsePact noProductsExist(PactDslWithProvider builder) {
5153
.path("/products")
5254
.willRespondWith()
5355
.status(200)
54-
.headers(Map.of("Content-Type", "application/json; charset=utf-8"))
56+
.headers(headers())
5557
.body("[]")
5658
.toPact();
5759
}
@@ -64,7 +66,7 @@ RequestResponsePact getOneProduct(PactDslWithProvider builder) {
6466
.path("/product/10")
6567
.willRespondWith()
6668
.status(200)
67-
.headers(Map.of("Content-Type", "application/json; charset=utf-8"))
69+
.headers(headers())
6870
.body(newJsonBody(object -> {
6971
object.stringType("id", "10");
7072
object.stringType("type", "CREDIT_CARD");
@@ -91,7 +93,7 @@ void getAllProducts_whenProductsExist(MockServer mockServer) {
9193
product.setId("09");
9294
product.setType("CREDIT_CARD");
9395
product.setName("Gem Visa");
94-
List<Product> expected = List.of(product, product);
96+
List<Product> expected = Arrays.asList(product, product);
9597

9698
RestTemplate restTemplate = new RestTemplateBuilder()
9799
.rootUri(mockServer.getUrl())
@@ -139,4 +141,10 @@ void getProductById_whenProductWithId11DoesNotExist(MockServer mockServer) {
139141
() -> new ProductService(restTemplate).getProduct("11"));
140142
assertEquals(404, e.getRawStatusCode());
141143
}
144+
145+
private Map<String, String> headers() {
146+
Map<String, String> headers = new HashMap<>();
147+
headers.put("Content-Type", "application/json; charset=utf-8");
148+
return headers;
149+
}
142150
}

0 commit comments

Comments
 (0)