From 7955b4fca9fa8de9d7e241f50086205f39e6a4bf Mon Sep 17 00:00:00 2001 From: Dirk Bolte Date: Sat, 25 May 2024 11:49:38 +0200 Subject: [PATCH] bug!: fix incompatibility with wiremock 3.6 - use WireMockServices instead of direct TemplateEngine instantiation - document version dependency --- .run/Test.run.xml | 24 +++++++++++++++++ README.md | 6 +++-- build.gradle | 6 +++++ demo/README.md | 4 +-- settings.gradle | 1 + .../extensions/state/StateExtension.java | 26 +++++++------------ .../extensions/DeleteStateEventListener.java | 10 +++---- .../extensions/RecordStateEventListener.java | 10 +++---- .../state/extensions/StateRequestMatcher.java | 15 ++++++----- 9 files changed, 65 insertions(+), 37 deletions(-) create mode 100644 .run/Test.run.xml create mode 100644 settings.gradle diff --git a/.run/Test.run.xml b/.run/Test.run.xml new file mode 100644 index 0000000..efd2604 --- /dev/null +++ b/.run/Test.run.xml @@ -0,0 +1,24 @@ + + + + + + + false + true + false + true + + + \ No newline at end of file diff --git a/README.md b/README.md index ffc1926..3bf7eb7 100644 --- a/README.md +++ b/README.md @@ -170,9 +170,11 @@ the `GET` won't have any knowledge of the previous post. | `wiremock-state-extension` version | `WireMock` version | |------------------------------------|--------------------| -| `0.0.3`+ | `3.0.0-beta-11`+ | -| `0.0.6`+ | `3.0.0-beta-14`+ | +| `0.7.0`+ | `3.6.0`+ | +| `0.5.1`+ | `3.3.1`+ | | `0.1.0`+ | `3.0.0`+ | +| `0.0.6`+ | `3.0.0-beta-14`+ | +| `0.0.3`+ | `3.0.0-beta-11`+ | ## Installation diff --git a/build.gradle b/build.gradle index 9d54b25..37e35a0 100644 --- a/build.gradle +++ b/build.gradle @@ -25,6 +25,11 @@ dependencies { } } +java { + sourceCompatibility = 11 + targetCompatibility = 11 +} + shadowJar { relocate "com.github.ben-manes.caffeine", 'wiremock.com.github.ben-manes.caffeine' relocate "com.github.jknack", 'wiremock.com.github.jknack' @@ -33,6 +38,7 @@ shadowJar { test { finalizedBy jacocoTestReport } + jacocoTestReport { dependsOn test reports { diff --git a/demo/README.md b/demo/README.md index dda7683..e7336b1 100644 --- a/demo/README.md +++ b/demo/README.md @@ -102,7 +102,7 @@ From directory of this README: ```shell cd .. -curl -o build/libs/wiremock-standalone-3.4.1.jar https://repo1.maven.org/maven2/org/wiremock/wiremock-standalone/3.4.1/wiremock-standalone-3.4.1.jar +curl -o build/libs/wiremock-standalone-3.6.0.jar https://repo1.maven.org/maven2/org/wiremock/wiremock-standalone/3.6.0/wiremock-standalone-3.6.0.jar ``` ### Step 4: Start WireMock with the State Extension @@ -111,7 +111,7 @@ From directory of this README: ```shell cd .. -java -cp build/libs/wiremock-state-extension-standalone-0.6.0-SNAPSHOT.jar:build/libs/wiremock-standalone-3.4.1.jar wiremock.Run --verbose --global-response-templating --root-dir demo/stubs +java -cp build/libs/wiremock-state-extension-standalone-0.6.0-SNAPSHOT.jar:build/libs/wiremock-standalone-3.6.0.jar wiremock.Run --verbose --global-response-templating --root-dir demo/stubs ``` This command starts WireMock with the State Extension enabled. diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ + diff --git a/src/main/java/org/wiremock/extensions/state/StateExtension.java b/src/main/java/org/wiremock/extensions/state/StateExtension.java index ce756df..61ee652 100644 --- a/src/main/java/org/wiremock/extensions/state/StateExtension.java +++ b/src/main/java/org/wiremock/extensions/state/StateExtension.java @@ -18,7 +18,6 @@ import com.github.tomakehurst.wiremock.extension.Extension; import com.github.tomakehurst.wiremock.extension.ExtensionFactory; import com.github.tomakehurst.wiremock.extension.WireMockServices; -import com.github.tomakehurst.wiremock.extension.responsetemplating.TemplateEngine; import com.github.tomakehurst.wiremock.store.Store; import org.wiremock.extensions.state.extensions.DeleteStateEventListener; import org.wiremock.extensions.state.extensions.RecordStateEventListener; @@ -28,7 +27,6 @@ import org.wiremock.extensions.state.internal.ContextManager; import org.wiremock.extensions.state.internal.TransactionManager; -import java.util.Collections; import java.util.List; /** @@ -51,26 +49,22 @@ */ public class StateExtension implements ExtensionFactory { - private final StateTemplateHelperProviderExtension stateTemplateHelperProviderExtension; - private final RecordStateEventListener recordStateEventListener; - private final DeleteStateEventListener deleteStateEventListener; - private final TransactionEventListener transactionEventListener; - private final StateRequestMatcher stateRequestMatcher; + private final Store store; public StateExtension(Store store) { - var transactionManager = new TransactionManager(store); - var contextManager = new ContextManager(store, transactionManager); - this.stateTemplateHelperProviderExtension = new StateTemplateHelperProviderExtension(contextManager); - var templateEngine = new TemplateEngine(stateTemplateHelperProviderExtension.provideTemplateHelpers(), null, Collections.emptySet(), false); - - this.recordStateEventListener = new RecordStateEventListener(contextManager, templateEngine); - this.deleteStateEventListener = new DeleteStateEventListener(contextManager, templateEngine); - this.transactionEventListener = new TransactionEventListener(transactionManager); - this.stateRequestMatcher = new StateRequestMatcher(contextManager, templateEngine); + this.store = store; } @Override public List create(WireMockServices services) { + var transactionManager = new TransactionManager(store); + var contextManager = new ContextManager(store, transactionManager); + var stateTemplateHelperProviderExtension = new StateTemplateHelperProviderExtension(contextManager); + var recordStateEventListener = new RecordStateEventListener(contextManager, services); + var deleteStateEventListener = new DeleteStateEventListener(contextManager, services); + var transactionEventListener = new TransactionEventListener(transactionManager); + var stateRequestMatcher = new StateRequestMatcher(contextManager, services); + return List.of( recordStateEventListener, deleteStateEventListener, diff --git a/src/main/java/org/wiremock/extensions/state/extensions/DeleteStateEventListener.java b/src/main/java/org/wiremock/extensions/state/extensions/DeleteStateEventListener.java index dfcc1ec..f5d2bd9 100644 --- a/src/main/java/org/wiremock/extensions/state/extensions/DeleteStateEventListener.java +++ b/src/main/java/org/wiremock/extensions/state/extensions/DeleteStateEventListener.java @@ -19,8 +19,8 @@ import com.github.tomakehurst.wiremock.core.ConfigurationException; import com.github.tomakehurst.wiremock.extension.Parameters; import com.github.tomakehurst.wiremock.extension.ServeEventListener; +import com.github.tomakehurst.wiremock.extension.WireMockServices; import com.github.tomakehurst.wiremock.extension.responsetemplating.RequestTemplateModel; -import com.github.tomakehurst.wiremock.extension.responsetemplating.TemplateEngine; import com.github.tomakehurst.wiremock.stubbing.ServeEvent; import org.apache.commons.lang3.StringUtils; import org.wiremock.extensions.state.internal.ContextManager; @@ -47,13 +47,13 @@ */ public class DeleteStateEventListener implements ServeEventListener, StateExtensionMixin { - private final TemplateEngine templateEngine; + private final WireMockServices wireMockServices; private final ContextManager contextManager; - public DeleteStateEventListener(ContextManager contextManager, TemplateEngine templateEngine) { + public DeleteStateEventListener(ContextManager contextManager, WireMockServices services) { this.contextManager = contextManager; - this.templateEngine = templateEngine; + this.wireMockServices = services; } @Override @@ -76,7 +76,7 @@ public void beforeResponseSent(ServeEvent serveEvent, Parameters parameters) { } private String renderTemplate(Object context, String value) { - return templateEngine.getUncachedTemplate(value).apply(context); + return wireMockServices.getTemplateEngine().getUncachedTemplate(value).apply(context); } private class ListenerInstance { diff --git a/src/main/java/org/wiremock/extensions/state/extensions/RecordStateEventListener.java b/src/main/java/org/wiremock/extensions/state/extensions/RecordStateEventListener.java index e373613..96aa1f1 100644 --- a/src/main/java/org/wiremock/extensions/state/extensions/RecordStateEventListener.java +++ b/src/main/java/org/wiremock/extensions/state/extensions/RecordStateEventListener.java @@ -19,8 +19,8 @@ import com.github.tomakehurst.wiremock.core.ConfigurationException; import com.github.tomakehurst.wiremock.extension.Parameters; import com.github.tomakehurst.wiremock.extension.ServeEventListener; +import com.github.tomakehurst.wiremock.extension.WireMockServices; import com.github.tomakehurst.wiremock.extension.responsetemplating.RequestTemplateModel; -import com.github.tomakehurst.wiremock.extension.responsetemplating.TemplateEngine; import com.github.tomakehurst.wiremock.stubbing.ServeEvent; import org.apache.commons.lang3.StringUtils; import org.wiremock.extensions.state.internal.ContextManager; @@ -43,12 +43,12 @@ */ public class RecordStateEventListener implements ServeEventListener, StateExtensionMixin { - private final TemplateEngine templateEngine; + private final WireMockServices wireMockServices; private final ContextManager contextManager; - public RecordStateEventListener(ContextManager contextManager, TemplateEngine templateEngine) { + public RecordStateEventListener(ContextManager contextManager, WireMockServices services) { this.contextManager = contextManager; - this.templateEngine = templateEngine; + this.wireMockServices = services; } public void beforeResponseSent(ServeEvent serveEvent, Parameters parameters) { @@ -72,7 +72,7 @@ public boolean applyGlobally() { private String renderTemplate(Object context, String value) { - return templateEngine.getUncachedTemplate(value).apply(context); + return wireMockServices.getTemplateEngine().getUncachedTemplate(value).apply(context); } private class ListenerInstance { diff --git a/src/main/java/org/wiremock/extensions/state/extensions/StateRequestMatcher.java b/src/main/java/org/wiremock/extensions/state/extensions/StateRequestMatcher.java index ba8648b..ec5b90c 100644 --- a/src/main/java/org/wiremock/extensions/state/extensions/StateRequestMatcher.java +++ b/src/main/java/org/wiremock/extensions/state/extensions/StateRequestMatcher.java @@ -18,8 +18,8 @@ import com.github.tomakehurst.wiremock.common.Json; import com.github.tomakehurst.wiremock.core.ConfigurationException; import com.github.tomakehurst.wiremock.extension.Parameters; +import com.github.tomakehurst.wiremock.extension.WireMockServices; import com.github.tomakehurst.wiremock.extension.responsetemplating.RequestTemplateModel; -import com.github.tomakehurst.wiremock.extension.responsetemplating.TemplateEngine; import com.github.tomakehurst.wiremock.http.Request; import com.github.tomakehurst.wiremock.matching.MatchResult; import com.github.tomakehurst.wiremock.matching.RequestMatcherExtension; @@ -50,12 +50,12 @@ */ public class StateRequestMatcher extends RequestMatcherExtension implements StateExtensionMixin { - private final TemplateEngine templateEngine; + private final WireMockServices wireMockServices; private final ContextManager contextManager; - public StateRequestMatcher(ContextManager contextManager, TemplateEngine templateEngine) { + public StateRequestMatcher(ContextManager contextManager, WireMockServices services) { this.contextManager = contextManager; - this.templateEngine = templateEngine; + this.wireMockServices = services; } private static List> getMatchers(Parameters parameters) { @@ -139,9 +139,10 @@ private MatchResult hasNotContext(Map model, String template) { } String renderTemplate(Object context, String value) { - return templateEngine.getUncachedTemplate(value).apply(context); + return wireMockServices.getTemplateEngine().getUncachedTemplate(value).apply(context); } + @SuppressWarnings("unchecked") Object renderTemplateRecursively(Object context, Object value) { if (value instanceof Collection) { Collection castedCollection = cast(value, Collection.class); @@ -162,7 +163,7 @@ Object renderTemplateRecursively(Object context, Object value) { private enum ContextMatcher { property((Context c, Object object) -> { - Map> mapValue = cast(object, Map.class); + @SuppressWarnings("unchecked") Map> mapValue = cast(object, Map.class); var results = mapValue.entrySet().stream().map(entry -> { var patterns = mapToObject(entry.getValue(), StringValuePattern.class); var propertyValue = c.getProperties().get(entry.getKey()); @@ -177,7 +178,7 @@ private enum ContextMatcher { }), list((Context c, Object object) -> { - Map>> mapValue = cast(object, Map.class); + @SuppressWarnings("unchecked") Map>> mapValue = cast(object, Map.class); var allResults = mapValue.entrySet().stream().map(listIndexEntry -> { Map listEntry; switch (listIndexEntry.getKey()) {