Skip to content

Commit

Permalink
Merge pull request #15 from YOU54F/step11
Browse files Browse the repository at this point in the history
Step11
  • Loading branch information
YOU54F authored Jan 30, 2024
2 parents d15384e + 2ebec6d commit 9aa6f34
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 91 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Maven CI

on:
push:
branches:
- step11
pull_request:
branches:
- step11

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

- uses: KengoTODA/actions-setup-docker-compose@v1
if: ${{ env.ACT }}
name: Install `docker-compose` for use with act
with:
version: '2.24.1'
- name: Start Pact Broker
run: docker-compose up -d

- name: Consumer - Publish Pact contracts
run: ./mvnw pact:publish -Dpact.publish.consumer.version=$(git rev-parse HEAD) -Dpact.publish.consumer.branchName=$(git rev-parse --abbrev-ref HEAD)
working-directory: consumer

- name: Provider - Pact verification
run: ./mvnw verify -Dpact.verifier.publishResults=true -Dpact.provider.version=$(git rev-parse HEAD) -Dpact.provider.branch=$(git rev-parse --abbrev-ref HEAD)
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
93 changes: 56 additions & 37 deletions README.md

Large diffs are not rendered by default.

28 changes: 12 additions & 16 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 All @@ -95,15 +95,11 @@
<plugin>
<groupId>au.com.dius.pact.provider</groupId>
<artifactId>maven</artifactId>
<version>4.1.17</version>
<version>4.6.5</version>
<configuration>
<pactBrokerUrl>http://localhost:9292</pactBrokerUrl>
<pactBrokerUsername>pact_workshop</pactBrokerUsername>
<pactBrokerPassword>pact_workshop</pactBrokerPassword>
<tags>
<tag>prod</tag>
<tag>test</tag>
</tags>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
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 org.springframework.web.client.HttpClientErrorException;

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

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

