Skip to content
Snippets Groups Projects

NXCALS-7673 porting PyTimber snapshot api to native metadata custom api

Files
3
@@ -14,12 +14,12 @@ import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.TimeZone;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -49,16 +49,23 @@ class SnapshotTest {
private static final int FILL_NUMBER = 8775;
static Stream<Arguments> dataForDynamicTimeWindowTest() {
return Stream.of(
Arguments.of(LoggingTimeZone.UTC, DynamicTimeUnit.SECONDS, 2, ChronoUnit.SECONDS),
Arguments.of(LoggingTimeZone.LOCAL, DynamicTimeUnit.MINUTES, 3, ChronoUnit.MINUTES),
Arguments.of(LoggingTimeZone.UTC, DynamicTimeUnit.HOURS, 5, ChronoUnit.HOURS),
Arguments.of(LoggingTimeZone.LOCAL, DynamicTimeUnit.DAYS, 7, ChronoUnit.DAYS),
Arguments.of(LoggingTimeZone.UTC, DynamicTimeUnit.HOURS, 5, ChronoUnit.HOURS)
Arguments.of(LoggingTimeZone.UTC, DynamicTimeUnit.WEEKS, 8, ChronoUnit.WEEKS),
Arguments.of(LoggingTimeZone.LOCAL, DynamicTimeUnit.MONTHS, 9, ChronoUnit.MONTHS),
Arguments.of(LoggingTimeZone.UTC, DynamicTimeUnit.YEARS, 1, ChronoUnit.YEARS)
);
}
@ParameterizedTest
@MethodSource("dataForDynamicTimeWindowTest")
void shouldCalculateDynamicTimeWindow(LoggingTimeZone loggingTimeZone, DynamicTimeUnit dynamicTimeUnit,
Integer dynamicDuration, ChronoUnit resultUnit) {
Integer dynamicDuration, ChronoUnit resultUnit) {
withMockedLocalDateTimeNow(loggingTimeZone.getTimeZone().toZoneId(), () -> {
Snapshot.TimeDefinition timeDefinition = Snapshot.TimeDefinition.dynamic(
dynamicDuration, loggingTimeZone, PriorTime.from("Start of day"), dynamicTimeUnit
@@ -76,10 +83,14 @@ class SnapshotTest {
Instant startOfDay = LocalDateTime.now(loggingTimeZone.getTimeZone().toZoneId())
.truncatedTo(ChronoUnit.DAYS).atZone(loggingTimeZone.getTimeZone().toZoneId()).toInstant();
assertEquals(startOfDay, timeWindow.getEndTime());
assertEquals(Duration.between(timeWindow.getStartTime(), timeWindow.getEndTime()),
Duration.of(dynamicDuration, resultUnit));
ZoneId zoneId = ZoneId.of("UTC");
if (loggingTimeZone == LoggingTimeZone.LOCAL) {
zoneId = TimeZone.getDefault().toZoneId();
}
assertEquals(timeWindow.getStartTime().atZone(zoneId),
timeWindow.getEndTime().atZone(zoneId).minus(dynamicDuration, resultUnit));
});
}
@@ -92,7 +103,7 @@ class SnapshotTest {
private void withMockedLocalDateTimeNow(ZoneId zoneId, Runnable runnable) {
LocalDateTime now = LocalDateTime.now();
ZonedDateTime zoned = now.atZone(zoneId);
try (MockedStatic<LocalDateTime> localDateTimeMockedStatic = Mockito.mockStatic(LocalDateTime.class)) {
try (MockedStatic<LocalDateTime> localDateTimeMockedStatic = Mockito.mockStatic(LocalDateTime.class, Mockito.CALLS_REAL_METHODS)) {
localDateTimeMockedStatic.when(LocalDateTime::now).thenReturn(now);
localDateTimeMockedStatic.when(() -> LocalDateTime.now(zoneId)).thenReturn(zoned.toLocalDateTime());
runnable.run();
Loading