Skip to content

Commit f463eaa

Browse files
committed
chore: refactoring
1 parent 15474cb commit f463eaa

File tree

4 files changed

+118
-34
lines changed

4 files changed

+118
-34
lines changed

build.gradle

+16-26
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,47 @@ buildscript {
1010
}
1111
}
1212

13-
project.ext.buildConfig = [
14-
repoType: "DEFAULT",
15-
sourceCompatibility: 17,
16-
targetCompatibility: 17,
17-
]
18-
apply from: project.buildscript.classLoader.getResource('main.gradle').toURI()
19-
2013
allprojects {
2114
apply plugin: 'java-library'
22-
}
2315

24-
project('wiremock-pact-lib', {
2516
project.ext.buildConfig = [
2617
repoType: "DEFAULT",
2718
sourceCompatibility: 17,
2819
targetCompatibility: 17,
20+
staticCodeAnalysis: [
21+
maxViolations: 0
22+
]
2923
]
3024
apply from: project.buildscript.classLoader.getResource('main.gradle').toURI()
3125

26+
test {
27+
useJUnitPlatform()
28+
}
29+
30+
dependencies {
31+
testImplementation 'org.assertj:assertj-core:3.25.3'
32+
testImplementation platform('org.junit:junit-bom:5.10.2')
33+
testImplementation 'org.junit.jupiter:junit-jupiter'
34+
testImplementation 'org.junit.platform:junit-platform-launcher'
35+
}
36+
}
37+
38+
project('wiremock-pact-lib', {
3239
dependencies {
3340
implementation 'au.com.dius.pact.core:model:4.6.9'
3441
compileOnly 'org.wiremock:wiremock:3.5.4'
3542
}
3643
})
3744

3845
project('wiremock-pact-test', {
39-
project.ext.buildConfig = [
40-
repoType: "DEFAULT",
41-
sourceCompatibility: 17,
42-
targetCompatibility: 17,
43-
]
44-
apply from: project.buildscript.classLoader.getResource('main.gradle').toURI()
45-
4646
dependencies {
4747
testImplementation(project(":wiremock-pact-lib")) {
4848
exclude group: "org.apache.groovy", module: "groovy-xml"
4949
}
5050
testImplementation 'org.wiremock:wiremock:3.5.4'
51-
implementation 'org.assertj:assertj-core:3.25.3'
52-
testImplementation platform('org.junit:junit-bom:5.10.2')
53-
testImplementation 'org.junit.jupiter:junit-jupiter'
54-
testImplementation 'org.junit.platform:junit-platform-launcher'
5551
testImplementation("io.rest-assured:rest-assured:5.4.0") {
5652
exclude group: "org.apache.groovy", module: "groovy-xml"
5753
}
5854
testImplementation "org.apache.groovy:groovy-xml:4.0.18"
59-
6055
}
61-
62-
test {
63-
useJUnitPlatform()
64-
}
65-
6656
})

wiremock-pact-lib/src/main/java/se/bjurr/wiremockpact/wiremockpactlib/api/WiremockPactApi.java

-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import com.github.tomakehurst.wiremock.client.WireMock;
1414
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
1515
import java.util.List;
16-
import java.util.Objects;
1716

1817
public final class WiremockPactApi {
1918
private String consumerName = "consumer-name";
@@ -44,7 +43,6 @@ public void toPact(final String pactDir) {
4443

4544
/** Record all given Wiremock requests to PACT. */
4645
public void toPact(final List<LoggedRequest> wiremockRequests, final String pactDir) {
47-
Objects.requireNonNull(wiremockRequests);
4846
final Consumer consumer = new Consumer(this.consumerName);
4947
final Provider provider = new Provider(this.providerName);
5048
final V4Pact v4 = new V4Pact(consumer, provider);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package se.bjurr.wiremockpact.wiremockpact.test;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import com.github.tomakehurst.wiremock.client.WireMock;
6+
import io.restassured.RestAssured;
7+
import io.restassured.http.ContentType;
8+
import org.junit.jupiter.api.Test;
9+
import se.bjurr.wiremockpact.wiremockpact.testutils.BaseTest;
10+
import se.bjurr.wiremockpact.wiremockpactlib.api.WiremockPactApi;
11+
12+
public class ExampleUsageTest extends BaseTest {
13+
14+
@Test
15+
public void testThatGetRequestCanGeneratePact() throws Exception {
16+
// Setup Wiremock
17+
WireMock.stubFor(WireMock.get("/getrequest").willReturn(WireMock.ok()));
18+
19+
// Invoke your code and let it invoke Wiremock
20+
RestAssured.given()
21+
.log()
22+
.all()
23+
.accept(ContentType.JSON)
24+
.when()
25+
.get("/getrequest")
26+
.then()
27+
.statusCode(200);
28+
29+
// Use this library to create PACT json
30+
WiremockPactApi.builder() //
31+
.consumerName(this.me) //
32+
.providerName(this.you) //
33+
.toPact(this.tmpdir.toString());
34+
35+
final String pactContent = this.readPactFileContent(this.tmpdir, this.you, this.me);
36+
37+
// And the content of that pact file will be
38+
assertThat(pactContent)
39+
.isEqualToIgnoringWhitespace(
40+
"""
41+
{
42+
"consumer": {
43+
"name": "this-is-me"
44+
},
45+
"interactions": [
46+
{
47+
"description": "generated from Wiremock",
48+
"key": "2b57c74e",
49+
"pending": false,
50+
"request": {
51+
"body": {
52+
"content": ""
53+
},
54+
"method": "GET",
55+
"path": "/getrequest"
56+
},
57+
"response": {
58+
"status": 200
59+
},
60+
"type": "Synchronous/HTTP"
61+
}
62+
],
63+
"metadata": {
64+
"pact-jvm": {
65+
"version": "4.6.9"
66+
},
67+
"pactSpecification": {
68+
"version": "4.0"
69+
}
70+
},
71+
"provider": {
72+
"name": "this-is-you"
73+
}
74+
}
75+
""");
76+
}
77+
}

wiremock-pact-test/src/test/java/se/bjurr/wiremockpact/wiremockpact/testutils/BaseTest.java

+25-6
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,43 @@
1414
@WireMockTest
1515
public class BaseTest {
1616

17+
protected Path tmpdir;
18+
protected String you;
19+
protected String me;
20+
1721
@BeforeEach
1822
public void before(final WireMockRuntimeInfo wmRuntimeInfo) {
1923
RestAssured.baseURI = "http://localhost:" + wmRuntimeInfo.getHttpPort();
24+
this.tmpdir = this.getTempDir();
25+
this.you = "this-is-you";
26+
this.me = "this-is-me";
27+
}
28+
29+
public Path getTempDir() {
30+
try {
31+
return Files.createTempDirectory(this.getClass().getName() + "-tempfile");
32+
} catch (final IOException e) {
33+
throw new RuntimeException(e.getMessage(), e);
34+
}
35+
}
36+
37+
public String readPactFileContent(final Path tmpdir, final String you, final String me)
38+
throws IOException {
39+
final Path pactFile = tmpdir.resolve(me + "-" + you + ".json");
40+
final String pactContent = Files.readString(pactFile);
41+
return pactContent;
2042
}
2143

2244
public void assertPactEquals(final String expected) {
23-
Path tmpdir = null;
24-
Path pactFile = null;
2545
try {
26-
tmpdir = Files.createTempDirectory(this.getClass().getName() + "-tempfile");
46+
final Path tmpdir = this.getTempDir();
2747
final String you = "this-is-you";
2848
final String me = "this-is-me";
2949
WiremockPactApi.builder().consumerName(me).providerName(you).toPact(tmpdir.toString());
30-
pactFile = tmpdir.resolve(me + "-" + you + ".json");
31-
final String pactContent = Files.readString(pactFile);
50+
final String pactContent = this.readPactFileContent(tmpdir, you, me);
3251
assertThat(pactContent).isEqualToIgnoringWhitespace(expected);
3352
} catch (final IOException e) {
34-
throw new RuntimeException(pactFile.toString(), e);
53+
throw new RuntimeException(e.getMessage(), e);
3554
}
3655
}
3756
}

0 commit comments

Comments
 (0)