Allow JsonPathExpectationsHelper to use a custom configuration

This commit improves JsonPathExpectationsHelper to allow a custom json
path configuration to be defined. The primary objective of a custom
configuration is to specify a custom mapper that can deserialize complex
object structure.

As part of this commit, it is now possible to invoke a Matcher against
a type that holds generic information, using a regular
ParameterizedTypeReference.

Given that the existing constructor takes a vararg of Object, this
commit also deprecates this constructor in favor of formatting the
expression String upfront.

Closes gh-31651
This commit is contained in:
Stéphane Nicoll
2024-01-15 15:22:46 +01:00
parent 4f8eca1350
commit 9f8038963f
5 changed files with 172 additions and 24 deletions

View File

@@ -16,11 +16,22 @@
package org.springframework.test.util;
import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.core.ParameterizedTypeReference;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.core.Is.is;
/**
@@ -28,10 +39,14 @@ import static org.hamcrest.core.Is.is;
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @author Stephane Nicoll
* @since 3.2
*/
class JsonPathExpectationsHelperTests {
private static final Configuration JACKSON_MAPPING_CONFIGURATION = Configuration.defaultConfiguration()
.mappingProvider(new JacksonMappingProvider(new ObjectMapper()));
private static final String CONTENT = """
{
'str': 'foo',
@@ -324,4 +339,41 @@ class JsonPathExpectationsHelperTests {
.withMessageContaining("Expected a map at JSON path \"" + expression + "\" but found: 'foo'");
}
@Test
void assertValueWithComplexTypeFallbacksOnValueType() {
new JsonPathExpectationsHelper("$.familyMembers[0]", JACKSON_MAPPING_CONFIGURATION)
.assertValue(SIMPSONS, new Member("Homer"));
}
@Test
void assertValueWithComplexTypeAndMatcher() {
new JsonPathExpectationsHelper("$.familyMembers[0]", JACKSON_MAPPING_CONFIGURATION)
.assertValue(SIMPSONS, CoreMatchers.instanceOf(Member.class), Member.class);
}
@Test
void assertValueWithComplexGenericTypeAndMatcher() {
JsonPathExpectationsHelper helper = new JsonPathExpectationsHelper("$.familyMembers", JACKSON_MAPPING_CONFIGURATION);
helper.assertValue(SIMPSONS, hasSize(5), new ParameterizedTypeReference<List<Member>>() {});
helper.assertValue(SIMPSONS, hasItem(new Member("Lisa")), new ParameterizedTypeReference<List<Member>>() {});
}
@Test
void evaluateJsonPathWithClassType() {
Member firstMember = new JsonPathExpectationsHelper("$.familyMembers[0]", JACKSON_MAPPING_CONFIGURATION)
.evaluateJsonPath(SIMPSONS, Member.class);
assertThat(firstMember).isEqualTo(new Member("Homer"));
}
@Test
void evaluateJsonPathWithGenericType() {
List<Member> family = new JsonPathExpectationsHelper("$.familyMembers", JACKSON_MAPPING_CONFIGURATION)
.evaluateJsonPath(SIMPSONS, new ParameterizedTypeReference<List<Member>>() {});
assertThat(family).containsExactly(new Member("Homer"), new Member("Marge"),
new Member("Bart"), new Member("Lisa"), new Member("Maggie"));
}
public record Member(String name) {}
}