forked from opensearch-project/data-prepper
-
Notifications
You must be signed in to change notification settings - Fork 0
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: Asif Sohail Mohammed <[email protected]>
- Loading branch information
1 parent
a328c63
commit ec2cb42
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
...r-core/src/test/java/org/opensearch/dataprepper/plugin/ObjectMapperConfigurationTest.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,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); | ||
} | ||
} | ||
|
||
} |