Provide a public API for resolving the type of a field
See gh-549
This commit is contained in:
committed by
Andy Wilkinson
parent
ada273ba9e
commit
4453fde23c
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2014-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.restdocs.payload;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link FieldTypeResolver}.
|
||||
*
|
||||
* @author Mathias Düsterhöft
|
||||
*/
|
||||
public class FieldTypeResolverTests {
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrownException = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void returnJsonFieldTypeResolver() {
|
||||
assertThat(FieldTypeResolver.forContent("{\"field\": \"value\"}".getBytes(),
|
||||
MediaType.APPLICATION_JSON)).isInstanceOf(JsonContentHandler.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnXmlContentHandler() {
|
||||
assertThat(FieldTypeResolver.forContent("<a><b>5</b></a>".getBytes(),
|
||||
MediaType.APPLICATION_XML)).isInstanceOf(XmlContentHandler.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void throwOnInvalidContent() {
|
||||
this.thrownException.expect(PayloadHandlingException.class);
|
||||
FieldTypeResolver.forContent("some".getBytes(), MediaType.APPLICATION_XML);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -29,6 +29,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* Tests for {@link JsonContentHandler}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Mathias Düsterhöft
|
||||
*/
|
||||
public class JsonContentHandlerTests {
|
||||
|
||||
@@ -39,14 +40,14 @@ public class JsonContentHandlerTests {
|
||||
public void typeForFieldWithNullValueMustMatch() {
|
||||
this.thrown.expect(FieldTypesDoNotMatchException.class);
|
||||
new JsonContentHandler("{\"a\": null}".getBytes())
|
||||
.determineFieldType(new FieldDescriptor("a").type(JsonFieldType.STRING));
|
||||
.resolveFieldType(new FieldDescriptor("a").type(JsonFieldType.STRING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typeForFieldWithNotNullAndThenNullValueMustMatch() {
|
||||
this.thrown.expect(FieldTypesDoNotMatchException.class);
|
||||
new JsonContentHandler("{\"a\":[{\"id\":1},{\"id\":null}]}".getBytes())
|
||||
.determineFieldType(
|
||||
.resolveFieldType(
|
||||
new FieldDescriptor("a[].id").type(JsonFieldType.STRING));
|
||||
}
|
||||
|
||||
@@ -54,7 +55,7 @@ public class JsonContentHandlerTests {
|
||||
public void typeForFieldWithNullAndThenNotNullValueMustMatch() {
|
||||
this.thrown.expect(FieldTypesDoNotMatchException.class);
|
||||
new JsonContentHandler("{\"a\":[{\"id\":null},{\"id\":1}]}".getBytes())
|
||||
.determineFieldType(
|
||||
.resolveFieldType(
|
||||
new FieldDescriptor("a.[].id").type(JsonFieldType.STRING));
|
||||
}
|
||||
|
||||
@@ -62,7 +63,7 @@ public class JsonContentHandlerTests {
|
||||
public void typeForOptionalFieldWithNumberAndThenNullValueIsNumber() {
|
||||
Object fieldType = new JsonContentHandler(
|
||||
"{\"a\":[{\"id\":1},{\"id\":null}]}\"".getBytes())
|
||||
.determineFieldType(new FieldDescriptor("a[].id").optional());
|
||||
.resolveFieldType(new FieldDescriptor("a[].id").optional());
|
||||
assertThat((JsonFieldType) fieldType).isEqualTo(JsonFieldType.NUMBER);
|
||||
}
|
||||
|
||||
@@ -70,7 +71,7 @@ public class JsonContentHandlerTests {
|
||||
public void typeForOptionalFieldWithNullAndThenNumberIsNumber() {
|
||||
Object fieldType = new JsonContentHandler(
|
||||
"{\"a\":[{\"id\":null},{\"id\":1}]}".getBytes())
|
||||
.determineFieldType(new FieldDescriptor("a[].id").optional());
|
||||
.resolveFieldType(new FieldDescriptor("a[].id").optional());
|
||||
assertThat((JsonFieldType) fieldType).isEqualTo(JsonFieldType.NUMBER);
|
||||
}
|
||||
|
||||
@@ -78,7 +79,7 @@ public class JsonContentHandlerTests {
|
||||
public void typeForFieldWithNumberAndThenNullValueIsVaries() {
|
||||
Object fieldType = new JsonContentHandler(
|
||||
"{\"a\":[{\"id\":1},{\"id\":null}]}\"".getBytes())
|
||||
.determineFieldType(new FieldDescriptor("a[].id"));
|
||||
.resolveFieldType(new FieldDescriptor("a[].id"));
|
||||
assertThat((JsonFieldType) fieldType).isEqualTo(JsonFieldType.VARIES);
|
||||
}
|
||||
|
||||
@@ -86,14 +87,14 @@ public class JsonContentHandlerTests {
|
||||
public void typeForFieldWithNullAndThenNumberIsVaries() {
|
||||
Object fieldType = new JsonContentHandler(
|
||||
"{\"a\":[{\"id\":null},{\"id\":1}]}".getBytes())
|
||||
.determineFieldType(new FieldDescriptor("a[].id"));
|
||||
.resolveFieldType(new FieldDescriptor("a[].id"));
|
||||
assertThat((JsonFieldType) fieldType).isEqualTo(JsonFieldType.VARIES);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typeForOptionalFieldWithNullValueCanBeProvidedExplicitly() {
|
||||
Object fieldType = new JsonContentHandler("{\"a\": null}".getBytes())
|
||||
.determineFieldType(
|
||||
.resolveFieldType(
|
||||
new FieldDescriptor("a").type(JsonFieldType.STRING).optional());
|
||||
assertThat((JsonFieldType) fieldType).isEqualTo(JsonFieldType.STRING);
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ import static org.springframework.restdocs.payload.PayloadDocumentation.subsecti
|
||||
* Tests for {@link XmlContentHandler}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Mathias Düsterhöft
|
||||
*/
|
||||
public class XmlContentHandlerTests {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user