Skip to content

Commit

Permalink
Merge pull request #7 from YOU54F/step4
Browse files Browse the repository at this point in the history
Step4
  • Loading branch information
YOU54F authored Jan 30, 2024
2 parents 72767fd + af5b64f commit 9477837
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 78 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Maven CI

on:
push:
branches:
- step4
pull_request:
branches:
- step4

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up JDK
uses: actions/setup-java@v2
with:
java-version: '17'
distribution: 'temurin'

- name: Consumer - Pact tests
run: ./mvnw verify
working-directory: consumer

- name: Consumer - Move Contracts to Provider
run: cp consumer/target/pacts/ProductCatalogue-ProductService.json provider/pacts

- name: Provider - Pact verification
run: ./mvnw verify || true
working-directory: provider
1 change: 1 addition & 0 deletions .java-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
17.0
85 changes: 44 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ If running this as a team workshop format, you may want to take a look through t

## Requirements

- JDK 8 or above
- Maven 3
- JDK 17+
- Maven 3+
- Docker for step 11

## Scenario
Expand Down Expand Up @@ -126,7 +126,8 @@ You can see the client interface test we created in `consumer/src/test/java/io/p
);

Product product = productServiceClient.getProductById(10);
assertThat(product, is(equalTo(new Product(10L, "28 Degrees", "CREDIT_CARD", "v1"))));
assertThat(product, is(equalTo(new Product(10L, "28 Degrees", "CREDIT_CARD", "v1", null))));

}
```

