use of org.datatransferproject.spi.transfer.provider.ImportResult in project data-transfer-project by google.
the class MicrosoftPhotosImporterTest method testImportItemAllSuccess.
@Test
public void testImportItemAllSuccess() throws Exception {
List<PhotoAlbum> albums = ImmutableList.of(new PhotoAlbum("id1", "albumb1", "This is a fake albumb"));
List<PhotoModel> photos = ImmutableList.of(new PhotoModel("Pic1", "http://fake.com/1.jpg", "A pic", "image/jpg", "p1", "id1", true), new PhotoModel("Pic2", "https://fake.com/2.png", "fine art", "image/png", "p2", "id1", true));
when(jobStore.getStream(uuid, "http://fake.com/1.jpg")).thenReturn(new InputStreamWrapper(new ByteArrayInputStream(new byte[CHUNK_SIZE])));
when(jobStore.getStream(uuid, "https://fake.com/2.png")).thenReturn(new InputStreamWrapper(new ByteArrayInputStream(new byte[CHUNK_SIZE])));
PhotosContainerResource data = new PhotosContainerResource(albums, photos);
Call call = mock(Call.class);
doReturn(call).when(client).newCall(argThat((Request r) -> r.url().toString().equals("https://www.baseurl.com/v1.0/me/drive/special/photos/children")));
Response response = mock(Response.class);
ResponseBody body = mock(ResponseBody.class);
when(body.bytes()).thenReturn(ResponseBody.create(MediaType.parse("application/json"), "{\"id\": \"id1\"}").bytes());
when(body.string()).thenReturn(ResponseBody.create(MediaType.parse("application/json"), "{\"id\": \"id1\"}").string());
when(response.code()).thenReturn(200);
when(response.body()).thenReturn(body);
when(call.execute()).thenReturn(response);
Call call2 = mock(Call.class);
doReturn(call2).when(client).newCall(argThat((Request r) -> r.url().toString().contains("createUploadSession")));
Response response2 = mock(Response.class);
ResponseBody body2 = mock(ResponseBody.class);
when(body2.bytes()).thenReturn(ResponseBody.create(MediaType.parse("application/json"), "{\"uploadUrl\": \"https://scalia.com/link\"}").bytes());
when(body2.string()).thenReturn(ResponseBody.create(MediaType.parse("application/json"), "{\"uploadUrl\": \"https://scalia.com/link\"}").string());
when(response2.code()).thenReturn(200);
when(response2.body()).thenReturn(body2);
when(call2.execute()).thenReturn(response2);
Call call3 = mock(Call.class);
doReturn(call3).when(client).newCall(argThat((Request r) -> r.url().toString().contains("scalia.com/link")));
Response response3 = mock(Response.class);
ResponseBody body3 = mock(ResponseBody.class);
when(body3.bytes()).thenReturn(ResponseBody.create(MediaType.parse("application/json"), "{\"id\": \"rand1\"}").bytes());
when(body3.string()).thenReturn(ResponseBody.create(MediaType.parse("application/json"), "{\"id\": \"rand1\"}").string());
when(response3.code()).thenReturn(200);
when(response3.body()).thenReturn(body3);
when(call3.execute()).thenReturn(response3);
ImportResult result = importer.importItem(uuid, executor, authData, data);
verify(client, times(5)).newCall(any());
assertThat(result).isEqualTo(ImportResult.OK);
}
use of org.datatransferproject.spi.transfer.provider.ImportResult in project data-transfer-project by google.
the class MicrosoftCalendarImportTest method testImport.
@Test
@SuppressWarnings("unchecked")
public void testImport() throws Exception {
server.enqueue(new MockResponse().setBody(BATCH_CALENDAR_RESPONSE));
server.enqueue(new MockResponse().setResponseCode(201).setBody(BATCH_EVENT_RESPONSE));
server.start();
HttpUrl baseUrl = server.url("");
MicrosoftCalendarImporter importer = new MicrosoftCalendarImporter(baseUrl.toString(), client, mapper, transformerService);
CalendarModel calendarModel = new CalendarModel("OldId1", "name", "name");
CalendarAttendeeModel attendeeModel = new CalendarAttendeeModel("Test Attendee", "test@test.com", false);
CalendarEventModel.CalendarEventTime start = new CalendarEventModel.CalendarEventTime(ZonedDateTime.now(ZoneId.of("GMT")).toOffsetDateTime(), false);
CalendarEventModel.CalendarEventTime end = new CalendarEventModel.CalendarEventTime(ZonedDateTime.now(ZoneId.of("GMT")).toOffsetDateTime(), false);
CalendarEventModel eventModel = new CalendarEventModel("OldId1", "Event1", "Test Notes", singletonList(attendeeModel), "Location1", start, end, null);
CalendarContainerResource resource = new CalendarContainerResource(singleton(calendarModel), singleton(eventModel));
FakeIdempotentImportExecutor executor = new FakeIdempotentImportExecutor();
ImportResult result = importer.importItem(JOB_ID, executor, token, resource);
Assert.assertEquals(ImportResult.ResultType.OK, result.getType());
// verify the batch calendar request
RecordedRequest calendarBatch = server.takeRequest();
Map<String, Object> calendarBody = (Map<String, Object>) mapper.readValue(calendarBatch.getBody().readUtf8(), Map.class);
List<Map<String, Object>> calendarRequests = (List<Map<String, Object>>) calendarBody.get("requests");
Assert.assertNotNull(calendarRequests);
Assert.assertEquals(1, calendarRequests.size());
Map<String, Object> calendarRequest = calendarRequests.get(0);
Assert.assertNotNull(calendarRequest.get("headers"));
Assert.assertEquals("POST", calendarRequest.get("method"));
Assert.assertEquals("/v1.0/me/calendars", calendarRequest.get("url"));
Map<String, Object> calendarRequestBody = (Map<String, Object>) calendarRequest.get("body");
Assert.assertNotNull(calendarRequestBody);
Assert.assertEquals("name", calendarRequestBody.get("name"));
// verify the calendar id mapping from old id to new id was saved
Assert.assertEquals("NewId1", executor.getCachedValue("OldId1"));
// verify the batch event request
RecordedRequest eventBatch = server.takeRequest();
Map<String, Object> eventRequests = (Map<String, Object>) mapper.readValue(eventBatch.getBody().readUtf8(), Map.class);
Map<String, Object> eventRequest = (Map<String, Object>) ((List<Map<String, Object>>) eventRequests.get("requests")).get(0);
Assert.assertNotNull(eventRequest.get("headers"));
Assert.assertEquals("POST", eventRequest.get("method"));
Assert.assertEquals("/v1.0/me/calendars/NewId1/events", // verify the URL is contructed correctly with NewId
eventRequest.get("url"));
Map<String, Object> eventRequestBody = (Map<String, Object>) eventRequest.get("body");
Assert.assertNotNull(eventRequestBody);
Assert.assertEquals("Event1", eventRequestBody.get("subject"));
Map<String, Object> location = (Map<String, Object>) eventRequestBody.get("location");
Assert.assertEquals("Location1", location.get("displayName"));
Assert.assertEquals("Default", location.get("locationType"));
Map<String, Object> body = (Map<String, Object>) eventRequestBody.get("body");
Assert.assertEquals("Test Notes", body.get("content"));
Assert.assertEquals("HTML", body.get("contentType"));
List<Map<String, Object>> attendees = (List<Map<String, Object>>) eventRequestBody.get("attendees");
Assert.assertEquals(1, attendees.size());
Map<String, Object> attendee = (Map<String, Object>) attendees.get(0);
Assert.assertEquals("required", attendee.get("type"));
Map<String, Object> emailAddress = (Map<String, Object>) attendee.get("emailAddress");
Assert.assertEquals("test@test.com", emailAddress.get("address"));
Assert.assertEquals("Test Attendee", emailAddress.get("name"));
// verify dates
Map<String, Object> startDate = (Map<String, Object>) eventRequestBody.get("start");
Assert.assertNotNull(startDate.get("dateTime"));
Assert.assertEquals("UTC", startDate.get("timeZone"));
Map<String, Object> endDate = (Map<String, Object>) eventRequestBody.get("end");
Assert.assertNotNull(endDate.get("dateTime"));
Assert.assertEquals("UTC", endDate.get("timeZone"));
}
use of org.datatransferproject.spi.transfer.provider.ImportResult in project data-transfer-project by google.
the class GoogleVideosImporter method importItem.
@Override
public ImportResult importItem(UUID jobId, IdempotentImportExecutor executor, TokensAndUrlAuthData authData, VideosContainerResource data) throws Exception {
if (data == null) {
// Nothing to do
return ImportResult.OK;
}
PhotosLibraryClient client;
if (clientsMap.containsKey(jobId)) {
client = clientsMap.get(jobId);
} else {
PhotosLibrarySettings settings = PhotosLibrarySettings.newBuilder().setCredentialsProvider(FixedCredentialsProvider.create(UserCredentials.newBuilder().setClientId(appCredentials.getKey()).setClientSecret(appCredentials.getSecret()).setAccessToken(new AccessToken(authData.getAccessToken(), new Date())).setRefreshToken(authData.getRefreshToken()).build())).build();
client = PhotosLibraryClient.initialize(settings);
clientsMap.put(jobId, client);
}
long bytes = 0L;
// Uploads videos
final Collection<VideoModel> videos = data.getVideos();
if (videos != null && videos.size() > 0) {
Stream<VideoModel> stream = videos.stream().filter(video -> shouldImport(video, executor)).map(this::transformVideoName);
// We partition into groups of 49 as 50 is the maximum number of items that can be created in
// one call. (We use 49 to avoid potential off by one errors)
// https://developers.google.com/photos/library/guides/upload-media#creating-media-item
final UnmodifiableIterator<List<VideoModel>> batches = Iterators.partition(stream.iterator(), 49);
while (batches.hasNext()) {
long batchBytes = importVideoBatch(batches.next(), client, executor);
bytes += batchBytes;
}
}
final ImportResult result = ImportResult.OK;
return result.copyWithBytes(bytes);
}
use of org.datatransferproject.spi.transfer.provider.ImportResult in project data-transfer-project by google.
the class SmugMugPhotosImporterTest method importEmptyAlbumName.
@Test
public void importEmptyAlbumName() throws Exception {
UUID jobId = UUID.randomUUID();
PhotoAlbum photoAlbum = new PhotoAlbum("albumid", "", "albumDescription");
PhotosContainerResource photosContainerResource = new PhotosContainerResource(Collections.singletonList(photoAlbum), ImmutableList.of());
SmugMugAlbum smugMugAlbum = new SmugMugAlbum("date", photoAlbum.getDescription(), "Untitled Album", "privacy", "albumUri1", "urlname", "weburi");
SmugMugAlbumResponse mockAlbumResponse = new SmugMugAlbumResponse(smugMugAlbum.getUri(), "Locator", "LocatorType", smugMugAlbum);
when(smugMugInterface.createAlbum(eq(smugMugAlbum.getName()))).thenReturn(mockAlbumResponse);
// Run test
SmugMugPhotosImporter importer = new SmugMugPhotosImporter(smugMugInterface, config, jobStore, new AppCredentials("key", "secret"), mock(ObjectMapper.class), monitor);
ImportResult result = importer.importItem(jobId, EXECUTOR, new TokenSecretAuthData("token", "secret"), photosContainerResource);
// Verify
verify(smugMugInterface, atLeastOnce()).createAlbum(ArgumentCaptor.forClass(String.class).capture());
}
use of org.datatransferproject.spi.transfer.provider.ImportResult in project data-transfer-project by google.
the class SmugMugPhotosImporterTest method importStoresAlbumInJobStore.
@Test
public void importStoresAlbumInJobStore() throws Exception {
// setup test objects
UUID jobId = UUID.randomUUID();
PhotoAlbum photoAlbum1 = new PhotoAlbum("albumId1", "albumName1", "albumDescription1");
PhotoModel photoModel1 = new PhotoModel("PHOTO_TITLE", "FETCHABLE_URL", "PHOTO_DESCRIPTION", "MEDIA_TYPE", "photoId1", photoAlbum1.getId(), false);
PhotoModel photoModel2 = new PhotoModel("PHOTO_TITLE", "FETCHABLE_URL", "PHOTO_DESCRIPTION", "MEDIA_TYPE", "photoId2", photoAlbum1.getId(), false);
PhotoModel photoModel3 = new PhotoModel("PHOTO_TITLE", "FETCHABLE_URL", "PHOTO_DESCRIPTION", "MEDIA_TYPE", "photoId3", photoAlbum1.getId(), false);
PhotosContainerResource photosContainerResource1 = new PhotosContainerResource(Collections.singletonList(photoAlbum1), ImmutableList.of());
PhotosContainerResource photosContainerResource2 = new PhotosContainerResource(ImmutableList.of(), ImmutableList.of(photoModel1, photoModel2, photoModel3));
SmugMugAlbum smugMugAlbum1 = new SmugMugAlbum("date", photoAlbum1.getDescription(), photoAlbum1.getName(), "privacy", "albumUri1", "urlname", "weburi");
String overflowAlbumName = smugMugAlbum1.getName() + " (1)";
SmugMugAlbum smugMugAlbum2 = new SmugMugAlbum("date", photoAlbum1.getDescription(), overflowAlbumName, "privacy", "albumUri2", "urlname", "weburi");
SmugMugAlbumResponse mockAlbumResponse1 = new SmugMugAlbumResponse(smugMugAlbum1.getUri(), "Locator", "LocatorType", smugMugAlbum1);
SmugMugAlbumResponse mockAlbumResponse2 = new SmugMugAlbumResponse(smugMugAlbum2.getUri(), "Locator", "LocatorType", smugMugAlbum2);
when(smugMugInterface.createAlbum(eq(smugMugAlbum1.getName()))).thenReturn(mockAlbumResponse1);
when(smugMugInterface.createAlbum(eq(smugMugAlbum2.getName()))).thenReturn(mockAlbumResponse2);
SmugMugImageUploadResponse smugMugUploadImageResponse = new SmugMugImageUploadResponse("imageUri", "albumImageUri", new ImageInfo("imageUri", "albumImageUri", "statusImageReplaceUri", "url"));
when(smugMugInterface.uploadImage(any(), any(), any())).thenReturn(smugMugUploadImageResponse);
when(smugMugInterface.getImageAsStream(any())).thenReturn(bufferedInputStream);
// Run test
SmugMugPhotosImporter importer = new SmugMugPhotosImporter(smugMugInterface, config, jobStore, new AppCredentials("key", "secret"), mock(ObjectMapper.class), monitor);
ImportResult result = importer.importItem(jobId, EXECUTOR, new TokenSecretAuthData("token", "secret"), photosContainerResource1);
result = importer.importItem(jobId, EXECUTOR, new TokenSecretAuthData("token", "secret"), photosContainerResource2);
// Verify
ArgumentCaptor<String> photoUrlsCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> albumNamesCaptor = ArgumentCaptor.forClass(String.class);
verify(smugMugInterface, atLeastOnce()).createAlbum(albumNamesCaptor.capture());
verify(smugMugInterface, atLeastOnce()).getImageAsStream(photoUrlsCaptor.capture());
List<String> capturedAlbumNames = albumNamesCaptor.getAllValues();
assertTrue(capturedAlbumNames.contains(smugMugAlbum1.getName()));
assertTrue(capturedAlbumNames.contains(smugMugAlbum2.getName()));
List<String> capturedPhotoUrls = photoUrlsCaptor.getAllValues();
assertTrue(capturedPhotoUrls.contains(photoModel1.getFetchableUrl()));
assertTrue(capturedPhotoUrls.contains(photoModel2.getFetchableUrl()));
assertTrue(capturedPhotoUrls.contains(photoModel3.getFetchableUrl()));
String overflowAlbumId = photoAlbum1.getId() + "-overflow-1";
assertThat((String) EXECUTOR.getCachedValue(photoAlbum1.getId())).isEqualTo(smugMugAlbum1.getUri());
assertThat((String) EXECUTOR.getCachedValue(overflowAlbumId)).isEqualTo(smugMugAlbum2.getUri());
SmugMugPhotoTempData tempData1 = new SmugMugPhotoTempData(photoAlbum1.getId(), smugMugAlbum1.getName(), smugMugAlbum1.getDescription(), smugMugAlbum1.getUri(), 2, overflowAlbumId);
SmugMugPhotoTempData tempData2 = new SmugMugPhotoTempData(overflowAlbumId, smugMugAlbum2.getName(), smugMugAlbum2.getDescription(), smugMugAlbum2.getUri(), 1, null);
assertThat(jobStore.findData(jobId, String.format(TEMP_DATA_FORMAT, photoAlbum1.getId()), SmugMugPhotoTempData.class).toString()).isEqualTo(tempData1.toString());
assertThat(jobStore.findData(jobId, String.format(TEMP_DATA_FORMAT, overflowAlbumId), SmugMugPhotoTempData.class).toString()).isEqualTo(tempData2.toString());
}
Aggregations