Automatically refactor more exception testing to use new JUnit rules

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=178911894
This commit is contained in:
mcilwain 2017-08-14 09:20:03 -04:00 committed by Ben McIlwain
parent 36ad38e5df
commit 7dc224627f
125 changed files with 1970 additions and 1982 deletions

View file

@ -18,6 +18,8 @@ import static com.google.appengine.api.taskqueue.QueueFactory.getQueue;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assert_;
import static google.registry.testing.JUnitBackports.assertThrows;
import static google.registry.testing.JUnitBackports.expectThrows;
import static google.registry.testing.TaskQueueHelper.assertTasksEnqueued;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.logging.Level.INFO;
@ -55,7 +57,6 @@ import java.util.logging.Logger;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -68,10 +69,6 @@ public class BigqueryPollJobActionTest {
.withDatastore()
.withTaskQueue()
.build();
@Rule
public final ExpectedException thrown = ExpectedException.none();
private static final String PROJECT_ID = "project_id";
private static final String JOB_ID = "job_id";
private static final String CHAINED_QUEUE_NAME = UpdateSnapshotViewAction.QUEUE;
@ -192,15 +189,13 @@ public class BigqueryPollJobActionTest {
public void testJobPending() throws Exception {
when(bigqueryJobsGet.execute()).thenReturn(
new Job().setStatus(new JobStatus().setState("PENDING")));
thrown.expect(NotModifiedException.class);
action.run();
assertThrows(NotModifiedException.class, () -> action.run());
}
@Test
public void testJobStatusUnreadable() throws Exception {
when(bigqueryJobsGet.execute()).thenThrow(IOException.class);
thrown.expect(NotModifiedException.class);
action.run();
assertThrows(NotModifiedException.class, () -> action.run());
}
@Test
@ -208,8 +203,7 @@ public class BigqueryPollJobActionTest {
when(bigqueryJobsGet.execute()).thenReturn(
new Job().setStatus(new JobStatus().setState("DONE")));
action.payload = "payload".getBytes(UTF_8);
thrown.expect(BadRequestException.class);
thrown.expectMessage("Cannot deserialize task from payload");
action.run();
BadRequestException thrown = expectThrows(BadRequestException.class, () -> action.run());
assertThat(thrown).hasMessageThat().contains("Cannot deserialize task from payload");
}
}

View file

@ -17,6 +17,7 @@ package google.registry.export;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.export.CheckSnapshotAction.CHECK_SNAPSHOT_KINDS_TO_LOAD_PARAM;
import static google.registry.export.CheckSnapshotAction.CHECK_SNAPSHOT_NAME_PARAM;
import static google.registry.testing.JUnitBackports.expectThrows;
import static google.registry.testing.TaskQueueHelper.assertNoTasksEnqueued;
import static google.registry.testing.TaskQueueHelper.assertTasksEnqueued;
import static org.mockito.Mockito.mock;
@ -40,7 +41,6 @@ import org.joda.time.Duration;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -53,8 +53,6 @@ public class CheckSnapshotActionTest {
@Rule public final InjectRule inject = new InjectRule();
@Rule public final AppEngineRule appEngine = AppEngineRule.builder().withTaskQueue().build();
@Rule public final ExpectedException thrown = ExpectedException.none();
private final DatastoreBackupService backupService = mock(DatastoreBackupService.class);
private DatastoreBackupInfo backupInfo;
@ -124,9 +122,8 @@ public class CheckSnapshotActionTest {
public void testPost_forPendingBackup_returnsNotModified() throws Exception {
setPendingBackup();
thrown.expect(NotModifiedException.class);
thrown.expectMessage("Datastore backup some_backup still pending");
action.run();
NotModifiedException thrown = expectThrows(NotModifiedException.class, () -> action.run());
assertThat(thrown).hasMessageThat().contains("Datastore backup some_backup still pending");
}
@Test
@ -141,11 +138,12 @@ public class CheckSnapshotActionTest {
.plus(Duration.standardMinutes(3))
.plus(Duration.millis(1234)));
thrown.expect(NoContentException.class);
thrown.expectMessage("Datastore backup some_backup abandoned - "
+ "not complete after 20 hours, 3 minutes and 1 second");
action.run();
NoContentException thrown = expectThrows(NoContentException.class, () -> action.run());
assertThat(thrown)
.hasMessageThat()
.contains(
"Datastore backup some_backup abandoned - "
+ "not complete after 20 hours, 3 minutes and 1 second");
}
@Test
@ -188,10 +186,8 @@ public class CheckSnapshotActionTest {
when(backupService.findByName("some_backup"))
.thenThrow(new IllegalArgumentException("No backup found"));
thrown.expect(BadRequestException.class);
thrown.expectMessage("Bad backup name some_backup: No backup found");
action.run();
BadRequestException thrown = expectThrows(BadRequestException.class, () -> action.run());
assertThat(thrown).hasMessageThat().contains("Bad backup name some_backup: No backup found");
}
@Test
@ -220,9 +216,7 @@ public class CheckSnapshotActionTest {
when(backupService.findByName("some_backup"))
.thenThrow(new IllegalArgumentException("No backup found"));
thrown.expect(BadRequestException.class);
thrown.expectMessage("Bad backup name some_backup: No backup found");
action.run();
BadRequestException thrown = expectThrows(BadRequestException.class, () -> action.run());
assertThat(thrown).hasMessageThat().contains("Bad backup name some_backup: No backup found");
}
}

View file

@ -17,6 +17,7 @@ package google.registry.export;
import static com.google.appengine.api.datastore.DatastoreServiceFactory.getDatastoreService;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
import static google.registry.testing.JUnitBackports.assertThrows;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
@ -32,17 +33,12 @@ import org.joda.time.Duration;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Unit tests for {@link DatastoreBackupInfo}. */
@RunWith(JUnit4.class)
public class DatastoreBackupInfoTest {
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Rule
public final InjectRule inject = new InjectRule();
@ -107,28 +103,28 @@ public class DatastoreBackupInfoTest {
@Test
public void testFailure_missingName() throws Exception {
backupEntity.removeProperty("name");
thrown.expect(NullPointerException.class);
new DatastoreBackupInfo(persistEntity(backupEntity));
assertThrows(
NullPointerException.class, () -> new DatastoreBackupInfo(persistEntity(backupEntity)));
}
@Test
public void testFailure_missingKinds() throws Exception {
backupEntity.removeProperty("kinds");
thrown.expect(NullPointerException.class);
new DatastoreBackupInfo(persistEntity(backupEntity));
assertThrows(
NullPointerException.class, () -> new DatastoreBackupInfo(persistEntity(backupEntity)));
}
@Test
public void testFailure_missingStartTime() throws Exception {
backupEntity.removeProperty("start_time");
thrown.expect(NullPointerException.class);
new DatastoreBackupInfo(persistEntity(backupEntity));
assertThrows(
NullPointerException.class, () -> new DatastoreBackupInfo(persistEntity(backupEntity)));
}
@Test
public void testFailure_badGcsFilenameFormat() throws Exception {
backupEntity.setProperty("gs_handle", new Text("foo"));
thrown.expect(IllegalArgumentException.class);
new DatastoreBackupInfo(persistEntity(backupEntity));
assertThrows(
IllegalArgumentException.class, () -> new DatastoreBackupInfo(persistEntity(backupEntity)));
}
}

View file

@ -17,6 +17,7 @@ package google.registry.export;
import static com.google.appengine.api.datastore.DatastoreServiceFactory.getDatastoreService;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.JUnitBackports.assertThrows;
import static google.registry.testing.TaskQueueHelper.assertTasksEnqueued;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -33,7 +34,6 @@ import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -41,9 +41,6 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class DatastoreBackupServiceTest {
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Rule
public final InjectRule inject = new InjectRule();
@ -119,13 +116,11 @@ public class DatastoreBackupServiceTest {
@Test
public void testFailure_findByName_multipleMatchingBackups() throws Exception {
thrown.expect(IllegalArgumentException.class);
backupService.findByName("backupA");
assertThrows(IllegalArgumentException.class, () -> backupService.findByName("backupA"));
}
@Test
public void testFailure_findByName_noMatchingBackups() throws Exception {
thrown.expect(IllegalArgumentException.class);
backupService.findByName("backupX");
assertThrows(IllegalArgumentException.class, () -> backupService.findByName("backupX"));
}
}

View file

@ -22,6 +22,7 @@ import static google.registry.export.LoadSnapshotAction.LOAD_SNAPSHOT_KINDS_PARA
import static google.registry.export.LoadSnapshotAction.PATH;
import static google.registry.export.LoadSnapshotAction.QUEUE;
import static google.registry.export.LoadSnapshotAction.enqueueLoadSnapshotTask;
import static google.registry.testing.JUnitBackports.expectThrows;
import static google.registry.testing.TaskQueueHelper.assertTasksEnqueued;
import static org.joda.time.DateTimeZone.UTC;
import static org.mockito.Matchers.any;
@ -52,7 +53,6 @@ import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentCaptor;
@ -65,10 +65,6 @@ public class LoadSnapshotActionTest {
public final AppEngineRule appEngine = AppEngineRule.builder()
.withTaskQueue()
.build();
@Rule
public final ExpectedException thrown = ExpectedException.none();
private final BigqueryFactory bigqueryFactory = mock(BigqueryFactory.class);
private final Bigquery bigquery = mock(Bigquery.class);
private final Bigquery.Jobs bigqueryJobs = mock(Bigquery.Jobs.class);
@ -186,16 +182,19 @@ public class LoadSnapshotActionTest {
@Test
public void testFailure_doPost_badGcsFilename() throws Exception {
action.snapshotFile = "gs://bucket/snapshot";
thrown.expect(BadRequestException.class);
thrown.expectMessage("Error calling load snapshot: backup info file extension missing");
action.run();
BadRequestException thrown = expectThrows(BadRequestException.class, () -> action.run());
assertThat(thrown)
.hasMessageThat()
.contains("Error calling load snapshot: backup info file extension missing");
}
@Test
public void testFailure_doPost_bigqueryThrowsException() throws Exception {
when(bigqueryJobsInsert.execute()).thenThrow(new IOException("The Internet has gone missing"));
thrown.expect(InternalServerErrorException.class);
thrown.expectMessage("Error loading snapshot: The Internet has gone missing");
action.run();
InternalServerErrorException thrown =
expectThrows(InternalServerErrorException.class, () -> action.run());
assertThat(thrown)
.hasMessageThat()
.contains("Error loading snapshot: The Internet has gone missing");
}
}

View file

@ -21,6 +21,7 @@ import static google.registry.export.PublishDetailReportAction.GCS_FOLDER_PREFIX
import static google.registry.export.PublishDetailReportAction.REGISTRAR_ID_PARAM;
import static google.registry.testing.DatastoreHelper.loadRegistrar;
import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.JUnitBackports.expectThrows;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
@ -45,17 +46,12 @@ import java.util.Map;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Unit tests for {@link PublishDetailReportAction}. */
@RunWith(JUnit4.class)
public class PublishDetailReportActionTest {
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Rule
public final AppEngineRule appEngine = AppEngineRule.builder()
.withDatastore()
@ -108,88 +104,120 @@ public class PublishDetailReportActionTest {
@Test
public void testFailure_noRegistrarParameter() throws Exception {
thrown.expect(BadRequestException.class);
thrown.expectMessage(REGISTRAR_ID_PARAM);
action.handleJsonRequest(ImmutableMap.of(
GCS_BUCKET_PARAM, "mah-buckit",
GCS_FOLDER_PREFIX_PARAM, "some/folder/",
DETAIL_REPORT_NAME_PARAM, "detail_report.csv"));
BadRequestException thrown =
expectThrows(
BadRequestException.class,
() ->
action.handleJsonRequest(
ImmutableMap.of(
GCS_BUCKET_PARAM, "mah-buckit",
GCS_FOLDER_PREFIX_PARAM, "some/folder/",
DETAIL_REPORT_NAME_PARAM, "detail_report.csv")));
assertThat(thrown).hasMessageThat().contains(REGISTRAR_ID_PARAM);
}
@Test
public void testFailure_noGcsBucketParameter() throws Exception {
thrown.expect(BadRequestException.class);
thrown.expectMessage(GCS_BUCKET_PARAM);
action.handleJsonRequest(ImmutableMap.of(
REGISTRAR_ID_PARAM, "TheRegistrar",
GCS_FOLDER_PREFIX_PARAM, "some/folder/",
DETAIL_REPORT_NAME_PARAM, "detail_report.csv"));
BadRequestException thrown =
expectThrows(
BadRequestException.class,
() ->
action.handleJsonRequest(
ImmutableMap.of(
REGISTRAR_ID_PARAM, "TheRegistrar",
GCS_FOLDER_PREFIX_PARAM, "some/folder/",
DETAIL_REPORT_NAME_PARAM, "detail_report.csv")));
assertThat(thrown).hasMessageThat().contains(GCS_BUCKET_PARAM);
}
@Test
public void testFailure_noGcsFolderPrefixParameter() throws Exception {
thrown.expect(BadRequestException.class);
thrown.expectMessage(GCS_FOLDER_PREFIX_PARAM);
action.handleJsonRequest(ImmutableMap.of(
REGISTRAR_ID_PARAM, "TheRegistrar",
GCS_BUCKET_PARAM, "mah-buckit",
DETAIL_REPORT_NAME_PARAM, "detail_report.csv"));
BadRequestException thrown =
expectThrows(
BadRequestException.class,
() ->
action.handleJsonRequest(
ImmutableMap.of(
REGISTRAR_ID_PARAM, "TheRegistrar",
GCS_BUCKET_PARAM, "mah-buckit",
DETAIL_REPORT_NAME_PARAM, "detail_report.csv")));
assertThat(thrown).hasMessageThat().contains(GCS_FOLDER_PREFIX_PARAM);
}
@Test
public void testFailure_noReportNameParameter() throws Exception {
thrown.expect(BadRequestException.class);
thrown.expectMessage(DETAIL_REPORT_NAME_PARAM);
action.handleJsonRequest(ImmutableMap.of(
REGISTRAR_ID_PARAM, "TheRegistrar",
GCS_BUCKET_PARAM, "mah-buckit",
GCS_FOLDER_PREFIX_PARAM, "some/folder/"));
BadRequestException thrown =
expectThrows(
BadRequestException.class,
() ->
action.handleJsonRequest(
ImmutableMap.of(
REGISTRAR_ID_PARAM, "TheRegistrar",
GCS_BUCKET_PARAM, "mah-buckit",
GCS_FOLDER_PREFIX_PARAM, "some/folder/")));
assertThat(thrown).hasMessageThat().contains(DETAIL_REPORT_NAME_PARAM);
}
@Test
public void testFailure_registrarNotFound() throws Exception {
thrown.expect(BadRequestException.class);
thrown.expectMessage("FakeRegistrar");
action.handleJsonRequest(ImmutableMap.of(
REGISTRAR_ID_PARAM, "FakeRegistrar",
GCS_BUCKET_PARAM, "mah-buckit",
GCS_FOLDER_PREFIX_PARAM, "some/folder/",
DETAIL_REPORT_NAME_PARAM, "detail_report.csv"));
BadRequestException thrown =
expectThrows(
BadRequestException.class,
() ->
action.handleJsonRequest(
ImmutableMap.of(
REGISTRAR_ID_PARAM, "FakeRegistrar",
GCS_BUCKET_PARAM, "mah-buckit",
GCS_FOLDER_PREFIX_PARAM, "some/folder/",
DETAIL_REPORT_NAME_PARAM, "detail_report.csv")));
assertThat(thrown).hasMessageThat().contains("FakeRegistrar");
}
@Test
public void testFailure_registrarHasNoDriveFolder() throws Exception {
persistResource(
loadRegistrar("TheRegistrar").asBuilder().setDriveFolderId(null).build());
thrown.expect(BadRequestException.class);
thrown.expectMessage("drive folder");
action.handleJsonRequest(ImmutableMap.of(
REGISTRAR_ID_PARAM, "TheRegistrar",
GCS_BUCKET_PARAM, "mah-buckit",
GCS_FOLDER_PREFIX_PARAM, "some/folder/",
DETAIL_REPORT_NAME_PARAM, "detail_report.csv"));
BadRequestException thrown =
expectThrows(
BadRequestException.class,
() ->
action.handleJsonRequest(
ImmutableMap.of(
REGISTRAR_ID_PARAM, "TheRegistrar",
GCS_BUCKET_PARAM, "mah-buckit",
GCS_FOLDER_PREFIX_PARAM, "some/folder/",
DETAIL_REPORT_NAME_PARAM, "detail_report.csv")));
assertThat(thrown).hasMessageThat().contains("drive folder");
}
@Test
public void testFailure_gcsBucketNotFound() throws Exception {
thrown.expect(BadRequestException.class);
thrown.expectMessage("fake-buckit");
action.handleJsonRequest(ImmutableMap.of(
REGISTRAR_ID_PARAM, "TheRegistrar",
GCS_BUCKET_PARAM, "fake-buckit",
GCS_FOLDER_PREFIX_PARAM, "some/folder/",
DETAIL_REPORT_NAME_PARAM, "detail_report.csv"));
BadRequestException thrown =
expectThrows(
BadRequestException.class,
() ->
action.handleJsonRequest(
ImmutableMap.of(
REGISTRAR_ID_PARAM, "TheRegistrar",
GCS_BUCKET_PARAM, "fake-buckit",
GCS_FOLDER_PREFIX_PARAM, "some/folder/",
DETAIL_REPORT_NAME_PARAM, "detail_report.csv")));
assertThat(thrown).hasMessageThat().contains("fake-buckit");
}
@Test
public void testFailure_gcsFileNotFound() throws Exception {
thrown.expect(BadRequestException.class);
thrown.expectMessage("some/folder/fake_file.csv");
action.handleJsonRequest(ImmutableMap.of(
REGISTRAR_ID_PARAM, "TheRegistrar",
GCS_BUCKET_PARAM, "mah-buckit",
GCS_FOLDER_PREFIX_PARAM, "some/folder/",
DETAIL_REPORT_NAME_PARAM, "fake_file.csv"));
BadRequestException thrown =
expectThrows(
BadRequestException.class,
() ->
action.handleJsonRequest(
ImmutableMap.of(
REGISTRAR_ID_PARAM, "TheRegistrar",
GCS_BUCKET_PARAM, "mah-buckit",
GCS_FOLDER_PREFIX_PARAM, "some/folder/",
DETAIL_REPORT_NAME_PARAM, "fake_file.csv")));
assertThat(thrown).hasMessageThat().contains("some/folder/fake_file.csv");
}
@Test
@ -197,12 +225,16 @@ public class PublishDetailReportActionTest {
when(driveConnection.createFile(
anyString(), any(MediaType.class), anyString(), any(byte[].class)))
.thenThrow(new IOException("Drive is down"));
thrown.expect(InternalServerErrorException.class);
thrown.expectMessage("Drive is down");
action.handleJsonRequest(ImmutableMap.of(
REGISTRAR_ID_PARAM, "TheRegistrar",
GCS_BUCKET_PARAM, "mah-buckit",
GCS_FOLDER_PREFIX_PARAM, "some/folder/",
DETAIL_REPORT_NAME_PARAM, "detail_report.csv"));
InternalServerErrorException thrown =
expectThrows(
InternalServerErrorException.class,
() ->
action.handleJsonRequest(
ImmutableMap.of(
REGISTRAR_ID_PARAM, "TheRegistrar",
GCS_BUCKET_PARAM, "mah-buckit",
GCS_FOLDER_PREFIX_PARAM, "some/folder/",
DETAIL_REPORT_NAME_PARAM, "detail_report.csv")));
assertThat(thrown).hasMessageThat().contains("Drive is down");
}
}

View file

@ -47,7 +47,6 @@ import google.registry.util.Retrier;
import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -65,9 +64,6 @@ public class SyncGroupMembersActionTest {
.withDatastore()
.build();
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Rule
public final InjectRule inject = new InjectRule();

View file

@ -21,6 +21,7 @@ import static google.registry.export.UpdateSnapshotViewAction.UPDATE_SNAPSHOT_DA
import static google.registry.export.UpdateSnapshotViewAction.UPDATE_SNAPSHOT_KIND_PARAM;
import static google.registry.export.UpdateSnapshotViewAction.UPDATE_SNAPSHOT_TABLE_ID_PARAM;
import static google.registry.export.UpdateSnapshotViewAction.createViewUpdateTask;
import static google.registry.testing.JUnitBackports.expectThrows;
import static google.registry.testing.TaskQueueHelper.assertTasksEnqueued;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
@ -41,7 +42,6 @@ import java.io.IOException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentCaptor;
@ -55,10 +55,6 @@ public class UpdateSnapshotViewActionTest {
public final AppEngineRule appEngine = AppEngineRule.builder()
.withTaskQueue()
.build();
@Rule
public final ExpectedException thrown = ExpectedException.none();
private final BigqueryFactory bigqueryFactory = mock(BigqueryFactory.class);
private final Bigquery bigquery = mock(Bigquery.class);
private final Bigquery.Datasets bigqueryDatasets = mock(Bigquery.Datasets.class);
@ -127,8 +123,8 @@ public class UpdateSnapshotViewActionTest {
public void testFailure_bigqueryConnectionThrowsError() throws Exception {
when(bigqueryTables.update(anyString(), anyString(), anyString(), any(Table.class)))
.thenThrow(new IOException("I'm sorry Dave, I can't let you do that"));
thrown.expect(InternalServerErrorException.class);
thrown.expectMessage("Error in update snapshot view action");
action.run();
InternalServerErrorException thrown =
expectThrows(InternalServerErrorException.class, () -> action.run());
assertThat(thrown).hasMessageThat().contains("Error in update snapshot view action");
}
}