Expand All @@ -146,7 +147,7 @@ consumer ❯ ./mvnw verify
[INFO] Building product-catalogue 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ product-catalogue ---
[INFO] --- maven-resources-plugin:3.3.0:resources (default-resources) @ product-catalogue ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
Expand All @@ -155,7 +156,7 @@ consumer ❯ ./mvnw verify
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ product-catalogue ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ product-catalogue ---
[INFO] --- maven-resources-plugin:3.3.0:testResources (default-testResources) @ product-catalogue ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory /home/ronald/Development/Projects/Pact/pact-workshop-Maven-Springboot-JUnit5/consumer/src/test/resources
Expand All @@ -181,7 +182,7 @@ consumer ❯ ./mvnw verify
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ product-catalogue ---
[INFO] --- maven-jar-plugin:3.3.0:jar (default-jar) @ product-catalogue ---
[INFO]
[INFO] --- spring-boot-maven-plugin:2.4.3:repackage (repackage) @ product-catalogue ---
[INFO] Replacing main artifact with repackaged archive
Expand Down Expand Up @@ -320,57 +321,59 @@ class ProductServiceClientPactTest {
public RequestResponsePact allProducts(PactDslWithProvider builder) {
return builder
.given("products exists")
.uponReceiving("get all products")
.path("/products")
.uponReceiving("get all products")
.path("/products")
.willRespondWith()
.status(200)
.body(
new PactDslJsonBody()
.minArrayLike("products", 1, 2)
.integerType("id", 9L)
.stringType("name", "Gem Visa")
.stringType("type", "CREDIT_CARD")
.closeObject()
.closeArray()
)
.status(200)
.body(
new PactDslJsonBody()
.minArrayLike("products", 1, 2)
.integerType("id", 9L)
.stringType("name", "Gem Visa")
.stringType("type", "CREDIT_CARD")
.closeObject()
.closeArray()
)
.toPact();
}

@Test
@PactTestFor(pactMethod = "allProducts", pactVersion = PactSpecVersion.V3)
void testAllProducts(MockServer mockServer) {
productServiceClient.setBaseUrl(mockServer.getUrl());
List<Product> products = productServiceClient.fetchProducts().getProducts();
assertThat(products, hasSize(2));
assertThat(products.get(0), is(equalTo(new Product(9L, "Gem Visa", "CREDIT_CARD", null, null))));
}

@Pact(consumer = "ProductCatalogue")
public RequestResponsePact singleProduct(PactDslWithProvider builder) {
return builder
.given("product with ID 10 exists", "id", 10)
.uponReceiving("get product with ID 10")
.path("/products/10")
.path("/products/10")
.willRespondWith()
.status(200)
.body(
new PactDslJsonBody()
.integerType("id", 10L)
.stringType("name", "28 Degrees")
.stringType("type", "CREDIT_CARD")
.stringType("code", "CC_001")
.stringType("version", "v1")
)
.status(200)
.body(
new PactDslJsonBody()
.integerType("id", 10L)
.stringType("name", "28 Degrees")
.stringType("type", "CREDIT_CARD")
.stringType("code", "CC_001")
.stringType("version", "v1")
)
.toPact();
}

@Test
@PactTestFor(pactMethod = "allProducts", port="9999")
void testAllProducts(MockServer mockServer) throws IOException {
@PactTestFor(pactMethod = "singleProduct", pactVersion = PactSpecVersion.V3)
void testSingleProduct(MockServer mockServer) {
productServiceClient.setBaseUrl(mockServer.getUrl());
List<Product> products = productServiceClient.fetchProducts().getProducts();
assertThat(products, hasSize(2));
assertThat(products.get(0), is(equalTo(new Product(9L, "Gem Visa", "CREDIT_CARD", null, null))));
}

@Test
@PactTestFor(pactMethod = "singleProduct", port="9999")
void testSingleProduct(MockServer mockServer) throws IOException {
Product product = productServiceClient.getProductById(10L);
assertThat(product, is(equalTo(new Product(10L, "28 Degrees", "CREDIT_CARD", "v1", "CC_001"))));
}
}

```


Expand All @@ -390,7 +393,7 @@ consumer ❯ ./mvnw verify
[INFO] Building product-catalogue 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ product-catalogue ---
[INFO] --- maven-resources-plugin:3.3.0:resources (default-resources) @ product-catalogue ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
Expand All @@ -399,7 +402,7 @@ consumer ❯ ./mvnw verify
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ product-catalogue ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ product-catalogue ---
[INFO] --- maven-resources-plugin:3.3.0:testResources (default-testResources) @ product-catalogue ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory /home/ronald/Development/Projects/Pact/pact-workshop-Maven-Springboot-JUnit5/consumer/src/test/resources
Expand All @@ -422,7 +425,7 @@ consumer ❯ ./mvnw verify
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ product-catalogue ---
[INFO] --- maven-jar-plugin:3.3.0:jar (default-jar) @ product-catalogue ---
[INFO] Building jar: /home/ronald/Development/Projects/Pact/pact-workshop-Maven-Springboot-JUnit5/consumer/target/product-catalogue-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.4.3:repackage (repackage) @ product-catalogue ---
Expand Down
22 changes: 11 additions & 11 deletions consumer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<version>3.1.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.pact.workshop</groupId>
Expand All @@ -15,9 +15,9 @@
<description>Product Catalogue service for Pact Workshop</description>

<properties>
<java.version>1.8</java.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

<dependencies>
Expand All @@ -40,14 +40,14 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
<version>2.27.2</version>
<groupId>org.wiremock</groupId>
<artifactId>wiremock</artifactId>
<version>3.0.1</version>
<scope>test</scope>
</dependency>

Expand All @@ -68,20 +68,20 @@
<dependency>
<groupId>au.com.dius.pact.consumer</groupId>
<artifactId>junit5</artifactId>
<version>4.1.7</version>
<version>4.6.5</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
<version>4.4</version>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.1</version>
<version>4.12.0</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import au.com.dius.pact.consumer.junit5.PactTestFor;
import au.com.dius.pact.core.model.RequestResponsePact;
import au.com.dius.pact.core.model.annotations.Pact;
import au.com.dius.pact.core.model.PactSpecVersion; // required for v4.6.x to set pactVersion
import io.pact.workshop.product_catalogue.models.Product;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -48,6 +48,15 @@ public RequestResponsePact allProducts(PactDslWithProvider builder) {
.toPact();
}

@Test
@PactTestFor(pactMethod = "allProducts", pactVersion = PactSpecVersion.V3)
void testAllProducts(MockServer mockServer) {
productServiceClient.setBaseUrl(mockServer.getUrl());
List<Product> products = productServiceClient.fetchProducts().getProducts();
assertThat(products, hasSize(2));
assertThat(products.get(0), is(equalTo(new Product(9L, "Gem Visa", "CREDIT_CARD", null, null))));
}

@Pact(consumer = "ProductCatalogue")
public RequestResponsePact singleProduct(PactDslWithProvider builder) {
return builder
Expand All @@ -68,17 +77,9 @@ public RequestResponsePact singleProduct(PactDslWithProvider builder) {
}

@Test
@PactTestFor(pactMethod = "allProducts", port="9999")
void testAllProducts(MockServer mockServer) throws IOException {
@PactTestFor(pactMethod = "singleProduct", pactVersion = PactSpecVersion.V3)
void testSingleProduct(MockServer mockServer) {
productServiceClient.setBaseUrl(mockServer.getUrl());
List<Product> products = productServiceClient.fetchProducts().getProducts();
assertThat(products, hasSize(2));
assertThat(products.get(0), is(equalTo(new Product(9L, "Gem Visa", "CREDIT_CARD", null, null))));
}

@Test
@PactTestFor(pactMethod = "singleProduct", port="9999")
void testSingleProduct(MockServer mockServer) throws IOException {
Product product = productServiceClient.getProductById(10L);
assertThat(product, is(equalTo(new Product(10L, "28 Degrees", "CREDIT_CARD", "v1", "CC_001"))));
}
Expand Down
1 change: 1 addition & 0 deletions provider/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.jpa.defer-datasource-initialization=true
19 changes: 10 additions & 9 deletions provider/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<version>3.1.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.pact.workshop</groupId>
<artifactId>product-service</artifactId>
<version>1.0-SNAPSHOT</version>
<description>Product Service for Pact Workshop</description>

<properties>
<java.version>1.8</java.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>


<dependencies>
<dependency>
Expand Down Expand Up @@ -46,7 +47,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>

Expand All @@ -60,13 +61,13 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
<version>4.4</version>
</dependency>

<dependency>
<groupId>au.com.dius.pact.provider</groupId>
<artifactId>junit5spring</artifactId>
<version>4.1.17</version>
<version>4.6.5</version>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.Id;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
@Data
Expand Down
6 changes: 3 additions & 3 deletions provider/src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
insert into product (id, name, type, version, code) values (1, 'Gem Visa', 'CREDIT_CARD', 'v1', 'CC_01_VISA');
insert into product (id, name, type, version, code) values (2, '28 Degrees', 'CREDIT_CARD', 'v1', 'CC_02_OTH');
insert into product (id, name, type, version, code) values (3, 'MyFlexiPay', 'PERSONAL_LOAN', 'v2', 'LN_PERS_01');
insert into product (id, name, type, version, code) values (9, 'Gem Visa', 'CREDIT_CARD', 'v1', 'CC_01_VISA');
insert into product (id, name, type, version, code) values (10, '28 Degrees', 'CREDIT_CARD', 'v1', 'CC_02_OTH');
insert into product (id, name, type, version, code) values (11, 'MyFlexiPay', 'PERSONAL_LOAN', 'v2', 'LN_PERS_01');
Loading

0 comments on commit 9477837

Please sign in to comment.