Skip to content

Commit 7810c5e

Browse files
author
Ronald Holshausen
committed
Merge branch 'step11'
2 parents b8d764f + f6f4dbd commit 7810c5e

File tree

12 files changed

+361
-307
lines changed

12 files changed

+361
-307
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ provider/src/test/resources/pacts
1616
.sts4-cache
1717

1818
### IntelliJ IDEA ###
19-
.idea
19+
.idea/
2020
*.iws
2121
*.iml
2222
*.ipr

.idea/.gitignore

-8
This file was deleted.

.idea/misc.xml

-11
This file was deleted.

.idea/vcs.xml

-6
This file was deleted.

README.md

+328-266
Large diffs are not rendered by default.

consumer/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ buildscript {
1010
plugins {
1111
id 'org.springframework.boot' version '2.3.4.RELEASE'
1212
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
13-
id 'au.com.dius.pact' version '4.1.6'
13+
id 'au.com.dius.pact' version '4.1.7'
1414
}
1515

1616
group = 'au.com.dius.pactworkshop'
1717
version = '0.0.1-SNAPSHOT'
18-
sourceCompatibility = '11'
18+
sourceCompatibility = '8'
1919

2020

2121

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

@@ -33,7 +35,7 @@ RequestResponsePact getAllProducts(PactDslWithProvider builder) {
3335
.matchHeader("Authorization", "Bearer (19|20)\\d\\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T([01][1-9]|2[0123]):[0-5][0-9]")
3436
.willRespondWith()
3537
.status(200)
36-
.headers(Map.of("Content-Type", "application/json; charset=utf-8"))
38+
.headers(headers())
3739
.body(newJsonArrayMinLike(2, array ->
3840
array.object(object -> {
3941
object.stringType("id", "09");
@@ -53,7 +55,7 @@ RequestResponsePact noProductsExist(PactDslWithProvider builder) {
5355
.matchHeader("Authorization", "Bearer (19|20)\\d\\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T([01][1-9]|2[0123]):[0-5][0-9]")
5456
.willRespondWith()
5557
.status(200)
56-
.headers(Map.of("Content-Type", "application/json; charset=utf-8"))
58+
.headers(headers())
5759
.body("[]")
5860
.toPact();
5961
}
@@ -78,7 +80,7 @@ RequestResponsePact getOneProduct(PactDslWithProvider builder) {
7880
.matchHeader("Authorization", "Bearer (19|20)\\d\\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T([01][1-9]|2[0123]):[0-5][0-9]")
7981
.willRespondWith()
8082
.status(200)
81-
.headers(Map.of("Content-Type", "application/json; charset=utf-8"))
83+
.headers(headers())
8284
.body(newJsonBody(object -> {
8385
object.stringType("id", "10");
8486
object.stringType("type", "CREDIT_CARD");
@@ -117,7 +119,7 @@ void getAllProducts_whenProductsExist(MockServer mockServer) {
117119
product.setId("09");
118120
product.setType("CREDIT_CARD");
119121
product.setName("Gem Visa");
120-
List<Product> expected = List.of(product, product);
122+
List<Product> expected = Arrays.asList(product, product);
121123

122124
RestTemplate restTemplate = new RestTemplateBuilder()
123125
.rootUri(mockServer.getUrl())
@@ -189,4 +191,10 @@ void getProductById_whenNoAuth(MockServer mockServer) {
189191
() -> new ProductService(restTemplate).getProduct("10"));
190192
assertEquals(401, e.getRawStatusCode());
191193
}
194+
195+
private Map<String, String> headers() {
196+
Map<String, String> headers = new HashMap<>();
197+
headers.put("Content-Type", "application/json; charset=utf-8");
198+
return headers;
199+
}
192200
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.springframework.boot.web.client.RestTemplateBuilder;
88
import org.springframework.web.client.RestTemplate;
99

10+
import java.util.Arrays;
1011
import java.util.List;
1112

1213
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
@@ -49,7 +50,7 @@ void getAllProducts() {
4950
"{\"id\":\"10\",\"type\":\"CREDIT_CARD\",\"name\":\"28 Degrees\",\"version\":\"v1\"}"+
5051
"]")));
5152

52-
List<Product> expected = List.of(new Product("9", "CREDIT_CARD", "GEM Visa", "v2"),
53+
List<Product> expected = Arrays.asList(new Product("9", "CREDIT_CARD", "GEM Visa", "v2"),
5354
new Product("10", "CREDIT_CARD", "28 Degrees", "v1"));
5455

5556
List<Product> products = productService.getAllProducts();
589 KB
Loading

provider/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55

66
group = 'au.com.dius.pactworkshop'
77
version = '0.0.1-SNAPSHOT'
8-
sourceCompatibility = '11'
8+
sourceCompatibility = '8'
99

1010
dependencies {
1111
implementation 'org.springframework.boot:spring-boot-starter'

provider/src/main/java/au/com/dius/pactworkshop/provider/ProductRepository.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,32 @@
22

33
import org.springframework.stereotype.Repository;
44

5+
import java.util.ArrayList;
6+
import java.util.HashMap;
57
import java.util.List;
68
import java.util.Map;
79
import java.util.Optional;
810

911
@Repository
1012
public class ProductRepository {
1113

12-
private final Map<String, Product> PRODUCTS = Map.of(
13-
"09", new Product("09", "CREDIT_CARD", "Gem Visa", "v1"),
14-
"10", new Product("10", "CREDIT_CARD", "28 Degrees", "v1"),
15-
"11", new Product("11", "PERSONAL_LOAN", "MyFlexiPay", "v2")
16-
);
14+
private final Map<String, Product> PRODUCTS = new HashMap<>();
1715

1816
public List<Product> fetchAll() {
19-
return List.copyOf(PRODUCTS.values());
17+
initProducts();
18+
19+
return new ArrayList<>(PRODUCTS.values());
2020
}
2121

2222
public Optional<Product> getById(String id) {
23+
initProducts();
24+
2325
return Optional.ofNullable(PRODUCTS.get(id));
2426
}
2527

28+
private void initProducts() {
29+
PRODUCTS.put("09", new Product("09", "CREDIT_CARD", "Gem Visa", "v1"));
30+
PRODUCTS.put("10", new Product("10", "CREDIT_CARD", "28 Degrees", "v1"));
31+
PRODUCTS.put("11", new Product("11", "PERSONAL_LOAN", "MyFlexiPay", "v2"));
32+
}
2633
}

provider/src/test/java/au/com/dius/pactworkshop/provider/ProductPactProviderTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.springframework.test.context.junit.jupiter.SpringExtension;
1919

2020
import java.text.SimpleDateFormat;
21+
import java.util.Arrays;
2122
import java.util.Collections;
2223
import java.util.Date;
2324
import java.util.List;
@@ -65,7 +66,7 @@ private void replaceAuthHeader(HttpRequest request) {
6566
@State("products exist")
6667
void toProductsExistState() {
6768
when(productRepository.fetchAll()).thenReturn(
68-
List.of(new Product("09", "CREDIT_CARD", "Gem Visa", "v1"),
69+
Arrays.asList(new Product("09", "CREDIT_CARD", "Gem Visa", "v1"),
6970
new Product("10", "CREDIT_CARD", "28 Degrees", "v1")));
7071
}
7172

0 commit comments

Comments
 (0)