@Test
public void shouldReturnTheLatestSnapshotIfSnapshotsExist() throws StorageException, IOException, InterruptedException {

    createSnapshots(10);
    // by the contract above, this is the data we should have
    long finalSnapshotTerm = 10;
    long finalSnapshotIndex = 10;
    byte[] finalSnapshotData = Integer.toString(10).getBytes(Charsets.US_ASCII.toString());
    // ok, now let's ask for the latest snapshot
    ExtendedSnapshot snapshot = snapshotsStore.getLatestSnapshot();
    if (snapshot == null) {
        throw new IllegalStateException("a snapshot should exist");
    }
    // check the term and index
    assertThat(snapshot.getTerm(), equalTo(finalSnapshotTerm));
    assertThat(snapshot.getIndex(), equalTo(finalSnapshotIndex));
    // read the data from the snapshot
    InputStream is = null;
    try {
        is = snapshot.getSnapshotInputStream();
        int bytesAvailable = is.available();
        byte[] buffer = new byte[bytesAvailable];
        int read = is.read(buffer);
        assertThat(read, equalTo(bytesAvailable));
        assertThat(Arrays.equals(buffer, finalSnapshotData), is(true));
    } finally {
        Closeables.close(is, true);
    }
}