Search in sources :

Example 11 with PortabilityJob

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

the class KoofrPhotosImporterTest method testImportItemFromJobStoreUserTimeZoneCalledOnce.

@Test
public void testImportItemFromJobStoreUserTimeZoneCalledOnce() throws Exception {
    ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4 });
    when(jobStore.getStream(any(), any())).thenReturn(new InputStreamWrapper(inputStream, 5L));
    UUID jobId = UUID.randomUUID();
    PortabilityJob job = mock(PortabilityJob.class);
    when(job.userTimeZone()).thenReturn(TimeZone.getTimeZone("Europe/Rome"));
    when(jobStore.findJob(jobId)).thenReturn(job);
    Collection<PhotoAlbum> albums = ImmutableList.of(new PhotoAlbum("id1", "Album 1", "This is a fake album"));
    DateFormat format = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
    format.setTimeZone(TimeZone.getTimeZone("Europe/Kiev"));
    Collection<PhotoModel> photos1 = ImmutableList.of(new PhotoModel("pic1.jpg", "http://fake.com/1.jpg", "A pic", "image/jpeg", "p1", "id1", true, format.parse("2021:02:16 11:55:00")));
    Collection<PhotoModel> photos2 = ImmutableList.of(new PhotoModel("pic2.jpg", "http://fake.com/2.jpg", "A pic", "image/jpeg", "p2", "id1", true, format.parse("2021:02:17 11:55:00")));
    PhotosContainerResource resource1 = spy(new PhotosContainerResource(albums, photos1));
    PhotosContainerResource resource2 = spy(new PhotosContainerResource(albums, photos2));
    importer.importItem(jobId, executor, authData, resource1);
    importer.importItem(jobId, executor, authData, resource2);
    InOrder clientInOrder = Mockito.inOrder(client);
    String[] titles = { "2021-02-16 10.55.00 pic1.jpg", "2021-02-17 10.55.00 pic2.jpg" };
    for (String title : titles) {
        clientInOrder.verify(client).uploadFile(any(), eq(title), any(), any(), any(), any());
    }
    verify(jobStore, atMostOnce()).findJob(jobId);
}
Also used : 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) InputStreamWrapper(org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore.InputStreamWrapper) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) UUID(java.util.UUID) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 12 with PortabilityJob

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

the class KoofrPhotosImporterTest method testImportItemFromJobStoreUserTimeZone.

@Test
public void testImportItemFromJobStoreUserTimeZone() throws Exception {
    ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4 });
    when(jobStore.getStream(any(), any())).thenReturn(new InputStreamWrapper(inputStream, 5L));
    UUID jobId = UUID.randomUUID();
    PortabilityJob job = mock(PortabilityJob.class);
    when(job.userTimeZone()).thenReturn(TimeZone.getTimeZone("Europe/Rome"));
    when(jobStore.findJob(jobId)).thenReturn(job);
    Collection<PhotoAlbum> albums = ImmutableList.of(new PhotoAlbum("id1", "Album 1", "This is a fake album"));
    DateFormat format = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
    format.setTimeZone(TimeZone.getTimeZone("Europe/Kiev"));
    Collection<PhotoModel> photos = ImmutableList.of(new PhotoModel("pic1.jpg", "http://fake.com/1.jpg", "A pic", "image/jpeg", "p1", "id1", true, format.parse("2021:02:16 11:55:00")));
    PhotosContainerResource resource = spy(new PhotosContainerResource(albums, photos));
    importer.importItem(jobId, executor, authData, resource);
    InOrder clientInOrder = Mockito.inOrder(client);
    clientInOrder.verify(client).uploadFile(any(), eq("2021-02-16 10.55.00 pic1.jpg"), any(), any(), any(), any());
}
Also used : PortabilityJob(org.datatransferproject.spi.cloud.types.PortabilityJob) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) InputStreamWrapper(org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore.InputStreamWrapper) InOrder(org.mockito.InOrder) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) UUID(java.util.UUID) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 13 with PortabilityJob

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

the class CreateTransferJobAction method createJob.

/**
 * Populates the initial state of the {@link PortabilityJob} instance.
 */
private PortabilityJob createJob(String encodedSessionKey, String dataType, String exportService, String importService, Optional<ExportInformation> exportInformation, String encryptionScheme) throws IOException {
    // Job auth data
    JobAuthorization jobAuthorization = JobAuthorization.builder().setSessionSecretKey(encodedSessionKey).setEncryptionScheme(encryptionScheme).setState(JobAuthorization.State.INITIAL).build();
    PortabilityJob.Builder builder = PortabilityJob.builder().setTransferDataType(dataType).setExportService(exportService).setImportService(importService).setAndValidateJobAuthorization(jobAuthorization);
    if (exportInformation.isPresent()) {
        builder.setExportInformation(objectMapper.writeValueAsString(exportInformation.get()));
    }
    return builder.build();
}
Also used : JobAuthorization(org.datatransferproject.spi.cloud.types.JobAuthorization) PortabilityJob(org.datatransferproject.spi.cloud.types.PortabilityJob)

