Skip to content

Commit a4bcb0a

Browse files
committed
Make RDE generation resilient to missing contact rows
This will prevent RDE from failing once we delete all contacts, just as a fail-safe. BUG= http://b/439636188
1 parent 759aadd commit a4bcb0a

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

core/src/main/java/google/registry/rde/DomainToXjcConverter.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -168,20 +168,18 @@ static XjcRdeDomain convertDomain(Domain model, RdeMode mode) {
168168
// as the holder of the domain name object.
169169
Optional<VKey<Contact>> registrant = model.getRegistrant();
170170
if (registrant.isPresent()) {
171-
Contact registrantContact = tm().transact(() -> tm().loadByKey(registrant.get()));
172-
checkState(
173-
registrantContact != null,
174-
"Registrant contact %s on domain %s does not exist",
175-
registrant,
176-
domainName);
177-
bean.setRegistrant(registrantContact.getContactId());
171+
Optional<Contact> registrantContact =
172+
tm().transact(() -> tm().loadByKeyIfPresent(registrant.get()));
173+
registrantContact.ifPresent(c -> bean.setRegistrant(c.getContactId()));
178174
}
179175

180176
// o Zero or more OPTIONAL <contact> elements that contain identifiers
181177
// for the human or organizational social information objects
182178
// associated with the domain name object.
183179
for (DesignatedContact contact : model.getContacts()) {
184-
bean.getContacts().add(convertDesignatedContact(contact, domainName));
180+
Optional<XjcDomainContactType> contactType =
181+
convertDesignatedContact(contact, domainName);
182+
contactType.ifPresent(c -> bean.getContacts().add(c));
185183
}
186184

187185
// o An OPTIONAL <secDNS> element that contains the public key
@@ -292,23 +290,21 @@ private static XjcSecdnsDsDataType convertDelegationSignerData(DomainDsData mode
292290
}
293291

294292
/** Converts {@link DesignatedContact} to {@link XjcDomainContactType}. */
295-
private static XjcDomainContactType convertDesignatedContact(
293+
private static Optional<XjcDomainContactType> convertDesignatedContact(
296294
DesignatedContact model, String domainName) {
297295
XjcDomainContactType bean = new XjcDomainContactType();
298296
checkState(
299297
model.getContactKey() != null,
300298
"Contact key for type %s is null on domain %s",
301299
model.getType(),
302300
domainName);
303-
Contact contact = tm().transact(() -> tm().loadByKey(model.getContactKey()));
304-
checkState(
305-
contact != null,
306-
"Contact %s on domain %s does not exist",
307-
model.getContactKey(),
308-
domainName);
301+
Optional<Contact> contact = tm().transact(() -> tm().loadByKeyIfPresent(model.getContactKey()));
302+
if (contact.isEmpty()) {
303+
return Optional.empty();
304+
}
309305
bean.setType(XjcDomainContactAttrType.fromValue(Ascii.toLowerCase(model.getType().toString())));
310-
bean.setValue(contact.getContactId());
311-
return bean;
306+
bean.setValue(contact.get().getContactId());
307+
return Optional.of(bean);
312308
}
313309

314310
private DomainToXjcConverter() {}

core/src/test/java/google/registry/rde/DomainToXjcConverterTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414

1515
package google.registry.rde;
1616

17+
import static com.google.common.collect.ImmutableSet.toImmutableSet;
1718
import static com.google.common.io.BaseEncoding.base16;
1819
import static com.google.common.truth.Truth.assertThat;
1920
import static com.google.common.truth.Truth.assertWithMessage;
21+
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
2022
import static google.registry.testing.DatabaseHelper.createTld;
2123
import static google.registry.testing.DatabaseHelper.persistEppResource;
2224
import static google.registry.testing.DatabaseHelper.persistResource;
@@ -69,6 +71,7 @@
6971
import google.registry.xjc.rdedomain.XjcRdeDomainElement;
7072
import google.registry.xjc.rgp.XjcRgpStatusType;
7173
import google.registry.xjc.secdns.XjcSecdnsDsDataType;
74+
import google.registry.xml.XmlException;
7275
import java.io.ByteArrayOutputStream;
7376
import java.util.Optional;
7477
import org.joda.money.Money;
@@ -198,6 +201,21 @@ void testMarshalThin() throws Exception {
198201
wrapDeposit(bean).marshal(new ByteArrayOutputStream(), UTF_8);
199202
}
200203

204+
@Test
205+
void testConvertAbsentContacts() throws XmlException {
206+
Domain domain = makeDomain(clock);
207+
tm().transact(
208+
() ->
209+
tm().delete(
210+
domain.getAllContacts().stream()
211+
.map(DesignatedContact::getContactKey)
212+
.collect(toImmutableSet())));
213+
XjcRdeDomain bean = DomainToXjcConverter.convertDomain(domain, RdeMode.FULL);
214+
assertThat(bean.getRegistrant()).isNull();
215+
assertThat(bean.getContacts()).isEmpty();
216+
wrapDeposit(bean).marshal(new ByteArrayOutputStream(), UTF_8);
217+
}
218+
201219
XjcRdeDeposit wrapDeposit(XjcRdeDomain domain) {
202220
XjcRdeDeposit deposit = new XjcRdeDeposit();
203221
deposit.setId("984302");

0 commit comments

Comments
 (0)