@Test
@PactTestFor(pactMethod = "allProducts")
@PactTestFor(pactMethod = "allProducts", pactVersion = PactSpecVersion.V3)
void testAllProducts(MockServer mockServer) {
productServiceClient.setBaseUrl(mockServer.getUrl());
List<Product> products = productServiceClient.fetchProducts().getProducts();
Expand Down Expand Up @@ -82,7 +82,7 @@ public RequestResponsePact singleProduct(PactDslWithProvider builder) {
}

@Test
@PactTestFor(pactMethod = "singleProduct")
@PactTestFor(pactMethod = "singleProduct", pactVersion = PactSpecVersion.V3)
void testSingleProduct(MockServer mockServer) {
productServiceClient.setBaseUrl(mockServer.getUrl());
Product product = productServiceClient.getProductById(10L);
Expand All @@ -105,7 +105,7 @@ public RequestResponsePact noProducts(PactDslWithProvider builder) {
}

@Test
@PactTestFor(pactMethod = "noProducts")
@PactTestFor(pactMethod = "noProducts", pactVersion = PactSpecVersion.V3)
void testNoProducts(MockServer mockServer) {
productServiceClient.setBaseUrl(mockServer.getUrl());
ProductServiceResponse products = productServiceClient.fetchProducts();
Expand All @@ -125,7 +125,7 @@ public RequestResponsePact singleProductNotExists(PactDslWithProvider builder) {
}

@Test
@PactTestFor(pactMethod = "singleProductNotExists")
@PactTestFor(pactMethod = "singleProductNotExists", pactVersion = PactSpecVersion.V3)
void testSingleProductNotExists(MockServer mockServer) {
productServiceClient.setBaseUrl(mockServer.getUrl());
try {
Expand All @@ -147,7 +147,7 @@ public RequestResponsePact noAuthToken(PactDslWithProvider builder) {
}

@Test
@PactTestFor(pactMethod = "noAuthToken")
@PactTestFor(pactMethod = "noAuthToken", pactVersion = PactSpecVersion.V3)
void testNoAuthToken(MockServer mockServer) {
productServiceClient.setBaseUrl(mockServer.getUrl());
try {
Expand All @@ -169,7 +169,7 @@ public RequestResponsePact noAuthToken2(PactDslWithProvider builder) {
}

@Test
@PactTestFor(pactMethod = "noAuthToken2")
@PactTestFor(pactMethod = "noAuthToken2", pactVersion = PactSpecVersion.V3)
void testNoAuthToken2(MockServer mockServer) {
productServiceClient.setBaseUrl(mockServer.getUrl());
try {
Expand Down
9 changes: 7 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@ services:
POSTGRES_PASSWORD: password
POSTGRES_DB: postgres

broker_app:
image: pactfoundation/pact-broker
pact-broker:
image: pactfoundation/pact-broker:latest-multi
links:
- postgres
ports:
- 9292:9292
restart: always
environment:
PACT_BROKER_BASIC_AUTH_USERNAME: pact_workshop
PACT_BROKER_BASIC_AUTH_PASSWORD: pact_workshop
PACT_BROKER_DATABASE_USERNAME: postgres
PACT_BROKER_DATABASE_PASSWORD: password
PACT_BROKER_DATABASE_HOST: postgres
PACT_BROKER_DATABASE_NAME: postgres
PACT_BROKER_DATABASE_CONNECT_MAX_RETRIES: "10"
PACT_BROKER_PUBLIC_HEARTBEAT: "true"
PACT_BROKER_WEBHOOK_SCHEME_WHITELIST: http
PACT_BROKER_WEBHOOK_HOST_WHITELIST: host.docker.internal
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
26 changes: 12 additions & 14 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 @@ -51,7 +52,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 @@ -65,13 +66,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 All @@ -92,15 +93,12 @@
<plugin>
<groupId>au.com.dius.pact.provider</groupId>
<artifactId>maven</artifactId>
<version>4.1.17</version>
<version>4.6.5</version>
<configuration>
<pactBrokerUrl>http://localhost:9292</pactBrokerUrl>
<pactBrokerUsername>pact_workshop</pactBrokerUsername>
<pactBrokerPassword>pact_workshop</pactBrokerPassword>
<tags>
<tag>prod</tag>
<tag>test</tag>
</tags>
<branchName>test</branchName>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Base64;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package io.pact.workshop.product_service.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
public class WebSecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.csrf(Customizer.withDefaults())
.addFilterBefore(new BearerAuthorizationFilter(), UsernamePasswordAuthenticationFilter.class);
return http.build();
}
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
import au.com.dius.pact.provider.junitsupport.State;
import au.com.dius.pact.provider.junitsupport.StateChangeAction;
import au.com.dius.pact.provider.junitsupport.loader.PactBroker;
import au.com.dius.pact.provider.junitsupport.loader.SelectorBuilder;
import au.com.dius.pact.provider.spring.junit5.PactVerificationSpringProvider;
import io.pact.workshop.product_service.products.Product;
import io.pact.workshop.product_service.products.ProductRepository;
import org.apache.http.HttpRequest;
import org.apache.hc.core5.http.HttpRequest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.boot.test.web.server.LocalServerPort;

import java.nio.ByteBuffer;
import java.util.Arrays;
Expand All @@ -38,6 +39,16 @@ void setup(PactVerificationContext context) {
context.setTarget(new HttpTestTarget("localhost", port));
}

@au.com.dius.pact.provider.junitsupport.loader.PactBrokerConsumerVersionSelectors
public static SelectorBuilder consumerVersionSelectors() {
// Select Pacts for consumers deployed or released to production, those on the main branch
// and those on a named branch step11, for use in our workshop
return new SelectorBuilder()
.deployedOrReleased()
.mainBranch()
.branch("step11");
}

@TestTemplate
@ExtendWith(PactVerificationSpringProvider.class)
void pactVerificationTestTemplate(PactVerificationContext context, HttpRequest request) {
Expand Down

0 comments on commit 9aa6f34

Please sign in to comment.