@Test
  public void testIndexSegmentOffsetsMethods() throws IOException {
    String storeName = "store";
    CompactionDetails details = getCompactionDetails(0);
    List<LogSegmentName> segmentsUnderCompaction = details.getLogSegmentsUnderCompaction();
    segmentsUnderCompaction.sort(LogSegmentName::compareTo);
    details = new CompactionDetails(0, segmentsUnderCompaction, null);

    CompactionLog cLog = new CompactionLog(tempDirStr, storeName, time, details, config);
    for (LogSegmentName name : segmentsUnderCompaction) {
      addOneIndexSegmentOffsetPair(name, cLog);
    }
    NavigableMap<Offset, Offset> indexSegmentOffsets = cLog.getBeforeAndAfterIndexSegmentOffsets();
    for (LogSegmentName name : segmentsUnderCompaction) {
      Offset before = new Offset(name, LogSegment.HEADER_SIZE);
      Offset expectedAfter = new Offset(name.getNextGenerationName(), LogSegment.HEADER_SIZE);
      Assert.assertEquals(expectedAfter, indexSegmentOffsets.get(before));
    }
    SortedMap<Offset, Offset> indexSegmentOffsetsCompleted =
        cLog.getBeforeAndAfterIndexSegmentOffsetsForCompletedCycles();
    // There is no completedCycle yet
    Assert.assertEquals(0, indexSegmentOffsetsCompleted.size());
    // All log segments are under compaction at current cycle.
    SortedMap<Offset, Offset> indexSegmentOffsetsCurrent = cLog.getBeforeAndAfterIndexSegmentOffsetsForCurrentCycle();
    Assert.assertEquals(segmentsUnderCompaction.size(), indexSegmentOffsetsCurrent.size());

    for (int i = 0; i < segmentsUnderCompaction.size(); i++) {
      LogSegmentName name = segmentsUnderCompaction.get(i);
      cLog.markCopyStart();
      if (i != segmentsUnderCompaction.size() - 1) {
        cLog.splitCurrentCycle(segmentsUnderCompaction.get(i + 1));
      }
      cLog.markCommitStart();
      cLog.markCleanupStart();

      indexSegmentOffsetsCurrent = cLog.getBeforeAndAfterIndexSegmentOffsetsForCurrentCycle();
      Assert.assertEquals(1, indexSegmentOffsetsCurrent.size());
      Assert.assertEquals(new Offset(name, LogSegment.HEADER_SIZE),
          indexSegmentOffsetsCurrent.keySet().iterator().next());

      cLog.close();
      cLog = new CompactionLog(tempDirStr, storeName, STORE_KEY_FACTORY, time, config);

      // Before completing this cycle, get the
      indexSegmentOffsetsCompleted = cLog.getBeforeAndAfterIndexSegmentOffsetsForCompletedCycles();
      Assert.assertEquals(i, indexSegmentOffsetsCompleted.size());
      for (int idx = 0; idx < i; idx++) {
        LogSegmentName sname = segmentsUnderCompaction.get(idx);
        Assert.assertTrue(indexSegmentOffsetsCompleted.containsKey(new Offset(sname, LogSegment.HEADER_SIZE)));
      }
      cLog.markCycleComplete();
      indexSegmentOffsetsCompleted = cLog.getBeforeAndAfterIndexSegmentOffsetsForCompletedCycles();
      Assert.assertEquals(i + 1, indexSegmentOffsetsCompleted.size());
      for (int idx = 0; idx < i + 1; idx++) {
        LogSegmentName sname = segmentsUnderCompaction.get(idx);
        Assert.assertTrue(indexSegmentOffsetsCompleted.containsKey(new Offset(sname, LogSegment.HEADER_SIZE)));
      }

      cLog.close();
      if (i != segmentsUnderCompaction.size() - 1) {
        cLog = new CompactionLog(tempDirStr, storeName, STORE_KEY_FACTORY, time, config);
      } else {
        AtomicReference<CompactionLog> compactionLogRef = new AtomicReference<>();
        CompactionLog.processCompactionLogs(tempDirStr, storeName, STORE_KEY_FACTORY, time, config, compactionLog -> {
          compactionLogRef.set(compactionLog);
          return false;
        });
        cLog = compactionLogRef.get();
      }
      indexSegmentOffsetsCompleted = cLog.getBeforeAndAfterIndexSegmentOffsetsForCompletedCycles();
      Assert.assertEquals(i + 1, indexSegmentOffsetsCompleted.size());
      for (int idx = 0; idx < i + 1; idx++) {
        LogSegmentName sname = segmentsUnderCompaction.get(idx);
        Assert.assertTrue(indexSegmentOffsetsCompleted.containsKey(new Offset(sname, LogSegment.HEADER_SIZE)));
      }
    }
  }
Testing the methods to get the before and after index segment maps
@throws IOException
ambry-store/src/test/java/com/github/ambry/store/CompactionLogTest.java