added clock service module
All checks were successful
Build, test and publish the Quarkus libraries / build (push) Successful in 10m19s
All checks were successful
Build, test and publish the Quarkus libraries / build (push) Successful in 10m19s
This commit is contained in:
parent
6e2ce2d489
commit
d4edd00f0f
9 changed files with 161 additions and 26 deletions
|
@ -0,0 +1,18 @@
|
|||
package ch.phoenixtechnologies.quarkus.commons.clock;
|
||||
|
||||
import io.quarkus.arc.DefaultBean;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.inject.Produces;
|
||||
|
||||
import java.time.Clock;
|
||||
|
||||
class ClockProducer {
|
||||
|
||||
@Produces
|
||||
@DefaultBean
|
||||
@ApplicationScoped
|
||||
Clock produceClock() {
|
||||
return Clock.systemDefaultZone();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package ch.phoenixtechnologies.quarkus.commons.clock;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
public interface ClockService {
|
||||
|
||||
Instant instant();
|
||||
|
||||
long currentTimeMillis();
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package ch.phoenixtechnologies.quarkus.commons.clock;
|
||||
|
||||
import io.quarkus.arc.DefaultBean;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
|
||||
@DefaultBean
|
||||
@ApplicationScoped
|
||||
class ClockServiceImpl implements ClockService {
|
||||
|
||||
private final Clock clock;
|
||||
|
||||
ClockServiceImpl(Clock clock) {
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Instant instant() {
|
||||
return clock.instant();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long currentTimeMillis() {
|
||||
return clock.millis();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package ch.phoenixtechnologies.quarkus.commons.clock;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Clock;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@QuarkusTest
|
||||
class ClockProducerTest {
|
||||
|
||||
@Inject
|
||||
Clock clock;
|
||||
|
||||
@Test
|
||||
void produceClock() {
|
||||
assertThat(clock.getZone())
|
||||
.as("Produced clock's zone should match expected value")
|
||||
.isEqualTo(Clock.systemDefaultZone().getZone());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package ch.phoenixtechnologies.quarkus.commons.clock;
|
||||
|
||||
import io.quarkus.test.InjectMock;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.mockito.InjectSpy;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@QuarkusTest
|
||||
class ClockServiceImplTest {
|
||||
|
||||
@Inject
|
||||
ClockService clockService;
|
||||
|
||||
@InjectMock
|
||||
Clock clock;
|
||||
|
||||
@Test
|
||||
void instant() {
|
||||
var expected = Instant.ofEpochMilli(1729280640915L);
|
||||
when(clock.instant()).thenReturn(expected);
|
||||
|
||||
assertThat(clockService.instant())
|
||||
.as("Instant should match expected value")
|
||||
.isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void currentTimeMillis() {
|
||||
var expected = 1729280640915L;
|
||||
when(clock.millis()).thenReturn(expected);
|
||||
|
||||
assertThat(clockService.currentTimeMillis())
|
||||
.as("Instant should match expected value")
|
||||
.isEqualTo(expected);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue