Skip to content

Commit

Permalink
get rid of pact, using wiremock instead
Browse files Browse the repository at this point in the history
  • Loading branch information
gy2006 committed Feb 25, 2025
1 parent f5f180e commit f6dddad
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 84 deletions.
23 changes: 12 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<spring-boot.version>3.3.7</spring-boot.version>
<spring-boot.version>3.3.8</spring-boot.version>
<instancio-junit.version>2.9.0</instancio-junit.version>
<testcontainers.version>1.20.4</testcontainers.version>
<springdoc-openapi.version>2.5.0</springdoc-openapi.version>
Expand All @@ -26,6 +26,8 @@
<maven-surefire-plugin.version>3.5.2</maven-surefire-plugin.version>
<hypersistence-utils-hibernate-63.version>3.9.0</hypersistence-utils-hibernate-63.version>
<jackson.version>2.17.3</jackson.version>
<wiremock-spring-boot.version>3.7.0</wiremock-spring-boot.version>
<httpclient5.version>5.3.1</httpclient5.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -153,6 +155,12 @@
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>${httpclient5.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -166,16 +174,9 @@
</dependency>

<dependency>
<groupId>au.com.dius.pact.provider</groupId>
<artifactId>junit5</artifactId>
<version>${pact.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>au.com.dius.pact.consumer</groupId>
<artifactId>junit5</artifactId>
<version>${pact.version}</version>
<groupId>org.wiremock.integrations</groupId>
<artifactId>wiremock-spring-boot</artifactId>
<version>${wiremock-spring-boot.version}</version>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
package com.flowci.flow.business;

import au.com.dius.pact.consumer.MockServer;
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.consumer.junit5.PactConsumerTest;
import au.com.dius.pact.consumer.junit5.PactIgnore;
import au.com.dius.pact.consumer.junit5.PactTestFor;
import au.com.dius.pact.core.model.V4Pact;
import au.com.dius.pact.core.model.annotations.Pact;
import com.flowci.SpringTest;
import com.flowci.common.exception.NotAvailableException;
import com.flowci.flow.model.YamlTemplate;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.cache.CacheManager;
import org.springframework.http.HttpHeaders;

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

import static org.junit.jupiter.api.Assertions.*;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.ok;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.when;

@PactConsumerTest
class FetchTemplateContentTest extends SpringTest {

@RegisterExtension
private final static WireMockExtension WireMockServer =
WireMockExtension.newInstance()
.options(wireMockConfig().dynamicPort())
.build();

@MockBean
private FetchTemplates fetchTemplates;

Expand All @@ -36,24 +38,13 @@ class FetchTemplateContentTest extends SpringTest {
@Autowired
private CacheManager yamlTemplateCacheManager;

@Pact(provider = "TemplateContentProvider", consumer = "consumer_template_content")
V4Pact createPact(PactDslWithProvider builder) throws IOException {
return builder
.given("maven template")
.uponReceiving("")
.path("/git/templates/helloworld.yaml")
.method("GET")
.willRespondWith()
.status(200)
.headers(Map.of(HttpHeaders.CONTENT_TYPE, "application/yaml"))
.body(getResourceAsString("template_helloworld.yaml"))
.toPact(V4Pact.class);
}

@Test
@PactTestFor(providerName = "TemplateContentProvider")
void whenFetchingContentSuccessfully_thenReturnYamlContentFromPact(MockServer mockServer) {
var mockUrl = mockServer.getUrl() + "/git/templates/helloworld.yaml";
void whenFetchingContentSuccessfully_thenReturnYamlContent() {
var mockUrl = WireMockServer.url("/git/templates/helloworld.yaml");
WireMockServer.stubFor(
get("/git/templates/helloworld.yaml")
.willReturn(ok(getResourceAsString("template_helloworld.yaml"))));

when(fetchTemplates.invoke())
.thenReturn(List.of(
new YamlTemplate(
Expand All @@ -73,7 +64,6 @@ void whenFetchingContentSuccessfully_thenReturnYamlContentFromPact(MockServer mo
}

@Test
@PactIgnore
void whenFetchingContentNotFound_thenThrowException() {
when(fetchTemplates.invoke()).thenReturn(Collections.emptyList());
assertThrows(NotAvailableException.class, () -> fetchTemplateContent.invoke("helloworld"));
Expand Down
70 changes: 27 additions & 43 deletions src/test/java/com/flowci/flow/business/FetchTemplatesTest.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package com.flowci.flow.business;

import au.com.dius.pact.consumer.MockServer;
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.consumer.junit5.PactConsumerTest;
import au.com.dius.pact.consumer.junit5.PactTestFor;
import au.com.dius.pact.core.model.V4Pact;
import au.com.dius.pact.core.model.annotations.Pact;
import com.flowci.SpringTest;
import com.flowci.common.config.AppProperties;
import com.flowci.common.exception.NotAvailableException;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.http.HttpHeaders;

import java.io.IOException;
import java.util.Map;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.junit.jupiter.api.Assertions.*;

@PactConsumerTest
class FetchTemplatesTest extends SpringTest {

@RegisterExtension
private final static WireMockExtension WireMockServer =
WireMockExtension.newInstance()
.options(wireMockConfig().dynamicPort())
.build();

@Autowired
private AppProperties appProperties;

Expand All @@ -31,46 +31,30 @@ class FetchTemplatesTest extends SpringTest {
@Autowired
private CacheManager yamlTemplateCacheManager;

@Pact(provider = "YamlTemplateProvider", consumer = "consumer_yaml_template")
V4Pact createPact(PactDslWithProvider builder) throws IOException {
return builder
.given("yaml templates are fetched")
.uponReceiving("")
.path("/git/templates.json")
.method("GET")
.willRespondWith()
.status(200)
.headers(Map.of(HttpHeaders.CONTENT_TYPE, "application/json"))
.body(getResourceAsString("flow_templates.json"))
.toPact(V4Pact.class);
}

@Pact(provider = "YamlTemplateProviderWith5xx", consumer = "consumer_yaml_template")
V4Pact createPactWith5xx(PactDslWithProvider builder) throws IOException {
return builder
.given("yaml templates are fetched")
.uponReceiving("")
.path("/git/templates.json")
.method("GET")
.willRespondWith()
.status(500)
.toPact(V4Pact.class);
}

@Test
@PactTestFor(providerName = "YamlTemplateProvider")
void whenFetchingSuccessfully_thenReturnListOfTemplates(MockServer mockServer) {
appProperties.getTemplates().setUrl(mockServer.getUrl() + "/git/templates.json");
void whenFetchingSuccessfully_thenReturnListOfTemplates() {
WireMockServer.stubFor(get("/git/templates.json")
.willReturn(aResponse()
.withBody(getResourceAsString("flow_templates.json"))
.withStatus(200)
.withHeader("Content-Type", "application/json")));

appProperties.getTemplates()
.setUrl(WireMockServer.url("/git/templates.json"));

var templates = fetchTemplates.invoke();
assertNotNull(templates);
assertEquals(2, templates.size());
}

@Test
@PactTestFor(providerName = "YamlTemplateProviderWith5xx")
void whenFetchingWith5xx_thenReturnListOfTemplates(MockServer mockServer) {
appProperties.getTemplates().setUrl(mockServer.getUrl() + "/git/templates.json");
void whenFetchingWith5xx_thenThrowNotAvailableException() {
WireMockServer.stubFor(get("/git/templates.json")
.willReturn(aResponse().withStatus(500)));

appProperties.getTemplates()
.setUrl(WireMockServer.url("/git/templates.json"));

assertThrows(NotAvailableException.class, () -> fetchTemplates.invoke());
}
}

0 comments on commit f6dddad

Please sign in to comment.