@Override
  public ImportResult importItem(
      UUID jobId,
      IdempotentImportExecutor idempotentImportExecutor,
      TokensAndUrlAuthData authData,
      VideosContainerResource data)
      throws Exception {
    if (data == null) {
      // Nothing to do
      return ImportResult.OK;
    }
    AppleMediaInterface mediaInterface = factory
      .getOrCreateMediaInterface(jobId, authData, appCredentials, exportingService, monitor);

    // Uploads album metadata
    final int albumCount =
        mediaInterface.importAlbums(
            jobId,
            idempotentImportExecutor,
            data.getAlbums().stream()
                .map(MediaAlbum::videoToMediaAlbum)
                .collect(Collectors.toList()),
          DataVertical.PHOTOS.getDataType());
    final Map<String, Long> importPhotosResult =
        mediaInterface.importAllMedia(
            jobId,
            idempotentImportExecutor,
            data.getVideos(),
          DataVertical.VIDEOS.getDataType());

    // generate import result
    final ImportResult result = ImportResult.OK;
    final Map<String, Integer> counts =
        new ImmutableMap.Builder<String, Integer>()
            .put(PhotosContainerResource.ALBUMS_COUNT_DATA_NAME, albumCount)
            .put(
                VideosContainerResource.VIDEOS_COUNT_DATA_NAME,
                importPhotosResult.get(ApplePhotosConstants.COUNT_KEY).intValue())
            .build();
    return result
        .copyWithBytes(importPhotosResult.get(ApplePhotosConstants.BYTES_KEY))
        .copyWithCounts(counts);
  }
@Test
  public void importVideosWithFailure() throws Exception {
    // set up
    final int videoCount = ApplePhotosConstants.maxNewMediaRequests + 1;
    ;
    final List<VideoModel> videos = createTestVideos(videoCount);
    final int errorCountGetUploadURL = 10;
    final int errorCountUploadContent = 10;
    final int errorCountCreateMedia = 10;
    final int successCount =
        videoCount - errorCountGetUploadURL - errorCountUploadContent - errorCountCreateMedia;
    final List<String> dataIds =
        videos.stream().map(VideoModel::getDataId).collect(Collectors.toList());
    final Map<String, Integer> datatIdToGetUploadURLStatus =
        setUpErrors(dataIds, 0, errorCountGetUploadURL);
    final Map<String, Integer> datatIdToUploadContentStatus =
        setUpErrors(dataIds, errorCountGetUploadURL, errorCountUploadContent);
    final Map<String, Integer> datatIdToCreateMediaStatus =
        setUpErrors(
            dataIds, errorCountGetUploadURL + errorCountUploadContent, errorCountCreateMedia);
    setUpGetUploadUrlResponse(datatIdToGetUploadURLStatus);
    setUpUploadContentResponse(datatIdToUploadContentStatus);
    setUpCreateMediaResponse(datatIdToCreateMediaStatus);

    // run test
    VideosContainerResource data = new VideosContainerResource(null, videos);
    final ImportResult importResult =
        appleVideosImporter.importItem(uuid, executor, authData, data);

    // verify correct methods were called
    verify(mediaInterface, times(2)).getUploadUrl(anyString(), anyString(), anyList());
    verify(mediaInterface)
        .getUploadUrl(
            uuid.toString(),
            DataVertical.VIDEOS.getDataType(),
            videos.subList(0, ApplePhotosConstants.maxNewMediaRequests).stream()
                .map(VideoModel::getDataId)
                .collect(Collectors.toList()));
    verify(mediaInterface)
        .getUploadUrl(
            uuid.toString(),
            DataVertical.VIDEOS.getDataType(),
            videos.subList(ApplePhotosConstants.maxNewMediaRequests, videoCount).stream()
                .map(VideoModel::getDataId)
                .collect(Collectors.toList()));

    verify(mediaInterface, times(2)).uploadContent(anyMap(), anyList());
    verify(mediaInterface, times(2)).createMedia(anyString(), anyString(), anyList());

    // check the result
    assertThat(importResult.getCounts().isPresent()).isTrue();
    assertThat(importResult.getCounts().get().get(VIDEOS_COUNT_DATA_NAME) == successCount).isTrue();
    assertThat(importResult.getBytes().get() == successCount * VIDEOS_FILE_SIZE).isTrue();

    final Map<String, Serializable> expectedKnownValue =
        videos.stream()
            .filter(
                VideoModel ->
                    datatIdToGetUploadURLStatus.get(VideoModel.getDataId()) == SC_OK)
            .filter(
                VideoModel ->
                    datatIdToUploadContentStatus.get(VideoModel.getDataId()) == SC_OK)
            .filter(
                VideoModel ->
                    datatIdToCreateMediaStatus.get(VideoModel.getDataId()) == SC_OK)
            .collect(
                Collectors.toMap(
                    video -> video.getAlbumId() + "-" +video.getDataId(),
                    video -> VIDEOS_DATAID_BASE + video.getDataId()));
    checkKnownValues(expectedKnownValue);

    // check errors
    List<ErrorDetail> expectedErrors = new ArrayList<>();
    for (int i = 0;
        i < errorCountGetUploadURL + errorCountUploadContent + errorCountCreateMedia;
        i++) {
      final VideoModel video = videos.get(i);
      final ErrorDetail.Builder errorDetailBuilder =
          ErrorDetail.builder()
              .setId(video.getIdempotentId())
              .setTitle(video.getName())
              .setException(
                  String.format(
                      "java.io.IOException: %s Fail to get upload url, errorCode:%d",
                      ApplePhotosConstants.APPLE_PHOTOS_IMPORT_ERROR_PREFIX,
                      SC_INTERNAL_SERVER_ERROR));
      if (i < errorCountGetUploadURL) {
        errorDetailBuilder.setException(
            String.format(
                "java.io.IOException: %s Fail to get upload url, errorCode:%d",
                ApplePhotosConstants.APPLE_PHOTOS_IMPORT_ERROR_PREFIX,
                SC_INTERNAL_SERVER_ERROR));
      } else if (i < errorCountGetUploadURL + errorCountGetUploadURL) {
        errorDetailBuilder.setException(String.format(
                "java.io.IOException: %s Fail to upload content",
                ApplePhotosConstants.APPLE_PHOTOS_IMPORT_ERROR_PREFIX));
      } else {
        errorDetailBuilder.setException(
            String.format(
                "java.io.IOException: %s Fail to create media, errorCode:%d",
                    ApplePhotosConstants.APPLE_PHOTOS_IMPORT_ERROR_PREFIX,
                    SC_INTERNAL_SERVER_ERROR));
      }
      expectedErrors.add(errorDetailBuilder.build());
    }

    checkErrors(expectedErrors);
    checkRecentErrors(expectedErrors);
  }