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,7 @@ import static com.google.common.net.MediaType.CSV_UTF_8;
import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.JUnitBackports.expectThrows;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.api.client.http.LowLevelHttpRequest;
@ -35,7 +36,6 @@ 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;
@ -50,9 +50,6 @@ public class IcannHttpReporterTest {
private static final byte[] FAKE_PAYLOAD = "test,csv\n1,2".getBytes(UTF_8);
private MockLowLevelHttpRequest mockRequest;
@Rule public final ExpectedException thrown = ExpectedException.none();
@Rule public AppEngineRule appEngineRule = new AppEngineRule.Builder().withDatastore().build();
private MockHttpTransport createMockTransport (final ByteSource iirdeaResponse) {
@ -127,36 +124,60 @@ public class IcannHttpReporterTest {
@Test
public void testFail_invalidFilename_nonSixDigitYearMonth() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage(
"Expected file format: tld-reportType-yyyyMM.csv, got test-transactions-20176.csv instead");
IcannHttpReporter reporter = createReporter();
reporter.send(FAKE_PAYLOAD, "test-transactions-20176.csv");
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() -> {
IcannHttpReporter reporter = createReporter();
reporter.send(FAKE_PAYLOAD, "test-transactions-20176.csv");
});
assertThat(thrown)
.hasMessageThat()
.contains(
"Expected file format: tld-reportType-yyyyMM.csv, "
+ "got test-transactions-20176.csv instead");
}
@Test
public void testFail_invalidFilename_notActivityOrTransactions() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage(
"Expected file format: tld-reportType-yyyyMM.csv, got test-invalid-201706.csv instead");
IcannHttpReporter reporter = createReporter();
reporter.send(FAKE_PAYLOAD, "test-invalid-201706.csv");
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() -> {
IcannHttpReporter reporter = createReporter();
reporter.send(FAKE_PAYLOAD, "test-invalid-201706.csv");
});
assertThat(thrown)
.hasMessageThat()
.contains(
"Expected file format: tld-reportType-yyyyMM.csv, got test-invalid-201706.csv instead");
}
@Test
public void testFail_invalidFilename_invalidTldName() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage(
"Expected file format: tld-reportType-yyyyMM.csv, got n!-n-activity-201706.csv instead");
IcannHttpReporter reporter = createReporter();
reporter.send(FAKE_PAYLOAD, "n!-n-activity-201706.csv");
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() -> {
IcannHttpReporter reporter = createReporter();
reporter.send(FAKE_PAYLOAD, "n!-n-activity-201706.csv");
});
assertThat(thrown)
.hasMessageThat()
.contains(
"Expected file format: tld-reportType-yyyyMM.csv, "
+ "got n!-n-activity-201706.csv instead");
}
@Test
public void testFail_invalidFilename_tldDoesntExist() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("TLD hello does not exist");
IcannHttpReporter reporter = createReporter();
reporter.send(FAKE_PAYLOAD, "hello-activity-201706.csv");
IllegalArgumentException thrown =
expectThrows(
IllegalArgumentException.class,
() -> {
IcannHttpReporter reporter = createReporter();
reporter.send(FAKE_PAYLOAD, "hello-activity-201706.csv");
});
assertThat(thrown).hasMessageThat().contains("TLD hello does not exist");
}
}

View file

@ -15,6 +15,7 @@
package google.registry.reporting;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.JUnitBackports.expectThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -27,9 +28,7 @@ import javax.servlet.http.HttpServletRequest;
import org.joda.time.DateTime;
import org.joda.time.YearMonth;
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;
@ -39,9 +38,6 @@ public class IcannReportingModuleTest {
HttpServletRequest req = mock(HttpServletRequest.class);
Clock clock;
@Rule public final ExpectedException thrown = ExpectedException.none();
@Before
public void setUp() {
clock = new FakeClock(DateTime.parse("2017-07-01TZ"));
@ -56,9 +52,12 @@ public class IcannReportingModuleTest {
@Test
public void testInvalidYearMonthParameter_throwsException() {
when(req.getParameter("yearMonth")).thenReturn("201705");
thrown.expect(BadRequestException.class);
thrown.expectMessage("yearMonth must be in yyyy-MM format, got 201705 instead");
IcannReportingModule.provideYearMonthOptional(req);
BadRequestException thrown =
expectThrows(
BadRequestException.class, () -> IcannReportingModule.provideYearMonthOptional(req));
assertThat(thrown)
.hasMessageThat()
.contains("yearMonth must be in yyyy-MM format, got 201705 instead");
}
@Test
@ -88,9 +87,14 @@ public class IcannReportingModuleTest {
@Test
public void testInvalidSubdir_throwsException() {
thrown.expect(BadRequestException.class);
thrown.expectMessage("subdir must not start or end with a \"/\", got /whoops instead.");
IcannReportingModule.provideSubdir(Optional.of("/whoops"), new YearMonth(2017, 6));
BadRequestException thrown =
expectThrows(
BadRequestException.class,
() ->
IcannReportingModule.provideSubdir(Optional.of("/whoops"), new YearMonth(2017, 6)));
assertThat(thrown)
.hasMessageThat()
.contains("subdir must not start or end with a \"/\", got /whoops instead.");
}
@Test