google-nomulus/javatests/google/registry/tools/GenerateDnsReportCommandTest.java
cgoldfeder 9a2afc7a9b Remove nearly all uses of ReferenceUnion
ReferenceUnion is a hack to work around the mismatch between how
we store references (by roid) and how they are represented in EPP
(by foreign key). If it ever needed to exist (not entirely clear...)
it should have remained tightly scoped within the domain commands
and resources. Instead it has leaked everywhere in the project,
causing lots of boilerplate. This CL hides all of that behind
standard Refs, and should be followed by work to remove ReferenceUnion
completely.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=122424416
2016-05-16 16:36:25 -04:00

219 lines
8.4 KiB
Java

// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.tools;
import static com.google.common.io.BaseEncoding.base16;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.DatastoreHelper.createTlds;
import static google.registry.testing.DatastoreHelper.newDomainResource;
import static google.registry.testing.DatastoreHelper.newHostResource;
import static google.registry.testing.DatastoreHelper.persistActiveDomain;
import static google.registry.testing.DatastoreHelper.persistActiveHost;
import static google.registry.testing.DatastoreHelper.persistResource;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.joda.time.DateTimeZone.UTC;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.net.InetAddresses;
import com.beust.jcommander.ParameterException;
import com.googlecode.objectify.Ref;
import google.registry.model.domain.DomainResource;
import google.registry.model.domain.secdns.DelegationSignerData;
import google.registry.model.eppcommon.StatusValue;
import google.registry.model.host.HostResource;
import google.registry.testing.FakeClock;
import org.joda.time.DateTime;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/** Unit tests for {@link GenerateDnsReportCommand}. */
public class GenerateDnsReportCommandTest extends CommandTestCase<GenerateDnsReportCommand> {
@Rule
public final TemporaryFolder folder = new TemporaryFolder();
private final DateTime now = DateTime.now(UTC);
private final FakeClock clock = new FakeClock();
private Path output;
private Object getOutputAsJson() throws IOException, ParseException {
try (Reader reader = Files.newBufferedReader(output, UTF_8)) {
return JSONValue.parseWithException(reader);
}
}
private HostResource nameserver1;
private HostResource nameserver2;
private HostResource nameserver3;
private HostResource nameserver4;
private DomainResource domain1;
private static final ImmutableMap<String, ?> DOMAIN1_OUTPUT = ImmutableMap.of(
"domain", "example.xn--q9jyb4c",
"nameservers", ImmutableList.of(
"ns1.example.xn--q9jyb4c",
"ns2.example.xn--q9jyb4c"),
"dsData", ImmutableList.of(
ImmutableMap.of(
"keyTag", 12345L,
"algorithm", 3L,
"digestType", 1L,
"digest", "49FD46E6C4B45C55D4AC"),
ImmutableMap.of(
"keyTag", 56789L,
"algorithm", 2L,
"digestType", 4L,
"digest", "69FD46E6C4A45C55D4AC")));
private static final ImmutableMap<String, ?> DOMAIN2_OUTPUT = ImmutableMap.of(
"domain", "foobar.xn--q9jyb4c",
"nameservers", ImmutableList.of(
"ns1.google.com",
"ns2.google.com"));
private static final ImmutableMap<String, ?> NAMESERVER1_OUTPUT = ImmutableMap.of(
"host", "ns1.example.xn--q9jyb4c",
"ips", ImmutableList.of(
"192.168.1.2",
"2607:f8b0:400d:c00:0:0:0:c0"));
private static final ImmutableMap<String, ?> NAMESERVER2_OUTPUT = ImmutableMap.of(
"host", "ns2.example.xn--q9jyb4c",
"ips", ImmutableList.of(
"192.168.1.1",
"2607:f8b0:400d:c00:0:0:0:c1"));
@Before
public void init() throws Exception {
output = Paths.get(folder.newFile().toString());
command.clock = clock;
clock.setTo(now);
createTlds("xn--q9jyb4c", "example");
nameserver1 = persistResource(
newHostResource("ns1.example.xn--q9jyb4c")
.asBuilder()
.setInetAddresses(ImmutableSet.of(
InetAddresses.forString("2607:f8b0:400d:c00::c0"),
InetAddresses.forString("192.168.1.2")))
.build());
nameserver2 = persistResource(
newHostResource("ns2.example.xn--q9jyb4c")
.asBuilder()
.setInetAddresses(ImmutableSet.of(
InetAddresses.forString("192.168.1.1"),
InetAddresses.forString("2607:f8b0:400d:c00::c1")))
.build());
nameserver3 = persistActiveHost("ns1.google.com");
nameserver4 = persistActiveHost("ns2.google.com");
domain1 = persistResource(newDomainResource("example.xn--q9jyb4c").asBuilder()
.setNameservers(ImmutableSet.of(Ref.create(nameserver1), Ref.create(nameserver2)))
.setDsData(ImmutableSet.of(
DelegationSignerData.create(12345, 3, 1, base16().decode("49FD46E6C4B45C55D4AC")),
DelegationSignerData.create(56789, 2, 4, base16().decode("69FD46E6C4A45C55D4AC"))))
.build());
persistResource(newDomainResource("foobar.xn--q9jyb4c").asBuilder()
.setNameservers(ImmutableSet.of(Ref.create(nameserver3), Ref.create(nameserver4)))
.build());
// Persist a domain in a different tld that should be ignored.
persistActiveDomain("should-be-ignored.example");
}
@Test
public void testSuccess() throws Exception {
runCommand("--output=" + output, "--tld=xn--q9jyb4c");
assertThat(getOutputAsJson()).isEqualTo(
ImmutableList.of(DOMAIN1_OUTPUT, DOMAIN2_OUTPUT, NAMESERVER1_OUTPUT, NAMESERVER2_OUTPUT));
}
@Test
public void testSuccess_skipDeletedDomain() throws Exception {
persistResource(domain1.asBuilder().setDeletionTime(now).build());
runCommand("--output=" + output, "--tld=xn--q9jyb4c");
assertThat(getOutputAsJson())
.isEqualTo(ImmutableList.of(DOMAIN2_OUTPUT, NAMESERVER1_OUTPUT, NAMESERVER2_OUTPUT));
}
@Test
public void testSuccess_skipDeletedNameserver() throws Exception {
persistResource(
nameserver1.asBuilder().setDeletionTime(now).build());
runCommand("--output=" + output, "--tld=xn--q9jyb4c");
assertThat(getOutputAsJson())
.isEqualTo(ImmutableList.of(DOMAIN1_OUTPUT, DOMAIN2_OUTPUT, NAMESERVER2_OUTPUT));
}
@Test
public void testSuccess_skipClientHoldDomain() throws Exception {
persistResource(domain1.asBuilder().addStatusValue(StatusValue.CLIENT_HOLD).build());
runCommand("--output=" + output, "--tld=xn--q9jyb4c");
assertThat(getOutputAsJson())
.isEqualTo(ImmutableList.of(DOMAIN2_OUTPUT, NAMESERVER1_OUTPUT, NAMESERVER2_OUTPUT));
}
@Test
public void testSuccess_skipServerHoldDomain() throws Exception {
persistResource(domain1.asBuilder().addStatusValue(StatusValue.SERVER_HOLD).build());
runCommand("--output=" + output, "--tld=xn--q9jyb4c");
assertThat(getOutputAsJson())
.isEqualTo(ImmutableList.of(DOMAIN2_OUTPUT, NAMESERVER1_OUTPUT, NAMESERVER2_OUTPUT));
}
@Test
public void testSuccess_skipPendingDeleteDomain() throws Exception {
persistResource(domain1.asBuilder()
.addStatusValue(StatusValue.PENDING_DELETE)
.setDeletionTime(now.plusDays(30))
.build());
runCommand("--output=" + output, "--tld=xn--q9jyb4c");
assertThat(getOutputAsJson())
.isEqualTo(ImmutableList.of(DOMAIN2_OUTPUT, NAMESERVER1_OUTPUT, NAMESERVER2_OUTPUT));
}
@Test
public void testSuccess_skipDomainsWithoutNameservers() throws Exception {
persistResource(domain1.asBuilder().setNameservers(null).build());
runCommand("--output=" + output, "--tld=xn--q9jyb4c");
assertThat(getOutputAsJson())
.isEqualTo(ImmutableList.of(DOMAIN2_OUTPUT, NAMESERVER1_OUTPUT, NAMESERVER2_OUTPUT));
}
@Test
public void testFailure_tldDoesNotExist() throws Exception {
thrown.expect(IllegalArgumentException.class);
runCommand("--tld=foobar");
}
@Test
public void testFailure_missingTldParameter() throws Exception {
thrown.expect(ParameterException.class);
runCommand("");
}
}