blob: 683c0405ca812fc1d712cd91f65395d6ba20a58f [file] [log] [blame]
/*
* Copyright 2017 The Mifos Initiative.
*
* 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.
*/
package io.mifos.core.test.domain;
import io.mifos.core.lang.DateConverter;
import org.junit.Assert;
import java.time.Clock;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
/**
* Support class for testing that the correct time stamp is returned
*
* @author Myrle Krantz
*/
@SuppressWarnings("WeakerAccess")
public class TimeStampChecker {
private static final int DEFAULT_MAXIMUM_DELTA = 2;
private final LocalDateTime expectedTimeStamp;
private final Duration maximumDelta;
public static TimeStampChecker roughlyNow()
{
return new TimeStampChecker(LocalDateTime.now(Clock.systemUTC()), Duration.ofSeconds(DEFAULT_MAXIMUM_DELTA));
}
public static TimeStampChecker inTheFuture(final Duration offset)
{
return new TimeStampChecker(LocalDateTime.now(Clock.systemUTC()).plus(offset), Duration.ofSeconds(DEFAULT_MAXIMUM_DELTA));
}
public static TimeStampChecker allowSomeWiggleRoom(final Duration maximumDelta)
{
return new TimeStampChecker(LocalDateTime.now(Clock.systemUTC()), maximumDelta);
}
private TimeStampChecker(final LocalDateTime expectedTimeStamp, final Duration maximumDelta) {
this.expectedTimeStamp = expectedTimeStamp;
this.maximumDelta = maximumDelta;
}
public void assertCorrect(final String timeStamp)
{
Assert.assertTrue("Delta from expected should have been less than " +
maximumDelta + ". Timestamp string was " + timeStamp + ".",
isCorrect(timeStamp));
}
public boolean isCorrect(final String timeStamp) {
final LocalDateTime parsedTimeStamp = DateConverter.fromIsoString(timeStamp);
final Duration deltaFromExpected = Duration.ofNanos(Math.abs(
parsedTimeStamp.until(expectedTimeStamp, ChronoUnit.NANOS)));
return deltaFromExpected.compareTo(maximumDelta) < 0;
}
}