diff --git a/java/google/registry/dns/RefreshDnsAction.java b/java/google/registry/dns/RefreshDnsAction.java index 6db9e5e04..8413d3a94 100644 --- a/java/google/registry/dns/RefreshDnsAction.java +++ b/java/google/registry/dns/RefreshDnsAction.java @@ -18,6 +18,8 @@ import static google.registry.model.EppResourceUtils.loadByForeignKey; import google.registry.dns.DnsConstants.TargetType; import google.registry.model.EppResource; +import google.registry.model.EppResource.ForeignKeyedEppResource; +import google.registry.model.annotations.ExternalMessagingName; import google.registry.model.domain.DomainResource; import google.registry.model.host.HostResource; import google.registry.request.Action; @@ -42,38 +44,35 @@ public final class RefreshDnsAction implements Runnable { if (!domainOrHostName.contains(".")) { throw new BadRequestException("URL parameter 'name' must be fully qualified"); } - - boolean domainLookup; - Class clazz; switch (type) { case DOMAIN: - domainLookup = true; - clazz = DomainResource.class; + loadAndVerifyExistence(DomainResource.class, domainOrHostName); + dnsQueue.addDomainRefreshTask(domainOrHostName); break; case HOST: - domainLookup = false; - clazz = HostResource.class; + verifyHostIsSubordinate(loadAndVerifyExistence(HostResource.class, domainOrHostName)); + dnsQueue.addHostRefreshTask(domainOrHostName); break; default: throw new BadRequestException("Unsupported type: " + type); } + } - EppResource eppResource = loadByForeignKey(clazz, domainOrHostName, clock.nowUtc()); - if (eppResource == null) { + private + T loadAndVerifyExistence(Class clazz, String foreignKey) { + T resource = loadByForeignKey(clazz, foreignKey, clock.nowUtc()); + if (resource == null) { + String typeName = clazz.getAnnotation(ExternalMessagingName.class).value(); throw new NotFoundException( - String.format("%s %s not found", type, domainOrHostName)); + String.format("%s %s not found", typeName, domainOrHostName)); } + return resource; + } - if (domainLookup) { - dnsQueue.addDomainRefreshTask(domainOrHostName); - } else { - if (((HostResource) eppResource).getSuperordinateDomain() == null) { - throw new BadRequestException( - String.format("%s isn't a subordinate hostname", domainOrHostName)); - } else { - // Don't enqueue host refresh tasks for external hosts. - dnsQueue.addHostRefreshTask(domainOrHostName); - } + private static void verifyHostIsSubordinate(HostResource host) { + if (host.getSuperordinateDomain() == null) { + throw new BadRequestException( + String.format("%s isn't a subordinate hostname", host.getFullyQualifiedHostName())); } } } diff --git a/javatests/google/registry/dns/DnsInjectionTest.java b/javatests/google/registry/dns/DnsInjectionTest.java index 3415eef11..817df078c 100644 --- a/javatests/google/registry/dns/DnsInjectionTest.java +++ b/javatests/google/registry/dns/DnsInjectionTest.java @@ -85,7 +85,7 @@ public final class DnsInjectionTest { } @Test - public void testWhoisHttpServer_injectsAndWorks() throws Exception { + public void testRefreshDns_domain_injectsAndWorks() throws Exception { persistActiveDomain("example.lol"); when(req.getParameter("type")).thenReturn("domain"); when(req.getParameter("name")).thenReturn("example.lol"); @@ -94,10 +94,27 @@ public final class DnsInjectionTest { } @Test - public void testWhoisHttpServer_missingDomain_throwsNotFound() throws Exception { + public void testRefreshDns_missingDomain_throwsNotFound() throws Exception { when(req.getParameter("type")).thenReturn("domain"); when(req.getParameter("name")).thenReturn("example.lol"); - thrown.expect(NotFoundException.class, "DOMAIN example.lol not found"); + thrown.expect(NotFoundException.class, "domain example.lol not found"); + component.refreshDns().run(); + } + + @Test + public void testRefreshDns_host_injectsAndWorks() throws Exception { + persistActiveSubordinateHost("ns1.example.lol", persistActiveDomain("example.lol")); + when(req.getParameter("type")).thenReturn("host"); + when(req.getParameter("name")).thenReturn("ns1.example.lol"); + component.refreshDns().run(); + assertDnsTasksEnqueued("ns1.example.lol"); + } + + @Test + public void testRefreshDns_missingHost_throwsNotFound() throws Exception { + when(req.getParameter("type")).thenReturn("host"); + when(req.getParameter("name")).thenReturn("ns1.example.lol"); + thrown.expect(NotFoundException.class, "host ns1.example.lol not found"); component.refreshDns().run(); } }