@Test
  public void rebuildTokenWhenLastSegmentAutoClosedTestCase1() throws StoreException {
    assumeTrue(isLogSegmented);
    IndexSegment lastIndexSegment = stateForTokenTest.index.getIndexSegments().lastEntry().getValue();
    Offset validOffset = lastIndexSegment.getStartOffset();
    StoreFindToken startToken =
        new StoreFindToken(validOffset, stateForTokenTest.sessionId, stateForTokenTest.incarnationId, false,
            lastIndexSegment.getResetKey(), lastIndexSegment.getResetKeyType(),
            lastIndexSegment.getResetKeyLifeVersion());
    autoCloseLastLogSegmentAndCleanUpJournal();
    StoreFindToken token = startToken;
    StoreFindToken expectedToken;
    int totalIndexEntryInLastIndexSegment = lastIndexSegment.size();
    for (int i = 0; i <= totalIndexEntryInLastIndexSegment; i++) {
      FindInfo findInfo = stateForTokenTest.index.findEntriesSince(token, 1);
      token = (StoreFindToken) findInfo.getFindToken();
      if (i < totalIndexEntryInLastIndexSegment) {
        //the index based token is moving forward after call findEntriesSince until it reached to the end of last index segment.
        expectedToken = new StoreFindToken(lastIndexSegment.listIterator(i + 1).previous().getKey(),
            lastIndexSegment.getStartOffset(), stateForTokenTest.sessionId, stateForTokenTest.incarnationId,
            lastIndexSegment.getResetKey(), lastIndexSegment.getResetKeyType(),
            lastIndexSegment.getResetKeyLifeVersion());
      } else {
        //after index based token point to the last entry of index segment, should return directly return the original index based token.
        expectedToken =
            new StoreFindToken(lastIndexSegment.listIterator(totalIndexEntryInLastIndexSegment).previous().getKey(),
                lastIndexSegment.getStartOffset(), stateForTokenTest.sessionId, stateForTokenTest.incarnationId,
                lastIndexSegment.getResetKey(), lastIndexSegment.getResetKeyType(),
                lastIndexSegment.getResetKeyLifeVersion());
      }
      compareTokens(expectedToken, token);
    }
  }
Test generating a journal based token and close last log segment, should return the index based token point to first
index entry of last index segment.
Then input the output index based token and check if we can move the token forward until it reached to the end of
last index segment.
@throws StoreException
ambry-store/src/test/java/com/github/ambry/store/IndexTest.java