Use Jackson configuration with JsonPath

Update `JacksonTester` so that the JsonPath instance is explicitly configured
with both a `JacksonJsonProvider` and a `JacksonMappingProvider`.

Prior to this commit, the handling of special characters was not symmetrical
between the serialization (handled via the JacksonTester) and the parsing (handled
via JsonPath) due to the fact that JsonPath used `SimpleJson` as its parser.

See gh-16629
This commit is contained in:
Diego Berrueta
2019-04-23 07:55:16 +10:00
committed by Phillip Webb
parent 4fc813b246
commit 756a7f12a3
10 changed files with 276 additions and 17 deletions

View File

@@ -0,0 +1,87 @@
/*
* Copyright 2012-2019 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
*
* https://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.google.gson.Gson;
import org.junit.Before;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for {@link GsonTester}. Shows typical usage.
*
* @author Andy Wilkinson
* @author Diego Berrueta
*/
public class GsonTesterIntegrationTests {
private GsonTester<ExampleObject> simpleJson;
private GsonTester<List<ExampleObject>> listJson;
private GsonTester<Map<String, Integer>> mapJson;
private GsonTester<String> stringJson;
private Gson gson;
private static final String JSON = "{\"name\":\"Spring\",\"age\":123}";
@Before
public void setup() {
this.gson = new Gson();
GsonTester.initFields(this, this.gson);
}
@Test
public void typicalTest() throws Exception {
String example = JSON;
assertThat(this.simpleJson.parse(example).getObject().getName())
.isEqualTo("Spring");
}
@Test
public void typicalListTest() throws Exception {
String example = "[" + JSON + "]";
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<>();
map.put("a", 1);
map.put("b", 2);
assertThat(this.mapJson.write(map)).extractingJsonPathNumberValue("@.a")
.isEqualTo(1);
}
@Test
public void stringLiteral() throws Exception {
String stringWithSpecialCharacters = "myString";
assertThat(this.stringJson.write(stringWithSpecialCharacters))
.extractingJsonPathStringValue("@")
.isEqualTo(stringWithSpecialCharacters);
}
}

View File

@@ -47,6 +47,8 @@ public class JacksonTesterIntegrationTests {
private JacksonTester<Map<String, Integer>> mapJson;
private JacksonTester<String> stringJson;
private ObjectMapper objectMapper;
private static final String JSON = "{\"name\":\"Spring\",\"age\":123}";
@@ -81,6 +83,28 @@ public class JacksonTesterIntegrationTests {
.isEqualTo(1);
}
@Test
public void stringLiteral() throws Exception {
String stringWithSpecialCharacters = "myString";
assertThat(this.stringJson.write(stringWithSpecialCharacters))
.extractingJsonPathStringValue("@")
.isEqualTo(stringWithSpecialCharacters);
}
// This test confirms that the handling of special characters is symmetrical between
// the serialisation (via the JacksonTester) and the parsing (via json-path). By
// default json-path uses SimpleJson as its parser, which has a slightly different
// behaviour to Jackson and breaks the symmetry. However JacksonTester
// configures json-path to use Jackson for evaluating the path expressions and
// restores the symmetry.
@Test
public void parseSpecialCharactersTest() throws Exception {
String stringWithSpecialCharacters = "\u0006\u007F";
assertThat(this.stringJson.write(stringWithSpecialCharacters))
.extractingJsonPathStringValue("@")
.isEqualTo(stringWithSpecialCharacters);
}
@Test
public void writeWithView() throws Exception {
this.objectMapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);

View File

@@ -16,6 +16,7 @@
package org.springframework.boot.test.json;
import com.jayway.jsonpath.Configuration;
import org.junit.Test;
import org.springframework.core.ResolvableType;
@@ -38,47 +39,61 @@ public class JsonContentTests {
@Test
public void createWhenResourceLoadClassIsNullShouldThrowException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new JsonContent<ExampleObject>(null, TYPE, JSON))
.isThrownBy(() -> new JsonContent<ExampleObject>(null, TYPE, JSON,
Configuration.defaultConfiguration()))
.withMessageContaining("ResourceLoadClass must not be null");
}
@Test
public void createWhenJsonIsNullShouldThrowException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new JsonContent<ExampleObject>(getClass(), TYPE, null))
.isThrownBy(() -> new JsonContent<ExampleObject>(getClass(), TYPE, null,
Configuration.defaultConfiguration()))
.withMessageContaining("JSON must not be null");
}
@Test
public void createWhenConfigurationIsNullShouldThrowException() {
assertThatIllegalArgumentException().isThrownBy(
() -> new JsonContent<ExampleObject>(getClass(), TYPE, JSON, null))
.withMessageContaining("Configuration must not be null");
}
@Test
public void createWhenTypeIsNullShouldCreateContent() {
JsonContent<ExampleObject> content = new JsonContent<>(getClass(), null, JSON);
JsonContent<ExampleObject> content = new JsonContent<>(getClass(), null, JSON,
Configuration.defaultConfiguration());
assertThat(content).isNotNull();
}
@Test
@SuppressWarnings("deprecation")
public void assertThatShouldReturnJsonContentAssert() {
JsonContent<ExampleObject> content = new JsonContent<>(getClass(), TYPE, JSON);
JsonContent<ExampleObject> content = new JsonContent<>(getClass(), TYPE, JSON,
Configuration.defaultConfiguration());
assertThat(content.assertThat()).isInstanceOf(JsonContentAssert.class);
}
@Test
public void getJsonShouldReturnJson() {
JsonContent<ExampleObject> content = new JsonContent<>(getClass(), TYPE, JSON);
JsonContent<ExampleObject> content = new JsonContent<>(getClass(), TYPE, JSON,
Configuration.defaultConfiguration());
assertThat(content.getJson()).isEqualTo(JSON);
}
@Test
public void toStringWhenHasTypeShouldReturnString() {
JsonContent<ExampleObject> content = new JsonContent<>(getClass(), TYPE, JSON);
JsonContent<ExampleObject> content = new JsonContent<>(getClass(), TYPE, JSON,
Configuration.defaultConfiguration());
assertThat(content.toString())
.isEqualTo("JsonContent " + JSON + " created from " + TYPE);
}
@Test
public void toStringWhenHasNoTypeShouldReturnString() {
JsonContent<ExampleObject> content = new JsonContent<>(getClass(), null, JSON);
JsonContent<ExampleObject> content = new JsonContent<>(getClass(), null, JSON,
Configuration.defaultConfiguration());
assertThat(content.toString()).isEqualTo("JsonContent " + JSON);
}