From 83cd126d183cf55fd3a16cc40c0f5f634ed71dc5 Mon Sep 17 00:00:00 2001 From: Nikolaos Georgiou Date: Sun, 16 Jun 2019 13:00:20 +0200 Subject: [PATCH] Implementation of isCloseToUtcNow --- .editorconfig | 2 +- .../core/api/AbstractLocalDateTimeAssert.java | 31 +++++++++ .../api/AbstractOffsetDateTimeAssert.java | 31 +++++++++ .../core/api/AbstractTemporalAssert.java | 4 +- ...alDateTimeAssert_isCloseToUtcNow_Test.java | 66 +++++++++++++++++++ ...etDateTimeAssert_isCloseToUtcNow_Test.java | 66 +++++++++++++++++++ 6 files changed, 196 insertions(+), 4 deletions(-) create mode 100644 src/test/java/org/assertj/core/api/localdatetime/LocalDateTimeAssert_isCloseToUtcNow_Test.java create mode 100644 src/test/java/org/assertj/core/api/offsetdatetime/OffsetDateTimeAssert_isCloseToUtcNow_Test.java diff --git a/.editorconfig b/.editorconfig index 2f5c047ee4..1f4e3419f8 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,4 +1,4 @@ -# EditorConfig: http://EditorConfig.org +# EditorConfig: https://editorconfig.org/ # top-most EditorConfig file root = true diff --git a/src/main/java/org/assertj/core/api/AbstractLocalDateTimeAssert.java b/src/main/java/org/assertj/core/api/AbstractLocalDateTimeAssert.java index d2f31a719c..61b58ecbba 100644 --- a/src/main/java/org/assertj/core/api/AbstractLocalDateTimeAssert.java +++ b/src/main/java/org/assertj/core/api/AbstractLocalDateTimeAssert.java @@ -12,6 +12,8 @@ */ package org.assertj.core.api; +import static java.time.Clock.systemUTC; +import static java.time.LocalDateTime.now; import static org.assertj.core.error.ShouldBeAfter.shouldBeAfter; import static org.assertj.core.error.ShouldBeAfterOrEqualTo.shouldBeAfterOrEqualTo; import static org.assertj.core.error.ShouldBeBefore.shouldBeBefore; @@ -24,8 +26,10 @@ import java.time.LocalDateTime; import java.time.format.DateTimeParseException; +import java.time.temporal.TemporalUnit; import java.util.Arrays; +import org.assertj.core.data.TemporalUnitOffset; import org.assertj.core.internal.Failures; import org.assertj.core.internal.Objects; @@ -37,6 +41,7 @@ * @author Paweł Stawicki * @author Joel Costigliola * @author Marcin Zajączkowski + * @author Nikolaos Georgiou */ public abstract class AbstractLocalDateTimeAssert> extends AbstractTemporalAssert { @@ -317,6 +322,32 @@ public SELF isNotIn(String... dateTimesAsString) { return isNotIn(convertToLocalDateTimeArray(dateTimesAsString)); } + /** + * Verifies that the actual {@link LocalDateTime} is close to the current date and time on the UTC timezone, + * according to the given {@link TemporalUnitOffset}. + * You can build the offset parameter using {@link Assertions#within(long, TemporalUnit)} or {@link Assertions#byLessThan(long, TemporalUnit)}. + *

+ * If the difference is equal to the offset, the assertion succeeds. + *

+ * Example: + *

 LocalDateTime actual = LocalDateTime.now(Clock.systemUTC());
