@Override
  public ImportResult importItem(
      UUID jobId,
      IdempotentImportExecutor idempotentImportExecutor,
      TokensAndUrlAuthData authData,
      VideosContainerResource resource)
      throws Exception {
    KoofrClient koofrClient = koofrClientFactory.create(authData);

    monitor.debug(
        () ->
            String.format(
                "%s: Importing %s albums and %s videos",
                jobId, resource.getAlbums().size(), resource.getVideos().size()));

    // TODO: VideosContainerResource does not support transmogrification

    for (VideoAlbum album : resource.getAlbums()) {
      // Create a Koofr folder and then save the id with the mapping data
      idempotentImportExecutor.executeAndSwallowIOExceptions(
          album.getId(), album.getName(), () -> createAlbumFolder(album, koofrClient));
    }

    final LongAdder totalImportedFilesSizes = new LongAdder();
    for (VideoModel videoModel : resource.getVideos()) {
      idempotentImportExecutor.importAndSwallowIOExceptions(
          videoModel,
          video -> {
            ItemImportResult<String> fileImportResult =
                importSingleVideo(videoModel, jobId, idempotentImportExecutor, koofrClient);
            if (fileImportResult != null && fileImportResult.hasBytes()) {
              totalImportedFilesSizes.add(fileImportResult.getBytes());
            }
            return fileImportResult;
          });
    }
    return ImportResult.OK.copyWithBytes(totalImportedFilesSizes.longValue());
  }
@Test
  public void testImportItemFromURLWithAlbum() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(200).setBody("123"));
    server.enqueue(new MockResponse().setResponseCode(200).setBody("4567"));
    server.enqueue(new MockResponse().setResponseCode(200).setBody("89"));

    when(client.ensureRootFolder()).thenReturn("/root");
    when(executor.getCachedValue(eq("id1"))).thenReturn("/root/Album 1");
    when(executor.getCachedValue(eq("id2"))).thenReturn("/root/Album");

    when(client.fileExists("/root/Album 1/video1.mp4")).thenReturn(false);
    when(client.fileExists("/root/Album 1/video2.mp4")).thenReturn(true);
    when(client.fileExists("/root/Album/video3.mp4")).thenReturn(false);

    String description1000 = new String(new char[1000]).replace("\0", "a");
    String description1001 = new String(new char[1001]).replace("\0", "a");

    UUID jobId = UUID.randomUUID();
    Collection<VideoAlbum> albums =
        ImmutableList.of(
            new VideoAlbum("id1", "Album 1", "This is a fake album"),
            new VideoAlbum("id2", "", description1001));

    Date uploadedTime = Date.from(Instant.parse("2020-09-04T12:40:57.741Z"));
    Collection<VideoModel> videos =
        ImmutableList.of(
            new VideoModel(
                "video1.mp4",
                server.url("/1.mp4").toString(),
                "A video 1",
                "video/mp4",
                "video1",
                "id1",
                false,
                uploadedTime),
            new VideoModel(
                "video2.mp4",
                server.url("/2.mp4").toString(),
                "A video 2",
                "video/mp4",
                "video2",
                "id1",
                false,
                null),
            new VideoModel(
                "video3.mp4",
                server.url("/3.mp4").toString(),
                description1001,
                "video/mp4",
                "video3",
                "id2",
                false,
                null));

    VideosContainerResource resource = spy(new VideosContainerResource(albums, videos));

    importer.importItem(jobId, executor, authData, resource);

    InOrder clientInOrder = Mockito.inOrder(client);

    clientInOrder.verify(client).ensureRootFolder();
    clientInOrder.verify(client).ensureFolder("/root", "Album 1");
    clientInOrder.verify(client).addDescription("/root/Album 1", "This is a fake album");
    clientInOrder.verify(client).ensureFolder("/root", "Album");
    clientInOrder.verify(client).addDescription("/root/Album", description1000);
    clientInOrder.verify(client).fileExists(eq("/root/Album 1/video1.mp4"));
    clientInOrder
        .verify(client)
        .uploadFile(
            eq("/root/Album 1"),
            eq("video1.mp4"),
            any(),
            eq("video/mp4"),
            eq(uploadedTime),
            eq("A video 1"));
    clientInOrder.verify(client).fileExists(eq("/root/Album 1/video2.mp4"));
    clientInOrder.verify(client).fileExists(eq("/root/Album/video3.mp4"));
    clientInOrder
        .verify(client)
        .uploadFile(
            eq("/root/Album"),
            eq("video3.mp4"),
            any(),
            eq("video/mp4"),
            isNull(),
            eq(description1000));
    clientInOrder.verifyNoMoreInteractions();
  }