Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented pact with plain java and Junit5 #1744

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions consumer/junit5/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,54 @@ variable to 'true'.
By default, the Pact lifecycle will be invoked for every test method and will expect there to be a method annotated
with `@Pact` for each test method invoked. To add non-Pact tests, just annotate the non-Pact test method with the
`@PactIgnore` annotation.

# ClassicJavaPact - Setup

1. Install Windows Subsystem for Linux (WSL).
2. Install Docker in the WSL.
[version of docker: Docker version 24.0.5, build 24.0.5-0ubuntu1~22.04.1]
3. Install Docker-compose plugin in WSL.
1. Download Docker Compose
```bash
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
```
2. Make the Docker Compose binary executable
```bash
sudo chmod +x /usr/local/bin/docker-compose
```
3. Verify the installation
```bash
docker-compose --version
```
4. Run the Docker compose command to run Pact-broker.yml file
```bash
docker-compose up -d
```
5. Run the ClassicJavaPactTest in Consumer/Junit5 package
6. Run the docker-compose file in resources of Junit5 in wsl
7. Then publish the interaction file to localserver at 9292 port. By using plugin and below gradle task:
```bash
id "au.com.dius.pact" version "4.3.10"
```
```bash
pact {
publish {
pactDirectory = file('build/pacts/samples')
pactBrokerUrl = 'http://localhost:9292'
consumerVersion = '1.0.0'
}
}
```
8. For example we have used test.pactflow.io url, we can replace it with our required pactserver url.
```bash
pact {
publish {
pactDirectory = file('build/pacts/samples')
pactBrokerUrl = 'https://test.pactflow.io/'
pactBrokerUsername = 'dXfltyFMgNOFZAxr8io9wJ37iUpY42M'
pactBrokerPassword = 'O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1'
consumerVersion = '1.0.0'
}
}
```
9. Run the ClassicJavaPactTest in Provider/Junit5 package
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package au.com.dius.pact.consumer.junit5;

import au.com.dius.pact.consumer.MockServer;
import au.com.dius.pact.consumer.dsl.DslPart;
import au.com.dius.pact.consumer.dsl.LambdaDsl;
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.core.model.PactSpecVersion;
import au.com.dius.pact.core.model.RequestResponsePact;
import au.com.dius.pact.core.model.annotations.Pact;
import au.com.dius.pact.core.model.annotations.PactDirectory;
import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.ContentType;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import java.io.IOException;

@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = "ProviderTestPost", pactVersion = PactSpecVersion.V3)
@PactDirectory("build/pacts/samples")
public class ClassicJavaPactTest {

public static DslPart getDslPartiPart() throws IOException {
return LambdaDsl.newJsonBody((body) -> {
body.stringMatcher("firstName",".*", "Prudhvi");
body.stringMatcher("lastName",".*","Raj");
}).build();
}

@Nested
class Test1 {

@Pact(provider = "ProviderTestPost", consumer = "ConsumerTestPost")
RequestResponsePact createFragment1(PactDslWithProvider builder) throws IOException {
return builder
.given("User Prudhvi")
.uponReceiving("a post call for Prudhvi")
.path("/users/prudhvi")
.method("POST")
.body(getDslPartiPart())
.willRespondWith()
.status(200)
.toPact();
}

@Test
void runTestPostPrudhvi(MockServer mockServer) throws IOException {
ClassicHttpResponse postResponse = (ClassicHttpResponse) Request.post(mockServer.getUrl() + "/users/prudhvi")
.bodyString("{\n" +
" \"firstName\": \"Prudhvi\",\n" +
" \"lastName\": \"Raj\"\n" +
"}", ContentType.APPLICATION_JSON)
.execute().returnResponse();
Assertions.assertEquals(postResponse.getCode(), 200);
}
}
}
31 changes: 31 additions & 0 deletions consumer/junit5/src/test/resources/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
version: "3"

services:
postgres:
image: postgres
healthcheck:
test: psql postgres --command "select 1" -U postgres
volumes:
- postgres-volume:/var/lib/postgresql/data
ports:
- "5402:5402"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: postgres

pact-broker:
image: "pactfoundation/pact-broker:2.110.0-pactbroker2.107.1"
ports:
- "9292:9292"
depends_on:
- postgres
environment:
PACT_BROKER_PORT: '9292'
PACT_BROKER_DATABASE_URL: "postgres://postgres:password@postgres/postgres"
PACT_BROKER_LOG_LEVEL: INFO
PACT_BROKER_SQL_LOG_LEVEL: DEBUG
PACT_BROKER_DATABASE_CONNECT_MAX_RETRIES: "5"
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'
volumes:
postgres-volume:
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package au.com.dius.pact.provider.junit5;

import au.com.dius.pact.core.model.Interaction;
import au.com.dius.pact.core.model.Pact;
import au.com.dius.pact.provider.junitsupport.Provider;
import au.com.dius.pact.provider.junitsupport.State;
import au.com.dius.pact.provider.junitsupport.loader.PactBroker;
import au.com.dius.pact.provider.junitsupport.loader.PactBrokerAuth;
import com.github.tomakehurst.wiremock.WireMockServer;
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 static com.github.tomakehurst.wiremock.client.WireMock.*;

@Provider("ProviderTestPost")
@PactBroker(
host = "test.pactflow.io",
scheme = "https",
authentication = @PactBrokerAuth(username = "dXfltyFMgNOFZAxr8io9wJ37iUpY42M", password = "O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1"),
consumers = "ConsumerTestPost"
)
public class ClassicJavaPactTest {
private static final int WIREMOCK_PORT = 8080;
private WireMockServer wireMockServer;

@BeforeEach
public void setup() {
wireMockServer = new WireMockServer(WIREMOCK_PORT);
wireMockServer.stubFor(
post(urlPathEqualTo("/users/prudhvi")).withRequestBody(equalToJson("{\n" +
" \"firstName\": \"Prudhvi\",\n" +
" \"lastName\": \"Raj\"\n" +
"}"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json; charset=UTF-8")
)
);
wireMockServer.start();
}

@BeforeEach
void setTarget(PactVerificationContext context) {
HttpTestTarget target = new HttpTestTarget("localhost", WIREMOCK_PORT);
context.setTarget(target);
String buildVersion = "1.0.0";
System.setProperty("pact.provider.version", buildVersion);
System.setProperty("pact.verifier.publishResults", "true");
}

@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void testTemplate(Pact pact, Interaction interaction, HttpRequest request, PactVerificationContext context) {
context.verifyInteraction();
}

@State("User Prudhvi")
public void testStatusofuserPrudhvi() {
System.out.println("Pact verification started...");
}

}
Loading