Snapshot { public TeamStates getTeamStates() { return teamStates; } @SuppressWarnings("unused") private  Snapshot();  Snapshot(Instant time); Optional<Long> getId(); Optional<Long> getEventId(); Instant getSnapshotTime(); TeamTagMap getTeamTagMap(); Instant getStartTime(); Instant getEndTime(); TeamStates getTeamStates(); TeamStates getPublicTeamStates(); String getStatusMessage(); Status getStatus(); int getUpdateFrequency(); static Builder builder(Instant time, Snapshot other); static Optional<Snapshot> loadForEvent(Connection connection, Event event); static Optional<Snapshot> loadLatest(Connection connection); static Optional<Snapshot> loadBefore(Connection connection, Instant time); void save(Connection connection); static void deleteAfter(Connection connection, Snapshot snapshot);  }
@Test public void testTagSeenEvents_ShouldAddLap() throws Exception { Snapshot snapshot = new Snapshot(Instant.EPOCH); AddTagEvent addTagEvent = new AddTagEvent(Instant.EPOCH.plus(1, ChronoUnit.SECONDS), new TagId("ABCD"), 0); snapshot = addTagEvent.doApply(snapshot); StartEvent startEvent = new StartEvent(Instant.EPOCH.plus(2, ChronoUnit.SECONDS)); snapshot = startEvent.doApply(snapshot); TagSeenEvent tagSeenEvent = new TagSeenEvent(Instant.EPOCH.plus(3, ChronoUnit.SECONDS), new TagId("ABCD"), 0, 0L); snapshot = tagSeenEvent.doApply(snapshot); TagSeenEvent tagSeenEvent2 = new TagSeenEvent(Instant.EPOCH.plus(60, ChronoUnit.SECONDS), new TagId("ABCD"), 0, 1L); snapshot = tagSeenEvent2.doApply(snapshot); MatcherAssert.assertThat(snapshot.getTeamStates().getNbLapsForTeam(0), Matchers.equalTo(1)); }
@Test public void testTagSeenEventBeforeStart_ShouldBeIgnored() throws Exception { Snapshot snapshot = new Snapshot(Instant.EPOCH); AddTagEvent addTagEvent = new AddTagEvent(Instant.EPOCH.plus(1, ChronoUnit.SECONDS), new TagId("ABCD"), 0); snapshot = addTagEvent.doApply(snapshot); TagSeenEvent tagSeenEvent = new TagSeenEvent(Instant.EPOCH.plus(2, ChronoUnit.SECONDS), new TagId("ABCD"), 0, 0L); snapshot = tagSeenEvent.doApply(snapshot); TagSeenEvent tagSeenEvent2 = new TagSeenEvent(Instant.EPOCH.plus(3, ChronoUnit.SECONDS), new TagId("ABCD"), 0, 1L); snapshot = tagSeenEvent2.doApply(snapshot); MatcherAssert.assertThat(snapshot.getTeamStates().getNbLapsForTeam(0), Matchers.equalTo(0)); }
@Test public void testTagSeenEventAfterEnd_ShouldBeIgnored() throws Exception { Snapshot snapshot = new Snapshot(Instant.EPOCH); AddTagEvent addTagEvent = new AddTagEvent(Instant.EPOCH.plus(1, ChronoUnit.SECONDS), new TagId("DCBA"), 3); snapshot = addTagEvent.doApply(snapshot); StartEvent startEvent = new StartEvent(Instant.EPOCH.plus(2, ChronoUnit.SECONDS)); snapshot = startEvent.doApply(snapshot); TagSeenEvent tagSeenEvent = new TagSeenEvent(Instant.EPOCH.plus(3, ChronoUnit.SECONDS), new TagId("DCBA"), 0, 0L); snapshot = tagSeenEvent.doApply(snapshot); EndEvent endEvent = new EndEvent(Instant.EPOCH.plus(50, ChronoUnit.SECONDS)); snapshot = endEvent.doApply(snapshot); TagSeenEvent tagSeenEvent2 = new TagSeenEvent(Instant.EPOCH.plus(100, ChronoUnit.SECONDS), new TagId("DCBA"), 0, 1L); snapshot = tagSeenEvent2.doApply(snapshot); MatcherAssert.assertThat(snapshot.getTeamStates().getNbLapsForTeam(3), Matchers.equalTo(0)); }
@Test public void testPredictedSpeedWhenStartedThenFirstEvent() throws Exception { TagId tag = new TagId("DCBA"); Snapshot snapshot = new Snapshot(Instant.EPOCH); AddTagEvent addTagEvent = new AddTagEvent(Instant.EPOCH, tag, 3); snapshot = addTagEvent.doApply(snapshot); StartEvent startEvent = new StartEvent(Instant.EPOCH); snapshot = startEvent.doApply(snapshot); TagSeenEvent tagSeenEvent = new TagSeenEvent(Instant.EPOCH.plus(10, ChronoUnit.SECONDS), tag, 0, 0L); snapshot = tagSeenEvent.doApply(snapshot); TagSeenEvent tagSeenEvent2 = new TagSeenEvent(Instant.EPOCH.plus(50, ChronoUnit.SECONDS), tag, 1, 1L); snapshot = tagSeenEvent2.doApply(snapshot); double speedShouldBe = 100D / (50D - 10D); MatcherAssert.assertThat(snapshot.getTeamStates().getStateForTeam(3).get() .getPredictedSpeed(), Matchers.equalTo(speedShouldBe)); MatcherAssert.assertThat(snapshot.getTeamStates().getStateForTeam(3).get() .getSpeed(), Matchers.equalTo(speedShouldBe)); }
@Test public void testPredictedSpeedWhenFirstEventThenStarted() throws Exception { TagId tag = new TagId("DCBA"); Snapshot snapshot = new Snapshot(Instant.EPOCH); AddTagEvent addTagEvent = new AddTagEvent(Instant.EPOCH, tag, 3); snapshot = addTagEvent.doApply(snapshot); TagSeenEvent tagSeenEvent = new TagSeenEvent(Instant.EPOCH.minus(10, ChronoUnit.SECONDS), tag, 0, 0L); snapshot = tagSeenEvent.doApply(snapshot); StartEvent startEvent = new StartEvent(Instant.EPOCH); snapshot = startEvent.doApply(snapshot); TagSeenEvent tagSeenEvent2 = new TagSeenEvent(Instant.EPOCH.plus(50, ChronoUnit.SECONDS), tag, 1, 1L); snapshot = tagSeenEvent2.doApply(snapshot); double speedShouldBe = 100D / 50D; MatcherAssert.assertThat(snapshot.getTeamStates().getStateForTeam(3).get() .getPredictedSpeed(), Matchers.equalTo(speedShouldBe)); MatcherAssert.assertThat(snapshot.getTeamStates().getStateForTeam(3).get() .getSpeed(), Matchers.equalTo(speedShouldBe)); }