Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Java Time module to jackson Object mapper #5129

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

package org.opensearch.dataprepper.model.source.coordinator.enhanced;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.opensearch.dataprepper.model.source.coordinator.SourcePartitionStoreItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -33,7 +35,8 @@
public abstract class EnhancedSourcePartition<T> implements EnhancedPartition<T> {

private static final Logger LOG = LoggerFactory.getLogger(EnhancedSourcePartition.class);
private static final ObjectMapper objectMapper = new ObjectMapper();
private static final ObjectMapper objectMapper = new ObjectMapper(new JsonFactory())
.registerModule(new JavaTimeModule());

private SourcePartitionStoreItem sourcePartitionStoreItem;

Expand All @@ -49,9 +52,8 @@ public void setSourcePartitionStoreItem(SourcePartitionStoreItem sourcePartition
* Helper method to convert progress state.
* This is because the state is currently stored as a String in the coordination store.
*
* @param progressStateClass class of progress state
* @param progressStateClass class of progress state
* @param serializedPartitionProgressState serialized value of the partition progress state
*
* @return returns the converted value of the progress state
*/
public T convertStringToPartitionProgressState(Class<T> progressStateClass, final String serializedPartitionProgressState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.junit.jupiter.api.Test;
import org.opensearch.dataprepper.model.source.coordinator.SourcePartitionStoreItem;

import java.time.Instant;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
Expand All @@ -22,14 +23,17 @@

public class EnhancedSourcePartitionTest {

private final ObjectMapper objectMapper = new ObjectMapper();
private String partitionKey;
private TestPartitionProgressState testPartitionProgressState;
private final ObjectMapper objectMapper = new ObjectMapper();
private TestInstantTypeProgressState testInstantTypeProgressState;

@BeforeEach
void setup() {
partitionKey = UUID.randomUUID().toString();
testPartitionProgressState = new TestPartitionProgressState(UUID.randomUUID().toString());
testInstantTypeProgressState = new TestInstantTypeProgressState(Instant.now());

}

private EnhancedSourcePartition<TestPartitionProgressState> createObjectUnderTest() {
Expand Down Expand Up @@ -79,6 +83,19 @@ void convertFromStringToPartitionState_converts_as_expected() {
assertThat(result.getTestValue(), equalTo(testPartitionProgressState.getTestValue()));
}

@Test
void convertFromInstantToPartitionState_converts_as_expected() {
final EnhancedSourcePartition<TestInstantTypeProgressState> objectUnderTest =
new TestInstantTypeSourcePartition(partitionKey, testInstantTypeProgressState);

final String serializedString = objectUnderTest.convertPartitionProgressStatetoString(Optional.of(testInstantTypeProgressState));

final TestInstantTypeProgressState result =
objectUnderTest.convertStringToPartitionProgressState(TestInstantTypeProgressState.class, serializedString);
assertThat(result, notNullValue());
assertThat(result.getTestValue(), equalTo(testInstantTypeProgressState.getTestValue()));
}

@Test
void convertPartitionStateToStringWithEmptyState_returns_null() {
final String result = createObjectUnderTest().convertPartitionProgressStatetoString(Optional.empty());
Expand Down Expand Up @@ -116,6 +133,31 @@ void convertFromStringToPartitionStateWithPrimitiveType_returns_expected_result(
assertThat(resultWithNullClass, equalTo(stateMap));
}

public static class TestInstantTypeSourcePartition extends EnhancedSourcePartition<TestInstantTypeProgressState> {

private final String partitionKey;
private final TestInstantTypeProgressState testPartitionProgressState;

public TestInstantTypeSourcePartition(final String partitionKey, final TestInstantTypeProgressState partitionProgressState) {
this.partitionKey = partitionKey;
this.testPartitionProgressState = partitionProgressState;
}

@Override
public String getPartitionType() {
return "TEST";
}

@Override
public String getPartitionKey() {
return partitionKey;
}

@Override
public Optional<TestInstantTypeProgressState> getProgressState() {
return Optional.of(testPartitionProgressState);
}
}

public class TestEnhancedSourcePartition extends EnhancedSourcePartition<TestPartitionProgressState> {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.dataprepper.model.source.coordinator.enhanced;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.time.Instant;

public class TestInstantTypeProgressState {

@JsonProperty("testValue")
private Instant testValue;

public TestInstantTypeProgressState(@JsonProperty("testValue") final Instant testValue) {
this.testValue = testValue;
}

public Instant getTestValue() {
return testValue;
}

}
Loading