Skip to content

Commit 06ad4d3

Browse files
committed
#290 mapping form matcher to simulation object
1 parent 4a7f351 commit 06ad4d3

File tree

5 files changed

+75
-13
lines changed

5 files changed

+75
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.specto.hoverfly.junit.core.model;
2+
3+
import com.fasterxml.jackson.core.JsonParser;
4+
import com.fasterxml.jackson.core.JsonProcessingException;
5+
import com.fasterxml.jackson.core.type.TypeReference;
6+
import com.fasterxml.jackson.databind.DeserializationContext;
7+
import com.fasterxml.jackson.databind.JsonDeserializer;
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import com.fasterxml.jackson.databind.type.CollectionType;
10+
import io.specto.hoverfly.junit.core.model.RequestFieldMatcher.MatcherType;
11+
import java.io.IOException;
12+
import java.util.List;
13+
import java.util.Map;
14+
15+
public class BodyRequestMatcherCustomDeserializer extends JsonDeserializer<List<RequestFieldMatcher>> {
16+
17+
private final ObjectMapper objectMapper = new ObjectMapper();
18+
19+
@Override
20+
public List<RequestFieldMatcher> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
21+
22+
CollectionType matcherType = deserializationContext.getTypeFactory().constructCollectionType(List.class, RequestFieldMatcher.class);
23+
List<RequestFieldMatcher> matchers = deserializationContext.readValue(jsonParser, matcherType);
24+
25+
matchers.stream()
26+
.filter(matcher -> matcher.getMatcher() == MatcherType.FORM)
27+
.forEach(formMatcher -> {
28+
try {
29+
String rawFormMatcherValue = objectMapper.writeValueAsString(formMatcher.getValue());
30+
Map<String, List<RequestFieldMatcher>> formMatcherValue = objectMapper.readValue(
31+
rawFormMatcherValue, new TypeReference<Map<String, List<RequestFieldMatcher>>>() {});
32+
33+
formMatcher.setValue(formMatcherValue);
34+
} catch (JsonProcessingException e) {
35+
throw new RuntimeException(e);
36+
}
37+
});
38+
return matchers;
39+
}
40+
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
1616
import com.fasterxml.jackson.annotation.JsonInclude;
1717
import com.fasterxml.jackson.core.JsonProcessingException;
18+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
1819
import io.specto.hoverfly.junit.core.ObjectMapperFactory;
1920
import java.util.List;
2021
import java.util.Map;
@@ -36,6 +37,7 @@ public class Request {
3637

3738
private Map<String, List<RequestFieldMatcher>> query;
3839

40+
@JsonDeserialize(using = BodyRequestMatcherCustomDeserializer.class)
3941
private List<RequestFieldMatcher> body;
4042

4143
private Map<String, List<RequestFieldMatcher>> headers;

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

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.apache.commons.lang3.builder.ToStringBuilder;
77

88
import java.util.List;
9+
import java.util.Map;
910

1011
import static io.specto.hoverfly.junit.core.model.RequestFieldMatcher.MatcherType.*;
1112

@@ -75,6 +76,10 @@ public static RequestFieldMatcher<List<String>> newArrayMatcher(List<String> val
7576
return new RequestFieldMatcher<>(ARRAY, value, arrayMatcherConfig);
7677
}
7778

79+
public static RequestFieldMatcher<Map<String, List<RequestFieldMatcher<?>>>> newFormMatcher(Map<String, List<RequestFieldMatcher<?>>> value) {
80+
return new RequestFieldMatcher<>(FORM, value);
81+
}
82+
7883
public static RequestFieldMatcher<String> newJwtMatcher(String value) {
7984
return new RequestFieldMatcher<>(JWT, value);
8085
}

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

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
package io.specto.hoverfly.junit.core.model;
22

33

4+
import static io.specto.hoverfly.junit.core.model.RequestFieldMatcher.newExactMatcher;
5+
import static java.util.Collections.singletonList;
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
48
import com.fasterxml.jackson.databind.ObjectMapper;
59
import com.google.common.collect.ImmutableList;
610
import com.google.common.collect.ImmutableMap;
711
import com.google.common.collect.Lists;
812
import com.google.common.collect.Sets;
913
import com.google.common.io.Resources;
10-
import org.junit.Test;
11-
import org.skyscreamer.jsonassert.JSONAssert;
12-
import org.skyscreamer.jsonassert.JSONCompareMode;
13-
1414
import java.net.URL;
1515
import java.nio.charset.StandardCharsets;
1616
import java.util.Arrays;
1717
import java.util.Collections;
18-
19-
import static io.specto.hoverfly.junit.core.model.RequestFieldMatcher.newExactMatcher;
20-
import static java.util.Collections.singletonList;
21-
import static org.assertj.core.api.Assertions.assertThat;
18+
import org.junit.Test;
19+
import org.skyscreamer.jsonassert.JSONAssert;
20+
import org.skyscreamer.jsonassert.JSONCompareMode;
2221

2322
public class SimulationTest {
2423

25-
24+
2625
private final ObjectMapper objectMapper = new ObjectMapper();
2726

2827
private final URL v5Resource = Resources.getResource("simulations/v5-simulation.json");
@@ -124,15 +123,18 @@ private Simulation getSimulationWithV5_2Matchers() {
124123
.query(ImmutableMap.of("key", singletonList(RequestFieldMatcher.newArrayMatcher(Arrays.asList("value1", "value2"), new ArrayMatcherConfig(true, true, false)))))
125124
// JWT Matcher
126125
.headers(ImmutableMap.of("Authorization", singletonList(RequestFieldMatcher.newJwtMatcher("{\"header\":{\"alg\":\"HS256\"},\"payload\":{\"sub\":\"1234567890\",\"name\":\"John Doe\"}}"))))
126+
.body(singletonList(RequestFieldMatcher.newFormMatcher(ImmutableMap.of(
127+
"grant_type", singletonList(RequestFieldMatcher.newExactMatcher("authorization_code")),
128+
"client_assertion", singletonList(RequestFieldMatcher.newJwtMatcher("{\"header\":{\"alg\":\"HS256\"},\"payload\":{\"sub\":\"1234567890\",\"name\":\"John Doe\"}}"))
129+
))))
127130
.requiresState(ImmutableMap.of("requiresStateKey", "requiresStateValue"));
128131
Response.Builder responseBuilder = getTestResponseBuilder()
129132
.transitionsState(ImmutableMap.of("transitionsStateKey", "transitionsStateValue"))
130133
.removesState(ImmutableList.of("removesStateKey"))
131134
.fixedDelay(3000);
132135
HoverflyData data = getTestHoverflyData(requestBuilder, responseBuilder);
133136
HoverflyMetaData meta = new HoverflyMetaData();
134-
Simulation simulation = new Simulation(data, meta);
135-
return simulation;
137+
return new Simulation(data, meta);
136138
}
137139

138140
private Simulation getLatestSimulation() {

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

+15-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,21 @@
2929
],
3030
"body": [
3131
{
32-
"matcher": "exact",
33-
"value": ""
32+
"matcher": "form",
33+
"value": {
34+
"grant_type": [
35+
{
36+
"matcher": "exact",
37+
"value": "authorization_code"
38+
}
39+
],
40+
"client_assertion": [
41+
{
42+
"matcher": "jwt",
43+
"value": "{\"header\":{\"alg\":\"HS256\"},\"payload\":{\"sub\":\"1234567890\",\"name\":\"John Doe\"}}"
44+
}
45+
]
46+
}
3447
}
3548
],
3649
"headers": {

0 commit comments

Comments
 (0)