@Test
public void shouldReturnLogEntryInResponseToRepeatedGetNextCommittedCallsWithNonZeroInitialIndexToSearchFromAndOverlapBetweenLogAndSnapshot() throws Exception {

    // we have a snapshot that contains data to index 7 (inclusive)
    SnapshotsStore.ExtendedSnapshot snapshot = new UnitTestSnapshot(7L, 2);
    // the log only contains entries from index 7 (inclusive) onwards
    LogEntry unappliedEntry0 = NOOP(7, 2);
    final LogEntry[] entries = new LogEntry[] {
            unappliedEntry0, // <------ we've committed up to here
            NOOP(8, 2),
            NOOP(9, 3),
            CLIENT(10, 3, new UnitTestCommand()),
            CLIENT(11, 3, new UnitTestCommand()),
            CLIENT(12, 3, new UnitTestCommand())
    };
    clearLog(); // want the log to be completely clear
    insertIntoLog(entries);
    // set the current term
    long currentTerm = 3;
    store.setCurrentTerm(currentTerm);
    // set the committed index
    long commitIndex = 7;
    store.setCommitIndex(commitIndex);
    // return the snapshot defined above when asked for the latest
    when(snapshotsStore.getLatestSnapshot()).thenReturn(snapshot);
    //
    // situation is as follows:
    //
    //                                   +----------- COMMITTED
    //                                   V
    //                                 ------------------------------
    //          .... EMPTY ....        | 07 | 08 | 09 | 10 | 11 | 12 | (LOG)
    //                                 ------------------------------
    //  ------------------------------------
    // |         LAST APPLIED = 7          | (SNAPSHOT)
    // ------------------------------------
    //                             ^
    //                             +----------- INDEX TO SEARCH FROM = 6
    //
    // so, the snapshot and the log overlap for one index: 7
    // and we've committed up to index 7 as well
    //
    // if the caller says they've applied up to index 6, we have two options:
    //   1. return a snapshot: they will flush their state and load the snapshot
    //   2. return only log entry 7: they simply apply that entry as usual
    //
    // we opt for option 2
    //
    Committed committed;
    // OK, now, let's start by saying that the last index we applied was '6' (kinda weird, given that we have a snapshot with last applied = 7, but, whatever)
    committed = algorithm.getNextCommitted(6);
    // check that we got the _log entry_ back
    checkMatchingCommittedLogEntry(committed, unappliedEntry0);
    // if we make a second call we should get nothing (because index 7 was the last one committed)
    committed = algorithm.getNextCommitted(committed.getIndex()); // index = 7
    assertThat(committed, nullValue());
    // we should not have changed our internal state
    assertThatLogContains(entries);
    assertThatTermAndCommitIndexHaveValues(currentTerm, commitIndex);
}