From d3a027a738d65dfcb576884aad895d40f56319ac Mon Sep 17 00:00:00 2001 From: Asif Sohail Mohammed Date: Thu, 5 Oct 2023 22:35:58 +0530 Subject: [PATCH] Remove support for Enum and Duration values from secrets manager (#3433) * Remove support for Enum and Duration values from secrets manager Signed-off-by: Asif Sohail Mohammed * Added unit tests Signed-off-by: Asif Sohail Mohammed --------- Signed-off-by: Asif Sohail Mohammed --- .../plugin/ObjectMapperConfiguration.java | 3 +- .../plugin/ObjectMapperConfigurationTest.java | 81 +++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 data-prepper-core/src/test/java/org/opensearch/dataprepper/plugin/ObjectMapperConfigurationTest.java diff --git a/data-prepper-core/src/main/java/org/opensearch/dataprepper/plugin/ObjectMapperConfiguration.java b/data-prepper-core/src/main/java/org/opensearch/dataprepper/plugin/ObjectMapperConfiguration.java index 0c5494591d..a593950fea 100644 --- a/data-prepper-core/src/main/java/org/opensearch/dataprepper/plugin/ObjectMapperConfiguration.java +++ b/data-prepper-core/src/main/java/org/opensearch/dataprepper/plugin/ObjectMapperConfiguration.java @@ -17,7 +17,7 @@ public class ObjectMapperConfiguration { static final Set TRANSLATE_VALUE_SUPPORTED_JAVA_TYPES = Set.of( String.class, Number.class, Long.class, Short.class, Integer.class, Double.class, Float.class, - Boolean.class, Duration.class, Enum.class, Character.class); + Boolean.class, Character.class); @Bean(name = "extensionPluginConfigObjectMapper") ObjectMapper extensionPluginConfigObjectMapper() { @@ -32,6 +32,7 @@ ObjectMapper extensionPluginConfigObjectMapper() { @Bean(name = "pluginConfigObjectMapper") ObjectMapper pluginConfigObjectMapper(final VariableExpander variableExpander) { final SimpleModule simpleModule = new SimpleModule(); + simpleModule.addDeserializer(Duration.class, new DataPrepperDurationDeserializer()); TRANSLATE_VALUE_SUPPORTED_JAVA_TYPES.stream().forEach(clazz -> simpleModule.addDeserializer( clazz, new DataPrepperScalarTypeDeserializer<>(variableExpander, clazz))); diff --git a/data-prepper-core/src/test/java/org/opensearch/dataprepper/plugin/ObjectMapperConfigurationTest.java b/data-prepper-core/src/test/java/org/opensearch/dataprepper/plugin/ObjectMapperConfigurationTest.java new file mode 100644 index 0000000000..8051166a5c --- /dev/null +++ b/data-prepper-core/src/test/java/org/opensearch/dataprepper/plugin/ObjectMapperConfigurationTest.java @@ -0,0 +1,81 @@ +package org.opensearch.dataprepper.plugin; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.databind.ObjectMapper; +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.time.Duration; +import java.util.Arrays; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +@ExtendWith(MockitoExtension.class) +class ObjectMapperConfigurationTest { + private final ObjectMapperConfiguration objectMapperConfiguration = new ObjectMapperConfiguration(); + + @Mock + private VariableExpander variableExpander; + + @Test + void test_duration_with_pluginConfigObjectMapper() { + final String durationTestString = "10s"; + final ObjectMapper objectMapper = objectMapperConfiguration.pluginConfigObjectMapper(variableExpander); + final Duration duration = objectMapper.convertValue(durationTestString, Duration.class); + assertThat(duration, equalTo(Duration.ofSeconds(10))); + } + + @Test + void test_enum_with_pluginConfigObjectMapper() { + final String testString = "test"; + final ObjectMapper objectMapper = objectMapperConfiguration.pluginConfigObjectMapper(variableExpander); + final TestType duration = objectMapper.convertValue(testString, TestType.class); + assertThat(duration, equalTo(TestType.fromOptionValue(testString))); + } + + @Test + void test_duration_with_extensionPluginConfigObjectMapper() { + final String durationTestString = "10s"; + final ObjectMapper objectMapper = objectMapperConfiguration.extensionPluginConfigObjectMapper(); + final Duration duration = objectMapper.convertValue(durationTestString, Duration.class); + assertThat(duration, equalTo(Duration.ofSeconds(10))); + } + + @Test + void test_enum_with_extensionPluginConfigObjectMapper() { + final String testString = "test"; + final ObjectMapper objectMapper = objectMapperConfiguration.extensionPluginConfigObjectMapper(); + final TestType duration = objectMapper.convertValue(testString, TestType.class); + assertThat(duration, equalTo(TestType.fromOptionValue(testString))); + } + + private enum TestType { + + TEST("test"); + + private static final Map NAMES_MAP = Arrays.stream(TestType.values()) + .collect(Collectors.toMap(TestType::toString, Function.identity())); + + private final String name; + + TestType(final String name) { + this.name = name; + } + + public String toString() { + return this.name; + } + + @JsonCreator + static TestType fromOptionValue(final String option) { + return NAMES_MAP.get(option); + } + } + +} \ No newline at end of file