From 01f23c71ca5df4eb35600b19a3db551bf5f0792b Mon Sep 17 00:00:00 2001 From: Maximilian Behrendt Date: Thu, 5 Dec 2024 13:47:49 +0100 Subject: [PATCH] use UTC to in optimal time comparison --- .../domain/model/emissionframe/OptimalTime.java | 5 +++-- .../model/emissionframe/OptimalTimeTest.java | 16 ++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/carbon-reductor-core/src/main/java/de/envite/greenbpm/carbonreductor/core/domain/model/emissionframe/OptimalTime.java b/carbon-reductor-core/src/main/java/de/envite/greenbpm/carbonreductor/core/domain/model/emissionframe/OptimalTime.java index d42662e..a676592 100644 --- a/carbon-reductor-core/src/main/java/de/envite/greenbpm/carbonreductor/core/domain/model/emissionframe/OptimalTime.java +++ b/carbon-reductor-core/src/main/java/de/envite/greenbpm/carbonreductor/core/domain/model/emissionframe/OptimalTime.java @@ -4,6 +4,7 @@ import io.github.domainprimitives.validation.Validation; import java.time.OffsetDateTime; +import java.time.ZoneOffset; import java.time.temporal.Temporal; import java.util.function.Consumer; @@ -11,7 +12,7 @@ public class OptimalTime extends ValueObject { private static Consumer> isNotNull() { - return (val) -> val.constraint(val.value() != null, () -> "should not be null"); + return val -> val.constraint(val.value() != null, () -> "should not be null"); } public OptimalTime(OffsetDateTime value) { @@ -24,6 +25,6 @@ public OffsetDateTime asOffsetDateTime() { public boolean isInFuture() { // TODO: Better Solution to handle "now == now"? - return this.asOffsetDateTime().isAfter(OffsetDateTime.now().minusMinutes(1)); + return this.asOffsetDateTime().isAfter(OffsetDateTime.now(ZoneOffset.UTC).minusMinutes(1)); } } diff --git a/carbon-reductor-core/src/test/java/de/envite/greenbpm/carbonreductor/core/domain/model/emissionframe/OptimalTimeTest.java b/carbon-reductor-core/src/test/java/de/envite/greenbpm/carbonreductor/core/domain/model/emissionframe/OptimalTimeTest.java index eba4938..7301a63 100644 --- a/carbon-reductor-core/src/test/java/de/envite/greenbpm/carbonreductor/core/domain/model/emissionframe/OptimalTimeTest.java +++ b/carbon-reductor-core/src/test/java/de/envite/greenbpm/carbonreductor/core/domain/model/emissionframe/OptimalTimeTest.java @@ -6,6 +6,7 @@ import org.junit.jupiter.api.Test; import java.time.OffsetDateTime; +import java.time.ZoneOffset; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -13,10 +14,11 @@ class OptimalTimeTest { + private final OffsetDateTime utcNow = OffsetDateTime.now(ZoneOffset.UTC); @Test void should_not_throw_if_valid() { - assertDoesNotThrow(() -> new OptimalTime(OffsetDateTime.now())); + assertDoesNotThrow(() -> new OptimalTime(utcNow)); } @Nested @@ -24,12 +26,10 @@ class ConvertToOffsetDateTime { @Test void should_convert() { - final OffsetDateTime offsetDateTime = OffsetDateTime.now(); - - final OptimalTime optimalTime = new OptimalTime(offsetDateTime); + final OptimalTime optimalTime = new OptimalTime(utcNow); SoftAssertions softAssertions = new SoftAssertions(); - softAssertions.assertThat(optimalTime.asOffsetDateTime()).isEqualTo(offsetDateTime); + softAssertions.assertThat(optimalTime.asOffsetDateTime()).isEqualTo(utcNow); softAssertions.assertThat(optimalTime.asOffsetDateTime()).isInstanceOf(OffsetDateTime.class); softAssertions.assertAll(); } @@ -40,21 +40,21 @@ class IsInFuture { @Test void should_return_true_if_is_in_future() { - final OptimalTime nextHour = new OptimalTime(OffsetDateTime.now().plusHours(1)); + final OptimalTime nextHour = new OptimalTime(utcNow.plusHours(1)); assertThat(nextHour.isInFuture()).isTrue(); } @Test void should_return_true_if_is_in_now() { - final OptimalTime now = new OptimalTime(OffsetDateTime.now()); + final OptimalTime now = new OptimalTime(utcNow); assertThat(now.isInFuture()).isTrue(); } @Test void should_return_false_if_is_in_past() { - final OptimalTime lastHour = new OptimalTime(OffsetDateTime.now().minusHours(1)); + final OptimalTime lastHour = new OptimalTime(utcNow.minusHours(1)); assertThat(lastHour.isInFuture()).isFalse(); }