Skip to content

Commit

Permalink
Remove support for Enum and Duration values from secrets manager (ope…
Browse files Browse the repository at this point in the history
…nsearch-project#3433)

* Remove support for Enum and Duration values from secrets manager

Signed-off-by: Asif Sohail Mohammed <[email protected]>

* Added unit tests

Signed-off-by: Asif Sohail Mohammed <[email protected]>

---------

Signed-off-by: Asif Sohail Mohammed <[email protected]>
  • Loading branch information
asifsmohammed authored Oct 5, 2023
1 parent db32da5 commit d3a027a
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class ObjectMapperConfiguration {
static final Set<Class> 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() {
Expand All @@ -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)));

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, TestType> 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);
}
}

}

0 comments on commit d3a027a

Please sign in to comment.