This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
155 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/uk/gov/justice/framework/command/client/util/Clock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package uk.gov.justice.framework.command.client.util; | ||
|
||
import java.time.ZonedDateTime; | ||
|
||
/** | ||
* Interface for clock providers. | ||
*/ | ||
public interface Clock { | ||
|
||
ZonedDateTime now(); | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/uk/gov/justice/framework/command/client/util/Sleeper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package uk.gov.justice.framework.command.client.util; | ||
|
||
import static java.lang.Thread.currentThread; | ||
import static java.lang.Thread.sleep; | ||
|
||
/** | ||
* Wrapper for Thread.sleep() to allow mocking | ||
*/ | ||
public class Sleeper { | ||
|
||
/** | ||
* Sleeps for the specified number of milliseconds | ||
* @param milliseconds the duration of the sleep in milliseconds | ||
*/ | ||
public void sleepFor(final long milliseconds) { | ||
try { | ||
sleep(milliseconds); | ||
} catch (final InterruptedException unused) { | ||
currentThread().interrupt(); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/uk/gov/justice/framework/command/client/util/UtcClock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package uk.gov.justice.framework.command.client.util; | ||
|
||
import static java.time.ZoneOffset.UTC; | ||
|
||
import java.time.ZonedDateTime; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
|
||
/** | ||
* Implementation of a clock that always generates a {@link ZonedDateTime} in UTC. | ||
*/ | ||
@ApplicationScoped | ||
public class UtcClock implements Clock { | ||
|
||
@Override | ||
public ZonedDateTime now() { | ||
return ZonedDateTime.now(UTC); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/test/java/uk/gov/justice/framework/command/client/util/SleeperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package uk.gov.justice.framework.command.client.util; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.Matchers.greaterThanOrEqualTo; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class SleeperTest { | ||
|
||
@InjectMocks | ||
private Sleeper sleeper; | ||
|
||
@Test | ||
public void shouldSleepForTheRequiredNumberOfMillseconds() throws Exception { | ||
|
||
final long sleepTime = 1_000; | ||
|
||
final long startTime = System.currentTimeMillis(); | ||
|
||
sleeper.sleepFor(sleepTime); | ||
|
||
final long endTime = System.currentTimeMillis(); | ||
|
||
assertThat(endTime - startTime, is(greaterThanOrEqualTo(sleepTime))); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/test/java/uk/gov/justice/framework/command/client/util/UtcClockTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package uk.gov.justice.framework.command.client.util; | ||
|
||
import static java.time.ZoneOffset.UTC; | ||
import static java.time.ZonedDateTime.now; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import java.time.ZonedDateTime; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
/** | ||
* Unit tests for the {@link UtcClock} class. | ||
*/ | ||
@RunWith(MockitoJUnitRunner.class) | ||
public class UtcClockTest { | ||
|
||
@InjectMocks | ||
private UtcClock clock; | ||
|
||
@Test | ||
public void shouldGetANewZonedDateTimeWithCurrentTime() throws Exception { | ||
|
||
final ZonedDateTime zonedDateTime = clock.now(); | ||
|
||
assertThat(zonedDateTime, is(notNullValue())); | ||
|
||
assertThat(zonedDateTime.isAfter(now().minusSeconds(2L)), is(true)); | ||
assertThat(zonedDateTime.getZone(), is(UTC)); | ||
} | ||
} |