|
16 | 16 |
|
17 | 17 | package org.springframework.ai.converter; |
18 | 18 |
|
19 | | -import java.time.LocalDate; |
20 | | -import java.util.ArrayList; |
21 | | -import java.util.List; |
22 | | - |
| 19 | +import ch.qos.logback.classic.Logger; |
| 20 | +import ch.qos.logback.classic.spi.ILoggingEvent; |
| 21 | +import ch.qos.logback.core.read.ListAppender; |
23 | 22 | import com.fasterxml.jackson.annotation.JsonProperty; |
24 | 23 | import com.fasterxml.jackson.annotation.JsonPropertyDescription; |
25 | 24 | import com.fasterxml.jackson.annotation.JsonPropertyOrder; |
| 25 | +import com.fasterxml.jackson.core.JsonParseException; |
26 | 26 | import com.fasterxml.jackson.databind.DeserializationFeature; |
27 | 27 | import com.fasterxml.jackson.databind.JsonNode; |
28 | 28 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 29 | +import org.junit.jupiter.api.BeforeEach; |
29 | 30 | import org.junit.jupiter.api.Nested; |
30 | 31 | import org.junit.jupiter.api.Test; |
31 | 32 | import org.junit.jupiter.api.extension.ExtendWith; |
32 | 33 | import org.mockito.Mock; |
33 | 34 | import org.mockito.junit.jupiter.MockitoExtension; |
34 | | - |
| 35 | +import org.slf4j.LoggerFactory; |
35 | 36 | import org.springframework.core.ParameterizedTypeReference; |
36 | 37 |
|
| 38 | +import java.time.LocalDate; |
| 39 | +import java.util.ArrayList; |
| 40 | +import java.util.List; |
| 41 | + |
37 | 42 | import static org.assertj.core.api.Assertions.assertThat; |
| 43 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 44 | +import static org.springframework.ai.util.LoggingMarkers.PII_MARKER; |
38 | 45 |
|
39 | 46 | /** |
40 | 47 | * @author Sebastian Ullrich |
|
45 | 52 | @ExtendWith(MockitoExtension.class) |
46 | 53 | class BeanOutputConverterTest { |
47 | 54 |
|
| 55 | + private ListAppender<ILoggingEvent> logAppender; |
| 56 | + |
48 | 57 | @Mock |
49 | 58 | private ObjectMapper objectMapperMock; |
50 | 59 |
|
| 60 | + @BeforeEach |
| 61 | + void beforeEach() { |
| 62 | + |
| 63 | + var logger = (Logger) LoggerFactory.getLogger(BeanOutputConverter.class); |
| 64 | + |
| 65 | + logAppender = new ListAppender<>(); |
| 66 | + logAppender.start(); |
| 67 | + logger.addAppender(logAppender); |
| 68 | + } |
| 69 | + |
51 | 70 | @Test |
52 | 71 | void shouldHavePreConfiguredDefaultObjectMapper() { |
53 | 72 | var converter = new BeanOutputConverter<>(new ParameterizedTypeReference<TestClass>() { |
@@ -135,6 +154,16 @@ void convertClassType() { |
135 | 154 | assertThat(testClass.getSomeString()).isEqualTo("some value"); |
136 | 155 | } |
137 | 156 |
|
| 157 | + @Test |
| 158 | + void failToConvertInvalidJson() { |
| 159 | + var converter = new BeanOutputConverter<>(TestClass.class); |
| 160 | + assertThatThrownBy(() -> converter.convert("{invalid json")).hasCauseInstanceOf(JsonParseException.class); |
| 161 | + final var loggingEvent = logAppender.list.getFirst(); |
| 162 | + assertThat(loggingEvent.getMessage()).isEqualTo( |
| 163 | + "Could not parse the given text to the desired target type:{invalid json into " + TestClass.class); |
| 164 | + assertThat(loggingEvent.getMarkerList()).contains(PII_MARKER); |
| 165 | + } |
| 166 | + |
138 | 167 | @Test |
139 | 168 | void convertClassWithDateType() { |
140 | 169 | var converter = new BeanOutputConverter<>(TestClassWithDateProperty.class); |
|
0 commit comments