From 49f51c4a373c7b8a509a3838f34090bbfb27dcf8 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Mon, 14 Mar 2016 09:53:34 -0700 Subject: [PATCH] LocalDnsHelper adds the default change upon creating a zone. This now matches the behaviour of the service. Fixes #672. --- .../gcloud/dns/testing/LocalDnsHelper.java | 18 ++++++++++++++---- .../gcloud/dns/testing/LocalDnsHelperTest.java | 8 ++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/LocalDnsHelper.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/LocalDnsHelper.java index f9cd1a11281e..a558ff5c4210 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/LocalDnsHelper.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/LocalDnsHelper.java @@ -56,6 +56,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -680,7 +681,15 @@ Response createZone(String projectId, ManagedZone zone, String... fields) { completeZone.setId(BigInteger.valueOf(Math.abs(ID_GENERATOR.nextLong() % Long.MAX_VALUE))); completeZone.setNameServers(randomNameservers()); ZoneContainer zoneContainer = new ZoneContainer(completeZone); - zoneContainer.dnsRecords().set(defaultRecords(completeZone)); + ImmutableSortedMap defaultsRecords = defaultRecords(completeZone); + zoneContainer.dnsRecords().set(defaultsRecords); + Change change = new Change(); + change.setAdditions(ImmutableList.copyOf(defaultsRecords.values())); + change.setStatus("done"); + change.setId("0"); + change.setStartTime(ISODateTimeFormat.dateTime().withZoneUTC() + .print(System.currentTimeMillis())); + zoneContainer.changes().add(change); ProjectContainer projectContainer = findProject(projectId); ZoneContainer oldValue = projectContainer.zones().putIfAbsent( completeZone.getName(), zoneContainer); @@ -718,8 +727,9 @@ Response createChange(String projectId, String zoneName, Change change, String.. completeChange.setDeletions(ImmutableList.copyOf(change.getDeletions())); } /* We need to set ID for the change. We are working in concurrent environment. We know that the - element fell on an index between 0 and maxId, so we will reset all IDs in this range (all of - them are valid for the respective objects). */ + element fell on an index between 1 and maxId (index 0 is the default change which creates SOA + and NS), so we will reset all IDs between 0 and maxId (all of them are valid for the respective + objects). */ ConcurrentLinkedQueue changeSequence = zoneContainer.changes(); changeSequence.add(completeChange); int maxId = changeSequence.size(); @@ -728,7 +738,7 @@ Response createChange(String projectId, String zoneName, Change change, String.. if (index == maxId) { break; } - c.setId(String.valueOf(++index)); + c.setId(String.valueOf(index++)); } completeChange.setStatus("pending"); completeChange.setStartTime(ISODateTimeFormat.dateTime().withZoneUTC() diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/testing/LocalDnsHelperTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/testing/LocalDnsHelperTest.java index 2d049e4ffeea..15fa437eb631 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/testing/LocalDnsHelperTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/testing/LocalDnsHelperTest.java @@ -850,14 +850,14 @@ public void testListChanges() { RPC.create(ZONE1, EMPTY_RPC_OPTIONS); Iterable results = RPC.listChangeRequests(ZONE1.getName(), EMPTY_RPC_OPTIONS).results(); ImmutableList changes = ImmutableList.copyOf(results); - assertEquals(0, changes.size()); + assertEquals(1, changes.size()); // zone has changes RPC.applyChangeRequest(ZONE1.getName(), CHANGE1, EMPTY_RPC_OPTIONS); RPC.applyChangeRequest(ZONE1.getName(), CHANGE2, EMPTY_RPC_OPTIONS); RPC.applyChangeRequest(ZONE1.getName(), CHANGE_KEEP, EMPTY_RPC_OPTIONS); results = RPC.listChangeRequests(ZONE1.getName(), EMPTY_RPC_OPTIONS).results(); changes = ImmutableList.copyOf(results); - assertEquals(3, changes.size()); + assertEquals(4, changes.size()); // error in options Map options = new HashMap<>(); options.put(DnsRpc.Option.PAGE_SIZE, 0); @@ -881,14 +881,14 @@ public void testListChanges() { options.put(DnsRpc.Option.PAGE_SIZE, 15); results = RPC.listChangeRequests(ZONE1.getName(), options).results(); changes = ImmutableList.copyOf(results); - assertEquals(3, changes.size()); + assertEquals(4, changes.size()); options = new HashMap<>(); options.put(DnsRpc.Option.SORTING_ORDER, "descending"); results = RPC.listChangeRequests(ZONE1.getName(), options).results(); ImmutableList descending = ImmutableList.copyOf(results); results = RPC.listChangeRequests(ZONE1.getName(), EMPTY_RPC_OPTIONS).results(); ImmutableList ascending = ImmutableList.copyOf(results); - int size = 3; + int size = 4; assertEquals(size, descending.size()); for (int i = 0; i < size; i++) { assertEquals(descending.get(i), ascending.get(size - i - 1));