Skip to content

Commit a390e1a

Browse files
author
APAC\PMUKIRI
committed
chore: BasicJavaPactConsumerAndProvider
Christoph Grüninger on behalf of Mercedes-Benz Tech Innovation GmbH.
1 parent c002e9a commit a390e1a

File tree

4 files changed

+187
-0
lines changed

4 files changed

+187
-0
lines changed

consumer/junit5/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,43 @@ variable to 'true'.
368368
By default, the Pact lifecycle will be invoked for every test method and will expect there to be a method annotated
369369
with `@Pact` for each test method invoked. To add non-Pact tests, just annotate the non-Pact test method with the
370370
`@PactIgnore` annotation.
371+
372+
# ClassicJavaPact - Setup
373+
374+
1. Install Windows Subsystem for Linux (WSL).
375+
2. Install Docker in the WSL.
376+
[version of docker: Docker version 24.0.5, build 24.0.5-0ubuntu1~22.04.1]
377+
3. Install Docker-compose plugin in WSL.
378+
1. Download Docker Compose
379+
```bash
380+
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
381+
```
382+
2. Make the Docker Compose binary executable
383+
```bash
384+
sudo chmod +x /usr/local/bin/docker-compose
385+
```
386+
3. Verify the installation
387+
```bash
388+
docker-compose --version
389+
```
390+
4. Run the Docker compose command to run Pact-broker.yml file
391+
```bash
392+
docker-compose up -d
393+
```
394+
5. Run the ClassicJavaPactTest in Consumer/Junit5 package
395+
6. Run the docker-compose file in resources of Junit5 in wsl
396+
7. Then publish the interaction file to localserver at 9292 port. By using plugin and below gradle task:
397+
```bash
398+
id "au.com.dius.pact" version "4.3.10"
399+
```
400+
```bash
401+
//./gradlew pactPublish --> command to publish pact file to pact broker.
402+
pact {
403+
publish {
404+
pactDirectory = file('build/pacts/samples')
405+
pactBrokerUrl = 'http://localhost:9292'
406+
consumerVersion = '1.0.0'
407+
}
408+
}
409+
```
410+
8. Run the ClassicJavaPactTest in Provider/Junit5 package
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package au.com.dius.pact.consumer.junit5;
2+
3+
import au.com.dius.pact.consumer.MockServer;
4+
import au.com.dius.pact.consumer.dsl.DslPart;
5+
import au.com.dius.pact.consumer.dsl.LambdaDsl;
6+
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
7+
import au.com.dius.pact.core.model.PactSpecVersion;
8+
import au.com.dius.pact.core.model.RequestResponsePact;
9+
import au.com.dius.pact.core.model.annotations.Pact;
10+
import au.com.dius.pact.core.model.annotations.PactDirectory;
11+
import org.apache.hc.client5.http.fluent.Request;
12+
import org.apache.hc.core5.http.ClassicHttpResponse;
13+
import org.apache.hc.core5.http.ContentType;
14+
import org.junit.jupiter.api.Assertions;
15+
import org.junit.jupiter.api.Nested;
16+
import org.junit.jupiter.api.Test;
17+
import org.junit.jupiter.api.extension.ExtendWith;
18+
import java.io.IOException;
19+
20+
@ExtendWith(PactConsumerTestExt.class)
21+
@PactTestFor(providerName = "ProviderTestPost", pactVersion = PactSpecVersion.V3)
22+
@PactDirectory("build/pacts/samples")
23+
public class ClassicJavaPactTest {
24+
25+
public static DslPart getDslPartiPart() throws IOException {
26+
return LambdaDsl.newJsonBody((body) -> {
27+
body.stringMatcher("firstName",".*", "Prudhvi");
28+
body.stringMatcher("lastName",".*","Raj");
29+
}).build();
30+
}
31+
32+
@Nested
33+
class Test1 {
34+
35+
@Pact(provider = "ProviderTestPost", consumer = "ConsumerTestPost")
36+
RequestResponsePact createFragment1(PactDslWithProvider builder) throws IOException {
37+
return builder
38+
.given("User Prudhvi")
39+
.uponReceiving("a post call for Prudhvi")
40+
.path("/users/prudhvi")
41+
.method("POST")
42+
.body(getDslPartiPart())
43+
.willRespondWith()
44+
.status(200)
45+
.toPact();
46+
}
47+
48+
@Test
49+
void runTestPostPrudhvi(MockServer mockServer) throws IOException {
50+
ClassicHttpResponse postResponse = (ClassicHttpResponse) Request.post(mockServer.getUrl() + "/users/prudhvi")
51+
.bodyString("{\n" +
52+
" \"firstName\": \"Prudhvi\",\n" +
53+
" \"lastName\": \"Raj\"\n" +
54+
"}", ContentType.APPLICATION_JSON)
55+
.execute().returnResponse();
56+
Assertions.assertEquals(postResponse.getCode(), 200);
57+
}
58+
}
59+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
version: "3"
2+
3+
services:
4+
postgres:
5+
image: postgres
6+
healthcheck:
7+
test: psql postgres --command "select 1" -U postgres
8+
volumes:
9+
- postgres-volume:/var/lib/postgresql/data
10+
ports:
11+
- "5402:5402"
12+
environment:
13+
POSTGRES_USER: postgres
14+
POSTGRES_PASSWORD: password
15+
POSTGRES_DB: postgres
16+
17+
pact-broker:
18+
image: "pactfoundation/pact-broker:2.110.0-pactbroker2.107.1"
19+
ports:
20+
- "9292:9292"
21+
depends_on:
22+
- postgres
23+
environment:
24+
PACT_BROKER_PORT: '9292'
25+
PACT_BROKER_DATABASE_URL: "postgres://postgres:password@postgres/postgres"
26+
PACT_BROKER_LOG_LEVEL: INFO
27+
PACT_BROKER_SQL_LOG_LEVEL: DEBUG
28+
PACT_BROKER_DATABASE_CONNECT_MAX_RETRIES: "5"
29+
PACT_BROKER_BASE_URL: 'https://localhost http://localhost http://localhost:9292 http://pact-broker:9292 https://host.docker.internal http://host.docker.internal http://host.docker.internal:9292'
30+
volumes:
31+
postgres-volume:
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package au.com.dius.pact.provider.junit5;
2+
3+
import au.com.dius.pact.core.model.Interaction;
4+
import au.com.dius.pact.core.model.Pact;
5+
import au.com.dius.pact.provider.junitsupport.Provider;
6+
import au.com.dius.pact.provider.junitsupport.State;
7+
import au.com.dius.pact.provider.junitsupport.loader.PactBroker;
8+
import com.github.tomakehurst.wiremock.WireMockServer;
9+
import org.apache.hc.core5.http.HttpRequest;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.TestTemplate;
12+
import org.junit.jupiter.api.extension.ExtendWith;
13+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
14+
15+
@Provider("ProviderTestPost")
16+
@PactBroker(host = "localhost", port = "9292", consumers = "ConsumerTestPost")
17+
public class ClassicJavaPactTest {
18+
private static final int WIREMOCK_PORT = 8080;
19+
private WireMockServer wireMockServer;
20+
21+
@BeforeEach
22+
public void setup() {
23+
wireMockServer = new WireMockServer(WIREMOCK_PORT);
24+
wireMockServer.stubFor(
25+
post(urlPathEqualTo("/users/prudhvi")).withRequestBody(equalToJson("{\n" +
26+
" \"firstName\": \"Prudhvi\",\n" +
27+
" \"lastName\": \"Raj\"\n" +
28+
"}"))
29+
.willReturn(aResponse()
30+
.withStatus(200)
31+
.withHeader("Content-Type", "application/json; charset=UTF-8")
32+
)
33+
);
34+
wireMockServer.start();
35+
}
36+
37+
@BeforeEach
38+
void setTarget(PactVerificationContext context) {
39+
HttpTestTarget target = new HttpTestTarget("localhost", WIREMOCK_PORT);
40+
context.setTarget(target);
41+
String buildVersion = "1.0.0";
42+
System.setProperty("pact.provider.version", buildVersion);
43+
System.setProperty("pact.verifier.publishResults", "true");
44+
}
45+
46+
@TestTemplate
47+
@ExtendWith(PactVerificationInvocationContextProvider.class)
48+
void testTemplate(Pact pact, Interaction interaction, HttpRequest request, PactVerificationContext context) {
49+
context.verifyInteraction();
50+
}
51+
52+
@State("User Prudhvi")
53+
public void testStatusofuserPrudhvi() {
54+
System.out.println("Pact verification started...");
55+
}
56+
57+
}

0 commit comments

Comments
 (0)