@Override
    public ImportResult importItem(
            UUID jobId,
            IdempotentImportExecutor idempotentExecutor,
            AuthData authData,
            MediaContainerResource data)
            throws Exception, IOException {
        Auth auth;
        try {
            auth = FlickrUtils.getAuth(authData, flickr);
        } catch (FlickrException e) {
            return new ImportResult(e);
        }
        RequestContext.getRequestContext().setAuth(auth);

        Preconditions.checkArgument(
                data.getAlbums() != null || data.getPhotos() != null || data.getVideos() != null,
                "Error: There is no data to import");

        if (data.getAlbums() != null) {
            storeAlbums(jobId, data.getAlbums());
        }

        if (data.getPhotos() != null) {
            for (PhotoModel photo : data.getPhotos()) {
                try {
                    importSinglePhoto(idempotentExecutor, jobId, photo);
                } catch (FlickrException e) {
                    if (e.getMessage().contains("Upload limit reached")) {
                        throw new DestinationMemoryFullException("Flickr destination memory reached", e);
                    } else if (e.getMessage().contains("Photo already in set")) {
                        // This can happen if we got a server error on our end, but the request went through.
                        // When our retry strategy kicked in the request was complete and the photo already
                        // uploaded
                        continue;
                    }
                    throw new IOException(e);
                }
            }
        }

        if (data.getVideos() != null) {
            for (VideoModel video : data.getVideos()) {
                try {
                    importSingleVideo(idempotentExecutor, jobId, video);
                } catch (FlickrException e) {
                    if (e.getMessage().contains("Upload limit reached")) {
                        throw new DestinationMemoryFullException("Flickr destination memory reached", e);
                    } else if (e.getMessage().contains("Photo already in set")) {
                        // This can happen if we got a server error on our end, but the request went through.
                        // When our retry strategy kicked in the request was complete and the photo already
                        // uploaded
                        continue;
                    }
                    throw new IOException(e);
                }
            }
        }

        return new ImportResult(ImportResult.ResultType.OK);
    }
@Test
    public void importStoresAlbumInJobStore() throws FlickrException, Exception {
        UUID jobId = UUID.randomUUID();

        MediaContainerResource mediaContainerResource =
                new MediaContainerResource(
                        Collections.singletonList(Media_ALBUM), Collections.singletonList(PHOTO_MODEL), Collections.singletonList(VIDEO_MODEL));

        // Setup Mock
        when(user.getId()).thenReturn("userId");
        when(authInterface.checkToken(any(OAuth1Token.class))).thenReturn(auth);

        when(flickr.getPhotosetsInterface()).thenReturn(photosetsInterface);
        when(flickr.getUploader()).thenReturn(uploader);
        when(flickr.getAuthInterface()).thenReturn(authInterface);
        when(imageStreamProvider.get(FETCHABLE_URL)).thenReturn(bufferedInputStream);
        when(imageStreamProvider.get(VIDEO_FETCHABLE_URL)).thenReturn(bufferedInputStream);
        when(uploader.upload(any(BufferedInputStream.class), refEq(PHOTO_UPLOAD_META_DATA)))
                .thenReturn(FLICKR_PHOTO_ID);
        when(uploader.upload(any(BufferedInputStream.class), refEq(VIDEO_UPLOAD_META_DATA)))
                .thenReturn(FLICKR_VIDEO_ID);

        String flickrAlbumTitle = ALBUM_NAME;
        Photoset photoset =
                FlickrTestUtils.initializePhotoset(FLICKR_ALBUM_ID, ALBUM_DESCRIPTION, FLICKR_PHOTO_ID);
        when(photosetsInterface.create(flickrAlbumTitle, ALBUM_DESCRIPTION, FLICKR_PHOTO_ID))
                .thenReturn(photoset);

        // Run test
        FlickrMediaImporter importer =
                new FlickrMediaImporter(
                        flickr,
                        jobStore,
                        imageStreamProvider,
                        monitor,
                        TransferServiceConfig.getDefaultInstance());
        ImportResult result =
                importer.importItem(
                        jobId, EXECUTOR, new TokenSecretAuthData("token", "secret"), mediaContainerResource);

        // Verify that the image stream provider got the correct URL and that the correct info was
        // uploaded
        verify(imageStreamProvider).get(FETCHABLE_URL);
        ArgumentCaptor<UploadMetaData> uploadMetaDataArgumentCaptor =
                ArgumentCaptor.forClass(UploadMetaData.class);
        verify(uploader, times(2)).upload(eq(bufferedInputStream), uploadMetaDataArgumentCaptor.capture());
        List<UploadMetaData> actualUploadMetaData = uploadMetaDataArgumentCaptor.getAllValues();

        assertThat(actualUploadMetaData.get(0).getTitle()).isEqualTo(PHOTO_TITLE);
        assertThat(actualUploadMetaData.get(0).getDescription()).isEqualTo(PHOTO_DESCRIPTION);
        assertThat(actualUploadMetaData.get(1).getTitle()).isEqualTo(VIDEO_TITLE);
        assertThat(actualUploadMetaData.get(1).getDescription()).isEqualTo(VIDEO_DESCRIPTION);

        // Verify the photosets interface got the command to create the correct album
        verify(photosetsInterface).create(flickrAlbumTitle, ALBUM_DESCRIPTION, FLICKR_PHOTO_ID);

        assertThat((String) EXECUTOR.getCachedValue(ALBUM_ID)).isEqualTo(FLICKR_ALBUM_ID);
    }