Example 14 with PortabilityJob

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

the class CreateTransferJobAction method handle.

@Override
public TransferJob handle(CreateTransferJob request) {
    String dataType = request.getDataType();
    String exportService = request.getExportService();
    String importService = request.getImportService();
    Optional<ExportInformation> exportInformation = Optional.ofNullable(request.getExportInformation());
    String exportCallbackUrl = request.getExportCallbackUrl();
    String importCallbackUrl = request.getImportCallbackUrl();
    // Create a new job and persist
    UUID jobId = UUID.randomUUID();
    SecretKey sessionKey = symmetricKeyGenerator.generate();
    String encodedSessionKey = BaseEncoding.base64Url().encode(sessionKey.getEncoded());
    String encryptionScheme = request.getEncryptionScheme();
    PortabilityJob job;
    try {
        job = createJob(encodedSessionKey, dataType, exportService, importService, exportInformation, encryptionScheme);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    AuthDataGenerator exportGenerator = registry.getAuthDataGenerator(job.exportService(), job.transferDataType(), EXPORT);
    Preconditions.checkNotNull(exportGenerator, "Generator not found for type: %s, service: %s", job.transferDataType(), job.exportService());
    AuthDataGenerator importGenerator = registry.getAuthDataGenerator(job.importService(), job.transferDataType(), IMPORT);
    Preconditions.checkNotNull(importGenerator, "Generator not found for type: %s, service: %s", job.transferDataType(), job.importService());
    try {
        String encodedJobId = encodeJobId(jobId);
        AuthFlowConfiguration exportConfiguration = exportGenerator.generateConfiguration(exportCallbackUrl, encodedJobId);
        AuthFlowConfiguration importConfiguration = importGenerator.generateConfiguration(importCallbackUrl, encodedJobId);
        job = setInitialAuthDataOnJob(sessionKey, job, exportConfiguration, importConfiguration);
        jobStore.createJob(jobId, job);
        monitor.debug(() -> format("Created new transfer of type '%s' from '%s' to '%s' with jobId: %s", dataType, exportService, importService, jobId), jobId, EventCode.API_JOB_CREATED);
        return new TransferJob(encodedJobId, job.exportService(), job.importService(), job.transferDataType(), exportConfiguration.getAuthUrl(), importConfiguration.getAuthUrl(), exportConfiguration.getTokenUrl(), importConfiguration.getTokenUrl(), exportConfiguration.getAuthProtocol(), importConfiguration.getAuthProtocol());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : AuthFlowConfiguration(org.datatransferproject.spi.api.types.AuthFlowConfiguration) ExportInformation(org.datatransferproject.types.common.ExportInformation) PortabilityJob(org.datatransferproject.spi.cloud.types.PortabilityJob) AuthDataGenerator(org.datatransferproject.spi.api.auth.AuthDataGenerator) SecretKey(javax.crypto.SecretKey) IOException(java.io.IOException) UUID(java.util.UUID) TransferJob(org.datatransferproject.types.client.transfer.TransferJob) CreateTransferJob(org.datatransferproject.types.client.transfer.CreateTransferJob)

Example 15 with PortabilityJob

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

the class GetReservedWorkerAction method handle.

@Override
public ReservedWorker handle(GetReservedWorker workerRequest) {
    String id = workerRequest.getId();
    UUID jobId = decodeJobId(id);
    PortabilityJob job = jobStore.findJob(jobId);
    Preconditions.checkNotNull(job, "Couldn't lookup worker for job " + id + " because the job doesn't exist");
    if (job.jobAuthorization().state() != CREDS_ENCRYPTION_KEY_GENERATED) {
        monitor.debug(() -> format("Job %s has not entered state CREDS_ENCRYPTION_KEY_GENERATED yet", jobId), jobId);
        return new ReservedWorker(null);
    }
    monitor.debug(() -> format("Got job %s in state CREDS_ENCRYPTION_KEY_GENERATED, returning its public key", jobId), jobId, EventCode.API_GOT_RESERVED_WORKER);
    return new ReservedWorker(job.jobAuthorization().authPublicKey());
}
Also used : PortabilityJob(org.datatransferproject.spi.cloud.types.PortabilityJob) GetReservedWorker(org.datatransferproject.types.client.transfer.GetReservedWorker) ReservedWorker(org.datatransferproject.types.client.transfer.ReservedWorker) UUID(java.util.UUID)

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