Skip to content

Commit

Permalink
Implementation of isCloseToUtcNow
Browse files Browse the repository at this point in the history
  • Loading branch information
ngeor authored and joel-costigliola committed Jun 24, 2019
1 parent 1160bc5 commit 83cd126
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# EditorConfig: http://EditorConfig.org
# EditorConfig: https://editorconfig.org/

# top-most EditorConfig file
root = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -37,6 +41,7 @@
* @author Paweł Stawicki
* @author Joel Costigliola
* @author Marcin Zajączkowski
* @author Nikolaos Georgiou
*/
public abstract class AbstractLocalDateTimeAssert<SELF extends AbstractLocalDateTimeAssert<SELF>> extends
AbstractTemporalAssert<SELF, LocalDateTime> {
Expand Down Expand Up @@ -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)}.
* <p>
* If the difference is equal to the offset, the assertion succeeds.
* <p>
* Example:
* <pre><code class='java'> 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));</code></pre>
*
* @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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -37,6 +41,7 @@
* @author Paweł Stawicki
* @author Joel Costigliola
* @author Marcin Zajączkowski
* @author Nikolaos Georgiou
*/
public abstract class AbstractOffsetDateTimeAssert<SELF extends AbstractOffsetDateTimeAssert<SELF>> extends
AbstractTemporalAssert<SELF, OffsetDateTime> {
Expand Down Expand Up @@ -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)}.
* <p>
* If the difference is equal to the offset, the assertion succeeds.
* <p>
* Example:
* <pre><code class='java'> 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));</code></pre>
*
* @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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public SELF isCloseTo(TEMPORAL other, TemporalOffset<? super TEMPORAL> offset) {
shouldBeCloseTo(actual, other,
offset.getBeyondOffsetDifferenceDescription(actual, other)));
}

return myself;
}

Expand All @@ -101,8 +100,7 @@ public SELF isCloseTo(TEMPORAL other, TemporalOffset<? super TEMPORAL> offset) {
* @throws AssertionError if the actual {@code Temporal} is not close to the given for a provided offset.
*/
public SELF isCloseTo(String otherAsString, TemporalOffset<? super TEMPORAL> 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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit 83cd126

Please sign in to comment.