-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: George Chen <qchea@amazon.com>
- Loading branch information
1 parent
94d1f15
commit db8a48c
Showing
10 changed files
with
169 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
.../org/opensearch/dataprepper/pipeline/parser/DataPrepperDeserializationProblemHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.pipeline.parser; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler; | ||
import com.fasterxml.jackson.databind.deser.ValueInstantiator; | ||
import com.fasterxml.jackson.databind.util.ClassUtil; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
import java.io.IOException; | ||
|
||
@Named | ||
public class DataPrepperDeserializationProblemHandler extends DeserializationProblemHandler { | ||
|
||
@Inject | ||
public DataPrepperDeserializationProblemHandler() { | ||
super(); | ||
} | ||
|
||
@Override | ||
public Object handleWeirdStringValue(DeserializationContext ctxt, Class<?> targetType, String valueToConvert, String failureMsg) throws IOException { | ||
throw new IOException( | ||
String.format("Cannot deserialize value of type %s from String \"%s\": %s", | ||
ClassUtil.nameOf(targetType), valueToConvert, failureMsg)); | ||
} | ||
|
||
@Override | ||
public Object handleUnexpectedToken(DeserializationContext ctxt, JavaType targetType, JsonToken t, JsonParser p, String failureMsg) throws IOException { | ||
throw JsonMappingException.from(ctxt.getParser(), | ||
String.format("Cannot deserialize value of type %s from %s.", | ||
ClassUtil.getTypeDescription(targetType), JsonToken.valueDescFor(t))); | ||
} | ||
|
||
@Override | ||
public Object handleMissingInstantiator(DeserializationContext ctxt, Class<?> instClass, ValueInstantiator valueInsta, JsonParser p, String msg) throws IOException { | ||
throw JsonMappingException.from(ctxt.getParser(), | ||
String.format("Cannot deserialize '%s' into '%s'.", p.getText(), instClass.getName())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
.../opensearch/dataprepper/pipeline/parser/DataPrepperDeserializationProblemHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.pipeline.parser; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.deser.ValueInstantiator; | ||
import com.fasterxml.jackson.databind.util.ClassUtil; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.io.IOException; | ||
import java.util.UUID; | ||
|
||
import static org.hamcrest.CoreMatchers.containsString; | ||
import static org.hamcrest.CoreMatchers.not; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class DataPrepperDeserializationProblemHandlerTest { | ||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | ||
private static final Class<?> TEST_TARGET_TYPE = Boolean.class; | ||
private static final String TEST_VALUE = UUID.randomUUID().toString(); | ||
private static final String TEST_FAILURE_MESSAGE = UUID.randomUUID().toString(); | ||
|
||
@Mock | ||
private DeserializationContext deserializationContext; | ||
|
||
@Mock | ||
private JsonParser jsonParser; | ||
|
||
@Mock | ||
private ValueInstantiator valueInstantiator; | ||
|
||
private final DataPrepperDeserializationProblemHandler objectUnderTest = new DataPrepperDeserializationProblemHandler(); | ||
|
||
@Test | ||
void testHandleWeirdStringValue() { | ||
final IOException exception = assertThrows(IOException.class, () -> objectUnderTest.handleWeirdStringValue( | ||
deserializationContext, TEST_TARGET_TYPE, TEST_VALUE, TEST_FAILURE_MESSAGE)); | ||
assertThat(exception.getMessage(), containsString(ClassUtil.nameOf(TEST_TARGET_TYPE))); | ||
assertThat(exception.getMessage(), containsString(TEST_VALUE)); | ||
assertThat(exception.getMessage(), containsString(TEST_FAILURE_MESSAGE)); | ||
} | ||
|
||
@Test | ||
void testHandleUnexpectedToken() { | ||
when(deserializationContext.getParser()).thenReturn(jsonParser); | ||
final JavaType javaType = OBJECT_MAPPER.constructType(TEST_TARGET_TYPE); | ||
final JsonToken jsonToken = JsonToken.END_OBJECT; | ||
final JsonMappingException exception = assertThrows(JsonMappingException.class, () -> objectUnderTest.handleUnexpectedToken( | ||
deserializationContext, javaType, jsonToken, jsonParser, TEST_FAILURE_MESSAGE)); | ||
assertThat(exception.getMessage(), containsString(ClassUtil.getTypeDescription(javaType))); | ||
assertThat(exception.getMessage(), not(containsString(TEST_FAILURE_MESSAGE))); | ||
} | ||
|
||
@Test | ||
void testHandleMissingInstantiator() throws IOException { | ||
when(deserializationContext.getParser()).thenReturn(jsonParser); | ||
when(jsonParser.getText()).thenReturn(UUID.randomUUID().toString()); | ||
final JsonMappingException exception = assertThrows(JsonMappingException.class, () -> objectUnderTest.handleMissingInstantiator( | ||
deserializationContext, TEST_TARGET_TYPE, valueInstantiator, jsonParser, TEST_FAILURE_MESSAGE)); | ||
assertThat(exception.getMessage(), containsString(jsonParser.getText())); | ||
assertThat(exception.getMessage(), containsString(TEST_TARGET_TYPE.getName())); | ||
assertThat(exception.getMessage(), not(containsString(TEST_FAILURE_MESSAGE))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters