### Question:
CleanUpFinalisedStateMachines { @VisibleForTesting void cleanupIfFinalState(String id, Transition transition) { if (transition.getState() instanceof FinalState) { Optional<Lock> lock = repository.tryLock(id, 1, SECONDS); try { lock.ifPresent(this::cleanupIfFinalizedTimeHasExpired); } finally { lock.ifPresent(Lock::unlock); } } } CleanUpFinalisedStateMachines(TransitionRepository repository,
                                         Collection<OnCleanupListener> cleanupListeners,
                                         long scheduleFrequency, TimeUnit scheduleTimeUnit,
                                         long keepFinalised, ChronoUnit keepFinalisedTimeUnit); void startScheduler(); void stopScheduler(); void checkForFinalizedStateMachinesAndCleanupIfRequired();  }### Answer:
@Test public void shouldNotAttemptCleanupWhenNonFinalState() { cleaner.cleanupIfFinalState("1", createTransitionInState(RECHARGE_REQUESTED_STATE)); verify(repository, never()).tryLock(eq("1"), anyLong(), any(TimeUnit.class)); }
@Test public void shouldAttemptCleanupWhenFinalState() { final Transition transition = createTransitionInState(RECHARGE_ACCEPTED_FINAL_STATE); when(repository.tryLock("2", 1, SECONDS)).thenReturn(Optional.of(lock)); when(lock.getLast()).thenReturn(transition); cleaner.cleanupIfFinalState("2", transition); verify(repository).tryLock(eq("2"), anyLong(), any(TimeUnit.class)); }