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);
}
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());
}
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();
}
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);
}
}
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());
}
Aggregations