Skip to content

Commit

Permalink
Update random demo to the Faker Extension (#104)
Browse files Browse the repository at this point in the history
* Update random demo to the Faker Extension

* Fix Pretty rendering

* Cleanup integration test

* Update test/integration-tests/src/test/java/org/wiremock/docker/it/samples/RandomSampleTest.java
  • Loading branch information
oleg-nenashev authored Jan 5, 2024
1 parent 81ac084 commit 4862fe5
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 34 deletions.
7 changes: 4 additions & 3 deletions samples/random/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
FROM wiremock/wiremock
FROM wiremock/wiremock:3.3.1-1

COPY stubs /home/wiremock

ADD https://repo1.maven.org/maven2/com/opentable/wiremock-body-transformer/1.1.3/wiremock-body-transformer-1.1.3.jar /var/wiremock/extensions/
ADD https://repo1.maven.org/maven2/org/wiremock/extensions/wiremock-faker-extension-standalone/0.1.1/wiremock-faker-extension-standalone-0.1.1.jar /var/wiremock/extensions/
# TODO: Checksum Validation

CMD ["--extensions", "com.opentable.extension.BodyTransformer"]
CMD ["--global-response-templating", "--extensions", "org.wiremock.RandomExtension"]
18 changes: 14 additions & 4 deletions samples/random/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# Sample - Random Response Field

> **WARNING:** This sample uses the [WireMock Body Transformer](https://github.com/opentable/wiremock-body-transformer) extension.
> This extension is currently archived and not supported.
> No stability guaranteed provided.
> Use the sample with caution
We use WireMock Faker extension in this demo:

To run:

```shell
docker build -t wiremock/random-data-demo .
docker run --rm - p 8080:8080 wiremock/random-data-demo
```

Then you can use the following endpoints:

- '/user' - random user ID
- '/user?pretty=true' - random user id with fancy layout
- '/users.csv' - generate a CSV file with a random number of users
1 change: 0 additions & 1 deletion samples/random/stubs/__files/random.json

This file was deleted.

7 changes: 7 additions & 0 deletions samples/random/stubs/__files/user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "{{ random 'Name.first_name' }}",
"surname": "{{ random 'Name.last_name' }}",
"country": "{{ random 'Address.country' }}",
"city": "{{ random 'Address.city' }}",
"favorite_tool": "WireMock"
}
2 changes: 2 additions & 0 deletions samples/random/stubs/__files/users.csv.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ID, First Name, Last Name, Country, City, Favorite Tool,
{{#each (range 0 (randomInt lower=1 upper=20)) as |index|}}{{index}}, {{ random 'Name.first_name' }}, {{ random 'Name.last_name' }}, {{ random 'Address.country' }}, {{ random 'Address.city' }}, WireMock,{{/each}}
11 changes: 0 additions & 11 deletions samples/random/stubs/mappings/random.json

This file was deleted.

46 changes: 46 additions & 0 deletions samples/random/stubs/mappings/user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"mappings": [
{
"request": {
"method": "GET",
"urlPath": "/user",
"queryParameters" : {
"pretty" : {
"absent" : true
}
}
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"jsonBody": {
"name": "{{ random 'Name.first_name' }}",
"surname": "{{ random 'Name.last_name' }}",
"country": "{{ random 'Address.country' }}",
"city": "{{ random 'Address.city' }}",
"favorite_tool": "WireMock"
}
}
},
{
"request": {
"method": "GET",
"urlPath": "/user",
"queryParameters" : {
"pretty" : {
"equalTo" : "true"
}
}
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"bodyFileName": "user.json"
}
}
]
}
13 changes: 13 additions & 0 deletions samples/random/stubs/mappings/users.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"request": {
"method": "GET",
"url": "/users"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "text/csv"
},
"bodyFileName": "users.csv.template"
}
}
7 changes: 1 addition & 6 deletions test/integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,10 @@
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.opentable</groupId>
<artifactId>wiremock-body-transformer</artifactId>
<version>1.1.3</version>
</artifactItem>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-webhooks-extension</artifactId>
<version>2.35.1</version>
</dependency>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/test-wiremock-extension</outputDirectory>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,11 @@ public Path getHomeDir() {
return TestConfig.getSamplesPath().resolve("random");
}

@Override
public WireMockContainer createWireMockContainer() {
return super.createWireMockContainer().
withExtensions(Collections.singleton("com.opentable.extension.BodyTransformer"),
new File("target/test-wiremock-extension/wiremock-body-transformer-1.1.3.jar"));
}

@Test
public void testRandom() throws Exception {
final HttpClient client = HttpClient.newBuilder().build();
final HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(wiremockServer.getUrl("random")))
.uri(new URI(wiremockServer.getUrl("user")))
.timeout(Duration.ofSeconds(10))
.header("Content-Type", "application/json")
.GET().build();
Expand All @@ -44,6 +37,6 @@ public void testRandom() throws Exception {

assertThat(response.body())
.as("Wrong response body")
.contains("randomInteger");
.contains("surname");
}
}

0 comments on commit 4862fe5

Please sign in to comment.