Migrate final try/catch test assertions to use assert/expectThrows

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=182091814
This commit is contained in:
mcilwain 2018-01-16 12:08:08 -08:00 committed by Ben McIlwain
parent fe7cc4f782
commit c416b3892d
17 changed files with 124 additions and 172 deletions

View file

@ -15,7 +15,6 @@
package google.registry.batch; package google.registry.batch;
import static com.google.common.truth.Truth.assertThat; 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.EppResourceUtils.loadByForeignKey;
import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.testing.DatastoreHelper.createTld; 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.persistDomainAsDeleted;
import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.DatastoreHelper.persistSimpleResource; 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.testing.TaskQueueHelper.assertDnsTasksEnqueued;
import static google.registry.util.DateTimeUtils.END_OF_TIME; import static google.registry.util.DateTimeUtils.END_OF_TIME;
import static google.registry.util.DateTimeUtils.START_OF_TIME; import static google.registry.util.DateTimeUtils.START_OF_TIME;
@ -207,13 +207,8 @@ public class DeleteProberDataActionTest extends MapreduceTestCase<DeleteProberDa
.setCreationTimeForTest(DateTime.now(UTC).minusYears(1)) .setCreationTimeForTest(DateTime.now(UTC).minusYears(1))
.build()); .build());
action.registryAdminClientId = null; action.registryAdminClientId = null;
try { IllegalStateException thrown = expectThrows(IllegalStateException.class, this::runMapreduce);
runMapreduce(); assertThat(thrown).hasMessageThat().contains("Registry admin client ID must be configured");
} catch (IllegalStateException e) {
assertThat(e).hasMessageThat().contains("Registry admin client ID must be configured");
return;
}
assert_().fail("Expected IllegalStateException");
} }
/** /**

View file

@ -15,10 +15,10 @@
package google.registry.billing; package google.registry.billing;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static google.registry.testing.DatastoreHelper.loadRegistrar; import static google.registry.testing.DatastoreHelper.loadRegistrar;
import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.GcsTestingUtils.writeGcsFile; import static google.registry.testing.GcsTestingUtils.writeGcsFile;
import static google.registry.testing.JUnitBackports.expectThrows;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import static javax.servlet.http.HttpServletResponse.SC_OK; import static javax.servlet.http.HttpServletResponse.SC_OK;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
@ -169,12 +169,8 @@ public class CopyDetailReportsActionTest {
when(driveConnection.createFile(any(), any(), any(), any())) when(driveConnection.createFile(any(), any(), any(), any()))
.thenThrow(new IOException("expected")); .thenThrow(new IOException("expected"));
try { RuntimeException thrown = expectThrows(RuntimeException.class, action::run);
action.run(); assertThat(thrown).hasMessageThat().isEqualTo("java.io.IOException: expected");
assertWithMessage("Expected a runtime exception to be thrown!").fail();
} catch (RuntimeException e) {
assertThat(e).hasMessageThat().isEqualTo("java.io.IOException: expected");
}
verify(driveConnection, times(3)) verify(driveConnection, times(3))
.createFile( .createFile(
"invoice_details_2017-10_TheRegistrar_hello.csv", "invoice_details_2017-10_TheRegistrar_hello.csv",

View file

@ -16,13 +16,13 @@ package google.registry.export;
import static com.google.appengine.tools.cloudstorage.GcsServiceFactory.createGcsService; import static com.google.appengine.tools.cloudstorage.GcsServiceFactory.createGcsService;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static google.registry.testing.DatastoreHelper.createTld; import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.persistActiveDomain; import static google.registry.testing.DatastoreHelper.persistActiveDomain;
import static google.registry.testing.DatastoreHelper.persistActiveDomainApplication; import static google.registry.testing.DatastoreHelper.persistActiveDomainApplication;
import static google.registry.testing.DatastoreHelper.persistDeletedDomain; import static google.registry.testing.DatastoreHelper.persistDeletedDomain;
import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.GcsTestingUtils.readGcsFile; import static google.registry.testing.GcsTestingUtils.readGcsFile;
import static google.registry.testing.JUnitBackports.assertThrows;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.appengine.tools.cloudstorage.GcsFilename; import com.google.appengine.tools.cloudstorage.GcsFilename;
@ -90,15 +90,11 @@ public class ExportDomainListsActionTest extends MapreduceTestCase<ExportDomainL
assertThat(Splitter.on('\n').splitToList(tlds)).containsExactly("onetwo.tld", "rudnitzky.tld"); assertThat(Splitter.on('\n').splitToList(tlds)).containsExactly("onetwo.tld", "rudnitzky.tld");
// Make sure that the test TLD file wasn't written out. // Make sure that the test TLD file wasn't written out.
GcsFilename nonexistentFile = new GcsFilename("outputbucket", "testtld.txt"); GcsFilename nonexistentFile = new GcsFilename("outputbucket", "testtld.txt");
try { assertThrows(FileNotFoundException.class, () -> readGcsFile(gcsService, nonexistentFile));
readGcsFile(gcsService, nonexistentFile); ListResult ls = gcsService.list("outputbucket", ListOptions.DEFAULT);
assertWithMessage("Expected FileNotFoundException to be thrown").fail(); assertThat(ls.next().getName()).isEqualTo("tld.txt");
} catch (FileNotFoundException e) { // Make sure that no other files were written out.
ListResult ls = gcsService.list("outputbucket", ListOptions.DEFAULT); assertThat(ls.hasNext()).isFalse();
assertThat(ls.next().getName()).isEqualTo("tld.txt");
// Make sure that no other files were written out.
assertThat(ls.hasNext()).isFalse();
}
} }
@Test @Test

View file

@ -15,12 +15,12 @@
package google.registry.export; package google.registry.export;
import static com.google.common.truth.Truth.assertThat; 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.EXPORT_MIME_TYPE;
import static google.registry.export.ExportReservedTermsAction.RESERVED_TERMS_FILENAME; import static google.registry.export.ExportReservedTermsAction.RESERVED_TERMS_FILENAME;
import static google.registry.testing.DatastoreHelper.createTld; import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.persistReservedList; import static google.registry.testing.DatastoreHelper.persistReservedList;
import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.JUnitBackports.expectThrows;
import static java.nio.charset.StandardCharsets.UTF_8; 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_INTERNAL_SERVER_ERROR;
import static javax.servlet.http.HttpServletResponse.SC_OK; import static javax.servlet.http.HttpServletResponse.SC_OK;
@ -123,26 +123,18 @@ public class ExportReservedTermsActionTest {
any(MediaType.class), any(MediaType.class),
anyString(), anyString(),
any(byte[].class))).thenThrow(new IOException("errorMessage")); any(byte[].class))).thenThrow(new IOException("errorMessage"));
try { RuntimeException thrown = expectThrows(RuntimeException.class, () -> runAction("tld"));
runAction("tld"); verify(response).setStatus(SC_INTERNAL_SERVER_ERROR);
assertWithMessage("Expected RuntimeException to be thrown").fail(); assertThat(thrown).hasCauseThat().hasMessageThat().isEqualTo("errorMessage");
} catch (RuntimeException e) {
verify(response).setStatus(SC_INTERNAL_SERVER_ERROR);
assertThat(e).hasCauseThat().hasMessageThat().isEqualTo("errorMessage");
}
} }
@Test @Test
public void test_uploadFileToDrive_failsWhenTldDoesntExist() throws Exception { public void test_uploadFileToDrive_failsWhenTldDoesntExist() throws Exception {
try { RuntimeException thrown = expectThrows(RuntimeException.class, () -> runAction("fakeTld"));
runAction("fakeTld"); verify(response).setStatus(SC_INTERNAL_SERVER_ERROR);
assertWithMessage("Expected RuntimeException to be thrown").fail(); assertThat(thrown)
} catch (RuntimeException e) { .hasCauseThat()
verify(response).setStatus(SC_INTERNAL_SERVER_ERROR); .hasMessageThat()
assertThat(e) .isEqualTo("No registry object found for fakeTld");
.hasCauseThat()
.hasMessageThat()
.isEqualTo("No registry object found for fakeTld");
}
} }
} }

View file

@ -17,7 +17,6 @@ package google.registry.flows.domain;
import static com.google.common.collect.Iterables.getLast; import static com.google.common.collect.Iterables.getLast;
import static com.google.common.io.BaseEncoding.base16; import static com.google.common.io.BaseEncoding.base16;
import static com.google.common.truth.Truth.assertThat; 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.index.DomainApplicationIndex.loadActiveApplicationsByDomainName;
import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.testing.DatastoreHelper.assertNoBillingEvents; import static google.registry.testing.DatastoreHelper.assertNoBillingEvents;
@ -1603,21 +1602,14 @@ public class DomainApplicationCreateFlowTest
persistContactsAndHosts(); persistContactsAndHosts();
clock.advanceOneMilli(); clock.advanceOneMilli();
persistActiveDomain(getUniqueIdFromCommand()); persistActiveDomain(getUniqueIdFromCommand());
try { ResourceAlreadyExistsException thrown =
runFlow(); expectThrows(ResourceAlreadyExistsException.class, this::runFlow);
assert_() assertAboutEppExceptions()
.fail( .that(thrown)
"Expected to throw ResourceAlreadyExistsException with message " .marshalsToXml()
+ "Object with given ID (%s) already exists", .and()
getUniqueIdFromCommand()); .hasMessage(
} catch (ResourceAlreadyExistsException e) { String.format("Object with given ID (%s) already exists", getUniqueIdFromCommand()));
assertAboutEppExceptions()
.that(e)
.marshalsToXml()
.and()
.hasMessage(
String.format("Object with given ID (%s) already exists", getUniqueIdFromCommand()));
}
} }
@Test @Test
@ -1631,7 +1623,7 @@ public class DomainApplicationCreateFlowTest
.build()); .build());
RegistrantNotAllowedException thrown = RegistrantNotAllowedException thrown =
expectThrows(RegistrantNotAllowedException.class, this::runFlow); expectThrows(RegistrantNotAllowedException.class, this::runFlow);
assertThat(thrown).hasMessageThat().contains("jd1234"); assertAboutEppExceptions().that(thrown).marshalsToXml().and().hasMessageThatContains("jd1234");
} }
@Test @Test
@ -1644,7 +1636,11 @@ public class DomainApplicationCreateFlowTest
.build()); .build());
NameserversNotAllowedForTldException thrown = NameserversNotAllowedForTldException thrown =
expectThrows(NameserversNotAllowedForTldException.class, this::runFlow); expectThrows(NameserversNotAllowedForTldException.class, this::runFlow);
assertThat(thrown).hasMessageThat().contains("ns1.example.net"); assertAboutEppExceptions()
.that(thrown)
.marshalsToXml()
.and()
.hasMessageThatContains("ns1.example.net");
} }
@Test @Test

View file

@ -16,7 +16,6 @@ package google.registry.flows.domain;
import static com.google.common.io.BaseEncoding.base16; import static com.google.common.io.BaseEncoding.base16;
import static com.google.common.truth.Truth.assertThat; 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 com.google.common.truth.Truth8.assertThat;
import static google.registry.model.domain.fee.Fee.FEE_EXTENSION_URIS; import static google.registry.model.domain.fee.Fee.FEE_EXTENSION_URIS;
import static google.registry.model.eppcommon.StatusValue.OK; import static google.registry.model.eppcommon.StatusValue.OK;
@ -998,21 +997,14 @@ public class DomainCreateFlowTest extends ResourceFlowTestCase<DomainCreateFlow,
public void testFailure_alreadyExists() throws Exception { public void testFailure_alreadyExists() throws Exception {
persistContactsAndHosts(); persistContactsAndHosts();
persistActiveDomain(getUniqueIdFromCommand()); persistActiveDomain(getUniqueIdFromCommand());
try { ResourceAlreadyExistsException thrown =
runFlow(); expectThrows(ResourceAlreadyExistsException.class, this::runFlow);
assert_() assertAboutEppExceptions()
.fail( .that(thrown)
"Expected to throw ResourceAlreadyExistsException with message " .marshalsToXml()
+ "Object with given ID (%s) already exists", .and()
getUniqueIdFromCommand()); .hasMessage(
} catch (ResourceAlreadyExistsException e) { String.format("Object with given ID (%s) already exists", getUniqueIdFromCommand()));
assertAboutEppExceptions()
.that(e)
.marshalsToXml()
.and()
.hasMessage(
String.format("Object with given ID (%s) already exists", getUniqueIdFromCommand()));
}
} }
@Test @Test

View file

@ -64,8 +64,8 @@ public final class CommitLogManifestInputTest {
reader.beginSlice(); reader.beginSlice();
seen.add(reader.next()); seen.add(reader.next());
try { try {
reader.next(); Key<CommitLogManifest> key = reader.next();
assert_().fail("Unexpected element"); assert_().fail("Unexpected element: " + key);
} catch (NoSuchElementException expected) { } catch (NoSuchElementException expected) {
} }
} }
@ -88,10 +88,12 @@ public final class CommitLogManifestInputTest {
reader.beginShard(); reader.beginShard();
reader.beginSlice(); reader.beginSlice();
try { try {
Key<CommitLogManifest> key = null;
for (int i = 0; i < 10; i++) { 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) { } catch (NoSuchElementException expected) {
} }
} }
@ -114,10 +116,12 @@ public final class CommitLogManifestInputTest {
reader.beginShard(); reader.beginShard();
reader.beginSlice(); reader.beginSlice();
try { try {
Key<CommitLogManifest> key = null;
for (int i = 0; i < 10; i++) { 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) { } catch (NoSuchElementException expected) {
} }
} }

View file

@ -140,8 +140,8 @@ public class EppResourceInputsTest {
reader.beginSlice(); reader.beginSlice();
seen.add(reader.next()); seen.add(reader.next());
try { try {
reader.next(); Key<DomainBase> key = reader.next();
assert_().fail("Unexpected element"); assert_().fail("Unexpected element: " + key);
} catch (NoSuchElementException expected) { } catch (NoSuchElementException expected) {
} }
} }
@ -165,8 +165,8 @@ public class EppResourceInputsTest {
reader.beginSlice(); reader.beginSlice();
seen.add(reader.next()); seen.add(reader.next());
try { try {
reader.next(); DomainResource domain = reader.next();
assert_().fail("Unexpected element"); assert_().fail("Unexpected element: " + domain);
} catch (NoSuchElementException expected) { } catch (NoSuchElementException expected) {
} }
} }

View file

@ -15,7 +15,7 @@
package google.registry.reporting; package google.registry.reporting;
import static com.google.common.truth.Truth.assertThat; 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.assertNoTasksEnqueued;
import static google.registry.testing.TaskQueueHelper.assertTasksEnqueued; import static google.registry.testing.TaskQueueHelper.assertTasksEnqueued;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -135,13 +135,9 @@ public class IcannReportingStagingActionTest {
createAction(ImmutableList.of(ReportType.ACTIVITY)); createAction(ImmutableList.of(ReportType.ACTIVITY));
when(stager.stageReports(ReportType.ACTIVITY)) when(stager.stageReports(ReportType.ACTIVITY))
.thenThrow(new BigqueryJobFailureException("Expected failure", null, null, null)); .thenThrow(new BigqueryJobFailureException("Expected failure", null, null, null));
try { BigqueryJobFailureException thrown =
action.run(); expectThrows(BigqueryJobFailureException.class, action::run);
assertWithMessage("Expected to encounter a BigqueryJobFailureException").fail(); assertThat(thrown).hasMessageThat().isEqualTo("Expected failure");
} catch (BigqueryJobFailureException expected) {
// Expect the exception.
assertThat(expected).hasMessageThat().isEqualTo("Expected failure");
}
verify(stager, times(3)).stageReports(ReportType.ACTIVITY); verify(stager, times(3)).stageReports(ReportType.ACTIVITY);
verify(emailUtils) verify(emailUtils)
.emailResults( .emailResults(

View file

@ -15,8 +15,8 @@
package google.registry.reporting; package google.registry.reporting;
import static com.google.common.truth.Truth.assertThat; 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.GcsTestingUtils.writeGcsFile;
import static google.registry.testing.JUnitBackports.expectThrows;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
@ -144,14 +144,10 @@ public class IcannReportingUploadActionTest {
public void testFail_FileNotFound() throws Exception { public void testFail_FileNotFound() throws Exception {
IcannReportingUploadAction action = createAction(); IcannReportingUploadAction action = createAction();
action.subdir = "somewhere/else"; action.subdir = "somewhere/else";
try { IllegalArgumentException thrown = expectThrows(IllegalArgumentException.class, action::run);
action.run(); assertThat(thrown)
assertWithMessage("Expected IllegalStateException to be thrown").fail(); .hasMessageThat()
} catch (IllegalArgumentException expected) { .isEqualTo("Object MANIFEST.txt in bucket basin/somewhere/else not found");
assertThat(expected)
.hasMessageThat()
.isEqualTo("Object MANIFEST.txt in bucket basin/somewhere/else not found");
}
} }
} }

View file

@ -41,6 +41,11 @@ public class EppExceptionSubject extends Subject<EppExceptionSubject, EppExcepti
return new And<>(this); return new And<>(this);
} }
public And<EppExceptionSubject> hasMessageThatContains(String expected) {
assertThat(actual()).hasMessageThat().contains(expected);
return new And<>(this);
}
public And<EppExceptionSubject> marshalsToXml() { public And<EppExceptionSubject> marshalsToXml() {
// Attempt to marshal the exception to EPP. If it doesn't work, this will throw. // Attempt to marshal the exception to EPP. If it doesn't work, this will throw.
try { try {

View file

@ -15,7 +15,6 @@
package google.registry.tools; package google.registry.tools;
import static com.google.common.truth.Truth.assertThat; 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 com.google.common.truth.Truth8.assertThat;
import static google.registry.model.registry.label.ReservationType.FULLY_BLOCKED; import static google.registry.model.registry.label.ReservationType.FULLY_BLOCKED;
import static google.registry.testing.DatastoreHelper.createTlds; import static google.registry.testing.DatastoreHelper.createTlds;
@ -175,13 +174,12 @@ public class CreateReservedListCommandTest extends
} }
private void runNameTestExpectedFailure(String name, String expectedErrorMsg) throws Exception { private void runNameTestExpectedFailure(String name, String expectedErrorMsg) throws Exception {
try { IllegalArgumentException thrown =
runCommandForced("--name=" + name, "--input=" + reservedTermsPath); expectThrows(
assertWithMessage("Expected IllegalArgumentException to be thrown").fail(); IllegalArgumentException.class,
} catch (IllegalArgumentException e) { () -> runCommandForced("--name=" + name, "--input=" + reservedTermsPath));
assertThat(ReservedList.get(name)).isEmpty(); assertThat(ReservedList.get(name)).isEmpty();
assertThat(e).hasMessageThat().isEqualTo(expectedErrorMsg); assertThat(thrown).hasMessageThat().isEqualTo(expectedErrorMsg);
}
} }
private void runNameTestWithOverride(String name) throws Exception { private void runNameTestWithOverride(String name) throws Exception {

View file

@ -15,7 +15,6 @@
package google.registry.tools; package google.registry.tools;
import static com.google.common.truth.Truth.assertThat; 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 com.google.common.truth.Truth8.assertThat;
import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.testing.DatastoreHelper.createTld; import static google.registry.testing.DatastoreHelper.createTld;
@ -61,14 +60,13 @@ public class DeletePremiumListCommandTest extends CommandTestCase<DeletePremiumL
PremiumList premiumList = persistPremiumList("xn--q9jyb4c", "blah,USD 100"); PremiumList premiumList = persistPremiumList("xn--q9jyb4c", "blah,USD 100");
createTld("xn--q9jyb4c"); createTld("xn--q9jyb4c");
persistResource(Registry.get("xn--q9jyb4c").asBuilder().setPremiumList(premiumList).build()); persistResource(Registry.get("xn--q9jyb4c").asBuilder().setPremiumList(premiumList).build());
try { IllegalArgumentException thrown =
runCommandForced("--name=" + premiumList.getName()); expectThrows(
assertWithMessage("Expected IllegalArgumentException to be thrown").fail(); IllegalArgumentException.class,
} catch (IllegalArgumentException e) { () -> runCommandForced("--name=" + premiumList.getName()));
assertThat(PremiumList.get(premiumList.getName())).isPresent(); assertThat(PremiumList.get(premiumList.getName())).isPresent();
assertThat(e) assertThat(thrown)
.hasMessageThat() .hasMessageThat()
.isEqualTo("Cannot delete premium list because it is used on these tld(s): xn--q9jyb4c"); .isEqualTo("Cannot delete premium list because it is used on these tld(s): xn--q9jyb4c");
}
} }
} }

View file

@ -15,7 +15,6 @@
package google.registry.tools; package google.registry.tools;
import static com.google.common.truth.Truth.assertThat; 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 com.google.common.truth.Truth8.assertThat;
import static google.registry.testing.DatastoreHelper.createTld; import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.persistReservedList; import static google.registry.testing.DatastoreHelper.persistReservedList;
@ -59,14 +58,13 @@ public class DeleteReservedListCommandTest extends CommandTestCase<DeleteReserve
public void testFailure_whenReservedListIsInUse() throws Exception { public void testFailure_whenReservedListIsInUse() throws Exception {
createTld("xn--q9jyb4c"); createTld("xn--q9jyb4c");
persistResource(Registry.get("xn--q9jyb4c").asBuilder().setReservedLists(reservedList).build()); persistResource(Registry.get("xn--q9jyb4c").asBuilder().setReservedLists(reservedList).build());
try { IllegalArgumentException thrown =
runCommandForced("--name=" + reservedList.getName()); expectThrows(
assertWithMessage("Expected IllegalArgumentException to be thrown").fail(); IllegalArgumentException.class,
} catch (IllegalArgumentException e) { () -> runCommandForced("--name=" + reservedList.getName()));
assertThat(ReservedList.get(reservedList.getName())).isPresent(); assertThat(ReservedList.get(reservedList.getName())).isPresent();
assertThat(e) assertThat(thrown)
.hasMessageThat() .hasMessageThat()
.isEqualTo("Cannot delete reserved list because it is used on these tld(s): xn--q9jyb4c"); .isEqualTo("Cannot delete reserved list because it is used on these tld(s): xn--q9jyb4c");
}
} }
} }

View file

@ -15,7 +15,6 @@
package google.registry.tools; package google.registry.tools;
import static com.google.common.truth.Truth.assertThat; 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.model.ofy.ObjectifyService.ofy;
import static google.registry.testing.DatastoreHelper.createTld; import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.deleteResource; import static google.registry.testing.DatastoreHelper.deleteResource;
@ -273,18 +272,14 @@ public class MutatingCommandTest {
+ "\n" + "\n"
+ "Update Registrar@Registrar2\n" + "Update Registrar@Registrar2\n"
+ "blockPremiumNames: false -> true\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. IllegalStateException thrown = expectThrows(IllegalStateException.class, command::execute);
assertThat(ofy().load().entity(registrar1).now()).isEqualTo(registrar1); assertThat(thrown).hasMessageThat().contains("Entity changed since init() was called.");
assertThat(ofy().load().entity(registrar2).now()).isEqualTo(registrar2); 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 @Test

View file

@ -23,6 +23,7 @@ import static google.registry.testing.DatastoreHelper.loadRegistrar;
import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.DatastoreHelper.persistSimpleResource; import static google.registry.testing.DatastoreHelper.persistSimpleResource;
import static google.registry.testing.DatastoreHelper.persistSimpleResources; import static google.registry.testing.DatastoreHelper.persistSimpleResources;
import static google.registry.testing.JUnitBackports.expectThrows;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
@ -191,17 +192,18 @@ public class RegistrarContactCommandTest extends CommandTestCase<RegistrarContac
.setGaeUserId("11111") .setGaeUserId("11111")
.setVisibleInDomainWhoisAsAbuse(true) .setVisibleInDomainWhoisAsAbuse(true)
.build()); .build());
try { IllegalArgumentException thrown =
runCommandForced( expectThrows(
"--mode=UPDATE", IllegalArgumentException.class,
"--email=john.doe@example.com", () ->
"--visible_in_domain_whois_as_abuse=false", runCommandForced(
"NewRegistrar"); "--mode=UPDATE",
throw new Exception( "--email=john.doe@example.com",
"Expected IllegalArgumentException: Cannot clear visible_in_domain_whois_as_abuse flag"); "--visible_in_domain_whois_as_abuse=false",
} catch (IllegalArgumentException e) { "NewRegistrar"));
assertThat(e).hasMessageThat().contains("Cannot clear visible_in_domain_whois_as_abuse flag"); assertThat(thrown)
} .hasMessageThat()
.contains("Cannot clear visible_in_domain_whois_as_abuse flag");
RegistrarContact registrarContact = loadRegistrar("NewRegistrar").getContacts().asList().get(1); RegistrarContact registrarContact = loadRegistrar("NewRegistrar").getContacts().asList().get(1);
assertThat(registrarContact.getVisibleInDomainWhoisAsAbuse()).isTrue(); assertThat(registrarContact.getVisibleInDomainWhoisAsAbuse()).isTrue();
} }
@ -323,16 +325,13 @@ public class RegistrarContactCommandTest extends CommandTestCase<RegistrarContac
RegistrarContact registrarContact = loadRegistrar("NewRegistrar").getContacts().asList().get(0); RegistrarContact registrarContact = loadRegistrar("NewRegistrar").getContacts().asList().get(0);
persistSimpleResource( persistSimpleResource(
registrarContact.asBuilder().setVisibleInDomainWhoisAsAbuse(true).build()); registrarContact.asBuilder().setVisibleInDomainWhoisAsAbuse(true).build());
try { IllegalArgumentException thrown =
runCommandForced( expectThrows(
"--mode=DELETE", IllegalArgumentException.class,
"--email=janedoe@theregistrar.com", () ->
"NewRegistrar"); runCommandForced(
throw new Exception( "--mode=DELETE", "--email=janedoe@theregistrar.com", "NewRegistrar"));
"Expected IllegalArgumentException: Cannot delete the domain WHOIS abuse contact"); assertThat(thrown).hasMessageThat().contains("Cannot delete the domain WHOIS abuse contact");
} catch (IllegalArgumentException e) {
assertThat(e).hasMessageThat().contains("Cannot delete the domain WHOIS abuse contact");
}
assertThat(loadRegistrar("NewRegistrar").getContacts()).isNotEmpty(); assertThat(loadRegistrar("NewRegistrar").getContacts()).isNotEmpty();
} }

View file

@ -16,7 +16,6 @@ package google.registry.whois;
import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8; 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.assertThat;
import static com.google.common.truth.Truth.assert_;
import static google.registry.testing.DatastoreHelper.createTlds; import static google.registry.testing.DatastoreHelper.createTlds;
import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.DatastoreHelper.persistSimpleResources; 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.makeHostResource;
import static google.registry.testing.FullFieldsTestEntityHelper.makeRegistrar; import static google.registry.testing.FullFieldsTestEntityHelper.makeRegistrar;
import static google.registry.testing.FullFieldsTestEntityHelper.makeRegistrarContacts; import static google.registry.testing.FullFieldsTestEntityHelper.makeRegistrarContacts;
import static google.registry.testing.JUnitBackports.expectThrows;
import static google.registry.whois.WhoisTestData.loadFile; import static google.registry.whois.WhoisTestData.loadFile;
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST; import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR; import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
@ -379,12 +379,8 @@ public class WhoisHttpServerTest {
.thenThrow(new IOException("missing cat interface")); .thenThrow(new IOException("missing cat interface"));
server.whoisMetrics = mock(WhoisMetrics.class); server.whoisMetrics = mock(WhoisMetrics.class);
try { RuntimeException thrown = expectThrows(RuntimeException.class, server::run);
server.run(); assertThat(thrown).hasCauseThat().hasMessageThat().isEqualTo("missing cat interface");
assert_().fail("Should have thrown RuntimeException");
} catch (RuntimeException e) {
assertThat(e.getCause().getMessage()).isEqualTo("missing cat interface");
}
WhoisMetric expected = WhoisMetric expected =
WhoisMetric.builderForRequest(clock) WhoisMetric.builderForRequest(clock)
.setNumResults(0) .setNumResults(0)