@Override
  public ImportResult importItem(
      UUID jobId,
      IdempotentImportExecutor executor,
      TokensAndUrlAuthData authData,
      VideosContainerResource data)
      throws Exception {
    if (data == null) {
      // Nothing to do
      return ImportResult.OK;
    }

    PhotosLibraryClient client;
    if (clientsMap.containsKey(jobId)) {
      client = clientsMap.get(jobId);
    } else {
      client = buildPhotosLibraryClient(appCredentials, authData);
      clientsMap.put(jobId, client);
    }

    for (VideoAlbum album : data.getAlbums()) {
      executor.importAndSwallowIOExceptions(album, (a) -> {
        String title = GooglePhotosImportUtils.cleanAlbumTitle(a.getName());
        return ItemImportResult.success(client.createAlbum(title).getId());
      });
    }

    long bytes = 0L;
    //     Uploads videos
    final Collection<VideoModel> videos = data.getVideos();
    if (videos != null && videos.size() > 0) {
      Stream<VideoModel> stream =
          videos.stream()
              .filter(video -> shouldImport(video, executor))
              .map(this::transformVideoName);
      // We partition into groups of 49 as 50 is the maximum number of items that can be created in
      // one call. (We use 49 to avoid potential off by one errors)
      // https://developers.google.com/photos/library/guides/upload-media#creating-media-item
      final UnmodifiableIterator<List<VideoModel>> batches =
          Iterators.partition(stream.iterator(), 49);
      while (batches.hasNext()) {
        long batchBytes = uploadBatchOfVideos(
            jobId,
            batches.next(),
            dataStore,
            client,
            executor,
            connectionProvider,
            monitor);
        bytes += batchBytes;
      }
    }
    final ImportResult result = ImportResult.OK;
    return result.copyWithBytes(bytes);
  }
@Test
  public void importTwoVideosInDifferentAlbums() throws Exception {
    String googleAlbumId = "googleId";
    Album expected = Album.newBuilder().setId(googleAlbumId).setTitle("albumName").build();
    when(client.createAlbum(anyString())).thenReturn(expected);

    // Mock uploads
    when(client.uploadMediaItem(any()))
        .thenReturn(
            UploadMediaItemResponse.newBuilder().setUploadToken("token1").build(),
            UploadMediaItemResponse.newBuilder().setUploadToken("token2").build());

    // Mock creation response
    final NewMediaItemResult newMediaItemResult =
        NewMediaItemResult.newBuilder()
            .setStatus(Status.newBuilder().setCode(Code.OK_VALUE).build())
            .setUploadToken("token1")
            .build();
    final NewMediaItemResult newMediaItemResult2 =
        NewMediaItemResult.newBuilder()
            .setStatus(Status.newBuilder().setCode(Code.OK_VALUE).build())
            .setUploadToken("token2")
            .build();
    BatchCreateMediaItemsResponse response =
        BatchCreateMediaItemsResponse.newBuilder()
            .addNewMediaItemResults(newMediaItemResult)
            .build();
    BatchCreateMediaItemsResponse response2 =
        BatchCreateMediaItemsResponse.newBuilder()
            .addNewMediaItemResults(newMediaItemResult2)
            .build();
    NewMediaItem mediaItem = NewMediaItemFactory.createNewMediaItem("token1", VIDEO_DESCRIPTION);
    NewMediaItem mediaItem2 = NewMediaItemFactory.createNewMediaItem("token2", VIDEO_DESCRIPTION);
    when(client.batchCreateMediaItems(eq(googleAlbumId), eq(List.of(mediaItem))))
        .thenReturn(response);
    when(client.batchCreateMediaItems(eq(List.of(mediaItem2))))
        .thenReturn(response2);

    InMemoryIdempotentImportExecutor executor =
        new InMemoryIdempotentImportExecutor(mock(Monitor.class));
    long length =
        googleVideosImporter
            .importItem(
                jobId,
                executor,
                mock(TokensAndUrlAuthData.class),
                new VideosContainerResource(
                    List.of(new VideoAlbum(ALBUM_ID, "name", null)),
                    List.of(
                        new VideoModel(
                            VIDEO_TITLE,
                            VIDEO_URI,
                            VIDEO_DESCRIPTION,
                            MP4_MEDIA_TYPE,
                            VIDEO_ID,
                            ALBUM_ID,
                            false,
                            null),
                        new VideoModel(
                            VIDEO_TITLE,
                            VIDEO_URI,
                            VIDEO_DESCRIPTION,
                            MP4_MEDIA_TYPE,
                            "myId2",
                            null,
                            false,
                            null))))
            .getBytes()
            .get();
    assertEquals(64L, length,"Expected the number of bytes to be the two files of 32L.");
    assertEquals(0, executor.getErrors().size(),"Expected executor to have no errors.");
  }