Skip to content

Commit d53df1e

Browse files
committed
Update simulation DTO to support matcher chaining with builder pattern
1 parent 06ad4d3 commit d53df1e

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

src/main/java/io/specto/hoverfly/junit/core/model/RequestFieldMatcher.java

+34
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class RequestFieldMatcher<T> {
1717
private MatcherType matcher;
1818
private T value;
1919
private MatcherConfig config;
20+
private RequestFieldMatcher<?> doMatch;
2021

2122
public RequestFieldMatcher() {
2223
}
@@ -56,6 +57,14 @@ public void setConfig(MatcherConfig config) {
5657
this.config = config;
5758
}
5859

60+
public RequestFieldMatcher<?> getDoMatch() {
61+
return doMatch;
62+
}
63+
64+
public void setDoMatch(RequestFieldMatcher<?> doMatch) {
65+
this.doMatch = doMatch;
66+
}
67+
5968
public static RequestFieldMatcher<String> newExactMatcher(String value) {
6069
return new RequestFieldMatcher<>(EXACT, value);
6170
}
@@ -105,6 +114,31 @@ public static RequestFieldMatcher<String> newJsonPathMatch(String value) {
105114
return new RequestFieldMatcher<>(MatcherType.JSONPATH, value);
106115
}
107116

117+
public static class MatcherChainingBuilder {
118+
119+
private final RequestFieldMatcher<?> rootMatcher;
120+
private RequestFieldMatcher<?> currentMatcher;
121+
122+
private MatcherChainingBuilder(RequestFieldMatcher<?> rootMatcher) {
123+
this.rootMatcher = rootMatcher;
124+
this.currentMatcher = rootMatcher;
125+
}
126+
127+
public static MatcherChainingBuilder root(RequestFieldMatcher<?> matcher) {
128+
return new MatcherChainingBuilder(matcher);
129+
}
130+
131+
public MatcherChainingBuilder next(RequestFieldMatcher<?> matcher) {
132+
currentMatcher.setDoMatch(matcher);
133+
currentMatcher = matcher;
134+
return this;
135+
}
136+
137+
public RequestFieldMatcher<?> build() {
138+
return rootMatcher;
139+
}
140+
141+
}
108142

109143
public enum MatcherType {
110144
EXACT,

src/test/java/io/specto/hoverfly/junit/core/model/SimulationTest.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.google.common.collect.Lists;
1212
import com.google.common.collect.Sets;
1313
import com.google.common.io.Resources;
14+
import io.specto.hoverfly.junit.core.model.RequestFieldMatcher.MatcherChainingBuilder;
1415
import java.net.URL;
1516
import java.nio.charset.StandardCharsets;
1617
import java.util.Arrays;
@@ -122,7 +123,12 @@ private Simulation getSimulationWithV5_2Matchers() {
122123
// Array Matcher
123124
.query(ImmutableMap.of("key", singletonList(RequestFieldMatcher.newArrayMatcher(Arrays.asList("value1", "value2"), new ArrayMatcherConfig(true, true, false)))))
124125
// JWT Matcher
125-
.headers(ImmutableMap.of("Authorization", singletonList(RequestFieldMatcher.newJwtMatcher("{\"header\":{\"alg\":\"HS256\"},\"payload\":{\"sub\":\"1234567890\",\"name\":\"John Doe\"}}"))))
126+
.headers(ImmutableMap.of("Authorization", singletonList(
127+
MatcherChainingBuilder.root(RequestFieldMatcher.newJwtMatcher("{\"header\":{\"alg\":\"HS256\"},\"payload\":{\"sub\":\"1234567890\",\"name\":\"John Doe\"}}"))
128+
.next(RequestFieldMatcher.newJsonPathMatch("$.payload.name"))
129+
.next(RequestFieldMatcher.newExactMatcher("John Doe"))
130+
.build()
131+
)))
126132
.body(singletonList(RequestFieldMatcher.newFormMatcher(ImmutableMap.of(
127133
"grant_type", singletonList(RequestFieldMatcher.newExactMatcher("authorization_code")),
128134
"client_assertion", singletonList(RequestFieldMatcher.newJwtMatcher("{\"header\":{\"alg\":\"HS256\"},\"payload\":{\"sub\":\"1234567890\",\"name\":\"John Doe\"}}"))

src/test/resources/simulations/v5_2-simulation.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,15 @@
5050
"Authorization": [
5151
{
5252
"matcher": "jwt",
53-
"value": "{\"header\":{\"alg\":\"HS256\"},\"payload\":{\"sub\":\"1234567890\",\"name\":\"John Doe\"}}"
53+
"value": "{\"header\":{\"alg\":\"HS256\"},\"payload\":{\"sub\":\"1234567890\",\"name\":\"John Doe\"}}",
54+
"doMatch": {
55+
"matcher": "jsonpath",
56+
"value": "$.payload.name",
57+
"doMatch": {
58+
"matcher": "exact",
59+
"value": "John Doe"
60+
}
61+
}
5462
}
5563
]
5664
},

0 commit comments

Comments
 (0)