From 793573e175b9019a33ed4ff04e3fc89cb740d764 Mon Sep 17 00:00:00 2001 From: David Groomes Date: Mon, 10 May 2021 23:19:38 -0500 Subject: [PATCH] Custom transformer --- README.md | 9 ++-- .../dgroomes/wiremock/ProgrammaticMain.java | 5 ++- .../wiremock/RandomIntegerTransformer.java | 45 +++++++++++++++++++ .../java/dgroomes/wiremock/WireMockUtil.java | 4 ++ .../mappings/__files/random-integer.json | 11 +++++ 5 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 programmatic/src/main/java/dgroomes/wiremock/RandomIntegerTransformer.java create mode 100644 programmatic/wiremock/scenarios/happy-path/mappings/__files/random-integer.json diff --git a/README.md b/README.md index 296cf4c..5566d26 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,11 @@ various configurations. Use it like a recipe book for when you write your own Wi Instructions: * Use Java 11 -* Run with `./gradlew programmatic:run` -* Make a request to a stubbed endpoint with `curl --request GET --url http://localhost:8070/message` +* Run: + * `./gradlew programmatic:run` +* Make requests to the stubbed endpoints: + * `curl --request GET --url http://localhost:8070/message` + * `curl --request GET --url http://localhost:8070/random-integer` * Observe the Jetty server statistics by going to in your browser * Statistics include things like the number of responses with 200/300/400/500 status codes, the number of connections, and the amount of memory used by the underlying Jetty server. @@ -44,7 +47,7 @@ Instructions: General clean-ups, changes and things I wish to implement for this project: * Make the sub-projects completely standalone and adhere to the convention I have in my other playground repos. -* Add a stub with a custom handler that uses custom Java code to respond to the request. +* DONE Add a stub with a custom handler that uses custom Java code to respond to the request. * Create a WireMock-in-Docker example * DONE create a sub-project that declares dependency constraint versions and which will be used as a `platform(...)` from the other projects. For details of this feature, see the [Gradle docs about "platform"](https://docs.gradle.org/current/userguide/platforms.html) diff --git a/programmatic/src/main/java/dgroomes/wiremock/ProgrammaticMain.java b/programmatic/src/main/java/dgroomes/wiremock/ProgrammaticMain.java index 4ecebdc..18a8fdf 100644 --- a/programmatic/src/main/java/dgroomes/wiremock/ProgrammaticMain.java +++ b/programmatic/src/main/java/dgroomes/wiremock/ProgrammaticMain.java @@ -16,8 +16,8 @@ public class ProgrammaticMain { private static final Logger log = LoggerFactory.getLogger(ProgrammaticMain.class); private static final int SLEEP_SECONDS = 30; private static final int PORT = 8070; -// private static final String ROOT_DIR = "wiremock/scenarios/happy-path"; - private static final String ROOT_DIR = "wiremock/scenarios/occasional-failure"; + private static final String ROOT_DIR = "wiremock/scenarios/happy-path"; +// private static final String ROOT_DIR = "wiremock/scenarios/occasional-failure"; public static void main(String[] args) throws InterruptedException { var start = Instant.now(); @@ -25,6 +25,7 @@ public static void main(String[] args) throws InterruptedException { .port(PORT); WireMockUtil.configureStatistics(options); WireMockUtil.configureRootDir(options, ROOT_DIR); + WireMockUtil.configureExtensions(options); WireMockServer wireMockServer = new WireMockServer(options); log.debug("Starting the WireMock server"); wireMockServer.start(); diff --git a/programmatic/src/main/java/dgroomes/wiremock/RandomIntegerTransformer.java b/programmatic/src/main/java/dgroomes/wiremock/RandomIntegerTransformer.java new file mode 100644 index 0000000..ae550ff --- /dev/null +++ b/programmatic/src/main/java/dgroomes/wiremock/RandomIntegerTransformer.java @@ -0,0 +1,45 @@ +package dgroomes.wiremock; + +import com.github.tomakehurst.wiremock.common.FileSource; +import com.github.tomakehurst.wiremock.extension.Parameters; +import com.github.tomakehurst.wiremock.extension.ResponseTransformer; +import com.github.tomakehurst.wiremock.http.Request; +import com.github.tomakehurst.wiremock.http.Response; + +import java.util.Random; + +/** + * Return a random integer. Optionally, when a "limit" argument is supplied, use that as the exclusive maximum value. + */ +public class RandomIntegerTransformer extends ResponseTransformer { + + private Random random = new Random(); + + @Override + public String getName() { + return "random-integer-transformer"; + } + + @Override + public boolean applyGlobally() { + return false; + } + + private String template = "{\n" + + " \"random_integer\":" + + " %d\n" + + "}"; + + @Override + public Response transform(Request request, Response response, FileSource files, Parameters parameters) { + if (parameters == null) { + parameters = new Parameters(); + } + + var limit = ((int) parameters.getOrDefault("limit", 100)); + var randomInt = random.nextInt(limit); + return Response.response() + .body(String.format(template, randomInt)) + .build(); + } +} diff --git a/programmatic/src/main/java/dgroomes/wiremock/WireMockUtil.java b/programmatic/src/main/java/dgroomes/wiremock/WireMockUtil.java index 8b85f21..3e155f4 100644 --- a/programmatic/src/main/java/dgroomes/wiremock/WireMockUtil.java +++ b/programmatic/src/main/java/dgroomes/wiremock/WireMockUtil.java @@ -133,4 +133,8 @@ public static void configureRootDir(WireMockConfiguration options, String rootDi options.withRootDirectory(rootDir); } + + public static void configureExtensions(WireMockConfiguration options) { + options.extensions(new RandomIntegerTransformer()); + } } diff --git a/programmatic/wiremock/scenarios/happy-path/mappings/__files/random-integer.json b/programmatic/wiremock/scenarios/happy-path/mappings/__files/random-integer.json new file mode 100644 index 0000000..639fb2f --- /dev/null +++ b/programmatic/wiremock/scenarios/happy-path/mappings/__files/random-integer.json @@ -0,0 +1,11 @@ +{ + "request": { + "urlPathPattern": "/random-integer(/?)", + "method": "GET" + }, + "response": { + "transformers": [ + "random-integer-transformer" + ] + } +}