diff --git a/javatests/google/registry/batch/DeleteProberDataActionTest.java b/javatests/google/registry/batch/DeleteProberDataActionTest.java index 606fe2e41..4da8fc51a 100644 --- a/javatests/google/registry/batch/DeleteProberDataActionTest.java +++ b/javatests/google/registry/batch/DeleteProberDataActionTest.java @@ -15,7 +15,6 @@ package google.registry.batch; import static com.google.common.truth.Truth.assertThat; -import static com.google.common.truth.Truth.assert_; import static google.registry.model.EppResourceUtils.loadByForeignKey; import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.testing.DatastoreHelper.createTld; @@ -26,6 +25,7 @@ import static google.registry.testing.DatastoreHelper.persistDeletedDomain; import static google.registry.testing.DatastoreHelper.persistDomainAsDeleted; import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.DatastoreHelper.persistSimpleResource; +import static google.registry.testing.JUnitBackports.expectThrows; import static google.registry.testing.TaskQueueHelper.assertDnsTasksEnqueued; import static google.registry.util.DateTimeUtils.END_OF_TIME; import static google.registry.util.DateTimeUtils.START_OF_TIME; @@ -207,13 +207,8 @@ public class DeleteProberDataActionTest extends MapreduceTestCase readGcsFile(gcsService, nonexistentFile)); + ListResult ls = gcsService.list("outputbucket", ListOptions.DEFAULT); + assertThat(ls.next().getName()).isEqualTo("tld.txt"); + // Make sure that no other files were written out. + assertThat(ls.hasNext()).isFalse(); } @Test diff --git a/javatests/google/registry/export/ExportReservedTermsActionTest.java b/javatests/google/registry/export/ExportReservedTermsActionTest.java index 51e438b00..a593c5faa 100644 --- a/javatests/google/registry/export/ExportReservedTermsActionTest.java +++ b/javatests/google/registry/export/ExportReservedTermsActionTest.java @@ -15,12 +15,12 @@ package google.registry.export; import static com.google.common.truth.Truth.assertThat; -import static com.google.common.truth.Truth.assertWithMessage; import static google.registry.export.ExportReservedTermsAction.EXPORT_MIME_TYPE; import static google.registry.export.ExportReservedTermsAction.RESERVED_TERMS_FILENAME; import static google.registry.testing.DatastoreHelper.createTld; import static google.registry.testing.DatastoreHelper.persistReservedList; import static google.registry.testing.DatastoreHelper.persistResource; +import static google.registry.testing.JUnitBackports.expectThrows; import static java.nio.charset.StandardCharsets.UTF_8; import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR; import static javax.servlet.http.HttpServletResponse.SC_OK; @@ -123,26 +123,18 @@ public class ExportReservedTermsActionTest { any(MediaType.class), anyString(), any(byte[].class))).thenThrow(new IOException("errorMessage")); - try { - runAction("tld"); - assertWithMessage("Expected RuntimeException to be thrown").fail(); - } catch (RuntimeException e) { - verify(response).setStatus(SC_INTERNAL_SERVER_ERROR); - assertThat(e).hasCauseThat().hasMessageThat().isEqualTo("errorMessage"); - } + RuntimeException thrown = expectThrows(RuntimeException.class, () -> runAction("tld")); + verify(response).setStatus(SC_INTERNAL_SERVER_ERROR); + assertThat(thrown).hasCauseThat().hasMessageThat().isEqualTo("errorMessage"); } @Test public void test_uploadFileToDrive_failsWhenTldDoesntExist() throws Exception { - try { - runAction("fakeTld"); - assertWithMessage("Expected RuntimeException to be thrown").fail(); - } catch (RuntimeException e) { - verify(response).setStatus(SC_INTERNAL_SERVER_ERROR); - assertThat(e) - .hasCauseThat() - .hasMessageThat() - .isEqualTo("No registry object found for fakeTld"); - } + RuntimeException thrown = expectThrows(RuntimeException.class, () -> runAction("fakeTld")); + verify(response).setStatus(SC_INTERNAL_SERVER_ERROR); + assertThat(thrown) + .hasCauseThat() + .hasMessageThat() + .isEqualTo("No registry object found for fakeTld"); } } diff --git a/javatests/google/registry/flows/domain/DomainApplicationCreateFlowTest.java b/javatests/google/registry/flows/domain/DomainApplicationCreateFlowTest.java index 90dff8c97..d79b6c925 100644 --- a/javatests/google/registry/flows/domain/DomainApplicationCreateFlowTest.java +++ b/javatests/google/registry/flows/domain/DomainApplicationCreateFlowTest.java @@ -17,7 +17,6 @@ package google.registry.flows.domain; import static com.google.common.collect.Iterables.getLast; import static com.google.common.io.BaseEncoding.base16; import static com.google.common.truth.Truth.assertThat; -import static com.google.common.truth.Truth.assert_; import static google.registry.model.index.DomainApplicationIndex.loadActiveApplicationsByDomainName; import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.testing.DatastoreHelper.assertNoBillingEvents; @@ -1603,21 +1602,14 @@ public class DomainApplicationCreateFlowTest persistContactsAndHosts(); clock.advanceOneMilli(); persistActiveDomain(getUniqueIdFromCommand()); - try { - runFlow(); - assert_() - .fail( - "Expected to throw ResourceAlreadyExistsException with message " - + "Object with given ID (%s) already exists", - getUniqueIdFromCommand()); - } catch (ResourceAlreadyExistsException e) { - assertAboutEppExceptions() - .that(e) - .marshalsToXml() - .and() - .hasMessage( - String.format("Object with given ID (%s) already exists", getUniqueIdFromCommand())); - } + ResourceAlreadyExistsException thrown = + expectThrows(ResourceAlreadyExistsException.class, this::runFlow); + assertAboutEppExceptions() + .that(thrown) + .marshalsToXml() + .and() + .hasMessage( + String.format("Object with given ID (%s) already exists", getUniqueIdFromCommand())); } @Test @@ -1631,7 +1623,7 @@ public class DomainApplicationCreateFlowTest .build()); RegistrantNotAllowedException thrown = expectThrows(RegistrantNotAllowedException.class, this::runFlow); - assertThat(thrown).hasMessageThat().contains("jd1234"); + assertAboutEppExceptions().that(thrown).marshalsToXml().and().hasMessageThatContains("jd1234"); } @Test @@ -1644,7 +1636,11 @@ public class DomainApplicationCreateFlowTest .build()); NameserversNotAllowedForTldException thrown = expectThrows(NameserversNotAllowedForTldException.class, this::runFlow); - assertThat(thrown).hasMessageThat().contains("ns1.example.net"); + assertAboutEppExceptions() + .that(thrown) + .marshalsToXml() + .and() + .hasMessageThatContains("ns1.example.net"); } @Test diff --git a/javatests/google/registry/flows/domain/DomainCreateFlowTest.java b/javatests/google/registry/flows/domain/DomainCreateFlowTest.java index e3e436c8c..00c3f2c5b 100644 --- a/javatests/google/registry/flows/domain/DomainCreateFlowTest.java +++ b/javatests/google/registry/flows/domain/DomainCreateFlowTest.java @@ -16,7 +16,6 @@ package google.registry.flows.domain; import static com.google.common.io.BaseEncoding.base16; import static com.google.common.truth.Truth.assertThat; -import static com.google.common.truth.Truth.assert_; import static com.google.common.truth.Truth8.assertThat; import static google.registry.model.domain.fee.Fee.FEE_EXTENSION_URIS; import static google.registry.model.eppcommon.StatusValue.OK; @@ -998,21 +997,14 @@ public class DomainCreateFlowTest extends ResourceFlowTestCase key = reader.next(); + assert_().fail("Unexpected element: " + key); } catch (NoSuchElementException expected) { } } @@ -88,10 +88,12 @@ public final class CommitLogManifestInputTest { reader.beginShard(); reader.beginSlice(); try { + Key key = null; for (int i = 0; i < 10; i++) { - seen.add(reader.next()); + key = reader.next(); + seen.add(key); } - assert_().fail("Unexpected element"); + assert_().fail("Unexpected element: " + key); } catch (NoSuchElementException expected) { } } @@ -114,10 +116,12 @@ public final class CommitLogManifestInputTest { reader.beginShard(); reader.beginSlice(); try { + Key key = null; for (int i = 0; i < 10; i++) { - seen.add(reader.next()); + key = reader.next(); + seen.add(key); } - assert_().fail("Unexpected element"); + assert_().fail("Unexpected element: " + key); } catch (NoSuchElementException expected) { } } diff --git a/javatests/google/registry/mapreduce/inputs/EppResourceInputsTest.java b/javatests/google/registry/mapreduce/inputs/EppResourceInputsTest.java index be9f4800b..74d09c750 100644 --- a/javatests/google/registry/mapreduce/inputs/EppResourceInputsTest.java +++ b/javatests/google/registry/mapreduce/inputs/EppResourceInputsTest.java @@ -140,8 +140,8 @@ public class EppResourceInputsTest { reader.beginSlice(); seen.add(reader.next()); try { - reader.next(); - assert_().fail("Unexpected element"); + Key key = reader.next(); + assert_().fail("Unexpected element: " + key); } catch (NoSuchElementException expected) { } } @@ -165,8 +165,8 @@ public class EppResourceInputsTest { reader.beginSlice(); seen.add(reader.next()); try { - reader.next(); - assert_().fail("Unexpected element"); + DomainResource domain = reader.next(); + assert_().fail("Unexpected element: " + domain); } catch (NoSuchElementException expected) { } } diff --git a/javatests/google/registry/reporting/IcannReportingStagingActionTest.java b/javatests/google/registry/reporting/IcannReportingStagingActionTest.java index 8b2e4febc..da0580589 100644 --- a/javatests/google/registry/reporting/IcannReportingStagingActionTest.java +++ b/javatests/google/registry/reporting/IcannReportingStagingActionTest.java @@ -15,7 +15,7 @@ package google.registry.reporting; import static com.google.common.truth.Truth.assertThat; -import static com.google.common.truth.Truth.assertWithMessage; +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; @@ -135,13 +135,9 @@ public class IcannReportingStagingActionTest { createAction(ImmutableList.of(ReportType.ACTIVITY)); when(stager.stageReports(ReportType.ACTIVITY)) .thenThrow(new BigqueryJobFailureException("Expected failure", null, null, null)); - try { - action.run(); - assertWithMessage("Expected to encounter a BigqueryJobFailureException").fail(); - } catch (BigqueryJobFailureException expected) { - // Expect the exception. - assertThat(expected).hasMessageThat().isEqualTo("Expected failure"); - } + BigqueryJobFailureException thrown = + expectThrows(BigqueryJobFailureException.class, action::run); + assertThat(thrown).hasMessageThat().isEqualTo("Expected failure"); verify(stager, times(3)).stageReports(ReportType.ACTIVITY); verify(emailUtils) .emailResults( diff --git a/javatests/google/registry/reporting/IcannReportingUploadActionTest.java b/javatests/google/registry/reporting/IcannReportingUploadActionTest.java index 6ad2bd5ff..3fbffce27 100644 --- a/javatests/google/registry/reporting/IcannReportingUploadActionTest.java +++ b/javatests/google/registry/reporting/IcannReportingUploadActionTest.java @@ -15,8 +15,8 @@ package google.registry.reporting; import static com.google.common.truth.Truth.assertThat; -import static com.google.common.truth.Truth.assertWithMessage; import static google.registry.testing.GcsTestingUtils.writeGcsFile; +import static google.registry.testing.JUnitBackports.expectThrows; import static java.nio.charset.StandardCharsets.UTF_8; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; @@ -144,14 +144,10 @@ public class IcannReportingUploadActionTest { public void testFail_FileNotFound() throws Exception { IcannReportingUploadAction action = createAction(); action.subdir = "somewhere/else"; - try { - action.run(); - assertWithMessage("Expected IllegalStateException to be thrown").fail(); - } catch (IllegalArgumentException expected) { - assertThat(expected) - .hasMessageThat() - .isEqualTo("Object MANIFEST.txt in bucket basin/somewhere/else not found"); - } + IllegalArgumentException thrown = expectThrows(IllegalArgumentException.class, action::run); + assertThat(thrown) + .hasMessageThat() + .isEqualTo("Object MANIFEST.txt in bucket basin/somewhere/else not found"); } } diff --git a/javatests/google/registry/testing/EppExceptionSubject.java b/javatests/google/registry/testing/EppExceptionSubject.java index 4d3b12760..ecbb1df17 100644 --- a/javatests/google/registry/testing/EppExceptionSubject.java +++ b/javatests/google/registry/testing/EppExceptionSubject.java @@ -41,6 +41,11 @@ public class EppExceptionSubject extends Subject(this); } + public And hasMessageThatContains(String expected) { + assertThat(actual()).hasMessageThat().contains(expected); + return new And<>(this); + } + public And marshalsToXml() { // Attempt to marshal the exception to EPP. If it doesn't work, this will throw. try { diff --git a/javatests/google/registry/tools/CreateReservedListCommandTest.java b/javatests/google/registry/tools/CreateReservedListCommandTest.java index bbd4c5187..ce8445c98 100644 --- a/javatests/google/registry/tools/CreateReservedListCommandTest.java +++ b/javatests/google/registry/tools/CreateReservedListCommandTest.java @@ -15,7 +15,6 @@ package google.registry.tools; import static com.google.common.truth.Truth.assertThat; -import static com.google.common.truth.Truth.assertWithMessage; import static com.google.common.truth.Truth8.assertThat; import static google.registry.model.registry.label.ReservationType.FULLY_BLOCKED; import static google.registry.testing.DatastoreHelper.createTlds; @@ -175,13 +174,12 @@ public class CreateReservedListCommandTest extends } private void runNameTestExpectedFailure(String name, String expectedErrorMsg) throws Exception { - try { - runCommandForced("--name=" + name, "--input=" + reservedTermsPath); - assertWithMessage("Expected IllegalArgumentException to be thrown").fail(); - } catch (IllegalArgumentException e) { - assertThat(ReservedList.get(name)).isEmpty(); - assertThat(e).hasMessageThat().isEqualTo(expectedErrorMsg); - } + IllegalArgumentException thrown = + expectThrows( + IllegalArgumentException.class, + () -> runCommandForced("--name=" + name, "--input=" + reservedTermsPath)); + assertThat(ReservedList.get(name)).isEmpty(); + assertThat(thrown).hasMessageThat().isEqualTo(expectedErrorMsg); } private void runNameTestWithOverride(String name) throws Exception { diff --git a/javatests/google/registry/tools/DeletePremiumListCommandTest.java b/javatests/google/registry/tools/DeletePremiumListCommandTest.java index 2a0202fc2..cb747fb29 100644 --- a/javatests/google/registry/tools/DeletePremiumListCommandTest.java +++ b/javatests/google/registry/tools/DeletePremiumListCommandTest.java @@ -15,7 +15,6 @@ package google.registry.tools; import static com.google.common.truth.Truth.assertThat; -import static com.google.common.truth.Truth.assertWithMessage; import static com.google.common.truth.Truth8.assertThat; import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.testing.DatastoreHelper.createTld; @@ -61,14 +60,13 @@ public class DeletePremiumListCommandTest extends CommandTestCase runCommandForced("--name=" + premiumList.getName())); + assertThat(PremiumList.get(premiumList.getName())).isPresent(); + assertThat(thrown) + .hasMessageThat() + .isEqualTo("Cannot delete premium list because it is used on these tld(s): xn--q9jyb4c"); } } diff --git a/javatests/google/registry/tools/DeleteReservedListCommandTest.java b/javatests/google/registry/tools/DeleteReservedListCommandTest.java index a9193828c..00d38c1fa 100644 --- a/javatests/google/registry/tools/DeleteReservedListCommandTest.java +++ b/javatests/google/registry/tools/DeleteReservedListCommandTest.java @@ -15,7 +15,6 @@ package google.registry.tools; import static com.google.common.truth.Truth.assertThat; -import static com.google.common.truth.Truth.assertWithMessage; import static com.google.common.truth.Truth8.assertThat; import static google.registry.testing.DatastoreHelper.createTld; import static google.registry.testing.DatastoreHelper.persistReservedList; @@ -59,14 +58,13 @@ public class DeleteReservedListCommandTest extends CommandTestCase runCommandForced("--name=" + reservedList.getName())); + assertThat(ReservedList.get(reservedList.getName())).isPresent(); + assertThat(thrown) + .hasMessageThat() + .isEqualTo("Cannot delete reserved list because it is used on these tld(s): xn--q9jyb4c"); } } diff --git a/javatests/google/registry/tools/MutatingCommandTest.java b/javatests/google/registry/tools/MutatingCommandTest.java index 6df0cd25a..d46c9071a 100644 --- a/javatests/google/registry/tools/MutatingCommandTest.java +++ b/javatests/google/registry/tools/MutatingCommandTest.java @@ -15,7 +15,6 @@ package google.registry.tools; import static com.google.common.truth.Truth.assertThat; -import static com.google.common.truth.Truth.assertWithMessage; import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.testing.DatastoreHelper.createTld; import static google.registry.testing.DatastoreHelper.deleteResource; @@ -273,18 +272,14 @@ public class MutatingCommandTest { + "\n" + "Update Registrar@Registrar2\n" + "blockPremiumNames: false -> true\n"); - try { - command.execute(); - assertWithMessage("Expected transaction to fail with IllegalStateException").fail(); - } catch (IllegalStateException e) { - assertThat(e.getMessage()).contains("Entity changed since init() was called."); - assertThat(ofy().load().entity(host1).now()).isNull(); - assertThat(ofy().load().entity(host2).now()).isEqualTo(newHost2); - // These two shouldn't've changed. - assertThat(ofy().load().entity(registrar1).now()).isEqualTo(registrar1); - assertThat(ofy().load().entity(registrar2).now()).isEqualTo(registrar2); - } + IllegalStateException thrown = expectThrows(IllegalStateException.class, command::execute); + assertThat(thrown).hasMessageThat().contains("Entity changed since init() was called."); + assertThat(ofy().load().entity(host1).now()).isNull(); + assertThat(ofy().load().entity(host2).now()).isEqualTo(newHost2); + // These two shouldn't've changed. + assertThat(ofy().load().entity(registrar1).now()).isEqualTo(registrar1); + assertThat(ofy().load().entity(registrar2).now()).isEqualTo(registrar2); } @Test diff --git a/javatests/google/registry/tools/RegistrarContactCommandTest.java b/javatests/google/registry/tools/RegistrarContactCommandTest.java index d9352f882..056d38bbf 100644 --- a/javatests/google/registry/tools/RegistrarContactCommandTest.java +++ b/javatests/google/registry/tools/RegistrarContactCommandTest.java @@ -23,6 +23,7 @@ import static google.registry.testing.DatastoreHelper.loadRegistrar; import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.DatastoreHelper.persistSimpleResource; import static google.registry.testing.DatastoreHelper.persistSimpleResources; +import static google.registry.testing.JUnitBackports.expectThrows; import static java.nio.charset.StandardCharsets.UTF_8; import com.google.common.collect.ImmutableList; @@ -191,17 +192,18 @@ public class RegistrarContactCommandTest extends CommandTestCase + runCommandForced( + "--mode=UPDATE", + "--email=john.doe@example.com", + "--visible_in_domain_whois_as_abuse=false", + "NewRegistrar")); + assertThat(thrown) + .hasMessageThat() + .contains("Cannot clear visible_in_domain_whois_as_abuse flag"); RegistrarContact registrarContact = loadRegistrar("NewRegistrar").getContacts().asList().get(1); assertThat(registrarContact.getVisibleInDomainWhoisAsAbuse()).isTrue(); } @@ -323,16 +325,13 @@ public class RegistrarContactCommandTest extends CommandTestCase + runCommandForced( + "--mode=DELETE", "--email=janedoe@theregistrar.com", "NewRegistrar")); + assertThat(thrown).hasMessageThat().contains("Cannot delete the domain WHOIS abuse contact"); assertThat(loadRegistrar("NewRegistrar").getContacts()).isNotEmpty(); } diff --git a/javatests/google/registry/whois/WhoisHttpServerTest.java b/javatests/google/registry/whois/WhoisHttpServerTest.java index ea73e27cf..31d820d72 100644 --- a/javatests/google/registry/whois/WhoisHttpServerTest.java +++ b/javatests/google/registry/whois/WhoisHttpServerTest.java @@ -16,7 +16,6 @@ package google.registry.whois; import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8; import static com.google.common.truth.Truth.assertThat; -import static com.google.common.truth.Truth.assert_; import static google.registry.testing.DatastoreHelper.createTlds; import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.DatastoreHelper.persistSimpleResources; @@ -25,6 +24,7 @@ import static google.registry.testing.FullFieldsTestEntityHelper.makeDomainResou import static google.registry.testing.FullFieldsTestEntityHelper.makeHostResource; import static google.registry.testing.FullFieldsTestEntityHelper.makeRegistrar; import static google.registry.testing.FullFieldsTestEntityHelper.makeRegistrarContacts; +import static google.registry.testing.JUnitBackports.expectThrows; import static google.registry.whois.WhoisTestData.loadFile; import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST; import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR; @@ -379,12 +379,8 @@ public class WhoisHttpServerTest { .thenThrow(new IOException("missing cat interface")); server.whoisMetrics = mock(WhoisMetrics.class); - try { - server.run(); - assert_().fail("Should have thrown RuntimeException"); - } catch (RuntimeException e) { - assertThat(e.getCause().getMessage()).isEqualTo("missing cat interface"); - } + RuntimeException thrown = expectThrows(RuntimeException.class, server::run); + assertThat(thrown).hasCauseThat().hasMessageThat().isEqualTo("missing cat interface"); WhoisMetric expected = WhoisMetric.builderForRequest(clock) .setNumResults(0)