Skip to content

Commit

Permalink
Add JSON partial matcher to DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
tommysitu committed May 1, 2019
1 parent 0444972 commit fd0eb6b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
4 changes: 4 additions & 0 deletions docs/pages/corefunctionality/matchers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ Here are some examples:
.body(equalsToJson(json(booking))) // Matches body which equals to a JSON Object
.willReturn(success())
.put("/api/bookings/1")
.body(matchesPartialJson(json(booking)))// Matches body which is a superset of a JSON Object (Partial JSON matching)
.willReturn(success())
.post("/api/bookings")
.body(matchesJsonPath("$.flightId")) // Matches body with a JSON path expression
.willReturn(created("http://localhost/api/bookings/1"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public static RequestFieldMatcher newJsonMatcher(String value) {
return new RequestFieldMatcher<>(MatcherType.JSON, value);
}


public static RequestFieldMatcher newJsonPartialMatcher(String value) {
return new RequestFieldMatcher<>(MatcherType.JSONPARTIAL, value);
}

public static RequestFieldMatcher newJsonPathMatch(String value) {
return new RequestFieldMatcher<>(MatcherType.JSONPATH, value);
}
Expand All @@ -76,6 +81,7 @@ public enum MatcherType {
XML,
XPATH,
JSON,
JSONPARTIAL,
JSONPATH;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static RequestFieldMatcher any() {
/**
* Create a matcher that matches on the given JSON
* @param value the JSON string value
* @return an {@link RequestFieldMatcher} that includes jsonMatch
* @return an {@link RequestFieldMatcher}
*/
public static RequestFieldMatcher equalsToJson(String value) {
validateJson(value);
Expand All @@ -102,10 +102,29 @@ public static RequestFieldMatcher equalsToJson(HttpBodyConverter converter) {
return equalsToJson(converter.body());
}

/**
* Create a matcher that matches on the given partial JSON document
* @param value the JSON string value
* @return an {@link RequestFieldMatcher}
*/
public static RequestFieldMatcher matchesPartialJson(String value) {
validateJson(value);
return RequestFieldMatcher.newJsonPartialMatcher(value);
}

/**
* Create a matcher that matches on a partial JSON document serialized from a JAVA object by {@link HttpBodyConverter}
* @param converter the {@link HttpBodyConverter} with an object to be serialized to JSON
* @return an {@link RequestFieldMatcher}
*/
public static RequestFieldMatcher matchesPartialJson(HttpBodyConverter converter) {
return matchesPartialJson(converter.body());
}

/**
* Create a matcher that matches on the given JsonPath expression
* @param expression the JsonPath expression
* @return an {@link RequestFieldMatcher} that includes jsonPathMatch
* @return an {@link RequestFieldMatcher}
*/
public static RequestFieldMatcher matchesJsonPath(String expression) {
return RequestFieldMatcher.newJsonPathMatch(expression);
Expand All @@ -114,7 +133,7 @@ public static RequestFieldMatcher matchesJsonPath(String expression) {
/**
* Create a matcher that matches on the given XML
* @param value the XML string value
* @return an {@link RequestFieldMatcher} that includes xmlMatch
* @return an {@link RequestFieldMatcher}
*/
public static RequestFieldMatcher equalsToXml(String value) {
validateXml(value);
Expand All @@ -133,7 +152,7 @@ public static RequestFieldMatcher equalsToXml(HttpBodyConverter converter) {
/**
* Create a matcher that matches on the given XPath expression
* @param expression the XPath expression
* @return an {@link RequestFieldMatcher} that includes xpathMatch
* @return an {@link RequestFieldMatcher}
*/
public static RequestFieldMatcher matchesXPath(String expression) {
return RequestFieldMatcher.newXpathMatcher(expression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ public void shouldCreateJsonMatcherFromString() {
assertThat(matcher.getValue()).isEqualTo("{\"flightId\":\"1\",\"class\":\"PREMIUM\"}");
}

@Test
public void shouldCreatePartialJsonMatcherFromString() {
RequestFieldMatcher matcher = HoverflyMatchers.matchesPartialJson("{\"flightId\":\"1\",\"class\":\"PREMIUM\"}");

assertThat(matcher.getMatcher()).isEqualTo(RequestFieldMatcher.MatcherType.JSONPARTIAL);
assertThat(matcher.getValue()).isEqualTo("{\"flightId\":\"1\",\"class\":\"PREMIUM\"}");
}

@Test
public void shouldThrowExceptionIfInputStringIsInvalidJsonFormat() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class HoverflyDslMatcherTest {
.anyQueryParams()
.willReturn(success(json(booking)))

// Match XML body
// Match JSON body
.put("/api/bookings/1")
.header("Content-Type", contains("application/json"))
.body(equalsToJson("{\"flightId\":\"1\",\"class\":\"PREMIUM\"}"))
Expand All @@ -63,6 +63,12 @@ public class HoverflyDslMatcherTest {
.body(equalsToJson(json(booking)))
.willReturn(success())

// Match partial JSON body
.put("/api/bookings/1")
.header("Content-Type", contains("application/json"))
.body(matchesPartialJson("{\"flightId\":\"1\"}"))
.willReturn(success())

// JsonPath Matcher
.post("/api/bookings")
// .body(matchesJsonPath("$[?(@.flightId == 1)]")) // TODO: this expression is not supported
Expand Down Expand Up @@ -240,6 +246,20 @@ public void shouldBeAbleToMatchBodyByJsonEqualityWithHttpBodyConverter() throws
assertThat(bookFlightResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
}

@Test
public void shouldBeAbleToMatchJSONBodyPartially() throws Exception {
// Given
final RequestEntity<String> bookFlightRequest = RequestEntity.put(new URI("http://www.my-test.com/api/bookings/1"))
.contentType(APPLICATION_JSON)
.body("{\"flightId\": \"1\"}");

// When
final ResponseEntity<String> bookFlightResponse = restTemplate.exchange(bookFlightRequest, String.class);

// Then
assertThat(bookFlightResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
}

@Test
public void shouldBeAbleToMatchBodyByJsonPath() throws Exception {
// Given
Expand Down

0 comments on commit fd0eb6b

Please sign in to comment.