Search in sources :

Example 21 with PortabilityJob

use of org.datatransferproject.spi.cloud.types.PortabilityJob in project data-transfer-project by google.

the class KoofrPhotosImporterTest method testImportItemFromURL.

@Test
public void testImportItemFromURL() throws Exception {
    // blank.jpg generated using
    // convert -size 1x1 xc:white blank.jpg
    // exiftool "-CreateDate=2020:08:03 11:55:24" blank.jpg
    final byte[] blankBytes = IOUtils.toByteArray(getClass().getClassLoader().getResourceAsStream("blank.jpg"));
    server.enqueue(new MockResponse().setResponseCode(200).setBody("123"));
    server.enqueue(new MockResponse().setResponseCode(200).setBody("4567"));
    final Buffer blankBuffer = new Buffer();
    blankBuffer.write(blankBytes);
    server.enqueue(new MockResponse().setResponseCode(200).setBody(blankBuffer));
    blankBuffer.close();
    server.enqueue(new MockResponse().setResponseCode(200).setBody("89"));
    server.enqueue(new MockResponse().setResponseCode(200).setBody("0"));
    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/pic1.jpg")).thenReturn(false);
    when(client.fileExists("/root/Album 1/pic2.png")).thenReturn(true);
    when(client.fileExists("/root/Album 1/2020-08-03 11.55.24 pic3.jpg")).thenReturn(false);
    when(client.fileExists("/root/Album 1/2020-08-17 11.55.24 pic4.jpg")).thenReturn(false);
    when(client.fileExists("/root/Album/pic5.jpg")).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();
    PortabilityJob job = mock(PortabilityJob.class);
    when(jobStore.findJob(jobId)).thenReturn(job);
    Collection<PhotoAlbum> albums = ImmutableList.of(new PhotoAlbum("id1", "Album 1", "This is a fake album"), new PhotoAlbum("id2", "", description1001));
    Collection<PhotoModel> photos = ImmutableList.of(new PhotoModel("pic1.jpg", server.url("/1.jpg").toString(), null, "image/jpeg", "p1", "id1", false, null), new PhotoModel("pic2.png", server.url("/2.png").toString(), "fine art", "image/png", "p2", "id1", false, null), new PhotoModel("pic3.jpg", server.url("/3.jpg").toString(), "A pic with EXIF", "image/jpeg", "p3", "id1", false, null), new PhotoModel("pic4.jpg", server.url("/4.jpg").toString(), "A pic with uploaded time", "image/jpeg", "p4", "id1", false, new SimpleDateFormat("yyyy:MM:dd HH:mm:ss").parse("2020:08:17 11:55:24")), new PhotoModel("pic5.jpg", server.url("/5.jpg").toString(), description1001, "image/jpeg", "p5", "id2", false, null));
    PhotosContainerResource resource = spy(new PhotosContainerResource(albums, photos));
    importer.importItem(jobId, executor, authData, resource);
    InOrder clientInOrder = Mockito.inOrder(client);
    verify(resource).transmogrify(any(KoofrTransmogrificationConfig.class));
    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/pic1.jpg"));
    clientInOrder.verify(client).uploadFile(eq("/root/Album 1"), eq("pic1.jpg"), any(), eq("image/jpeg"), isNull(), isNull());
    clientInOrder.verify(client).fileExists(eq("/root/Album 1/pic2.png"));
    clientInOrder.verify(client).fileExists(eq("/root/Album 1/2020-08-03 11.55.24 pic3.jpg"));
    clientInOrder.verify(client).uploadFile(eq("/root/Album 1"), eq("2020-08-03 11.55.24 pic3.jpg"), any(), eq("image/jpeg"), eq(new SimpleDateFormat("yyyy:MM:dd HH:mm:ss").parse("2020:08:03 11:55:24")), eq("A pic with EXIF"));
    clientInOrder.verify(client).uploadFile(eq("/root/Album 1"), eq("2020-08-17 11.55.24 pic4.jpg"), any(), eq("image/jpeg"), eq(new SimpleDateFormat("yyyy:MM:dd HH:mm:ss").parse("2020:08:17 11:55:24")), eq("A pic with uploaded time"));
    clientInOrder.verify(client).fileExists(eq("/root/Album/pic5.jpg"));
    clientInOrder.verify(client).uploadFile(eq("/root/Album"), eq("pic5.jpg"), any(), eq("image/jpeg"), isNull(), eq(description1000));
    clientInOrder.verifyNoMoreInteractions();
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) InOrder(org.mockito.InOrder) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PortabilityJob(org.datatransferproject.spi.cloud.types.PortabilityJob) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) KoofrTransmogrificationConfig(org.datatransferproject.transfer.koofr.KoofrTransmogrificationConfig) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) UUID(java.util.UUID) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 22 with PortabilityJob

use of org.datatransferproject.spi.cloud.types.PortabilityJob in project data-transfer-project by google.

the class GenerateServiceAuthDataAction method handle.

