Skip to content

Commit

Permalink
Add isAtSameInstantAs to OffsetDateTime assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
raynicho authored and joel-costigliola committed Oct 26, 2019
1 parent 25ab08f commit a12dc51
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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.ShouldBeAtSameInstant.shouldBeAtSameInstant;
import static org.assertj.core.error.ShouldBeBefore.shouldBeBefore;
import static org.assertj.core.error.ShouldBeBeforeOrEqualTo.shouldBeBeforeOrEqualTo;
import static org.assertj.core.error.ShouldBeEqualIgnoringHours.shouldBeEqualIgnoringHours;
Expand Down Expand Up @@ -671,6 +672,34 @@ public SELF isStrictlyBetween(String startExclusive, String endExclusive) {
return isStrictlyBetween(parse(startExclusive), parse(endExclusive));
}

/**
* Verifies that actual and given {@code OffsetDateTime} are at the same {@link java.time.Instant}.
* <p>
* Example:
* <pre><code class='java'> OffsetDateTime OffsetDateTime1 = OffsetDateTime.of(2000, 12, 12, 3, 0, 0, 0, ZoneOffset.ofHours(3));
* OffsetDateTime OffsetDateTime2 = OffsetDateTime.of(2000, 12, 12, 0, 0, 0, 0, ZoneOffset.ofHours(0));
* // assertion succeeds
* assertThat(OffsetDateTime1).isAtSameInstantAs(OffsetDateTime2);
*
* OffsetDateTime OffsetDateTime1 = OffsetDateTime.of(2000, 12, 12, 3, 0, 0, 0, ZoneOffset.ofHours(3));
* OffsetDateTime OffsetDateTime2 = OffsetDateTime.of(2000, 12, 12, 2, 0, 0, 0, ZoneOffset.ofHours(0));
* // assertion fails
* assertThat(OffsetDateTimeA).isAtSameInstantAs(OffsetDateTimeB);</code></pre>
*
* @param other the given {@link OffsetDateTime}.
* @return this assertion object.
* @throws AssertionError if the actual {@code OffsetDateTime} is {@code null}.
* @throws IllegalArgumentException if other {@code OffsetDateTime} is {@code null}.
* @throws AssertionError if the actual {@code OffsetDateTime} is not at the same {@code Instant} as the other.
*/
public SELF isAtSameInstantAs(OffsetDateTime other) {
Objects.instance().assertNotNull(info, actual);
assertOffsetDateTimeParameterIsNotNull(other);
if (!actual.toInstant().equals(other.toInstant()))
throw Failures.instance().failure(info, shouldBeAtSameInstant(actual, other));
return myself;
}

/**
* {@inheritDoc}
*/
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/org/assertj/core/error/ShouldBeAtSameInstant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.error;

import java.time.OffsetDateTime;

/**
* Creates an error message indicating that an assertion that verifies that two {@link OffsetDateTime OffsetDateTimes}
* have the same {@link java.time.Instant}, failed.
*
* @author Raymond Pressly
*/
public class ShouldBeAtSameInstant extends BasicErrorMessageFactory {

/**
* Creates a new <code>{@link ShouldBeAtSameInstant}</code>.
* @param actual the actual value in the failed assertion.
* @param other the expected value in the failed assertion.
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldBeAtSameInstant(OffsetDateTime actual, OffsetDateTime other) {
return new ShouldBeAtSameInstant(actual, other);
}

private ShouldBeAtSameInstant(OffsetDateTime actual, OffsetDateTime expected) {
super("%nExpecting%n" +
" <%s>%n" +
"to be at the same instant as:%n" +
" <%s>%n" +
"but actual instance was%n" +
" <%s>%n" +
"and expected instant was:%n" +
" <%s>",
actual, expected, actual.toInstant(), expected.toInstant());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ public class OffsetDateTimeAssertBaseTest extends BaseTest {
public static final OffsetDateTime REFERENCE = OffsetDateTime.of(2000, 12, 14, 0, 0, 0, 0, ZoneOffset.UTC);
public static final OffsetDateTime BEFORE = OffsetDateTime.of(2000, 12, 13, 23, 59, 59, 999, ZoneOffset.UTC);
public static final OffsetDateTime AFTER = OffsetDateTime.of(2000, 12, 14, 0, 0, 0, 1, ZoneOffset.UTC);

}
public static final OffsetDateTime SAME_INSTANT = OffsetDateTime.of(2000, 12, 14, 3, 0, 0, 0, ZoneOffset.ofHours(3));
public static final OffsetDateTime DIFFERENT_INSTANT = OffsetDateTime.of(2000, 12, 14, 0, 0, 0, 0, ZoneOffset.ofHours(-3));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.OffsetDateTime.now;
import static org.assertj.core.api.AbstractOffsetDateTimeAssert.NULL_OFFSET_DATE_TIME_PARAMETER_MESSAGE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.error.ShouldBeAtSameInstant.shouldBeAtSameInstant;
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.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("OffsetDateTimeAssert isAtSameInstantAs")
class OffsetDateTimeAssert_isAtSameInstantAs_Test extends OffsetDateTimeAssertBaseTest {

@Test
void should_pass_if_at_the_same_instant() {
assertThat(REFERENCE).isAtSameInstantAs(SAME_INSTANT)
.isAtSameInstantAs(REFERENCE);
}

@Test
void should_fail_if_at_a_different_instant() {
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat(REFERENCE).isAtSameInstantAs(DIFFERENT_INSTANT));
// THEN
assertThat(assertionError).hasMessage(shouldBeAtSameInstant(REFERENCE, DIFFERENT_INSTANT).create());
}

@Test
void should_fail_if_actual_is_null() {
// GIVEN
OffsetDateTime actual = null;
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat(actual).isAtSameInstantAs(now()));
// THEN
assertThat(assertionError).hasMessage(actualIsNull());
}

@Test
void should_fail_if_given_OffsetDateTime_is_null() {
assertThatIllegalArgumentException().isThrownBy(() -> assertThat(REFERENCE).isAtSameInstantAs(null))
.withMessage(NULL_OFFSET_DATE_TIME_PARAMETER_MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.error;

import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.error.ShouldBeAtSameInstant.shouldBeAtSameInstant;
import static org.assertj.core.presentation.StandardRepresentation.STANDARD_REPRESENTATION;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;

import org.assertj.core.description.TextDescription;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("ShouldBeAtSameInstant create")
class ShouldBeAtSameInstant_create_Test {

@Test
void should_create_error_message() {
// GIVEN
OffsetDateTime actual = OffsetDateTime.of(2000, 12, 14, 0, 0, 0, 0, ZoneOffset.UTC);
OffsetDateTime other = OffsetDateTime.of(2000, 12, 14, 0, 0, 0, 0, ZoneOffset.ofHours(-3));
// WHEN
String message = shouldBeAtSameInstant(actual, other).create(new TextDescription("Test"), STANDARD_REPRESENTATION);
// THEN
assertThat(message).isEqualTo(format("[Test] %n" +
"Expecting%n" +
" <2000-12-14T00:00Z>%n" +
"to be at the same instant as:%n" +
" <2000-12-14T00:00-03:00>%n" +
"but actual instance was%n" +
" <2000-12-14T00:00:00Z>%n" +
"and expected instant was:%n" +
" <2000-12-14T03:00:00Z>"));
}
}

0 comments on commit a12dc51

Please sign in to comment.