Skip to content

Commit

Permalink
Merge branch 'main' into FMWK-575-align-configuration-prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
agrgr authored Nov 25, 2024
2 parents ab50ca0 + 855134c commit d307f3b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ private DateConverters() {
converters.add(LongToJava8LocalDateConverter.INSTANCE);
converters.add(DurationToStringConverter.INSTANCE);
converters.add(StringToDurationConverter.INSTANCE);
converters.add(InstantToLongConverter.INSTANCE);
converters.add(LongToInstantConverter.INSTANCE);

if (JODA_TIME_IS_PRESENT) {
converters.add(LocalDateToLongConverter.INSTANCE);
Expand Down Expand Up @@ -295,4 +297,24 @@ public Duration convert(String source) {
return Duration.parse(source);
}
}

@WritingConverter
public enum InstantToLongConverter implements Converter<Instant, Long> {
INSTANCE;

@Override
public Long convert(Instant source) {
return source == null ? null : source.toEpochMilli();
}
}

@ReadingConverter
public enum LongToInstantConverter implements Converter<Long, Instant> {
INSTANCE;

@Override
public Instant convert(Long source) {
return source == null ? null : Instant.ofEpochMilli(source);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
import java.net.URL;
import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiConsumer;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.within;
import static org.springframework.data.aerospike.sample.SampleClasses.SimpleClass.SIMPLESET;
import static org.springframework.data.aerospike.sample.SampleClasses.SimpleClassWithPersistenceConstructor.SIMPLESET2;
import static org.springframework.data.aerospike.sample.SampleClasses.User.SIMPLESET3;
Expand Down Expand Up @@ -672,6 +675,23 @@ void objectWithCalendarField(int converterOption) {
new Bin("calendar", DateConverters.CalendarToMapConverter.INSTANCE.convert(calendar)));
}

@ParameterizedTest
@ValueSource(ints = {0, 1})
void objectWithInstantField(int converterOption) {
Instant instant = Instant.now().truncatedTo(ChronoUnit.MILLIS);
DocumentWithInstant object = new DocumentWithInstant(id, instant);

BiConsumer<DocumentWithInstant, DocumentWithInstant> objectAssertFunction = (expected, actual) -> {
assertThat(expected.getId()).isEqualTo(actual.getId());
assertThat(expected.getInstant()).isCloseTo(actual.getInstant(), within(1, ChronoUnit.MILLIS));
};

assertWriteAndRead(converterOption, object,
"DocumentWithInstant", id, objectAssertFunction,
new Bin("@_class", DocumentWithInstant.class.getName()),
new Bin("instant", DateConverters.InstantToLongConverter.INSTANCE.convert(instant)));
}

@ParameterizedTest()
@ValueSource(ints = {0, 1})
void objectWithDurationField(int converterOption) {
Expand Down Expand Up @@ -784,6 +804,17 @@ private <T> void assertWriteAndRead(int converterOption,
String expectedSet,
Object expectedUserKey,
Bin... expectedBins) {
BiConsumer<T,T> equalsAssertFunction = (a, b) -> assertThat(a).isEqualTo(b);
assertWriteAndRead(converterOption, object, expectedSet, expectedUserKey, equalsAssertFunction,
expectedBins);
}

private <T> void assertWriteAndRead(int converterOption,
T object,
String expectedSet,
Object expectedUserKey,
BiConsumer<T,T> objectAssertFunction,
Bin... expectedBins) {
MappingAerospikeConverter aerospikeConverter = getAerospikeMappingConverterByOption(converterOption);
AerospikeWriteData forWrite = AerospikeWriteData.forWrite(NAMESPACE);

Expand All @@ -808,7 +839,7 @@ private <T> void assertWriteAndRead(int converterOption,

@SuppressWarnings("unchecked") T actual = (T) aerospikeConverter.read(object.getClass(), forRead);

assertThat(actual).isEqualTo(object);
objectAssertFunction.accept(actual, object);
}

private boolean compareMaps(AerospikeDataSettings settings, Bin expected, Bin actual) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.math.BigInteger;
import java.net.URL;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -988,6 +989,16 @@ public static class DocumentWithCalendar {
private Calendar calendar;
}

@Data
@AllArgsConstructor
@Document
public static class DocumentWithInstant {
@Id
private String id;
@Field
private Instant instant;
}

@Data
@AllArgsConstructor
@Document
Expand Down

0 comments on commit d307f3b

Please sign in to comment.