public ServiceAuthData handle(GenerateServiceAuthData request) {
    try {
        String id = request.getId();
        Preconditions.checkNotNull(id, "transfer job ID required for GenerateServiceAuthDataAction");
        UUID jobId = decodeJobId(id);
        Preconditions.checkNotNull(request.getAuthToken(), "Auth token required for GenerateServiceAuthDataAction, transfer job ID: %s", jobId);
        PortabilityJob job = jobStore.findJob(jobId);
        Preconditions.checkNotNull(job, "existing job not found for transfer job ID: %s", jobId);
        // TODO: Determine service from job or from authUrl path?
        AuthMode authMode = GenerateServiceAuthData.Mode.EXPORT == request.getMode() ? AuthMode.EXPORT : AuthMode.IMPORT;
        String service = (authMode == AuthMode.EXPORT) ? job.exportService() : job.importService();
        AuthDataGenerator generator = registry.getAuthDataGenerator(service, job.transferDataType(), authMode);
        // Obtain the session key for this job
        String encodedSessionKey = job.jobAuthorization().sessionSecretKey();
        SecretKey key = symmetricKeyGenerator.parse(BaseEncoding.base64Url().decode(encodedSessionKey));
        // Retrieve initial auth data, if it existed
        AuthData initialAuthData = null;
        String encryptedInitialAuthData = (authMode == AuthMode.EXPORT) ? job.jobAuthorization().encryptedInitialExportAuthData() : job.jobAuthorization().encryptedInitialImportAuthData();
        if (encryptedInitialAuthData != null) {
            // Retrieve and parse the session key from the job
            // Decrypt and deserialize the object
            String serialized = decrypterFactory.create(key).decrypt(encryptedInitialAuthData);
            initialAuthData = objectMapper.readValue(serialized, AuthData.class);
        }
        // TODO: Use UUID instead of UUID.toString()
        // Generate auth data
        AuthData authData = generator.generateAuthData(request.getCallbackUrl(), request.getAuthToken(), jobId.toString(), initialAuthData, null);
        Preconditions.checkNotNull(authData, "Auth data should not be null");
        monitor.debug(() -> format("Generated auth data in mode '%s' for job: %s", authMode, jobId), jobId, EventCode.API_GENERATED_AUTH_DATA);
        // Serialize the auth data
        String serialized = objectMapper.writeValueAsString(authData);
        return new ServiceAuthData(serialized);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : PortabilityJob(org.datatransferproject.spi.cloud.types.PortabilityJob) AuthDataGenerator(org.datatransferproject.spi.api.auth.AuthDataGenerator) SecretKey(javax.crypto.SecretKey) ServiceAuthData(org.datatransferproject.types.client.transfer.ServiceAuthData) GenerateServiceAuthData(org.datatransferproject.types.client.transfer.GenerateServiceAuthData) AuthData(org.datatransferproject.types.transfer.auth.AuthData) ServiceAuthData(org.datatransferproject.types.client.transfer.ServiceAuthData) GenerateServiceAuthData(org.datatransferproject.types.client.transfer.GenerateServiceAuthData) IOException(java.io.IOException) UUID(java.util.UUID) AuthMode(org.datatransferproject.spi.api.auth.AuthServiceProviderRegistry.AuthMode)

Example 23 with PortabilityJob

use of org.datatransferproject.spi.cloud.types.PortabilityJob in project data-transfer-project by google.

the class GetTransferJobAction method handle.

@Override
public TransferJob handle(GetTransferJob transferRequest) {
    String id = transferRequest.getId();
    Preconditions.checkNotNull(id, "transfer job ID required for GetTransferJobAction");
    UUID jobId = decodeJobId(id);
    PortabilityJob job = jobStore.findJob(jobId);
    monitor.debug(() -> format("Fetched job with jobId: %s", jobId), jobId, EventCode.API_GOT_TRANSFER_JOB);
    // TODO(#553): This list of nulls should be cleaned up when we refactor TransferJob.
    return new TransferJob(id, job.exportService(), job.importService(), job.transferDataType(), null, null, null, null, null, null);
}
Also used : PortabilityJob(org.datatransferproject.spi.cloud.types.PortabilityJob) UUID(java.util.UUID) TransferJob(org.datatransferproject.types.client.transfer.TransferJob) GetTransferJob(org.datatransferproject.types.client.transfer.GetTransferJob)

Example 24 with PortabilityJob

use of org.datatransferproject.spi.cloud.types.PortabilityJob in project data-transfer-project by google.

the class GooglePhotosImporter method getOrCreateStringDictionary.

private synchronized BaseMultilingualDictionary getOrCreateStringDictionary(UUID jobId) {
    if (!multilingualStrings.containsKey(jobId)) {
        PortabilityJob job = jobStore.findJob(jobId);
        String locale = job != null ? job.userLocale() : null;
        multilingualStrings.put(jobId, new BaseMultilingualDictionary(locale));
    }
    return multilingualStrings.get(jobId);
}
Also used : PortabilityJob(org.datatransferproject.spi.cloud.types.PortabilityJob) BaseMultilingualDictionary(org.datatransferproject.spi.transfer.i18n.BaseMultilingualDictionary)

Aggregations

PortabilityJob (org.datatransferproject.spi.cloud.types.PortabilityJob)24 UUID (java.util.UUID)11 IOException (java.io.IOException)8 JobAuthorization (org.datatransferproject.spi.cloud.types.JobAuthorization)6 Test (org.junit.Test)6 PhotoAlbum (org.datatransferproject.types.common.models.photos.PhotoAlbum)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 SimpleDateFormat (java.text.SimpleDateFormat)3 TransferJob (org.datatransferproject.types.client.transfer.TransferJob)3 PhotoModel (org.datatransferproject.types.common.models.photos.PhotoModel)3 PhotosContainerResource (org.datatransferproject.types.common.models.photos.PhotosContainerResource)3 InOrder (org.mockito.InOrder)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DateFormat (java.text.DateFormat)2 SecretKey (javax.crypto.SecretKey)2 LocalJobStore (org.datatransferproject.cloud.local.LocalJobStore)2 GoogleAlbum (org.datatransferproject.datatransfer.google.mediaModels.GoogleAlbum)2 AuthDataGenerator (org.datatransferproject.spi.api.auth.AuthDataGenerator)2 JobStore (org.datatransferproject.spi.cloud.storage.JobStore)2 InputStreamWrapper (org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore.InputStreamWrapper)2