Add transactional import helper in RdeImportUtilsTest

This is needed to fix an inability in Java 8 to correctly infer the type
when the transaction was being allowed to return the value it loaded. The
error was:

INFO: Compilation unit  has error diagnostics: [third_party/java_src/gtld/javatests/google/registry/rde/imports/RdeImportUtilsTest.java:109: error: incompatible types: inference variable R has incompatible bounds
    ofy().transact(() -> rdeImportUtils.importEppResource(newContact));
                  ^
    upper bounds: java.lang.Object
    lower bounds: void, third_party/java_src/gtld/javatests/google/registry/rde/imports/RdeImportUtilsTest.java:132:
error: incompatible types: inference variable R has incompatible bounds

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=179082154
This commit is contained in:
mcilwain 2017-12-14 12:51:55 -08:00 committed by Ben McIlwain
parent 9157930983
commit c8059d4d8a
2 changed files with 22 additions and 11 deletions

View file

@ -110,8 +110,8 @@ public class RdeImportUtils {
* <p>If the resource is imported, {@link ForeignKeyIndex} and {@link EppResourceIndex} are also * <p>If the resource is imported, {@link ForeignKeyIndex} and {@link EppResourceIndex} are also
* created. * created.
*/ */
public <T extends EppResource & ForeignKeyedEppResource> void public <T extends EppResource & ForeignKeyedEppResource> void importEppResource(
importEppResource(final T resource) { final T resource) {
Object existing = ofy.load().key(Key.create(resource)).now(); Object existing = ofy.load().key(Key.create(resource)).now();
if (existing != null) { if (existing != null) {
// This will roll back the transaction and prevent duplicate history entries from being saved. // This will roll back the transaction and prevent duplicate history entries from being saved.

View file

@ -37,6 +37,7 @@ import com.google.common.io.ByteSource;
import com.googlecode.objectify.Key; import com.googlecode.objectify.Key;
import google.registry.gcs.GcsUtils; import google.registry.gcs.GcsUtils;
import google.registry.model.EppResource; import google.registry.model.EppResource;
import google.registry.model.EppResource.ForeignKeyedEppResource;
import google.registry.model.contact.ContactResource; import google.registry.model.contact.ContactResource;
import google.registry.model.domain.DesignatedContact; import google.registry.model.domain.DesignatedContact;
import google.registry.model.domain.DesignatedContact.Type; import google.registry.model.domain.DesignatedContact.Type;
@ -106,7 +107,7 @@ public class RdeImportUtilsTest extends ShardableTestCase {
@Test @Test
public void testImportNewContact() { public void testImportNewContact() {
final ContactResource newContact = buildNewContact(); final ContactResource newContact = buildNewContact();
ofy().transact(() -> rdeImportUtils.importEppResource(newContact)); importResourceInTransaction(newContact);
assertEppResourceIndexEntityFor(newContact); assertEppResourceIndexEntityFor(newContact);
assertForeignKeyIndexFor(newContact); assertForeignKeyIndexFor(newContact);
@ -129,7 +130,7 @@ public class RdeImportUtilsTest extends ShardableTestCase {
.setLastEppUpdateTime(newContact.getLastEppUpdateTime().plusSeconds(1)) .setLastEppUpdateTime(newContact.getLastEppUpdateTime().plusSeconds(1))
.build(); .build();
try { try {
ofy().transact(() -> rdeImportUtils.importEppResource(updatedContact)); importResourceInTransaction(updatedContact);
fail("Expected ResourceExistsException"); fail("Expected ResourceExistsException");
} catch (ResourceExistsException expected) { } catch (ResourceExistsException expected) {
// verify the updated contact was not saved // verify the updated contact was not saved
@ -145,7 +146,7 @@ public class RdeImportUtilsTest extends ShardableTestCase {
@Test @Test
public void testImportNewHost() throws UnknownHostException { public void testImportNewHost() throws UnknownHostException {
final HostResource newHost = buildNewHost(); final HostResource newHost = buildNewHost();
ofy().transact(() -> rdeImportUtils.importEppResource(newHost)); importResourceInTransaction(newHost);
assertEppResourceIndexEntityFor(newHost); assertEppResourceIndexEntityFor(newHost);
assertForeignKeyIndexFor(newHost); assertForeignKeyIndexFor(newHost);
@ -169,7 +170,7 @@ public class RdeImportUtilsTest extends ShardableTestCase {
.setLastEppUpdateTime(newHost.getLastEppUpdateTime().plusSeconds(1)) .setLastEppUpdateTime(newHost.getLastEppUpdateTime().plusSeconds(1))
.build(); .build();
try { try {
ofy().transact(() -> rdeImportUtils.importEppResource(updatedHost)); importResourceInTransaction(updatedHost);
fail("Expected ResourceExistsException"); fail("Expected ResourceExistsException");
} catch (ResourceExistsException expected) { } catch (ResourceExistsException expected) {
// verify the contact was not updated // verify the contact was not updated
@ -184,7 +185,7 @@ public class RdeImportUtilsTest extends ShardableTestCase {
@Test @Test
public void testImportNewDomain() throws Exception { public void testImportNewDomain() throws Exception {
final DomainResource newDomain = buildNewDomain(); final DomainResource newDomain = buildNewDomain();
ofy().transact(() -> rdeImportUtils.importEppResource(newDomain)); importResourceInTransaction(newDomain);
DomainResource saved = getDomain("Dexample1-TEST"); DomainResource saved = getDomain("Dexample1-TEST");
assertThat(saved.getFullyQualifiedDomainName()) assertThat(saved.getFullyQualifiedDomainName())
@ -209,7 +210,7 @@ public class RdeImportUtilsTest extends ShardableTestCase {
.setFullyQualifiedDomainName("1" + newDomain.getFullyQualifiedDomainName()) .setFullyQualifiedDomainName("1" + newDomain.getFullyQualifiedDomainName())
.build(); .build();
try { try {
ofy().transact(() -> rdeImportUtils.importEppResource(updatedDomain)); importResourceInTransaction(updatedDomain);
fail("Expected ResourceExistsException"); fail("Expected ResourceExistsException");
} catch (ResourceExistsException expected) { } catch (ResourceExistsException expected) {
DomainResource saved = getDomain("Dexample1-TEST"); DomainResource saved = getDomain("Dexample1-TEST");
@ -354,4 +355,14 @@ public class RdeImportUtilsTest extends ShardableTestCase {
assertThat(indices.get(0).getBucket()) assertThat(indices.get(0).getBucket())
.isEqualTo(EppResourceIndexBucket.getBucketKey(Key.create(resource))); .isEqualTo(EppResourceIndexBucket.getBucketKey(Key.create(resource)));
} }
private <T extends EppResource & ForeignKeyedEppResource> void importResourceInTransaction(
T resource) {
ofy()
.transact(
() -> {
rdeImportUtils.importEppResource(resource);
return null;
});
}
} }