+   *
+   * // assertion will pass as it is executed less than one second after actual was built
+   * assertThat(actual).isCloseToUtcNow(within(1, ChronoUnit.SECONDS));
+   *
+   * // assertion will fail
+   * assertThat(actual.plusSeconds(2)).isCloseToUtcNow(within(1, ChronoUnit.SECONDS));
+ * + * @param offset The offset used for comparison + * @return this assertion object + * @throws NullPointerException if {@code offset} parameter is {@code null}. + * @throws AssertionError if the actual {@code LocalDateTime} is {@code null}. + * @throws AssertionError if the actual {@code LocalDateTime} is not close to the current time by less than the given offset. + */ + public SELF isCloseToUtcNow(TemporalUnitOffset offset) { + return isCloseTo(now(systemUTC()), offset); + } + private static Object[] convertToLocalDateTimeArray(String... dateTimesAsString) { return Arrays.stream(dateTimesAsString).map(LocalDateTime::parse).toArray(); } diff --git a/src/main/java/org/assertj/core/api/AbstractOffsetDateTimeAssert.java b/src/main/java/org/assertj/core/api/AbstractOffsetDateTimeAssert.java index abc1656a44..4d9a6e8a32 100644 --- a/src/main/java/org/assertj/core/api/AbstractOffsetDateTimeAssert.java +++ b/src/main/java/org/assertj/core/api/AbstractOffsetDateTimeAssert.java @@ -12,6 +12,8 @@ */ package org.assertj.core.api; +import static java.time.Clock.systemUTC; +import static java.time.OffsetDateTime.now; import static org.assertj.core.error.ShouldBeAfter.shouldBeAfter; import static org.assertj.core.error.ShouldBeAfterOrEqualTo.shouldBeAfterOrEqualTo; import static org.assertj.core.error.ShouldBeBefore.shouldBeBefore; @@ -25,7 +27,9 @@ import java.time.OffsetDateTime; import java.time.format.DateTimeParseException; +import java.time.temporal.TemporalUnit; +import org.assertj.core.data.TemporalUnitOffset; import org.assertj.core.internal.Failures; import org.assertj.core.internal.Objects; @@ -37,6 +41,7 @@ * @author Paweł Stawicki * @author Joel Costigliola * @author Marcin Zajączkowski + * @author Nikolaos Georgiou */ public abstract class AbstractOffsetDateTimeAssert> extends AbstractTemporalAssert { @@ -238,6 +243,32 @@ public SELF isAfter(String offsetDateTimeAsString) { return isAfter(parse(offsetDateTimeAsString)); } + /** + * Verifies that the actual {@link OffsetDateTime} is close to the current date and time on the UTC timezone, + * according to the given {@link TemporalUnitOffset}. + * You can build the offset parameter using {@link Assertions#within(long, TemporalUnit)} or {@link Assertions#byLessThan(long, TemporalUnit)}. + *

+ * If the difference is equal to the offset, the assertion succeeds. + *

+ * Example: + *

 OffsetDateTime actual = OffsetDateTime.now(Clock.systemUTC());
+   *
+   * // assertion will pass as it is executed less than one second after actual was built
+   * assertThat(actual).isCloseToUtcNow(within(1, ChronoUnit.SECONDS));
+   *
+   * // assertion will fail
+   * assertThat(actual.plusSeconds(2)).isCloseToUtcNow(within(1, ChronoUnit.SECONDS));
+ * + * @param offset The offset used for comparison + * @return this assertion object + * @throws NullPointerException if {@code offset} parameter is {@code null}. + * @throws AssertionError if the actual {@code OffsetDateTime} is {@code null}. + * @throws AssertionError if the actual {@code OffsetDateTime} is not close to the current time by less than the given offset. + */ + public SELF isCloseToUtcNow(TemporalUnitOffset offset) { + return isCloseTo(now(systemUTC()), offset); + } + /** * Same assertion as {@link #isEqualTo(Object)} (where Object is expected to be {@link java.time.OffsetDateTime}) but * here you diff --git a/src/main/java/org/assertj/core/api/AbstractTemporalAssert.java b/src/main/java/org/assertj/core/api/AbstractTemporalAssert.java index c11416da9d..b30706a4ca 100644 --- a/src/main/java/org/assertj/core/api/AbstractTemporalAssert.java +++ b/src/main/java/org/assertj/core/api/AbstractTemporalAssert.java @@ -80,7 +80,6 @@ public SELF isCloseTo(TEMPORAL other, TemporalOffset offset) { shouldBeCloseTo(actual, other, offset.getBeyondOffsetDifferenceDescription(actual, other))); } - return myself; } @@ -101,8 +100,7 @@ public SELF isCloseTo(TEMPORAL other, TemporalOffset offset) { * @throws AssertionError if the actual {@code Temporal} is not close to the given for a provided offset. */ public SELF isCloseTo(String otherAsString, TemporalOffset offset) { - checkNotNull(otherAsString, - "The String representing of the temporal object to compare actual with should not be null"); + checkNotNull(otherAsString, "The String representing of the temporal object to compare actual with should not be null"); return isCloseTo(parse(otherAsString), offset); } diff --git a/src/test/java/org/assertj/core/api/localdatetime/LocalDateTimeAssert_isCloseToUtcNow_Test.java b/src/test/java/org/assertj/core/api/localdatetime/LocalDateTimeAssert_isCloseToUtcNow_Test.java new file mode 100644 index 0000000000..3aafe95d35 --- /dev/null +++ b/src/test/java/org/assertj/core/api/localdatetime/LocalDateTimeAssert_isCloseToUtcNow_Test.java @@ -0,0 +1,66 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * Copyright 2012-2019 the original author or authors. + */ +package org.assertj.core.api.localdatetime; + +import static java.time.Clock.systemUTC; +import static java.time.LocalDateTime.now; +import static java.time.temporal.ChronoUnit.SECONDS; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatNullPointerException; +import static org.assertj.core.api.Assertions.within; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; +import static org.assertj.core.util.FailureMessages.actualIsNull; + +import java.time.LocalDateTime; + +import org.junit.jupiter.api.Test; + +/** + * @author Nikolaos Georgiou + */ +public class LocalDateTimeAssert_isCloseToUtcNow_Test extends LocalDateTimeAssertBaseTest { + + @Test + public void should_pass_when_executed_within_time_offset() { + // GIVEN + LocalDateTime now = now(systemUTC()); + // THEN + assertThat(now).isCloseToUtcNow(within(1, SECONDS)); + } + + @Test + public void should_fail_when_executed_after_time_offset() { + // GIVEN + LocalDateTime now = now(systemUTC()).minusSeconds(2); + // WHEN + AssertionError error = expectAssertionError(() -> assertThat(now).isCloseToUtcNow(within(1, SECONDS))); + // THEN + assertThat(error).hasMessageContaining("within 1 Seconds but difference was 2 Seconds"); + } + + @Test + public void should_fail_if_actual_is_null() { + // GIVEN + LocalDateTime actual = null; + // WHEN + AssertionError error = expectAssertionError(() -> assertThat(actual).isCloseToUtcNow(within(1, SECONDS))); + // THEN + assertThat(error).hasMessage(actualIsNull()); + } + + @Test + public void should_fail_if_offset_parameter_is_null() { + assertThatNullPointerException().isThrownBy(() -> assertThat(now()).isCloseToUtcNow(null)) + .withMessage("The offset should not be null"); + } +} diff --git a/src/test/java/org/assertj/core/api/offsetdatetime/OffsetDateTimeAssert_isCloseToUtcNow_Test.java b/src/test/java/org/assertj/core/api/offsetdatetime/OffsetDateTimeAssert_isCloseToUtcNow_Test.java new file mode 100644 index 0000000000..3304d26417 --- /dev/null +++ b/src/test/java/org/assertj/core/api/offsetdatetime/OffsetDateTimeAssert_isCloseToUtcNow_Test.java @@ -0,0 +1,66 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * Copyright 2012-2019 the original author or authors. + */ +package org.assertj.core.api.offsetdatetime; + +import static java.time.Clock.systemUTC; +import static java.time.OffsetDateTime.now; +import static java.time.temporal.ChronoUnit.SECONDS; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatNullPointerException; +import static org.assertj.core.api.Assertions.within; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; +import static org.assertj.core.util.FailureMessages.actualIsNull; + +import java.time.OffsetDateTime; + +import org.junit.jupiter.api.Test; + +/** + * @author Nikolaos Georgiou + */ +public class OffsetDateTimeAssert_isCloseToUtcNow_Test extends OffsetDateTimeAssertBaseTest { + + @Test + public void should_pass_when_executed_within_time_offset() { + // GIVEN + OffsetDateTime now = now(systemUTC()); + // THEN + assertThat(now).isCloseToUtcNow(within(1, SECONDS)); + } + + @Test + public void should_fail_when_executed_after_time_offset() { + // GIVEN + OffsetDateTime now = now(systemUTC()).minusSeconds(2); + // THEN + AssertionError error = expectAssertionError(() -> assertThat(now).isCloseToUtcNow(within(1, SECONDS))); + // THEN + assertThat(error).hasMessageContaining("within 1 Seconds but difference was 2 Seconds"); + } + + @Test + public void should_fail_if_actual_is_null() { + // GIVEN + OffsetDateTime actual = null; + // WHEN + AssertionError error = expectAssertionError(() -> assertThat(actual).isCloseToUtcNow(within(1, SECONDS))); + // THEN + assertThat(error).hasMessage(actualIsNull()); + } + + @Test + public void should_fail_if_offset_parameter_is_null() { + assertThatNullPointerException().isThrownBy(() -> assertThat(now()).isCloseToUtcNow(null)) + .withMessage("The offset should not be null"); + } +}