From 618b677b53362c481ae11fbe2cc41d8e16697b94 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Thu, 14 Apr 2016 20:32:10 -0400 Subject: [PATCH] Integration test for batch. Fixes #874. (#919) * Integration test for batch. Fixes #874. * Adjusted batch calls in integration tests. * Removed unnecessary copying. * Removed unused import. * Replaced change IDs equals tests with not null. --- .../com/google/cloud/dns/it/ITDnsTest.java | 1064 +++++++++++++++-- 1 file changed, 977 insertions(+), 87 deletions(-) diff --git a/gcloud-java-dns/src/test/java/com/google/cloud/dns/it/ITDnsTest.java b/gcloud-java-dns/src/test/java/com/google/cloud/dns/it/ITDnsTest.java index c511ef3672e2..5a27af8e28ae 100644 --- a/gcloud-java-dns/src/test/java/com/google/cloud/dns/it/ITDnsTest.java +++ b/gcloud-java-dns/src/test/java/com/google/cloud/dns/it/ITDnsTest.java @@ -27,6 +27,10 @@ import com.google.cloud.dns.ChangeRequest; import com.google.cloud.dns.ChangeRequestInfo; import com.google.cloud.dns.Dns; +import com.google.cloud.dns.Dns.ChangeRequestField; +import com.google.cloud.dns.Dns.ProjectField; +import com.google.cloud.dns.Dns.RecordSetField; +import com.google.cloud.dns.Dns.ZoneField; import com.google.cloud.dns.DnsBatch; import com.google.cloud.dns.DnsBatchResult; import com.google.cloud.dns.DnsException; @@ -36,7 +40,7 @@ import com.google.cloud.dns.Zone; import com.google.cloud.dns.ZoneInfo; import com.google.common.collect.ImmutableList; -import com.google.common.collect.Iterators; +import com.google.common.collect.Iterables; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -109,8 +113,7 @@ private static void clear() { List toDelete = new LinkedList<>(); while (recordSetIterator.hasNext()) { RecordSet recordSet = recordSetIterator.next(); - if (!ImmutableList.of(RecordSet.Type.NS, RecordSet.Type.SOA) - .contains(recordSet.type())) { + if (!ImmutableList.of(RecordSet.Type.NS, RecordSet.Type.SOA).contains(recordSet.type())) { toDelete.add(recordSet); } } @@ -154,7 +157,7 @@ private static void assertEqChangesIgnoreStatus(ChangeRequest expected, ChangeRe private static void waitForChangeToComplete(String zoneName, String changeId) { ChangeRequest changeRequest = DNS.getChangeRequest(zoneName, changeId, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); + Dns.ChangeRequestOption.fields(ChangeRequestField.STATUS)); waitForChangeToComplete(changeRequest); } @@ -223,7 +226,7 @@ public void testCreateZoneWithErrors() { @Test public void testCreateZoneWithOptions() { try { - Zone created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.CREATION_TIME)); + Zone created = DNS.create(ZONE1, Dns.ZoneOption.fields(ZoneField.CREATION_TIME)); assertEquals(ZONE1.name(), created.name()); // always returned assertNotNull(created.creationTimeMillis()); assertNull(created.description()); @@ -232,7 +235,7 @@ public void testCreateZoneWithOptions() { assertNull(created.nameServerSet()); assertNull(created.generatedId()); created.delete(); - created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.DESCRIPTION)); + created = DNS.create(ZONE1, Dns.ZoneOption.fields(ZoneField.DESCRIPTION)); assertEquals(ZONE1.name(), created.name()); // always returned assertNull(created.creationTimeMillis()); assertEquals(ZONE1.description(), created.description()); @@ -241,7 +244,7 @@ public void testCreateZoneWithOptions() { assertNull(created.nameServerSet()); assertNull(created.generatedId()); created.delete(); - created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.DNS_NAME)); + created = DNS.create(ZONE1, Dns.ZoneOption.fields(ZoneField.DNS_NAME)); assertEquals(ZONE1.name(), created.name()); // always returned assertNull(created.creationTimeMillis()); assertEquals(ZONE1.dnsName(), created.dnsName()); @@ -250,7 +253,7 @@ public void testCreateZoneWithOptions() { assertNull(created.nameServerSet()); assertNull(created.generatedId()); created.delete(); - created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); + created = DNS.create(ZONE1, Dns.ZoneOption.fields(ZoneField.NAME)); assertEquals(ZONE1.name(), created.name()); // always returned assertNull(created.creationTimeMillis()); assertNull(created.dnsName()); @@ -259,7 +262,7 @@ public void testCreateZoneWithOptions() { assertNull(created.nameServerSet()); assertNull(created.generatedId()); created.delete(); - created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVER_SET)); + created = DNS.create(ZONE1, Dns.ZoneOption.fields(ZoneField.NAME_SERVER_SET)); assertEquals(ZONE1.name(), created.name()); // always returned assertNull(created.creationTimeMillis()); assertNull(created.dnsName()); @@ -268,7 +271,7 @@ public void testCreateZoneWithOptions() { assertNull(created.nameServerSet()); // we did not set it assertNull(created.generatedId()); created.delete(); - created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVERS)); + created = DNS.create(ZONE1, Dns.ZoneOption.fields(ZoneField.NAME_SERVERS)); assertEquals(ZONE1.name(), created.name()); // always returned assertNull(created.creationTimeMillis()); assertNull(created.dnsName()); @@ -277,7 +280,7 @@ public void testCreateZoneWithOptions() { assertNull(created.nameServerSet()); assertNull(created.generatedId()); created.delete(); - created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.ZONE_ID)); + created = DNS.create(ZONE1, Dns.ZoneOption.fields(ZoneField.ZONE_ID)); assertEquals(ZONE1.name(), created.name()); // always returned assertNull(created.creationTimeMillis()); assertNull(created.dnsName()); @@ -287,8 +290,8 @@ public void testCreateZoneWithOptions() { assertNotNull(created.generatedId()); 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)); + created = DNS.create(ZONE1, Dns.ZoneOption.fields(ZoneField.ZONE_ID, + ZoneField.NAME_SERVERS, ZoneField.NAME_SERVER_SET, ZoneField.DESCRIPTION)); assertEquals(ZONE1.name(), created.name()); // always returned assertNull(created.creationTimeMillis()); assertNull(created.dnsName()); @@ -304,8 +307,8 @@ public void testCreateZoneWithOptions() { @Test public void testGetZone() { try { - DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); - Zone created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.CREATION_TIME)); + DNS.create(ZONE1, Dns.ZoneOption.fields(ZoneField.NAME)); + Zone created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(ZoneField.CREATION_TIME)); assertEquals(ZONE1.name(), created.name()); // always returned assertNotNull(created.creationTimeMillis()); assertNull(created.description()); @@ -313,7 +316,7 @@ public void testGetZone() { assertTrue(created.nameServers().isEmpty()); // never returns null assertNull(created.nameServerSet()); assertNull(created.generatedId()); - created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.DESCRIPTION)); + created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(ZoneField.DESCRIPTION)); assertEquals(ZONE1.name(), created.name()); // always returned assertNull(created.creationTimeMillis()); assertEquals(ZONE1.description(), created.description()); @@ -321,7 +324,7 @@ public void testGetZone() { assertTrue(created.nameServers().isEmpty()); // never returns null assertNull(created.nameServerSet()); assertNull(created.generatedId()); - created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.DNS_NAME)); + created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(ZoneField.DNS_NAME)); assertEquals(ZONE1.name(), created.name()); // always returned assertNull(created.creationTimeMillis()); assertEquals(ZONE1.dnsName(), created.dnsName()); @@ -329,7 +332,7 @@ public void testGetZone() { assertTrue(created.nameServers().isEmpty()); // never returns null assertNull(created.nameServerSet()); assertNull(created.generatedId()); - created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.NAME)); + created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(ZoneField.NAME)); assertEquals(ZONE1.name(), created.name()); // always returned assertNull(created.creationTimeMillis()); assertNull(created.dnsName()); @@ -337,7 +340,7 @@ public void testGetZone() { assertTrue(created.nameServers().isEmpty()); // never returns null assertNull(created.nameServerSet()); assertNull(created.generatedId()); - created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVER_SET)); + created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(ZoneField.NAME_SERVER_SET)); assertEquals(ZONE1.name(), created.name()); // always returned assertNull(created.creationTimeMillis()); assertNull(created.dnsName()); @@ -345,7 +348,7 @@ public void testGetZone() { assertTrue(created.nameServers().isEmpty()); // never returns null assertNull(created.nameServerSet()); // we did not set it assertNull(created.generatedId()); - created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVERS)); + created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(ZoneField.NAME_SERVERS)); assertEquals(ZONE1.name(), created.name()); // always returned assertNull(created.creationTimeMillis()); assertNull(created.dnsName()); @@ -353,7 +356,7 @@ public void testGetZone() { assertFalse(created.nameServers().isEmpty()); assertNull(created.nameServerSet()); assertNull(created.generatedId()); - created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.ZONE_ID)); + created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(ZoneField.ZONE_ID)); assertEquals(ZONE1.name(), created.name()); // always returned assertNull(created.creationTimeMillis()); assertNull(created.dnsName()); @@ -362,8 +365,8 @@ public void testGetZone() { assertTrue(created.nameServers().isEmpty()); // never returns null assertNotNull(created.generatedId()); // 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)); + created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(ZoneField.ZONE_ID, + ZoneField.NAME_SERVERS, ZoneField.NAME_SERVER_SET, ZoneField.DESCRIPTION)); assertEquals(ZONE1.name(), created.name()); // always returned assertNull(created.creationTimeMillis()); assertNull(created.dnsName()); @@ -424,7 +427,7 @@ public void testListZones() { assertEquals(1, zones.size()); // field options Iterator zoneIterator = DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), - Dns.ZoneListOption.fields(Dns.ZoneField.ZONE_ID)).iterateAll(); + Dns.ZoneListOption.fields(ZoneField.ZONE_ID)).iterateAll(); Zone zone = zoneIterator.next(); assertNull(zone.creationTimeMillis()); assertNotNull(zone.name()); @@ -435,7 +438,7 @@ public void testListZones() { assertNotNull(zone.generatedId()); assertFalse(zoneIterator.hasNext()); zoneIterator = DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), - Dns.ZoneListOption.fields(Dns.ZoneField.CREATION_TIME)).iterateAll(); + Dns.ZoneListOption.fields(ZoneField.CREATION_TIME)).iterateAll(); zone = zoneIterator.next(); assertNotNull(zone.creationTimeMillis()); assertNotNull(zone.name()); @@ -446,7 +449,7 @@ public void testListZones() { assertNull(zone.generatedId()); assertFalse(zoneIterator.hasNext()); zoneIterator = DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), - Dns.ZoneListOption.fields(Dns.ZoneField.DNS_NAME)).iterateAll(); + Dns.ZoneListOption.fields(ZoneField.DNS_NAME)).iterateAll(); zone = zoneIterator.next(); assertNull(zone.creationTimeMillis()); assertNotNull(zone.name()); @@ -457,7 +460,7 @@ public void testListZones() { assertNull(zone.generatedId()); assertFalse(zoneIterator.hasNext()); zoneIterator = DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), - Dns.ZoneListOption.fields(Dns.ZoneField.DESCRIPTION)).iterateAll(); + Dns.ZoneListOption.fields(ZoneField.DESCRIPTION)).iterateAll(); zone = zoneIterator.next(); assertNull(zone.creationTimeMillis()); assertNotNull(zone.name()); @@ -468,7 +471,7 @@ public void testListZones() { assertNull(zone.generatedId()); assertFalse(zoneIterator.hasNext()); zoneIterator = DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), - Dns.ZoneListOption.fields(Dns.ZoneField.NAME_SERVERS)).iterateAll(); + Dns.ZoneListOption.fields(ZoneField.NAME_SERVERS)).iterateAll(); zone = zoneIterator.next(); assertNull(zone.creationTimeMillis()); assertNotNull(zone.name()); @@ -479,7 +482,7 @@ public void testListZones() { assertNull(zone.generatedId()); assertFalse(zoneIterator.hasNext()); zoneIterator = DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), - Dns.ZoneListOption.fields(Dns.ZoneField.NAME_SERVER_SET)).iterateAll(); + Dns.ZoneListOption.fields(ZoneField.NAME_SERVER_SET)).iterateAll(); zone = zoneIterator.next(); assertNull(zone.creationTimeMillis()); assertNotNull(zone.name()); @@ -490,8 +493,8 @@ public void testListZones() { assertNull(zone.generatedId()); assertFalse(zoneIterator.hasNext()); // several combined - zones = filter(DNS.listZones(Dns.ZoneListOption.fields(Dns.ZoneField.ZONE_ID, - Dns.ZoneField.DESCRIPTION), + zones = filter(DNS.listZones(Dns.ZoneListOption.fields(ZoneField.ZONE_ID, + ZoneField.DESCRIPTION), Dns.ZoneListOption.pageSize(1)).iterateAll()); assertEquals(2, zones.size()); for (Zone current : zones) { @@ -524,12 +527,12 @@ public void testDeleteZone() { @Test public void testCreateChange() { try { - DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); + DNS.create(ZONE1, Dns.ZoneOption.fields(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.generatedId()); + assertNotNull(created.generatedId()); assertTrue(ImmutableList.of(ChangeRequest.Status.PENDING, ChangeRequest.Status.DONE) .contains(created.status())); assertEqChangesIgnoreStatus(created, DNS.getChangeRequest(ZONE1.name(), "1")); @@ -538,51 +541,51 @@ public void testCreateChange() { waitForChangeToComplete(created); // with options created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID)); + Dns.ChangeRequestOption.fields(ChangeRequestField.ID)); assertTrue(created.additions().isEmpty()); assertNull(created.startTimeMillis()); assertTrue(created.deletions().isEmpty()); - assertEquals("3", created.generatedId()); + assertNotNull(created.generatedId()); assertNull(created.status()); waitForChangeToComplete(created); created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); waitForChangeToComplete(created); created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); + Dns.ChangeRequestOption.fields(ChangeRequestField.STATUS)); assertTrue(created.additions().isEmpty()); assertNull(created.startTimeMillis()); assertTrue(created.deletions().isEmpty()); - assertEquals("5", created.generatedId()); + assertNotNull(created.generatedId()); assertNotNull(created.status()); waitForChangeToComplete(created); created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); waitForChangeToComplete(created); created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); + Dns.ChangeRequestOption.fields(ChangeRequestField.START_TIME)); assertTrue(created.additions().isEmpty()); assertNotNull(created.startTimeMillis()); assertTrue(created.deletions().isEmpty()); - assertEquals("7", created.generatedId()); + assertNotNull(created.generatedId()); assertNull(created.status()); waitForChangeToComplete(created); created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); waitForChangeToComplete(created); created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS)); + Dns.ChangeRequestOption.fields(ChangeRequestField.ADDITIONS)); assertEquals(CHANGE_ADD_ZONE1.additions(), created.additions()); assertNull(created.startTimeMillis()); assertTrue(created.deletions().isEmpty()); - assertEquals("9", created.generatedId()); + assertNotNull(created.generatedId()); assertNull(created.status()); // finishes with delete otherwise we cannot delete the zone waitForChangeToComplete(created); created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS)); + Dns.ChangeRequestOption.fields(ChangeRequestField.DELETIONS)); waitForChangeToComplete(created); assertEquals(CHANGE_DELETE_ZONE1.deletions(), created.deletions()); assertNull(created.startTimeMillis()); assertTrue(created.additions().isEmpty()); - assertEquals("10", created.generatedId()); + assertNotNull(created.generatedId()); assertNull(created.status()); waitForChangeToComplete(created); } finally { @@ -724,47 +727,47 @@ public void testListChanges() { // field options changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name(), Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), - Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.ADDITIONS)).iterateAll()); + Dns.ChangeRequestListOption.fields(ChangeRequestField.ADDITIONS)).iterateAll()); change = changes.get(1); assertEquals(CHANGE_ADD_ZONE1.additions(), change.additions()); assertTrue(change.deletions().isEmpty()); - assertEquals("1", change.generatedId()); + assertNotNull(change.generatedId()); 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()); + Dns.ChangeRequestListOption.fields(ChangeRequestField.DELETIONS)).iterateAll()); change = changes.get(2); assertTrue(change.additions().isEmpty()); assertNotNull(change.deletions()); - assertEquals("2", change.generatedId()); + assertNotNull(change.generatedId()); 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()); + Dns.ChangeRequestListOption.fields(ChangeRequestField.ID)).iterateAll()); change = changes.get(1); assertTrue(change.additions().isEmpty()); assertTrue(change.deletions().isEmpty()); - assertEquals("1", change.generatedId()); + assertNotNull(change.generatedId()); 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()); + Dns.ChangeRequestListOption.fields(ChangeRequestField.START_TIME)).iterateAll()); change = changes.get(1); assertTrue(change.additions().isEmpty()); assertTrue(change.deletions().isEmpty()); - assertEquals("1", change.generatedId()); + assertNotNull(change.generatedId()); 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()); + Dns.ChangeRequestListOption.fields(ChangeRequestField.STATUS)).iterateAll()); change = changes.get(1); assertTrue(change.additions().isEmpty()); assertTrue(change.deletions().isEmpty()); - assertEquals("1", change.generatedId()); + assertNotNull(change.generatedId()); assertNull(change.startTimeMillis()); assertEquals(ChangeRequest.Status.DONE, change.status()); } finally { @@ -775,7 +778,7 @@ public void testListChanges() { @Test public void testGetChange() { try { - Zone zone = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); + Zone zone = DNS.create(ZONE1, Dns.ZoneOption.fields(ZoneField.NAME)); ChangeRequest created = zone.applyChangeRequest(CHANGE_ADD_ZONE1); ChangeRequest retrieved = DNS.getChangeRequest(zone.name(), created.generatedId()); assertEqChangesIgnoreStatus(created, retrieved); @@ -783,37 +786,37 @@ public void testGetChange() { zone.applyChangeRequest(CHANGE_DELETE_ZONE1); // with options created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID)); + Dns.ChangeRequestOption.fields(ChangeRequestField.ID)); retrieved = DNS.getChangeRequest(zone.name(), created.generatedId(), - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID)); + Dns.ChangeRequestOption.fields(ChangeRequestField.ID)); assertEqChangesIgnoreStatus(created, retrieved); waitForChangeToComplete(zone.name(), created.generatedId()); zone.applyChangeRequest(CHANGE_DELETE_ZONE1); created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); + Dns.ChangeRequestOption.fields(ChangeRequestField.STATUS)); retrieved = DNS.getChangeRequest(zone.name(), created.generatedId(), - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); + Dns.ChangeRequestOption.fields(ChangeRequestField.STATUS)); assertEqChangesIgnoreStatus(created, retrieved); waitForChangeToComplete(zone.name(), created.generatedId()); zone.applyChangeRequest(CHANGE_DELETE_ZONE1); created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); + Dns.ChangeRequestOption.fields(ChangeRequestField.START_TIME)); retrieved = DNS.getChangeRequest(zone.name(), created.generatedId(), - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); + Dns.ChangeRequestOption.fields(ChangeRequestField.START_TIME)); assertEqChangesIgnoreStatus(created, retrieved); waitForChangeToComplete(zone.name(), created.generatedId()); zone.applyChangeRequest(CHANGE_DELETE_ZONE1); created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS)); + Dns.ChangeRequestOption.fields(ChangeRequestField.ADDITIONS)); retrieved = DNS.getChangeRequest(zone.name(), created.generatedId(), - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS)); + Dns.ChangeRequestOption.fields(ChangeRequestField.ADDITIONS)); assertEqChangesIgnoreStatus(created, retrieved); waitForChangeToComplete(zone.name(), created.generatedId()); // finishes with delete otherwise we cannot delete the zone created = zone.applyChangeRequest(CHANGE_DELETE_ZONE1, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS)); + Dns.ChangeRequestOption.fields(ChangeRequestField.DELETIONS)); retrieved = DNS.getChangeRequest(zone.name(), created.generatedId(), - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS)); + Dns.ChangeRequestOption.fields(ChangeRequestField.DELETIONS)); assertEqChangesIgnoreStatus(created, retrieved); waitForChangeToComplete(zone.name(), created.generatedId()); } finally { @@ -827,14 +830,14 @@ public void testGetProject() { ProjectInfo project = DNS.getProject(); assertNotNull(project.quota()); // options - project = DNS.getProject(Dns.ProjectOption.fields(Dns.ProjectField.QUOTA)); + project = DNS.getProject(Dns.ProjectOption.fields(ProjectField.QUOTA)); assertNotNull(project.quota()); - project = DNS.getProject(Dns.ProjectOption.fields(Dns.ProjectField.PROJECT_ID)); + project = DNS.getProject(Dns.ProjectOption.fields(ProjectField.PROJECT_ID)); assertNull(project.quota()); - project = DNS.getProject(Dns.ProjectOption.fields(Dns.ProjectField.PROJECT_NUMBER)); + project = DNS.getProject(Dns.ProjectOption.fields(ProjectField.PROJECT_NUMBER)); assertNull(project.quota()); - project = DNS.getProject(Dns.ProjectOption.fields(Dns.ProjectField.PROJECT_NUMBER, - Dns.ProjectField.QUOTA, Dns.ProjectField.PROJECT_ID)); + project = DNS.getProject(Dns.ProjectOption.fields(ProjectField.PROJECT_NUMBER, + ProjectField.QUOTA, ProjectField.PROJECT_ID)); assertNotNull(project.quota()); } @@ -852,7 +855,7 @@ public void testListDnsRecords() { } // field options Iterator recordSetIterator = DNS.listRecordSets(zone.name(), - Dns.RecordSetListOption.fields(Dns.RecordSetField.TTL)).iterateAll(); + Dns.RecordSetListOption.fields(RecordSetField.TTL)).iterateAll(); int counter = 0; while (recordSetIterator.hasNext()) { RecordSet recordSet = recordSetIterator.next(); @@ -864,7 +867,7 @@ public void testListDnsRecords() { } assertEquals(2, counter); recordSetIterator = DNS.listRecordSets(zone.name(), - Dns.RecordSetListOption.fields(Dns.RecordSetField.NAME)).iterateAll(); + Dns.RecordSetListOption.fields(RecordSetField.NAME)).iterateAll(); counter = 0; while (recordSetIterator.hasNext()) { RecordSet recordSet = recordSetIterator.next(); @@ -876,7 +879,7 @@ public void testListDnsRecords() { } assertEquals(2, counter); recordSetIterator = DNS.listRecordSets(zone.name(), - Dns.RecordSetListOption.fields(Dns.RecordSetField.DNS_RECORDS)) + Dns.RecordSetListOption.fields(RecordSetField.DNS_RECORDS)) .iterateAll(); counter = 0; while (recordSetIterator.hasNext()) { @@ -889,7 +892,7 @@ public void testListDnsRecords() { } assertEquals(2, counter); recordSetIterator = DNS.listRecordSets(zone.name(), - Dns.RecordSetListOption.fields(Dns.RecordSetField.TYPE), + Dns.RecordSetListOption.fields(RecordSetField.TYPE), Dns.RecordSetListOption.pageSize(1)).iterateAll(); // also test paging counter = 0; while (recordSetIterator.hasNext()) { @@ -903,7 +906,7 @@ public void testListDnsRecords() { assertEquals(2, counter); // test page size Page recordSetPage = DNS.listRecordSets(zone.name(), - Dns.RecordSetListOption.fields(Dns.RecordSetField.TYPE), + Dns.RecordSetListOption.fields(RecordSetField.TYPE), Dns.RecordSetListOption.pageSize(1)); assertEquals(1, ImmutableList.copyOf(recordSetPage.values().iterator()).size()); // test name filter @@ -923,8 +926,7 @@ public void testListDnsRecords() { waitForChangeToComplete(ZONE1.name(), change.generatedId()); recordSetIterator = DNS.listRecordSets(ZONE1.name(), Dns.RecordSetListOption.dnsName(A_RECORD_ZONE1.name()), - Dns.RecordSetListOption.type(A_RECORD_ZONE1.type())) - .iterateAll(); + Dns.RecordSetListOption.type(A_RECORD_ZONE1.type())).iterateAll(); counter = 0; while (recordSetIterator.hasNext()) { RecordSet recordSet = recordSetIterator.next(); @@ -967,19 +969,161 @@ public void testListDnsRecords() { } @Test - public void testListZoneBatch() { - DNS.create(ZONE1); - DNS.create(ZONE_EMPTY_DESCRIPTION); + public void testListZonesBatch() { try { DnsBatch batch = DNS.batch(); - DnsBatchResult> batchResult = batch.listZones(); + DnsBatchResult> result = batch.listZones(); + batch.submit(); + List zones = filter(result.get().iterateAll()); + assertEquals(0, zones.size()); + // some zones exists + Zone firstZone = DNS.create(ZONE1); + batch = DNS.batch(); + result = batch.listZones(); + batch.submit(); + zones = filter(result.get().iterateAll()); + assertEquals(1, zones.size()); + assertEquals(firstZone, zones.get(0)); + Zone created = DNS.create(ZONE_EMPTY_DESCRIPTION); + batch = DNS.batch(); + result = batch.listZones(); + DnsBatchResult> zeroSizeError = + batch.listZones(Dns.ZoneListOption.pageSize(0)); + DnsBatchResult> negativeSizeError = + batch.listZones(Dns.ZoneListOption.pageSize(-1)); + DnsBatchResult> okSize = batch.listZones(Dns.ZoneListOption.pageSize(1)); + DnsBatchResult> nameError = batch.listZones(Dns.ZoneListOption.dnsName("aaaaa")); + DnsBatchResult> okName = + batch.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName())); + DnsBatchResult> idResult = + batch.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), + Dns.ZoneListOption.fields(ZoneField.ZONE_ID)); + DnsBatchResult> timeResult = + batch.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), + Dns.ZoneListOption.fields(ZoneField.CREATION_TIME)); + DnsBatchResult> dnsNameResult = + batch.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), + Dns.ZoneListOption.fields(ZoneField.DNS_NAME)); + DnsBatchResult> descriptionResult = + batch.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), + Dns.ZoneListOption.fields(ZoneField.DESCRIPTION)); + DnsBatchResult> nameServersResult = + batch.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), + Dns.ZoneListOption.fields(ZoneField.NAME_SERVERS)); + DnsBatchResult> nameServerSetResult = + batch.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), + Dns.ZoneListOption.fields(ZoneField.NAME_SERVER_SET)); + DnsBatchResult> combinationResult = + batch.listZones(Dns.ZoneListOption.fields(ZoneField.ZONE_ID, ZoneField.DESCRIPTION), + Dns.ZoneListOption.pageSize(1)); batch.submit(); - assertTrue(batchResult.completed()); - Iterator iteratorBatch = batchResult.get().iterateAll(); - Iterator iteratorList = DNS.listZones().iterateAll(); - assertEquals(Iterators.size(iteratorList), Iterators.size(iteratorBatch)); - while (iteratorBatch.hasNext()) { - assertTrue(Iterators.contains(iteratorList, iteratorBatch.next())); + zones = filter(result.get().iterateAll()); + assertEquals(2, zones.size()); + assertTrue(zones.contains(firstZone)); + assertTrue(zones.contains(created)); + // error in options + try { + zeroSizeError.get(); + fail(); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + assertFalse(ex.retryable()); + } + try { + negativeSizeError.get(); + fail(); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + assertFalse(ex.retryable()); + } + // ok size + assertEquals(1, Iterables.size(okSize.get().values())); + // dns name problems + try { + nameError.get(); + fail(); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + assertFalse(ex.retryable()); + } + // ok name + zones = filter(okName.get().iterateAll()); + assertEquals(1, zones.size()); + // field options + Iterator zoneIterator = idResult.get().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(zone.generatedId()); + assertFalse(zoneIterator.hasNext()); + zoneIterator = timeResult.get().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.generatedId()); + assertFalse(zoneIterator.hasNext()); + zoneIterator = dnsNameResult.get().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.generatedId()); + assertFalse(zoneIterator.hasNext()); + zoneIterator = descriptionResult.get().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.generatedId()); + assertFalse(zoneIterator.hasNext()); + zoneIterator = nameServersResult.get().iterateAll(); + zone = zoneIterator.next(); + assertNull(zone.creationTimeMillis()); + assertNotNull(zone.name()); + assertNull(zone.dnsName()); + assertNull(zone.description()); + assertNull(zone.nameServerSet()); + assertFalse(zone.nameServers().isEmpty()); + assertNull(zone.generatedId()); + assertFalse(zoneIterator.hasNext()); + zoneIterator = nameServerSetResult.get().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.generatedId()); + assertFalse(zoneIterator.hasNext()); + // several combined + zones = filter(combinationResult.get().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.generatedId()); } } finally { DNS.delete(ZONE1.name()); @@ -987,5 +1131,751 @@ public void testListZoneBatch() { } } - // todo(mderka) implement tests for other batch calls, issue #874 + @Test + public void testCreateValidZoneBatch() { + try { + DnsBatch batch = DNS.batch(); + DnsBatchResult completeZoneResult = batch.createZone(ZONE1); + DnsBatchResult partialZoneResult = batch.createZone(ZONE_EMPTY_DESCRIPTION); + batch.submit(); + Zone created = completeZoneResult.get(); + 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.generatedId()); + Zone retrieved = DNS.getZone(ZONE1.name()); + assertEquals(created, retrieved); + created = partialZoneResult.get(); + 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.generatedId()); + retrieved = DNS.getZone(ZONE_EMPTY_DESCRIPTION.name()); + assertEquals(created, retrieved); + } finally { + DNS.delete(ZONE1.name()); + DNS.delete(ZONE_EMPTY_DESCRIPTION.name()); + } + } + + @Test + public void testCreateZoneWithErrorsBatch() { + try { + DnsBatch batch = DNS.batch(); + DnsBatchResult nameErrorResult = batch.createZone(ZONE_NAME_ERROR); + DnsBatchResult noPeriodResult = batch.createZone(ZONE_DNS_NO_PERIOD); + batch.submit(); + try { + nameErrorResult.get(); + fail("Zone name is missing a period. The service returns an error."); + } catch (DnsException ex) { + // expected + assertFalse(ex.retryable()); + } + try { + noPeriodResult.get(); + fail("Zone name is missing a period. The service returns an error."); + } catch (DnsException ex) { + // expected + assertFalse(ex.retryable()); + } + } finally { + DNS.delete(ZONE_NAME_ERROR.name()); + DNS.delete(ZONE_DNS_NO_PERIOD.name()); + } + } + + @Test + public void testCreateZoneWithOptionsBatch() { + try { + DnsBatch batch = DNS.batch(); + DnsBatchResult batchResult = + batch.createZone(ZONE1, Dns.ZoneOption.fields(ZoneField.CREATION_TIME)); + batch.submit(); + Zone created = batchResult.get(); + 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.generatedId()); + created.delete(); + batch = DNS.batch(); + batchResult = batch.createZone(ZONE1, Dns.ZoneOption.fields(ZoneField.DESCRIPTION)); + batch.submit(); + created = batchResult.get(); + 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.generatedId()); + created.delete(); + batch = DNS.batch(); + batchResult = batch.createZone(ZONE1, Dns.ZoneOption.fields(ZoneField.DNS_NAME)); + batch.submit(); + created = batchResult.get(); + 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.generatedId()); + created.delete(); + batch = DNS.batch(); + batchResult = batch.createZone(ZONE1, Dns.ZoneOption.fields(ZoneField.NAME)); + batch.submit(); + created = batchResult.get(); + 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.generatedId()); + created.delete(); + batch = DNS.batch(); + batchResult = batch.createZone(ZONE1, Dns.ZoneOption.fields(ZoneField.NAME_SERVER_SET)); + batch.submit(); + created = batchResult.get(); + 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.generatedId()); + created.delete(); + batch = DNS.batch(); + batchResult = batch.createZone(ZONE1, Dns.ZoneOption.fields(ZoneField.NAME_SERVERS)); + batch.submit(); + created = batchResult.get(); + 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.generatedId()); + created.delete(); + batch = DNS.batch(); + batchResult = batch.createZone(ZONE1, Dns.ZoneOption.fields(ZoneField.ZONE_ID)); + batch.submit(); + created = batchResult.get(); + 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.generatedId()); + created.delete(); + batch = DNS.batch(); + batchResult = batch.createZone(ZONE1, Dns.ZoneOption.fields(ZoneField.ZONE_ID, + ZoneField.NAME_SERVERS, ZoneField.NAME_SERVER_SET, ZoneField.DESCRIPTION)); + batch.submit(); + // combination of multiple things + created = batchResult.get(); + 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.generatedId()); + } finally { + DNS.delete(ZONE1.name()); + } + } + + @Test + public void testGetZoneBatch() { + try { + DNS.create(ZONE1, Dns.ZoneOption.fields(ZoneField.NAME)); + DnsBatch batch = DNS.batch(); + DnsBatchResult timeResult = + batch.getZone(ZONE1.name(), Dns.ZoneOption.fields(ZoneField.CREATION_TIME)); + DnsBatchResult descriptionResult = + batch.getZone(ZONE1.name(), Dns.ZoneOption.fields(ZoneField.DESCRIPTION)); + DnsBatchResult dnsNameResult = + batch.getZone(ZONE1.name(), Dns.ZoneOption.fields(ZoneField.DNS_NAME)); + DnsBatchResult nameResult = + batch.getZone(ZONE1.name(), Dns.ZoneOption.fields(ZoneField.NAME)); + DnsBatchResult nameServerSetResult = + batch.getZone(ZONE1.name(), Dns.ZoneOption.fields(ZoneField.NAME_SERVER_SET)); + DnsBatchResult nameServersResult = + batch.getZone(ZONE1.name(), Dns.ZoneOption.fields(ZoneField.NAME_SERVERS)); + DnsBatchResult idResult = + batch.getZone(ZONE1.name(), Dns.ZoneOption.fields(ZoneField.ZONE_ID)); + DnsBatchResult combinationResult = batch.getZone(ZONE1.name(), + Dns.ZoneOption.fields(ZoneField.ZONE_ID, ZoneField.NAME_SERVERS, + ZoneField.NAME_SERVER_SET, ZoneField.DESCRIPTION)); + batch.submit(); + Zone created = timeResult.get(); + 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.generatedId()); + created = descriptionResult.get(); + 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.generatedId()); + created = dnsNameResult.get(); + 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.generatedId()); + created = nameResult.get(); + 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.generatedId()); + created = nameServerSetResult.get(); + 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.generatedId()); + created = nameServersResult.get(); + 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.generatedId()); + created = idResult.get(); + 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.generatedId()); + // combination of multiple things + created = combinationResult.get(); + 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.generatedId()); + } finally { + DNS.delete(ZONE1.name()); + } + } + + @Test + public void testDeleteZoneBatch() { + try { + Zone created = DNS.create(ZONE1); + assertEquals(created, DNS.getZone(ZONE1.name())); + DnsBatch batch = DNS.batch(); + DnsBatchResult result = batch.deleteZone(ZONE1.name()); + batch.submit(); + assertNull(DNS.getZone(ZONE1.name())); + assertTrue(result.get()); + } finally { + DNS.delete(ZONE1.name()); + } + } + + @Test + public void testGetProjectBatch() { + // fetches all fields + DnsBatch batch = DNS.batch(); + DnsBatchResult result = batch.getProject(); + DnsBatchResult resultQuota = + batch.getProject(Dns.ProjectOption.fields(ProjectField.QUOTA)); + DnsBatchResult resultId = + batch.getProject(Dns.ProjectOption.fields(ProjectField.PROJECT_ID)); + DnsBatchResult resultNumber = + batch.getProject(Dns.ProjectOption.fields(ProjectField.PROJECT_NUMBER)); + DnsBatchResult resultCombination = + batch.getProject(Dns.ProjectOption.fields(ProjectField.PROJECT_NUMBER, + ProjectField.QUOTA, ProjectField.PROJECT_ID)); + batch.submit(); + assertNotNull(result.get().quota()); + assertNotNull(resultQuota.get().quota()); + assertNull(resultId.get().quota()); + assertNull(resultNumber.get().quota()); + assertNotNull(resultCombination.get().quota()); + } + + @Test + public void testCreateChangeBatch() { + try { + DNS.create(ZONE1, Dns.ZoneOption.fields(ZoneField.NAME)); + DnsBatch batch = DNS.batch(); + DnsBatchResult result = + batch.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); + batch.submit(); + ChangeRequest created = result.get(); + assertEquals(CHANGE_ADD_ZONE1.additions(), created.additions()); + assertNotNull(created.startTimeMillis()); + assertTrue(created.deletions().isEmpty()); + assertNotNull(created.generatedId()); + assertTrue(ImmutableList.of(ChangeRequest.Status.PENDING, ChangeRequest.Status.DONE) + .contains(created.status())); + assertEqChangesIgnoreStatus(created, DNS.getChangeRequest(ZONE1.name(), "1")); + waitForChangeToComplete(created); + created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + waitForChangeToComplete(created); + // with options + batch = DNS.batch(); + result = batch.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(ChangeRequestField.ID)); + batch.submit(); + created = result.get(); + assertTrue(created.additions().isEmpty()); + assertNull(created.startTimeMillis()); + assertTrue(created.deletions().isEmpty()); + assertNotNull(created.generatedId()); + assertNull(created.status()); + waitForChangeToComplete(created); + created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + waitForChangeToComplete(created); + batch = DNS.batch(); + result = batch.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(ChangeRequestField.STATUS)); + batch.submit(); + created = result.get(); + assertTrue(created.additions().isEmpty()); + assertNull(created.startTimeMillis()); + assertTrue(created.deletions().isEmpty()); + assertNotNull(created.generatedId()); + assertNotNull(created.status()); + waitForChangeToComplete(created); + created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + waitForChangeToComplete(created); + batch = DNS.batch(); + result = batch.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(ChangeRequestField.START_TIME)); + batch.submit(); + created = result.get(); + assertTrue(created.additions().isEmpty()); + assertNotNull(created.startTimeMillis()); + assertTrue(created.deletions().isEmpty()); + assertNotNull(created.generatedId()); + assertNull(created.status()); + waitForChangeToComplete(created); + created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + waitForChangeToComplete(created); + batch = DNS.batch(); + result = batch.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, + Dns.ChangeRequestOption.fields(ChangeRequestField.ADDITIONS)); + batch.submit(); + created = result.get(); + assertEquals(CHANGE_ADD_ZONE1.additions(), created.additions()); + assertNull(created.startTimeMillis()); + assertTrue(created.deletions().isEmpty()); + assertNotNull(created.generatedId()); + assertNull(created.status()); + // finishes with delete otherwise we cannot delete the zone + waitForChangeToComplete(created); + batch = DNS.batch(); + result = batch.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1, + Dns.ChangeRequestOption.fields(ChangeRequestField.DELETIONS)); + batch.submit(); + created = result.get(); + waitForChangeToComplete(created); + assertEquals(CHANGE_DELETE_ZONE1.deletions(), created.deletions()); + assertNull(created.startTimeMillis()); + assertTrue(created.additions().isEmpty()); + assertNotNull(created.generatedId()); + assertNull(created.status()); + waitForChangeToComplete(created); + } finally { + clear(); + } + } + + @Test + public void testGetChangeBatch() { + try { + Zone zone = DNS.create(ZONE1, Dns.ZoneOption.fields(ZoneField.NAME)); + ChangeRequest created = zone.applyChangeRequest(CHANGE_ADD_ZONE1); + waitForChangeToComplete(zone.name(), created.generatedId()); + DnsBatch batch = DNS.batch(); + DnsBatchResult completeResult = + batch.getChangeRequest(zone.name(), created.generatedId()); + DnsBatchResult idResult = + batch.getChangeRequest(zone.name(), created.generatedId(), + Dns.ChangeRequestOption.fields(ChangeRequestField.ID)); + DnsBatchResult statusResult = + batch.getChangeRequest(zone.name(), created.generatedId(), + Dns.ChangeRequestOption.fields(ChangeRequestField.STATUS)); + DnsBatchResult timeResult = + batch.getChangeRequest(zone.name(), created.generatedId(), + Dns.ChangeRequestOption.fields(ChangeRequestField.START_TIME)); + DnsBatchResult additionsResult = + batch.getChangeRequest(zone.name(), created.generatedId(), + Dns.ChangeRequestOption.fields(ChangeRequestField.ADDITIONS)); + batch.submit(); + assertEqChangesIgnoreStatus(created, completeResult.get()); + // with options + ChangeRequest retrieved = idResult.get(); + assertEquals(created.generatedId(), retrieved.generatedId()); + assertEquals(0, retrieved.additions().size()); + assertEquals(0, retrieved.deletions().size()); + assertNull(retrieved.startTimeMillis()); + assertNull(retrieved.status()); + retrieved = statusResult.get(); + assertEquals(created.generatedId(), retrieved.generatedId()); + assertEquals(0, retrieved.additions().size()); + assertEquals(0, retrieved.deletions().size()); + assertNull(retrieved.startTimeMillis()); + assertEquals(ChangeRequestInfo.Status.DONE, retrieved.status()); + retrieved = timeResult.get(); + assertEquals(created.generatedId(), retrieved.generatedId()); + assertEquals(0, retrieved.additions().size()); + assertEquals(0, retrieved.deletions().size()); + assertEquals(created.startTimeMillis(), retrieved.startTimeMillis()); + assertNull(retrieved.status()); + retrieved = additionsResult.get(); + assertEquals(created.generatedId(), retrieved.generatedId()); + assertEquals(2, retrieved.additions().size()); + assertTrue(retrieved.additions().contains(A_RECORD_ZONE1)); + assertTrue(retrieved.additions().contains(AAAA_RECORD_ZONE1)); + assertEquals(0, retrieved.deletions().size()); + assertNull(retrieved.startTimeMillis()); + assertNull(retrieved.status()); + // finishes with delete otherwise we cannot delete the zone + created = zone.applyChangeRequest(CHANGE_DELETE_ZONE1, + Dns.ChangeRequestOption.fields(ChangeRequestField.DELETIONS)); + batch = DNS.batch(); + DnsBatchResult deletionsResult = + batch.getChangeRequest(zone.name(), created.generatedId(), + Dns.ChangeRequestOption.fields(ChangeRequestField.DELETIONS)); + batch.submit(); + retrieved = deletionsResult.get(); + assertEquals(created.generatedId(), retrieved.generatedId()); + assertEquals(0, retrieved.additions().size()); + assertEquals(2, retrieved.deletions().size()); + assertTrue(retrieved.deletions().contains(AAAA_RECORD_ZONE1)); + assertTrue(retrieved.deletions().contains(A_RECORD_ZONE1)); + assertNull(retrieved.startTimeMillis()); + assertNull(retrieved.status()); + waitForChangeToComplete(zone.name(), created.generatedId()); + } finally { + clear(); + } + } + + @Test + public void testListChangesBatch() { + try { + DnsBatch batch = DNS.batch(); + DnsBatchResult> result = batch.listChangeRequests(ZONE1.name()); + batch.submit(); + try { + result.get(); + fail("Zone does not exist yet"); + } catch (DnsException ex) { + // expected + assertEquals(404, ex.code()); + assertFalse(ex.retryable()); + } + // zone exists but has no changes + DNS.create(ZONE1); + batch = DNS.batch(); + result = batch.listChangeRequests(ZONE1.name()); + batch.submit(); + assertEquals(1, Iterables.size(result.get().values())); // default change creating SOA and NS + // zone has changes + ChangeRequest change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); + waitForChangeToComplete(ZONE1.name(), change.generatedId()); + change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + waitForChangeToComplete(ZONE1.name(), change.generatedId()); + batch = DNS.batch(); + result = batch.listChangeRequests(ZONE1.name()); + DnsBatchResult> errorPageSize = batch.listChangeRequests(ZONE1.name(), + Dns.ChangeRequestListOption.pageSize(0)); + DnsBatchResult> errorPageNegative = batch.listChangeRequests(ZONE1.name(), + Dns.ChangeRequestListOption.pageSize(-1)); + DnsBatchResult> resultAscending = batch.listChangeRequests(ZONE1.name(), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING)); + DnsBatchResult> resultDescending = batch.listChangeRequests(ZONE1.name(), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.DESCENDING)); + DnsBatchResult> resultAdditions = batch.listChangeRequests(ZONE1.name(), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), + Dns.ChangeRequestListOption.fields(ChangeRequestField.ADDITIONS)); + DnsBatchResult> resultDeletions = batch.listChangeRequests(ZONE1.name(), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), + Dns.ChangeRequestListOption.fields(ChangeRequestField.DELETIONS)); + DnsBatchResult> resultId = batch.listChangeRequests(ZONE1.name(), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), + Dns.ChangeRequestListOption.fields(ChangeRequestField.ID)); + DnsBatchResult> resultTime = batch.listChangeRequests(ZONE1.name(), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), + Dns.ChangeRequestListOption.fields(ChangeRequestField.START_TIME)); + DnsBatchResult> resultStatus = batch.listChangeRequests(ZONE1.name(), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), + Dns.ChangeRequestListOption.fields(ChangeRequestField.STATUS)); + batch.submit(); + assertEquals(3, Iterables.size(result.get().values())); + // error in options + try { + errorPageSize.get(); + fail(); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + assertFalse(ex.retryable()); + } + try { + errorPageNegative.get(); + fail(); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + assertFalse(ex.retryable()); + } + // sorting order + ImmutableList ascending = + ImmutableList.copyOf(resultAscending.get().iterateAll()); + ImmutableList descending = + ImmutableList.copyOf(resultDescending.get().iterateAll()); + int size = 3; + 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 + change = Iterables.get(resultAdditions.get().values(), 1); + assertEquals(CHANGE_ADD_ZONE1.additions(), change.additions()); + assertTrue(change.deletions().isEmpty()); + assertNotNull(change.generatedId()); + assertNull(change.startTimeMillis()); + assertNull(change.status()); + change = Iterables.get(resultDeletions.get().values(), 2); + assertTrue(change.additions().isEmpty()); + assertNotNull(change.deletions()); + assertNotNull(change.generatedId()); + assertNull(change.startTimeMillis()); + assertNull(change.status()); + change = Iterables.get(resultId.get().values(), 1); + assertTrue(change.additions().isEmpty()); + assertTrue(change.deletions().isEmpty()); + assertNotNull(change.generatedId()); + assertNull(change.startTimeMillis()); + assertNull(change.status()); + change = Iterables.get(resultTime.get().values(), 1); + assertTrue(change.additions().isEmpty()); + assertTrue(change.deletions().isEmpty()); + assertNotNull(change.generatedId()); + assertNotNull(change.startTimeMillis()); + assertNull(change.status()); + change = Iterables.get(resultStatus.get().values(), 1); + assertTrue(change.additions().isEmpty()); + assertTrue(change.deletions().isEmpty()); + assertNotNull(change.generatedId()); + assertNull(change.startTimeMillis()); + assertEquals(ChangeRequest.Status.DONE, change.status()); + } finally { + clear(); + } + } + + @Test + public void testListDnsRecordSetsBatch() { + try { + Zone zone = DNS.create(ZONE1); + DnsBatch batch = DNS.batch(); + DnsBatchResult> result = batch.listRecordSets(zone.name()); + batch.submit(); + ImmutableList recordSets = ImmutableList.copyOf(result.get().iterateAll()); + assertEquals(2, recordSets.size()); + ImmutableList defaultRecords = + ImmutableList.of(RecordSet.Type.NS, RecordSet.Type.SOA); + for (RecordSet recordSet : recordSets) { + assertTrue(defaultRecords.contains(recordSet.type())); + } + // field options + batch = DNS.batch(); + DnsBatchResult> ttlResult = batch.listRecordSets(zone.name(), + Dns.RecordSetListOption.fields(RecordSetField.TTL)); + DnsBatchResult> nameResult = batch.listRecordSets(zone.name(), + Dns.RecordSetListOption.fields(RecordSetField.NAME)); + DnsBatchResult> recordsResult = batch.listRecordSets(zone.name(), + Dns.RecordSetListOption.fields(RecordSetField.DNS_RECORDS)); + DnsBatchResult> pageSizeResult = batch.listRecordSets(zone.name(), + Dns.RecordSetListOption.fields(RecordSetField.TYPE), + Dns.RecordSetListOption.pageSize(1)); + batch.submit(); + Iterator recordSetIterator = ttlResult.get().iterateAll(); + int counter = 0; + while (recordSetIterator.hasNext()) { + RecordSet recordSet = recordSetIterator.next(); + assertEquals(recordSets.get(counter).ttl(), recordSet.ttl()); + assertEquals(recordSets.get(counter).name(), recordSet.name()); + assertEquals(recordSets.get(counter).type(), recordSet.type()); + assertTrue(recordSet.records().isEmpty()); + counter++; + } + assertEquals(2, counter); + recordSetIterator = nameResult.get().iterateAll(); + counter = 0; + while (recordSetIterator.hasNext()) { + RecordSet recordSet = recordSetIterator.next(); + assertEquals(recordSets.get(counter).name(), recordSet.name()); + assertEquals(recordSets.get(counter).type(), recordSet.type()); + assertTrue(recordSet.records().isEmpty()); + assertNull(recordSet.ttl()); + counter++; + } + assertEquals(2, counter); + recordSetIterator = recordsResult.get().iterateAll(); + counter = 0; + while (recordSetIterator.hasNext()) { + RecordSet recordSet = recordSetIterator.next(); + assertEquals(recordSets.get(counter).records(), recordSet.records()); + assertEquals(recordSets.get(counter).name(), recordSet.name()); + assertEquals(recordSets.get(counter).type(), recordSet.type()); + assertNull(recordSet.ttl()); + counter++; + } + assertEquals(2, counter); + recordSetIterator = pageSizeResult.get().iterateAll(); // also test paging + counter = 0; + while (recordSetIterator.hasNext()) { + RecordSet recordSet = recordSetIterator.next(); + assertEquals(recordSets.get(counter).type(), recordSet.type()); + assertEquals(recordSets.get(counter).name(), recordSet.name()); + assertTrue(recordSet.records().isEmpty()); + assertNull(recordSet.ttl()); + counter++; + } + assertEquals(2, counter); + // test page size + Page recordSetPage = pageSizeResult.get(); + assertEquals(1, ImmutableList.copyOf(recordSetPage.values().iterator()).size()); + // test name filter + ChangeRequest change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); + waitForChangeToComplete(ZONE1.name(), change.generatedId()); + batch = DNS.batch(); + result = batch.listRecordSets(ZONE1.name(), + Dns.RecordSetListOption.dnsName(A_RECORD_ZONE1.name())); + batch.submit(); + recordSetIterator = result.get().iterateAll(); + counter = 0; + while (recordSetIterator.hasNext()) { + RecordSet recordSet = recordSetIterator.next(); + assertTrue(ImmutableList.of(A_RECORD_ZONE1.type(), AAAA_RECORD_ZONE1.type()) + .contains(recordSet.type())); + counter++; + } + assertEquals(2, counter); + // test type filter + batch = DNS.batch(); + result = batch.listRecordSets(ZONE1.name(), + Dns.RecordSetListOption.dnsName(A_RECORD_ZONE1.name()), + Dns.RecordSetListOption.type(A_RECORD_ZONE1.type())); + batch.submit(); + recordSetIterator = result.get().iterateAll(); + counter = 0; + while (recordSetIterator.hasNext()) { + RecordSet recordSet = recordSetIterator.next(); + assertEquals(A_RECORD_ZONE1, recordSet); + counter++; + } + assertEquals(1, counter); + batch = DNS.batch(); + DnsBatchResult> noNameError = batch.listRecordSets(ZONE1.name(), + Dns.RecordSetListOption.type(A_RECORD_ZONE1.type())); + DnsBatchResult> zeroSizeError = + batch.listRecordSets(ZONE1.name(), Dns.RecordSetListOption.pageSize(0)); + DnsBatchResult> negativeSizeError = + batch.listRecordSets(ZONE1.name(), Dns.RecordSetListOption.pageSize(-1)); + batch.submit(); + // check wrong arguments + try { + // name is not set + noNameError.get(); + fail(); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + assertFalse(ex.retryable()); + } + try { + zeroSizeError.get(); + fail(); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + assertFalse(ex.retryable()); + } + try { + negativeSizeError.get(); + fail(); + } catch (DnsException ex) { + // expected + assertEquals(400, ex.code()); + assertFalse(ex.retryable()); + } + waitForChangeToComplete(ZONE1.name(), change.generatedId()); + } finally { + clear(); + } + } + + @Test + public void testBatchCombined() { + // only testing that the combination is possible + // the results are validated in the other test methods + try { + DNS.create(ZONE1); + DnsBatch batch = DNS.batch(); + DnsBatchResult zoneResult = batch.getZone(ZONE_NAME1); + DnsBatchResult changeRequestResult = batch.getChangeRequest(ZONE_NAME1, "0"); + DnsBatchResult> pageResult = batch.listRecordSets(ZONE_NAME1); + DnsBatchResult projectResult = batch.getProject(); + assertFalse(zoneResult.completed()); + try { + zoneResult.get(); + fail("this should be submitted first"); + } catch (IllegalStateException ex) { + // expected + } + batch.submit(); + assertNotNull(zoneResult.get().creationTimeMillis()); + assertEquals(ZONE1.dnsName(), zoneResult.get().dnsName()); + assertEquals(ZONE1.description(), zoneResult.get().description()); + assertFalse(zoneResult.get().nameServers().isEmpty()); + assertNull(zoneResult.get().nameServerSet()); // we did not set it + assertNotNull(zoneResult.get().generatedId()); + assertNotNull(projectResult.get().quota()); + assertEquals(2, Iterables.size(pageResult.get().values())); + assertNotNull(changeRequestResult.get()); + } finally { + DNS.delete(ZONE1.name()); + } + } }