@Test
  public void testFileCopiedIndexFilesAreValid() throws Exception {
    // Arrange -
    // 1. Clean up source and target directories
    // 2. Create 2 index files in the target directory. Index file 1 is of size 1KB and index file 2 is of size 50KB.
    // 3. Set up Spy on FCHandler with FileCopyGetMetaDataResponse to return the index files.
    // 4. Set up Spy on FCHandler with FileCopyGetChunkResponse to return the index files.
    cleanUpDirectories(Arrays.asList(sourcePartitionDir, targetPartitionDir));

    LogSegmentName logSegmentName = getRandomLogSegmentName();
    String logSegmentFileName = logSegmentName + "_log";
    String indexFileName1 = logSegmentName + "_1_index";
    String indexFileName2 = logSegmentName + "_2_index";

    File logSegment = new File(targetPartitionDir, logSegmentFileName);
    File indexFile1 = new File(targetPartitionDir, indexFileName1);
    File indexFile2 = new File(targetPartitionDir, indexFileName2);

    createSampleFile(indexFile1, 1024); // 1 KB
    createSampleFile(indexFile2, 50 * 1024); // 50KB

    List<FileInfo> indexInfos = Arrays.asList(new StoreFileInfo[] {
        new StoreFileInfo(indexFileName1, indexFile1.length()),
        new StoreFileInfo(indexFileName2, indexFile2.length())
    });
    StoreLogInfo storeLogInfo = new StoreLogInfo(new StoreFileInfo(logSegmentFileName, logSegment.length()),
        indexInfos, new ArrayList<>());
    FileCopyGetMetaDataResponse fileCopyGetMetaDataResponse = new FileCopyGetMetaDataResponse(
        FileCopyGetMetaDataResponse.FILE_COPY_PROTOCOL_METADATA_RESPONSE_VERSION_V_1, 0, "clientId",
        1, Collections.singletonList(storeLogInfo), "snapshotId",
        ServerErrorCode.NoError, null);

    StoreFileCopyHandler spyHandler = spy(super.handler);
    doReturn(fileCopyGetMetaDataResponse)
        .when(spyHandler)
        .getFileCopyGetMetaDataResponse(any());

    final FileCopyGetChunkResponse fileCopyGetChunkResponseForIndexFile = getFileCopyGetChunkResponse(indexFile1);
    final FileCopyGetChunkResponse fileCopyGetChunkResponseForIndexFile2 = getFileCopyGetChunkResponse(indexFile2);

    doReturn(fileCopyGetChunkResponseForIndexFile).
    doReturn(fileCopyGetChunkResponseForIndexFile2)
        .when(spyHandler)
        .getFileCopyGetChunkResponse(any(), any(), any(), any(), anyBoolean());

    // Act - Run FCHandler.copy using Spy handler
    // TODO: Temporary directory should have been created before FileCopyHandler.copy() is called.
    File fileCopyTempDirectory = new File(sourcePartitionDir, "fileCopyTempDirectory");
    assertFalse("Temporary directory already exists!", fileCopyTempDirectory.exists());
    assertTrue(fileCopyTempDirectory.mkdirs());
    spyHandler.copy(fileCopyInfo);
    assertTrue(fileCopyTempDirectory.delete());

    String targetIndexFileName1 = fileStore.resetCompactionCycleIndexInFileName(indexFileName1);
    String targetIndexFileName2 = fileStore.resetCompactionCycleIndexInFileName(indexFileName2);
    // Assert -
    // 1. Check that no sub-directories are created in the source directory
    // 2. Check that the index files are copied correctly to the source directory
    // 3. Check that the number of index files in the source directory is 2
    assertNoSubDirectories(sourcePartitionDir);
    assertCopiedFileIsValid(indexFile1, new File(sourcePartitionDir, targetIndexFileName1));
    assertCopiedFileIsValid(indexFile2, new File(sourcePartitionDir, targetIndexFileName2));
    assertEquals(2, getNumberOfFilesMatchingSuffix(sourcePartitionDir, "_index"));
  }
Tests that the file copy handler can copy files from source to target.
It creates index files in the target directory,
and then verifies that the files are copied correctly to the source directory.
ambry-file-transfer/src/test/java/com/github/ambry/filetransfer/handler/StoreFileCopyHandlerIntegTest.java