Skip to content

Commit

Permalink
bug!: fix incompatibility with wiremock 3.6 (#130)
Browse files Browse the repository at this point in the history
- dump gradle plugin version
- fix API change

<!-- Please describe your pull request here. -->

## References

#129 

<!-- References to relevant GitHub issues and pull requests, esp.
upstream and downstream changes -->

## Submitter checklist

- [ ] Recommended: Join [WireMock Slack](https://slack.wiremock.org/) to
get any help in `#help-contributing` or a project-specific channel like
`#wiremock-java`
- [ ] The PR request is well described and justified, including the body
and the references
- [ ] The PR title represents the desired changelog entry
- [ ] The repository's code style is followed (see the contributing
guide)
- [ ] Test coverage that demonstrates that the change works as expected
- [ ] For new features, there's necessary documentation in this pull
request or in a subsequent PR to
[wiremock.org](https://github.com/wiremock/wiremock.org)

<!--
Put an `x` into the [ ] to show you have filled the information.
The template comes from
https://github.com/wiremock/.github/blob/main/.github/pull_request_template.md
You can override it by creating .github/pull_request_template.md in your
own repository
-->
  • Loading branch information
dirkbolte authored Jun 4, 2024
1 parent 77f157b commit 831aed4
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 37 deletions.
24 changes: 24 additions & 0 deletions .run/Test.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Test" type="GradleRunConfiguration" factoryName="Gradle">
<ExternalSystemSettings>
<option name="executionName" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="externalSystemIdString" value="GRADLE" />
<option name="scriptParameters" value="" />
<option name="taskDescriptions">
<list />
</option>
<option name="taskNames">
<list>
<option value=":test" />
</list>
</option>
<option name="vmOptions" />
</ExternalSystemSettings>
<ExternalSystemDebugServerProcess>false</ExternalSystemDebugServerProcess>
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
<DebugAllEnabled>false</DebugAllEnabled>
<RunAsTest>true</RunAsTest>
<method v="2" />
</configuration>
</component>
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -33,6 +38,7 @@ shadowJar {
test {
finalizedBy jacocoTestReport
}

jacocoTestReport {
dependsOn test
reports {
Expand Down
4 changes: 2 additions & 2 deletions demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

26 changes: 10 additions & 16 deletions src/main/java/org/wiremock/extensions/state/StateExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand All @@ -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<String, Object> store;

public StateExtension(Store<String, Object> 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<Extension> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Map.Entry<ContextMatcher, Object>> getMatchers(Parameters parameters) {
Expand Down Expand Up @@ -139,9 +139,10 @@ private MatchResult hasNotContext(Map<String, Object> 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<Object> castedCollection = cast(value, Collection.class);
Expand All @@ -162,7 +163,7 @@ Object renderTemplateRecursively(Object context, Object value) {
private enum ContextMatcher {

property((Context c, Object object) -> {
Map<String, Map<String, Object>> mapValue = cast(object, Map.class);
@SuppressWarnings("unchecked") Map<String, Map<String, Object>> 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());
Expand All @@ -177,7 +178,7 @@ private enum ContextMatcher {
}),

list((Context c, Object object) -> {
Map<String, Map<String, Map<String, Object>>> mapValue = cast(object, Map.class);
@SuppressWarnings("unchecked") Map<String, Map<String, Map<String, Object>>> mapValue = cast(object, Map.class);
var allResults = mapValue.entrySet().stream().map(listIndexEntry -> {
Map<String, String> listEntry;
switch (listIndexEntry.getKey()) {
Expand Down

0 comments on commit 831aed4

Please sign in to comment.