Skip to content

Commit 5286c86

Browse files
committed
Fix hasContext matcher with property/list in logical operator
1 parent 835b1a7 commit 5286c86

File tree

4 files changed

+63
-11
lines changed

4 files changed

+63
-11
lines changed

src/main/java/org/wiremock/extensions/state/extensions/requestmatcher/StateRequestMatcher.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,31 +109,31 @@ public MatchResult match(Request request, Parameters parameters) {
109109
if (validationMessage != null) {
110110
throw createConfigurationError(validationMessage);
111111
}
112-
return matchContext(model, parameters, matcher);
112+
return matchContext(model, matcher);
113113

114114
} catch (IllegalArgumentException ex) {
115115
throw createConfigurationError("You have to specify 'hasContext' or 'hasNotContext'");
116116
}
117117
}
118118

119-
private MatchResult matchContext(Map<String, Object> model, Parameters parameters, BaseRequestMatcher matcher) {
119+
private MatchResult matchContext(Map<String, Object> model, BaseRequestMatcher matcher) {
120120
if (matcher instanceof BaseContextMatcher) {
121-
return matchContext(model, parameters, (BaseContextMatcher) matcher);
121+
return matchContext(model, (BaseContextMatcher) matcher);
122122
} else if (matcher instanceof Not) {
123-
var matchResult = matchContext(model, parameters, ((Not) matcher).getBaseRequestMatcher());
123+
var matchResult = matchContext(model, ((Not) matcher).getBaseRequestMatcher());
124124
return MatchResult.partialMatch(1.0 - matchResult.getDistance());
125125
} else if (matcher instanceof And) {
126126
var containedMatcher = ((And) matcher).getBaseRequestMatcher();
127127
var matchResults = containedMatcher
128128
.stream()
129-
.map(it -> matchContext(model, parameters, it))
129+
.map(it -> matchContext(model, it))
130130
.collect(Collectors.toList());
131131
return MatchResult.aggregate(matchResults);
132132
} else if (matcher instanceof Or) {
133133
var containedMatcher = ((Or) matcher).getBaseRequestMatcher();
134134
var matchResults = containedMatcher
135135
.stream()
136-
.map(it -> matchContext(model, parameters, it))
136+
.map(it -> matchContext(model, it))
137137
.filter(MatchResult::isExactMatch)
138138
.collect(Collectors.toList());
139139
return matchResults.stream().findFirst().orElseGet(MatchResult::noMatch);
@@ -142,10 +142,10 @@ private MatchResult matchContext(Map<String, Object> model, Parameters parameter
142142
}
143143
}
144144

145-
private MatchResult matchContext(Map<String, Object> model, Parameters parameters, BaseContextMatcher matcher) {
145+
private MatchResult matchContext(Map<String, Object> model, BaseContextMatcher matcher) {
146146
var template = matcher.getContextTemplate();
147147
if (matcher instanceof HasContext) {
148-
return hasContext(model, parameters, template);
148+
return hasContext(model, matcher.unmappedFields(), template);
149149
} else if (matcher instanceof HasNotContext) {
150150
return hasNotContext(model, template);
151151
} else {
@@ -162,7 +162,7 @@ private MatchResult hasContext(Map<String, Object> model, Parameters parameters,
162162
logger().info(context, "hasContext matched");
163163
return MatchResult.exactMatch();
164164
} else {
165-
return calculateMatch(model, context, matchers);
165+
return calculateMatch(model, context, matchers);
166166
}
167167
}).orElseGet(MatchResult::noMatch);
168168
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
package org.wiremock.extensions.state.extensions.requestmatcher.model;
22

3+
import com.github.tomakehurst.wiremock.extension.Parameters;
4+
5+
import java.util.Map;
6+
37
public interface BaseContextMatcher {
48
String getContextTemplate();
59

10+
default Parameters unmappedFields() {
11+
return Parameters.empty();
12+
}
13+
614
}
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
11
package org.wiremock.extensions.state.extensions.requestmatcher.model;
22

3+
import com.fasterxml.jackson.annotation.JsonAnySetter;
34
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
45
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import com.github.tomakehurst.wiremock.extension.Parameters;
57

68
@JsonIgnoreProperties(ignoreUnknown = true)
79
public class HasContext implements BaseRequestMatcher, BaseContextMatcher {
810
private final String contextTemplate;
11+
private final Parameters unmappedFields = Parameters.empty();
912

1013
public HasContext(@JsonProperty("hasContext") String contextTemplate) {
1114
this.contextTemplate = contextTemplate;
1215
}
1316

17+
@JsonAnySetter
18+
public void setUnmappedField(String key, Object value) {
19+
unmappedFields.put(key, value);
20+
}
21+
1422
@Override
1523
public String getContextTemplate() {
1624
return contextTemplate;
1725
}
1826

1927
@Override
2028
public String assertValid() {
21-
if(contextTemplate == null) {
29+
if (contextTemplate == null) {
2230
return "'hasContext' must be specified";
2331
} else {
2432
return null;
2533
}
2634
}
35+
36+
@Override
37+
public Parameters unmappedFields() {
38+
return unmappedFields;
39+
}
2740
}

src/test/java/org/wiremock/extensions/state/functionality/StateRequestMatcherTest.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,17 @@ void test_onePositive_oneNegative_fail() {
358358

359359
getAndAssertContextMatcher(context, HttpStatus.SC_NOT_FOUND);
360360
}
361+
362+
@DisplayName("fails when all matcher with nested property matcher fail")
363+
@Test
364+
void test_allNegative_property_matcher_fail() {
365+
var context = postAndAssertContextValue("a context value");
366+
367+
createGetStub(Map.of("or", List.of(Map.of("hasNotContext", context), Map.of("hasContext", context, "property", Map.of("contextValue", Map.of("equalTo", "another context value"))))));
368+
369+
getAndAssertContextMatcher(context, HttpStatus.SC_NOT_FOUND);
370+
}
371+
361372
}
362373

363374
@DisplayName("with matcher 'or'")
@@ -379,6 +390,16 @@ void test_onePositive_oneNegative_ok() {
379390
getAndAssertContextMatcher(context, HttpStatus.SC_OK);
380391
}
381392

393+
@DisplayName("succeeds when one matcher's nested property matcher succeeds")
394+
@Test
395+
void test_onePositive_oneNegative_property_matcher_ok() {
396+
var context = postAndAssertContextValue("a context value");
397+
398+
createGetStub(Map.of("or", List.of(Map.of("hasContext", context, "property", Map.of("stateValue", Map.of("equalTo", "a context value"))), Map.of("hasContext", randomAlphabetic(5)))));
399+
400+
getAndAssertContextMatcher(context, HttpStatus.SC_OK);
401+
}
402+
382403
@DisplayName("succeeds when nested matchers succeed")
383404
@Test
384405
void test_nestedMatchers_ok() {
@@ -394,7 +415,17 @@ void test_nestedMatchers_ok() {
394415
void test_allNegative_fail() {
395416
var context = postAndAssertContextValue(randomAlphabetic(5));
396417

397-
createGetStub(Map.of("and", List.of(Map.of("hasNotContext", context), Map.of("hasContext", randomAlphabetic(5)))));
418+
createGetStub(Map.of("or", List.of(Map.of("hasNotContext", context), Map.of("hasContext", randomAlphabetic(5)))));
419+
420+
getAndAssertContextMatcher(context, HttpStatus.SC_NOT_FOUND);
421+
}
422+
423+
@DisplayName("fails when all matcher with nested property matcher fail")
424+
@Test
425+
void test_allNegative_property_matcher_fail() {
426+
var context = postAndAssertContextValue("a context value");
427+
428+
createGetStub(Map.of("or", List.of(Map.of("hasNotContext", context), Map.of("hasContext", context, "property", Map.of("stateValue", Map.of("equalTo", "another context value"))))));
398429

399430
getAndAssertContextMatcher(context, HttpStatus.SC_NOT_FOUND);
400431
}

0 commit comments

Comments
 (0)