Skip to content

Commit 3c450f7

Browse files
authored
Adding support for usage with standalone WireMock (#10)
- providing ServiceLoader for extension factory (using CaffeineStore) - adding publication for standalone jar with shadowJar - extending documentation <!-- Please describe your pull request here. --> ## References - TODO <!-- References to relevant GitHub issues and pull requests, esp. upstream and downstream changes --> ## Submitter checklist - [ ] 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 -->
1 parent aa11a1e commit 3c450f7

File tree

5 files changed

+159
-9
lines changed

5 files changed

+159
-9
lines changed

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ the `GET` won't have any knowledge of the previous post.
5454

5555
| `wiremock-extension-state` version | `WireMock` version |
5656
|------------------------------------|--------------------|
57-
| `0.0.3` | `3.0.0-beta-11`+ |
57+
| `0.0.3`+ | `3.0.0-beta-11`+ |
5858

5959
## Installation
6060

@@ -100,8 +100,11 @@ add authentication to GitHub packages.
100100

101101
## Register extension
102102

103+
### Java
104+
103105
This extension makes use of Wiremock's `ExtensionFactory`, so only one extension has to be registered: `StateExtension`.
104-
In order to use them, templating has to be enabled as well:
106+
In order to use them, templating has to be enabled as well. A store for all state data has to be provided. This extension
107+
provides a `CaffeineStore` which can be used - or you can provide your own store:
105108

106109
```java
107110
public class MySandbox {
@@ -122,6 +125,17 @@ public class MySandbox {
122125
}
123126
```
124127

128+
### Standalone
129+
130+
This extension uses the `ServiceLoader` extension to be loaded by WireMock. As Standalone version, it will use `CaffeineStore` for
131+
storing any data.
132+
133+
The standalone jar can be downloaded from [GitHub](https://github.com/wiremock/wiremock-extension-state/packages/1902576) .
134+
135+
```bash
136+
java -cp "wiremock-state-extension-standalone-0.0.4.jar:wiremock-standalone-3.0.0-beta-11.jar" wiremock.Run
137+
```
138+
125139
## Record a state
126140

127141
The state is recorded in `withServeEventListener` of a stub. The following parameters have to be provided:
@@ -350,8 +364,6 @@ As for other matchers, templating is supported.
350364
}
351365
```
352366

353-
354-
355367
## Retrieve a state
356368

357369
A state can be retrieved using a handlebar helper. In the example above, the `StateHelper` is registered by the name `state`.

build.gradle

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ plugins {
1313
id 'jacoco'
1414
id 'com.diffplug.spotless' version '6.20.0'
1515
id 'com.palantir.git-version' version '3.0.0'
16+
id 'com.github.johnrengelman.shadow' version '8.1.1'
1617
}
1718

1819
wrapper {
19-
gradleVersion = '7.2'
20+
gradleVersion = '8.2.1'
2021
distributionType = Wrapper.DistributionType.BIN
2122
}
22-
project.archivesBaseName = 'wiremock-state-extension'
23+
2324
project.ext {
25+
baseArtifact = 'wiremock-state-extension'
2426
versions = [
2527
wiremock : '3.0.0-beta-12',
2628
caffeine : '3.1.6',
@@ -32,6 +34,42 @@ project.ext {
3234
]
3335
}
3436

37+
project.archivesBaseName = "${baseArtifact}"
38+
configurations {
39+
standaloneOnly
40+
}
41+
42+
jar {
43+
archiveBaseName.set("${baseArtifact}")
44+
exclude 'META-INF/services'
45+
}
46+
47+
shadowJar {
48+
archiveBaseName.set("${baseArtifact}-standalone")
49+
archiveClassifier.set('')
50+
configurations = [
51+
project.configurations.runtimeClasspath,
52+
project.configurations.standaloneOnly
53+
]
54+
55+
with copySpec {
56+
from("shadowjar/resources") {}
57+
}
58+
59+
relocate "com.github.ben-manes.caffeine", 'wiremock.org.extensions.state.caffeine'
60+
relocate "com.github.jknack", 'wiremock.org.extensions.state.jknack'
61+
62+
63+
dependencies {
64+
exclude(dependency('junit:junit'))
65+
}
66+
67+
mergeServiceFiles()
68+
69+
exclude 'META-INF/maven/**'
70+
exclude 'module-info.class'
71+
exclude 'handlebars-*.js'
72+
}
3573

3674
group 'org.wiremock'
3775

@@ -48,18 +86,60 @@ publishing {
4886
}
4987
}
5088
}
89+
90+
getComponents().withType(AdhocComponentWithVariants).each { c ->
91+
c.withVariantsFromConfiguration(project.configurations.shadowRuntimeElements) {
92+
skip()
93+
}
94+
}
95+
5196
publications {
5297
mavenJava(MavenPublication) {
53-
artifactId = 'wiremock-state-extension'
98+
artifactId = "${baseArtifact}"
5499

55100
from components.java
56101

57102
pom {
58-
name = 'wiremock-state-extension'
103+
name = "${baseArtifact}"
59104
description = 'A WireMock extension to transfer state in between stubs'
60105
url = 'https://github.com/dirkbolte/wiremock-extension-state'
61106

62107

108+
scm {
109+
connection = 'https://github.com/dirkbolte/wiremock-extension-state.git'
110+
developerConnection = 'https://github.com/dirkbolte/wiremock-extension-state.git'
111+
url = 'https://github.com/dirkbolte/wiremock-extension-state.git'
112+
}
113+
114+
licenses {
115+
license {
116+
name = 'The Apache Software License, Version 2.0'
117+
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
118+
distribution = 'repo'
119+
}
120+
}
121+
122+
developers {
123+
developer {
124+
id = 'dirkbolte'
125+
name = 'Dirk Bolte'
126+
127+
}
128+
}
129+
}
130+
}
131+
standaloneJar(MavenPublication) { publication ->
132+
artifactId = "${baseArtifact}-standalone"
133+
134+
project.shadow.component(publication)
135+
136+
pom {
137+
138+
name = "${baseArtifact}-standalone"
139+
description = 'A WireMock extension to transfer state in between stubs - to be used with WireMock standalone'
140+
url = 'https://github.com/dirkbolte/wiremock-extension-state'
141+
142+
63143
scm {
64144
connection = 'https://github.com/dirkbolte/wiremock-extension-state.git'
65145
developerConnection = 'https://github.com/dirkbolte/wiremock-extension-state.git'
@@ -117,6 +197,7 @@ compileJava {
117197
compileTestJava {
118198
options.encoding = 'UTF-8'
119199
}
200+
assemble.dependsOn jar, shadowJar
120201

121202
test {
122203
useJUnitPlatform()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip
44
networkTimeout=10000
55
zipStoreBase=GRADLE_USER_HOME
66
zipStorePath=wrapper/dists
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.wiremock.extensions.state.StandaloneStateExtension
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (C) 2023 Dirk Bolte
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.wiremock.extensions.state;
17+
18+
import com.github.tomakehurst.wiremock.extension.Extension;
19+
import com.github.tomakehurst.wiremock.extension.ExtensionFactory;
20+
import com.github.tomakehurst.wiremock.extension.WireMockServices;
21+
import com.github.tomakehurst.wiremock.extension.responsetemplating.TemplateEngine;
22+
import com.github.tomakehurst.wiremock.store.Store;
23+
import org.wiremock.extensions.state.extensions.DeleteStateEventListener;
24+
import org.wiremock.extensions.state.extensions.RecordStateEventListener;
25+
import org.wiremock.extensions.state.extensions.StateRequestMatcher;
26+
import org.wiremock.extensions.state.extensions.StateTemplateHelperProviderExtension;
27+
import org.wiremock.extensions.state.internal.ContextManager;
28+
29+
import java.util.Collections;
30+
import java.util.List;
31+
32+
/**
33+
* Factory to register all extensions for handling state for standalone service.
34+
*
35+
* Uses {@link org.wiremock.extensions.state.CaffeineStore}.
36+
*
37+
* @see CaffeineStore
38+
*/
39+
public class StandaloneStateExtension implements ExtensionFactory {
40+
41+
private final TemplateEngine templateEngine = new TemplateEngine(Collections.emptyMap(), null, Collections.emptySet(), false);
42+
43+
private final Store<String, Object> store = new CaffeineStore();
44+
45+
private final ContextManager contextManager = new ContextManager(store);;
46+
47+
@Override
48+
public List<Extension> create(WireMockServices services) {
49+
return List.of(
50+
new RecordStateEventListener(contextManager, templateEngine),
51+
new DeleteStateEventListener(contextManager, templateEngine),
52+
new StateRequestMatcher(contextManager, templateEngine),
53+
new StateTemplateHelperProviderExtension(contextManager)
54+
);
55+
}
56+
}

0 commit comments

Comments
 (0)