feat(clock): add local and zoned date methods to service
All checks were successful
Build / build (pull_request) Successful in 2m59s
All checks were successful
Build / build (pull_request) Successful in 2m59s
This commit is contained in:
parent
bbd1d80d6c
commit
6ac4bd783e
3 changed files with 56 additions and 4 deletions
|
@ -1,10 +1,19 @@
|
||||||
package ch.phoenix.oss.quarkus.commons.clock;
|
package ch.phoenix.oss.quarkus.commons.clock;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
|
|
||||||
public interface ClockService {
|
public interface ClockService {
|
||||||
|
|
||||||
Instant instant();
|
Instant instant();
|
||||||
|
|
||||||
long currentTimeMillis();
|
long currentTimeMillis();
|
||||||
|
|
||||||
|
LocalDate localDate();
|
||||||
|
|
||||||
|
LocalDateTime localDateTime();
|
||||||
|
|
||||||
|
ZonedDateTime zonedDateTime();
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,7 @@ package ch.phoenix.oss.quarkus.commons.clock;
|
||||||
|
|
||||||
import io.quarkus.arc.DefaultBean;
|
import io.quarkus.arc.DefaultBean;
|
||||||
import jakarta.enterprise.context.ApplicationScoped;
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
import java.time.Clock;
|
import java.time.*;
|
||||||
import java.time.Instant;
|
|
||||||
|
|
||||||
@DefaultBean
|
@DefaultBean
|
||||||
@ApplicationScoped
|
@ApplicationScoped
|
||||||
|
@ -24,4 +23,19 @@ class ClockServiceImpl implements ClockService {
|
||||||
public long currentTimeMillis() {
|
public long currentTimeMillis() {
|
||||||
return clock.millis();
|
return clock.millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public LocalDate localDate() {
|
||||||
|
return LocalDate.now(clock);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public LocalDateTime localDateTime() {
|
||||||
|
return LocalDateTime.now(clock);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ZonedDateTime zonedDateTime() {
|
||||||
|
return ZonedDateTime.now(clock);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,7 @@ import static org.mockito.Mockito.when;
|
||||||
import io.quarkus.test.InjectMock;
|
import io.quarkus.test.InjectMock;
|
||||||
import io.quarkus.test.junit.QuarkusTest;
|
import io.quarkus.test.junit.QuarkusTest;
|
||||||
import jakarta.inject.Inject;
|
import jakarta.inject.Inject;
|
||||||
import java.time.Clock;
|
import java.time.*;
|
||||||
import java.time.Instant;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
@QuarkusTest
|
@QuarkusTest
|
||||||
|
@ -38,4 +37,34 @@ class ClockServiceImplTest {
|
||||||
.as("Instant should match expected value")
|
.as("Instant should match expected value")
|
||||||
.isEqualTo(expected);
|
.isEqualTo(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void localDate() {
|
||||||
|
var instant = Instant.ofEpochMilli(1729280640915L);
|
||||||
|
when(clock.instant()).thenReturn(instant);
|
||||||
|
when(clock.getZone()).thenReturn(ZoneId.of("UTC"));
|
||||||
|
assertThat(clockService.localDate())
|
||||||
|
.as("LocalDate should match expected value")
|
||||||
|
.isEqualTo(LocalDate.parse("2024-10-18"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void localDateTime() {
|
||||||
|
var instant = Instant.ofEpochMilli(1729280640915L);
|
||||||
|
when(clock.instant()).thenReturn(instant);
|
||||||
|
when(clock.getZone()).thenReturn(ZoneId.of("UTC"));
|
||||||
|
assertThat(clockService.localDateTime())
|
||||||
|
.as("LocalDateTime should match expected value")
|
||||||
|
.isEqualTo(LocalDateTime.parse("2024-10-18T19:44:00.915"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void zonedDateTime() {
|
||||||
|
var instant = Instant.ofEpochMilli(1729280640915L);
|
||||||
|
when(clock.instant()).thenReturn(instant);
|
||||||
|
when(clock.getZone()).thenReturn(ZoneId.of("UTC"));
|
||||||
|
assertThat(clockService.zonedDateTime())
|
||||||
|
.as("ZonedDateTime should match expected value")
|
||||||
|
.isEqualTo(ZonedDateTime.parse("2024-10-18T19:44:00.915Z[UTC]"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue