Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JUnit Migration - Migrated exception assertion to Junit5 AssertThrows #1133

Merged
merged 17 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
exception assertion migration 2/2
Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Differential Revision: https://phabricator.intern.facebook.com/D38456882
  • Loading branch information
chaaransen committed Aug 5, 2022
commit f084c831586cd0da8b0fd6cb04832e3c16200116
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;

Expand All @@ -52,6 +53,7 @@
import static org.mockito.Mockito.when;

public class FacebookPhotosExporterTest {

private static final String ALBUM_NAME = "My Album";
private static final String ALBUM_ID = "1946595";
private static final String ALBUM_DESCRIPTION = "This is a test album";
Expand Down Expand Up @@ -181,11 +183,13 @@ public void testSpecifiedAlbums() throws CopyExceptionWithFailureReason {
.contains(new IdOnlyContainerResource(ALBUM_ID));
}

@Test(expected = IllegalStateException.class)
@Test
public void testIllegalExport() throws CopyExceptionWithFailureReason {
facebookPhotosExporter.export(
uuid,
new TokensAndUrlAuthData("accessToken", null, null),
Optional.of(new ExportInformation(new StringPaginationToken(PHOTO_TOKEN_PREFIX), null)));
Assertions.assertThrows(IllegalStateException.class, () -> {
facebookPhotosExporter.export(
uuid,
new TokensAndUrlAuthData("accessToken", null, null),
Optional.of(new ExportInformation(new StringPaginationToken(PHOTO_TOKEN_PREFIX), null)));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.hamcrest.CoreMatchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;

Expand Down Expand Up @@ -144,9 +145,9 @@ public void importTwoPhotos() throws Exception {
Mockito.when(googlePhotosInterface.uploadPhotoContent(any())).thenReturn("token1", "token2");
BatchMediaItemResponse batchMediaItemResponse =
new BatchMediaItemResponse(
new NewMediaItemResult[] {
buildMediaItemResult("token1", Code.OK_VALUE),
buildMediaItemResult("token2", Code.OK_VALUE)
new NewMediaItemResult[]{
buildMediaItemResult("token1", Code.OK_VALUE),
buildMediaItemResult("token2", Code.OK_VALUE)
});
Mockito.when(googlePhotosInterface.createPhotos(any(NewMediaItemUpload.class)))
.thenReturn(batchMediaItemResponse);
Expand Down Expand Up @@ -202,9 +203,9 @@ public void importTwoPhotosWithFailure() throws Exception {
Mockito.when(googlePhotosInterface.uploadPhotoContent(any())).thenReturn("token1", "token2");
BatchMediaItemResponse batchMediaItemResponse =
new BatchMediaItemResponse(
new NewMediaItemResult[] {
buildMediaItemResult("token1", Code.OK_VALUE),
buildMediaItemResult("token2", Code.UNAUTHENTICATED_VALUE)
new NewMediaItemResult[]{
buildMediaItemResult("token1", Code.OK_VALUE),
buildMediaItemResult("token2", Code.UNAUTHENTICATED_VALUE)
});
Mockito.when(googlePhotosInterface.createPhotos(any(NewMediaItemUpload.class)))
.thenReturn(batchMediaItemResponse);
Expand Down Expand Up @@ -308,7 +309,7 @@ public void importPhotoInTempStore() throws Exception {

BatchMediaItemResponse batchMediaItemResponse =
new BatchMediaItemResponse(
new NewMediaItemResult[] {buildMediaItemResult("token1", Code.OK_VALUE)});
new NewMediaItemResult[]{buildMediaItemResult("token1", Code.OK_VALUE)});

Mockito.when(googlePhotosInterface.createPhotos(any(NewMediaItemUpload.class)))
.thenReturn(batchMediaItemResponse);
Expand Down Expand Up @@ -354,7 +355,7 @@ public void importPhotoInTempStoreFailure() throws Exception {

BatchMediaItemResponse batchMediaItemResponse =
new BatchMediaItemResponse(
new NewMediaItemResult[] {buildMediaItemResult("token1", Code.OK_VALUE)});
new NewMediaItemResult[]{buildMediaItemResult("token1", Code.OK_VALUE)});

Mockito.when(googlePhotosInterface.createPhotos(any(NewMediaItemUpload.class)))
.thenReturn(batchMediaItemResponse);
Expand Down Expand Up @@ -410,7 +411,7 @@ public void importPhotoFailedToFindAlbum() throws Exception {
assertEquals(0, bytes);
}

@Test(expected = IOException.class)
@Test
public void importPhotoCreatePhotosOtherException() throws Exception {
PhotoModel photoModel =
new PhotoModel(
Expand Down Expand Up @@ -438,11 +439,13 @@ public void importPhotoCreatePhotosOtherException() throws Exception {
GoogleAlbum responseAlbum = new GoogleAlbum();
Mockito.when(googlePhotosInterface.getAlbum(any())).thenReturn(responseAlbum);

googlePhotosImporter.importPhotoBatch(
uuid,
Mockito.mock(TokensAndUrlAuthData.class),
Lists.newArrayList(photoModel),
executor,
NEW_ALBUM_ID);
Assertions.assertThrows(IOException.class, () -> {
googlePhotosImporter.importPhotoBatch(
uuid,
Mockito.mock(TokensAndUrlAuthData.class),
Lists.newArrayList(photoModel),
executor,
NEW_ALBUM_ID);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@
import org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;

/**
* This tests the MicrosoftMediaImporter. As of now, it only tests the number of requests called.
* More tests are needed to test request bodies and parameters.
*/
public class MicrosoftMediaImporterTest {

private static final int CHUNK_SIZE = 32000 * 1024; // 32000KiB
private final static String BASE_URL = "https://www.baseurl.com";
private final static UUID uuid = UUID.randomUUID();
Expand Down Expand Up @@ -97,7 +99,8 @@ public void testCleanAlbumNames() throws Exception {
List<MediaAlbum> albums =
ImmutableList.of(new MediaAlbum("id1", "album1.", "This is a fake albumb"));

MediaContainerResource data = new MediaContainerResource(albums, null /*phots*/, null /*videos*/);
MediaContainerResource data = new MediaContainerResource(albums, null /*phots*/,
null /*videos*/);

Call call = mock(Call.class);
doReturn(call).when(client).newCall(argThat((Request r) -> {
Expand All @@ -112,7 +115,7 @@ public void testCleanAlbumNames() throws Exception {
}

return r.url().toString().equals(
"https://www.baseurl.com/v1.0/me/drive/special/photo-video/children")
"https://www.baseurl.com/v1.0/me/drive/special/photo-video/children")
&& body.contains("album1_");
}));
Response response = mock(Response.class);
Expand All @@ -132,18 +135,19 @@ public void testCleanAlbumNames() throws Exception {
assertThat(result).isEqualTo(ImportResult.OK);
}

@Test(expected = PermissionDeniedException.class)
@Test
public void testImportItemPermissionDenied() throws Exception {
List<MediaAlbum> albums =
ImmutableList.of(new MediaAlbum("id1", "album1.", "This is a fake albumb"));

MediaContainerResource data = new MediaContainerResource(albums, null /*photos*/, null /*videos*/);
MediaContainerResource data = new MediaContainerResource(albums, null /*photos*/,
null /*videos*/);

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/photo-video/children")));
-> r.url().toString().equals(
"https://www.baseurl.com/v1.0/me/drive/special/photo-video/children")));
Response response = mock(Response.class);
ResponseBody body = mock(ResponseBody.class);
when(body.bytes())
Expand All @@ -157,7 +161,9 @@ public void testImportItemPermissionDenied() throws Exception {
when(response.body()).thenReturn(body);
when(call.execute()).thenReturn(response);

ImportResult result = importer.importItem(uuid, executor, authData, data);
Assertions.assertThrows(PermissionDeniedException.class, () -> {
ImportResult result = importer.importItem(uuid, executor, authData, data);
});
}

@Test
Expand All @@ -178,8 +184,8 @@ public void testImportItemAllSuccess() throws Exception {
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/photo-video/children")));
-> r.url().toString().equals(
"https://www.baseurl.com/v1.0/me/drive/special/photo-video/children")));
Response response = mock(Response.class);
ResponseBody body = mock(ResponseBody.class);
when(body.bytes())
Expand All @@ -199,14 +205,14 @@ public void testImportItemAllSuccess() throws Exception {
ResponseBody body2 = mock(ResponseBody.class);
when(body2.bytes())
.thenReturn(ResponseBody
.create(MediaType.parse("application/json"),
"{\"uploadUrl\": \"https://scalia.com/link\"}")
.bytes());
.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());
.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);
Expand All @@ -218,10 +224,10 @@ public void testImportItemAllSuccess() throws Exception {
ResponseBody body3 = mock(ResponseBody.class);
when(body3.bytes())
.thenReturn(ResponseBody.create(MediaType.parse("application/json"), "{\"id\": \"rand1\"}")
.bytes());
.bytes());
when(body3.string())
.thenReturn(ResponseBody.create(MediaType.parse("application/json"), "{\"id\": \"rand1\"}")
.string());
.string());
when(response3.code()).thenReturn(200);
when(response3.body()).thenReturn(body3);
when(call3.execute()).thenReturn(response3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;


/**
Expand Down Expand Up @@ -140,7 +141,7 @@ public void testCleanAlbumNames() throws Exception {
assertThat(result).isEqualTo(ImportResult.OK);
}

@Test(expected = PermissionDeniedException.class)
@Test
public void testImportItemPermissionDenied() throws Exception {
List<PhotoAlbum> albums =
ImmutableList.of(new PhotoAlbum("id1", "album1.", "This is a fake albumb"));
Expand All @@ -162,7 +163,9 @@ public void testImportItemPermissionDenied() throws Exception {
when(response.body()).thenReturn(body);
when(call.execute()).thenReturn(response);

ImportResult result = importer.importItem(uuid, executor, authData, data);
Assertions.assertThrows(PermissionDeniedException.class, () -> {
ImportResult result = importer.importItem(uuid, executor, authData, data);
});
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.Assert.assertFalse;

import org.junit.Test;
import org.junit.jupiter.api.Assertions;

public class ItemImportResultTest {

Expand All @@ -15,23 +16,31 @@ public void testNullBytesIsOk() {
assertFalse(result.hasBytes());
}

@Test(expected = IllegalStateException.class)
@Test
public void testNullBytesThrowOnGet() {
ItemImportResult.success("blabla", null).getBytes();
Assertions.assertThrows(IllegalStateException.class, () -> {
ItemImportResult.success("blabla", null).getBytes();
});
}

@Test(expected = NullPointerException.class)
@Test
public void testSuccessWithNoData() {
ItemImportResult.success(null, 0L);
Assertions.assertThrows(NullPointerException.class, () -> {
ItemImportResult.success(null, 0L);
});
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testFailWithIncorrectBytes() {
ItemImportResult.success("blabla", -1L);
Assertions.assertThrows(IllegalArgumentException.class, () -> {
ItemImportResult.success("blabla", -1L);
});
}

@Test(expected = NullPointerException.class)
@Test
public void testErrorWithNoException() {
ItemImportResult.error(null, 10L);
Assertions.assertThrows(NullPointerException.class, () -> {
ItemImportResult.error(null, 10L);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.datatransferproject.types.common.models.photos.PhotoModel;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;

public class CallableSizeCalculatorTest {

Expand Down Expand Up @@ -80,12 +81,14 @@ public void testSizeIsNotProvided() throws Exception {
Truth.assertThat(nextByte).isEqualTo(-1);
}

@Test(expected = IOException.class)
@Test
public void testExceptionIsThrown() throws Exception {
when(connectionProvider.getInputStreamForItem(any(), any()))
.thenThrow(new IOException("oh no!"));
new CallableSizeCalculator(jobId, connectionProvider,
Collections.singleton(createItem("1-3242"))).call();
Assertions.assertThrows(IOException.class, () -> {
new CallableSizeCalculator(jobId, connectionProvider,
Collections.singleton(createItem("1-3242"))).call();
});
}

private DownloadableItem createItem(String dataId) {
Expand Down