Consider optionality when finding uncommon fields in subsection extraction

Fixes gh-573
This commit is contained in:
Andy Wilkinson
2019-09-18 14:26:40 +01:00
parent 3f66b066f7
commit 93a43dfd0a
6 changed files with 104 additions and 18 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.restdocs.payload;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import com.fasterxml.jackson.core.JsonParseException;
@@ -101,11 +102,35 @@ public class FieldPathPayloadSubsectionExtractorTests {
public void extractMapSubsectionWithVaryingStructureFromMultiElementArrayInAJsonMap()
throws JsonParseException, JsonMappingException, IOException {
this.thrown.expect(PayloadHandlingException.class);
this.thrown.expectMessage("The following uncommon paths were found: [a.[].b.d]");
this.thrown.expectMessage("The following non-optional uncommon paths were found: [a.[].b.d]");
new FieldPathPayloadSubsectionExtractor("a.[].b").extractSubsection(
"{\"a\":[{\"b\":{\"c\":5}},{\"b\":{\"c\":6, \"d\": 7}}]}".getBytes(), MediaType.APPLICATION_JSON);
}
@Test
@SuppressWarnings("unchecked")
public void extractMapSubsectionWithVaryingStructureDueToOptionalFieldsFromMultiElementArrayInAJsonMap()
throws JsonParseException, JsonMappingException, IOException {
byte[] extractedPayload = new FieldPathPayloadSubsectionExtractor("a.[].b").extractSubsection(
"{\"a\":[{\"b\":{\"c\":5}},{\"b\":{\"c\":6, \"d\": 7}}]}".getBytes(), MediaType.APPLICATION_JSON,
Arrays.asList(new FieldDescriptor("d").optional()));
Map<String, Object> extracted = new ObjectMapper().readValue(extractedPayload, Map.class);
assertThat(extracted.size()).isEqualTo(1);
assertThat(extracted).containsOnlyKeys("c");
}
@Test
@SuppressWarnings("unchecked")
public void extractMapSubsectionWithVaryingStructureDueToOptionalParentFieldsFromMultiElementArrayInAJsonMap()
throws JsonParseException, JsonMappingException, IOException {
byte[] extractedPayload = new FieldPathPayloadSubsectionExtractor("a.[].b").extractSubsection(
"{\"a\":[{\"b\":{\"c\":5}},{\"b\":{\"c\":6, \"d\": { \"e\": 7}}}]}".getBytes(),
MediaType.APPLICATION_JSON, Arrays.asList(new FieldDescriptor("d").optional()));
Map<String, Object> extracted = new ObjectMapper().readValue(extractedPayload, Map.class);
assertThat(extracted.size()).isEqualTo(1);
assertThat(extracted).containsOnlyKeys("c");
}
@Test
public void extractedSubsectionIsPrettyPrintedWhenInputIsPrettyPrinted()
throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {