@Test
public void shouldDecrementNextIndexWhenReceivingAppendEntriesRejectToFindMatchingPrefix() throws StorageException, RPCException {

    insertIntoLog(
            NOOP(1, 1),
            NOOP(2, 1),
            NOOP(3, 2)
    );
    store.setCommitIndex(1);
    // become the leader
    becomeLeaderInTerm(3, false);
    // ... which should trigger a NOOP being added for this term
    assertThatLogContains(
            SENTINEL(),
            NOOP(1, 1),
            NOOP(2, 1),
            NOOP(3, 2),
            NOOP(4, 3)
    );
    Collection<AppendEntries> appendEntriesRequests;
    // check the "I am leader messages"
    appendEntriesRequests = getRPCs(4, AppendEntries.class);
    assertThatAppendEntriesHaveValues(appendEntriesRequests, 3, 1, 3, 2, NOOP(4, 3));
    assertThatNoMoreRPCsWereSent();
    // apparently S_03 does not have the same log you do
    algorithm.onAppendEntriesReply(S_03, 3, 3, 1, false);
    // move to the heartbeat timeout
    timer.fastForward();
    // again, heartbeat, but with more entries (to try overwrite their prefix)
    appendEntriesRequests = getRPCs(4, AppendEntries.class);
    for (AppendEntries appendEntries : appendEntriesRequests) {
        if (appendEntries.server.equals(S_03)) { // this server should get two entries (implying that nextIndex was decremented)
            assertThatAppendEntriesHasValues(
                    appendEntries,
                    3, 1, 2, 1,
                    NOOP(3, 2),
                    NOOP(4, 3));
        } else { // everyone gets the heartbeat with the noop entry only
            assertThatAppendEntriesHasValues(appendEntries, 3, 1, 3, 2, NOOP(4, 3));
        }
    }
    assertThatNoMoreRPCsWereSent();
    // check that no one was notified and that nothing changed
    // with the log or commitIndex
    verifyNoMoreInteractions(listener);
    assertThatLogContains(
            SENTINEL(),
            NOOP(1, 1),
            NOOP(2, 1),
            NOOP(3, 2),
            NOOP(4, 3)
    );
    assertThatTermAndCommitIndexHaveValues(3, 1);
}