@Test
  public void inventoryExistingFilesAndInitTest() throws Exception {
    assertTrue("Couldn't create reserve file directory", reserveFileDir.mkdir());
    // create valid existing store reserve directory and swap directory
    File storeReserveDir = new File(reserveFileDir, DiskSpaceAllocator.STORE_DIR_PREFIX + storeId0);
    File swapReserveDir = new File(reserveFileDir, DiskSpaceAllocator.SWAP_DIR_NAME);
    assertTrue("Couldn't create store reserve directory", storeReserveDir.mkdir());
    assertTrue("Couldn't create swap reserve directory", swapReserveDir.mkdir());
    // create sub-directories in store and swap reserve directories
    File fileSizeDir1 = new File(storeReserveDir, DiskSpaceAllocator.FILE_SIZE_DIR_PREFIX + 50);
    File fileSizeDir2 = new File(swapReserveDir, DiskSpaceAllocator.FILE_SIZE_DIR_PREFIX + 30);
    File fileSizeDir3 = new File(swapReserveDir, DiskSpaceAllocator.FILE_SIZE_DIR_PREFIX + 50);
    assertTrue("Couldn't create file size directory", fileSizeDir1.mkdir());
    assertTrue("Couldn't create file size directory", fileSizeDir2.mkdir());
    assertTrue("Couldn't create file size directory", fileSizeDir3.mkdir());
    // create files in above directories
    File reserveFile1 = new File(fileSizeDir1, DiskSpaceAllocator.RESERVE_FILE_PREFIX + UUID.randomUUID());
    File reserveFile2 = new File(fileSizeDir2, DiskSpaceAllocator.RESERVE_FILE_PREFIX + UUID.randomUUID());
    File reserveFile3 = new File(fileSizeDir3, DiskSpaceAllocator.RESERVE_FILE_PREFIX + UUID.randomUUID());
    assertTrue("Couldn't create file", reserveFile1.createNewFile());
    assertTrue("Couldn't create file", reserveFile2.createNewFile());
    assertTrue("Couldn't create file", reserveFile3.createNewFile());
    // create invalid directory and file for testing
    File invalidDir = new File(reserveFileDir, DiskSpaceAllocator.FILE_SIZE_DIR_PREFIX + 50);
    File invalidFile = new File(reserveFileDir, DiskSpaceAllocator.RESERVE_FILE_PREFIX + UUID.randomUUID());
    assertTrue(invalidDir.mkdir());
    assertTrue(invalidFile.createNewFile());
    // create extra file inside invalid directory to mock non-empty directory
    File extraFile = new File(invalidDir, DiskSpaceAllocator.RESERVE_FILE_PREFIX + UUID.randomUUID());
    assertTrue(extraFile.createNewFile());
    // create an extra store reserve dir (storeId1) to test if DSA can correctly delete the unrecognized dir
    File extraStoreReserveDir = new File(reserveFileDir, DiskSpaceAllocator.STORE_DIR_PREFIX + storeId1);
    assertTrue("Couldn't create an extra store reserve directory", extraStoreReserveDir.mkdir());
    // create size dir and put some files into this extra store reserve dir
    File sizeDir = new File(extraStoreReserveDir, DiskSpaceAllocator.FILE_SIZE_DIR_PREFIX + 50);
    assertTrue("Couldn't create file size directory in extra store reserve dir", sizeDir.mkdir());
    File randomFile = new File(sizeDir, DiskSpaceAllocator.RESERVE_FILE_PREFIX + UUID.randomUUID());
    assertTrue("Couldn't create random file in extra store reserve dir", randomFile.createNewFile());
    // instantiate DiskSpaceAllocator
    alloc = constructAllocator();
    // verify invalid directories and files no longer exist
    assertFalse("Invalid directory shouldn't exist after inventory", invalidDir.exists());
    assertFalse("Invalid file shouldn't exist after inventory", invalidFile.exists());
    // verify inventoried in-mem store/swap reserve file maps match those existing valid files
    Map<String, DiskSpaceAllocator.ReserveFileMap> storeFileMap = alloc.getStoreReserveFileMap();
    // note that, after inventory, store reserve file map contains extra storeId1, which will be deleted when initializePool is called.
    assertTrue("Store reserve file map doesn't match existing directory/file",
        storeFileMap.containsKey(storeId0) && storeFileMap.containsKey(storeId1) && storeFileMap.size() == 2);
    DiskSpaceAllocator.ReserveFileMap swapFileMap = alloc.getSwapReserveFileMap();
    assertTrue("Swap reserve file map doesn't match existing directory/file",
        swapFileMap.getCount(30) == 1 && swapFileMap.getFileSizeSet().size() == 2);
    assertTrue("Swap reserve file map doesn't match existing directory/file",
        swapFileMap.getCount(50) == 1 && swapFileMap.getFileSizeSet().size() == 2);

    // initialize pool to verify extra swap segment is deleted and required store segments are added.
    // before init: store reserve pool has reserve_50 segment; swap reserve pool has reserve_30, reserve_50
    // after init (expected state): store reserve pool has reserve_30 segment; swap reserve pool is empty.
    alloc.initializePool(Collections.singletonList(new DiskSpaceRequirements(storeId0, 30, 1, 1)));
    swapFileMap = alloc.getSwapReserveFileMap();
    assertNull("The swap segment should not exist in in-mem swap map", swapFileMap.remove(30));
    assertNull("The swap segment should not exist in in-mem swap map", swapFileMap.remove(50));
    storeFileMap = alloc.getStoreReserveFileMap();
    assertTrue("Store reserve file map should not contain storeId1 dir",
        !storeFileMap.containsKey(storeId1) && storeFileMap.size() == 1);
    assertNull("The store segment with size 50 should not exist in in-mem store file map",
        storeFileMap.get(storeId0).remove(50));
    assertNotNull("The store segment with size 30 should exist", storeFileMap.get(storeId0).remove(30));
  }
Test that {@link DiskSpaceAllocator#inventoryExistingReserveFiles()} can correctly delete invalid directories/files
and inventory valid ones.
@throws Exception
ambry-store/src/test/java/com/github/ambry/store/DiskSpaceAllocatorTest.java