Add utilities to help with JSON testing
Add Jackson, Gson and Basic String helper classes that allow AssertJ assertions to be used to test JSON. Fixes gh-5471
This commit is contained in:
@@ -0,0 +1,212 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.boot.test.json;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link AbstractJsonMarshalTester}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public abstract class AbstractJsonMarshalTesterTests {
|
||||
|
||||
private static final String JSON = "{\"name\":\"Spring\",\"age\":123}";
|
||||
|
||||
private static final String MAP_JSON = "{\"a\":" + JSON + "}";
|
||||
|
||||
private static final String ARRAY_JSON = "[" + JSON + "]";
|
||||
|
||||
private static final ExampleObject OBJECT = createExampleObject("Spring", 123);
|
||||
|
||||
private static final ResolvableType TYPE = ResolvableType
|
||||
.forClass(ExampleObject.class);
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temp = new TemporaryFolder();
|
||||
|
||||
@Test
|
||||
public void writeShouldReturnJsonContent() throws Exception {
|
||||
JsonContent<Object> content = createTester(TYPE).write(OBJECT);
|
||||
assertThat(content).isEqualToJson(JSON);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeListShouldReturnJsonContent() throws Exception {
|
||||
ResolvableType type = ResolvableTypes.get("listOfExampleObject");
|
||||
List<ExampleObject> value = Collections.singletonList(OBJECT);
|
||||
JsonContent<Object> content = createTester(type).write(value);
|
||||
assertThat(content).isEqualToJson(ARRAY_JSON);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeArrayShouldReturnJsonContent() throws Exception {
|
||||
ResolvableType type = ResolvableTypes.get("arrayOfExampleObject");
|
||||
ExampleObject[] value = new ExampleObject[] { OBJECT };
|
||||
JsonContent<Object> content = createTester(type).write(value);
|
||||
assertThat(content).isEqualToJson(ARRAY_JSON);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeMapShouldReturnJsonContent() throws Exception {
|
||||
ResolvableType type = ResolvableTypes.get("mapOfExampleObject");
|
||||
Map<String, Object> value = new LinkedHashMap<String, Object>();
|
||||
value.put("a", OBJECT);
|
||||
JsonContent<Object> content = createTester(type).write(value);
|
||||
assertThat(content).isEqualToJson(MAP_JSON);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createWhenResourceLoadClassIsNullShouldThrowException() throws Exception {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("ResourceLoadClass must not be null");
|
||||
createTester(null, ResolvableType.forClass(ExampleObject.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createWhenTypeIsNullShouldThrowException() throws Exception {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("Type must not be null");
|
||||
createTester(getClass(), null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseBytesShouldReturnObject() throws Exception {
|
||||
AbstractJsonMarshalTester<Object> tester = createTester(TYPE);
|
||||
assertThat(tester.parse(JSON.getBytes())).isEqualTo(OBJECT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseStringShouldReturnObject() throws Exception {
|
||||
AbstractJsonMarshalTester<Object> tester = createTester(TYPE);
|
||||
assertThat(tester.parse(JSON)).isEqualTo(OBJECT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readResourcePathShouldReturnObject() throws Exception {
|
||||
AbstractJsonMarshalTester<Object> tester = createTester(TYPE);
|
||||
assertThat(tester.read("example.json")).isEqualTo(OBJECT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readFileShouldReturnObject() throws Exception {
|
||||
File file = this.temp.newFile("example.json");
|
||||
FileCopyUtils.copy(JSON.getBytes(), file);
|
||||
AbstractJsonMarshalTester<Object> tester = createTester(TYPE);
|
||||
assertThat(tester.read(file)).isEqualTo(OBJECT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readInputStreamShouldReturnObject() throws Exception {
|
||||
InputStream stream = new ByteArrayInputStream(JSON.getBytes());
|
||||
AbstractJsonMarshalTester<Object> tester = createTester(TYPE);
|
||||
assertThat(tester.read(stream)).isEqualTo(OBJECT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readResourceShouldReturnObject() throws Exception {
|
||||
Resource resource = new ByteArrayResource(JSON.getBytes());
|
||||
AbstractJsonMarshalTester<Object> tester = createTester(TYPE);
|
||||
assertThat(tester.read(resource)).isEqualTo(OBJECT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readReaderShouldReturnObject() throws Exception {
|
||||
Reader reader = new StringReader(JSON);
|
||||
AbstractJsonMarshalTester<Object> tester = createTester(TYPE);
|
||||
assertThat(tester.read(reader)).isEqualTo(OBJECT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseListShouldReturnContent() throws Exception {
|
||||
ResolvableType type = ResolvableTypes.get("listOfExampleObject");
|
||||
AbstractJsonMarshalTester<Object> tester = createTester(type);
|
||||
assertThat(tester.parse(ARRAY_JSON)).asList().containsOnly(OBJECT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseArrayShouldReturnContent() throws Exception {
|
||||
ResolvableType type = ResolvableTypes.get("arrayOfExampleObject");
|
||||
AbstractJsonMarshalTester<Object> tester = createTester(type);
|
||||
assertThat(tester.parse(ARRAY_JSON)).asArray().containsOnly(OBJECT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseMapShouldReturnContent() throws Exception {
|
||||
ResolvableType type = ResolvableTypes.get("mapOfExampleObject");
|
||||
AbstractJsonMarshalTester<Object> tester = createTester(type);
|
||||
assertThat(tester.parse(MAP_JSON)).asMap().containsEntry("a", OBJECT);
|
||||
}
|
||||
|
||||
protected static final ExampleObject createExampleObject(String name, int age) {
|
||||
ExampleObject exampleObject = new ExampleObject();
|
||||
exampleObject.setName(name);
|
||||
exampleObject.setAge(age);
|
||||
return exampleObject;
|
||||
}
|
||||
|
||||
protected final AbstractJsonMarshalTester<Object> createTester(ResolvableType type) {
|
||||
return createTester(AbstractJsonMarshalTesterTests.class, type);
|
||||
}
|
||||
|
||||
protected abstract AbstractJsonMarshalTester<Object> createTester(
|
||||
Class<?> resourceLoadClass, ResolvableType type);
|
||||
|
||||
/**
|
||||
* Access to field backed {@link ResolvableType}.
|
||||
*/
|
||||
public static class ResolvableTypes {
|
||||
|
||||
public List<ExampleObject> listOfExampleObject;
|
||||
|
||||
public ExampleObject[] arrayOfExampleObject;
|
||||
|
||||
public Map<String, ExampleObject> mapOfExampleObject;
|
||||
|
||||
public static ResolvableType get(String name) {
|
||||
Field field = ReflectionUtils.findField(ResolvableTypes.class, name);
|
||||
return ResolvableType.forField(field);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.boot.test.json;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link BasicJsonTester}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class BasicJsonTesterTests {
|
||||
|
||||
private static final String JSON = "{\"spring\":[\"boot\",\"framework\"]}";
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder tempFolder = new TemporaryFolder();
|
||||
|
||||
private BasicJsonTester json = new BasicJsonTester(getClass());
|
||||
|
||||
@Test
|
||||
public void createWhenResourceLoadClassIsNullShouldThrowException() throws Exception {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("ResourceLoadClass must not be null");
|
||||
new BasicJsonTester(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromJsonStringShouldReturnJsonContent() throws Exception {
|
||||
assertThat(this.json.from(JSON)).isEqualToJson("source.json");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromResourceStringShouldReturnJsonContent() throws Exception {
|
||||
assertThat(this.json.from("source.json")).isEqualToJson(JSON);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromResourceStringWithClassShouldReturnJsonContent() throws Exception {
|
||||
assertThat(this.json.from("source.json", getClass())).isEqualToJson(JSON);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromByteArrayShouldReturnJsonContent() throws Exception {
|
||||
assertThat(this.json.from(JSON.getBytes())).isEqualToJson("source.json");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromFileShouldReturnJsonContent() throws Exception {
|
||||
File file = this.tempFolder.newFile("file.json");
|
||||
FileCopyUtils.copy(JSON.getBytes(), file);
|
||||
assertThat(this.json.from(file)).isEqualToJson("source.json");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromInputStreamShouldReturnJsonContent() throws Exception {
|
||||
InputStream inputStream = new ByteArrayInputStream(JSON.getBytes());
|
||||
assertThat(this.json.from(inputStream)).isEqualToJson("source.json");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromResourceShouldReturnJsonContent() throws Exception {
|
||||
Resource resource = new ByteArrayResource(JSON.getBytes());
|
||||
assertThat(this.json.from(resource)).isEqualToJson("source.json");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.boot.test.json;
|
||||
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Example object used for serialization.
|
||||
*/
|
||||
public class ExampleObject {
|
||||
|
||||
private String name;
|
||||
|
||||
private int age;
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return this.age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == null || obj.getClass() != getClass()) {
|
||||
return false;
|
||||
}
|
||||
ExampleObject other = (ExampleObject) obj;
|
||||
return ObjectUtils.nullSafeEquals(this.name, other.name)
|
||||
&& ObjectUtils.nullSafeEquals(this.age, other.age);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.name + " " + this.age;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.boot.test.json;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link GsonTester}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class GsonTesterTests extends AbstractJsonMarshalTesterTests {
|
||||
|
||||
@Test
|
||||
public void initFieldsWhenTestIsNullShouldThrowException() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("TestInstance must not be null");
|
||||
GsonTester.initFields(null, new GsonBuilder().create());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initFieldsWhenObjectMapperIsNullShouldThrowException() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("Marshaller must not be null");
|
||||
GsonTester.initFields(new InitFieldsTestClass(), (Gson) null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initFieldsShouldSetNullFields() {
|
||||
InitFieldsTestClass test = new InitFieldsTestClass();
|
||||
assertThat(test.test).isNull();
|
||||
assertThat(test.base).isNull();
|
||||
GsonTester.initFields(test, new GsonBuilder().create());
|
||||
assertThat(test.test).isNotNull();
|
||||
assertThat(test.base).isNotNull();
|
||||
assertThat(test.test.getType().resolve()).isEqualTo(List.class);
|
||||
assertThat(test.test.getType().resolveGeneric()).isEqualTo(ExampleObject.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractJsonMarshalTester<Object> createTester(Class<?> resourceLoadClass,
|
||||
ResolvableType type) {
|
||||
return new GsonTester<Object>(resourceLoadClass, type,
|
||||
new GsonBuilder().create());
|
||||
}
|
||||
|
||||
static abstract class InitFieldsBaseClass {
|
||||
|
||||
public GsonTester<ExampleObject> base;
|
||||
|
||||
public GsonTester<ExampleObject> baseSet = new GsonTester<ExampleObject>(
|
||||
InitFieldsBaseClass.class, ResolvableType.forClass(ExampleObject.class),
|
||||
new GsonBuilder().create());
|
||||
|
||||
}
|
||||
|
||||
static class InitFieldsTestClass extends InitFieldsBaseClass {
|
||||
|
||||
public GsonTester<List<ExampleObject>> test;
|
||||
|
||||
public GsonTester<ExampleObject> testSet = new GsonTester<ExampleObject>(
|
||||
InitFieldsBaseClass.class, ResolvableType.forClass(ExampleObject.class),
|
||||
new GsonBuilder().create());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.boot.test.json;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link JacksonTester}. Shows typical usage.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class JacksonTesterIntegrationTests {
|
||||
|
||||
private JacksonTester<List<ExampleObject>> listJson;
|
||||
|
||||
private JacksonTester<Map<String, Integer>> mapJson;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
JacksonTester.initFields(this, new ObjectMapper());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typicalListTest() throws Exception {
|
||||
String example = "[{\"name\":\"Spring\",\"age\":123}]";
|
||||
assertThat(this.listJson.parse(example)).asList().hasSize(1);
|
||||
assertThat(this.listJson.parse(example).getObject().get(0).getName())
|
||||
.isEqualTo("Spring");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typicalMapTest() throws Exception {
|
||||
Map<String, Integer> map = new LinkedHashMap<String, Integer>();
|
||||
map.put("a", 1);
|
||||
map.put("b", 2);
|
||||
assertThat(this.mapJson.write(map)).extractingJsonPathNumberValue("@.a")
|
||||
.isEqualTo(1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.boot.test.json;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link JacksonTester}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class JacksonTesterTests extends AbstractJsonMarshalTesterTests {
|
||||
|
||||
@Test
|
||||
public void initFieldsWhenTestIsNullShouldThrowException() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("TestInstance must not be null");
|
||||
JacksonTester.initFields(null, new ObjectMapper());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initFieldsWhenObjectMapperIsNullShouldThrowException() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("Marshaller must not be null");
|
||||
JacksonTester.initFields(new InitFieldsTestClass(), (ObjectMapper) null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initFieldsShouldSetNullFields() {
|
||||
InitFieldsTestClass test = new InitFieldsTestClass();
|
||||
assertThat(test.test).isNull();
|
||||
assertThat(test.base).isNull();
|
||||
JacksonTester.initFields(test, new ObjectMapper());
|
||||
assertThat(test.test).isNotNull();
|
||||
assertThat(test.base).isNotNull();
|
||||
assertThat(test.test.getType().resolve()).isEqualTo(List.class);
|
||||
assertThat(test.test.getType().resolveGeneric()).isEqualTo(ExampleObject.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractJsonMarshalTester<Object> createTester(Class<?> resourceLoadClass,
|
||||
ResolvableType type) {
|
||||
return new JacksonTester<Object>(resourceLoadClass, type, new ObjectMapper());
|
||||
}
|
||||
|
||||
static abstract class InitFieldsBaseClass {
|
||||
|
||||
public JacksonTester<ExampleObject> base;
|
||||
|
||||
public JacksonTester<ExampleObject> baseSet = new JacksonTester<ExampleObject>(
|
||||
InitFieldsBaseClass.class, ResolvableType.forClass(ExampleObject.class),
|
||||
new ObjectMapper());
|
||||
|
||||
}
|
||||
|
||||
static class InitFieldsTestClass extends InitFieldsBaseClass {
|
||||
|
||||
public JacksonTester<List<ExampleObject>> test;
|
||||
|
||||
public JacksonTester<ExampleObject> testSet = new JacksonTester<ExampleObject>(
|
||||
InitFieldsBaseClass.class, ResolvableType.forClass(ExampleObject.class),
|
||||
new ObjectMapper());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.boot.test.json;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link JsonContent}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class JsonContentTests {
|
||||
|
||||
private static final String JSON = "{\"name\":\"spring\", \"age\":100}";
|
||||
|
||||
private static final ResolvableType TYPE = ResolvableType
|
||||
.forClass(ExampleObject.class);
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void createWhenResourceLoadClassIsNullShouldThrowException() throws Exception {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("ResourceLoadClass must not be null");
|
||||
new JsonContent<ExampleObject>(null, TYPE, JSON);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createThenJsonIsNullShouldThrowException() throws Exception {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("JSON must not be null");
|
||||
new JsonContent<ExampleObject>(getClass(), TYPE, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createWhenTypeIsNullShouldCreateContent() throws Exception {
|
||||
JsonContent<ExampleObject> content = new JsonContent<ExampleObject>(getClass(),
|
||||
null, JSON);
|
||||
assertThat(content).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertThatShouldReturnJsonContentAssert() throws Exception {
|
||||
JsonContent<ExampleObject> content = new JsonContent<ExampleObject>(getClass(),
|
||||
TYPE, JSON);
|
||||
assertThat(content.assertThat()).isInstanceOf(JsonContentAssert.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getJsonShouldReturnJson() throws Exception {
|
||||
JsonContent<ExampleObject> content = new JsonContent<ExampleObject>(getClass(),
|
||||
TYPE, JSON);
|
||||
assertThat(content.getJson()).isEqualTo(JSON);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toStringWhenHasTypeShouldReturnString() throws Exception {
|
||||
JsonContent<ExampleObject> content = new JsonContent<ExampleObject>(getClass(),
|
||||
TYPE, JSON);
|
||||
assertThat(content.toString())
|
||||
.isEqualTo("JsonContent " + JSON + " created from " + TYPE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toStringWhenNoTypeShouldReturnString() throws Exception {
|
||||
JsonContent<ExampleObject> content = new JsonContent<ExampleObject>(getClass(),
|
||||
null, JSON);
|
||||
assertThat(content.toString()).isEqualTo("JsonContent " + JSON);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.boot.test.json;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.assertj.core.api.AssertProvider;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link ObjectContentAssert}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class ObjectContentAssertTests {
|
||||
|
||||
private static final ExampleObject SOURCE = new ExampleObject();
|
||||
|
||||
private static final ExampleObject DIFFERENT;
|
||||
|
||||
static {
|
||||
DIFFERENT = new ExampleObject();
|
||||
DIFFERENT.setAge(123);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isEqualToWhenObjectsAreEqualShouldPass() throws Exception {
|
||||
assertThat(forObject(SOURCE)).isEqualTo(SOURCE);
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void isEqualToWhenObjectsAreDifferentShouldFail() throws Exception {
|
||||
assertThat(forObject(SOURCE)).isEqualTo(DIFFERENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asArrayForArrayShouldReturnObjectArrayAssert() throws Exception {
|
||||
ExampleObject[] source = new ExampleObject[] { SOURCE };
|
||||
assertThat(forObject(source)).asArray().containsExactly(SOURCE);
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void asArrayForNonArrayShouldFail() throws Exception {
|
||||
assertThat(forObject(SOURCE)).asArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asMapForMapShouldReturnMapAssert() throws Exception {
|
||||
Map<String, ExampleObject> source = Collections.singletonMap("a", SOURCE);
|
||||
assertThat(forObject(source)).asMap().containsEntry("a", SOURCE);
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void asMapForNonMapShouldFail() throws Exception {
|
||||
assertThat(forObject(SOURCE)).asMap();
|
||||
}
|
||||
|
||||
private AssertProvider<ObjectContentAssert<Object>> forObject(final Object source) {
|
||||
return new AssertProvider<ObjectContentAssert<Object>>() {
|
||||
|
||||
@Override
|
||||
public ObjectContentAssert<Object> assertThat() {
|
||||
return new ObjectContentAssert<Object>(source);
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.boot.test.json;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link ObjectContent}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class ObjectContentTests {
|
||||
|
||||
private static final ExampleObject OBJECT = new ExampleObject();
|
||||
|
||||
private static final ResolvableType TYPE = ResolvableType
|
||||
.forClass(ExampleObject.class);
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void createWhenObjectIsNullShouldThrowException() throws Exception {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("Object must not be null");
|
||||
new ObjectContent<ExampleObject>(TYPE, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createWhenTypeIsNullShouldCreateContent() throws Exception {
|
||||
ObjectContent<ExampleObject> content = new ObjectContent<ExampleObject>(null,
|
||||
OBJECT);
|
||||
assertThat(content).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertThatShouldReturnObjectContentAssert() throws Exception {
|
||||
ObjectContent<ExampleObject> content = new ObjectContent<ExampleObject>(TYPE,
|
||||
OBJECT);
|
||||
assertThat(content.assertThat()).isInstanceOf(ObjectContentAssert.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getObjectShouldReturnObject() throws Exception {
|
||||
ObjectContent<ExampleObject> content = new ObjectContent<ExampleObject>(TYPE,
|
||||
OBJECT);
|
||||
assertThat(content.getObject()).isEqualTo(OBJECT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toStringWhenHasTypeShouldReturnString() throws Exception {
|
||||
ObjectContent<ExampleObject> content = new ObjectContent<ExampleObject>(TYPE,
|
||||
OBJECT);
|
||||
assertThat(content.toString())
|
||||
.isEqualTo("ObjectContent " + OBJECT + " created from " + TYPE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toStringWhenHasNoTypeShouldReturnString() throws Exception {
|
||||
ObjectContent<ExampleObject> content = new ObjectContent<ExampleObject>(null,
|
||||
OBJECT);
|
||||
assertThat(content.toString()).isEqualTo("ObjectContent " + OBJECT);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"gnirps":[
|
||||
"boot",
|
||||
"framework"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"name" : "Spring",
|
||||
"age" : 123
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"spring":[
|
||||
"framework",
|
||||
"boot"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"familyMembers":[
|
||||
{
|
||||
"name":"Homer"
|
||||
},
|
||||
{
|
||||
"name":"Marge"
|
||||
},
|
||||
{
|
||||
"name":"Bart"
|
||||
},
|
||||
{
|
||||
"name":"Lisa"
|
||||
},
|
||||
{
|
||||
"name":"Maggie"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"spring":[
|
||||
"boot",
|
||||
"framework"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"str":"foo",
|
||||
"num":5,
|
||||
"bool":true,
|
||||
"arr":[
|
||||
42
|
||||
],
|
||||
"colorMap":{
|
||||
"red":"rojo"
|
||||
},
|
||||
"whitespace":" ",
|
||||
"emptyString":"",
|
||||
"emptyArray":[
|
||||
|
||||
],
|
||||
"emptyMap":{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user