Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,9 @@ private FieldDescriptor copyWithType(FieldDescriptor source, Object type) {
}

private static Attribute[] asArray(Map<String, Object> attributeMap) {
List<Attributes.Attribute> attributes = new ArrayList<>();
for (Map.Entry<String, Object> attribute : attributeMap.entrySet()) {
attributes
.add(Attributes.key(attribute.getKey()).value(attribute.getValue()));
}
return attributes.toArray(new Attribute[attributes.size()]);
return attributeMap.entrySet().stream()
.map((attribute) -> Attributes.key(attribute.getKey()).value(attribute.getValue()))
.toArray(Attribute[]::new);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1644,12 +1644,9 @@ public static PayloadSubsectionExtractor<?> beneathPath(String path) {
}

private static Attribute[] asArray(Map<String, Object> attributeMap) {
List<Attributes.Attribute> attributes = new ArrayList<>();
for (Map.Entry<String, Object> attribute : attributeMap.entrySet()) {
attributes
.add(Attributes.key(attribute.getKey()).value(attribute.getValue()));
}
return attributes.toArray(new Attribute[attributes.size()]);
return attributeMap.entrySet().stream()
.map((attribute) -> Attributes.key(attribute.getKey()).value(attribute.getValue()))
.toArray(Attribute[]::new);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.springframework.restdocs.payload;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

Expand All @@ -39,64 +38,62 @@ public class JsonContentHandlerTests {
public ExpectedException thrown = ExpectedException.none();

@Test
public void typeForFieldWithNullValueMustMatch() throws IOException {
public void typeForFieldWithNullValueMustMatch() {
this.thrown.expect(FieldTypesDoNotMatchException.class);
new JsonContentHandler("{\"a\": null}".getBytes())
.determineFieldType(new FieldDescriptor("a").type(JsonFieldType.STRING));
}

@Test
public void typeForFieldWithNotNullAndThenNullValueMustMatch() throws IOException {
public void typeForFieldWithNotNullAndThenNullValueMustMatch() {
this.thrown.expect(FieldTypesDoNotMatchException.class);
new JsonContentHandler("{\"a\":[{\"id\":1},{\"id\":null}]}".getBytes())
.determineFieldType(
new FieldDescriptor("a[].id").type(JsonFieldType.STRING));
}

@Test
public void typeForFieldWithNullAndThenNotNullValueMustMatch() throws IOException {
public void typeForFieldWithNullAndThenNotNullValueMustMatch() {
this.thrown.expect(FieldTypesDoNotMatchException.class);
new JsonContentHandler("{\"a\":[{\"id\":null},{\"id\":1}]}".getBytes())
.determineFieldType(
new FieldDescriptor("a.[].id").type(JsonFieldType.STRING));
}

@Test
public void typeForOptionalFieldWithNumberAndThenNullValueIsNumber()
throws IOException {
public void typeForOptionalFieldWithNumberAndThenNullValueIsNumber() {
Object fieldType = new JsonContentHandler(
"{\"a\":[{\"id\":1},{\"id\":null}]}\"".getBytes())
.determineFieldType(new FieldDescriptor("a[].id").optional());
assertThat((JsonFieldType) fieldType, is(equalTo(JsonFieldType.NUMBER)));
}

@Test
public void typeForOptionalFieldWithNullAndThenNumberIsNumber() throws IOException {
public void typeForOptionalFieldWithNullAndThenNumberIsNumber() {
Object fieldType = new JsonContentHandler(
"{\"a\":[{\"id\":null},{\"id\":1}]}".getBytes())
.determineFieldType(new FieldDescriptor("a[].id").optional());
assertThat((JsonFieldType) fieldType, is(equalTo(JsonFieldType.NUMBER)));
}

@Test
public void typeForFieldWithNumberAndThenNullValueIsVaries() throws IOException {
public void typeForFieldWithNumberAndThenNullValueIsVaries() {
Object fieldType = new JsonContentHandler(
"{\"a\":[{\"id\":1},{\"id\":null}]}\"".getBytes())
.determineFieldType(new FieldDescriptor("a[].id"));
assertThat((JsonFieldType) fieldType, is(equalTo(JsonFieldType.VARIES)));
}

@Test
public void typeForFieldWithNullAndThenNumberIsVaries() throws IOException {
public void typeForFieldWithNullAndThenNumberIsVaries() {
Object fieldType = new JsonContentHandler(
"{\"a\":[{\"id\":null},{\"id\":1}]}".getBytes())
.determineFieldType(new FieldDescriptor("a[].id"));
assertThat((JsonFieldType) fieldType, is(equalTo(JsonFieldType.VARIES)));
}

@Test
public void typeForOptionalFieldWithNullValueCanBeProvidedExplicitly()
throws IOException {
public void typeForOptionalFieldWithNullValueCanBeProvidedExplicitly() {
Object fieldType = new JsonContentHandler("{\"a\": null}".getBytes())
.determineFieldType(
new FieldDescriptor("a").type(JsonFieldType.STRING).optional());
Expand Down