Skip to content

Commit

Permalink
Merge pull request #218
Browse files Browse the repository at this point in the history
Synching Timers to Conductor via PUT call
  • Loading branch information
tedyoung authored Nov 19, 2024
2 parents 43f42c3 + 2899f96 commit 90269cd
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.jitterted.mobreg.adapter.out.conductor;

import com.jitterted.mobreg.application.port.Broadcaster;
import com.jitterted.mobreg.domain.CountdownTimer;
import com.jitterted.mobreg.domain.EnsembleTimer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -14,37 +13,31 @@

@Component
public class ConductorHttpBroadcaster implements Broadcaster {
private static final String BASE_URL = "https://conductor.suigi.dev";
private final RestClient restClient = RestClient.create();

private static final Logger logger = LoggerFactory.getLogger(ConductorHttpBroadcaster.class);

@Override
public void sendCurrentTimer(EnsembleTimer ensembleTimer) {

if (ensembleTimer.state() == CountdownTimer.TimerState.WAITING_TO_START) {
CreateTimerRequest createTimerRequest = new CreateTimerRequest(
ensembleTimer.ensembleName(),
ensembleTimer.timeRemaining()
.minutes() * 60);
try {
ResponseEntity<Void> responseEntity =
restClient.post()
.uri("https://conductor.suigi.dev/timers")
.contentType(APPLICATION_JSON)
.body(createTimerRequest)
.retrieve()
.toBodilessEntity();
System.out.println("POST response: " + responseEntity);
} catch (RestClientException e) {
logger.warn("Attempted to POST to Conductor service", e);
}
try {
String timerName = "Ensembler - " + ensembleTimer.ensembleName();
String uri = BASE_URL + "/timers/" + timerName;
ResponseEntity<Void> responseEntity =
restClient.put()
.uri(uri)
.contentType(APPLICATION_JSON)
.body(SyncConductorTimerRequest.from(ensembleTimer))
.retrieve()
.toBodilessEntity();
System.out.println("PUT response: " + responseEntity);
} catch (RestClientException e) {
logger.warn("Attempted to PUT to Conductor service", e);
}
}

@Override
public void sendEvent(EnsembleTimer.TimerEvent timerEvent) {
}

record CreateTimerRequest(String name, int durationSeconds) {
}
}
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";
};
}

}
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");
}

}

0 comments on commit 90269cd

Please sign in to comment.