-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Synching Timers to Conductor via PUT call
- Loading branch information
Showing
3 changed files
with
177 additions
and
21 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
42 changes: 42 additions & 0 deletions
42
src/main/java/com/jitterted/mobreg/adapter/out/conductor/SyncConductorTimerRequest.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,42 @@ | ||
package com.jitterted.mobreg.adapter.out.conductor; | ||
|
||
import com.jitterted.mobreg.domain.CountdownTimer; | ||
import com.jitterted.mobreg.domain.EnsembleTimer; | ||
import com.jitterted.mobreg.domain.Member; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.List; | ||
|
||
record SyncConductorTimerRequest( | ||
int timeRemainingSeconds, | ||
String state, | ||
String driver, | ||
String navigator, | ||
String nextDriver, | ||
List<String> restOfParticipants) { | ||
|
||
public static SyncConductorTimerRequest from(EnsembleTimer timer) { | ||
return new SyncConductorTimerRequest( | ||
timer.timeRemaining().minutes() * 60 + | ||
timer.timeRemaining().seconds(), | ||
mapTimerState(timer.state()), | ||
timer.rotation().driver().firstName(), | ||
timer.rotation().navigator().firstName(), | ||
timer.rotation().nextDriver().firstName(), | ||
timer.rotation().restOfParticipants().stream() | ||
.map(Member::firstName) | ||
.toList() | ||
); | ||
} | ||
|
||
@NotNull | ||
private static String mapTimerState(CountdownTimer.TimerState state) { | ||
return switch (state) { | ||
case WAITING_TO_START -> "Waiting"; | ||
case RUNNING -> "Running"; | ||
case PAUSED -> "Paused"; | ||
case FINISHED -> "Finished"; | ||
}; | ||
} | ||
|
||
} |
121 changes: 121 additions & 0 deletions
121
src/test/java/com/jitterted/mobreg/adapter/out/conductor/SyncConductorTimerRequestTest.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,121 @@ | ||
package com.jitterted.mobreg.adapter.out.conductor; | ||
|
||
import com.jitterted.mobreg.domain.EnsembleId; | ||
import com.jitterted.mobreg.domain.EnsembleTimer; | ||
import com.jitterted.mobreg.domain.EnsembleTimerFactory; | ||
import com.jitterted.mobreg.domain.Member; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.Instant; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class SyncConductorTimerRequestTest { | ||
|
||
private static final EnsembleId IRRELEVANT_ENSEMBLE_ID = EnsembleId.of(42); | ||
private static final String IRRELEVANT_ENSEMBLE_NAME = "My Ensemble"; | ||
|
||
@Nested | ||
class TimerState { | ||
@Test | ||
void waitingTimerIsMappedToConductorStateWaiting() { | ||
EnsembleTimer timer = EnsembleTimerFactory.createTimer(); | ||
|
||
SyncConductorTimerRequest request = SyncConductorTimerRequest.from(timer); | ||
|
||
assertThat(request.state()) | ||
.isEqualTo("Waiting"); | ||
} | ||
|
||
@Test | ||
void runningTimerIsMappedToConductorStateRunning() { | ||
EnsembleTimer timer = EnsembleTimerFactory.createTimer(); | ||
Instant startedAt = Instant.now(); | ||
timer.startTimerAt(startedAt); | ||
timer.tick(startedAt.plusSeconds(10)); | ||
|
||
SyncConductorTimerRequest request = SyncConductorTimerRequest.from(timer); | ||
|
||
assertThat(request.state()) | ||
.isEqualTo("Running"); | ||
} | ||
|
||
@Test | ||
void pausedTimerIsMappedToConductorStatePaused() { | ||
EnsembleTimer timer = EnsembleTimerFactory.createTimer(); | ||
timer.startTimerAt(Instant.now()); | ||
timer.pause(); | ||
|
||
SyncConductorTimerRequest request = SyncConductorTimerRequest.from(timer); | ||
|
||
assertThat(request.state()) | ||
.isEqualTo("Paused"); | ||
} | ||
|
||
@Test | ||
void finishedTimerIsMappedToConductorStateFinished() { | ||
EnsembleTimer timer = EnsembleTimerFactory | ||
.create4MinuteTimerInFinishedState() | ||
.ensembleTimer(); | ||
|
||
SyncConductorTimerRequest request = SyncConductorTimerRequest.from(timer); | ||
|
||
assertThat(request.state()) | ||
.isEqualTo("Finished"); | ||
} | ||
} | ||
|
||
@Nested | ||
class RemainingTime { | ||
@Test | ||
void waitingTimerHasFullDurationRemaining() { | ||
var timer = EnsembleTimerFactory.createTimerWith4MinuteDuration(); | ||
|
||
SyncConductorTimerRequest request = SyncConductorTimerRequest.from(timer); | ||
|
||
assertThat(request.timeRemainingSeconds()) | ||
.isEqualTo(4 * 60); | ||
} | ||
|
||
@Test | ||
void runningTimeHasTimeRemainingInSeconds() { | ||
var timer = EnsembleTimerFactory.createTimerWith4MinuteDuration(); | ||
Instant startedAt = Instant.now(); | ||
timer.startTimerAt(startedAt); | ||
timer.tick(startedAt.plusSeconds(15)); | ||
|
||
SyncConductorTimerRequest request = SyncConductorTimerRequest.from(timer); | ||
|
||
assertThat(request.timeRemainingSeconds()) | ||
.isEqualTo(4 * 60 - 15); | ||
} | ||
} | ||
|
||
@Test | ||
void rotationIsMappedToRequestParts() { | ||
EnsembleTimer timer = new EnsembleTimer( | ||
IRRELEVANT_ENSEMBLE_ID, | ||
IRRELEVANT_ENSEMBLE_NAME, | ||
List.of( | ||
new Member("Alex", "alex_github"), | ||
new Member("Berta", "berta_github"), | ||
new Member("Chloe", "chloe_github"), | ||
new Member("David", "david_github"), | ||
new Member("Erika", "erika_github") | ||
)); | ||
|
||
SyncConductorTimerRequest request = SyncConductorTimerRequest.from(timer); | ||
|
||
assertThat(request.navigator()) | ||
.isEqualTo("Chloe"); | ||
assertThat(request.driver()) | ||
.isEqualTo("Berta"); | ||
assertThat(request.nextDriver()) | ||
.isEqualTo("Alex"); | ||
assertThat(request.restOfParticipants()) | ||
.containsExactly("David", "Erika"); | ||
} | ||
|
||
} |