Skip to content

Commit

Permalink
📚 example: add testcontainers-java example #8
Browse files Browse the repository at this point in the history
  • Loading branch information
nilwurtz committed Nov 21, 2023
1 parent 0030b87 commit 1a123f7
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 2 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ jobs:
java-version: '17'
distribution: 'corretto'

- name: Check example validity
- name: Check example validity kotlin
run: mvn test
working-directory: examples/testcontainers-kotlin
working-directory: examples/testcontainers-kotlin

- name: Check example validity java
run: mvn test
working-directory: examples/testcontainers-java
38 changes: 38 additions & 0 deletions examples/testcontainers-java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
13 changes: 13 additions & 0 deletions examples/testcontainers-java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Wiremock Graphql Extension Example - testcontainers-java

This example shows how to use the Wiremock Graphql Extension with java and Testcontainers.

## Requirements
- Java 17
- Maven 3
- Docker

## Run
```bash
mvn test
```
120 changes: 120 additions & 0 deletions examples/testcontainers-java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<name>testcontainers-java-example</name>
<groupId>org.example</groupId>
<artifactId>testcontainers-java</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<wiremock.testcontainers.version>1.0-alpha-13</wiremock.testcontainers.version>
<testcontainers.version>1.19.0</testcontainers.version>
<extension.version>0.7.0</extension.version>
<wiremock.version>3.3.1</wiremock.version>
<junit.version>5.10.1</junit.version>
</properties>

<repositories>
<repository>
<id>mavenCentral</id>
<url>https://repo1.maven.org/maven2/</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>


<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock</artifactId>
<version>${wiremock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.wiremock</groupId>
<artifactId>wiremock-testcontainers-java</artifactId>
<version>${wiremock.testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.github.nilwurtz</groupId>
<artifactId>wiremock-graphql-extension</artifactId>
<version>${extension.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<testSourceDirectory>src/test/java</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<id>copy</id>
<phase>test-compile</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>io.github.nilwurtz</groupId>
<artifactId>wiremock-graphql-extension</artifactId>
<version>${extension.version}</version>
<classifier>jar-with-dependencies</classifier>
<destFileName>wiremock-graphql-extension-jar-with-dependencies.jar</destFileName>
<overWrite>true</overWrite>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/test-wiremock-extension</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.1.2</version>
</plugin>
</plugins>
</build>
</project>
69 changes: 69 additions & 0 deletions examples/testcontainers-java/src/test/java/TestSample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import com.github.tomakehurst.wiremock.client.WireMock;
import io.github.nilwurtz.GraphqlBodyMatcher;
import org.junit.jupiter.api.DisplayName;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
import org.wiremock.integrations.testcontainers.WireMockContainer;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.http.HttpClient;
import java.net.http.HttpResponse;
import java.nio.file.Paths;
import java.util.Collections;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@Testcontainers
public class TestSample {
@Container
private static final WireMockContainer wiremockContainer = new WireMockContainer(
DockerImageName.parse(WireMockContainer.OFFICIAL_IMAGE_NAME)
.withTag("3.3.1")
).withExtensions(
GraphqlBodyMatcher.extensionName,
Collections.singleton("io.github.nilwurtz.GraphqlBodyMatcher"),
Collections.singleton(
Paths.get(
"target",
"test-wiremock-extension",
"wiremock-graphql-extension-jar-with-dependencies.jar"
).toFile()
)
);

@Test
public void testRunning() {
assertTrue(wiremockContainer.isRunning());
}

@Test
@DisplayName("Matches if GraphQL query is semantically equal to the request")
public void testGraphql() throws IOException, InterruptedException {
new WireMock(wiremockContainer.getPort()).register(
WireMock.post(WireMock.urlEqualTo("/graphql"))
.andMatching(
GraphqlBodyMatcher.extensionName,
GraphqlBodyMatcher.Companion.withRequest(
"{ \"query\": \"{ query { name id }}\" }"
)
)
.willReturn(
WireMock.aResponse()
.withBody("{\"data\": {\"id\": 1, \"name\": \"test\"}}")
));


var client = HttpClient.newHttpClient();
var request = java.net.http.HttpRequest.newBuilder()
.uri(java.net.URI.create(wiremockContainer.getBaseUrl() + "/graphql"))
.POST(java.net.http.HttpRequest.BodyPublishers.ofString("{ \"query\": \"{ query { id name }}\" }"))
.build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());

assertEquals(200, response.statusCode());
assertEquals("{\"data\": {\"id\": 1, \"name\": \"test\"}}", response.body());
}
}

0 comments on commit 1a123f7

Please sign in to comment.