Skip to content

Commit

Permalink
Introduce TimeProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Pavlovskiy committed Aug 21, 2023
1 parent ca43041 commit 376c7c3
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.nikitades.carres.domain.Notifier;
import com.nikitades.carres.domain.Reservation;
import com.nikitades.carres.domain.ReservationRepository;
import com.nikitades.carres.domain.TimeProvider;
import com.nikitades.carres.domain.exception.BadReservationDurationException;
import com.nikitades.carres.domain.exception.CannotReserveVehicleForTooSoonException;
import com.nikitades.carres.domain.exception.ReservationOverlapsWithAnotherOneException;
Expand All @@ -26,6 +27,7 @@ public class ReservationModule {
private final ReservationRepository reservationRepository;
private final CarRepository carRepository;
private final UuidProvider uuidProvider;
private final TimeProvider timeProvider;
private final AnalyticsReporter analyticsReporter;
private final Notifier notifier;

Expand Down Expand Up @@ -61,7 +63,8 @@ public Reservation createReservation(
car.get(),
startTime,
durationMinutes,
startTime
startTime,
timeProvider.utcNow()
);
} catch (BadReservationDurationException e) {
throw new BadRequestException(e.getMessage(), "BAD_RESERVATION_DURATION");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public static Reservation createWithNoOverlapping(
Car car,
Instant startsAt,
int durationMinutes,
Instant createdAt
Instant createdAt,
Instant now
)
throws BadReservationDurationException, ReservationOverlapsWithAnotherOneException, CannotReserveVehicleForTooSoonException {
Reservation newReservation = new Reservation(
Expand All @@ -63,7 +64,7 @@ public static Reservation createWithNoOverlapping(
createdAt
);

if (startsAt.isBefore(Instant.now().plus(Duration.ofHours(1)))) {
if (startsAt.isBefore(now.plus(Duration.ofHours(1)))) {
throw new CannotReserveVehicleForTooSoonException(
"New reservation must be at least in 1 hour from now"
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.nikitades.carres.domain;

import java.time.Instant;

public interface TimeProvider {
public Instant utcNow();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.nikitades.carres.infrastructure.java;

import com.nikitades.carres.domain.TimeProvider;
import java.time.Instant;
import org.springframework.stereotype.Component;

@Component
public class JavaTimeProvider implements TimeProvider {

public Instant utcNow() {
return Instant.now();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
import com.nikitades.carres.domain.Notifier;
import com.nikitades.carres.domain.Reservation;
import com.nikitades.carres.domain.ReservationRepository;
import com.nikitades.carres.domain.TimeProvider;
import com.nikitades.carres.infrastructure.java.JavaTimeProvider;
import com.nikitades.carres.infrastructure.java.JavaUuidProvider;
import com.nikitades.carres.infrastructure.prometheus.AnalyticsReporter;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
Expand Down Expand Up @@ -48,6 +52,8 @@ class ReservationModuleTest {

UuidProvider uuidProvider = new JavaUuidProvider();

TimeProvider timeProvider = new JavaTimeProvider();

ReservationModule instance;

@BeforeEach
Expand All @@ -57,6 +63,7 @@ void init() {
reservationRepository,
carRepository,
uuidProvider,
timeProvider,
analyticsReporter,
notifier
);
Expand All @@ -75,7 +82,7 @@ void whenMissingCarIdIsPassed_thenNotFoundExceptionIsThrown() {
UUID.randomUUID(),
"[email protected]",
UUID.randomUUID(),
Instant.now(),
timeProvider.utcNow(),
30
)
);
Expand All @@ -95,7 +102,7 @@ void whenTooShortReservationDurationIsPassed_thenBadRequestExceptionIsThrown() {
UUID.randomUUID(),
"[email protected]",
UUID.randomUUID(),
Instant.now(),
timeProvider.utcNow(),
10
)
);
Expand All @@ -115,7 +122,7 @@ void whenReservationIsMadeSoonerThanACertainTimeFromNow_thenBadRequestExceptionI
UUID.randomUUID(),
"[email protected]",
UUID.randomUUID(),
Instant.now().plus(Duration.ofMinutes(5)),
timeProvider.utcNow().plus(Duration.ofMinutes(5)),
30
)
);
Expand All @@ -132,21 +139,23 @@ void whenOtherReservationsOverlapWithNewlyCreatedOne_thenBadRequestExceptionIsTh
.of(Reservation.class)
.set(
field(Reservation::getStartsAt),
LocalDateTime
.now()
timeProvider
.utcNow()
.atZone(ZoneId.of("UTC"))
.plus(Duration.ofDays(1))
.withHour(15)
.withMinute(0)
.toInstant(ZoneOffset.UTC)
.toInstant()
)
.set(
field(Reservation::getEndsAt),
LocalDateTime
.now()
timeProvider
.utcNow()
.atZone(ZoneId.of("UTC"))
.plus(Duration.ofDays(1))
.withHour(15)
.withMinute(30)
.toInstant(ZoneOffset.UTC)
.toInstant()
)
.create();
when(reservationRepository.findByCarId(any(UUID.class))).thenReturn(List.of(reservation1));
Expand All @@ -159,12 +168,13 @@ void whenOtherReservationsOverlapWithNewlyCreatedOne_thenBadRequestExceptionIsTh
UUID.randomUUID(),
"[email protected]",
UUID.randomUUID(),
LocalDateTime
.now()
timeProvider
.utcNow()
.atZone(ZoneId.of("UTC"))
.plus(Duration.ofDays(1))
.withHour(15)
.withMinute(15)
.toInstant(ZoneOffset.UTC),
.toInstant(),
15
);
}
Expand All @@ -182,12 +192,13 @@ void whenReservationSuccessfullyCreated_thenAnalyticsCounterIncremented() {
UUID.randomUUID(),
"[email protected]",
UUID.randomUUID(),
LocalDateTime
.now()
timeProvider
.utcNow()
.atZone(ZoneId.of("UTC"))
.plus(Duration.ofDays(1))
.withHour(15)
.withMinute(15)
.toInstant(ZoneOffset.UTC),
.toInstant(),
15
);

Expand Down
Loading

0 comments on commit 376c7c3

Please sign in to comment.