From cbf737da9a05d69355199bf979e8e2b950aa56e1 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Wed, 24 Feb 2016 08:45:58 -0800 Subject: [PATCH 1/3] Added integration tests. --- .../main/java/com/google/gcloud/dns/Dns.java | 17 +- .../com/google/gcloud/spi/DefaultDnsRpc.java | 4 +- .../com/google/gcloud/dns/DnsImplTest.java | 2 +- .../java/com/google/gcloud/dns/DnsTest.java | 2 +- .../java/com/google/gcloud/dns/ITDnsTest.java | 1268 +++++++++++++++++ 5 files changed, 1283 insertions(+), 10 deletions(-) create mode 100644 gcloud-java-dns/src/test/java/com/google/gcloud/dns/ITDnsTest.java diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java index 3ad2094ec2e3..b2cb9fbad371 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java @@ -68,7 +68,8 @@ static String selector(ProjectField... fields) { * The fields of a zone. * *

These values can be used to specify the fields to include in a partial response when calling - * {@link Dns#getZone(String, ZoneOption...)}. The name is always returned, even if not specified. + * {@link Dns#getZone(String, ZoneOption...)}. The name is always returned, even if not + * specified. */ enum ZoneField { CREATION_TIME("creationTime"), @@ -103,8 +104,8 @@ static String selector(ZoneField... fields) { * The fields of a DNS record. * *

These values can be used to specify the fields to include in a partial response when calling - * {@link Dns#listDnsRecords(String, DnsRecordListOption...)}. The name is always returned even if - * not selected. + * {@link Dns#listDnsRecords(String, DnsRecordListOption...)}. The name and type are always + * returned even if not selected. */ enum DnsRecordField { DNS_RECORDS("rrdatas"), @@ -125,6 +126,7 @@ String selector() { static String selector(DnsRecordField... fields) { Set fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1); fieldStrings.add(NAME.selector()); + fieldStrings.add(TYPE.selector()); for (DnsRecordField field : fields) { fieldStrings.add(field.selector()); } @@ -198,7 +200,7 @@ class DnsRecordListOption extends AbstractOption implements Serializable { */ public static DnsRecordListOption fields(DnsRecordField... fields) { StringBuilder builder = new StringBuilder(); - builder.append("rrsets(").append(DnsRecordField.selector(fields)).append(')'); + builder.append("nextPageToken,rrsets(").append(DnsRecordField.selector(fields)).append(')'); return new DnsRecordListOption(DnsRpc.Option.FIELDS, builder.toString()); } @@ -234,7 +236,7 @@ public static DnsRecordListOption dnsName(String dnsName) { * Dns.DnsRecordListOption#dnsName(String)} must also be present. */ public static DnsRecordListOption type(DnsRecord.Type type) { - return new DnsRecordListOption(DnsRpc.Option.DNS_TYPE, type); + return new DnsRecordListOption(DnsRpc.Option.DNS_TYPE, type.name()); } } @@ -281,7 +283,7 @@ class ZoneListOption extends AbstractOption implements Serializable { */ public static ZoneListOption fields(ZoneField... fields) { StringBuilder builder = new StringBuilder(); - builder.append("managedZones(").append(ZoneField.selector(fields)).append(')'); + builder.append("nextPageToken,managedZones(").append(ZoneField.selector(fields)).append(')'); return new ZoneListOption(DnsRpc.Option.FIELDS, builder.toString()); } @@ -388,7 +390,8 @@ class ChangeRequestListOption extends AbstractOption implements Serializable { */ public static ChangeRequestListOption fields(ChangeRequestField... fields) { StringBuilder builder = new StringBuilder(); - builder.append("changes(").append(ChangeRequestField.selector(fields)).append(')'); + builder.append("nextPageToken,changes(").append(ChangeRequestField.selector(fields)) + .append(')'); return new ChangeRequestListOption(DnsRpc.Option.FIELDS, builder.toString()); } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DefaultDnsRpc.java b/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DefaultDnsRpc.java index 6ed9c7e0f216..1df0a8a2f831 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DefaultDnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DefaultDnsRpc.java @@ -162,7 +162,9 @@ public Change getChangeRequest(String zoneName, String changeRequestId, Map zones = DNS.listZones(); + Iterator zoneIterator = zones.iterateAll(); + while (zoneIterator.hasNext()) { + Zone zone = zoneIterator.next(); + List toDelete = new LinkedList<>(); + if (zone.name().startsWith(PREFIX)) { + Iterator dnsRecordIterator = zone.listDnsRecords().iterateAll(); + while (dnsRecordIterator.hasNext()) { + DnsRecord record = dnsRecordIterator.next(); + if (!ImmutableList.of(DnsRecord.Type.NS, DnsRecord.Type.SOA).contains(record.type())) { + toDelete.add(record); + } + } + if (!toDelete.isEmpty()) { + zone.applyChangeRequest(ChangeRequest.builder().deletions(toDelete).build()); + } + zone.delete(); + } + } + } + + private static List filter(Iterator iterator) { + List result = new LinkedList<>(); + while (iterator.hasNext()) { + Zone zone = iterator.next(); + if (zone.name().startsWith(PREFIX)) { + result.add(zone); + } + } + return result; + } + + @BeforeClass + public static void before() { + purge(); + } + + @AfterClass + public static void after() { + purge(); + } + + @Test + public void testCreateValidZone() { + Zone created = DNS.create(ZONE1); + assertEquals(ZONE1.description(), created.description()); + assertEquals(ZONE1.dnsName(), created.dnsName()); + assertEquals(ZONE1.name(), created.name()); + assertNotNull(created.creationTimeMillis()); + assertNotNull(created.nameServers()); + assertNull(created.nameServerSet()); + assertNotNull(created.id()); + Zone retrieved = DNS.getZone(ZONE1.name()); + assertEquals(created, retrieved); + created = DNS.create(ZONE_EMPTY_DESCRIPTION); + assertEquals(ZONE_EMPTY_DESCRIPTION.description(), created.description()); + assertEquals(ZONE_EMPTY_DESCRIPTION.dnsName(), created.dnsName()); + assertEquals(ZONE_EMPTY_DESCRIPTION.name(), created.name()); + assertNotNull(created.creationTimeMillis()); + assertNotNull(created.nameServers()); + assertNull(created.nameServerSet()); + assertNotNull(created.id()); + retrieved = DNS.getZone(ZONE_EMPTY_DESCRIPTION.name()); + assertEquals(created, retrieved); + } + + @After + public void tearDown() { + purge(); + } + + @Test + public void testCreateZoneWithErrors() { + try { + DNS.create(ZONE_MISSING_DNS_NAME); + fail("Zone is missing DNS name. The service returns an error."); + } catch (DnsException ex) { + // expected + // todo(mderka) test non-retryable when implemented within #593 + } + try { + DNS.create(ZONE_MISSING_DESCRIPTION); + fail("Zone is missing description name. The service returns an error."); + } catch (DnsException ex) { + // expected + // todo(mderka) test non-retryable when implemented within #593 + } + try { + DNS.create(ZONE_NAME_ERROR); + fail("Zone name is missing a period. The service returns an error."); + } catch (DnsException ex) { + // expected + // todo(mderka) test non-retryable when implemented within #593 + } + try { + DNS.create(ZONE_DNS_NO_PERIOD); + fail("Zone name is missing a period. The service returns an error."); + } catch (DnsException ex) { + // expected + // todo(mderka) test non-retryable when implemented within #593 + } + } + + @Test + public void testCreateZoneWithOptions() { + Zone created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.CREATION_TIME)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNotNull(created.creationTimeMillis()); + assertNull(created.description()); + assertNull(created.dnsName()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); + assertNull(created.id()); + created.delete(); + created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.DESCRIPTION)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertEquals(ZONE1.description(), created.description()); + assertNull(created.dnsName()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); + assertNull(created.id()); + created.delete(); + created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.DNS_NAME)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertEquals(ZONE1.dnsName(), created.dnsName()); + assertNull(created.description()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); + assertNull(created.id()); + created.delete(); + created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertNull(created.description()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); + assertNull(created.id()); + created.delete(); + created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVER_SET)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertNull(created.description()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); // we did not set it + assertNull(created.id()); + created.delete(); + created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVERS)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertNull(created.description()); + assertFalse(created.nameServers().isEmpty()); + assertNull(created.nameServerSet()); + assertNull(created.id()); + created.delete(); + created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.ZONE_ID)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertNull(created.description()); + assertNotNull(created.nameServers()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNotNull(created.id()); + created.delete(); + // combination of multiple things + created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.ZONE_ID, + Dns.ZoneField.NAME_SERVERS, Dns.ZoneField.NAME_SERVER_SET, Dns.ZoneField.DESCRIPTION)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertEquals(ZONE1.description(), created.description()); + assertFalse(created.nameServers().isEmpty()); + assertNull(created.nameServerSet()); // we did not set it + assertNotNull(created.id()); + } + + @Test + public void testZoneReload() { + Zone created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); + created = created.reload(Dns.ZoneOption.fields(Dns.ZoneField.CREATION_TIME)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNotNull(created.creationTimeMillis()); + assertNull(created.description()); + assertNull(created.dnsName()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); + assertNull(created.id()); + created = created.reload(Dns.ZoneOption.fields(Dns.ZoneField.DESCRIPTION)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertEquals(ZONE1.description(), created.description()); + assertNull(created.dnsName()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); + assertNull(created.id()); + created = created.reload(Dns.ZoneOption.fields(Dns.ZoneField.DNS_NAME)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertEquals(ZONE1.dnsName(), created.dnsName()); + assertNull(created.description()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); + assertNull(created.id()); + created = created.reload(Dns.ZoneOption.fields(Dns.ZoneField.NAME)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertNull(created.description()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); + assertNull(created.id()); + created = created.reload(Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVER_SET)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertNull(created.description()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); // we did not set it + assertNull(created.id()); + created = created.reload(Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVERS)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertNull(created.description()); + assertFalse(created.nameServers().isEmpty()); + assertNull(created.nameServerSet()); + assertNull(created.id()); + created = created.reload(Dns.ZoneOption.fields(Dns.ZoneField.ZONE_ID)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertNull(created.description()); + assertNotNull(created.nameServers()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNotNull(created.id()); + // combination of multiple things + created = created.reload(Dns.ZoneOption.fields(Dns.ZoneField.ZONE_ID, + Dns.ZoneField.NAME_SERVERS, Dns.ZoneField.NAME_SERVER_SET, Dns.ZoneField.DESCRIPTION)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertEquals(ZONE1.description(), created.description()); + assertFalse(created.nameServers().isEmpty()); + assertNull(created.nameServerSet()); // we did not set it + assertNotNull(created.id()); + } + + @Test + public void testGetZone() { + DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); + Zone created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.CREATION_TIME)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNotNull(created.creationTimeMillis()); + assertNull(created.description()); + assertNull(created.dnsName()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); + assertNull(created.id()); + created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.DESCRIPTION)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertEquals(ZONE1.description(), created.description()); + assertNull(created.dnsName()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); + assertNull(created.id()); + created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.DNS_NAME)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertEquals(ZONE1.dnsName(), created.dnsName()); + assertNull(created.description()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); + assertNull(created.id()); + created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.NAME)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertNull(created.description()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); + assertNull(created.id()); + created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVER_SET)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertNull(created.description()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); // we did not set it + assertNull(created.id()); + created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVERS)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertNull(created.description()); + assertFalse(created.nameServers().isEmpty()); + assertNull(created.nameServerSet()); + assertNull(created.id()); + created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.ZONE_ID)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertNull(created.description()); + assertNotNull(created.nameServers()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNotNull(created.id()); + // combination of multiple things + created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.ZONE_ID, + Dns.ZoneField.NAME_SERVERS, Dns.ZoneField.NAME_SERVER_SET, Dns.ZoneField.DESCRIPTION)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertEquals(ZONE1.description(), created.description()); + assertFalse(created.nameServers().isEmpty()); + assertNull(created.nameServerSet()); // we did not set it + assertNotNull(created.id()); + } + + @Test + public void testListZones() { + List zones = filter(DNS.listZones().iterateAll()); + assertEquals(0, zones.size()); + // some zones exists + Zone created = DNS.create(ZONE1); + zones = filter(DNS.listZones().iterateAll()); + assertEquals(created, zones.get(0)); + assertEquals(1, zones.size()); + created = DNS.create(ZONE_EMPTY_DESCRIPTION); + zones = filter(DNS.listZones().iterateAll()); + assertEquals(2, zones.size()); + assertTrue(zones.contains(created)); + // error in options + try { + DNS.listZones(Dns.ZoneListOption.pageSize(0)); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + // todo(mderka) test not-retryable + } + try { + DNS.listZones(Dns.ZoneListOption.pageSize(-1)); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + // todo(mderka) test not-retryable + } + // ok size + zones = filter(DNS.listZones(Dns.ZoneListOption.pageSize(1000)).iterateAll()); + assertEquals(2, zones.size()); // we still have only 2 zones + // dns name problems + try { + DNS.listZones(Dns.ZoneListOption.dnsName("aaaaa")); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + // todo(mderka) test not-retryable + } + // ok name + zones = filter(DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName())).iterateAll()); + assertEquals(1, zones.size()); + // field options + zones = ImmutableList.copyOf(DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), + Dns.ZoneListOption.fields(Dns.ZoneField.ZONE_ID)).iterateAll()); + assertEquals(1, zones.size()); + Zone zone = zones.get(0); + assertNull(zone.creationTimeMillis()); + assertNotNull(zone.name()); + assertNull(zone.dnsName()); + assertNull(zone.description()); + assertNull(zone.nameServerSet()); + assertTrue(zone.nameServers().isEmpty()); + assertNotNull(zone.id()); + zones = ImmutableList.copyOf(DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), + Dns.ZoneListOption.fields(Dns.ZoneField.CREATION_TIME)).iterateAll()); + assertEquals(1, zones.size()); + zone = zones.get(0); + assertNotNull(zone.creationTimeMillis()); + assertNotNull(zone.name()); + assertNull(zone.dnsName()); + assertNull(zone.description()); + assertNull(zone.nameServerSet()); + assertTrue(zone.nameServers().isEmpty()); + assertNull(zone.id()); + zones = ImmutableList.copyOf(DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), + Dns.ZoneListOption.fields(Dns.ZoneField.DNS_NAME)).iterateAll()); + assertEquals(1, zones.size()); + zone = zones.get(0); + assertNull(zone.creationTimeMillis()); + assertNotNull(zone.name()); + assertNotNull(zone.dnsName()); + assertNull(zone.description()); + assertNull(zone.nameServerSet()); + assertTrue(zone.nameServers().isEmpty()); + assertNull(zone.id()); + zones = ImmutableList.copyOf(DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), + Dns.ZoneListOption.fields(Dns.ZoneField.DESCRIPTION)).iterateAll()); + assertEquals(1, zones.size()); + zone = zones.get(0); + assertNull(zone.creationTimeMillis()); + assertNotNull(zone.name()); + assertNull(zone.dnsName()); + assertNotNull(zone.description()); + assertNull(zone.nameServerSet()); + assertTrue(zone.nameServers().isEmpty()); + assertNull(zone.id()); + zones = ImmutableList.copyOf(DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), + Dns.ZoneListOption.fields(Dns.ZoneField.NAME_SERVERS)).iterateAll()); + assertEquals(1, zones.size()); + zone = zones.get(0); + assertNull(zone.creationTimeMillis()); + assertNotNull(zone.name()); + assertNull(zone.dnsName()); + assertNull(zone.description()); + assertNull(zone.nameServerSet()); + assertTrue(!zone.nameServers().isEmpty()); + assertNull(zone.id()); + zones = ImmutableList.copyOf(DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), + Dns.ZoneListOption.fields(Dns.ZoneField.NAME_SERVER_SET)).iterateAll()); + assertEquals(1, zones.size()); + zone = zones.get(0); + assertNull(zone.creationTimeMillis()); + assertNotNull(zone.name()); + assertNull(zone.dnsName()); + assertNull(zone.description()); + assertNull(zone.nameServerSet()); // we cannot set it using gcloud java + assertTrue(zone.nameServers().isEmpty()); + assertNull(zone.id()); + // several combined + zones = filter(DNS.listZones(Dns.ZoneListOption.fields(Dns.ZoneField.ZONE_ID, + Dns.ZoneField.DESCRIPTION), + Dns.ZoneListOption.pageSize(1)).iterateAll()); + assertEquals(2, zones.size()); + for (Zone current : zones) { + assertNull(current.creationTimeMillis()); + assertNotNull(current.name()); + assertNull(current.dnsName()); + assertNotNull(current.description()); + assertNull(current.nameServerSet()); + assertTrue(zone.nameServers().isEmpty()); + assertNotNull(current.id()); + } + } + + @Test + public void testDeleteZoneUsingServiceObject() { + Zone created = DNS.create(ZONE1); + assertEquals(created, DNS.getZone(ZONE1.name())); + DNS.delete(ZONE1.name()); + assertNull(DNS.getZone(ZONE1.name())); + } + + @Test + public void testDeleteZoneUsingZoneObject() { + Zone created = DNS.create(ZONE1); + assertEquals(created, DNS.getZone(ZONE1.name())); + created.delete(); + assertNull(DNS.getZone(ZONE1.name())); + } + + private void assertEqChangesIgnoreStatus(ChangeRequest expected, ChangeRequest actual) { + ChangeRequest unifiedEx = ChangeRequest.fromPb(expected.toPb().setStatus("pending")); + ChangeRequest unifiedAct = ChangeRequest.fromPb(actual.toPb().setStatus("pending")); + assertEquals(unifiedEx, unifiedAct); + } + + private static void checkChangeComplete(String zoneName, String changeId) { + for (int i = 0; i < TIME_LIMIT; i++) { + ChangeRequest changeRequest = DNS.getChangeRequest(zoneName, changeId, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); + if (ChangeRequest.Status.DONE.equals(changeRequest.status())) { + break; + } else { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + throw new RuntimeException("Thread was interrupted while waiting for change."); + } + } + } + } + + @Test + public void testCreateChangeUsingServiceObject() { + DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); + ChangeRequest created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); + assertEquals(CHANGE_ADD_ZONE1.additions(), created.additions()); + assertNotNull(created.startTimeMillis()); + assertTrue(created.deletions().isEmpty()); + assertEquals("1", created.id()); + assertTrue(ImmutableList.of(ChangeRequest.Status.PENDING, ChangeRequest.Status.DONE) + .contains(created.status())); + assertEqChangesIgnoreStatus(created, DNS.getChangeRequest(ZONE1.name(), "1")); + checkChangeComplete(ZONE1.name(), "1"); + DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + checkChangeComplete(ZONE1.name(), "2"); + // with options + created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID)); + assertTrue(created.additions().isEmpty()); + assertNull(created.startTimeMillis()); + assertTrue(created.deletions().isEmpty()); + assertEquals("3", created.id()); + assertNull(created.status()); + checkChangeComplete(ZONE1.name(), "3"); + DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + checkChangeComplete(ZONE1.name(), "4"); + created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); + assertTrue(created.additions().isEmpty()); + assertNull(created.startTimeMillis()); + assertTrue(created.deletions().isEmpty()); + assertEquals("5", created.id()); + assertNotNull(created.status()); + checkChangeComplete(ZONE1.name(), "5"); + DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + checkChangeComplete(ZONE1.name(), "6"); + created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); + assertTrue(created.additions().isEmpty()); + assertNotNull(created.startTimeMillis()); + assertTrue(created.deletions().isEmpty()); + assertEquals("7", created.id()); + assertNull(created.status()); + checkChangeComplete(ZONE1.name(), "7"); + DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + checkChangeComplete(ZONE1.name(), "8"); + created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS)); + assertEquals(CHANGE_ADD_ZONE1.additions(), created.additions()); + assertNull(created.startTimeMillis()); + assertTrue(created.deletions().isEmpty()); + assertEquals("9", created.id()); + assertNull(created.status()); + // finishes with delete otherwise we cannot delete the zone + checkChangeComplete(ZONE1.name(), "9"); + created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS)); + checkChangeComplete(ZONE1.name(), "10"); + assertEquals(CHANGE_DELETE_ZONE1.deletions(), created.deletions()); + assertNull(created.startTimeMillis()); + assertTrue(created.additions().isEmpty()); + assertEquals("10", created.id()); + assertNull(created.status()); + checkChangeComplete(ZONE1.name(), "10"); + } + + @Test + public void testCreateChangeUsingZoneObject() { + Zone zone = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); + ChangeRequest created = zone.applyChangeRequest(CHANGE_ADD_ZONE1); + assertEquals(CHANGE_ADD_ZONE1.additions(), created.additions()); + assertNotNull(created.startTimeMillis()); + assertTrue(created.deletions().isEmpty()); + assertEquals("1", created.id()); + assertTrue(ImmutableList.of(ChangeRequest.Status.PENDING, ChangeRequest.Status.DONE) + .contains(created.status())); + assertEqChangesIgnoreStatus(created, DNS.getChangeRequest(ZONE1.name(), "1")); + checkChangeComplete(ZONE1.name(), "1"); + zone.applyChangeRequest(CHANGE_DELETE_ZONE1); + checkChangeComplete(ZONE1.name(), "2"); + // with options + created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID)); + assertTrue(created.additions().isEmpty()); + assertNull(created.startTimeMillis()); + assertTrue(created.deletions().isEmpty()); + assertEquals("3", created.id()); + assertNull(created.status()); + checkChangeComplete(ZONE1.name(), "3"); + zone.applyChangeRequest(CHANGE_DELETE_ZONE1); + checkChangeComplete(ZONE1.name(), "4"); + created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); + assertTrue(created.additions().isEmpty()); + assertNull(created.startTimeMillis()); + assertTrue(created.deletions().isEmpty()); + assertEquals("5", created.id()); + assertNotNull(created.status()); + checkChangeComplete(ZONE1.name(), "5"); + zone.applyChangeRequest(CHANGE_DELETE_ZONE1); + checkChangeComplete(ZONE1.name(), "6"); + created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); + assertTrue(created.additions().isEmpty()); + assertNotNull(created.startTimeMillis()); + assertTrue(created.deletions().isEmpty()); + assertEquals("7", created.id()); + assertNull(created.status()); + checkChangeComplete(ZONE1.name(), "7"); + zone.applyChangeRequest(CHANGE_DELETE_ZONE1); + checkChangeComplete(ZONE1.name(), "8"); + created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS)); + assertEquals(CHANGE_ADD_ZONE1.additions(), created.additions()); + assertNull(created.startTimeMillis()); + assertTrue(created.deletions().isEmpty()); + assertEquals("9", created.id()); + assertNull(created.status()); + // finishes with delete otherwise we cannot delete the zone + checkChangeComplete(ZONE1.name(), "9"); + created = zone.applyChangeRequest(CHANGE_DELETE_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS)); + checkChangeComplete(ZONE1.name(), "10"); + assertEquals(CHANGE_DELETE_ZONE1.deletions(), created.deletions()); + assertNull(created.startTimeMillis()); + assertTrue(created.additions().isEmpty()); + assertEquals("10", created.id()); + assertNull(created.status()); + checkChangeComplete(ZONE1.name(), "10"); + } + + @Test + public void testListChangesUsingService() { + // no such zone exists + try { + DNS.listChangeRequests(ZONE1.name()); + fail(); + } catch (DnsException ex) { + // expected + assertEquals(404, ex.code()); + // todo(mderka) test retry functionality + } + // zone exists but has no changes + DNS.create(ZONE1); + ImmutableList changes = ImmutableList.copyOf( + DNS.listChangeRequests(ZONE1.name()).iterateAll()); + assertEquals(1, changes.size()); // default change creating SOA and NS + // zone has changes + ChangeRequest change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); + checkChangeComplete(ZONE1.name(), change.id()); + change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + checkChangeComplete(ZONE1.name(), change.id()); + change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); + checkChangeComplete(ZONE1.name(), change.id()); + change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + checkChangeComplete(ZONE1.name(), change.id()); + changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name()).iterateAll()); + assertEquals(5, changes.size()); + // error in options + try { + DNS.listChangeRequests(ZONE1.name(), Dns.ChangeRequestListOption.pageSize(0)); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + // todo(mderka) test retry functionality + } + try { + DNS.listChangeRequests(ZONE1.name(), Dns.ChangeRequestListOption.pageSize(-1)); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + // todo(mderka) test retry functionality + } + // sorting order + ImmutableList ascending = ImmutableList.copyOf(DNS.listChangeRequests( + ZONE1.name(), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING)).iterateAll()); + ImmutableList descending = ImmutableList.copyOf(DNS.listChangeRequests( + ZONE1.name(), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.DESCENDING)).iterateAll()); + int size = 5; + assertEquals(size, descending.size()); + assertEquals(size, ascending.size()); + for (int i = 0; i < size; i++) { + assertEquals(descending.get(i), ascending.get(size - i - 1)); + } + // field options + changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name(), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), + Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.ADDITIONS)).iterateAll()); + change = changes.get(1); + assertEquals(CHANGE_ADD_ZONE1.additions(), change.additions()); + assertTrue(change.deletions().isEmpty()); + assertEquals("1", change.id()); + assertNull(change.startTimeMillis()); + assertNull(change.status()); + changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name(), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), + Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.DELETIONS)).iterateAll()); + change = changes.get(2); + assertTrue(change.additions().isEmpty()); + assertNotNull(change.deletions()); + assertEquals("2", change.id()); + assertNull(change.startTimeMillis()); + assertNull(change.status()); + changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name(), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), + Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.ID)).iterateAll()); + change = changes.get(1); + assertTrue(change.additions().isEmpty()); + assertTrue(change.deletions().isEmpty()); + assertEquals("1", change.id()); + assertNull(change.startTimeMillis()); + assertNull(change.status()); + changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name(), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), + Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.START_TIME)).iterateAll()); + change = changes.get(1); + assertTrue(change.additions().isEmpty()); + assertTrue(change.deletions().isEmpty()); + assertEquals("1", change.id()); + assertNotNull(change.startTimeMillis()); + assertNull(change.status()); + changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name(), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), + Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.STATUS)).iterateAll()); + change = changes.get(1); + assertTrue(change.additions().isEmpty()); + assertTrue(change.deletions().isEmpty()); + assertEquals("1", change.id()); + assertNull(change.startTimeMillis()); + assertEquals(ChangeRequest.Status.DONE, change.status()); + } + + @Test + public void testListChangesUsingZoneObject() { + // zone exists but has no changes + Zone created = DNS.create(ZONE1); + ImmutableList changes = ImmutableList.copyOf( + created.listChangeRequests().iterateAll()); + assertEquals(1, changes.size()); // default change creating SOA and NS + // zone has changes + ChangeRequest change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); + checkChangeComplete(ZONE1.name(), change.id()); + change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + checkChangeComplete(ZONE1.name(), change.id()); + change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); + checkChangeComplete(ZONE1.name(), change.id()); + change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + checkChangeComplete(ZONE1.name(), change.id()); + changes = ImmutableList.copyOf(created.listChangeRequests().iterateAll()); + assertEquals(5, changes.size()); + // error in options + try { + created.listChangeRequests(Dns.ChangeRequestListOption.pageSize(0)); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + // todo(mderka) test retry functionality + } + try { + created.listChangeRequests(Dns.ChangeRequestListOption.pageSize(-1)); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + // todo(mderka) test retry functionality + } + // sorting order + ImmutableList ascending = ImmutableList.copyOf(created.listChangeRequests( + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING)).iterateAll()); + ImmutableList descending = ImmutableList.copyOf(created.listChangeRequests( + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.DESCENDING)).iterateAll()); + int size = 5; + assertEquals(size, descending.size()); + assertEquals(size, ascending.size()); + for (int i = 0; i < size; i++) { + assertEquals(descending.get(i), ascending.get(size - i - 1)); + } + // field options + changes = ImmutableList.copyOf(created.listChangeRequests( + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), + Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.ADDITIONS)).iterateAll()); + change = changes.get(1); + assertEquals(CHANGE_ADD_ZONE1.additions(), change.additions()); + assertTrue(change.deletions().isEmpty()); + assertEquals("1", change.id()); + assertNull(change.startTimeMillis()); + assertNull(change.status()); + changes = ImmutableList.copyOf(created.listChangeRequests( + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), + Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.DELETIONS)).iterateAll()); + change = changes.get(2); + assertTrue(change.additions().isEmpty()); + assertNotNull(change.deletions()); + assertEquals("2", change.id()); + assertNull(change.startTimeMillis()); + assertNull(change.status()); + changes = ImmutableList.copyOf(created.listChangeRequests( + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), + Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.ID)).iterateAll()); + change = changes.get(1); + assertTrue(change.additions().isEmpty()); + assertTrue(change.deletions().isEmpty()); + assertEquals("1", change.id()); + assertNull(change.startTimeMillis()); + assertNull(change.status()); + changes = ImmutableList.copyOf(created.listChangeRequests( + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), + Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.START_TIME)).iterateAll()); + change = changes.get(1); + assertTrue(change.additions().isEmpty()); + assertTrue(change.deletions().isEmpty()); + assertEquals("1", change.id()); + assertNotNull(change.startTimeMillis()); + assertNull(change.status()); + changes = ImmutableList.copyOf(created.listChangeRequests( + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), + Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.STATUS)).iterateAll()); + change = changes.get(1); + assertTrue(change.additions().isEmpty()); + assertTrue(change.deletions().isEmpty()); + assertEquals("1", change.id()); + assertNull(change.startTimeMillis()); + assertEquals(ChangeRequest.Status.DONE, change.status()); + } + + @Test + public void testGetChangeUsingZoneObject() { + Zone zone = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); + ChangeRequest created = zone.applyChangeRequest(CHANGE_ADD_ZONE1); + ChangeRequest retrieved = zone.getChangeRequest(created.id()); + assertEqChangesIgnoreStatus(created, retrieved); + checkChangeComplete(ZONE1.name(), "1"); + zone.applyChangeRequest(CHANGE_DELETE_ZONE1); + checkChangeComplete(ZONE1.name(), "2"); + // with options + created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID)); + retrieved = zone.getChangeRequest(created.id(), + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID)); + assertEqChangesIgnoreStatus(created, retrieved); + checkChangeComplete(ZONE1.name(), "3"); + zone.applyChangeRequest(CHANGE_DELETE_ZONE1); + checkChangeComplete(ZONE1.name(), "4"); + created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); + retrieved = zone.getChangeRequest(created.id(), + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); + assertEqChangesIgnoreStatus(created, retrieved); + checkChangeComplete(ZONE1.name(), "5"); + zone.applyChangeRequest(CHANGE_DELETE_ZONE1); + checkChangeComplete(ZONE1.name(), "6"); + created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); + retrieved = zone.getChangeRequest(created.id(), + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); + assertEqChangesIgnoreStatus(created, retrieved); + checkChangeComplete(ZONE1.name(), "7"); + zone.applyChangeRequest(CHANGE_DELETE_ZONE1); + checkChangeComplete(ZONE1.name(), "8"); + created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS)); + retrieved = zone.getChangeRequest(created.id(), + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS)); + assertEqChangesIgnoreStatus(created, retrieved); + // finishes with delete otherwise we cannot delete the zone + checkChangeComplete(ZONE1.name(), "9"); + created = zone.applyChangeRequest(CHANGE_DELETE_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS)); + retrieved = zone.getChangeRequest(created.id(), + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS)); + assertEqChangesIgnoreStatus(created, retrieved); + checkChangeComplete(ZONE1.name(), "10"); + } + + @Test + public void testGetChangeUsingServiceObject() { + Zone zone = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); + ChangeRequest created = zone.applyChangeRequest(CHANGE_ADD_ZONE1); + ChangeRequest retrieved = DNS.getChangeRequest(zone.name(), created.id()); + assertEqChangesIgnoreStatus(created, retrieved); + zone.applyChangeRequest(CHANGE_DELETE_ZONE1); + // with options + created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID)); + retrieved = DNS.getChangeRequest(zone.name(), created.id(), + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID)); + assertEqChangesIgnoreStatus(created, retrieved); + zone.applyChangeRequest(CHANGE_DELETE_ZONE1); + created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); + retrieved = DNS.getChangeRequest(zone.name(), created.id(), + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); + assertEqChangesIgnoreStatus(created, retrieved); + zone.applyChangeRequest(CHANGE_DELETE_ZONE1); + created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); + retrieved = DNS.getChangeRequest(zone.name(), created.id(), + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); + assertEqChangesIgnoreStatus(created, retrieved); + zone.applyChangeRequest(CHANGE_DELETE_ZONE1); + created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS)); + retrieved = DNS.getChangeRequest(zone.name(), created.id(), + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS)); + assertEqChangesIgnoreStatus(created, retrieved); + // finishes with delete otherwise we cannot delete the zone + created = zone.applyChangeRequest(CHANGE_DELETE_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS)); + retrieved = DNS.getChangeRequest(zone.name(), created.id(), + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS)); + assertEqChangesIgnoreStatus(created, retrieved); + } + + @Test + public void testGetProject() { + // fetches all fields + ProjectInfo project = DNS.getProject(); + assertNotNull(project.quota()); + assertNotNull(project.number()); + assertNotNull(project.id()); + assertEquals(PROJECT_ID, project.id()); + // options + project = DNS.getProject(Dns.ProjectOption.fields(Dns.ProjectField.QUOTA)); + assertNotNull(project.quota()); + assertNull(project.number()); + assertNotNull(project.id()); // id is always returned + project = DNS.getProject(Dns.ProjectOption.fields(Dns.ProjectField.PROJECT_ID)); + assertNull(project.quota()); + assertNull(project.number()); + assertNotNull(project.id()); + project = DNS.getProject(Dns.ProjectOption.fields(Dns.ProjectField.PROJECT_NUMBER)); + assertNull(project.quota()); + assertNotNull(project.number()); + assertNotNull(project.id()); + project = DNS.getProject(Dns.ProjectOption.fields(Dns.ProjectField.PROJECT_NUMBER, + Dns.ProjectField.QUOTA, Dns.ProjectField.PROJECT_ID)); + assertNotNull(project.quota()); + assertNotNull(project.number()); + assertNotNull(project.id()); + } + + @Test + public void testListDnsRecordsUsingService() { + Zone zone = DNS.create(ZONE1); + ImmutableList dnsRecords = ImmutableList.copyOf( + DNS.listDnsRecords(zone.name()).iterateAll()); + assertEquals(2, dnsRecords.size()); + ImmutableList defaultRecords = + ImmutableList.of(DnsRecord.Type.NS, DnsRecord.Type.SOA); + for (DnsRecord record : dnsRecords) { + assertTrue(defaultRecords.contains(record.type())); + } + // field options + Iterator dnsRecordIterator = DNS.listDnsRecords(zone.name(), + Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TTL)).iterateAll(); + int counter = 0; + while (dnsRecordIterator.hasNext()) { + DnsRecord record = dnsRecordIterator.next(); + assertEquals(dnsRecords.get(counter).ttl(), record.ttl()); + assertEquals(dnsRecords.get(counter).name(), record.name()); + assertEquals(dnsRecords.get(counter).type(), record.type()); + assertTrue(record.records().isEmpty()); + counter++; + } + assertEquals(2, counter); + dnsRecordIterator = DNS.listDnsRecords(zone.name(), + Dns.DnsRecordListOption.fields(Dns.DnsRecordField.NAME)).iterateAll(); + counter = 0; + while (dnsRecordIterator.hasNext()) { + DnsRecord record = dnsRecordIterator.next(); + assertEquals(dnsRecords.get(counter).name(), record.name()); + assertEquals(dnsRecords.get(counter).type(), record.type()); + assertTrue(record.records().isEmpty()); + assertNull(record.ttl()); + counter++; + } + assertEquals(2, counter); + dnsRecordIterator = DNS.listDnsRecords(zone.name(), + Dns.DnsRecordListOption.fields(Dns.DnsRecordField.DNS_RECORDS)).iterateAll(); + counter = 0; + while (dnsRecordIterator.hasNext()) { + DnsRecord record = dnsRecordIterator.next(); + assertEquals(dnsRecords.get(counter).records(), record.records()); + assertEquals(dnsRecords.get(counter).name(), record.name()); + assertEquals(dnsRecords.get(counter).type(), record.type()); + assertNull(record.ttl()); + counter++; + } + assertEquals(2, counter); + dnsRecordIterator = DNS.listDnsRecords(zone.name(), + Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TYPE), + Dns.DnsRecordListOption.pageSize(1)).iterateAll(); // also test paging + counter = 0; + while (dnsRecordIterator.hasNext()) { + DnsRecord record = dnsRecordIterator.next(); + assertEquals(dnsRecords.get(counter).type(), record.type()); + assertEquals(dnsRecords.get(counter).name(), record.name()); + assertTrue(record.records().isEmpty()); + assertNull(record.ttl()); + counter++; + } + assertEquals(2, counter); + // test page size + Page dnsRecordPage = DNS.listDnsRecords(zone.name(), + Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TYPE), + Dns.DnsRecordListOption.pageSize(1)); + assertEquals(1, ImmutableList.copyOf(dnsRecordPage.values().iterator()).size()); + // test name filter + ChangeRequest change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); + checkChangeComplete(ZONE1.name(), change.id()); + dnsRecordIterator = DNS.listDnsRecords(ZONE1.name(), + Dns.DnsRecordListOption.dnsName(A_RECORD_ZONE1.name())).iterateAll(); + counter = 0; + while (dnsRecordIterator.hasNext()) { + DnsRecord record = dnsRecordIterator.next(); + assertTrue(ImmutableList.of(A_RECORD_ZONE1.type(), AAAA_RECORD_ZONE1.type()) + .contains(record.type())); + counter++; + } + assertEquals(2, counter); + // test type filter + checkChangeComplete(ZONE1.name(), change.id()); + dnsRecordIterator = DNS.listDnsRecords(ZONE1.name(), + Dns.DnsRecordListOption.dnsName(A_RECORD_ZONE1.name()), + Dns.DnsRecordListOption.type(A_RECORD_ZONE1.type())) + .iterateAll(); + counter = 0; + while (dnsRecordIterator.hasNext()) { + DnsRecord record = dnsRecordIterator.next(); + assertEquals(A_RECORD_ZONE1, record); + counter++; + } + assertEquals(1, counter); + change = zone.applyChangeRequest(CHANGE_DELETE_ZONE1); + // check wrong arguments + try { + // name is not set + DNS.listDnsRecords(ZONE1.name(), + Dns.DnsRecordListOption.type(A_RECORD_ZONE1.type())); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + // todo(mderka) test retry functionality when available + } + try { + DNS.listDnsRecords(ZONE1.name(), + Dns.DnsRecordListOption.pageSize(0)); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + // todo(mderka) test retry functionality when available + } + try { + DNS.listDnsRecords(ZONE1.name(), + Dns.DnsRecordListOption.pageSize(-1)); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + // todo(mderka) test retry functionality when available + } + checkChangeComplete(ZONE1.name(), change.id()); + } + + @Test + public void testListDnsRecordsUsingZoneObject() { + Zone zone = DNS.create(ZONE1); + ImmutableList dnsRecords = ImmutableList.copyOf( + zone.listDnsRecords().iterateAll()); + assertEquals(2, dnsRecords.size()); + ImmutableList defaultRecords = + ImmutableList.of(DnsRecord.Type.NS, DnsRecord.Type.SOA); + for (DnsRecord record : dnsRecords) { + assertTrue(defaultRecords.contains(record.type())); + } + // field options + Iterator dnsRecordIterator = zone.listDnsRecords( + Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TTL)).iterateAll(); + int counter = 0; + while (dnsRecordIterator.hasNext()) { + DnsRecord record = dnsRecordIterator.next(); + assertEquals(dnsRecords.get(counter).ttl(), record.ttl()); + assertEquals(dnsRecords.get(counter).name(), record.name()); + assertEquals(dnsRecords.get(counter).type(), record.type()); + assertTrue(record.records().isEmpty()); + counter++; + } + assertEquals(2, counter); + dnsRecordIterator = zone.listDnsRecords( + Dns.DnsRecordListOption.fields(Dns.DnsRecordField.NAME)).iterateAll(); + counter = 0; + while (dnsRecordIterator.hasNext()) { + DnsRecord record = dnsRecordIterator.next(); + assertEquals(dnsRecords.get(counter).name(), record.name()); + assertEquals(dnsRecords.get(counter).type(), record.type()); + assertTrue(record.records().isEmpty()); + assertNull(record.ttl()); + counter++; + } + assertEquals(2, counter); + dnsRecordIterator = zone.listDnsRecords( + Dns.DnsRecordListOption.fields(Dns.DnsRecordField.DNS_RECORDS)).iterateAll(); + counter = 0; + while (dnsRecordIterator.hasNext()) { + DnsRecord record = dnsRecordIterator.next(); + assertEquals(dnsRecords.get(counter).records(), record.records()); + assertEquals(dnsRecords.get(counter).name(), record.name()); + assertEquals(dnsRecords.get(counter).type(), record.type()); + assertNull(record.ttl()); + counter++; + } + assertEquals(2, counter); + dnsRecordIterator = zone.listDnsRecords( + Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TYPE), + Dns.DnsRecordListOption.pageSize(1)).iterateAll(); // also test paging + counter = 0; + while (dnsRecordIterator.hasNext()) { + DnsRecord record = dnsRecordIterator.next(); + assertEquals(dnsRecords.get(counter).type(), record.type()); + assertEquals(dnsRecords.get(counter).name(), record.name()); + assertTrue(record.records().isEmpty()); + assertNull(record.ttl()); + counter++; + } + assertEquals(2, counter); + // test page size + Page dnsRecordPage = zone.listDnsRecords( + Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TYPE), + Dns.DnsRecordListOption.pageSize(1)); + assertEquals(1, ImmutableList.copyOf(dnsRecordPage.values().iterator()).size()); + // test name filter + ChangeRequest change = zone.applyChangeRequest(CHANGE_ADD_ZONE1); + checkChangeComplete(ZONE1.name(), change.id()); + dnsRecordIterator = zone.listDnsRecords(Dns.DnsRecordListOption.dnsName(A_RECORD_ZONE1.name())) + .iterateAll(); + counter = 0; + while (dnsRecordIterator.hasNext()) { + DnsRecord record = dnsRecordIterator.next(); + assertTrue(ImmutableList.of(A_RECORD_ZONE1.type(), AAAA_RECORD_ZONE1.type()) + .contains(record.type())); + counter++; + } + assertEquals(2, counter); + // test type filter + checkChangeComplete(ZONE1.name(), change.id()); + dnsRecordIterator = zone.listDnsRecords(Dns.DnsRecordListOption.dnsName(A_RECORD_ZONE1.name()), + Dns.DnsRecordListOption.type(A_RECORD_ZONE1.type())) + .iterateAll(); + counter = 0; + while (dnsRecordIterator.hasNext()) { + DnsRecord record = dnsRecordIterator.next(); + assertEquals(A_RECORD_ZONE1, record); + counter++; + } + assertEquals(1, counter); + change = zone.applyChangeRequest(CHANGE_DELETE_ZONE1); + // check wrong arguments + try { + // name is not set + zone.listDnsRecords(Dns.DnsRecordListOption.type(A_RECORD_ZONE1.type())); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + // todo(mderka) test retry functionality when available + } + try { + zone.listDnsRecords(Dns.DnsRecordListOption.pageSize(0)); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + // todo(mderka) test retry functionality when available + } + try { + zone.listDnsRecords(Dns.DnsRecordListOption.pageSize(-1)); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + // todo(mderka) test retry functionality when available + } + checkChangeComplete(ZONE1.name(), change.id()); + } +} From 75269ff6a0b2b8518ad4dc562905089c20236412 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Fri, 26 Feb 2016 18:21:53 -0500 Subject: [PATCH 2/3] Removed unnecessary integration tests. Adjusted deleting objects. --- .../java/com/google/gcloud/dns/ITDnsTest.java | 1763 +++++++---------- 1 file changed, 700 insertions(+), 1063 deletions(-) diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ITDnsTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ITDnsTest.java index 07475c52cfda..e473d1c4912c 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ITDnsTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ITDnsTest.java @@ -26,10 +26,11 @@ import com.google.common.collect.ImmutableList; import com.google.gcloud.Page; -import org.junit.After; import org.junit.AfterClass; import org.junit.BeforeClass; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.Timeout; import java.util.Iterator; import java.util.LinkedList; @@ -41,7 +42,6 @@ public class ITDnsTest { // todo(mderka) Implement test for creating invalid change when DnsException is finished. #673 - public static final int TIME_LIMIT = 10; public static final String PREFIX = "gcldjvit-"; public static final Dns DNS = DnsOptions.builder().build().service(); public static final String PROJECT_ID = DNS.options().projectId(); @@ -80,16 +80,16 @@ public class ITDnsTest { .description(ZONE_DESCRIPTION1) .dnsName(ZONE_DNS_NAME_NO_PERIOD) .build(); - public static final DnsRecord A_RECORD_ZONE1 = DnsRecord - .builder("www." + ZONE1.dnsName(), DnsRecord.Type.A) - .records(ImmutableList.of("123.123.55.1")) - .ttl(25, TimeUnit.SECONDS) - .build(); - public static final DnsRecord AAAA_RECORD_ZONE1 = DnsRecord - .builder("www." + ZONE1.dnsName(), DnsRecord.Type.AAAA) - .records(ImmutableList.of("ed:ed:12:aa:36:3:3:105")) - .ttl(25, TimeUnit.SECONDS) - .build(); + public static final DnsRecord A_RECORD_ZONE1 = + DnsRecord.builder("www." + ZONE1.dnsName(), DnsRecord.Type.A) + .records(ImmutableList.of("123.123.55.1")) + .ttl(25, TimeUnit.SECONDS) + .build(); + public static final DnsRecord AAAA_RECORD_ZONE1 = + DnsRecord.builder("www." + ZONE1.dnsName(), DnsRecord.Type.AAAA) + .records(ImmutableList.of("ed:ed:12:aa:36:3:3:105")) + .ttl(25, TimeUnit.SECONDS) + .build(); public static final ChangeRequest CHANGE_ADD_ZONE1 = ChangeRequest.builder() .add(A_RECORD_ZONE1) .add(AAAA_RECORD_ZONE1) @@ -99,7 +99,7 @@ public class ITDnsTest { .delete(AAAA_RECORD_ZONE1) .build(); - public static void purge() { + public static void clear() { Page zones = DNS.listZones(); Iterator zoneIterator = zones.iterateAll(); while (zoneIterator.hasNext()) { @@ -114,7 +114,9 @@ public static void purge() { } } if (!toDelete.isEmpty()) { - zone.applyChangeRequest(ChangeRequest.builder().deletions(toDelete).build()); + ChangeRequest deletion = + zone.applyChangeRequest(ChangeRequest.builder().deletions(toDelete).build()); + checkChangeComplete(zone.name(), deletion.id()); } zone.delete(); } @@ -134,868 +136,617 @@ private static List filter(Iterator iterator) { @BeforeClass public static void before() { - purge(); + clear(); } @AfterClass public static void after() { - purge(); + clear(); } - @Test - public void testCreateValidZone() { - Zone created = DNS.create(ZONE1); - assertEquals(ZONE1.description(), created.description()); - assertEquals(ZONE1.dnsName(), created.dnsName()); - assertEquals(ZONE1.name(), created.name()); - assertNotNull(created.creationTimeMillis()); - assertNotNull(created.nameServers()); - assertNull(created.nameServerSet()); - assertNotNull(created.id()); - Zone retrieved = DNS.getZone(ZONE1.name()); - assertEquals(created, retrieved); - created = DNS.create(ZONE_EMPTY_DESCRIPTION); - assertEquals(ZONE_EMPTY_DESCRIPTION.description(), created.description()); - assertEquals(ZONE_EMPTY_DESCRIPTION.dnsName(), created.dnsName()); - assertEquals(ZONE_EMPTY_DESCRIPTION.name(), created.name()); - assertNotNull(created.creationTimeMillis()); - assertNotNull(created.nameServers()); - assertNull(created.nameServerSet()); - assertNotNull(created.id()); - retrieved = DNS.getZone(ZONE_EMPTY_DESCRIPTION.name()); - assertEquals(created, retrieved); + private static void assertEqChangesIgnoreStatus(ChangeRequest expected, ChangeRequest actual) { + ChangeRequest unifiedEx = ChangeRequest.fromPb(expected.toPb().setStatus("pending")); + ChangeRequest unifiedAct = ChangeRequest.fromPb(actual.toPb().setStatus("pending")); + assertEquals(unifiedEx, unifiedAct); } - @After - public void tearDown() { - purge(); + private static void checkChangeComplete(String zoneName, String changeId) { + while (true) { + ChangeRequest changeRequest = DNS.getChangeRequest(zoneName, changeId, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); + if (ChangeRequest.Status.DONE.equals(changeRequest.status())) { + break; + } + } } + @Rule + public Timeout globalTimeout = Timeout.seconds(300); + @Test - public void testCreateZoneWithErrors() { - try { - DNS.create(ZONE_MISSING_DNS_NAME); - fail("Zone is missing DNS name. The service returns an error."); - } catch (DnsException ex) { - // expected - // todo(mderka) test non-retryable when implemented within #593 - } - try { - DNS.create(ZONE_MISSING_DESCRIPTION); - fail("Zone is missing description name. The service returns an error."); - } catch (DnsException ex) { - // expected - // todo(mderka) test non-retryable when implemented within #593 - } - try { - DNS.create(ZONE_NAME_ERROR); - fail("Zone name is missing a period. The service returns an error."); - } catch (DnsException ex) { - // expected - // todo(mderka) test non-retryable when implemented within #593 - } + public void testCreateValidZone() { try { - DNS.create(ZONE_DNS_NO_PERIOD); - fail("Zone name is missing a period. The service returns an error."); - } catch (DnsException ex) { - // expected - // todo(mderka) test non-retryable when implemented within #593 + Zone created = DNS.create(ZONE1); + assertEquals(ZONE1.description(), created.description()); + assertEquals(ZONE1.dnsName(), created.dnsName()); + assertEquals(ZONE1.name(), created.name()); + assertNotNull(created.creationTimeMillis()); + assertNotNull(created.nameServers()); + assertNull(created.nameServerSet()); + assertNotNull(created.id()); + Zone retrieved = DNS.getZone(ZONE1.name()); + assertEquals(created, retrieved); + created = DNS.create(ZONE_EMPTY_DESCRIPTION); + assertEquals(ZONE_EMPTY_DESCRIPTION.description(), created.description()); + assertEquals(ZONE_EMPTY_DESCRIPTION.dnsName(), created.dnsName()); + assertEquals(ZONE_EMPTY_DESCRIPTION.name(), created.name()); + assertNotNull(created.creationTimeMillis()); + assertNotNull(created.nameServers()); + assertNull(created.nameServerSet()); + assertNotNull(created.id()); + retrieved = DNS.getZone(ZONE_EMPTY_DESCRIPTION.name()); + assertEquals(created, retrieved); + } finally { + DNS.delete(ZONE1.name()); + DNS.delete(ZONE_EMPTY_DESCRIPTION.name()); } } @Test - public void testCreateZoneWithOptions() { - Zone created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.CREATION_TIME)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNotNull(created.creationTimeMillis()); - assertNull(created.description()); - assertNull(created.dnsName()); - assertTrue(created.nameServers().isEmpty()); // never returns null - assertNull(created.nameServerSet()); - assertNull(created.id()); - created.delete(); - created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.DESCRIPTION)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNull(created.creationTimeMillis()); - assertEquals(ZONE1.description(), created.description()); - assertNull(created.dnsName()); - assertTrue(created.nameServers().isEmpty()); // never returns null - assertNull(created.nameServerSet()); - assertNull(created.id()); - created.delete(); - created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.DNS_NAME)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNull(created.creationTimeMillis()); - assertEquals(ZONE1.dnsName(), created.dnsName()); - assertNull(created.description()); - assertTrue(created.nameServers().isEmpty()); // never returns null - assertNull(created.nameServerSet()); - assertNull(created.id()); - created.delete(); - created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNull(created.creationTimeMillis()); - assertNull(created.dnsName()); - assertNull(created.description()); - assertTrue(created.nameServers().isEmpty()); // never returns null - assertNull(created.nameServerSet()); - assertNull(created.id()); - created.delete(); - created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVER_SET)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNull(created.creationTimeMillis()); - assertNull(created.dnsName()); - assertNull(created.description()); - assertTrue(created.nameServers().isEmpty()); // never returns null - assertNull(created.nameServerSet()); // we did not set it - assertNull(created.id()); - created.delete(); - created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVERS)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNull(created.creationTimeMillis()); - assertNull(created.dnsName()); - assertNull(created.description()); - assertFalse(created.nameServers().isEmpty()); - assertNull(created.nameServerSet()); - assertNull(created.id()); - created.delete(); - created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.ZONE_ID)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNull(created.creationTimeMillis()); - assertNull(created.dnsName()); - assertNull(created.description()); - assertNotNull(created.nameServers()); - assertTrue(created.nameServers().isEmpty()); // never returns null - assertNotNull(created.id()); - created.delete(); - // combination of multiple things - created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.ZONE_ID, - Dns.ZoneField.NAME_SERVERS, Dns.ZoneField.NAME_SERVER_SET, Dns.ZoneField.DESCRIPTION)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNull(created.creationTimeMillis()); - assertNull(created.dnsName()); - assertEquals(ZONE1.description(), created.description()); - assertFalse(created.nameServers().isEmpty()); - assertNull(created.nameServerSet()); // we did not set it - assertNotNull(created.id()); + public void testCreateZoneWithErrors() { + try { + try { + DNS.create(ZONE_MISSING_DNS_NAME); + fail("Zone is missing DNS name. The service returns an error."); + } catch (DnsException ex) { + // expected + // todo(mderka) test non-retryable when implemented within #593 + } + try { + DNS.create(ZONE_MISSING_DESCRIPTION); + fail("Zone is missing description name. The service returns an error."); + } catch (DnsException ex) { + // expected + // todo(mderka) test non-retryable when implemented within #593 + } + try { + DNS.create(ZONE_NAME_ERROR); + fail("Zone name is missing a period. The service returns an error."); + } catch (DnsException ex) { + // expected + // todo(mderka) test non-retryable when implemented within #593 + } + try { + DNS.create(ZONE_DNS_NO_PERIOD); + fail("Zone name is missing a period. The service returns an error."); + } catch (DnsException ex) { + // expected + // todo(mderka) test non-retryable when implemented within #593 + } + } finally { + DNS.delete(ZONE_MISSING_DNS_NAME.name()); + DNS.delete(ZONE_MISSING_DESCRIPTION.name()); + DNS.delete(ZONE_NAME_ERROR.name()); + DNS.delete(ZONE_DNS_NO_PERIOD.name()); + } } @Test - public void testZoneReload() { - Zone created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); - created = created.reload(Dns.ZoneOption.fields(Dns.ZoneField.CREATION_TIME)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNotNull(created.creationTimeMillis()); - assertNull(created.description()); - assertNull(created.dnsName()); - assertTrue(created.nameServers().isEmpty()); // never returns null - assertNull(created.nameServerSet()); - assertNull(created.id()); - created = created.reload(Dns.ZoneOption.fields(Dns.ZoneField.DESCRIPTION)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNull(created.creationTimeMillis()); - assertEquals(ZONE1.description(), created.description()); - assertNull(created.dnsName()); - assertTrue(created.nameServers().isEmpty()); // never returns null - assertNull(created.nameServerSet()); - assertNull(created.id()); - created = created.reload(Dns.ZoneOption.fields(Dns.ZoneField.DNS_NAME)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNull(created.creationTimeMillis()); - assertEquals(ZONE1.dnsName(), created.dnsName()); - assertNull(created.description()); - assertTrue(created.nameServers().isEmpty()); // never returns null - assertNull(created.nameServerSet()); - assertNull(created.id()); - created = created.reload(Dns.ZoneOption.fields(Dns.ZoneField.NAME)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNull(created.creationTimeMillis()); - assertNull(created.dnsName()); - assertNull(created.description()); - assertTrue(created.nameServers().isEmpty()); // never returns null - assertNull(created.nameServerSet()); - assertNull(created.id()); - created = created.reload(Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVER_SET)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNull(created.creationTimeMillis()); - assertNull(created.dnsName()); - assertNull(created.description()); - assertTrue(created.nameServers().isEmpty()); // never returns null - assertNull(created.nameServerSet()); // we did not set it - assertNull(created.id()); - created = created.reload(Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVERS)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNull(created.creationTimeMillis()); - assertNull(created.dnsName()); - assertNull(created.description()); - assertFalse(created.nameServers().isEmpty()); - assertNull(created.nameServerSet()); - assertNull(created.id()); - created = created.reload(Dns.ZoneOption.fields(Dns.ZoneField.ZONE_ID)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNull(created.creationTimeMillis()); - assertNull(created.dnsName()); - assertNull(created.description()); - assertNotNull(created.nameServers()); - assertTrue(created.nameServers().isEmpty()); // never returns null - assertNotNull(created.id()); - // combination of multiple things - created = created.reload(Dns.ZoneOption.fields(Dns.ZoneField.ZONE_ID, - Dns.ZoneField.NAME_SERVERS, Dns.ZoneField.NAME_SERVER_SET, Dns.ZoneField.DESCRIPTION)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNull(created.creationTimeMillis()); - assertNull(created.dnsName()); - assertEquals(ZONE1.description(), created.description()); - assertFalse(created.nameServers().isEmpty()); - assertNull(created.nameServerSet()); // we did not set it - assertNotNull(created.id()); + public void testCreateZoneWithOptions() { + try { + Zone created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.CREATION_TIME)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNotNull(created.creationTimeMillis()); + assertNull(created.description()); + assertNull(created.dnsName()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); + assertNull(created.id()); + created.delete(); + created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.DESCRIPTION)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertEquals(ZONE1.description(), created.description()); + assertNull(created.dnsName()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); + assertNull(created.id()); + created.delete(); + created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.DNS_NAME)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertEquals(ZONE1.dnsName(), created.dnsName()); + assertNull(created.description()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); + assertNull(created.id()); + created.delete(); + created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertNull(created.description()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); + assertNull(created.id()); + created.delete(); + created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVER_SET)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertNull(created.description()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); // we did not set it + assertNull(created.id()); + created.delete(); + created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVERS)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertNull(created.description()); + assertFalse(created.nameServers().isEmpty()); + assertNull(created.nameServerSet()); + assertNull(created.id()); + created.delete(); + created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.ZONE_ID)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertNull(created.description()); + assertNotNull(created.nameServers()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNotNull(created.id()); + created.delete(); + // combination of multiple things + created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.ZONE_ID, + Dns.ZoneField.NAME_SERVERS, Dns.ZoneField.NAME_SERVER_SET, Dns.ZoneField.DESCRIPTION)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertEquals(ZONE1.description(), created.description()); + assertFalse(created.nameServers().isEmpty()); + assertNull(created.nameServerSet()); // we did not set it + assertNotNull(created.id()); + } finally { + DNS.delete(ZONE1.name()); + } } @Test public void testGetZone() { - DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); - Zone created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.CREATION_TIME)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNotNull(created.creationTimeMillis()); - assertNull(created.description()); - assertNull(created.dnsName()); - assertTrue(created.nameServers().isEmpty()); // never returns null - assertNull(created.nameServerSet()); - assertNull(created.id()); - created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.DESCRIPTION)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNull(created.creationTimeMillis()); - assertEquals(ZONE1.description(), created.description()); - assertNull(created.dnsName()); - assertTrue(created.nameServers().isEmpty()); // never returns null - assertNull(created.nameServerSet()); - assertNull(created.id()); - created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.DNS_NAME)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNull(created.creationTimeMillis()); - assertEquals(ZONE1.dnsName(), created.dnsName()); - assertNull(created.description()); - assertTrue(created.nameServers().isEmpty()); // never returns null - assertNull(created.nameServerSet()); - assertNull(created.id()); - created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.NAME)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNull(created.creationTimeMillis()); - assertNull(created.dnsName()); - assertNull(created.description()); - assertTrue(created.nameServers().isEmpty()); // never returns null - assertNull(created.nameServerSet()); - assertNull(created.id()); - created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVER_SET)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNull(created.creationTimeMillis()); - assertNull(created.dnsName()); - assertNull(created.description()); - assertTrue(created.nameServers().isEmpty()); // never returns null - assertNull(created.nameServerSet()); // we did not set it - assertNull(created.id()); - created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVERS)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNull(created.creationTimeMillis()); - assertNull(created.dnsName()); - assertNull(created.description()); - assertFalse(created.nameServers().isEmpty()); - assertNull(created.nameServerSet()); - assertNull(created.id()); - created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.ZONE_ID)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNull(created.creationTimeMillis()); - assertNull(created.dnsName()); - assertNull(created.description()); - assertNotNull(created.nameServers()); - assertTrue(created.nameServers().isEmpty()); // never returns null - assertNotNull(created.id()); - // combination of multiple things - created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.ZONE_ID, - Dns.ZoneField.NAME_SERVERS, Dns.ZoneField.NAME_SERVER_SET, Dns.ZoneField.DESCRIPTION)); - assertEquals(ZONE1.name(), created.name()); // always returned - assertNull(created.creationTimeMillis()); - assertNull(created.dnsName()); - assertEquals(ZONE1.description(), created.description()); - assertFalse(created.nameServers().isEmpty()); - assertNull(created.nameServerSet()); // we did not set it - assertNotNull(created.id()); + try { + DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); + Zone created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.CREATION_TIME)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNotNull(created.creationTimeMillis()); + assertNull(created.description()); + assertNull(created.dnsName()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); + assertNull(created.id()); + created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.DESCRIPTION)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertEquals(ZONE1.description(), created.description()); + assertNull(created.dnsName()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); + assertNull(created.id()); + created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.DNS_NAME)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertEquals(ZONE1.dnsName(), created.dnsName()); + assertNull(created.description()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); + assertNull(created.id()); + created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.NAME)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertNull(created.description()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); + assertNull(created.id()); + created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVER_SET)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertNull(created.description()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNull(created.nameServerSet()); // we did not set it + assertNull(created.id()); + created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVERS)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertNull(created.description()); + assertFalse(created.nameServers().isEmpty()); + assertNull(created.nameServerSet()); + assertNull(created.id()); + created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.ZONE_ID)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertNull(created.description()); + assertNotNull(created.nameServers()); + assertTrue(created.nameServers().isEmpty()); // never returns null + assertNotNull(created.id()); + // combination of multiple things + created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.ZONE_ID, + Dns.ZoneField.NAME_SERVERS, Dns.ZoneField.NAME_SERVER_SET, Dns.ZoneField.DESCRIPTION)); + assertEquals(ZONE1.name(), created.name()); // always returned + assertNull(created.creationTimeMillis()); + assertNull(created.dnsName()); + assertEquals(ZONE1.description(), created.description()); + assertFalse(created.nameServers().isEmpty()); + assertNull(created.nameServerSet()); // we did not set it + assertNotNull(created.id()); + } finally { + DNS.delete(ZONE1.name()); + } } @Test public void testListZones() { - List zones = filter(DNS.listZones().iterateAll()); - assertEquals(0, zones.size()); - // some zones exists - Zone created = DNS.create(ZONE1); - zones = filter(DNS.listZones().iterateAll()); - assertEquals(created, zones.get(0)); - assertEquals(1, zones.size()); - created = DNS.create(ZONE_EMPTY_DESCRIPTION); - zones = filter(DNS.listZones().iterateAll()); - assertEquals(2, zones.size()); - assertTrue(zones.contains(created)); - // error in options - try { - DNS.listZones(Dns.ZoneListOption.pageSize(0)); - } catch (DnsException ex) { - // expected - assertEquals(400, ex.code()); - // todo(mderka) test not-retryable - } - try { - DNS.listZones(Dns.ZoneListOption.pageSize(-1)); - } catch (DnsException ex) { - // expected - assertEquals(400, ex.code()); - // todo(mderka) test not-retryable - } - // ok size - zones = filter(DNS.listZones(Dns.ZoneListOption.pageSize(1000)).iterateAll()); - assertEquals(2, zones.size()); // we still have only 2 zones - // dns name problems try { - DNS.listZones(Dns.ZoneListOption.dnsName("aaaaa")); - } catch (DnsException ex) { - // expected - assertEquals(400, ex.code()); - // todo(mderka) test not-retryable - } - // ok name - zones = filter(DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName())).iterateAll()); - assertEquals(1, zones.size()); - // field options - zones = ImmutableList.copyOf(DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), - Dns.ZoneListOption.fields(Dns.ZoneField.ZONE_ID)).iterateAll()); - assertEquals(1, zones.size()); - Zone zone = zones.get(0); - assertNull(zone.creationTimeMillis()); - assertNotNull(zone.name()); - assertNull(zone.dnsName()); - assertNull(zone.description()); - assertNull(zone.nameServerSet()); - assertTrue(zone.nameServers().isEmpty()); - assertNotNull(zone.id()); - zones = ImmutableList.copyOf(DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), - Dns.ZoneListOption.fields(Dns.ZoneField.CREATION_TIME)).iterateAll()); - assertEquals(1, zones.size()); - zone = zones.get(0); - assertNotNull(zone.creationTimeMillis()); - assertNotNull(zone.name()); - assertNull(zone.dnsName()); - assertNull(zone.description()); - assertNull(zone.nameServerSet()); - assertTrue(zone.nameServers().isEmpty()); - assertNull(zone.id()); - zones = ImmutableList.copyOf(DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), - Dns.ZoneListOption.fields(Dns.ZoneField.DNS_NAME)).iterateAll()); - assertEquals(1, zones.size()); - zone = zones.get(0); - assertNull(zone.creationTimeMillis()); - assertNotNull(zone.name()); - assertNotNull(zone.dnsName()); - assertNull(zone.description()); - assertNull(zone.nameServerSet()); - assertTrue(zone.nameServers().isEmpty()); - assertNull(zone.id()); - zones = ImmutableList.copyOf(DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), - Dns.ZoneListOption.fields(Dns.ZoneField.DESCRIPTION)).iterateAll()); - assertEquals(1, zones.size()); - zone = zones.get(0); - assertNull(zone.creationTimeMillis()); - assertNotNull(zone.name()); - assertNull(zone.dnsName()); - assertNotNull(zone.description()); - assertNull(zone.nameServerSet()); - assertTrue(zone.nameServers().isEmpty()); - assertNull(zone.id()); - zones = ImmutableList.copyOf(DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), - Dns.ZoneListOption.fields(Dns.ZoneField.NAME_SERVERS)).iterateAll()); - assertEquals(1, zones.size()); - zone = zones.get(0); - assertNull(zone.creationTimeMillis()); - assertNotNull(zone.name()); - assertNull(zone.dnsName()); - assertNull(zone.description()); - assertNull(zone.nameServerSet()); - assertTrue(!zone.nameServers().isEmpty()); - assertNull(zone.id()); - zones = ImmutableList.copyOf(DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), - Dns.ZoneListOption.fields(Dns.ZoneField.NAME_SERVER_SET)).iterateAll()); - assertEquals(1, zones.size()); - zone = zones.get(0); - assertNull(zone.creationTimeMillis()); - assertNotNull(zone.name()); - assertNull(zone.dnsName()); - assertNull(zone.description()); - assertNull(zone.nameServerSet()); // we cannot set it using gcloud java - assertTrue(zone.nameServers().isEmpty()); - assertNull(zone.id()); - // several combined - zones = filter(DNS.listZones(Dns.ZoneListOption.fields(Dns.ZoneField.ZONE_ID, - Dns.ZoneField.DESCRIPTION), - Dns.ZoneListOption.pageSize(1)).iterateAll()); - assertEquals(2, zones.size()); - for (Zone current : zones) { - assertNull(current.creationTimeMillis()); - assertNotNull(current.name()); - assertNull(current.dnsName()); - assertNotNull(current.description()); - assertNull(current.nameServerSet()); + List zones = filter(DNS.listZones().iterateAll()); + assertEquals(0, zones.size()); + // some zones exists + Zone created = DNS.create(ZONE1); + zones = filter(DNS.listZones().iterateAll()); + assertEquals(created, zones.get(0)); + assertEquals(1, zones.size()); + created = DNS.create(ZONE_EMPTY_DESCRIPTION); + zones = filter(DNS.listZones().iterateAll()); + assertEquals(2, zones.size()); + assertTrue(zones.contains(created)); + // error in options + try { + DNS.listZones(Dns.ZoneListOption.pageSize(0)); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + // todo(mderka) test not-retryable + } + try { + DNS.listZones(Dns.ZoneListOption.pageSize(-1)); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + // todo(mderka) test not-retryable + } + // ok size + zones = filter(DNS.listZones(Dns.ZoneListOption.pageSize(1000)).iterateAll()); + assertEquals(2, zones.size()); // we still have only 2 zones + // dns name problems + try { + DNS.listZones(Dns.ZoneListOption.dnsName("aaaaa")); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + // todo(mderka) test not-retryable + } + // ok name + zones = filter(DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName())).iterateAll()); + assertEquals(1, zones.size()); + // field options + Iterator zoneIterator = DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), + Dns.ZoneListOption.fields(Dns.ZoneField.ZONE_ID)).iterateAll(); + Zone zone = zoneIterator.next(); + assertNull(zone.creationTimeMillis()); + assertNotNull(zone.name()); + assertNull(zone.dnsName()); + assertNull(zone.description()); + assertNull(zone.nameServerSet()); assertTrue(zone.nameServers().isEmpty()); - assertNotNull(current.id()); - } - } - - @Test - public void testDeleteZoneUsingServiceObject() { - Zone created = DNS.create(ZONE1); - assertEquals(created, DNS.getZone(ZONE1.name())); - DNS.delete(ZONE1.name()); - assertNull(DNS.getZone(ZONE1.name())); - } - - @Test - public void testDeleteZoneUsingZoneObject() { - Zone created = DNS.create(ZONE1); - assertEquals(created, DNS.getZone(ZONE1.name())); - created.delete(); - assertNull(DNS.getZone(ZONE1.name())); - } - - private void assertEqChangesIgnoreStatus(ChangeRequest expected, ChangeRequest actual) { - ChangeRequest unifiedEx = ChangeRequest.fromPb(expected.toPb().setStatus("pending")); - ChangeRequest unifiedAct = ChangeRequest.fromPb(actual.toPb().setStatus("pending")); - assertEquals(unifiedEx, unifiedAct); - } - - private static void checkChangeComplete(String zoneName, String changeId) { - for (int i = 0; i < TIME_LIMIT; i++) { - ChangeRequest changeRequest = DNS.getChangeRequest(zoneName, changeId, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); - if (ChangeRequest.Status.DONE.equals(changeRequest.status())) { - break; - } else { - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - throw new RuntimeException("Thread was interrupted while waiting for change."); - } + assertNotNull(zone.id()); + assertFalse(zoneIterator.hasNext()); + zoneIterator = DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), + Dns.ZoneListOption.fields(Dns.ZoneField.CREATION_TIME)).iterateAll(); + zone = zoneIterator.next(); + assertNotNull(zone.creationTimeMillis()); + assertNotNull(zone.name()); + assertNull(zone.dnsName()); + assertNull(zone.description()); + assertNull(zone.nameServerSet()); + assertTrue(zone.nameServers().isEmpty()); + assertNull(zone.id()); + assertFalse(zoneIterator.hasNext()); + zoneIterator = DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), + Dns.ZoneListOption.fields(Dns.ZoneField.DNS_NAME)).iterateAll(); + zone = zoneIterator.next(); + assertNull(zone.creationTimeMillis()); + assertNotNull(zone.name()); + assertNotNull(zone.dnsName()); + assertNull(zone.description()); + assertNull(zone.nameServerSet()); + assertTrue(zone.nameServers().isEmpty()); + assertNull(zone.id()); + assertFalse(zoneIterator.hasNext()); + zoneIterator = DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), + Dns.ZoneListOption.fields(Dns.ZoneField.DESCRIPTION)).iterateAll(); + zone = zoneIterator.next(); + assertNull(zone.creationTimeMillis()); + assertNotNull(zone.name()); + assertNull(zone.dnsName()); + assertNotNull(zone.description()); + assertNull(zone.nameServerSet()); + assertTrue(zone.nameServers().isEmpty()); + assertNull(zone.id()); + assertFalse(zoneIterator.hasNext()); + zoneIterator = DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), + Dns.ZoneListOption.fields(Dns.ZoneField.NAME_SERVERS)).iterateAll(); + zone = zoneIterator.next(); + assertNull(zone.creationTimeMillis()); + assertNotNull(zone.name()); + assertNull(zone.dnsName()); + assertNull(zone.description()); + assertNull(zone.nameServerSet()); + assertTrue(!zone.nameServers().isEmpty()); + assertNull(zone.id()); + assertFalse(zoneIterator.hasNext()); + zoneIterator = DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), + Dns.ZoneListOption.fields(Dns.ZoneField.NAME_SERVER_SET)).iterateAll(); + zone = zoneIterator.next(); + assertNull(zone.creationTimeMillis()); + assertNotNull(zone.name()); + assertNull(zone.dnsName()); + assertNull(zone.description()); + assertNull(zone.nameServerSet()); // we cannot set it using gcloud java + assertTrue(zone.nameServers().isEmpty()); + assertNull(zone.id()); + assertFalse(zoneIterator.hasNext()); + // several combined + zones = filter(DNS.listZones(Dns.ZoneListOption.fields(Dns.ZoneField.ZONE_ID, + Dns.ZoneField.DESCRIPTION), + Dns.ZoneListOption.pageSize(1)).iterateAll()); + assertEquals(2, zones.size()); + for (Zone current : zones) { + assertNull(current.creationTimeMillis()); + assertNotNull(current.name()); + assertNull(current.dnsName()); + assertNotNull(current.description()); + assertNull(current.nameServerSet()); + assertTrue(zone.nameServers().isEmpty()); + assertNotNull(current.id()); } + } finally { + DNS.delete(ZONE1.name()); + DNS.delete(ZONE_EMPTY_DESCRIPTION.name()); } } @Test - public void testCreateChangeUsingServiceObject() { - DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); - ChangeRequest created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); - assertEquals(CHANGE_ADD_ZONE1.additions(), created.additions()); - assertNotNull(created.startTimeMillis()); - assertTrue(created.deletions().isEmpty()); - assertEquals("1", created.id()); - assertTrue(ImmutableList.of(ChangeRequest.Status.PENDING, ChangeRequest.Status.DONE) - .contains(created.status())); - assertEqChangesIgnoreStatus(created, DNS.getChangeRequest(ZONE1.name(), "1")); - checkChangeComplete(ZONE1.name(), "1"); - DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - checkChangeComplete(ZONE1.name(), "2"); - // with options - created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID)); - assertTrue(created.additions().isEmpty()); - assertNull(created.startTimeMillis()); - assertTrue(created.deletions().isEmpty()); - assertEquals("3", created.id()); - assertNull(created.status()); - checkChangeComplete(ZONE1.name(), "3"); - DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - checkChangeComplete(ZONE1.name(), "4"); - created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); - assertTrue(created.additions().isEmpty()); - assertNull(created.startTimeMillis()); - assertTrue(created.deletions().isEmpty()); - assertEquals("5", created.id()); - assertNotNull(created.status()); - checkChangeComplete(ZONE1.name(), "5"); - DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - checkChangeComplete(ZONE1.name(), "6"); - created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); - assertTrue(created.additions().isEmpty()); - assertNotNull(created.startTimeMillis()); - assertTrue(created.deletions().isEmpty()); - assertEquals("7", created.id()); - assertNull(created.status()); - checkChangeComplete(ZONE1.name(), "7"); - DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - checkChangeComplete(ZONE1.name(), "8"); - created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS)); - assertEquals(CHANGE_ADD_ZONE1.additions(), created.additions()); - assertNull(created.startTimeMillis()); - assertTrue(created.deletions().isEmpty()); - assertEquals("9", created.id()); - assertNull(created.status()); - // finishes with delete otherwise we cannot delete the zone - checkChangeComplete(ZONE1.name(), "9"); - created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS)); - checkChangeComplete(ZONE1.name(), "10"); - assertEquals(CHANGE_DELETE_ZONE1.deletions(), created.deletions()); - assertNull(created.startTimeMillis()); - assertTrue(created.additions().isEmpty()); - assertEquals("10", created.id()); - assertNull(created.status()); - checkChangeComplete(ZONE1.name(), "10"); + public void testDeleteZone() { + try { + Zone created = DNS.create(ZONE1); + assertEquals(created, DNS.getZone(ZONE1.name())); + DNS.delete(ZONE1.name()); + assertNull(DNS.getZone(ZONE1.name())); + } finally { + DNS.delete(ZONE1.name()); + } } - @Test - public void testCreateChangeUsingZoneObject() { - Zone zone = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); - ChangeRequest created = zone.applyChangeRequest(CHANGE_ADD_ZONE1); - assertEquals(CHANGE_ADD_ZONE1.additions(), created.additions()); - assertNotNull(created.startTimeMillis()); - assertTrue(created.deletions().isEmpty()); - assertEquals("1", created.id()); - assertTrue(ImmutableList.of(ChangeRequest.Status.PENDING, ChangeRequest.Status.DONE) - .contains(created.status())); - assertEqChangesIgnoreStatus(created, DNS.getChangeRequest(ZONE1.name(), "1")); - checkChangeComplete(ZONE1.name(), "1"); - zone.applyChangeRequest(CHANGE_DELETE_ZONE1); - checkChangeComplete(ZONE1.name(), "2"); - // with options - created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID)); - assertTrue(created.additions().isEmpty()); - assertNull(created.startTimeMillis()); - assertTrue(created.deletions().isEmpty()); - assertEquals("3", created.id()); - assertNull(created.status()); - checkChangeComplete(ZONE1.name(), "3"); - zone.applyChangeRequest(CHANGE_DELETE_ZONE1); - checkChangeComplete(ZONE1.name(), "4"); - created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); - assertTrue(created.additions().isEmpty()); - assertNull(created.startTimeMillis()); - assertTrue(created.deletions().isEmpty()); - assertEquals("5", created.id()); - assertNotNull(created.status()); - checkChangeComplete(ZONE1.name(), "5"); - zone.applyChangeRequest(CHANGE_DELETE_ZONE1); - checkChangeComplete(ZONE1.name(), "6"); - created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); - assertTrue(created.additions().isEmpty()); - assertNotNull(created.startTimeMillis()); - assertTrue(created.deletions().isEmpty()); - assertEquals("7", created.id()); - assertNull(created.status()); - checkChangeComplete(ZONE1.name(), "7"); - zone.applyChangeRequest(CHANGE_DELETE_ZONE1); - checkChangeComplete(ZONE1.name(), "8"); - created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS)); - assertEquals(CHANGE_ADD_ZONE1.additions(), created.additions()); - assertNull(created.startTimeMillis()); - assertTrue(created.deletions().isEmpty()); - assertEquals("9", created.id()); - assertNull(created.status()); - // finishes with delete otherwise we cannot delete the zone - checkChangeComplete(ZONE1.name(), "9"); - created = zone.applyChangeRequest(CHANGE_DELETE_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS)); - checkChangeComplete(ZONE1.name(), "10"); - assertEquals(CHANGE_DELETE_ZONE1.deletions(), created.deletions()); - assertNull(created.startTimeMillis()); - assertTrue(created.additions().isEmpty()); - assertEquals("10", created.id()); - assertNull(created.status()); - checkChangeComplete(ZONE1.name(), "10"); - } @Test - public void testListChangesUsingService() { - // no such zone exists + public void testCreateChange() { try { - DNS.listChangeRequests(ZONE1.name()); - fail(); - } catch (DnsException ex) { - // expected - assertEquals(404, ex.code()); - // todo(mderka) test retry functionality - } - // zone exists but has no changes - DNS.create(ZONE1); - ImmutableList changes = ImmutableList.copyOf( - DNS.listChangeRequests(ZONE1.name()).iterateAll()); - assertEquals(1, changes.size()); // default change creating SOA and NS - // zone has changes - ChangeRequest change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); - checkChangeComplete(ZONE1.name(), change.id()); - change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - checkChangeComplete(ZONE1.name(), change.id()); - change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); - checkChangeComplete(ZONE1.name(), change.id()); - change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - checkChangeComplete(ZONE1.name(), change.id()); - changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name()).iterateAll()); - assertEquals(5, changes.size()); - // error in options - try { - DNS.listChangeRequests(ZONE1.name(), Dns.ChangeRequestListOption.pageSize(0)); - } catch (DnsException ex) { - // expected - assertEquals(400, ex.code()); - // todo(mderka) test retry functionality - } - try { - DNS.listChangeRequests(ZONE1.name(), Dns.ChangeRequestListOption.pageSize(-1)); - } catch (DnsException ex) { - // expected - assertEquals(400, ex.code()); - // todo(mderka) test retry functionality - } - // sorting order - ImmutableList ascending = ImmutableList.copyOf(DNS.listChangeRequests( - ZONE1.name(), - Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING)).iterateAll()); - ImmutableList descending = ImmutableList.copyOf(DNS.listChangeRequests( - ZONE1.name(), - Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.DESCENDING)).iterateAll()); - int size = 5; - assertEquals(size, descending.size()); - assertEquals(size, ascending.size()); - for (int i = 0; i < size; i++) { - assertEquals(descending.get(i), ascending.get(size - i - 1)); + DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); + ChangeRequest created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); + assertEquals(CHANGE_ADD_ZONE1.additions(), created.additions()); + assertNotNull(created.startTimeMillis()); + assertTrue(created.deletions().isEmpty()); + assertEquals("1", created.id()); + assertTrue(ImmutableList.of(ChangeRequest.Status.PENDING, ChangeRequest.Status.DONE) + .contains(created.status())); + assertEqChangesIgnoreStatus(created, DNS.getChangeRequest(ZONE1.name(), "1")); + checkChangeComplete(ZONE1.name(), "1"); + DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + checkChangeComplete(ZONE1.name(), "2"); + // with options + created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID)); + assertTrue(created.additions().isEmpty()); + assertNull(created.startTimeMillis()); + assertTrue(created.deletions().isEmpty()); + assertEquals("3", created.id()); + assertNull(created.status()); + checkChangeComplete(ZONE1.name(), "3"); + DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + checkChangeComplete(ZONE1.name(), "4"); + created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); + assertTrue(created.additions().isEmpty()); + assertNull(created.startTimeMillis()); + assertTrue(created.deletions().isEmpty()); + assertEquals("5", created.id()); + assertNotNull(created.status()); + checkChangeComplete(ZONE1.name(), "5"); + DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + checkChangeComplete(ZONE1.name(), "6"); + created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); + assertTrue(created.additions().isEmpty()); + assertNotNull(created.startTimeMillis()); + assertTrue(created.deletions().isEmpty()); + assertEquals("7", created.id()); + assertNull(created.status()); + checkChangeComplete(ZONE1.name(), "7"); + DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + checkChangeComplete(ZONE1.name(), "8"); + created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS)); + assertEquals(CHANGE_ADD_ZONE1.additions(), created.additions()); + assertNull(created.startTimeMillis()); + assertTrue(created.deletions().isEmpty()); + assertEquals("9", created.id()); + assertNull(created.status()); + // finishes with delete otherwise we cannot delete the zone + checkChangeComplete(ZONE1.name(), "9"); + created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS)); + checkChangeComplete(ZONE1.name(), "10"); + assertEquals(CHANGE_DELETE_ZONE1.deletions(), created.deletions()); + assertNull(created.startTimeMillis()); + assertTrue(created.additions().isEmpty()); + assertEquals("10", created.id()); + assertNull(created.status()); + checkChangeComplete(ZONE1.name(), "10"); + } finally { + clear(); } - // field options - changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name(), - Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), - Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.ADDITIONS)).iterateAll()); - change = changes.get(1); - assertEquals(CHANGE_ADD_ZONE1.additions(), change.additions()); - assertTrue(change.deletions().isEmpty()); - assertEquals("1", change.id()); - assertNull(change.startTimeMillis()); - assertNull(change.status()); - changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name(), - Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), - Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.DELETIONS)).iterateAll()); - change = changes.get(2); - assertTrue(change.additions().isEmpty()); - assertNotNull(change.deletions()); - assertEquals("2", change.id()); - assertNull(change.startTimeMillis()); - assertNull(change.status()); - changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name(), - Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), - Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.ID)).iterateAll()); - change = changes.get(1); - assertTrue(change.additions().isEmpty()); - assertTrue(change.deletions().isEmpty()); - assertEquals("1", change.id()); - assertNull(change.startTimeMillis()); - assertNull(change.status()); - changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name(), - Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), - Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.START_TIME)).iterateAll()); - change = changes.get(1); - assertTrue(change.additions().isEmpty()); - assertTrue(change.deletions().isEmpty()); - assertEquals("1", change.id()); - assertNotNull(change.startTimeMillis()); - assertNull(change.status()); - changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name(), - Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), - Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.STATUS)).iterateAll()); - change = changes.get(1); - assertTrue(change.additions().isEmpty()); - assertTrue(change.deletions().isEmpty()); - assertEquals("1", change.id()); - assertNull(change.startTimeMillis()); - assertEquals(ChangeRequest.Status.DONE, change.status()); } @Test - public void testListChangesUsingZoneObject() { - // zone exists but has no changes - Zone created = DNS.create(ZONE1); - ImmutableList changes = ImmutableList.copyOf( - created.listChangeRequests().iterateAll()); - assertEquals(1, changes.size()); // default change creating SOA and NS - // zone has changes - ChangeRequest change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); - checkChangeComplete(ZONE1.name(), change.id()); - change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - checkChangeComplete(ZONE1.name(), change.id()); - change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); - checkChangeComplete(ZONE1.name(), change.id()); - change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - checkChangeComplete(ZONE1.name(), change.id()); - changes = ImmutableList.copyOf(created.listChangeRequests().iterateAll()); - assertEquals(5, changes.size()); - // error in options - try { - created.listChangeRequests(Dns.ChangeRequestListOption.pageSize(0)); - } catch (DnsException ex) { - // expected - assertEquals(400, ex.code()); - // todo(mderka) test retry functionality - } + public void testListChanges() { try { - created.listChangeRequests(Dns.ChangeRequestListOption.pageSize(-1)); - } catch (DnsException ex) { - // expected - assertEquals(400, ex.code()); - // todo(mderka) test retry functionality - } - // sorting order - ImmutableList ascending = ImmutableList.copyOf(created.listChangeRequests( - Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING)).iterateAll()); - ImmutableList descending = ImmutableList.copyOf(created.listChangeRequests( - Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.DESCENDING)).iterateAll()); - int size = 5; - assertEquals(size, descending.size()); - assertEquals(size, ascending.size()); - for (int i = 0; i < size; i++) { - assertEquals(descending.get(i), ascending.get(size - i - 1)); + // no such zone exists + try { + DNS.listChangeRequests(ZONE1.name()); + fail(); + } catch (DnsException ex) { + // expected + assertEquals(404, ex.code()); + // todo(mderka) test retry functionality + } + // zone exists but has no changes + DNS.create(ZONE1); + ImmutableList changes = ImmutableList.copyOf( + DNS.listChangeRequests(ZONE1.name()).iterateAll()); + assertEquals(1, changes.size()); // default change creating SOA and NS + // zone has changes + ChangeRequest change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); + checkChangeComplete(ZONE1.name(), change.id()); + change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + checkChangeComplete(ZONE1.name(), change.id()); + change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); + checkChangeComplete(ZONE1.name(), change.id()); + change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + checkChangeComplete(ZONE1.name(), change.id()); + changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name()).iterateAll()); + assertEquals(5, changes.size()); + // error in options + try { + DNS.listChangeRequests(ZONE1.name(), Dns.ChangeRequestListOption.pageSize(0)); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + // todo(mderka) test retry functionality + } + try { + DNS.listChangeRequests(ZONE1.name(), Dns.ChangeRequestListOption.pageSize(-1)); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + // todo(mderka) test retry functionality + } + // sorting order + ImmutableList ascending = ImmutableList.copyOf(DNS.listChangeRequests( + ZONE1.name(), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING)).iterateAll()); + ImmutableList descending = ImmutableList.copyOf(DNS.listChangeRequests( + ZONE1.name(), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.DESCENDING)).iterateAll()); + int size = 5; + assertEquals(size, descending.size()); + assertEquals(size, ascending.size()); + for (int i = 0; i < size; i++) { + assertEquals(descending.get(i), ascending.get(size - i - 1)); + } + // field options + changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name(), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), + Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.ADDITIONS)).iterateAll()); + change = changes.get(1); + assertEquals(CHANGE_ADD_ZONE1.additions(), change.additions()); + assertTrue(change.deletions().isEmpty()); + assertEquals("1", change.id()); + assertNull(change.startTimeMillis()); + assertNull(change.status()); + changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name(), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), + Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.DELETIONS)).iterateAll()); + change = changes.get(2); + assertTrue(change.additions().isEmpty()); + assertNotNull(change.deletions()); + assertEquals("2", change.id()); + assertNull(change.startTimeMillis()); + assertNull(change.status()); + changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name(), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), + Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.ID)).iterateAll()); + change = changes.get(1); + assertTrue(change.additions().isEmpty()); + assertTrue(change.deletions().isEmpty()); + assertEquals("1", change.id()); + assertNull(change.startTimeMillis()); + assertNull(change.status()); + changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name(), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), + Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.START_TIME)).iterateAll()); + change = changes.get(1); + assertTrue(change.additions().isEmpty()); + assertTrue(change.deletions().isEmpty()); + assertEquals("1", change.id()); + assertNotNull(change.startTimeMillis()); + assertNull(change.status()); + changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name(), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), + Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.STATUS)).iterateAll()); + change = changes.get(1); + assertTrue(change.additions().isEmpty()); + assertTrue(change.deletions().isEmpty()); + assertEquals("1", change.id()); + assertNull(change.startTimeMillis()); + assertEquals(ChangeRequest.Status.DONE, change.status()); + } finally { + clear(); } - // field options - changes = ImmutableList.copyOf(created.listChangeRequests( - Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), - Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.ADDITIONS)).iterateAll()); - change = changes.get(1); - assertEquals(CHANGE_ADD_ZONE1.additions(), change.additions()); - assertTrue(change.deletions().isEmpty()); - assertEquals("1", change.id()); - assertNull(change.startTimeMillis()); - assertNull(change.status()); - changes = ImmutableList.copyOf(created.listChangeRequests( - Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), - Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.DELETIONS)).iterateAll()); - change = changes.get(2); - assertTrue(change.additions().isEmpty()); - assertNotNull(change.deletions()); - assertEquals("2", change.id()); - assertNull(change.startTimeMillis()); - assertNull(change.status()); - changes = ImmutableList.copyOf(created.listChangeRequests( - Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), - Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.ID)).iterateAll()); - change = changes.get(1); - assertTrue(change.additions().isEmpty()); - assertTrue(change.deletions().isEmpty()); - assertEquals("1", change.id()); - assertNull(change.startTimeMillis()); - assertNull(change.status()); - changes = ImmutableList.copyOf(created.listChangeRequests( - Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), - Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.START_TIME)).iterateAll()); - change = changes.get(1); - assertTrue(change.additions().isEmpty()); - assertTrue(change.deletions().isEmpty()); - assertEquals("1", change.id()); - assertNotNull(change.startTimeMillis()); - assertNull(change.status()); - changes = ImmutableList.copyOf(created.listChangeRequests( - Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), - Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.STATUS)).iterateAll()); - change = changes.get(1); - assertTrue(change.additions().isEmpty()); - assertTrue(change.deletions().isEmpty()); - assertEquals("1", change.id()); - assertNull(change.startTimeMillis()); - assertEquals(ChangeRequest.Status.DONE, change.status()); - } - - @Test - public void testGetChangeUsingZoneObject() { - Zone zone = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); - ChangeRequest created = zone.applyChangeRequest(CHANGE_ADD_ZONE1); - ChangeRequest retrieved = zone.getChangeRequest(created.id()); - assertEqChangesIgnoreStatus(created, retrieved); - checkChangeComplete(ZONE1.name(), "1"); - zone.applyChangeRequest(CHANGE_DELETE_ZONE1); - checkChangeComplete(ZONE1.name(), "2"); - // with options - created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID)); - retrieved = zone.getChangeRequest(created.id(), - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID)); - assertEqChangesIgnoreStatus(created, retrieved); - checkChangeComplete(ZONE1.name(), "3"); - zone.applyChangeRequest(CHANGE_DELETE_ZONE1); - checkChangeComplete(ZONE1.name(), "4"); - created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); - retrieved = zone.getChangeRequest(created.id(), - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); - assertEqChangesIgnoreStatus(created, retrieved); - checkChangeComplete(ZONE1.name(), "5"); - zone.applyChangeRequest(CHANGE_DELETE_ZONE1); - checkChangeComplete(ZONE1.name(), "6"); - created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); - retrieved = zone.getChangeRequest(created.id(), - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); - assertEqChangesIgnoreStatus(created, retrieved); - checkChangeComplete(ZONE1.name(), "7"); - zone.applyChangeRequest(CHANGE_DELETE_ZONE1); - checkChangeComplete(ZONE1.name(), "8"); - created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS)); - retrieved = zone.getChangeRequest(created.id(), - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS)); - assertEqChangesIgnoreStatus(created, retrieved); - // finishes with delete otherwise we cannot delete the zone - checkChangeComplete(ZONE1.name(), "9"); - created = zone.applyChangeRequest(CHANGE_DELETE_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS)); - retrieved = zone.getChangeRequest(created.id(), - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS)); - assertEqChangesIgnoreStatus(created, retrieved); - checkChangeComplete(ZONE1.name(), "10"); } @Test - public void testGetChangeUsingServiceObject() { - Zone zone = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); - ChangeRequest created = zone.applyChangeRequest(CHANGE_ADD_ZONE1); - ChangeRequest retrieved = DNS.getChangeRequest(zone.name(), created.id()); - assertEqChangesIgnoreStatus(created, retrieved); - zone.applyChangeRequest(CHANGE_DELETE_ZONE1); - // with options - created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID)); - retrieved = DNS.getChangeRequest(zone.name(), created.id(), - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID)); - assertEqChangesIgnoreStatus(created, retrieved); - zone.applyChangeRequest(CHANGE_DELETE_ZONE1); - created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); - retrieved = DNS.getChangeRequest(zone.name(), created.id(), - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); - assertEqChangesIgnoreStatus(created, retrieved); - zone.applyChangeRequest(CHANGE_DELETE_ZONE1); - created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); - retrieved = DNS.getChangeRequest(zone.name(), created.id(), - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); - assertEqChangesIgnoreStatus(created, retrieved); - zone.applyChangeRequest(CHANGE_DELETE_ZONE1); - created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS)); - retrieved = DNS.getChangeRequest(zone.name(), created.id(), - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS)); - assertEqChangesIgnoreStatus(created, retrieved); - // finishes with delete otherwise we cannot delete the zone - created = zone.applyChangeRequest(CHANGE_DELETE_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS)); - retrieved = DNS.getChangeRequest(zone.name(), created.id(), - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS)); - assertEqChangesIgnoreStatus(created, retrieved); + public void testGetChange() { + try { + Zone zone = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); + ChangeRequest created = zone.applyChangeRequest(CHANGE_ADD_ZONE1); + ChangeRequest retrieved = DNS.getChangeRequest(zone.name(), created.id()); + assertEqChangesIgnoreStatus(created, retrieved); + zone.applyChangeRequest(CHANGE_DELETE_ZONE1); + // with options + created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID)); + retrieved = DNS.getChangeRequest(zone.name(), created.id(), + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID)); + assertEqChangesIgnoreStatus(created, retrieved); + zone.applyChangeRequest(CHANGE_DELETE_ZONE1); + created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); + retrieved = DNS.getChangeRequest(zone.name(), created.id(), + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); + assertEqChangesIgnoreStatus(created, retrieved); + zone.applyChangeRequest(CHANGE_DELETE_ZONE1); + created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); + retrieved = DNS.getChangeRequest(zone.name(), created.id(), + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); + assertEqChangesIgnoreStatus(created, retrieved); + zone.applyChangeRequest(CHANGE_DELETE_ZONE1); + created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS)); + retrieved = DNS.getChangeRequest(zone.name(), created.id(), + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS)); + assertEqChangesIgnoreStatus(created, retrieved); + // finishes with delete otherwise we cannot delete the zone + created = zone.applyChangeRequest(CHANGE_DELETE_ZONE1, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS)); + retrieved = DNS.getChangeRequest(zone.name(), created.id(), + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS)); + assertEqChangesIgnoreStatus(created, retrieved); + } finally { + clear(); + } } @Test @@ -1027,242 +778,128 @@ public void testGetProject() { } @Test - public void testListDnsRecordsUsingService() { - Zone zone = DNS.create(ZONE1); - ImmutableList dnsRecords = ImmutableList.copyOf( - DNS.listDnsRecords(zone.name()).iterateAll()); - assertEquals(2, dnsRecords.size()); - ImmutableList defaultRecords = - ImmutableList.of(DnsRecord.Type.NS, DnsRecord.Type.SOA); - for (DnsRecord record : dnsRecords) { - assertTrue(defaultRecords.contains(record.type())); - } - // field options - Iterator dnsRecordIterator = DNS.listDnsRecords(zone.name(), - Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TTL)).iterateAll(); - int counter = 0; - while (dnsRecordIterator.hasNext()) { - DnsRecord record = dnsRecordIterator.next(); - assertEquals(dnsRecords.get(counter).ttl(), record.ttl()); - assertEquals(dnsRecords.get(counter).name(), record.name()); - assertEquals(dnsRecords.get(counter).type(), record.type()); - assertTrue(record.records().isEmpty()); - counter++; - } - assertEquals(2, counter); - dnsRecordIterator = DNS.listDnsRecords(zone.name(), - Dns.DnsRecordListOption.fields(Dns.DnsRecordField.NAME)).iterateAll(); - counter = 0; - while (dnsRecordIterator.hasNext()) { - DnsRecord record = dnsRecordIterator.next(); - assertEquals(dnsRecords.get(counter).name(), record.name()); - assertEquals(dnsRecords.get(counter).type(), record.type()); - assertTrue(record.records().isEmpty()); - assertNull(record.ttl()); - counter++; - } - assertEquals(2, counter); - dnsRecordIterator = DNS.listDnsRecords(zone.name(), - Dns.DnsRecordListOption.fields(Dns.DnsRecordField.DNS_RECORDS)).iterateAll(); - counter = 0; - while (dnsRecordIterator.hasNext()) { - DnsRecord record = dnsRecordIterator.next(); - assertEquals(dnsRecords.get(counter).records(), record.records()); - assertEquals(dnsRecords.get(counter).name(), record.name()); - assertEquals(dnsRecords.get(counter).type(), record.type()); - assertNull(record.ttl()); - counter++; - } - assertEquals(2, counter); - dnsRecordIterator = DNS.listDnsRecords(zone.name(), - Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TYPE), - Dns.DnsRecordListOption.pageSize(1)).iterateAll(); // also test paging - counter = 0; - while (dnsRecordIterator.hasNext()) { - DnsRecord record = dnsRecordIterator.next(); - assertEquals(dnsRecords.get(counter).type(), record.type()); - assertEquals(dnsRecords.get(counter).name(), record.name()); - assertTrue(record.records().isEmpty()); - assertNull(record.ttl()); - counter++; - } - assertEquals(2, counter); - // test page size - Page dnsRecordPage = DNS.listDnsRecords(zone.name(), - Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TYPE), - Dns.DnsRecordListOption.pageSize(1)); - assertEquals(1, ImmutableList.copyOf(dnsRecordPage.values().iterator()).size()); - // test name filter - ChangeRequest change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); - checkChangeComplete(ZONE1.name(), change.id()); - dnsRecordIterator = DNS.listDnsRecords(ZONE1.name(), - Dns.DnsRecordListOption.dnsName(A_RECORD_ZONE1.name())).iterateAll(); - counter = 0; - while (dnsRecordIterator.hasNext()) { - DnsRecord record = dnsRecordIterator.next(); - assertTrue(ImmutableList.of(A_RECORD_ZONE1.type(), AAAA_RECORD_ZONE1.type()) - .contains(record.type())); - counter++; - } - assertEquals(2, counter); - // test type filter - checkChangeComplete(ZONE1.name(), change.id()); - dnsRecordIterator = DNS.listDnsRecords(ZONE1.name(), - Dns.DnsRecordListOption.dnsName(A_RECORD_ZONE1.name()), - Dns.DnsRecordListOption.type(A_RECORD_ZONE1.type())) - .iterateAll(); - counter = 0; - while (dnsRecordIterator.hasNext()) { - DnsRecord record = dnsRecordIterator.next(); - assertEquals(A_RECORD_ZONE1, record); - counter++; - } - assertEquals(1, counter); - change = zone.applyChangeRequest(CHANGE_DELETE_ZONE1); - // check wrong arguments - try { - // name is not set - DNS.listDnsRecords(ZONE1.name(), - Dns.DnsRecordListOption.type(A_RECORD_ZONE1.type())); - } catch (DnsException ex) { - // expected - assertEquals(400, ex.code()); - // todo(mderka) test retry functionality when available - } - try { - DNS.listDnsRecords(ZONE1.name(), - Dns.DnsRecordListOption.pageSize(0)); - } catch (DnsException ex) { - // expected - assertEquals(400, ex.code()); - // todo(mderka) test retry functionality when available - } - try { - DNS.listDnsRecords(ZONE1.name(), - Dns.DnsRecordListOption.pageSize(-1)); - } catch (DnsException ex) { - // expected - assertEquals(400, ex.code()); - // todo(mderka) test retry functionality when available - } - checkChangeComplete(ZONE1.name(), change.id()); - } - - @Test - public void testListDnsRecordsUsingZoneObject() { - Zone zone = DNS.create(ZONE1); - ImmutableList dnsRecords = ImmutableList.copyOf( - zone.listDnsRecords().iterateAll()); - assertEquals(2, dnsRecords.size()); - ImmutableList defaultRecords = - ImmutableList.of(DnsRecord.Type.NS, DnsRecord.Type.SOA); - for (DnsRecord record : dnsRecords) { - assertTrue(defaultRecords.contains(record.type())); - } - // field options - Iterator dnsRecordIterator = zone.listDnsRecords( - Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TTL)).iterateAll(); - int counter = 0; - while (dnsRecordIterator.hasNext()) { - DnsRecord record = dnsRecordIterator.next(); - assertEquals(dnsRecords.get(counter).ttl(), record.ttl()); - assertEquals(dnsRecords.get(counter).name(), record.name()); - assertEquals(dnsRecords.get(counter).type(), record.type()); - assertTrue(record.records().isEmpty()); - counter++; - } - assertEquals(2, counter); - dnsRecordIterator = zone.listDnsRecords( - Dns.DnsRecordListOption.fields(Dns.DnsRecordField.NAME)).iterateAll(); - counter = 0; - while (dnsRecordIterator.hasNext()) { - DnsRecord record = dnsRecordIterator.next(); - assertEquals(dnsRecords.get(counter).name(), record.name()); - assertEquals(dnsRecords.get(counter).type(), record.type()); - assertTrue(record.records().isEmpty()); - assertNull(record.ttl()); - counter++; - } - assertEquals(2, counter); - dnsRecordIterator = zone.listDnsRecords( - Dns.DnsRecordListOption.fields(Dns.DnsRecordField.DNS_RECORDS)).iterateAll(); - counter = 0; - while (dnsRecordIterator.hasNext()) { - DnsRecord record = dnsRecordIterator.next(); - assertEquals(dnsRecords.get(counter).records(), record.records()); - assertEquals(dnsRecords.get(counter).name(), record.name()); - assertEquals(dnsRecords.get(counter).type(), record.type()); - assertNull(record.ttl()); - counter++; - } - assertEquals(2, counter); - dnsRecordIterator = zone.listDnsRecords( - Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TYPE), - Dns.DnsRecordListOption.pageSize(1)).iterateAll(); // also test paging - counter = 0; - while (dnsRecordIterator.hasNext()) { - DnsRecord record = dnsRecordIterator.next(); - assertEquals(dnsRecords.get(counter).type(), record.type()); - assertEquals(dnsRecords.get(counter).name(), record.name()); - assertTrue(record.records().isEmpty()); - assertNull(record.ttl()); - counter++; - } - assertEquals(2, counter); - // test page size - Page dnsRecordPage = zone.listDnsRecords( - Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TYPE), - Dns.DnsRecordListOption.pageSize(1)); - assertEquals(1, ImmutableList.copyOf(dnsRecordPage.values().iterator()).size()); - // test name filter - ChangeRequest change = zone.applyChangeRequest(CHANGE_ADD_ZONE1); - checkChangeComplete(ZONE1.name(), change.id()); - dnsRecordIterator = zone.listDnsRecords(Dns.DnsRecordListOption.dnsName(A_RECORD_ZONE1.name())) - .iterateAll(); - counter = 0; - while (dnsRecordIterator.hasNext()) { - DnsRecord record = dnsRecordIterator.next(); - assertTrue(ImmutableList.of(A_RECORD_ZONE1.type(), AAAA_RECORD_ZONE1.type()) - .contains(record.type())); - counter++; - } - assertEquals(2, counter); - // test type filter - checkChangeComplete(ZONE1.name(), change.id()); - dnsRecordIterator = zone.listDnsRecords(Dns.DnsRecordListOption.dnsName(A_RECORD_ZONE1.name()), - Dns.DnsRecordListOption.type(A_RECORD_ZONE1.type())) - .iterateAll(); - counter = 0; - while (dnsRecordIterator.hasNext()) { - DnsRecord record = dnsRecordIterator.next(); - assertEquals(A_RECORD_ZONE1, record); - counter++; - } - assertEquals(1, counter); - change = zone.applyChangeRequest(CHANGE_DELETE_ZONE1); - // check wrong arguments - try { - // name is not set - zone.listDnsRecords(Dns.DnsRecordListOption.type(A_RECORD_ZONE1.type())); - } catch (DnsException ex) { - // expected - assertEquals(400, ex.code()); - // todo(mderka) test retry functionality when available - } + public void testListDnsRecords() { try { - zone.listDnsRecords(Dns.DnsRecordListOption.pageSize(0)); - } catch (DnsException ex) { - // expected - assertEquals(400, ex.code()); - // todo(mderka) test retry functionality when available - } - try { - zone.listDnsRecords(Dns.DnsRecordListOption.pageSize(-1)); - } catch (DnsException ex) { - // expected - assertEquals(400, ex.code()); - // todo(mderka) test retry functionality when available + Zone zone = DNS.create(ZONE1); + ImmutableList dnsRecords = ImmutableList.copyOf( + DNS.listDnsRecords(zone.name()).iterateAll()); + assertEquals(2, dnsRecords.size()); + ImmutableList defaultRecords = + ImmutableList.of(DnsRecord.Type.NS, DnsRecord.Type.SOA); + for (DnsRecord record : dnsRecords) { + assertTrue(defaultRecords.contains(record.type())); + } + // field options + Iterator dnsRecordIterator = DNS.listDnsRecords(zone.name(), + Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TTL)).iterateAll(); + int counter = 0; + while (dnsRecordIterator.hasNext()) { + DnsRecord record = dnsRecordIterator.next(); + assertEquals(dnsRecords.get(counter).ttl(), record.ttl()); + assertEquals(dnsRecords.get(counter).name(), record.name()); + assertEquals(dnsRecords.get(counter).type(), record.type()); + assertTrue(record.records().isEmpty()); + counter++; + } + assertEquals(2, counter); + dnsRecordIterator = DNS.listDnsRecords(zone.name(), + Dns.DnsRecordListOption.fields(Dns.DnsRecordField.NAME)).iterateAll(); + counter = 0; + while (dnsRecordIterator.hasNext()) { + DnsRecord record = dnsRecordIterator.next(); + assertEquals(dnsRecords.get(counter).name(), record.name()); + assertEquals(dnsRecords.get(counter).type(), record.type()); + assertTrue(record.records().isEmpty()); + assertNull(record.ttl()); + counter++; + } + assertEquals(2, counter); + dnsRecordIterator = DNS.listDnsRecords(zone.name(), + Dns.DnsRecordListOption.fields(Dns.DnsRecordField.DNS_RECORDS)).iterateAll(); + counter = 0; + while (dnsRecordIterator.hasNext()) { + DnsRecord record = dnsRecordIterator.next(); + assertEquals(dnsRecords.get(counter).records(), record.records()); + assertEquals(dnsRecords.get(counter).name(), record.name()); + assertEquals(dnsRecords.get(counter).type(), record.type()); + assertNull(record.ttl()); + counter++; + } + assertEquals(2, counter); + dnsRecordIterator = DNS.listDnsRecords(zone.name(), + Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TYPE), + Dns.DnsRecordListOption.pageSize(1)).iterateAll(); // also test paging + counter = 0; + while (dnsRecordIterator.hasNext()) { + DnsRecord record = dnsRecordIterator.next(); + assertEquals(dnsRecords.get(counter).type(), record.type()); + assertEquals(dnsRecords.get(counter).name(), record.name()); + assertTrue(record.records().isEmpty()); + assertNull(record.ttl()); + counter++; + } + assertEquals(2, counter); + // test page size + Page dnsRecordPage = DNS.listDnsRecords(zone.name(), + Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TYPE), + Dns.DnsRecordListOption.pageSize(1)); + assertEquals(1, ImmutableList.copyOf(dnsRecordPage.values().iterator()).size()); + // test name filter + ChangeRequest change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); + checkChangeComplete(ZONE1.name(), change.id()); + dnsRecordIterator = DNS.listDnsRecords(ZONE1.name(), + Dns.DnsRecordListOption.dnsName(A_RECORD_ZONE1.name())).iterateAll(); + counter = 0; + while (dnsRecordIterator.hasNext()) { + DnsRecord record = dnsRecordIterator.next(); + assertTrue(ImmutableList.of(A_RECORD_ZONE1.type(), AAAA_RECORD_ZONE1.type()) + .contains(record.type())); + counter++; + } + assertEquals(2, counter); + // test type filter + checkChangeComplete(ZONE1.name(), change.id()); + dnsRecordIterator = DNS.listDnsRecords(ZONE1.name(), + Dns.DnsRecordListOption.dnsName(A_RECORD_ZONE1.name()), + Dns.DnsRecordListOption.type(A_RECORD_ZONE1.type())) + .iterateAll(); + counter = 0; + while (dnsRecordIterator.hasNext()) { + DnsRecord record = dnsRecordIterator.next(); + assertEquals(A_RECORD_ZONE1, record); + counter++; + } + assertEquals(1, counter); + change = zone.applyChangeRequest(CHANGE_DELETE_ZONE1); + // check wrong arguments + try { + // name is not set + DNS.listDnsRecords(ZONE1.name(), + Dns.DnsRecordListOption.type(A_RECORD_ZONE1.type())); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + // todo(mderka) test retry functionality when available + } + try { + DNS.listDnsRecords(ZONE1.name(), + Dns.DnsRecordListOption.pageSize(0)); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + // todo(mderka) test retry functionality when available + } + try { + DNS.listDnsRecords(ZONE1.name(), + Dns.DnsRecordListOption.pageSize(-1)); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + // todo(mderka) test retry functionality when available + } + checkChangeComplete(ZONE1.name(), change.id()); + } finally { + clear(); } - checkChangeComplete(ZONE1.name(), change.id()); } } From 22153aa90264ef13758435b541ec6ee8f17cd620 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Tue, 1 Mar 2016 10:17:19 -0800 Subject: [PATCH 3/3] Added sleep and renamed change completion check. Moved integration test to a separate package and adjusted accordingly. Added missing fails. --- .../google/gcloud/dns/{ => it}/ITDnsTest.java | 125 +++++++++--------- 1 file changed, 64 insertions(+), 61 deletions(-) rename gcloud-java-dns/src/test/java/com/google/gcloud/dns/{ => it}/ITDnsTest.java (92%) diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ITDnsTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java similarity index 92% rename from gcloud-java-dns/src/test/java/com/google/gcloud/dns/ITDnsTest.java rename to gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java index e473d1c4912c..ae721e742ae8 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ITDnsTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.google.gcloud.dns; +package com.google.gcloud.dns.it; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -25,6 +25,14 @@ import com.google.common.collect.ImmutableList; import com.google.gcloud.Page; +import com.google.gcloud.dns.ChangeRequest; +import com.google.gcloud.dns.Dns; +import com.google.gcloud.dns.DnsException; +import com.google.gcloud.dns.DnsOptions; +import com.google.gcloud.dns.DnsRecord; +import com.google.gcloud.dns.ProjectInfo; +import com.google.gcloud.dns.Zone; +import com.google.gcloud.dns.ZoneInfo; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -62,24 +70,20 @@ public class ITDnsTest { .description(ZONE_DESCRIPTION1) .dnsName(ZONE_DNS_NAME1) .build(); - public static final ZoneInfo ZONE_NAME_ERROR = - ZoneInfo.builder(ZONE_NAME_TOO_LONG) - .description(ZONE_DESCRIPTION1) - .dnsName(ZONE_DNS_NAME1) - .build(); - public static final ZoneInfo ZONE_MISSING_DESCRIPTION = - ZoneInfo.builder(ZONE_NAME1) - .dnsName(ZONE_DNS_NAME1) - .build(); - public static final ZoneInfo ZONE_MISSING_DNS_NAME = - ZoneInfo.builder(ZONE_NAME1) - .description(ZONE_DESCRIPTION1) - .build(); - public static final ZoneInfo ZONE_DNS_NO_PERIOD = - ZoneInfo.builder(ZONE_NAME1) - .description(ZONE_DESCRIPTION1) - .dnsName(ZONE_DNS_NAME_NO_PERIOD) - .build(); + public static final ZoneInfo ZONE_NAME_ERROR = ZoneInfo.builder(ZONE_NAME_TOO_LONG) + .description(ZONE_DESCRIPTION1) + .dnsName(ZONE_DNS_NAME1) + .build(); + public static final ZoneInfo ZONE_MISSING_DESCRIPTION = ZoneInfo.builder(ZONE_NAME1) + .dnsName(ZONE_DNS_NAME1) + .build(); + public static final ZoneInfo ZONE_MISSING_DNS_NAME = ZoneInfo.builder(ZONE_NAME1) + .description(ZONE_DESCRIPTION1) + .build(); + public static final ZoneInfo ZONE_DNS_NO_PERIOD = ZoneInfo.builder(ZONE_NAME1) + .description(ZONE_DESCRIPTION1) + .dnsName(ZONE_DNS_NAME_NO_PERIOD) + .build(); public static final DnsRecord A_RECORD_ZONE1 = DnsRecord.builder("www." + ZONE1.dnsName(), DnsRecord.Type.A) .records(ImmutableList.of("123.123.55.1")) @@ -116,7 +120,7 @@ public static void clear() { if (!toDelete.isEmpty()) { ChangeRequest deletion = zone.applyChangeRequest(ChangeRequest.builder().deletions(toDelete).build()); - checkChangeComplete(zone.name(), deletion.id()); + waitUntilComplete(zone.name(), deletion.id()); } zone.delete(); } @@ -145,17 +149,23 @@ public static void after() { } private static void assertEqChangesIgnoreStatus(ChangeRequest expected, ChangeRequest actual) { - ChangeRequest unifiedEx = ChangeRequest.fromPb(expected.toPb().setStatus("pending")); - ChangeRequest unifiedAct = ChangeRequest.fromPb(actual.toPb().setStatus("pending")); - assertEquals(unifiedEx, unifiedAct); + assertEquals(expected.additions(), actual.additions()); + assertEquals(expected.deletions(), actual.deletions()); + assertEquals(expected.id(), actual.id()); + assertEquals(expected.startTimeMillis(), actual.startTimeMillis()); } - private static void checkChangeComplete(String zoneName, String changeId) { + private static void waitUntilComplete(String zoneName, String changeId) { while (true) { ChangeRequest changeRequest = DNS.getChangeRequest(zoneName, changeId, Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); if (ChangeRequest.Status.DONE.equals(changeRequest.status())) { - break; + return; + } + try { + Thread.sleep(500); + } catch (InterruptedException e) { + fail("Thread was interrupted while waiting for change processing."); } } } @@ -404,6 +414,7 @@ public void testListZones() { // error in options try { DNS.listZones(Dns.ZoneListOption.pageSize(0)); + fail(); } catch (DnsException ex) { // expected assertEquals(400, ex.code()); @@ -411,6 +422,7 @@ public void testListZones() { } try { DNS.listZones(Dns.ZoneListOption.pageSize(-1)); + fail(); } catch (DnsException ex) { // expected assertEquals(400, ex.code()); @@ -422,6 +434,7 @@ public void testListZones() { // dns name problems try { DNS.listZones(Dns.ZoneListOption.dnsName("aaaaa")); + fail(); } catch (DnsException ex) { // expected assertEquals(400, ex.code()); @@ -529,7 +542,6 @@ public void testDeleteZone() { } } - @Test public void testCreateChange() { try { @@ -542,9 +554,9 @@ public void testCreateChange() { assertTrue(ImmutableList.of(ChangeRequest.Status.PENDING, ChangeRequest.Status.DONE) .contains(created.status())); assertEqChangesIgnoreStatus(created, DNS.getChangeRequest(ZONE1.name(), "1")); - checkChangeComplete(ZONE1.name(), "1"); + waitUntilComplete(ZONE1.name(), "1"); DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - checkChangeComplete(ZONE1.name(), "2"); + waitUntilComplete(ZONE1.name(), "2"); // with options created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID)); @@ -553,9 +565,9 @@ public void testCreateChange() { assertTrue(created.deletions().isEmpty()); assertEquals("3", created.id()); assertNull(created.status()); - checkChangeComplete(ZONE1.name(), "3"); + waitUntilComplete(ZONE1.name(), "3"); DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - checkChangeComplete(ZONE1.name(), "4"); + waitUntilComplete(ZONE1.name(), "4"); created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); assertTrue(created.additions().isEmpty()); @@ -563,9 +575,9 @@ public void testCreateChange() { assertTrue(created.deletions().isEmpty()); assertEquals("5", created.id()); assertNotNull(created.status()); - checkChangeComplete(ZONE1.name(), "5"); + waitUntilComplete(ZONE1.name(), "5"); DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - checkChangeComplete(ZONE1.name(), "6"); + waitUntilComplete(ZONE1.name(), "6"); created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); assertTrue(created.additions().isEmpty()); @@ -573,9 +585,9 @@ public void testCreateChange() { assertTrue(created.deletions().isEmpty()); assertEquals("7", created.id()); assertNull(created.status()); - checkChangeComplete(ZONE1.name(), "7"); + waitUntilComplete(ZONE1.name(), "7"); DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - checkChangeComplete(ZONE1.name(), "8"); + waitUntilComplete(ZONE1.name(), "8"); created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS)); assertEquals(CHANGE_ADD_ZONE1.additions(), created.additions()); @@ -584,16 +596,16 @@ public void testCreateChange() { assertEquals("9", created.id()); assertNull(created.status()); // finishes with delete otherwise we cannot delete the zone - checkChangeComplete(ZONE1.name(), "9"); + waitUntilComplete(ZONE1.name(), "9"); created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1, Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS)); - checkChangeComplete(ZONE1.name(), "10"); + waitUntilComplete(ZONE1.name(), "10"); assertEquals(CHANGE_DELETE_ZONE1.deletions(), created.deletions()); assertNull(created.startTimeMillis()); assertTrue(created.additions().isEmpty()); assertEquals("10", created.id()); assertNull(created.status()); - checkChangeComplete(ZONE1.name(), "10"); + waitUntilComplete(ZONE1.name(), "10"); } finally { clear(); } @@ -618,18 +630,19 @@ public void testListChanges() { assertEquals(1, changes.size()); // default change creating SOA and NS // zone has changes ChangeRequest change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); - checkChangeComplete(ZONE1.name(), change.id()); + waitUntilComplete(ZONE1.name(), change.id()); change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - checkChangeComplete(ZONE1.name(), change.id()); + waitUntilComplete(ZONE1.name(), change.id()); change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); - checkChangeComplete(ZONE1.name(), change.id()); + waitUntilComplete(ZONE1.name(), change.id()); change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - checkChangeComplete(ZONE1.name(), change.id()); + waitUntilComplete(ZONE1.name(), change.id()); changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name()).iterateAll()); assertEquals(5, changes.size()); // error in options try { DNS.listChangeRequests(ZONE1.name(), Dns.ChangeRequestListOption.pageSize(0)); + fail(); } catch (DnsException ex) { // expected assertEquals(400, ex.code()); @@ -637,6 +650,7 @@ public void testListChanges() { } try { DNS.listChangeRequests(ZONE1.name(), Dns.ChangeRequestListOption.pageSize(-1)); + fail(); } catch (DnsException ex) { // expected assertEquals(400, ex.code()); @@ -754,27 +768,16 @@ public void testGetProject() { // fetches all fields ProjectInfo project = DNS.getProject(); assertNotNull(project.quota()); - assertNotNull(project.number()); - assertNotNull(project.id()); - assertEquals(PROJECT_ID, project.id()); // options project = DNS.getProject(Dns.ProjectOption.fields(Dns.ProjectField.QUOTA)); assertNotNull(project.quota()); - assertNull(project.number()); - assertNotNull(project.id()); // id is always returned project = DNS.getProject(Dns.ProjectOption.fields(Dns.ProjectField.PROJECT_ID)); assertNull(project.quota()); - assertNull(project.number()); - assertNotNull(project.id()); project = DNS.getProject(Dns.ProjectOption.fields(Dns.ProjectField.PROJECT_NUMBER)); assertNull(project.quota()); - assertNotNull(project.number()); - assertNotNull(project.id()); project = DNS.getProject(Dns.ProjectOption.fields(Dns.ProjectField.PROJECT_NUMBER, Dns.ProjectField.QUOTA, Dns.ProjectField.PROJECT_ID)); assertNotNull(project.quota()); - assertNotNull(project.number()); - assertNotNull(project.id()); } @Test @@ -846,7 +849,7 @@ public void testListDnsRecords() { assertEquals(1, ImmutableList.copyOf(dnsRecordPage.values().iterator()).size()); // test name filter ChangeRequest change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); - checkChangeComplete(ZONE1.name(), change.id()); + waitUntilComplete(ZONE1.name(), change.id()); dnsRecordIterator = DNS.listDnsRecords(ZONE1.name(), Dns.DnsRecordListOption.dnsName(A_RECORD_ZONE1.name())).iterateAll(); counter = 0; @@ -858,7 +861,7 @@ public void testListDnsRecords() { } assertEquals(2, counter); // test type filter - checkChangeComplete(ZONE1.name(), change.id()); + waitUntilComplete(ZONE1.name(), change.id()); dnsRecordIterator = DNS.listDnsRecords(ZONE1.name(), Dns.DnsRecordListOption.dnsName(A_RECORD_ZONE1.name()), Dns.DnsRecordListOption.type(A_RECORD_ZONE1.type())) @@ -874,30 +877,30 @@ public void testListDnsRecords() { // check wrong arguments try { // name is not set - DNS.listDnsRecords(ZONE1.name(), - Dns.DnsRecordListOption.type(A_RECORD_ZONE1.type())); + DNS.listDnsRecords(ZONE1.name(), Dns.DnsRecordListOption.type(A_RECORD_ZONE1.type())); + fail(); } catch (DnsException ex) { // expected assertEquals(400, ex.code()); // todo(mderka) test retry functionality when available } try { - DNS.listDnsRecords(ZONE1.name(), - Dns.DnsRecordListOption.pageSize(0)); + DNS.listDnsRecords(ZONE1.name(), Dns.DnsRecordListOption.pageSize(0)); + fail(); } catch (DnsException ex) { // expected assertEquals(400, ex.code()); // todo(mderka) test retry functionality when available } try { - DNS.listDnsRecords(ZONE1.name(), - Dns.DnsRecordListOption.pageSize(-1)); + DNS.listDnsRecords(ZONE1.name(), Dns.DnsRecordListOption.pageSize(-1)); + fail(); } catch (DnsException ex) { // expected assertEquals(400, ex.code()); // todo(mderka) test retry functionality when available } - checkChangeComplete(ZONE1.name(), change.id()); + waitUntilComplete(ZONE1.name(), change.id()); } finally { clear(); }