From d9df5dd0fc04332bce2eefd3c1c88f04229eadcb Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Tue, 22 Mar 2016 15:25:16 -0700 Subject: [PATCH 01/21] Added third concept of DNS batch. --- .../java/com/google/gcloud/BatchResult.java | 37 ++++ .../main/java/com/google/cloud/dns/Dns.java | 8 + .../com/google/cloud/dns/DnsException.java | 5 + .../java/com/google/cloud/dns/DnsImpl.java | 95 +++++++- .../google/cloud/dns/spi/DefaultDnsRpc.java | 78 ++++++- .../java/com/google/cloud/dns/spi/DnsRpc.java | 50 +++++ .../java/com/google/gcloud/dns/DnsBatch.java | 205 ++++++++++++++++++ .../com/google/gcloud/dns/DnsBatchResult.java | 44 ++++ .../com/google/cloud/dns/it/ITDnsTest.java | 20 ++ 9 files changed, 534 insertions(+), 8 deletions(-) create mode 100644 gcloud-java-core/src/main/java/com/google/gcloud/BatchResult.java create mode 100644 gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java create mode 100644 gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatchResult.java diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/BatchResult.java b/gcloud-java-core/src/main/java/com/google/gcloud/BatchResult.java new file mode 100644 index 000000000000..8ba86d362ea8 --- /dev/null +++ b/gcloud-java-core/src/main/java/com/google/gcloud/BatchResult.java @@ -0,0 +1,37 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gcloud; + +/** + * The class that holds a single result of a batch call. + */ +public interface BatchResult { + + /** + * Returns {@code true} if the batch has been submitted and the result is available, and {@code + * false} otherwise + */ + boolean submitted(); + + /** + * Returns result of this call. + * + * @throws IllegalArgumentException if the batch has not been submitted yet + * @throws E if an error occured when processing this request + */ + T get() throws E; +} diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/Dns.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/Dns.java index f7c1aef0a02c..343bb322db20 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/Dns.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/Dns.java @@ -503,4 +503,12 @@ ChangeRequest getChangeRequest(String zoneName, String changeRequestId, * @see Cloud DNS Chages: list */ Page listChangeRequests(String zoneName, ChangeRequestListOption... options); + + /** + * Initiates a new empty batch ready to be populated with service calls, which will use this + * {@code Dns} instance when submitted for processing to Google Cloud DNS. + */ + DnsBatch batch(); + + void submitBatch(DnsBatch batch); } diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsException.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsException.java index 8da76625cead..06e7ea0a7200 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsException.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsException.java @@ -16,6 +16,7 @@ package com.google.cloud.dns; +import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.common.collect.ImmutableSet; import com.google.cloud.BaseServiceException; import com.google.cloud.RetryHelper.RetryHelperException; @@ -39,6 +40,10 @@ public class DnsException extends BaseServiceException { new Error(null, "rateLimitExceeded")); private static final long serialVersionUID = 490302380416260252L; + public DnsException(GoogleJsonError error) { + super(error, true); + } + public DnsException(IOException exception) { super(exception, true); } diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java index 785face46173..d85b1706cb63 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java @@ -19,9 +19,17 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.cloud.RetryHelper.runWithRetries; +import com.google.api.client.googleapis.batch.BatchRequest; +import com.google.api.client.googleapis.batch.json.JsonBatchCallback; +import com.google.api.client.googleapis.json.GoogleJsonError; +import com.google.api.client.http.HttpHeaders; import com.google.api.services.dns.model.Change; import com.google.api.services.dns.model.ManagedZone; +<<<<<<< 2d2532fe332eb090479ef9ede002215549da668a import com.google.api.services.dns.model.Project; +======= +import com.google.api.services.dns.model.ManagedZonesListResponse; +>>>>>>> Added third concept of DNS batch. import com.google.api.services.dns.model.ResourceRecordSet; import com.google.common.base.Function; import com.google.common.collect.ImmutableList; @@ -34,6 +42,8 @@ import com.google.cloud.RetryHelper; import com.google.cloud.dns.spi.DnsRpc; +import java.io.IOException; +import java.util.List; import java.util.Map; import java.util.concurrent.Callable; @@ -110,6 +120,16 @@ public Page nextPage() { dnsRpc = options.rpc(); } + private static Function pbToZoneFunction(final DnsOptions options) { + return new Function() { + @Override + public Zone apply( + com.google.api.services.dns.model.ManagedZone zonePb) { + return Zone.fromPb(options.service(), zonePb); + } + }; + } + @Override public Page listZones(ZoneListOption... options) { return listZones(options(), optionMap(options)); @@ -137,8 +157,8 @@ public DnsRpc.ListResult call() { }, serviceOptions.retryParams(), EXCEPTION_HANDLER); String cursor = result.pageToken(); // transform that list into zone objects - Iterable zones = result.results() == null - ? ImmutableList.of() : Iterables.transform(result.results(), pbToZoneFunction); + Iterable zones = result.results() == null ? ImmutableList.of() + : Iterables.transform(result.results(), pbToZoneFunction(serviceOptions)); return new PageImpl<>(new ZonePageFetcher(serviceOptions, cursor, optionsMap), cursor, zones); } catch (RetryHelper.RetryHelperException e) { @@ -306,6 +326,77 @@ public Change call() { } } + @Override + public DnsBatch batch() { + return new DnsBatch(this); + } + + @Override + public void submitBatch(DnsBatch batch) { + BatchRequest batchRequest = dnsRpc.createBatch(); + for (final DnsBatch.Request request : batch.requests()) { + final Map optionMap = optionMap(request.options()); + JsonBatchCallback callback; + switch (request.operation()) { + case LIST_ZONES: + callback = listZonesCallback(this.options(), request, optionMap); + batchRequest = dnsRpc.prepareListZones(batchRequest, callback, optionMap); + break; + case CREATE_ZONE: + callback = createZoneCallback(this.options(), request, optionMap); + batchRequest = dnsRpc.prepareCreateZone(request.zoneInfo().toPb(), batchRequest, callback, + optionMap); + break; + case DELETE_ZONE: + callback = createZoneCallback(this.options(), request, optionMap); + batchRequest = dnsRpc.prepareDeleteZone(request.zoneName(), batchRequest, callback); + break; + // todo(mderka) implement the rest + } + } + dnsRpc.submitBatch(batchRequest); + } + + // todo(mderka) make methods to prepare other callbacks + private JsonBatchCallback listZonesCallback(final DnsOptions serviceOptions, + final DnsBatch.Request request, final Map optionMap) { + JsonBatchCallback callback = new JsonBatchCallback() { + @Override + public void onSuccess(ManagedZonesListResponse response, HttpHeaders httpHeaders) + throws IOException { + DnsBatchResult result = request.result(); + List zones = response.getManagedZones(); + Page zonePage = new PageImpl<>( + new ZonePageFetcher(options(), response.getNextPageToken(), optionMap), + response.getNextPageToken(), zones == null ? ImmutableList.of() + : Iterables.transform(zones, pbToZoneFunction(serviceOptions))); + result.result(zonePage); + result.submit(); + } + + @Override + public void onFailure(GoogleJsonError googleJsonError, HttpHeaders httpHeaders) + throws IOException { + DnsBatchResult result = request.result(); + result.error(new DnsException(googleJsonError)); + result.submit(); + } + }; + return callback; + } + + // todo(mderka) make methods to prepare other callbacks + private JsonBatchCallback createZoneCallback(final DnsOptions serviceOptions, + final DnsBatch.Request request, final Map optionMap) { + throw new UnsupportedOperationException("not implemented yet"); + } + + // todo(mderka) make methods to prepare other callbacks + private JsonBatchCallback deleteZoneCallback(final DnsOptions serviceOptions, + final DnsBatch.Request request, final Map optionMap) { + throw new UnsupportedOperationException("not implemented yet"); + } + private Map optionMap(Option... options) { Map temp = Maps.newEnumMap(DnsRpc.Option.class); for (Option option : options) { diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java index 05b803513acb..bf64ca86fb6a 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java @@ -1,3 +1,18 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.google.cloud.dns.spi; import static com.google.cloud.dns.spi.DnsRpc.ListResult.of; @@ -10,6 +25,8 @@ import static com.google.cloud.dns.spi.DnsRpc.Option.SORTING_ORDER; import static java.net.HttpURLConnection.HTTP_NOT_FOUND; +import com.google.api.client.googleapis.batch.BatchRequest; +import com.google.api.client.googleapis.batch.json.JsonBatchCallback; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.HttpTransport; import com.google.api.client.json.jackson.JacksonFactory; @@ -85,12 +102,7 @@ public ManagedZone getZone(String zoneName, Map options) throws DnsEx public ListResult listZones(Map options) throws DnsException { // fields, page token, page size try { - ManagedZonesListResponse zoneList = dns.managedZones().list(this.options.projectId()) - .setFields(FIELDS.getString(options)) - .setMaxResults(PAGE_SIZE.getInt(options)) - .setDnsName(DNS_NAME.getString(options)) - .setPageToken(PAGE_TOKEN.getString(options)) - .execute(); + ManagedZonesListResponse zoneList = zoneListRequest(options).execute(); return of(zoneList.getNextPageToken(), zoneList.getManagedZones()); } catch (IOException ex) { throw translate(ex); @@ -193,4 +205,58 @@ public ListResult listChangeRequests(String zoneName, Map opt throw translate(ex); } } + + @Override + public BatchRequest createBatch() { + return dns.batch(); + } + + @Override + public BatchRequest prepareListZones(BatchRequest batch, JsonBatchCallback callback, + Map options) { + try { + zoneListRequest(options).queue(batch, callback); + return batch; + } catch (IOException ex) { + throw translate(ex); + } + } + + @Override + public BatchRequest prepareCreateZone(ManagedZone zone, BatchRequest batch, + JsonBatchCallback callback, Map options) { + // todo(mderka) implement + throw new UnsupportedOperationException("not implemented yet"); + } + + @Override + public BatchRequest prepareGetZone(String zoneName, BatchRequest batch, + JsonBatchCallback callback, Map options) { + // todo(mderka) implement + throw new UnsupportedOperationException("not implemented yet"); + } + + @Override + public BatchRequest prepareDeleteZone(String zoneName, BatchRequest batch, + JsonBatchCallback callback) { + // todo(mderka) implement + throw new UnsupportedOperationException("not implemented yet"); + } + + @Override + public void submitBatch(BatchRequest batchRequest) { + try { + batchRequest.execute(); + } catch (IOException ex) { + throw translate(ex); + } + } + + private Dns.ManagedZones.List zoneListRequest(Map options) throws IOException { + return dns.managedZones().list(this.options.projectId()) + .setFields(FIELDS.getString(options)) + .setMaxResults(PAGE_SIZE.getInt(options)) + .setDnsName(DNS_NAME.getString(options)) + .setPageToken(PAGE_TOKEN.getString(options)); + } } diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java index 57d6b45ba3a5..61ea3d8b374b 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java @@ -16,6 +16,8 @@ package com.google.cloud.dns.spi; +import com.google.api.client.googleapis.batch.BatchRequest; +import com.google.api.client.googleapis.batch.json.JsonBatchCallback; import com.google.api.services.dns.model.Change; import com.google.api.services.dns.model.ManagedZone; import com.google.api.services.dns.model.Project; @@ -23,6 +25,7 @@ import com.google.common.collect.ImmutableList; import com.google.cloud.dns.DnsException; +import java.io.IOException; import java.util.Map; public interface DnsRpc { @@ -171,4 +174,51 @@ Change getChangeRequest(String zoneName, String changeRequestId, Map */ ListResult listChangeRequests(String zoneName, Map options) throws DnsException; + + /** + * Initializes an empty batch. + */ + BatchRequest createBatch(); + + /** + * Prepares a call to "list zones" and adds it to the batch with the provided {@code callback} and + * {@code options}. + * + * @return updated batch + */ + BatchRequest prepareListZones(BatchRequest batch, JsonBatchCallback callback, + Map options); + + /** + * Prepares a call to "create zone" and adds it to the batch with the provided {@code callback} + * and {@code options}. + * + * @return updated batch + */ + BatchRequest prepareCreateZone(ManagedZone zone, BatchRequest batch, JsonBatchCallback callback, + Map options); + + /** + * Prepares a call to "get zone" and adds it to the batch with the provided {@code callback} and + * {@code options}. + * + * @return updated batch + */ + BatchRequest prepareGetZone(String zoneName, BatchRequest batch, JsonBatchCallback callback, + Map options); + + /** + * Prepares a call to "delete zone" and adds it to the batch with the provided {@code callback} + * and {@code options}. + * + * @return updated batch + */ + BatchRequest prepareDeleteZone(String zoneName, BatchRequest batch, JsonBatchCallback callback); + + // todo(mderka) add prepare for every single opration + + /** + * Submits a batch of requests for processing using a single HTTP request to Cloud DNS. + */ + void submitBatch(BatchRequest requests); } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java new file mode 100644 index 000000000000..73e6233e9116 --- /dev/null +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java @@ -0,0 +1,205 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gcloud.dns; + +import com.google.gcloud.Page; + +import java.util.LinkedList; +import java.util.List; + +/** + * A batch of operations to be submitted to Google Cloud DNS using a single HTTP request. + */ +public class DnsBatch { + + private List requests = new LinkedList<>(); + private Dns dns; + + /** + * An operation to be submitted to Google Cloud DNS within this batch. Only an subset of the class + * attributes appropriate for the represented operation is initialized. Refer to the class method + * and attribute documentation for the specific fields. + */ + public static class Request { + + private final String zoneName; + private final String changeId; + private final ChangeRequest changeRequest; + private final ZoneInfo zoneInfo; + private final Operation operation; + private final AbstractOption[] options; + private final DnsBatchResult result; + + private Request(RequestBuilder builder) { + this.zoneName = builder.zoneName; + this.changeId = builder.changeId; + this.changeRequest = builder.changeRequest; + this.zoneInfo = builder.zoneInfo; + this.operation = builder.operation; + this.options = builder.options; + this.result = builder.result; + } + + private static RequestBuilder builder(Operation operation, DnsBatchResult result, + AbstractOption... options) { + return new RequestBuilder(operation, result, options); + } + + /** + * Returns the name of the zone to which the operation is applied. This field is initialized for + * zone create, get and delete operation, and listing DNS records and changes within a zone. + * Returns {@code null} in other cases. + */ + public String zoneName() { + return zoneName; + } + + /** + * Returns the id of the change request which is being retrieved. Getting a change request is + * the only operation when this attribute is initialized. The method returns {@code null} in the + * remaining cases. + */ + public String changeId() { + return changeId; + } + + /** + * Returns the change request which is being created. Creating a change request is the only + * operation when this attribute is initialized. The method returns {@code null} in the + * remaining cases. + */ + public ChangeRequest changeRequest() { + return changeRequest; + } + + /** + * Returns the zone which is being created. Creating a zone is the only operation when this + * attribute is initialized. The method returns {@code null} in the remaining cases. + */ + public ZoneInfo zoneInfo() { + return zoneInfo; + } + + /** + * Returns the type of the operation represented by this {@link DnsBatch.Request}. This field is + * always initialized. + */ + public Operation operation() { + return operation; + } + + /** + * Returns options provided to the operation. Returns an empty array if no options were + * provided. + */ + public AbstractOption[] options() { + return options == null ? new AbstractOption[0] : options; + } + + DnsBatchResult result() { + return result; + } + } + + static class RequestBuilder { + private final AbstractOption[] options; + private String zoneName; + private String changeId; + private ChangeRequest changeRequest; + private ZoneInfo zoneInfo; + private final Operation operation; + private final DnsBatchResult result; + + RequestBuilder(Operation operation, DnsBatchResult result, AbstractOption... options) { + this.operation = operation; + this.options = options; + this.result = result; + } + + RequestBuilder zoneName(String zoneName) { + this.zoneName = zoneName; + return this; + } + + RequestBuilder changeId(String changeId) { + this.changeId = changeId; + return this; + } + + RequestBuilder changeRequest(ChangeRequest changeRequest) { + this.changeRequest = changeRequest; + return this; + } + + RequestBuilder zoneInfo(ZoneInfo zoneInfo) { + this.zoneInfo = zoneInfo; + return this; + } + + Request build() { + return new Request(this); + } + } + + /** + * Represents the type of the batch operation. + */ + public enum Operation { + CREATE_ZONE, + DELETE_ZONE, + GET_ZONE, + LIST_ZONES, + APPLY_CHANGE_REQUEST, + GET_CHANGE_REQUEST, + LIST_CHANGES_REQUESTS, + LIST_DNS_RECORDS + } + + DnsBatch(Dns dns) { + this.dns = dns; + } + + public Dns service() { + return dns; + } + + List requests() { + return requests; + } + + /** + * Adds a {@code DnsBatch.Request} representing the list zones operation to this batch. The + * request will not have initialized any fields except for the operation type and options (if + * provided). The {@code options} can be used to restrict the fields returned or provide page size + * limits in the same way as for {@link Dns#listZones(Dns.ZoneListOption...)}. + */ + public DnsBatchResult> listZones(Dns.ZoneListOption... options) { + DnsBatchResult> result = new DnsBatchResult<>(); + Request request = Request.builder(Operation.LIST_ZONES, result, options).build(); + requests.add(request); + return result; + } + + // todo(mderka) add the rest of the operations + + /** + * Submits this batch for processing using a single HTTP request. + */ + public void submit() { + dns.submitBatch(this); + } +} \ No newline at end of file diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatchResult.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatchResult.java new file mode 100644 index 000000000000..7eb929f0235e --- /dev/null +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatchResult.java @@ -0,0 +1,44 @@ +package com.google.gcloud.dns; + +import com.google.gcloud.BatchResult; + +/** + * This class holds a single result of a batch call to the Cloud DNS. + */ +public class DnsBatchResult implements BatchResult { + + private T result; + private boolean submitted = false; + private DnsException error; + + DnsBatchResult() { + } + + @Override + public boolean submitted() { + return submitted; + } + + @Override + public T get() throws DnsException { + if(!submitted()) { + throw new IllegalStateException("Batch has not been submitted yet"); + } + if(error != null) { + throw error; + } + return result; + } + + void result(T result) { + this.result = result; + } + + void error(DnsException error) { + this.error = error; + } + + void submit() { + this.submitted = true; + } +} 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 2d6ce2e45457..94e39042fb0f 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 @@ -962,4 +962,24 @@ public void testListDnsRecords() { clear(); } } + + @Test + public void testListZoneBatch() { + DNS.create(ZONE1); + DNS.create(ZONE_EMPTY_DESCRIPTION); + try { + DnsBatch batch = DNS.batch(); + DnsBatchResult> batchResult = batch.listZones(); + batch.submit(); + assertTrue(batchResult.submitted()); + Iterator iteratorBatch = batchResult.get().iterateAll(); + Iterator iteratorList = DNS.listZones().iterateAll(); + while(iteratorBatch.hasNext()) { + assertEquals(iteratorList.next(), iteratorBatch.next()); + } + } finally { + DNS.delete(ZONE1.name()); + DNS.delete(ZONE_EMPTY_DESCRIPTION.name()); + } + } } From 99f7d092fb28307b78ef174d32eb4dec6b88ce5f Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Mon, 4 Apr 2016 14:30:23 -0700 Subject: [PATCH 02/21] Simplified the internals. Removed DnsBatch.Request. --- .../java/com/google/gcloud/BatchResult.java | 26 +- .../main/java/com/google/cloud/dns/Dns.java | 5 +- .../java/com/google/cloud/dns/DnsImpl.java | 81 +------ .../java/com/google/cloud/dns/spi/DnsRpc.java | 9 +- .../java/com/google/gcloud/dns/DnsBatch.java | 222 +++++------------- .../com/google/gcloud/dns/DnsBatchResult.java | 10 +- 6 files changed, 100 insertions(+), 253 deletions(-) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/BatchResult.java b/gcloud-java-core/src/main/java/com/google/gcloud/BatchResult.java index 8ba86d362ea8..80c843c0951e 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/BatchResult.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/BatchResult.java @@ -17,13 +17,13 @@ package com.google.gcloud; /** - * The class that holds a single result of a batch call. + * this class holds a single result of a batch call. */ -public interface BatchResult { +public interface BatchResult { /** * Returns {@code true} if the batch has been submitted and the result is available, and {@code - * false} otherwise + * false} otherwise. */ boolean submitted(); @@ -34,4 +34,24 @@ public interface BatchResult { * @throws E if an error occured when processing this request */ T get() throws E; + + /** + * Registers a callback for the batch operation. + */ + void notify(Callback callback); + + /** + * An interface for the batch callbacks. + */ + interface Callback { + /** + * The method to be called when the batched operation succeeds. + */ + void success(T result); + + /** + * The method to be called when the batched operation fails. + */ + void error(E exception); + } } diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/Dns.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/Dns.java index 343bb322db20..cc992ddbca3e 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/Dns.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/Dns.java @@ -505,10 +505,7 @@ ChangeRequest getChangeRequest(String zoneName, String changeRequestId, Page listChangeRequests(String zoneName, ChangeRequestListOption... options); /** - * Initiates a new empty batch ready to be populated with service calls, which will use this - * {@code Dns} instance when submitted for processing to Google Cloud DNS. + * Initiates a new empty batch ready to be populated with service calls. */ DnsBatch batch(); - - void submitBatch(DnsBatch batch); } diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java index d85b1706cb63..0a80079c7d70 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java @@ -19,17 +19,10 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.cloud.RetryHelper.runWithRetries; -import com.google.api.client.googleapis.batch.BatchRequest; -import com.google.api.client.googleapis.batch.json.JsonBatchCallback; -import com.google.api.client.googleapis.json.GoogleJsonError; -import com.google.api.client.http.HttpHeaders; import com.google.api.services.dns.model.Change; import com.google.api.services.dns.model.ManagedZone; -<<<<<<< 2d2532fe332eb090479ef9ede002215549da668a import com.google.api.services.dns.model.Project; -======= import com.google.api.services.dns.model.ManagedZonesListResponse; ->>>>>>> Added third concept of DNS batch. import com.google.api.services.dns.model.ResourceRecordSet; import com.google.common.base.Function; import com.google.common.collect.ImmutableList; @@ -42,8 +35,6 @@ import com.google.cloud.RetryHelper; import com.google.cloud.dns.spi.DnsRpc; -import java.io.IOException; -import java.util.List; import java.util.Map; import java.util.concurrent.Callable; @@ -54,7 +45,7 @@ final class DnsImpl extends BaseService implements Dns { private final DnsRpc dnsRpc; - private static class ZonePageFetcher implements PageImpl.NextPageFetcher { + static class ZonePageFetcher implements PageImpl.NextPageFetcher { private static final long serialVersionUID = 2158209410430566961L; private final Map requestOptions; @@ -120,7 +111,7 @@ public Page nextPage() { dnsRpc = options.rpc(); } - private static Function pbToZoneFunction(final DnsOptions options) { + static Function pbToZoneFunction(final DnsOptions options) { return new Function() { @Override public Zone apply( @@ -328,73 +319,7 @@ public Change call() { @Override public DnsBatch batch() { - return new DnsBatch(this); - } - - @Override - public void submitBatch(DnsBatch batch) { - BatchRequest batchRequest = dnsRpc.createBatch(); - for (final DnsBatch.Request request : batch.requests()) { - final Map optionMap = optionMap(request.options()); - JsonBatchCallback callback; - switch (request.operation()) { - case LIST_ZONES: - callback = listZonesCallback(this.options(), request, optionMap); - batchRequest = dnsRpc.prepareListZones(batchRequest, callback, optionMap); - break; - case CREATE_ZONE: - callback = createZoneCallback(this.options(), request, optionMap); - batchRequest = dnsRpc.prepareCreateZone(request.zoneInfo().toPb(), batchRequest, callback, - optionMap); - break; - case DELETE_ZONE: - callback = createZoneCallback(this.options(), request, optionMap); - batchRequest = dnsRpc.prepareDeleteZone(request.zoneName(), batchRequest, callback); - break; - // todo(mderka) implement the rest - } - } - dnsRpc.submitBatch(batchRequest); - } - - // todo(mderka) make methods to prepare other callbacks - private JsonBatchCallback listZonesCallback(final DnsOptions serviceOptions, - final DnsBatch.Request request, final Map optionMap) { - JsonBatchCallback callback = new JsonBatchCallback() { - @Override - public void onSuccess(ManagedZonesListResponse response, HttpHeaders httpHeaders) - throws IOException { - DnsBatchResult result = request.result(); - List zones = response.getManagedZones(); - Page zonePage = new PageImpl<>( - new ZonePageFetcher(options(), response.getNextPageToken(), optionMap), - response.getNextPageToken(), zones == null ? ImmutableList.of() - : Iterables.transform(zones, pbToZoneFunction(serviceOptions))); - result.result(zonePage); - result.submit(); - } - - @Override - public void onFailure(GoogleJsonError googleJsonError, HttpHeaders httpHeaders) - throws IOException { - DnsBatchResult result = request.result(); - result.error(new DnsException(googleJsonError)); - result.submit(); - } - }; - return callback; - } - - // todo(mderka) make methods to prepare other callbacks - private JsonBatchCallback createZoneCallback(final DnsOptions serviceOptions, - final DnsBatch.Request request, final Map optionMap) { - throw new UnsupportedOperationException("not implemented yet"); - } - - // todo(mderka) make methods to prepare other callbacks - private JsonBatchCallback deleteZoneCallback(final DnsOptions serviceOptions, - final DnsBatch.Request request, final Map optionMap) { - throw new UnsupportedOperationException("not implemented yet"); + return new DnsBatch(this.options()); } private Map optionMap(Option... options) { diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java index 61ea3d8b374b..53e83f7ae4bf 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java @@ -25,7 +25,6 @@ import com.google.common.collect.ImmutableList; import com.google.cloud.dns.DnsException; -import java.io.IOException; import java.util.Map; public interface DnsRpc { @@ -184,7 +183,7 @@ ListResult listChangeRequests(String zoneName, Map options) * Prepares a call to "list zones" and adds it to the batch with the provided {@code callback} and * {@code options}. * - * @return updated batch + * @return the updated batch */ BatchRequest prepareListZones(BatchRequest batch, JsonBatchCallback callback, Map options); @@ -193,7 +192,7 @@ BatchRequest prepareListZones(BatchRequest batch, JsonBatchCallback callback, * Prepares a call to "create zone" and adds it to the batch with the provided {@code callback} * and {@code options}. * - * @return updated batch + * @return the updated batch */ BatchRequest prepareCreateZone(ManagedZone zone, BatchRequest batch, JsonBatchCallback callback, Map options); @@ -202,7 +201,7 @@ BatchRequest prepareCreateZone(ManagedZone zone, BatchRequest batch, JsonBatchCa * Prepares a call to "get zone" and adds it to the batch with the provided {@code callback} and * {@code options}. * - * @return updated batch + * @return the updated batch */ BatchRequest prepareGetZone(String zoneName, BatchRequest batch, JsonBatchCallback callback, Map options); @@ -211,7 +210,7 @@ BatchRequest prepareGetZone(String zoneName, BatchRequest batch, JsonBatchCallba * Prepares a call to "delete zone" and adds it to the batch with the provided {@code callback} * and {@code options}. * - * @return updated batch + * @return the updated batch */ BatchRequest prepareDeleteZone(String zoneName, BatchRequest batch, JsonBatchCallback callback); diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java index 73e6233e9116..7c62abd6c152 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java @@ -16,190 +16,92 @@ package com.google.gcloud.dns; +import static com.google.common.base.Preconditions.checkArgument; + +import com.google.api.client.googleapis.batch.BatchRequest; +import com.google.api.client.googleapis.batch.json.JsonBatchCallback; +import com.google.api.client.googleapis.json.GoogleJsonError; +import com.google.api.client.http.HttpHeaders; +import com.google.api.services.dns.model.ManagedZone; +import com.google.api.services.dns.model.ManagedZonesListResponse; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Iterables; +import com.google.common.collect.Maps; import com.google.gcloud.Page; +import com.google.gcloud.PageImpl; +import com.google.gcloud.dns.spi.DnsRpc; -import java.util.LinkedList; +import java.io.IOException; import java.util.List; +import java.util.Map; /** * A batch of operations to be submitted to Google Cloud DNS using a single HTTP request. */ public class DnsBatch { - private List requests = new LinkedList<>(); - private Dns dns; + private final BatchRequest batch; + private transient DnsRpc dnsRpc; + private final DnsOptions options; - /** - * An operation to be submitted to Google Cloud DNS within this batch. Only an subset of the class - * attributes appropriate for the represented operation is initialized. Refer to the class method - * and attribute documentation for the specific fields. - */ - public static class Request { - - private final String zoneName; - private final String changeId; - private final ChangeRequest changeRequest; - private final ZoneInfo zoneInfo; - private final Operation operation; - private final AbstractOption[] options; - private final DnsBatchResult result; - - private Request(RequestBuilder builder) { - this.zoneName = builder.zoneName; - this.changeId = builder.changeId; - this.changeRequest = builder.changeRequest; - this.zoneInfo = builder.zoneInfo; - this.operation = builder.operation; - this.options = builder.options; - this.result = builder.result; - } - - private static RequestBuilder builder(Operation operation, DnsBatchResult result, - AbstractOption... options) { - return new RequestBuilder(operation, result, options); - } - - /** - * Returns the name of the zone to which the operation is applied. This field is initialized for - * zone create, get and delete operation, and listing DNS records and changes within a zone. - * Returns {@code null} in other cases. - */ - public String zoneName() { - return zoneName; - } - - /** - * Returns the id of the change request which is being retrieved. Getting a change request is - * the only operation when this attribute is initialized. The method returns {@code null} in the - * remaining cases. - */ - public String changeId() { - return changeId; - } - - /** - * Returns the change request which is being created. Creating a change request is the only - * operation when this attribute is initialized. The method returns {@code null} in the - * remaining cases. - */ - public ChangeRequest changeRequest() { - return changeRequest; - } - - /** - * Returns the zone which is being created. Creating a zone is the only operation when this - * attribute is initialized. The method returns {@code null} in the remaining cases. - */ - public ZoneInfo zoneInfo() { - return zoneInfo; - } - - /** - * Returns the type of the operation represented by this {@link DnsBatch.Request}. This field is - * always initialized. - */ - public Operation operation() { - return operation; - } - - /** - * Returns options provided to the operation. Returns an empty array if no options were - * provided. - */ - public AbstractOption[] options() { - return options == null ? new AbstractOption[0] : options; - } - - DnsBatchResult result() { - return result; - } - } - - static class RequestBuilder { - private final AbstractOption[] options; - private String zoneName; - private String changeId; - private ChangeRequest changeRequest; - private ZoneInfo zoneInfo; - private final Operation operation; - private final DnsBatchResult result; - - RequestBuilder(Operation operation, DnsBatchResult result, AbstractOption... options) { - this.operation = operation; - this.options = options; - this.result = result; - } - - RequestBuilder zoneName(String zoneName) { - this.zoneName = zoneName; - return this; - } - - RequestBuilder changeId(String changeId) { - this.changeId = changeId; - return this; - } - - RequestBuilder changeRequest(ChangeRequest changeRequest) { - this.changeRequest = changeRequest; - return this; - } - - RequestBuilder zoneInfo(ZoneInfo zoneInfo) { - this.zoneInfo = zoneInfo; - return this; - } - - Request build() { - return new Request(this); - } + DnsBatch(DnsOptions options) { + this.options = options; + this.dnsRpc = options.rpc(); + this.batch = dnsRpc.createBatch(); } /** - * Represents the type of the batch operation. - */ - public enum Operation { - CREATE_ZONE, - DELETE_ZONE, - GET_ZONE, - LIST_ZONES, - APPLY_CHANGE_REQUEST, - GET_CHANGE_REQUEST, - LIST_CHANGES_REQUESTS, - LIST_DNS_RECORDS - } - - DnsBatch(Dns dns) { - this.dns = dns; - } - - public Dns service() { - return dns; - } - - List requests() { - return requests; - } - - /** - * Adds a {@code DnsBatch.Request} representing the list zones operation to this batch. The + * Adds a request representing the list zones operation to this batch. The * request will not have initialized any fields except for the operation type and options (if * provided). The {@code options} can be used to restrict the fields returned or provide page size * limits in the same way as for {@link Dns#listZones(Dns.ZoneListOption...)}. */ public DnsBatchResult> listZones(Dns.ZoneListOption... options) { DnsBatchResult> result = new DnsBatchResult<>(); - Request request = Request.builder(Operation.LIST_ZONES, result, options).build(); - requests.add(request); + final Map optionMap = optionMap(options); + JsonBatchCallback callback = listZonesCallback(this.options, result, optionMap); + dnsRpc.prepareListZones(this.batch, callback, optionMap); return result; } - // todo(mderka) add the rest of the operations - /** * Submits this batch for processing using a single HTTP request. */ public void submit() { - dns.submitBatch(this); + dnsRpc.submitBatch(batch); + } + + // todo(mderka) make methods to prepare other callbacks + private JsonBatchCallback listZonesCallback(final DnsOptions serviceOptions, + final DnsBatchResult result, final Map optionMap) { + JsonBatchCallback callback = new JsonBatchCallback() { + @Override + public void onSuccess(ManagedZonesListResponse response, HttpHeaders httpHeaders) + throws IOException { + List zones = response.getManagedZones(); + Page zonePage = new PageImpl<>( + new DnsImpl.ZonePageFetcher(options, response.getNextPageToken(), optionMap), + response.getNextPageToken(), zones == null ? ImmutableList.of() + : Iterables.transform(zones, DnsImpl.pbToZoneFunction(serviceOptions))); + result.success(zonePage); + } + + @Override + public void onFailure(GoogleJsonError googleJsonError, HttpHeaders httpHeaders) + throws IOException { + result.error(new DnsException(googleJsonError)); + } + }; + return callback; + } + + private Map optionMap(AbstractOption... options) { + Map temp = Maps.newEnumMap(DnsRpc.Option.class); + for (AbstractOption option : options) { + Object prev = temp.put(option.rpcOption(), option.value()); + checkArgument(prev == null, "Duplicate option %s", option); + } + return ImmutableMap.copyOf(temp); } -} \ No newline at end of file +} diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatchResult.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatchResult.java index 7eb929f0235e..7dcba8197307 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatchResult.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatchResult.java @@ -30,15 +30,19 @@ public T get() throws DnsException { return result; } - void result(T result) { - this.result = result; + @Override + public void notify(Callback callback) { + // todo(mderka) implement + throw new UnsupportedOperationException("Not implemented yet"); } void error(DnsException error) { this.error = error; + this.submitted = true; } - void submit() { + void success(T result) { + this.result = result; this.submitted = true; } } From 0e5ff2aaec65c81abe801d6dfa9e7e75f5e374a0 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Tue, 5 Apr 2016 14:46:49 -0700 Subject: [PATCH 03/21] Work in progress. Implemented more calls and added tests. --- .../java/com/google/gcloud/BatchResult.java | 6 +- .../google/cloud/dns/spi/DefaultDnsRpc.java | 202 +++++++---- .../java/com/google/cloud/dns/spi/DnsRpc.java | 45 ++- .../java/com/google/gcloud/dns/DnsBatch.java | 174 ++++++++-- .../com/google/gcloud/dns/DnsBatchResult.java | 20 +- .../com/google/cloud/dns/it/ITDnsTest.java | 2 + .../google/gcloud/dns/DnsBatchResultTest.java | 71 ++++ .../com/google/gcloud/dns/DnsBatchTest.java | 328 ++++++++++++++++++ 8 files changed, 746 insertions(+), 102 deletions(-) create mode 100644 gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchResultTest.java create mode 100644 gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/BatchResult.java b/gcloud-java-core/src/main/java/com/google/gcloud/BatchResult.java index 80c843c0951e..98dae5d22aa4 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/BatchResult.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/BatchResult.java @@ -17,12 +17,12 @@ package com.google.gcloud; /** - * this class holds a single result of a batch call. + * This class holds a single result of a batch call. */ public interface BatchResult { /** - * Returns {@code true} if the batch has been submitted and the result is available, and {@code + * Returns {@code true} if the batch has been submitted and the result is available; {@code * false} otherwise. */ boolean submitted(); @@ -31,7 +31,7 @@ public interface BatchResult { * Returns result of this call. * * @throws IllegalArgumentException if the batch has not been submitted yet - * @throws E if an error occured when processing this request + * @throws E if an error occurred when processing this request */ T get() throws E; diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java index bf64ca86fb6a..74396d180953 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java @@ -27,6 +27,8 @@ import com.google.api.client.googleapis.batch.BatchRequest; import com.google.api.client.googleapis.batch.json.JsonBatchCallback; +import com.google.api.client.googleapis.json.GoogleJsonError; +import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.HttpTransport; import com.google.api.client.json.jackson.JacksonFactory; @@ -73,22 +75,24 @@ public DefaultDnsRpc(DnsOptions options) { @Override public ManagedZone create(ManagedZone zone, Map options) throws DnsException { try { - return dns.managedZones() - .create(this.options.projectId(), zone) - .setFields(FIELDS.getString(options)) - .execute(); + return createZoneRequest(zone, options).execute(); } catch (IOException ex) { throw translate(ex); } } + private Dns.ManagedZones.Create createZoneRequest(ManagedZone zone, Map options) + throws IOException { + return dns.managedZones() + .create(this.options.projectId(), zone) + .setFields(FIELDS.getString(options)); + } + @Override public ManagedZone getZone(String zoneName, Map options) throws DnsException { // just fields option try { - return dns.managedZones().get(this.options.projectId(), zoneName) - .setFields(FIELDS.getString(options)) - .execute(); + return getZoneRequest(zoneName, options).execute(); } catch (IOException ex) { DnsException serviceException = translate(ex); if (serviceException.code() == HTTP_NOT_FOUND) { @@ -98,6 +102,13 @@ public ManagedZone getZone(String zoneName, Map options) throws DnsEx } } + private Dns.ManagedZones.Get getZoneRequest(String zoneName, Map options) + throws IOException { + return dns.managedZones() + .get(this.options.projectId(), zoneName) + .setFields(FIELDS.getString(options)); + } + @Override public ListResult listZones(Map options) throws DnsException { // fields, page token, page size @@ -109,10 +120,18 @@ public ListResult listZones(Map options) throws DnsExcep } } + private Dns.ManagedZones.List zoneListRequest(Map options) throws IOException { + return dns.managedZones().list(this.options.projectId()) + .setFields(FIELDS.getString(options)) + .setMaxResults(PAGE_SIZE.getInt(options)) + .setDnsName(DNS_NAME.getString(options)) + .setPageToken(PAGE_TOKEN.getString(options)); + } + @Override public boolean deleteZone(String zoneName) throws DnsException { try { - dns.managedZones().delete(this.options.projectId(), zoneName).execute(); + deleteZoneRequest(zoneName).execute(); return true; } catch (IOException ex) { DnsException serviceException = translate(ex); @@ -123,54 +142,69 @@ public boolean deleteZone(String zoneName) throws DnsException { } } + private Dns.ManagedZones.Delete deleteZoneRequest(String zoneName) throws IOException { + return dns.managedZones().delete(this.options.projectId(), zoneName); + } + @Override public ListResult listRecordSets(String zoneName, Map options) throws DnsException { - // options are fields, page token, dns name, type + try { - ResourceRecordSetsListResponse response = dns.resourceRecordSets() - .list(this.options.projectId(), zoneName) - .setFields(FIELDS.getString(options)) - .setPageToken(PAGE_TOKEN.getString(options)) - .setMaxResults(PAGE_SIZE.getInt(options)) - .setName(NAME.getString(options)) - .setType(DNS_TYPE.getString(options)) - .execute(); + ResourceRecordSetsListResponse response = listDnsRecordsRequest(zoneName, options).execute(); return of(response.getNextPageToken(), response.getRrsets()); } catch (IOException ex) { throw translate(ex); } } + private Dns.ResourceRecordSets.List listDnsRecordsRequest(String zoneName, Map options) + throws IOException { + // options are fields, page token, dns name, type + return dns.resourceRecordSets() + .list(this.options.projectId(), zoneName) + .setFields(FIELDS.getString(options)) + .setPageToken(PAGE_TOKEN.getString(options)) + .setMaxResults(PAGE_SIZE.getInt(options)) + .setName(NAME.getString(options)) + .setType(DNS_TYPE.getString(options)); + } + @Override public Project getProject(Map options) throws DnsException { try { - return dns.projects().get(this.options.projectId()) - .setFields(FIELDS.getString(options)).execute(); + return getProjectRequest(options).execute(); } catch (IOException ex) { throw translate(ex); } } + private Dns.Projects.Get getProjectRequest(Map options) throws IOException { + return dns.projects().get(this.options.projectId()).setFields(FIELDS.getString(options)); + } + @Override public Change applyChangeRequest(String zoneName, Change changeRequest, Map options) throws DnsException { try { - return dns.changes().create(this.options.projectId(), zoneName, changeRequest) - .setFields(FIELDS.getString(options)) - .execute(); + return applyChangeRequestRequest(zoneName, changeRequest, options).execute(); } catch (IOException ex) { throw translate(ex); } } + private Dns.Changes.Create applyChangeRequestRequest(String zoneName, Change changeRequest, + Map options) throws IOException { + return dns.changes() + .create(this.options.projectId(), zoneName, changeRequest) + .setFields(FIELDS.getString(options)); + } + @Override public Change getChangeRequest(String zoneName, String changeRequestId, Map options) throws DnsException { try { - return dns.changes().get(this.options.projectId(), zoneName, changeRequestId) - .setFields(FIELDS.getString(options)) - .execute(); + return getChangeRequestRequest(zoneName, changeRequestId, options).execute(); } catch (IOException ex) { DnsException serviceException = translate(ex); if (serviceException.code() == HTTP_NOT_FOUND) { @@ -186,77 +220,127 @@ public Change getChangeRequest(String zoneName, String changeRequestId, Map options) throws IOException { + return dns.changes() + .get(this.options.projectId(), zoneName, changeRequestId) + .setFields(FIELDS.getString(options)); + } + @Override public ListResult listChangeRequests(String zoneName, Map options) throws DnsException { - // options are fields, page token, page size, sort order try { - Dns.Changes.List request = dns.changes().list(this.options.projectId(), zoneName) - .setFields(FIELDS.getString(options)) - .setMaxResults(PAGE_SIZE.getInt(options)) - .setPageToken(PAGE_TOKEN.getString(options)); - if (SORTING_ORDER.getString(options) != null) { - // todo check and change if more sorting options are implemented, issue #604 - request = request.setSortBy(SORT_BY).setSortOrder(SORTING_ORDER.getString(options)); - } - ChangesListResponse response = request.execute(); + ChangesListResponse response = listChangeRequestsRequest(zoneName, options).execute(); return of(response.getNextPageToken(), response.getChanges()); } catch (IOException ex) { throw translate(ex); } } + // todo(mderka) rename + private Dns.Changes.List listChangeRequestsRequest(String zoneName, Map options) + throws IOException { + // options are fields, page token, page size, sort order + Dns.Changes.List request = dns.changes().list(this.options.projectId(), zoneName) + .setFields(FIELDS.getString(options)) + .setMaxResults(PAGE_SIZE.getInt(options)) + .setPageToken(PAGE_TOKEN.getString(options)); + if (SORTING_ORDER.getString(options) != null) { + // todo check and change if more sorting options are implemented, issue #604 + request = request.setSortBy(SORT_BY).setSortOrder(SORTING_ORDER.getString(options)); + } + return request; + } + @Override public BatchRequest createBatch() { return dns.batch(); } @Override - public BatchRequest prepareListZones(BatchRequest batch, JsonBatchCallback callback, - Map options) { + public BatchRequest addToBatchListZones(Object batch, + DnsRpc.Callback callback, Map options) { + BatchRequest casted = (BatchRequest) batch; try { - zoneListRequest(options).queue(batch, callback); - return batch; + zoneListRequest(options).queue(casted, jsonCallback(callback)); + return casted; } catch (IOException ex) { throw translate(ex); } } @Override - public BatchRequest prepareCreateZone(ManagedZone zone, BatchRequest batch, - JsonBatchCallback callback, Map options) { - // todo(mderka) implement - throw new UnsupportedOperationException("not implemented yet"); + public BatchRequest addToBatchCreateZone(ManagedZone zone, Object batch, + DnsRpc.Callback callback, Map options) { + BatchRequest casted = (BatchRequest) batch; + try { + createZoneRequest(zone, options).queue(casted, jsonCallback(callback)); + return casted; + } catch (IOException ex) { + throw translate(ex); + } } @Override - public BatchRequest prepareGetZone(String zoneName, BatchRequest batch, - JsonBatchCallback callback, Map options) { - // todo(mderka) implement - throw new UnsupportedOperationException("not implemented yet"); + public BatchRequest addToBatchGetZone(String zoneName, Object batch, + DnsRpc.Callback callback, Map options) { + BatchRequest casted = (BatchRequest) batch; + try { + getZoneRequest(zoneName, options).queue(casted, jsonCallback(callback)); + return casted; + } catch (IOException ex) { + throw translate(ex); + } } @Override - public BatchRequest prepareDeleteZone(String zoneName, BatchRequest batch, - JsonBatchCallback callback) { - // todo(mderka) implement - throw new UnsupportedOperationException("not implemented yet"); + public BatchRequest addToBatchDeleteZone(String zoneName, Object batch, + DnsRpc.Callback callback) { + BatchRequest casted = (BatchRequest) batch; + try { + deleteZoneRequest(zoneName).queue(casted, jsonCallback(callback)); + return casted; + } catch (IOException ex) { + throw translate(ex); + } } @Override - public void submitBatch(BatchRequest batchRequest) { + public BatchRequest addToBatchGetProject(Object batch, DnsRpc.Callback callback, + Map options) { + BatchRequest casted = (BatchRequest) batch; try { - batchRequest.execute(); + getProjectRequest(options).queue(casted, jsonCallback(callback)); + return casted; } catch (IOException ex) { throw translate(ex); } } - private Dns.ManagedZones.List zoneListRequest(Map options) throws IOException { - return dns.managedZones().list(this.options.projectId()) - .setFields(FIELDS.getString(options)) - .setMaxResults(PAGE_SIZE.getInt(options)) - .setDnsName(DNS_NAME.getString(options)) - .setPageToken(PAGE_TOKEN.getString(options)); + private static JsonBatchCallback jsonCallback(final DnsRpc.Callback callback) { + JsonBatchCallback jsonCallback = new JsonBatchCallback() { + @Override + public void onSuccess(T response, HttpHeaders httpHeaders) throws IOException { + callback.onSuccess(response); + } + + @Override + public void onFailure(GoogleJsonError googleJsonError, HttpHeaders httpHeaders) + throws IOException { + callback.onFailure(googleJsonError); + } + }; + return jsonCallback; + } + + @Override + public void submitBatch(Object batchRequest) { + try { + ((BatchRequest) batchRequest).execute(); + } catch (IOException ex) { + throw translate(ex); + } } } diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java index 53e83f7ae4bf..dbdc0d60d8a2 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java @@ -16,10 +16,10 @@ package com.google.cloud.dns.spi; -import com.google.api.client.googleapis.batch.BatchRequest; -import com.google.api.client.googleapis.batch.json.JsonBatchCallback; +import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.services.dns.model.Change; import com.google.api.services.dns.model.ManagedZone; +import com.google.api.services.dns.model.ManagedZonesListResponse; import com.google.api.services.dns.model.Project; import com.google.api.services.dns.model.ResourceRecordSet; import com.google.common.collect.ImmutableList; @@ -85,6 +85,22 @@ public String pageToken() { } } + /** + * An interface for batch callbacks. + */ + interface Callback { + + /** + * This method will be called upon success of the batch operation. + */ + void onSuccess(T response); + + /** + * This method will be called upon failure of the batch operation. + */ + void onFailure(GoogleJsonError googleJsonError); + } + /** * Creates a new zone. * @@ -177,7 +193,7 @@ ListResult listChangeRequests(String zoneName, Map options) /** * Initializes an empty batch. */ - BatchRequest createBatch(); + Object createBatch(); /** * Prepares a call to "list zones" and adds it to the batch with the provided {@code callback} and @@ -185,8 +201,8 @@ ListResult listChangeRequests(String zoneName, Map options) * * @return the updated batch */ - BatchRequest prepareListZones(BatchRequest batch, JsonBatchCallback callback, - Map options); + Object addToBatchListZones(Object batch, + Callback callback, Map options); /** * Prepares a call to "create zone" and adds it to the batch with the provided {@code callback} @@ -194,8 +210,8 @@ BatchRequest prepareListZones(BatchRequest batch, JsonBatchCallback callback, * * @return the updated batch */ - BatchRequest prepareCreateZone(ManagedZone zone, BatchRequest batch, JsonBatchCallback callback, - Map options); + Object addToBatchCreateZone(ManagedZone zone, Object batch, + Callback callback, Map options); /** * Prepares a call to "get zone" and adds it to the batch with the provided {@code callback} and @@ -203,7 +219,7 @@ BatchRequest prepareCreateZone(ManagedZone zone, BatchRequest batch, JsonBatchCa * * @return the updated batch */ - BatchRequest prepareGetZone(String zoneName, BatchRequest batch, JsonBatchCallback callback, + Object addToBatchGetZone(String zoneName, Object batch, Callback callback, Map options); /** @@ -212,12 +228,19 @@ BatchRequest prepareGetZone(String zoneName, BatchRequest batch, JsonBatchCallba * * @return the updated batch */ - BatchRequest prepareDeleteZone(String zoneName, BatchRequest batch, JsonBatchCallback callback); + Object addToBatchDeleteZone(String zoneName, Object batch, Callback callback); - // todo(mderka) add prepare for every single opration + /** + * Prepares a call to "get project" and adds it to the batch with the provided {@code callback} + * and {@code options}. + * + * @return the updated batch + */ + Object addToBatchGetProject(Object batch, Callback callback, + Map options); /** * Submits a batch of requests for processing using a single HTTP request to Cloud DNS. */ - void submitBatch(BatchRequest requests); + void submitBatch(Object batch); } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java index 7c62abd6c152..a865d21c845c 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java @@ -17,13 +17,12 @@ package com.google.gcloud.dns; import static com.google.common.base.Preconditions.checkArgument; +import static java.net.HttpURLConnection.HTTP_NOT_FOUND; -import com.google.api.client.googleapis.batch.BatchRequest; -import com.google.api.client.googleapis.batch.json.JsonBatchCallback; import com.google.api.client.googleapis.json.GoogleJsonError; -import com.google.api.client.http.HttpHeaders; import com.google.api.services.dns.model.ManagedZone; import com.google.api.services.dns.model.ManagedZonesListResponse; +import com.google.api.services.dns.model.Project; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; @@ -32,7 +31,6 @@ import com.google.gcloud.PageImpl; import com.google.gcloud.dns.spi.DnsRpc; -import java.io.IOException; import java.util.List; import java.util.Map; @@ -41,8 +39,8 @@ */ public class DnsBatch { - private final BatchRequest batch; - private transient DnsRpc dnsRpc; + private final Object batch; // DnsBatch + private final DnsRpc dnsRpc; private final DnsOptions options; DnsBatch(DnsOptions options) { @@ -51,17 +49,88 @@ public class DnsBatch { this.batch = dnsRpc.createBatch(); } + Object batch() { + return batch; + } + + DnsRpc dnsRpc() { + return dnsRpc; + } + + DnsOptions options() { + return options; + } + /** - * Adds a request representing the list zones operation to this batch. The - * request will not have initialized any fields except for the operation type and options (if - * provided). The {@code options} can be used to restrict the fields returned or provide page size - * limits in the same way as for {@link Dns#listZones(Dns.ZoneListOption...)}. + * Adds a request representing the "list zones" operation to this batch. The {@code options} can + * be used to restrict the fields returned or provide page size limits in the same way as for + * {@link Dns#listZones(Dns.ZoneListOption...)}. The returned {@link DnsBatchResult} will return a + * page of zones upon calling {@link DnsBatchResult#get()} on successful completion, or it will + * throw a {@link DnsException} if the operation failed. */ public DnsBatchResult> listZones(Dns.ZoneListOption... options) { DnsBatchResult> result = new DnsBatchResult<>(); final Map optionMap = optionMap(options); - JsonBatchCallback callback = listZonesCallback(this.options, result, optionMap); - dnsRpc.prepareListZones(this.batch, callback, optionMap); + DnsRpc.Callback callback = listZonesCallback(result, optionMap); + dnsRpc.addToBatchListZones(this.batch, callback, optionMap); + return result; + } + + /** + * Adds a request representing the "create zone" operation to this batch. The {@code options} can + * be used to restrict the fields returned in the same way as for {@link Dns#create(ZoneInfo, + * Dns.ZoneOption...)}. The returned {@link DnsBatchResult} will return the created {@link Zone} + * upon calling {@link DnsBatchResult#get()} on successful completion, or it will throw a {@link + * DnsException} if the operation failed. + */ + public DnsBatchResult createZone(ZoneInfo zone, Dns.ZoneOption... options) { + DnsBatchResult result = new DnsBatchResult<>(); + DnsRpc.Callback callback = zoneCallback(this.options, result); + Map optionMap = optionMap(options); + dnsRpc.addToBatchCreateZone(zone.toPb(), this.batch, callback, optionMap); + return result; + } + + /** + * Adds a request representing the "delete zone" operation to this batch. The returned {@link + * DnsBatchResult} will return {@code true} upon calling {@link DnsBatchResult#get()} on + * successful deletion, {@code false} if the zone was not found, or it will throw a {@link + * DnsException} if the operation failed. + */ + public DnsBatchResult deleteZone(String zoneName) { + DnsBatchResult result = new DnsBatchResult<>(); + DnsRpc.Callback callback = deleteZoneCallback(result); + dnsRpc.addToBatchDeleteZone(zoneName, this.batch, callback); + return result; + } + + /** + * Adds a request representing the "create zone" operation to this batch. The {@code options} can + * be used to restrict the fields returned in the same way as for {@link Dns#getZone(String, + * Dns.ZoneOption...)}. The returned {@link DnsBatchResult} will return the requested {@link Zone} + * upon calling {@link DnsBatchResult#get()} on successful completion, {@code null} if no such + * zone exists, or it will throw a {@link DnsException} if the operation failed. + */ + public DnsBatchResult getZone(String zoneName, Dns.ZoneOption... options) { + DnsBatchResult result = new DnsBatchResult<>(); + DnsRpc.Callback callback = zoneCallback(this.options, result); + Map optionMap = optionMap(options); + dnsRpc.addToBatchGetZone(zoneName, this.batch, callback, optionMap); + return result; + } + + /** + * Adds a request representing the "get project" operation to this batch. The {@code options} can + * be used to restrict the fields returned in the same way as for {@link + * Dns#getProject(Dns.ProjectOption...)} The returned {@link DnsBatchResult} will return the + * requested {@link ProjectInfo} upon calling {@link DnsBatchResult#get()} on successful + * completion, or it will throw a {@link DnsException} if the operation failed. + */ + public DnsBatchResult getProject(Dns.ProjectOption... options) { + DnsBatchResult result = new DnsBatchResult<>(); + DnsRpc.Callback callback = projectCallback(result); + Map optionMap = optionMap(options); + dnsRpc.addToBatchGetProject(this.batch, callback, optionMap); return result; } @@ -72,36 +141,87 @@ public void submit() { dnsRpc.submitBatch(batch); } - // todo(mderka) make methods to prepare other callbacks - private JsonBatchCallback listZonesCallback(final DnsOptions serviceOptions, + private Map optionMap(AbstractOption... options) { + Map temp = Maps.newEnumMap(DnsRpc.Option.class); + for (AbstractOption option : options) { + Object prev = temp.put(option.rpcOption(), option.value()); + checkArgument(prev == null, "Duplicate option %s", option); + } + return ImmutableMap.copyOf(temp); + } + + private DnsRpc.Callback listZonesCallback( final DnsBatchResult result, final Map optionMap) { - JsonBatchCallback callback = new JsonBatchCallback() { + DnsRpc.Callback callback = new DnsRpc.Callback() { @Override - public void onSuccess(ManagedZonesListResponse response, HttpHeaders httpHeaders) - throws IOException { + public void onSuccess(ManagedZonesListResponse response) { List zones = response.getManagedZones(); Page zonePage = new PageImpl<>( new DnsImpl.ZonePageFetcher(options, response.getNextPageToken(), optionMap), response.getNextPageToken(), zones == null ? ImmutableList.of() - : Iterables.transform(zones, DnsImpl.pbToZoneFunction(serviceOptions))); + : Iterables.transform(zones, DnsImpl.pbToZoneFunction(options))); result.success(zonePage); } @Override - public void onFailure(GoogleJsonError googleJsonError, HttpHeaders httpHeaders) - throws IOException { + public void onFailure(GoogleJsonError googleJsonError) { result.error(new DnsException(googleJsonError)); } }; return callback; } - private Map optionMap(AbstractOption... options) { - Map temp = Maps.newEnumMap(DnsRpc.Option.class); - for (AbstractOption option : options) { - Object prev = temp.put(option.rpcOption(), option.value()); - checkArgument(prev == null, "Duplicate option %s", option); - } - return ImmutableMap.copyOf(temp); + private DnsRpc.Callback deleteZoneCallback(final DnsBatchResult result) { + DnsRpc.Callback callback = new DnsRpc.Callback() { + @Override + public void onSuccess(Void response) { + result.success(true); + } + + @Override + public void onFailure(GoogleJsonError googleJsonError) { + DnsException serviceException = new DnsException(googleJsonError); + if (serviceException.code() == HTTP_NOT_FOUND) { + result.success(false); + return; + } + result.error(serviceException); + } + }; + return callback; + } + + /** + * A joint callback for both "get zone" and "create zone" operation. + */ + private DnsRpc.Callback zoneCallback(final DnsOptions serviceOptions, + final DnsBatchResult result) { + DnsRpc.Callback callback = new DnsRpc.Callback() { + @Override + public void onSuccess(ManagedZone response) { + result.success(response == null ? null : Zone.fromPb(serviceOptions.service(), response)); + } + + @Override + public void onFailure(GoogleJsonError googleJsonError) { + result.error(new DnsException(googleJsonError)); + } + }; + return callback; + } + + private DnsRpc.Callback projectCallback(final DnsBatchResult result) { + DnsRpc.Callback callback = new DnsRpc.Callback() { + @Override + public void onSuccess(Project response) { + result.success(response == null ? null : ProjectInfo.fromPb(response)); + } + + @Override + public void onFailure(GoogleJsonError googleJsonError) { + result.error(new DnsException(googleJsonError)); + } + }; + return callback; } } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatchResult.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatchResult.java index 7dcba8197307..e9b6e99ad025 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatchResult.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatchResult.java @@ -1,3 +1,19 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.google.gcloud.dns; import com.google.gcloud.BatchResult; @@ -21,10 +37,10 @@ public boolean submitted() { @Override public T get() throws DnsException { - if(!submitted()) { + if (!submitted()) { throw new IllegalStateException("Batch has not been submitted yet"); } - if(error != null) { + if (error != null) { throw error; } return result; 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 94e39042fb0f..d6eabe09328b 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 @@ -982,4 +982,6 @@ public void testListZoneBatch() { DNS.delete(ZONE_EMPTY_DESCRIPTION.name()); } } + + // todo(mderka) implement tests for other batch calls } diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchResultTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchResultTest.java new file mode 100644 index 000000000000..680208bb3277 --- /dev/null +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchResultTest.java @@ -0,0 +1,71 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gcloud.dns; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; + +public class DnsBatchResultTest { + + private DnsBatchResult RESULT; + + @Before + public void setUp() { + RESULT = new DnsBatchResult<>(); + } + + @Test + public void testSuccess() { + assertFalse(RESULT.submitted()); + try { + RESULT.get(); + fail("This was not submitted yet."); + } catch (IllegalStateException ex) { + // expected + } + RESULT.success(true); + assertTrue(RESULT.get()); + } + + @Test + public void testError() { + assertFalse(RESULT.submitted()); + try { + RESULT.get(); + fail("This was not submitted yet."); + } catch (IllegalStateException ex) { + // expected + } + DnsException ex = new DnsException(new IOException("some error")); + RESULT.error(ex); + try { + RESULT.get(); + fail("This is a failed operation and should have thrown a DnsException."); + } catch (DnsException real) { + assertSame(ex, real); + } + } + + // todo(mderka) test notify when implemented +} diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java new file mode 100644 index 000000000000..bba8bae742b4 --- /dev/null +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java @@ -0,0 +1,328 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gcloud.dns; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.google.api.services.dns.model.ManagedZone; +import com.google.api.services.dns.model.ManagedZonesListResponse; +import com.google.api.services.dns.model.Project; +import com.google.gcloud.Page; +import com.google.gcloud.dns.spi.DnsRpc; + +import org.easymock.Capture; +import org.easymock.EasyMock; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.util.Map; + +public class DnsBatchTest { + + private static final String ZONE_NAME = "somezonename"; + private static final String DNS_NAME = "example.com."; + private static final String DESCRIPTION = "desc"; + private static final Integer MAX_SIZE = 20; + private static final String PAGE_TOKEN = "some token"; + + private static final ZoneInfo ZONE_INFO = ZoneInfo.of(ZONE_NAME, DNS_NAME, DESCRIPTION); + private static final Dns.ZoneOption ZONE_FIELDS = + Dns.ZoneOption.fields(Dns.ZoneField.CREATION_TIME); + private static final Dns.ProjectOption PROJECT_FIELDS = + Dns.ProjectOption.fields(Dns.ProjectField.QUOTA); + private static final Dns.ZoneListOption[] ZONE_LIST_OPTIONS = { + Dns.ZoneListOption.pageSize(MAX_SIZE), Dns.ZoneListOption.pageToken(PAGE_TOKEN), + Dns.ZoneListOption.fields(Dns.ZoneField.DESCRIPTION), + Dns.ZoneListOption.dnsName(DNS_NAME)}; + private static final ProjectInfo PROJECT_INFO = ProjectInfo.builder().build(); + + private DnsOptions optionsMock; + private DnsRpc dnsRpcMock; + private Object batchMock; + private DnsBatch dnsBatch; + private Dns dns = EasyMock.createStrictMock(Dns.class); + + @Before + public void setUp() { + optionsMock = EasyMock.createMock(DnsOptions.class); + dnsRpcMock = EasyMock.createMock(DnsRpc.class); + batchMock = EasyMock.createMock(Object.class); + EasyMock.expect(optionsMock.rpc()).andReturn(dnsRpcMock); + EasyMock.expect(dnsRpcMock.createBatch()).andReturn(batchMock); + EasyMock.replay(optionsMock); + EasyMock.replay(dnsRpcMock); + EasyMock.replay(batchMock); + dnsBatch = new DnsBatch(optionsMock); + } + + @After + public void tearDown() { + EasyMock.verify(batchMock); + EasyMock.verify(dnsRpcMock); + EasyMock.verify(optionsMock); + } + + @Test + public void testConstructor() { + assertSame(batchMock, dnsBatch.batch()); + assertSame(optionsMock, dnsBatch.options()); + assertSame(dnsRpcMock, dnsBatch.dnsRpc()); + } + + @Test + public void testListZones() { + EasyMock.reset(dnsRpcMock); + Capture> callback = Capture.newInstance(); + Capture> capturedOptions = Capture.newInstance(); + EasyMock.expect(dnsRpcMock.addToBatchListZones(EasyMock.eq(batchMock), + EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(dnsBatch); + EasyMock.replay(dnsRpcMock); + DnsBatchResult> batchResult = dnsBatch.listZones(); + assertEquals(0, capturedOptions.getValue().size()); + assertNotNull(callback.getValue()); + try { + batchResult.get(); + fail("No result available yet."); + } catch (IllegalStateException ex) { + // expected + } + batchResult.error(new DnsException(new IOException("expected"))); + try { + batchResult.get(); + fail("TShould throw a DnsException on error."); + } catch (DnsException ex) { + // expected + } + } + + @Test + public void testListZonesWithOptions() { + EasyMock.reset(dnsRpcMock); + Capture> callback = Capture.newInstance(); + Capture> capturedOptions = Capture.newInstance(); + EasyMock.expect(dnsRpcMock.addToBatchListZones(EasyMock.eq(batchMock), + EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(dnsBatch); + EasyMock.replay(dnsRpcMock); + dnsBatch.listZones(ZONE_LIST_OPTIONS); + assertNotNull(callback.getValue()); + Integer size = (Integer) capturedOptions.getValue().get(ZONE_LIST_OPTIONS[0].rpcOption()); + assertEquals(MAX_SIZE, size); + String selector = (String) capturedOptions.getValue().get(ZONE_LIST_OPTIONS[1].rpcOption()); + assertEquals(PAGE_TOKEN, selector); + selector = (String) capturedOptions.getValue().get(ZONE_LIST_OPTIONS[2].rpcOption()); + assertTrue(selector.contains(Dns.ZoneField.DESCRIPTION.selector())); + assertTrue(selector.contains(Dns.ZoneField.NAME.selector())); + selector = (String) capturedOptions.getValue().get(ZONE_LIST_OPTIONS[3].rpcOption()); + assertEquals(DNS_NAME, selector); + // cannot test callback success without ManagedZonesListResponse which is final + } + + @Test + public void testCreateZone() { + EasyMock.reset(dnsRpcMock); + Capture> callback = Capture.newInstance(); + Capture> capturedOptions = Capture.newInstance(); + Capture capturedZone = Capture.newInstance(); + EasyMock.expect(dnsRpcMock.addToBatchCreateZone(EasyMock.capture(capturedZone), + EasyMock.eq(batchMock), EasyMock.capture(callback), EasyMock.capture(capturedOptions))) + .andReturn(dnsBatch); + EasyMock.replay(dnsRpcMock); + DnsBatchResult batchResult = dnsBatch.createZone(ZONE_INFO); + assertEquals(0, capturedOptions.getValue().size()); + assertEquals(ZONE_INFO.toPb(), capturedZone.getValue()); + assertNotNull(callback.getValue()); + try { + batchResult.get(); + fail("No result available yet."); + } catch (IllegalStateException ex) { + // expected + } + // testing error here, success is tested with options + batchResult.error(new DnsException(new IOException("expected"))); + try { + batchResult.get(); + fail("TShould throw a DnsException on error."); + } catch (DnsException ex) { + // expected + } + } + + @Test + public void testCreateZoneWithOptions() { + EasyMock.reset(optionsMock); + EasyMock.expect(optionsMock.service()).andReturn(dns); + EasyMock.replay(optionsMock); + EasyMock.reset(dnsRpcMock); + Capture> callback = Capture.newInstance(); + Capture> capturedOptions = Capture.newInstance(); + Capture capturedZone = Capture.newInstance(); + EasyMock.expect(dnsRpcMock.addToBatchCreateZone(EasyMock.capture(capturedZone), + EasyMock.eq(batchMock), EasyMock.capture(callback), EasyMock.capture(capturedOptions))) + .andReturn(dnsBatch); + EasyMock.replay(dnsRpcMock); + DnsBatchResult batchResult = dnsBatch.createZone(ZONE_INFO, ZONE_FIELDS); + assertEquals(ZONE_INFO.toPb(), capturedZone.getValue()); + assertNotNull(callback.getValue()); + String selector = (String) capturedOptions.getValue().get(ZONE_FIELDS.rpcOption()); + assertTrue(selector.contains(Dns.ZoneField.CREATION_TIME.selector())); + assertTrue(selector.contains(Dns.ZoneField.NAME.selector())); + DnsRpc.Callback capturedCallback = callback.getValue(); + capturedCallback.onSuccess(ZONE_INFO.toPb()); + assertEquals(ZONE_INFO.toPb(), batchResult.get().toPb()); + } + + @Test + public void testGetZone() { + EasyMock.reset(dnsRpcMock); + Capture> callback = Capture.newInstance(); + Capture> capturedOptions = Capture.newInstance(); + EasyMock.expect(dnsRpcMock.addToBatchGetZone(EasyMock.eq(ZONE_NAME), EasyMock.eq(batchMock), + EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(dnsBatch); + EasyMock.replay(dnsRpcMock); + DnsBatchResult batchResult = dnsBatch.getZone(ZONE_NAME); + assertEquals(0, capturedOptions.getValue().size()); + assertNotNull(callback.getValue()); + try { + batchResult.get(); + fail("No result available yet."); + } catch (IllegalStateException ex) { + // expected + } + // testing error here, success is tested with options + batchResult.error(new DnsException(new IOException("expected"))); + try { + batchResult.get(); + fail("TShould throw a DnsException on error."); + } catch (DnsException ex) { + // expected + } + } + + @Test + public void testGetZoneWithOptions() { + EasyMock.reset(dnsRpcMock); + EasyMock.reset(optionsMock); + Capture> callback = Capture.newInstance(); + Capture> capturedOptions = Capture.newInstance(); + EasyMock.expect(dnsRpcMock.addToBatchGetZone(EasyMock.eq(ZONE_NAME), EasyMock.eq(batchMock), + EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(dnsBatch); + EasyMock.replay(dnsRpcMock); + DnsBatchResult batchResult = dnsBatch.getZone(ZONE_NAME, ZONE_FIELDS); + assertNotNull(callback.getValue()); + String selector = (String) capturedOptions.getValue().get(ZONE_FIELDS.rpcOption()); + assertTrue(selector.contains(Dns.ZoneField.CREATION_TIME.selector())); + assertTrue(selector.contains(Dns.ZoneField.NAME.selector())); + DnsRpc.Callback capturedCallback = callback.getValue(); + EasyMock.expect(optionsMock.service()).andReturn(dns); + EasyMock.replay(optionsMock); + capturedCallback.onSuccess(ZONE_INFO.toPb()); + assertEquals(ZONE_INFO.toPb(), batchResult.get().toPb()); + } + + @Test + public void testDeleteZone() { + EasyMock.reset(dnsRpcMock); + Capture> callback = Capture.newInstance(); + EasyMock.expect(dnsRpcMock.addToBatchDeleteZone(EasyMock.eq(ZONE_NAME), EasyMock.eq(batchMock), + EasyMock.capture(callback))).andReturn(dnsBatch); + EasyMock.replay(dnsRpcMock); + DnsBatchResult batchResult = dnsBatch.deleteZone(ZONE_NAME); + assertNotNull(callback.getValue()); + try { + batchResult.get(); + fail("No result available yet."); + } catch (IllegalStateException ex) { + // expected + } + // testing error here, success is tested with options + batchResult.error(new DnsException(new IOException("expected"))); + try { + batchResult.get(); + fail("TShould throw a DnsException on error."); + } catch (DnsException ex) { + // expected + } + } + + @Test + public void testDeleteZoneOnSuccess() { + EasyMock.reset(dnsRpcMock); + Capture> callback = Capture.newInstance(); + EasyMock.expect(dnsRpcMock.addToBatchDeleteZone(EasyMock.eq(ZONE_NAME), EasyMock.eq(batchMock), + EasyMock.capture(callback))).andReturn(dnsBatch); + EasyMock.replay(dnsRpcMock); + DnsBatchResult batchResult = dnsBatch.deleteZone(ZONE_NAME); + assertNotNull(callback.getValue()); + DnsRpc.Callback capturedCallback = callback.getValue(); + Void result = null; + capturedCallback.onSuccess(result); + assertTrue(batchResult.get()); + } + + @Test + public void testGetProject() { + EasyMock.reset(dnsRpcMock); + Capture> callback = Capture.newInstance(); + Capture> capturedOptions = Capture.newInstance(); + EasyMock.expect(dnsRpcMock.addToBatchGetProject(EasyMock.eq(batchMock), + EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(dnsBatch); + EasyMock.replay(dnsRpcMock); + DnsBatchResult batchResult = dnsBatch.getProject(); + assertEquals(0, capturedOptions.getValue().size()); + assertNotNull(callback.getValue()); + try { + batchResult.get(); + fail("No result available yet."); + } catch (IllegalStateException ex) { + // expected + } + // testing error here, success is tested with options + batchResult.error(new DnsException(new IOException("expected"))); + try { + batchResult.get(); + fail("TShould throw a DnsException on error."); + } catch (DnsException ex) { + // expected + } + } + + @Test + public void testGetProjectWithOptions() { + EasyMock.reset(dnsRpcMock); + Capture> callback = Capture.newInstance(); + Capture> capturedOptions = Capture.newInstance(); + EasyMock.expect(dnsRpcMock.addToBatchGetProject(EasyMock.eq(batchMock), + EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(dnsBatch); + EasyMock.replay(dnsRpcMock); + DnsBatchResult batchResult = dnsBatch.getProject(PROJECT_FIELDS); + assertNotNull(callback.getValue()); + String selector = (String) capturedOptions.getValue().get(PROJECT_FIELDS.rpcOption()); + assertTrue(selector.contains(Dns.ProjectField.QUOTA.selector())); + assertTrue(selector.contains(Dns.ProjectField.PROJECT_ID.selector())); + DnsRpc.Callback capturedCallback = callback.getValue(); + capturedCallback.onSuccess(PROJECT_INFO.toPb()); + assertEquals(PROJECT_INFO, batchResult.get()); + } + + // todo(mderka) test submit and other methods when implemented +} From e51b9873237fc5c5ef0e0e8f61be29bd17e0c5ee Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Tue, 5 Apr 2016 18:54:59 -0700 Subject: [PATCH 04/21] Turned BatchResult into an abstract class. --- .../java/com/google/gcloud/BatchResult.java | 49 ++++++++++++++++--- .../main/java/com/google/cloud/dns/Dns.java | 2 +- .../google/cloud/dns/spi/DefaultDnsRpc.java | 12 ++--- .../java/com/google/gcloud/dns/DnsBatch.java | 22 +++++---- .../com/google/gcloud/dns/DnsBatchResult.java | 36 ++------------ 5 files changed, 65 insertions(+), 56 deletions(-) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/BatchResult.java b/gcloud-java-core/src/main/java/com/google/gcloud/BatchResult.java index 98dae5d22aa4..2a0da48aa76a 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/BatchResult.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/BatchResult.java @@ -19,31 +19,64 @@ /** * This class holds a single result of a batch call. */ -public interface BatchResult { +public abstract class BatchResult { + + private T result; + private boolean submitted = false; + private E error; /** - * Returns {@code true} if the batch has been submitted and the result is available; {@code - * false} otherwise. + * Returns {@code true} if the batch has been submitted and the result is available; {@code false} + * otherwise. */ - boolean submitted(); + public boolean submitted() { + return submitted; + } /** * Returns result of this call. * - * @throws IllegalArgumentException if the batch has not been submitted yet + * @throws IllegalStateException if the batch has not been submitted yet * @throws E if an error occurred when processing this request */ - T get() throws E; + public T get() throws E { + if (!submitted()) { + throw new IllegalStateException("Batch has not been submitted yet"); + } + if (error != null) { + throw error; + } + return result; + } /** * Registers a callback for the batch operation. */ - void notify(Callback callback); + public void notify(Callback callback) { + // todo(mderka) implement + throw new UnsupportedOperationException("Not implemented yet"); + } + + /** + * Sets an error and makes this submitted. + */ + protected void error(E error) { + this.error = error; + this.submitted = true; + } + + /** + * Sets a result and makes this submitted. + */ + protected void success(T result) { + this.result = result; + this.submitted = true; + } /** * An interface for the batch callbacks. */ - interface Callback { + public interface Callback { /** * The method to be called when the batched operation succeeds. */ diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/Dns.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/Dns.java index cc992ddbca3e..2f8469f85635 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/Dns.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/Dns.java @@ -505,7 +505,7 @@ ChangeRequest getChangeRequest(String zoneName, String changeRequestId, Page listChangeRequests(String zoneName, ChangeRequestListOption... options); /** - * Initiates a new empty batch ready to be populated with service calls. + * Creates a new empty batch for grouping multiple service calls in one underlying RPC call. */ DnsBatch batch(); } diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java index 74396d180953..d053cd2d354c 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java @@ -264,7 +264,7 @@ public BatchRequest addToBatchListZones(Object batch, DnsRpc.Callback callback, Map options) { BatchRequest casted = (BatchRequest) batch; try { - zoneListRequest(options).queue(casted, jsonCallback(callback)); + zoneListRequest(options).queue(casted, toJsonCallback(callback)); return casted; } catch (IOException ex) { throw translate(ex); @@ -276,7 +276,7 @@ public BatchRequest addToBatchCreateZone(ManagedZone zone, Object batch, DnsRpc.Callback callback, Map options) { BatchRequest casted = (BatchRequest) batch; try { - createZoneRequest(zone, options).queue(casted, jsonCallback(callback)); + createZoneRequest(zone, options).queue(casted, toJsonCallback(callback)); return casted; } catch (IOException ex) { throw translate(ex); @@ -288,7 +288,7 @@ public BatchRequest addToBatchGetZone(String zoneName, Object batch, DnsRpc.Callback callback, Map options) { BatchRequest casted = (BatchRequest) batch; try { - getZoneRequest(zoneName, options).queue(casted, jsonCallback(callback)); + getZoneRequest(zoneName, options).queue(casted, toJsonCallback(callback)); return casted; } catch (IOException ex) { throw translate(ex); @@ -300,7 +300,7 @@ public BatchRequest addToBatchDeleteZone(String zoneName, Object batch, DnsRpc.Callback callback) { BatchRequest casted = (BatchRequest) batch; try { - deleteZoneRequest(zoneName).queue(casted, jsonCallback(callback)); + deleteZoneRequest(zoneName).queue(casted, toJsonCallback(callback)); return casted; } catch (IOException ex) { throw translate(ex); @@ -312,14 +312,14 @@ public BatchRequest addToBatchGetProject(Object batch, DnsRpc.Callback Map options) { BatchRequest casted = (BatchRequest) batch; try { - getProjectRequest(options).queue(casted, jsonCallback(callback)); + getProjectRequest(options).queue(casted, toJsonCallback(callback)); return casted; } catch (IOException ex) { throw translate(ex); } } - private static JsonBatchCallback jsonCallback(final DnsRpc.Callback callback) { + private static JsonBatchCallback toJsonCallback(final DnsRpc.Callback callback) { JsonBatchCallback jsonCallback = new JsonBatchCallback() { @Override public void onSuccess(T response, HttpHeaders httpHeaders) throws IOException { diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java index a865d21c845c..1a44d69234be 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java @@ -35,7 +35,7 @@ import java.util.Map; /** - * A batch of operations to be submitted to Google Cloud DNS using a single HTTP request. + * A batch of operations to be submitted to Google Cloud DNS using a single RPC request. */ public class DnsBatch { @@ -71,7 +71,7 @@ DnsOptions options() { public DnsBatchResult> listZones(Dns.ZoneListOption... options) { DnsBatchResult> result = new DnsBatchResult<>(); final Map optionMap = optionMap(options); - DnsRpc.Callback callback = listZonesCallback(result, optionMap); + DnsRpc.Callback callback = newListZonesCallback(result, optionMap); dnsRpc.addToBatchListZones(this.batch, callback, optionMap); return result; } @@ -85,7 +85,7 @@ public DnsBatchResult> listZones(Dns.ZoneListOption... options) { */ public DnsBatchResult createZone(ZoneInfo zone, Dns.ZoneOption... options) { DnsBatchResult result = new DnsBatchResult<>(); - DnsRpc.Callback callback = zoneCallback(this.options, result); + DnsRpc.Callback callback = newZoneCallback(this.options, result); Map optionMap = optionMap(options); dnsRpc.addToBatchCreateZone(zone.toPb(), this.batch, callback, optionMap); return result; @@ -99,7 +99,7 @@ public DnsBatchResult createZone(ZoneInfo zone, Dns.ZoneOption... options) */ public DnsBatchResult deleteZone(String zoneName) { DnsBatchResult result = new DnsBatchResult<>(); - DnsRpc.Callback callback = deleteZoneCallback(result); + DnsRpc.Callback callback = newDeleteZoneCallback(result); dnsRpc.addToBatchDeleteZone(zoneName, this.batch, callback); return result; } @@ -113,7 +113,7 @@ public DnsBatchResult deleteZone(String zoneName) { */ public DnsBatchResult getZone(String zoneName, Dns.ZoneOption... options) { DnsBatchResult result = new DnsBatchResult<>(); - DnsRpc.Callback callback = zoneCallback(this.options, result); + DnsRpc.Callback callback = newZoneCallback(this.options, result); Map optionMap = optionMap(options); dnsRpc.addToBatchGetZone(zoneName, this.batch, callback, optionMap); return result; @@ -128,12 +128,14 @@ public DnsBatchResult getZone(String zoneName, Dns.ZoneOption... options) */ public DnsBatchResult getProject(Dns.ProjectOption... options) { DnsBatchResult result = new DnsBatchResult<>(); - DnsRpc.Callback callback = projectCallback(result); + DnsRpc.Callback callback = newProjectCallback(result); Map optionMap = optionMap(options); dnsRpc.addToBatchGetProject(this.batch, callback, optionMap); return result; } + // todo(mderka) implement remaining operations + /** * Submits this batch for processing using a single HTTP request. */ @@ -150,7 +152,7 @@ public void submit() { return ImmutableMap.copyOf(temp); } - private DnsRpc.Callback listZonesCallback( + private DnsRpc.Callback newListZonesCallback( final DnsBatchResult result, final Map optionMap) { DnsRpc.Callback callback = new DnsRpc.Callback() { @Override @@ -171,7 +173,7 @@ public void onFailure(GoogleJsonError googleJsonError) { return callback; } - private DnsRpc.Callback deleteZoneCallback(final DnsBatchResult result) { + private DnsRpc.Callback newDeleteZoneCallback(final DnsBatchResult result) { DnsRpc.Callback callback = new DnsRpc.Callback() { @Override public void onSuccess(Void response) { @@ -194,7 +196,7 @@ public void onFailure(GoogleJsonError googleJsonError) { /** * A joint callback for both "get zone" and "create zone" operation. */ - private DnsRpc.Callback zoneCallback(final DnsOptions serviceOptions, + private DnsRpc.Callback newZoneCallback(final DnsOptions serviceOptions, final DnsBatchResult result) { DnsRpc.Callback callback = new DnsRpc.Callback() { @Override @@ -210,7 +212,7 @@ public void onFailure(GoogleJsonError googleJsonError) { return callback; } - private DnsRpc.Callback projectCallback(final DnsBatchResult result) { + private DnsRpc.Callback newProjectCallback(final DnsBatchResult result) { DnsRpc.Callback callback = new DnsRpc.Callback() { @Override public void onSuccess(Project response) { diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatchResult.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatchResult.java index e9b6e99ad025..68bc0d617cab 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatchResult.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatchResult.java @@ -21,44 +21,18 @@ /** * This class holds a single result of a batch call to the Cloud DNS. */ -public class DnsBatchResult implements BatchResult { - - private T result; - private boolean submitted = false; - private DnsException error; +public class DnsBatchResult extends BatchResult { DnsBatchResult() { } @Override - public boolean submitted() { - return submitted; - } - - @Override - public T get() throws DnsException { - if (!submitted()) { - throw new IllegalStateException("Batch has not been submitted yet"); - } - if (error != null) { - throw error; - } - return result; + public void error(DnsException error) { + super.error(error); } @Override - public void notify(Callback callback) { - // todo(mderka) implement - throw new UnsupportedOperationException("Not implemented yet"); - } - - void error(DnsException error) { - this.error = error; - this.submitted = true; - } - - void success(T result) { - this.result = result; - this.submitted = true; + public void success(T result) { + super.success(result); } } From 74b8158c33dfd672c9b5cce50fc1a6b3e958f8d0 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Wed, 6 Apr 2016 14:34:48 -0700 Subject: [PATCH 05/21] Created RpcBatch interface for opacity of batch. Added core unit test for BatchResult. Added javadoc for BatchResult generics. Changed prefix of newCallback methods. Fixed method javadoc. --- .../java/com/google/gcloud/BatchResult.java | 9 +- .../com/google/gcloud/BatchResultTest.java | 70 ++++++ .../google/cloud/dns/spi/DefaultDnsRpc.java | 209 ++++++++++-------- .../java/com/google/cloud/dns/spi/DnsRpc.java | 14 +- .../java/com/google/gcloud/dns/DnsBatch.java | 35 +-- .../com/google/gcloud/dns/DnsBatchResult.java | 4 +- .../com/google/gcloud/dns/spi/RpcBatch.java | 76 +++++++ .../com/google/gcloud/dns/DnsBatchTest.java | 25 ++- 8 files changed, 313 insertions(+), 129 deletions(-) create mode 100644 gcloud-java-core/src/test/java/com/google/gcloud/BatchResultTest.java create mode 100644 gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/BatchResult.java b/gcloud-java-core/src/main/java/com/google/gcloud/BatchResult.java index 2a0da48aa76a..621fbb0278cc 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/BatchResult.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/BatchResult.java @@ -16,8 +16,11 @@ package com.google.gcloud; +import static com.google.common.base.Preconditions.checkState; + /** - * This class holds a single result of a batch call. + * This class holds a single result of a batch call. {@code T} is the type of the result and + * {@code E} is the type of the service-dependent exception thrown when processing error occurs. */ public abstract class BatchResult { @@ -40,9 +43,7 @@ public boolean submitted() { * @throws E if an error occurred when processing this request */ public T get() throws E { - if (!submitted()) { - throw new IllegalStateException("Batch has not been submitted yet"); - } + checkState(submitted(), "Batch has not been submitted yet"); if (error != null) { throw error; } diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/BatchResultTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/BatchResultTest.java new file mode 100644 index 000000000000..93cf66ec118d --- /dev/null +++ b/gcloud-java-core/src/test/java/com/google/gcloud/BatchResultTest.java @@ -0,0 +1,70 @@ +/* + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gcloud; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.Before; +import org.junit.Test; + +public class BatchResultTest { + + private BatchResult RESULT; + + @Before + public void setUp() { + RESULT = new BatchResult() {}; + } + + @Test + public void testSuccess() { + assertFalse(RESULT.submitted()); + try { + RESULT.get(); + fail("This was not submitted yet."); + } catch (IllegalStateException ex) { + // expected + } + RESULT.success(true); + assertTrue(RESULT.get()); + } + + @Test + public void testError() { + assertFalse(RESULT.submitted()); + try { + RESULT.get(); + fail("This was not submitted yet."); + } catch (IllegalStateException ex) { + // expected + } + BaseServiceException ex = new BaseServiceException(0, "message", "reason", false); + RESULT.error(ex); + try { + RESULT.get(); + fail("This is a failed operation and should have thrown a DnsException."); + } catch (BaseServiceException real) { + assertSame(ex, real); + } + } + + // todo(mderka) test notify when implemented + +} diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java index d053cd2d354c..6ca4e7419104 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java @@ -55,6 +55,89 @@ public class DefaultDnsRpc implements DnsRpc { private final Dns dns; private final DnsOptions options; + private class DefaultRpcBatch implements RpcBatch { + + BatchRequest batch; + + private DefaultRpcBatch(BatchRequest batch) { + this.batch = batch; + } + + @Override + public void addToBatchListZones(DnsRpc.Callback callback, + Map options) { + try { + zoneListCall(options).queue(batch, toJsonCallback(callback)); + } catch (IOException ex) { + throw translate(ex); + } + } + + @Override + public void addToBatchCreateZone(ManagedZone zone, DnsRpc.Callback callback, + Map options) { + try { + createZoneCall(zone, options).queue(batch, toJsonCallback(callback)); + } catch (IOException ex) { + throw translate(ex); + } + } + + @Override + public void addToBatchGetZone(String zoneName, DnsRpc.Callback callback, + Map options) { + try { + getZoneCall(zoneName, options).queue(batch, toJsonCallback(callback)); + } catch (IOException ex) { + throw translate(ex); + } + } + + @Override + public void addToBatchDeleteZone(String zoneName, DnsRpc.Callback callback) { + try { + deleteZoneCall(zoneName).queue(batch, toJsonCallback(callback)); + } catch (IOException ex) { + throw translate(ex); + } + } + + @Override + public void addToBatchGetProject(DnsRpc.Callback callback, + Map options) { + try { + getProjectCall(options).queue(batch, toJsonCallback(callback)); + } catch (IOException ex) { + throw translate(ex); + } + } + + private JsonBatchCallback toJsonCallback(final DnsRpc.Callback callback) { + JsonBatchCallback jsonCallback = new JsonBatchCallback() { + @Override + public void onSuccess(T response, HttpHeaders httpHeaders) throws IOException { + callback.onSuccess(response); + } + + @Override + public void onFailure(GoogleJsonError googleJsonError, HttpHeaders httpHeaders) + throws IOException { + callback.onFailure(googleJsonError); + } + }; + return jsonCallback; + } + + @Override + public void submitBatch() { + try { + batch.execute(); + } catch (IOException ex) { + throw translate(ex); + } + } + } + private static DnsException translate(IOException exception) { return new DnsException(exception); } @@ -75,13 +158,13 @@ public DefaultDnsRpc(DnsOptions options) { @Override public ManagedZone create(ManagedZone zone, Map options) throws DnsException { try { - return createZoneRequest(zone, options).execute(); + return createZoneCall(zone, options).execute(); } catch (IOException ex) { throw translate(ex); } } - private Dns.ManagedZones.Create createZoneRequest(ManagedZone zone, Map options) + private Dns.ManagedZones.Create createZoneCall(ManagedZone zone, Map options) throws IOException { return dns.managedZones() .create(this.options.projectId(), zone) @@ -92,7 +175,7 @@ private Dns.ManagedZones.Create createZoneRequest(ManagedZone zone, Map options) throws DnsException { // just fields option try { - return getZoneRequest(zoneName, options).execute(); + return getZoneCall(zoneName, options).execute(); } catch (IOException ex) { DnsException serviceException = translate(ex); if (serviceException.code() == HTTP_NOT_FOUND) { @@ -102,7 +185,7 @@ public ManagedZone getZone(String zoneName, Map options) throws DnsEx } } - private Dns.ManagedZones.Get getZoneRequest(String zoneName, Map options) + private Dns.ManagedZones.Get getZoneCall(String zoneName, Map options) throws IOException { return dns.managedZones() .get(this.options.projectId(), zoneName) @@ -113,14 +196,14 @@ private Dns.ManagedZones.Get getZoneRequest(String zoneName, Map opti public ListResult listZones(Map options) throws DnsException { // fields, page token, page size try { - ManagedZonesListResponse zoneList = zoneListRequest(options).execute(); + ManagedZonesListResponse zoneList = zoneListCall(options).execute(); return of(zoneList.getNextPageToken(), zoneList.getManagedZones()); } catch (IOException ex) { throw translate(ex); } } - private Dns.ManagedZones.List zoneListRequest(Map options) throws IOException { + private Dns.ManagedZones.List zoneListCall(Map options) throws IOException { return dns.managedZones().list(this.options.projectId()) .setFields(FIELDS.getString(options)) .setMaxResults(PAGE_SIZE.getInt(options)) @@ -131,7 +214,7 @@ private Dns.ManagedZones.List zoneListRequest(Map options) thr @Override public boolean deleteZone(String zoneName) throws DnsException { try { - deleteZoneRequest(zoneName).execute(); + deleteZoneCall(zoneName).execute(); return true; } catch (IOException ex) { DnsException serviceException = translate(ex); @@ -142,7 +225,7 @@ public boolean deleteZone(String zoneName) throws DnsException { } } - private Dns.ManagedZones.Delete deleteZoneRequest(String zoneName) throws IOException { + private Dns.ManagedZones.Delete deleteZoneCall(String zoneName) throws IOException { return dns.managedZones().delete(this.options.projectId(), zoneName); } @@ -151,14 +234,14 @@ public ListResult listRecordSets(String zoneName, Map options) + private Dns.ResourceRecordSets.List listDnsRecordsCall(String zoneName, Map options) throws IOException { // options are fields, page token, dns name, type return dns.resourceRecordSets() @@ -173,13 +256,13 @@ private Dns.ResourceRecordSets.List listDnsRecordsRequest(String zoneName, Map options) throws DnsException { try { - return getProjectRequest(options).execute(); + return getProjectCall(options).execute(); } catch (IOException ex) { throw translate(ex); } } - private Dns.Projects.Get getProjectRequest(Map options) throws IOException { + private Dns.Projects.Get getProjectCall(Map options) throws IOException { return dns.projects().get(this.options.projectId()).setFields(FIELDS.getString(options)); } @@ -187,13 +270,13 @@ private Dns.Projects.Get getProjectRequest(Map options) throws IOExce public Change applyChangeRequest(String zoneName, Change changeRequest, Map options) throws DnsException { try { - return applyChangeRequestRequest(zoneName, changeRequest, options).execute(); + return applyChangeRequestCall(zoneName, changeRequest, options).execute(); } catch (IOException ex) { throw translate(ex); } } - private Dns.Changes.Create applyChangeRequestRequest(String zoneName, Change changeRequest, + private Dns.Changes.Create applyChangeRequestCall(String zoneName, Change changeRequest, Map options) throws IOException { return dns.changes() .create(this.options.projectId(), zoneName, changeRequest) @@ -204,7 +287,7 @@ private Dns.Changes.Create applyChangeRequestRequest(String zoneName, Change cha public Change getChangeRequest(String zoneName, String changeRequestId, Map options) throws DnsException { try { - return getChangeRequestRequest(zoneName, changeRequestId, options).execute(); + return getChangeRequestCall(zoneName, changeRequestId, options).execute(); } catch (IOException ex) { DnsException serviceException = translate(ex); if (serviceException.code() == HTTP_NOT_FOUND) { @@ -220,8 +303,7 @@ public Change getChangeRequest(String zoneName, String changeRequestId, Map options) throws IOException { return dns.changes() .get(this.options.projectId(), zoneName, changeRequestId) @@ -232,15 +314,14 @@ private Dns.Changes.Get getChangeRequestRequest(String zoneName, String changeRe public ListResult listChangeRequests(String zoneName, Map options) throws DnsException { try { - ChangesListResponse response = listChangeRequestsRequest(zoneName, options).execute(); + ChangesListResponse response = listChangeRequestsCall(zoneName, options).execute(); return of(response.getNextPageToken(), response.getChanges()); } catch (IOException ex) { throw translate(ex); } } - // todo(mderka) rename - private Dns.Changes.List listChangeRequestsRequest(String zoneName, Map options) + private Dns.Changes.List listChangeRequestsCall(String zoneName, Map options) throws IOException { // options are fields, page token, page size, sort order Dns.Changes.List request = dns.changes().list(this.options.projectId(), zoneName) @@ -255,92 +336,46 @@ private Dns.Changes.List listChangeRequestsRequest(String zoneName, Map callback, Map options) { - BatchRequest casted = (BatchRequest) batch; - try { - zoneListRequest(options).queue(casted, toJsonCallback(callback)); - return casted; - } catch (IOException ex) { - throw translate(ex); - } + batch.addToBatchListZones(callback, options); + return batch; } @Override - public BatchRequest addToBatchCreateZone(ManagedZone zone, Object batch, - DnsRpc.Callback callback, Map options) { - BatchRequest casted = (BatchRequest) batch; - try { - createZoneRequest(zone, options).queue(casted, toJsonCallback(callback)); - return casted; - } catch (IOException ex) { - throw translate(ex); - } + public RpcBatch addToBatchCreateZone(ManagedZone zone, RpcBatch batch, + Callback callback, Map options) { + batch.addToBatchCreateZone(zone, callback, options); + return batch; } @Override - public BatchRequest addToBatchGetZone(String zoneName, Object batch, - DnsRpc.Callback callback, Map options) { - BatchRequest casted = (BatchRequest) batch; - try { - getZoneRequest(zoneName, options).queue(casted, toJsonCallback(callback)); - return casted; - } catch (IOException ex) { - throw translate(ex); - } + public RpcBatch addToBatchGetZone(String zoneName, RpcBatch batch, Callback callback, + Map options) { + batch.addToBatchGetZone(zoneName, callback, options); + return batch; } @Override - public BatchRequest addToBatchDeleteZone(String zoneName, Object batch, - DnsRpc.Callback callback) { - BatchRequest casted = (BatchRequest) batch; - try { - deleteZoneRequest(zoneName).queue(casted, toJsonCallback(callback)); - return casted; - } catch (IOException ex) { - throw translate(ex); - } + public RpcBatch addToBatchDeleteZone(String zoneName, RpcBatch batch, Callback callback) { + batch.addToBatchDeleteZone(zoneName, callback); + return batch; } @Override - public BatchRequest addToBatchGetProject(Object batch, DnsRpc.Callback callback, + public RpcBatch addToBatchGetProject(RpcBatch batch, Callback callback, Map options) { - BatchRequest casted = (BatchRequest) batch; - try { - getProjectRequest(options).queue(casted, toJsonCallback(callback)); - return casted; - } catch (IOException ex) { - throw translate(ex); - } - } - - private static JsonBatchCallback toJsonCallback(final DnsRpc.Callback callback) { - JsonBatchCallback jsonCallback = new JsonBatchCallback() { - @Override - public void onSuccess(T response, HttpHeaders httpHeaders) throws IOException { - callback.onSuccess(response); - } - - @Override - public void onFailure(GoogleJsonError googleJsonError, HttpHeaders httpHeaders) - throws IOException { - callback.onFailure(googleJsonError); - } - }; - return jsonCallback; + batch.addToBatchGetProject(callback, options); + return batch; } @Override - public void submitBatch(Object batchRequest) { - try { - ((BatchRequest) batchRequest).execute(); - } catch (IOException ex) { - throw translate(ex); - } + public void submitBatch(RpcBatch batch) { + batch.submitBatch(); } } diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java index dbdc0d60d8a2..891b2f3f32db 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java @@ -193,7 +193,7 @@ ListResult listChangeRequests(String zoneName, Map options) /** * Initializes an empty batch. */ - Object createBatch(); + RpcBatch createBatch(); /** * Prepares a call to "list zones" and adds it to the batch with the provided {@code callback} and @@ -201,7 +201,7 @@ ListResult listChangeRequests(String zoneName, Map options) * * @return the updated batch */ - Object addToBatchListZones(Object batch, + RpcBatch addToBatchListZones(RpcBatch batch, Callback callback, Map options); /** @@ -210,7 +210,7 @@ Object addToBatchListZones(Object batch, * * @return the updated batch */ - Object addToBatchCreateZone(ManagedZone zone, Object batch, + RpcBatch addToBatchCreateZone(ManagedZone zone, RpcBatch batch, Callback callback, Map options); /** @@ -219,7 +219,7 @@ Object addToBatchCreateZone(ManagedZone zone, Object batch, * * @return the updated batch */ - Object addToBatchGetZone(String zoneName, Object batch, Callback callback, + RpcBatch addToBatchGetZone(String zoneName, RpcBatch batch, Callback callback, Map options); /** @@ -228,7 +228,7 @@ Object addToBatchGetZone(String zoneName, Object batch, Callback ca * * @return the updated batch */ - Object addToBatchDeleteZone(String zoneName, Object batch, Callback callback); + RpcBatch addToBatchDeleteZone(String zoneName, RpcBatch batch, Callback callback); /** * Prepares a call to "get project" and adds it to the batch with the provided {@code callback} @@ -236,11 +236,11 @@ Object addToBatchGetZone(String zoneName, Object batch, Callback ca * * @return the updated batch */ - Object addToBatchGetProject(Object batch, Callback callback, + RpcBatch addToBatchGetProject(RpcBatch batch, Callback callback, Map options); /** * Submits a batch of requests for processing using a single HTTP request to Cloud DNS. */ - void submitBatch(Object batch); + void submitBatch(RpcBatch batch); } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java index 1a44d69234be..dcada8c5fc05 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java @@ -30,6 +30,7 @@ import com.google.gcloud.Page; import com.google.gcloud.PageImpl; import com.google.gcloud.dns.spi.DnsRpc; +import com.google.gcloud.dns.spi.RpcBatch; import java.util.List; import java.util.Map; @@ -39,7 +40,7 @@ */ public class DnsBatch { - private final Object batch; // DnsBatch + private RpcBatch batch; private final DnsRpc dnsRpc; private final DnsOptions options; @@ -71,8 +72,8 @@ DnsOptions options() { public DnsBatchResult> listZones(Dns.ZoneListOption... options) { DnsBatchResult> result = new DnsBatchResult<>(); final Map optionMap = optionMap(options); - DnsRpc.Callback callback = newListZonesCallback(result, optionMap); - dnsRpc.addToBatchListZones(this.batch, callback, optionMap); + DnsRpc.Callback callback = createListZonesCallback(result, optionMap); + this.batch = dnsRpc.addToBatchListZones(this.batch, callback, optionMap); return result; } @@ -85,9 +86,9 @@ public DnsBatchResult> listZones(Dns.ZoneListOption... options) { */ public DnsBatchResult createZone(ZoneInfo zone, Dns.ZoneOption... options) { DnsBatchResult result = new DnsBatchResult<>(); - DnsRpc.Callback callback = newZoneCallback(this.options, result); + DnsRpc.Callback callback = createZoneCallback(this.options, result); Map optionMap = optionMap(options); - dnsRpc.addToBatchCreateZone(zone.toPb(), this.batch, callback, optionMap); + this.batch = dnsRpc.addToBatchCreateZone(zone.toPb(), this.batch, callback, optionMap); return result; } @@ -99,13 +100,13 @@ public DnsBatchResult createZone(ZoneInfo zone, Dns.ZoneOption... options) */ public DnsBatchResult deleteZone(String zoneName) { DnsBatchResult result = new DnsBatchResult<>(); - DnsRpc.Callback callback = newDeleteZoneCallback(result); - dnsRpc.addToBatchDeleteZone(zoneName, this.batch, callback); + DnsRpc.Callback callback = createDeleteZoneCallback(result); + this.batch = dnsRpc.addToBatchDeleteZone(zoneName, this.batch, callback); return result; } /** - * Adds a request representing the "create zone" operation to this batch. The {@code options} can + * Adds a request representing the "get zone" operation to this batch. The {@code options} can * be used to restrict the fields returned in the same way as for {@link Dns#getZone(String, * Dns.ZoneOption...)}. The returned {@link DnsBatchResult} will return the requested {@link Zone} * upon calling {@link DnsBatchResult#get()} on successful completion, {@code null} if no such @@ -113,24 +114,24 @@ public DnsBatchResult deleteZone(String zoneName) { */ public DnsBatchResult getZone(String zoneName, Dns.ZoneOption... options) { DnsBatchResult result = new DnsBatchResult<>(); - DnsRpc.Callback callback = newZoneCallback(this.options, result); + DnsRpc.Callback callback = createZoneCallback(this.options, result); Map optionMap = optionMap(options); - dnsRpc.addToBatchGetZone(zoneName, this.batch, callback, optionMap); + this.batch = dnsRpc.addToBatchGetZone(zoneName, this.batch, callback, optionMap); return result; } /** * Adds a request representing the "get project" operation to this batch. The {@code options} can * be used to restrict the fields returned in the same way as for {@link - * Dns#getProject(Dns.ProjectOption...)} The returned {@link DnsBatchResult} will return the + * Dns#getProject(Dns.ProjectOption...)}. The returned {@link DnsBatchResult} will return the * requested {@link ProjectInfo} upon calling {@link DnsBatchResult#get()} on successful * completion, or it will throw a {@link DnsException} if the operation failed. */ public DnsBatchResult getProject(Dns.ProjectOption... options) { DnsBatchResult result = new DnsBatchResult<>(); - DnsRpc.Callback callback = newProjectCallback(result); + DnsRpc.Callback callback = createProjectCallback(result); Map optionMap = optionMap(options); - dnsRpc.addToBatchGetProject(this.batch, callback, optionMap); + this.batch = dnsRpc.addToBatchGetProject(this.batch, callback, optionMap); return result; } @@ -152,7 +153,7 @@ public void submit() { return ImmutableMap.copyOf(temp); } - private DnsRpc.Callback newListZonesCallback( + private DnsRpc.Callback createListZonesCallback( final DnsBatchResult result, final Map optionMap) { DnsRpc.Callback callback = new DnsRpc.Callback() { @Override @@ -173,7 +174,7 @@ public void onFailure(GoogleJsonError googleJsonError) { return callback; } - private DnsRpc.Callback newDeleteZoneCallback(final DnsBatchResult result) { + private DnsRpc.Callback createDeleteZoneCallback(final DnsBatchResult result) { DnsRpc.Callback callback = new DnsRpc.Callback() { @Override public void onSuccess(Void response) { @@ -196,7 +197,7 @@ public void onFailure(GoogleJsonError googleJsonError) { /** * A joint callback for both "get zone" and "create zone" operation. */ - private DnsRpc.Callback newZoneCallback(final DnsOptions serviceOptions, + private DnsRpc.Callback createZoneCallback(final DnsOptions serviceOptions, final DnsBatchResult result) { DnsRpc.Callback callback = new DnsRpc.Callback() { @Override @@ -212,7 +213,7 @@ public void onFailure(GoogleJsonError googleJsonError) { return callback; } - private DnsRpc.Callback newProjectCallback(final DnsBatchResult result) { + private DnsRpc.Callback createProjectCallback(final DnsBatchResult result) { DnsRpc.Callback callback = new DnsRpc.Callback() { @Override public void onSuccess(Project response) { diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatchResult.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatchResult.java index 68bc0d617cab..b72ac34bdaed 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatchResult.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatchResult.java @@ -27,12 +27,12 @@ public class DnsBatchResult extends BatchResult { } @Override - public void error(DnsException error) { + protected void error(DnsException error) { super.error(error); } @Override - public void success(T result) { + protected void success(T result) { super.success(result); } } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java new file mode 100644 index 000000000000..1899ebb2f48c --- /dev/null +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java @@ -0,0 +1,76 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gcloud.dns.spi; + +import com.google.api.services.dns.model.ManagedZone; +import com.google.api.services.dns.model.ManagedZonesListResponse; +import com.google.api.services.dns.model.Project; + +import java.util.Map; + +/** + * An interface for the collection of batch operations. + */ +public interface RpcBatch { + + /** + * Adds a call to "list zones" to the batch with the provided {@code callback} and {@code + * options}. + * + * @return the updated batch + */ + void addToBatchListZones(DnsRpc.Callback callback, + Map options); + + /** + * Adds a call to "create zone" to the batch with the provided {@code callback} and {@code + * options}. + * + * @return the updated batch + */ + void addToBatchCreateZone(ManagedZone zone, DnsRpc.Callback callback, + Map options); + + /** + * Adds a call to "get zone" to the batch with the provided {@code callback} and {@code options}. + * + * @return the updated batch + */ + void addToBatchGetZone(String zoneName, DnsRpc.Callback callback, + Map options); + + /** + * Adds a call to "delete zone" to the batch with the provided {@code callback} and {@code + * options}. + * + * @return the updated batch + */ + void addToBatchGetProject(DnsRpc.Callback callback, + Map options); + + /** + * Adds a call to "get project" to the batch with the provided {@code callback} and {@code + * options}. + */ + void addToBatchDeleteZone(String zoneName, DnsRpc.Callback callback); + + /** + * Submits a batch of requests for processing using a single HTTP request to Cloud DNS. + */ + void submitBatch(); +} + diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java index bba8bae742b4..614de61d1ad2 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java @@ -27,6 +27,7 @@ import com.google.api.services.dns.model.Project; import com.google.gcloud.Page; import com.google.gcloud.dns.spi.DnsRpc; +import com.google.gcloud.dns.spi.RpcBatch; import org.easymock.Capture; import org.easymock.EasyMock; @@ -58,7 +59,7 @@ public class DnsBatchTest { private DnsOptions optionsMock; private DnsRpc dnsRpcMock; - private Object batchMock; + private RpcBatch batchMock; private DnsBatch dnsBatch; private Dns dns = EasyMock.createStrictMock(Dns.class); @@ -66,7 +67,7 @@ public class DnsBatchTest { public void setUp() { optionsMock = EasyMock.createMock(DnsOptions.class); dnsRpcMock = EasyMock.createMock(DnsRpc.class); - batchMock = EasyMock.createMock(Object.class); + batchMock = EasyMock.createMock(RpcBatch.class); EasyMock.expect(optionsMock.rpc()).andReturn(dnsRpcMock); EasyMock.expect(dnsRpcMock.createBatch()).andReturn(batchMock); EasyMock.replay(optionsMock); @@ -95,7 +96,7 @@ public void testListZones() { Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); EasyMock.expect(dnsRpcMock.addToBatchListZones(EasyMock.eq(batchMock), - EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(dnsBatch); + EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(batchMock); EasyMock.replay(dnsRpcMock); DnsBatchResult> batchResult = dnsBatch.listZones(); assertEquals(0, capturedOptions.getValue().size()); @@ -121,7 +122,7 @@ public void testListZonesWithOptions() { Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); EasyMock.expect(dnsRpcMock.addToBatchListZones(EasyMock.eq(batchMock), - EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(dnsBatch); + EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(batchMock); EasyMock.replay(dnsRpcMock); dnsBatch.listZones(ZONE_LIST_OPTIONS); assertNotNull(callback.getValue()); @@ -145,7 +146,7 @@ public void testCreateZone() { Capture capturedZone = Capture.newInstance(); EasyMock.expect(dnsRpcMock.addToBatchCreateZone(EasyMock.capture(capturedZone), EasyMock.eq(batchMock), EasyMock.capture(callback), EasyMock.capture(capturedOptions))) - .andReturn(dnsBatch); + .andReturn(batchMock); EasyMock.replay(dnsRpcMock); DnsBatchResult batchResult = dnsBatch.createZone(ZONE_INFO); assertEquals(0, capturedOptions.getValue().size()); @@ -178,7 +179,7 @@ public void testCreateZoneWithOptions() { Capture capturedZone = Capture.newInstance(); EasyMock.expect(dnsRpcMock.addToBatchCreateZone(EasyMock.capture(capturedZone), EasyMock.eq(batchMock), EasyMock.capture(callback), EasyMock.capture(capturedOptions))) - .andReturn(dnsBatch); + .andReturn(batchMock); EasyMock.replay(dnsRpcMock); DnsBatchResult batchResult = dnsBatch.createZone(ZONE_INFO, ZONE_FIELDS); assertEquals(ZONE_INFO.toPb(), capturedZone.getValue()); @@ -197,7 +198,7 @@ public void testGetZone() { Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); EasyMock.expect(dnsRpcMock.addToBatchGetZone(EasyMock.eq(ZONE_NAME), EasyMock.eq(batchMock), - EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(dnsBatch); + EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(batchMock); EasyMock.replay(dnsRpcMock); DnsBatchResult batchResult = dnsBatch.getZone(ZONE_NAME); assertEquals(0, capturedOptions.getValue().size()); @@ -225,7 +226,7 @@ public void testGetZoneWithOptions() { Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); EasyMock.expect(dnsRpcMock.addToBatchGetZone(EasyMock.eq(ZONE_NAME), EasyMock.eq(batchMock), - EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(dnsBatch); + EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(batchMock); EasyMock.replay(dnsRpcMock); DnsBatchResult batchResult = dnsBatch.getZone(ZONE_NAME, ZONE_FIELDS); assertNotNull(callback.getValue()); @@ -244,7 +245,7 @@ public void testDeleteZone() { EasyMock.reset(dnsRpcMock); Capture> callback = Capture.newInstance(); EasyMock.expect(dnsRpcMock.addToBatchDeleteZone(EasyMock.eq(ZONE_NAME), EasyMock.eq(batchMock), - EasyMock.capture(callback))).andReturn(dnsBatch); + EasyMock.capture(callback))).andReturn(batchMock); EasyMock.replay(dnsRpcMock); DnsBatchResult batchResult = dnsBatch.deleteZone(ZONE_NAME); assertNotNull(callback.getValue()); @@ -269,7 +270,7 @@ public void testDeleteZoneOnSuccess() { EasyMock.reset(dnsRpcMock); Capture> callback = Capture.newInstance(); EasyMock.expect(dnsRpcMock.addToBatchDeleteZone(EasyMock.eq(ZONE_NAME), EasyMock.eq(batchMock), - EasyMock.capture(callback))).andReturn(dnsBatch); + EasyMock.capture(callback))).andReturn(batchMock); EasyMock.replay(dnsRpcMock); DnsBatchResult batchResult = dnsBatch.deleteZone(ZONE_NAME); assertNotNull(callback.getValue()); @@ -285,7 +286,7 @@ public void testGetProject() { Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); EasyMock.expect(dnsRpcMock.addToBatchGetProject(EasyMock.eq(batchMock), - EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(dnsBatch); + EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(batchMock); EasyMock.replay(dnsRpcMock); DnsBatchResult batchResult = dnsBatch.getProject(); assertEquals(0, capturedOptions.getValue().size()); @@ -312,7 +313,7 @@ public void testGetProjectWithOptions() { Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); EasyMock.expect(dnsRpcMock.addToBatchGetProject(EasyMock.eq(batchMock), - EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(dnsBatch); + EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(batchMock); EasyMock.replay(dnsRpcMock); DnsBatchResult batchResult = dnsBatch.getProject(PROJECT_FIELDS); assertNotNull(callback.getValue()); From 5415913ebfc92efd6e1fd57de01c6e8b80f00740 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Wed, 6 Apr 2016 15:59:14 -0700 Subject: [PATCH 06/21] Fixed documentation. --- .../src/main/java/com/google/cloud/dns/spi/DnsRpc.java | 2 +- .../src/main/java/com/google/gcloud/dns/spi/RpcBatch.java | 8 -------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java index 891b2f3f32db..c7a94fcbfeec 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java @@ -191,7 +191,7 @@ ListResult listChangeRequests(String zoneName, Map options) throws DnsException; /** - * Initializes an empty batch. + * Creates an empty batch. */ RpcBatch createBatch(); diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java index 1899ebb2f48c..d4351b2a5960 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java @@ -30,8 +30,6 @@ public interface RpcBatch { /** * Adds a call to "list zones" to the batch with the provided {@code callback} and {@code * options}. - * - * @return the updated batch */ void addToBatchListZones(DnsRpc.Callback callback, Map options); @@ -39,16 +37,12 @@ void addToBatchListZones(DnsRpc.Callback callback, /** * Adds a call to "create zone" to the batch with the provided {@code callback} and {@code * options}. - * - * @return the updated batch */ void addToBatchCreateZone(ManagedZone zone, DnsRpc.Callback callback, Map options); /** * Adds a call to "get zone" to the batch with the provided {@code callback} and {@code options}. - * - * @return the updated batch */ void addToBatchGetZone(String zoneName, DnsRpc.Callback callback, Map options); @@ -56,8 +50,6 @@ void addToBatchGetZone(String zoneName, DnsRpc.Callback callback, /** * Adds a call to "delete zone" to the batch with the provided {@code callback} and {@code * options}. - * - * @return the updated batch */ void addToBatchGetProject(DnsRpc.Callback callback, Map options); From ab7cd0fcf69368d99c3877bcbc46e2ca1c5cff8e Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Wed, 6 Apr 2016 17:21:16 -0700 Subject: [PATCH 07/21] Removed addToBatch methods from RPC. --- .../google/cloud/dns/spi/DefaultDnsRpc.java | 51 ++----------- .../java/com/google/cloud/dns/spi/DnsRpc.java | 49 ------------ .../java/com/google/gcloud/dns/DnsBatch.java | 16 ++-- .../com/google/gcloud/dns/spi/RpcBatch.java | 12 +-- .../com/google/gcloud/dns/DnsBatchTest.java | 76 +++++++++---------- 5 files changed, 54 insertions(+), 150 deletions(-) diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java index 6ca4e7419104..d36c8cb2169d 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java @@ -64,7 +64,7 @@ private DefaultRpcBatch(BatchRequest batch) { } @Override - public void addToBatchListZones(DnsRpc.Callback callback, + public void addListZones(DnsRpc.Callback callback, Map options) { try { zoneListCall(options).queue(batch, toJsonCallback(callback)); @@ -74,7 +74,7 @@ public void addToBatchListZones(DnsRpc.Callback callba } @Override - public void addToBatchCreateZone(ManagedZone zone, DnsRpc.Callback callback, + public void addCreateZone(ManagedZone zone, DnsRpc.Callback callback, Map options) { try { createZoneCall(zone, options).queue(batch, toJsonCallback(callback)); @@ -84,7 +84,7 @@ public void addToBatchCreateZone(ManagedZone zone, DnsRpc.Callback } @Override - public void addToBatchGetZone(String zoneName, DnsRpc.Callback callback, + public void addGetZone(String zoneName, DnsRpc.Callback callback, Map options) { try { getZoneCall(zoneName, options).queue(batch, toJsonCallback(callback)); @@ -94,7 +94,7 @@ public void addToBatchGetZone(String zoneName, DnsRpc.Callback call } @Override - public void addToBatchDeleteZone(String zoneName, DnsRpc.Callback callback) { + public void addDeleteZone(String zoneName, DnsRpc.Callback callback) { try { deleteZoneCall(zoneName).queue(batch, toJsonCallback(callback)); } catch (IOException ex) { @@ -103,7 +103,7 @@ public void addToBatchDeleteZone(String zoneName, DnsRpc.Callback callback } @Override - public void addToBatchGetProject(DnsRpc.Callback callback, + public void addGetProject(DnsRpc.Callback callback, Map options) { try { getProjectCall(options).queue(batch, toJsonCallback(callback)); @@ -129,7 +129,7 @@ public void onFailure(GoogleJsonError googleJsonError, HttpHeaders httpHeaders) } @Override - public void submitBatch() { + public void submit() { try { batch.execute(); } catch (IOException ex) { @@ -339,43 +339,4 @@ private Dns.Changes.List listChangeRequestsCall(String zoneName, Map public RpcBatch createBatch() { return new DefaultRpcBatch(dns.batch()); } - - @Override - public RpcBatch addToBatchListZones(RpcBatch batch, - DnsRpc.Callback callback, Map options) { - batch.addToBatchListZones(callback, options); - return batch; - } - - @Override - public RpcBatch addToBatchCreateZone(ManagedZone zone, RpcBatch batch, - Callback callback, Map options) { - batch.addToBatchCreateZone(zone, callback, options); - return batch; - } - - @Override - public RpcBatch addToBatchGetZone(String zoneName, RpcBatch batch, Callback callback, - Map options) { - batch.addToBatchGetZone(zoneName, callback, options); - return batch; - } - - @Override - public RpcBatch addToBatchDeleteZone(String zoneName, RpcBatch batch, Callback callback) { - batch.addToBatchDeleteZone(zoneName, callback); - return batch; - } - - @Override - public RpcBatch addToBatchGetProject(RpcBatch batch, Callback callback, - Map options) { - batch.addToBatchGetProject(callback, options); - return batch; - } - - @Override - public void submitBatch(RpcBatch batch) { - batch.submitBatch(); - } } diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java index c7a94fcbfeec..eca0404d8e91 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java @@ -194,53 +194,4 @@ ListResult listChangeRequests(String zoneName, Map options) * Creates an empty batch. */ RpcBatch createBatch(); - - /** - * Prepares a call to "list zones" and adds it to the batch with the provided {@code callback} and - * {@code options}. - * - * @return the updated batch - */ - RpcBatch addToBatchListZones(RpcBatch batch, - Callback callback, Map options); - - /** - * Prepares a call to "create zone" and adds it to the batch with the provided {@code callback} - * and {@code options}. - * - * @return the updated batch - */ - RpcBatch addToBatchCreateZone(ManagedZone zone, RpcBatch batch, - Callback callback, Map options); - - /** - * Prepares a call to "get zone" and adds it to the batch with the provided {@code callback} and - * {@code options}. - * - * @return the updated batch - */ - RpcBatch addToBatchGetZone(String zoneName, RpcBatch batch, Callback callback, - Map options); - - /** - * Prepares a call to "delete zone" and adds it to the batch with the provided {@code callback} - * and {@code options}. - * - * @return the updated batch - */ - RpcBatch addToBatchDeleteZone(String zoneName, RpcBatch batch, Callback callback); - - /** - * Prepares a call to "get project" and adds it to the batch with the provided {@code callback} - * and {@code options}. - * - * @return the updated batch - */ - RpcBatch addToBatchGetProject(RpcBatch batch, Callback callback, - Map options); - - /** - * Submits a batch of requests for processing using a single HTTP request to Cloud DNS. - */ - void submitBatch(RpcBatch batch); } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java index dcada8c5fc05..535649c82604 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java @@ -73,7 +73,7 @@ public DnsBatchResult> listZones(Dns.ZoneListOption... options) { DnsBatchResult> result = new DnsBatchResult<>(); final Map optionMap = optionMap(options); DnsRpc.Callback callback = createListZonesCallback(result, optionMap); - this.batch = dnsRpc.addToBatchListZones(this.batch, callback, optionMap); + batch.addListZones(callback, optionMap); return result; } @@ -88,7 +88,7 @@ public DnsBatchResult createZone(ZoneInfo zone, Dns.ZoneOption... options) DnsBatchResult result = new DnsBatchResult<>(); DnsRpc.Callback callback = createZoneCallback(this.options, result); Map optionMap = optionMap(options); - this.batch = dnsRpc.addToBatchCreateZone(zone.toPb(), this.batch, callback, optionMap); + batch.addCreateZone(zone.toPb(), callback, optionMap); return result; } @@ -101,13 +101,13 @@ public DnsBatchResult createZone(ZoneInfo zone, Dns.ZoneOption... options) public DnsBatchResult deleteZone(String zoneName) { DnsBatchResult result = new DnsBatchResult<>(); DnsRpc.Callback callback = createDeleteZoneCallback(result); - this.batch = dnsRpc.addToBatchDeleteZone(zoneName, this.batch, callback); + batch.addDeleteZone(zoneName, callback); return result; } /** - * Adds a request representing the "get zone" operation to this batch. The {@code options} can - * be used to restrict the fields returned in the same way as for {@link Dns#getZone(String, + * Adds a request representing the "get zone" operation to this batch. The {@code options} can be + * used to restrict the fields returned in the same way as for {@link Dns#getZone(String, * Dns.ZoneOption...)}. The returned {@link DnsBatchResult} will return the requested {@link Zone} * upon calling {@link DnsBatchResult#get()} on successful completion, {@code null} if no such * zone exists, or it will throw a {@link DnsException} if the operation failed. @@ -116,7 +116,7 @@ public DnsBatchResult getZone(String zoneName, Dns.ZoneOption... options) DnsBatchResult result = new DnsBatchResult<>(); DnsRpc.Callback callback = createZoneCallback(this.options, result); Map optionMap = optionMap(options); - this.batch = dnsRpc.addToBatchGetZone(zoneName, this.batch, callback, optionMap); + batch.addGetZone(zoneName, callback, optionMap); return result; } @@ -131,7 +131,7 @@ public DnsBatchResult getProject(Dns.ProjectOption... options) { DnsBatchResult result = new DnsBatchResult<>(); DnsRpc.Callback callback = createProjectCallback(result); Map optionMap = optionMap(options); - this.batch = dnsRpc.addToBatchGetProject(this.batch, callback, optionMap); + batch.addGetProject(callback, optionMap); return result; } @@ -141,7 +141,7 @@ public DnsBatchResult getProject(Dns.ProjectOption... options) { * Submits this batch for processing using a single HTTP request. */ public void submit() { - dnsRpc.submitBatch(batch); + batch.submit(); } private Map optionMap(AbstractOption... options) { diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java index d4351b2a5960..fddc183c32c0 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java @@ -31,38 +31,38 @@ public interface RpcBatch { * Adds a call to "list zones" to the batch with the provided {@code callback} and {@code * options}. */ - void addToBatchListZones(DnsRpc.Callback callback, + void addListZones(DnsRpc.Callback callback, Map options); /** * Adds a call to "create zone" to the batch with the provided {@code callback} and {@code * options}. */ - void addToBatchCreateZone(ManagedZone zone, DnsRpc.Callback callback, + void addCreateZone(ManagedZone zone, DnsRpc.Callback callback, Map options); /** * Adds a call to "get zone" to the batch with the provided {@code callback} and {@code options}. */ - void addToBatchGetZone(String zoneName, DnsRpc.Callback callback, + void addGetZone(String zoneName, DnsRpc.Callback callback, Map options); /** * Adds a call to "delete zone" to the batch with the provided {@code callback} and {@code * options}. */ - void addToBatchGetProject(DnsRpc.Callback callback, + void addGetProject(DnsRpc.Callback callback, Map options); /** * Adds a call to "get project" to the batch with the provided {@code callback} and {@code * options}. */ - void addToBatchDeleteZone(String zoneName, DnsRpc.Callback callback); + void addDeleteZone(String zoneName, DnsRpc.Callback callback); /** * Submits a batch of requests for processing using a single HTTP request to Cloud DNS. */ - void submitBatch(); + void submit(); } diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java index 614de61d1ad2..7363c530ef03 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java @@ -92,12 +92,11 @@ public void testConstructor() { @Test public void testListZones() { - EasyMock.reset(dnsRpcMock); + EasyMock.reset(batchMock); Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); - EasyMock.expect(dnsRpcMock.addToBatchListZones(EasyMock.eq(batchMock), - EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(batchMock); - EasyMock.replay(dnsRpcMock); + batchMock.addListZones(EasyMock.capture(callback), EasyMock.capture(capturedOptions)); + EasyMock.replay(batchMock); DnsBatchResult> batchResult = dnsBatch.listZones(); assertEquals(0, capturedOptions.getValue().size()); assertNotNull(callback.getValue()); @@ -118,12 +117,11 @@ public void testListZones() { @Test public void testListZonesWithOptions() { - EasyMock.reset(dnsRpcMock); + EasyMock.reset(batchMock); Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); - EasyMock.expect(dnsRpcMock.addToBatchListZones(EasyMock.eq(batchMock), - EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(batchMock); - EasyMock.replay(dnsRpcMock); + batchMock.addListZones(EasyMock.capture(callback), EasyMock.capture(capturedOptions)); + EasyMock.replay(batchMock); dnsBatch.listZones(ZONE_LIST_OPTIONS); assertNotNull(callback.getValue()); Integer size = (Integer) capturedOptions.getValue().get(ZONE_LIST_OPTIONS[0].rpcOption()); @@ -140,14 +138,13 @@ public void testListZonesWithOptions() { @Test public void testCreateZone() { - EasyMock.reset(dnsRpcMock); + EasyMock.reset(batchMock); Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); Capture capturedZone = Capture.newInstance(); - EasyMock.expect(dnsRpcMock.addToBatchCreateZone(EasyMock.capture(capturedZone), - EasyMock.eq(batchMock), EasyMock.capture(callback), EasyMock.capture(capturedOptions))) - .andReturn(batchMock); - EasyMock.replay(dnsRpcMock); + batchMock.addCreateZone(EasyMock.capture(capturedZone), EasyMock.capture(callback), + EasyMock.capture(capturedOptions)); + EasyMock.replay(batchMock); DnsBatchResult batchResult = dnsBatch.createZone(ZONE_INFO); assertEquals(0, capturedOptions.getValue().size()); assertEquals(ZONE_INFO.toPb(), capturedZone.getValue()); @@ -170,17 +167,16 @@ public void testCreateZone() { @Test public void testCreateZoneWithOptions() { + EasyMock.reset(batchMock); EasyMock.reset(optionsMock); EasyMock.expect(optionsMock.service()).andReturn(dns); EasyMock.replay(optionsMock); - EasyMock.reset(dnsRpcMock); Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); Capture capturedZone = Capture.newInstance(); - EasyMock.expect(dnsRpcMock.addToBatchCreateZone(EasyMock.capture(capturedZone), - EasyMock.eq(batchMock), EasyMock.capture(callback), EasyMock.capture(capturedOptions))) - .andReturn(batchMock); - EasyMock.replay(dnsRpcMock); + batchMock.addCreateZone(EasyMock.capture(capturedZone), EasyMock.capture(callback), + EasyMock.capture(capturedOptions)); + EasyMock.replay(batchMock); DnsBatchResult batchResult = dnsBatch.createZone(ZONE_INFO, ZONE_FIELDS); assertEquals(ZONE_INFO.toPb(), capturedZone.getValue()); assertNotNull(callback.getValue()); @@ -194,12 +190,12 @@ public void testCreateZoneWithOptions() { @Test public void testGetZone() { - EasyMock.reset(dnsRpcMock); + EasyMock.reset(batchMock); Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); - EasyMock.expect(dnsRpcMock.addToBatchGetZone(EasyMock.eq(ZONE_NAME), EasyMock.eq(batchMock), - EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(batchMock); - EasyMock.replay(dnsRpcMock); + batchMock.addGetZone(EasyMock.eq(ZONE_NAME), EasyMock.capture(callback), + EasyMock.capture(capturedOptions)); + EasyMock.replay(batchMock); DnsBatchResult batchResult = dnsBatch.getZone(ZONE_NAME); assertEquals(0, capturedOptions.getValue().size()); assertNotNull(callback.getValue()); @@ -221,13 +217,13 @@ public void testGetZone() { @Test public void testGetZoneWithOptions() { - EasyMock.reset(dnsRpcMock); + EasyMock.reset(batchMock); EasyMock.reset(optionsMock); Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); - EasyMock.expect(dnsRpcMock.addToBatchGetZone(EasyMock.eq(ZONE_NAME), EasyMock.eq(batchMock), - EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(batchMock); - EasyMock.replay(dnsRpcMock); + batchMock.addGetZone(EasyMock.eq(ZONE_NAME), EasyMock.capture(callback), + EasyMock.capture(capturedOptions)); + EasyMock.replay(batchMock); DnsBatchResult batchResult = dnsBatch.getZone(ZONE_NAME, ZONE_FIELDS); assertNotNull(callback.getValue()); String selector = (String) capturedOptions.getValue().get(ZONE_FIELDS.rpcOption()); @@ -242,11 +238,10 @@ public void testGetZoneWithOptions() { @Test public void testDeleteZone() { - EasyMock.reset(dnsRpcMock); + EasyMock.reset(batchMock); Capture> callback = Capture.newInstance(); - EasyMock.expect(dnsRpcMock.addToBatchDeleteZone(EasyMock.eq(ZONE_NAME), EasyMock.eq(batchMock), - EasyMock.capture(callback))).andReturn(batchMock); - EasyMock.replay(dnsRpcMock); + batchMock.addDeleteZone(EasyMock.eq(ZONE_NAME), EasyMock.capture(callback)); + EasyMock.replay(batchMock); DnsBatchResult batchResult = dnsBatch.deleteZone(ZONE_NAME); assertNotNull(callback.getValue()); try { @@ -267,11 +262,10 @@ public void testDeleteZone() { @Test public void testDeleteZoneOnSuccess() { - EasyMock.reset(dnsRpcMock); + EasyMock.reset(batchMock); Capture> callback = Capture.newInstance(); - EasyMock.expect(dnsRpcMock.addToBatchDeleteZone(EasyMock.eq(ZONE_NAME), EasyMock.eq(batchMock), - EasyMock.capture(callback))).andReturn(batchMock); - EasyMock.replay(dnsRpcMock); + batchMock.addDeleteZone(EasyMock.eq(ZONE_NAME), EasyMock.capture(callback)); + EasyMock.replay(batchMock); DnsBatchResult batchResult = dnsBatch.deleteZone(ZONE_NAME); assertNotNull(callback.getValue()); DnsRpc.Callback capturedCallback = callback.getValue(); @@ -282,12 +276,11 @@ public void testDeleteZoneOnSuccess() { @Test public void testGetProject() { - EasyMock.reset(dnsRpcMock); + EasyMock.reset(batchMock); Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); - EasyMock.expect(dnsRpcMock.addToBatchGetProject(EasyMock.eq(batchMock), - EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(batchMock); - EasyMock.replay(dnsRpcMock); + batchMock.addGetProject(EasyMock.capture(callback), EasyMock.capture(capturedOptions)); + EasyMock.replay(batchMock); DnsBatchResult batchResult = dnsBatch.getProject(); assertEquals(0, capturedOptions.getValue().size()); assertNotNull(callback.getValue()); @@ -309,12 +302,11 @@ public void testGetProject() { @Test public void testGetProjectWithOptions() { - EasyMock.reset(dnsRpcMock); + EasyMock.reset(batchMock); Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); - EasyMock.expect(dnsRpcMock.addToBatchGetProject(EasyMock.eq(batchMock), - EasyMock.capture(callback), EasyMock.capture(capturedOptions))).andReturn(batchMock); - EasyMock.replay(dnsRpcMock); + batchMock.addGetProject(EasyMock.capture(callback), EasyMock.capture(capturedOptions)); + EasyMock.replay(batchMock); DnsBatchResult batchResult = dnsBatch.getProject(PROJECT_FIELDS); assertNotNull(callback.getValue()); String selector = (String) capturedOptions.getValue().get(PROJECT_FIELDS.rpcOption()); From 328ae00004e0a26bf7b9b117078fa269b999c590 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Wed, 6 Apr 2016 17:37:15 -0700 Subject: [PATCH 08/21] Removed conflicts with master branch. --- .../src/main/java/com/google/gcloud/dns/DnsBatch.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java index 535649c82604..ffb77b191f34 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java @@ -144,9 +144,9 @@ public void submit() { batch.submit(); } - private Map optionMap(AbstractOption... options) { + private Map optionMap(Option... options) { Map temp = Maps.newEnumMap(DnsRpc.Option.class); - for (AbstractOption option : options) { + for (Option option : options) { Object prev = temp.put(option.rpcOption(), option.value()); checkArgument(prev == null, "Duplicate option %s", option); } From 842dc61b229d20b7e46a14a8d3dbf9522683068a Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Thu, 7 Apr 2016 12:58:33 -0700 Subject: [PATCH 09/21] Implemented batch processing for change requests and record sets. --- .../java/com/google/cloud/dns/DnsImpl.java | 11 +- .../google/cloud/dns/spi/DefaultDnsRpc.java | 57 +++++- .../java/com/google/cloud/dns/spi/DnsRpc.java | 16 -- .../java/com/google/gcloud/dns/DnsBatch.java | 192 +++++++++++++++--- .../com/google/gcloud/dns/spi/RpcBatch.java | 62 +++++- 5 files changed, 268 insertions(+), 70 deletions(-) diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java index 0a80079c7d70..4ca37b8fb82c 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java @@ -22,7 +22,6 @@ import com.google.api.services.dns.model.Change; import com.google.api.services.dns.model.ManagedZone; import com.google.api.services.dns.model.Project; -import com.google.api.services.dns.model.ManagedZonesListResponse; import com.google.api.services.dns.model.ResourceRecordSet; import com.google.common.base.Function; import com.google.common.collect.ImmutableList; @@ -64,7 +63,7 @@ public Page nextPage() { } } - private static class ChangeRequestPageFetcher implements PageImpl.NextPageFetcher { + static class ChangeRequestPageFetcher implements PageImpl.NextPageFetcher { private static final long serialVersionUID = 4473265130673029139L; private final String zoneName; @@ -85,14 +84,14 @@ public Page nextPage() { } } - private static class DnsRecordPageFetcher implements PageImpl.NextPageFetcher { + static class RecordSetPageFetcher implements PageImpl.NextPageFetcher { private static final long serialVersionUID = -6039369212511530846L; private final Map requestOptions; private final DnsOptions serviceOptions; private final String zoneName; - DnsRecordPageFetcher(String zoneName, DnsOptions serviceOptions, String cursor, + RecordSetPageFetcher(String zoneName, DnsOptions serviceOptions, String cursor, Map optionMap) { this.zoneName = zoneName; this.requestOptions = @@ -209,7 +208,7 @@ public DnsRpc.ListResult call() { Iterable recordSets = result.results() == null ? ImmutableList.of() : Iterables.transform(result.results(), RecordSet.FROM_PB_FUNCTION); - return new PageImpl<>(new DnsRecordPageFetcher(zoneName, serviceOptions, cursor, optionsMap), + return new PageImpl<>(new RecordSetPageFetcher(zoneName, serviceOptions, cursor, optionsMap), cursor, recordSets); } catch (RetryHelper.RetryHelperException e) { throw DnsException.translateAndThrow(e); @@ -322,7 +321,7 @@ public DnsBatch batch() { return new DnsBatch(this.options()); } - private Map optionMap(Option... options) { + static Map optionMap(Option... options) { Map temp = Maps.newEnumMap(DnsRpc.Option.class); for (Option option : options) { Object prev = temp.put(option.rpcOption(), option.value()); diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java index d36c8cb2169d..ef6e9a520b14 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java @@ -64,7 +64,7 @@ private DefaultRpcBatch(BatchRequest batch) { } @Override - public void addListZones(DnsRpc.Callback callback, + public void addListZones(RpcBatch.Callback callback, Map options) { try { zoneListCall(options).queue(batch, toJsonCallback(callback)); @@ -74,7 +74,7 @@ public void addListZones(DnsRpc.Callback callback, } @Override - public void addCreateZone(ManagedZone zone, DnsRpc.Callback callback, + public void addCreateZone(ManagedZone zone, RpcBatch.Callback callback, Map options) { try { createZoneCall(zone, options).queue(batch, toJsonCallback(callback)); @@ -84,7 +84,7 @@ public void addCreateZone(ManagedZone zone, DnsRpc.Callback callbac } @Override - public void addGetZone(String zoneName, DnsRpc.Callback callback, + public void addGetZone(String zoneName, RpcBatch.Callback callback, Map options) { try { getZoneCall(zoneName, options).queue(batch, toJsonCallback(callback)); @@ -94,7 +94,7 @@ public void addGetZone(String zoneName, DnsRpc.Callback callback, } @Override - public void addDeleteZone(String zoneName, DnsRpc.Callback callback) { + public void addDeleteZone(String zoneName, RpcBatch.Callback callback) { try { deleteZoneCall(zoneName).queue(batch, toJsonCallback(callback)); } catch (IOException ex) { @@ -103,7 +103,7 @@ public void addDeleteZone(String zoneName, DnsRpc.Callback callback) { } @Override - public void addGetProject(DnsRpc.Callback callback, + public void addGetProject(RpcBatch.Callback callback, Map options) { try { getProjectCall(options).queue(batch, toJsonCallback(callback)); @@ -112,7 +112,48 @@ public void addGetProject(DnsRpc.Callback callback, } } - private JsonBatchCallback toJsonCallback(final DnsRpc.Callback callback) { + @Override + public void addListRecordSets(String zoneName, + RpcBatch.Callback callback, Map options) { + try { + listRecordSetsCall(zoneName, options).queue(batch, toJsonCallback(callback)); + } catch (IOException ex) { + throw translate(ex); + } + } + + @Override + public void addListChangeRequests(String zoneName, + RpcBatch.Callback callback, Map options) { + try { + listChangeRequestsCall(zoneName, options).queue(batch, toJsonCallback(callback)); + } catch (IOException ex) { + throw translate(ex); + } + } + + @Override + public void addGetChangeRequest(String zoneName, String changeRequestId, + RpcBatch.Callback callback, Map options) { + try { + getChangeRequestCall(zoneName, changeRequestId, options).queue(batch, + toJsonCallback(callback)); + } catch (IOException ex) { + throw translate(ex); + } + } + + @Override + public void addApplyChangeRequest(String zoneName, Change change, + RpcBatch.Callback callback, Map options) { + try { + applyChangeRequestCall(zoneName, change, options).queue(batch, toJsonCallback(callback)); + } catch (IOException ex) { + throw translate(ex); + } + } + + private JsonBatchCallback toJsonCallback(final RpcBatch.Callback callback) { JsonBatchCallback jsonCallback = new JsonBatchCallback() { @Override public void onSuccess(T response, HttpHeaders httpHeaders) throws IOException { @@ -234,14 +275,14 @@ public ListResult listRecordSets(String zoneName, Map options) + private Dns.ResourceRecordSets.List listRecordSetsCall(String zoneName, Map options) throws IOException { // options are fields, page token, dns name, type return dns.resourceRecordSets() diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java index eca0404d8e91..f0b15a77e5ad 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java @@ -85,22 +85,6 @@ public String pageToken() { } } - /** - * An interface for batch callbacks. - */ - interface Callback { - - /** - * This method will be called upon success of the batch operation. - */ - void onSuccess(T response); - - /** - * This method will be called upon failure of the batch operation. - */ - void onFailure(GoogleJsonError googleJsonError); - } - /** * Creates a new zone. * diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java index ffb77b191f34..d2d5302234a3 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java @@ -16,17 +16,18 @@ package com.google.gcloud.dns; -import static com.google.common.base.Preconditions.checkArgument; import static java.net.HttpURLConnection.HTTP_NOT_FOUND; import com.google.api.client.googleapis.json.GoogleJsonError; +import com.google.api.services.dns.model.Change; +import com.google.api.services.dns.model.ChangesListResponse; import com.google.api.services.dns.model.ManagedZone; import com.google.api.services.dns.model.ManagedZonesListResponse; import com.google.api.services.dns.model.Project; +import com.google.api.services.dns.model.ResourceRecordSet; +import com.google.api.services.dns.model.ResourceRecordSetsListResponse; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; -import com.google.common.collect.Maps; import com.google.gcloud.Page; import com.google.gcloud.PageImpl; import com.google.gcloud.dns.spi.DnsRpc; @@ -71,8 +72,9 @@ DnsOptions options() { */ public DnsBatchResult> listZones(Dns.ZoneListOption... options) { DnsBatchResult> result = new DnsBatchResult<>(); - final Map optionMap = optionMap(options); - DnsRpc.Callback callback = createListZonesCallback(result, optionMap); + Map optionMap = DnsImpl.optionMap(options); + RpcBatch.Callback callback = createListZonesCallback(result, + optionMap); batch.addListZones(callback, optionMap); return result; } @@ -86,8 +88,8 @@ public DnsBatchResult> listZones(Dns.ZoneListOption... options) { */ public DnsBatchResult createZone(ZoneInfo zone, Dns.ZoneOption... options) { DnsBatchResult result = new DnsBatchResult<>(); - DnsRpc.Callback callback = createZoneCallback(this.options, result); - Map optionMap = optionMap(options); + RpcBatch.Callback callback = createZoneCallback(this.options, result); + Map optionMap = DnsImpl.optionMap(options); batch.addCreateZone(zone.toPb(), callback, optionMap); return result; } @@ -100,7 +102,7 @@ public DnsBatchResult createZone(ZoneInfo zone, Dns.ZoneOption... options) */ public DnsBatchResult deleteZone(String zoneName) { DnsBatchResult result = new DnsBatchResult<>(); - DnsRpc.Callback callback = createDeleteZoneCallback(result); + RpcBatch.Callback callback = createDeleteZoneCallback(result); batch.addDeleteZone(zoneName, callback); return result; } @@ -114,8 +116,8 @@ public DnsBatchResult deleteZone(String zoneName) { */ public DnsBatchResult getZone(String zoneName, Dns.ZoneOption... options) { DnsBatchResult result = new DnsBatchResult<>(); - DnsRpc.Callback callback = createZoneCallback(this.options, result); - Map optionMap = optionMap(options); + RpcBatch.Callback callback = createZoneCallback(this.options, result); + Map optionMap = DnsImpl.optionMap(options); batch.addGetZone(zoneName, callback, optionMap); return result; } @@ -129,13 +131,82 @@ public DnsBatchResult getZone(String zoneName, Dns.ZoneOption... options) */ public DnsBatchResult getProject(Dns.ProjectOption... options) { DnsBatchResult result = new DnsBatchResult<>(); - DnsRpc.Callback callback = createProjectCallback(result); - Map optionMap = optionMap(options); + RpcBatch.Callback callback = createProjectCallback(result); + Map optionMap = DnsImpl.optionMap(options); batch.addGetProject(callback, optionMap); return result; } - // todo(mderka) implement remaining operations + /** + * Adds a request representing the "list record sets" operation in the zone specified by {@code + * zoneName} to this batch. The {@code options} can be used to restrict the fields returned or + * provide page size limits in the same way as for {@link Dns#listRecordSets(String, + * Dns.RecordSetListOption...)}. The returned {@link DnsBatchResult} will return a page of record + * sets upon calling {@link DnsBatchResult#get()} on successful completion, or it will throw a + * {@link DnsException} if the operation failed or the zone does not exist. + */ + public DnsBatchResult> listRecordSets(String zoneName, + Dns.RecordSetListOption... options) { + DnsBatchResult> result = new DnsBatchResult<>(); + Map optionMap = DnsImpl.optionMap(options); + RpcBatch.Callback callback = + createListRecordSetsCallback(zoneName, result, optionMap); + batch.addListRecordSets(zoneName, callback, optionMap); + return result; + } + + /** + * Adds a request representing the "list change requests" operation in the zone specified by + * {@code zoneName} to this batch. The {@code options} can be used to restrict the fields returned + * or provide page size limits in the same way as for {@link Dns#listChangeRequests(String, + * Dns.ChangeRequestListOption...)}. The returned {@link DnsBatchResult} will return a page of + * change requests upon calling {@link DnsBatchResult#get()} on successful completion, or it will + * throw a {@link DnsException} if the operation failed or the zone does not exist. + */ + public DnsBatchResult> listChangeRequests(String zoneName, + Dns.ChangeRequestListOption... options) { + DnsBatchResult> result = new DnsBatchResult<>(); + Map optionMap = DnsImpl.optionMap(options); + RpcBatch.Callback callback = + createListChangeRequestsCallback(zoneName, result, optionMap); + batch.addListChangeRequests(zoneName, callback, optionMap); + return result; + } + + /** + * Adds a request representing the "get change request" operation for the zone specified by {@code + * zoneName} to this batch. The {@code options} can be used to restrict the fields returned in the + * same way as for {@link Dns#getChangeRequest(String, String, Dns.ChangeRequestOption...)}. The + * returned {@link DnsBatchResult} will return the requested {@link ChangeRequest} upon calling + * {@link DnsBatchResult#get()} on successful completion, {@code null} if the change request does + * not exist, or it will throw a {@link DnsException} if the operation failed or the zone does not + * exists. + */ + public DnsBatchResult getChangeRequest(String zoneName, String changeRequestId, + Dns.ChangeRequestOption... options) { + DnsBatchResult result = new DnsBatchResult<>(); + RpcBatch.Callback callback = createChangeRequestCallback(zoneName, result); + Map optionMap = DnsImpl.optionMap(options); + batch.addGetChangeRequest(zoneName, changeRequestId, callback, optionMap); + return result; + } + + /** + * Adds a request representing the "apply change request" operation to the zone specified by + * {@code zoneName} to this batch. The {@code options} can be used to restrict the fields returned + * in the same way as for {@link Dns#applyChangeRequest(String, ChangeRequestInfo, + * Dns.ChangeRequestOption...)}. The returned {@link DnsBatchResult} will return the requested + * {@link ChangeRequest} upon calling {@link DnsBatchResult#get()} on successful completion, or it + * will throw a {@link DnsException} if the operation failed or the zone does not exists. + */ + public DnsBatchResult applyChangeRequest(String zoneName, + ChangeRequestInfo changeRequest, Dns.ChangeRequestOption... options) { + DnsBatchResult result = new DnsBatchResult<>(); + RpcBatch.Callback callback = createChangeRequestCallback(zoneName, result); + Map optionMap = DnsImpl.optionMap(options); + batch.addApplyChangeRequest(zoneName, changeRequest.toPb(), callback, optionMap); + return result; + } /** * Submits this batch for processing using a single HTTP request. @@ -144,18 +215,9 @@ public void submit() { batch.submit(); } - private Map optionMap(Option... options) { - Map temp = Maps.newEnumMap(DnsRpc.Option.class); - for (Option option : options) { - Object prev = temp.put(option.rpcOption(), option.value()); - checkArgument(prev == null, "Duplicate option %s", option); - } - return ImmutableMap.copyOf(temp); - } - - private DnsRpc.Callback createListZonesCallback( + private RpcBatch.Callback createListZonesCallback( final DnsBatchResult result, final Map optionMap) { - DnsRpc.Callback callback = new DnsRpc.Callback() { + RpcBatch.Callback callback = new RpcBatch.Callback() { @Override public void onSuccess(ManagedZonesListResponse response) { List zones = response.getManagedZones(); @@ -174,8 +236,8 @@ public void onFailure(GoogleJsonError googleJsonError) { return callback; } - private DnsRpc.Callback createDeleteZoneCallback(final DnsBatchResult result) { - DnsRpc.Callback callback = new DnsRpc.Callback() { + private RpcBatch.Callback createDeleteZoneCallback(final DnsBatchResult result) { + RpcBatch.Callback callback = new RpcBatch.Callback() { @Override public void onSuccess(Void response) { result.success(true); @@ -186,9 +248,9 @@ public void onFailure(GoogleJsonError googleJsonError) { DnsException serviceException = new DnsException(googleJsonError); if (serviceException.code() == HTTP_NOT_FOUND) { result.success(false); - return; + } else { + result.error(serviceException); } - result.error(serviceException); } }; return callback; @@ -197,9 +259,9 @@ public void onFailure(GoogleJsonError googleJsonError) { /** * A joint callback for both "get zone" and "create zone" operation. */ - private DnsRpc.Callback createZoneCallback(final DnsOptions serviceOptions, + private RpcBatch.Callback createZoneCallback(final DnsOptions serviceOptions, final DnsBatchResult result) { - DnsRpc.Callback callback = new DnsRpc.Callback() { + RpcBatch.Callback callback = new RpcBatch.Callback() { @Override public void onSuccess(ManagedZone response) { result.success(response == null ? null : Zone.fromPb(serviceOptions.service(), response)); @@ -213,8 +275,8 @@ public void onFailure(GoogleJsonError googleJsonError) { return callback; } - private DnsRpc.Callback createProjectCallback(final DnsBatchResult result) { - DnsRpc.Callback callback = new DnsRpc.Callback() { + private RpcBatch.Callback createProjectCallback(final DnsBatchResult result) { + RpcBatch.Callback callback = new RpcBatch.Callback() { @Override public void onSuccess(Project response) { result.success(response == null ? null : ProjectInfo.fromPb(response)); @@ -227,4 +289,70 @@ public void onFailure(GoogleJsonError googleJsonError) { }; return callback; } + + private RpcBatch.Callback createListRecordSetsCallback( + final String zoneName, final DnsBatchResult> result, + final Map optionMap) { + RpcBatch.Callback callback = new RpcBatch.Callback() { + @Override + public void onSuccess(ResourceRecordSetsListResponse response) { + List recordSets = response.getRrsets(); + Page page = new PageImpl<>( + new DnsImpl.RecordSetPageFetcher(zoneName, options, response.getNextPageToken(), + optionMap), + response.getNextPageToken(), recordSets == null ? ImmutableList.of() + : Iterables.transform(recordSets, RecordSet.FROM_PB_FUNCTION)); + result.success(page); + } + + @Override + public void onFailure(GoogleJsonError googleJsonError) { + result.error(new DnsException(googleJsonError)); + } + }; + return callback; + } + + private RpcBatch.Callback createListChangeRequestsCallback( + final String zoneName, final DnsBatchResult result, final Map optionMap) { + RpcBatch.Callback callback = new RpcBatch.Callback() { + @Override + public void onSuccess(ChangesListResponse response) { + List changes = response.getChanges(); + Page page = new PageImpl<>( + new DnsImpl.ChangeRequestPageFetcher(zoneName, options, response.getNextPageToken(), + optionMap), + response.getNextPageToken(), changes == null ? ImmutableList.of() + : Iterables.transform(changes, ChangeRequest.fromPbFunction(options.service(), + zoneName))); + result.success(page); + } + + @Override + public void onFailure(GoogleJsonError googleJsonError) { + result.error(new DnsException(googleJsonError)); + } + }; + return callback; + } + + /** + * A joint callback for both "get change request" and "create change request" operation. + */ + private RpcBatch.Callback createChangeRequestCallback(final String zoneName, + final DnsBatchResult result) { + RpcBatch.Callback callback = new RpcBatch.Callback() { + @Override + public void onSuccess(Change response) { + result.success(response == null ? null : ChangeRequest.fromPb(options.service(), + zoneName, response)); + } + + @Override + public void onFailure(GoogleJsonError googleJsonError) { + result.error(new DnsException(googleJsonError)); + } + }; + return callback; + } } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java index fddc183c32c0..7a99156ab208 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java @@ -16,9 +16,13 @@ package com.google.gcloud.dns.spi; +import com.google.api.client.googleapis.json.GoogleJsonError; +import com.google.api.services.dns.model.Change; +import com.google.api.services.dns.model.ChangesListResponse; import com.google.api.services.dns.model.ManagedZone; import com.google.api.services.dns.model.ManagedZonesListResponse; import com.google.api.services.dns.model.Project; +import com.google.api.services.dns.model.ResourceRecordSetsListResponse; import java.util.Map; @@ -27,38 +31,80 @@ */ public interface RpcBatch { + /** + * An interface for batch callbacks. + */ + interface Callback { + + /** + * This method will be called upon success of the batch operation. + */ + void onSuccess(T response); + + /** + * This method will be called upon failure of the batch operation. + */ + void onFailure(GoogleJsonError googleJsonError); + } + /** * Adds a call to "list zones" to the batch with the provided {@code callback} and {@code * options}. */ - void addListZones(DnsRpc.Callback callback, - Map options); + void addListZones(Callback callback, Map options); /** * Adds a call to "create zone" to the batch with the provided {@code callback} and {@code * options}. */ - void addCreateZone(ManagedZone zone, DnsRpc.Callback callback, + void addCreateZone(ManagedZone zone, Callback callback, Map options); /** * Adds a call to "get zone" to the batch with the provided {@code callback} and {@code options}. */ - void addGetZone(String zoneName, DnsRpc.Callback callback, - Map options); + void addGetZone(String zoneName, Callback callback, Map options); /** * Adds a call to "delete zone" to the batch with the provided {@code callback} and {@code * options}. */ - void addGetProject(DnsRpc.Callback callback, - Map options); + void addGetProject(Callback callback, Map options); /** * Adds a call to "get project" to the batch with the provided {@code callback} and {@code * options}. */ - void addDeleteZone(String zoneName, DnsRpc.Callback callback); + void addDeleteZone(String zoneName, Callback callback); + + /** + * Adds a call to "list record sets" to the batch with the provided {@code callback} and {@code + * options}. + */ + void addListRecordSets(String zoneName, Callback callback, + Map options); + + /** + * Adds a call to "list change requests" to the batch with the provided {@code callback} and + * {@code options}. + */ + void addListChangeRequests(String zoneName, Callback callback, + Map options); + + /** + * Adds a call to "get change request" to the batch with the provided {@code callback} and + * {@code options}. + */ + void addGetChangeRequest(String zoneName, String changeRequestId, Callback callback, + Map options); + + /** + * Adds a call to "apply change request" to the batch with the provided {@code callback} and + * {@code options}. + */ + void addApplyChangeRequest(String zoneName, Change change, Callback callback, + Map options); + /** * Submits a batch of requests for processing using a single HTTP request to Cloud DNS. From ac0b723cb7f259c6aa8de429bf3f17b106182043 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Thu, 7 Apr 2016 15:10:11 -0700 Subject: [PATCH 10/21] Implemented the rest of the batch functions and tests. - Renames several forgotten DnsRecord variables and functions in tests - Moved the Callback interface from DnsRpc to RpcBatch --- .../java/com/google/cloud/dns/DnsImpl.java | 6 - .../google/cloud/dns/spi/DefaultDnsRpc.java | 3 +- .../java/com/google/cloud/dns/spi/DnsRpc.java | 2 - .../java/com/google/gcloud/dns/DnsBatch.java | 21 +- .../com/google/cloud/dns/DnsImplTest.java | 22 +- .../com/google/cloud/dns/it/ITDnsTest.java | 7 +- .../google/gcloud/dns/DnsBatchResultTest.java | 20 +- .../com/google/gcloud/dns/DnsBatchTest.java | 269 ++++++++++++++++-- 8 files changed, 281 insertions(+), 69 deletions(-) diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java index 4ca37b8fb82c..108acb2b0ec3 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java @@ -129,12 +129,6 @@ private static Page listZones(final DnsOptions serviceOptions, final Map optionsMap) { // define transformation function // this differs from the other list operations since zone is functional and requires dns service - Function pbToZoneFunction = new Function() { - @Override - public Zone apply(ManagedZone zonePb) { - return Zone.fromPb(serviceOptions.service(), zonePb); - } - }; try { // get a list of managed zones final DnsRpc rpc = serviceOptions.rpc(); diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java index ef6e9a520b14..7abd7fd42639 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java @@ -154,7 +154,7 @@ public void addApplyChangeRequest(String zoneName, Change change, } private JsonBatchCallback toJsonCallback(final RpcBatch.Callback callback) { - JsonBatchCallback jsonCallback = new JsonBatchCallback() { + return new JsonBatchCallback() { @Override public void onSuccess(T response, HttpHeaders httpHeaders) throws IOException { callback.onSuccess(response); @@ -166,7 +166,6 @@ public void onFailure(GoogleJsonError googleJsonError, HttpHeaders httpHeaders) callback.onFailure(googleJsonError); } }; - return jsonCallback; } @Override diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java index f0b15a77e5ad..5d5318df9810 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java @@ -16,10 +16,8 @@ package com.google.cloud.dns.spi; -import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.services.dns.model.Change; import com.google.api.services.dns.model.ManagedZone; -import com.google.api.services.dns.model.ManagedZonesListResponse; import com.google.api.services.dns.model.Project; import com.google.api.services.dns.model.ResourceRecordSet; import com.google.common.collect.ImmutableList; diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java index d2d5302234a3..514be7fd5ed3 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java @@ -217,7 +217,7 @@ public void submit() { private RpcBatch.Callback createListZonesCallback( final DnsBatchResult result, final Map optionMap) { - RpcBatch.Callback callback = new RpcBatch.Callback() { + return new RpcBatch.Callback() { @Override public void onSuccess(ManagedZonesListResponse response) { List zones = response.getManagedZones(); @@ -233,11 +233,10 @@ public void onFailure(GoogleJsonError googleJsonError) { result.error(new DnsException(googleJsonError)); } }; - return callback; } private RpcBatch.Callback createDeleteZoneCallback(final DnsBatchResult result) { - RpcBatch.Callback callback = new RpcBatch.Callback() { + return new RpcBatch.Callback() { @Override public void onSuccess(Void response) { result.success(true); @@ -253,7 +252,6 @@ public void onFailure(GoogleJsonError googleJsonError) { } } }; - return callback; } /** @@ -261,7 +259,7 @@ public void onFailure(GoogleJsonError googleJsonError) { */ private RpcBatch.Callback createZoneCallback(final DnsOptions serviceOptions, final DnsBatchResult result) { - RpcBatch.Callback callback = new RpcBatch.Callback() { + return new RpcBatch.Callback() { @Override public void onSuccess(ManagedZone response) { result.success(response == null ? null : Zone.fromPb(serviceOptions.service(), response)); @@ -272,11 +270,10 @@ public void onFailure(GoogleJsonError googleJsonError) { result.error(new DnsException(googleJsonError)); } }; - return callback; } private RpcBatch.Callback createProjectCallback(final DnsBatchResult result) { - RpcBatch.Callback callback = new RpcBatch.Callback() { + return new RpcBatch.Callback() { @Override public void onSuccess(Project response) { result.success(response == null ? null : ProjectInfo.fromPb(response)); @@ -287,13 +284,12 @@ public void onFailure(GoogleJsonError googleJsonError) { result.error(new DnsException(googleJsonError)); } }; - return callback; } private RpcBatch.Callback createListRecordSetsCallback( final String zoneName, final DnsBatchResult> result, final Map optionMap) { - RpcBatch.Callback callback = new RpcBatch.Callback() { + return new RpcBatch.Callback() { @Override public void onSuccess(ResourceRecordSetsListResponse response) { List recordSets = response.getRrsets(); @@ -310,12 +306,11 @@ public void onFailure(GoogleJsonError googleJsonError) { result.error(new DnsException(googleJsonError)); } }; - return callback; } private RpcBatch.Callback createListChangeRequestsCallback( final String zoneName, final DnsBatchResult result, final Map optionMap) { - RpcBatch.Callback callback = new RpcBatch.Callback() { + return new RpcBatch.Callback() { @Override public void onSuccess(ChangesListResponse response) { List changes = response.getChanges(); @@ -333,7 +328,6 @@ public void onFailure(GoogleJsonError googleJsonError) { result.error(new DnsException(googleJsonError)); } }; - return callback; } /** @@ -341,7 +335,7 @@ public void onFailure(GoogleJsonError googleJsonError) { */ private RpcBatch.Callback createChangeRequestCallback(final String zoneName, final DnsBatchResult result) { - RpcBatch.Callback callback = new RpcBatch.Callback() { + return new RpcBatch.Callback() { @Override public void onSuccess(Change response) { result.success(response == null ? null : ChangeRequest.fromPb(options.service(), @@ -353,6 +347,5 @@ public void onFailure(GoogleJsonError googleJsonError) { result.error(new DnsException(googleJsonError)); } }; - return callback; } } diff --git a/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsImplTest.java b/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsImplTest.java index fe3b08f8625b..4d57c8d16efb 100644 --- a/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsImplTest.java +++ b/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsImplTest.java @@ -91,7 +91,7 @@ public class DnsImplTest { Dns.ChangeRequestListOption.pageToken(PAGE_TOKEN), Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.STATUS), Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING)}; - private static final Dns.RecordSetListOption[] DNS_RECORD_LIST_OPTIONS = { + private static final Dns.RecordSetListOption[] RECORD_SET_LIST_OPTIONS = { Dns.RecordSetListOption.pageSize(MAX_SIZE), Dns.RecordSetListOption.pageToken(PAGE_TOKEN), Dns.RecordSetListOption.fields(Dns.RecordSetField.TTL), @@ -350,7 +350,7 @@ public void testListZonesWithOptions() { } @Test - public void testListDnsRecords() { + public void testListRecordSets() { EasyMock.expect(dnsRpcMock.listRecordSets(ZONE_INFO.name(), EMPTY_RPC_OPTIONS)) .andReturn(LIST_OF_PB_DNS_RECORDS); EasyMock.replay(dnsRpcMock); @@ -362,28 +362,28 @@ public void testListDnsRecords() { } @Test - public void testListDnsRecordsWithOptions() { + public void testListRecordSetsWithOptions() { Capture> capturedOptions = Capture.newInstance(); EasyMock.expect(dnsRpcMock.listRecordSets(EasyMock.eq(ZONE_NAME), EasyMock.capture(capturedOptions))).andReturn(LIST_OF_PB_DNS_RECORDS); EasyMock.replay(dnsRpcMock); dns = options.service(); // creates DnsImpl - Page dnsPage = dns.listRecordSets(ZONE_NAME, DNS_RECORD_LIST_OPTIONS); + Page dnsPage = dns.listRecordSets(ZONE_NAME, RECORD_SET_LIST_OPTIONS); assertEquals(2, Lists.newArrayList(dnsPage.values()).size()); assertTrue(Lists.newArrayList(dnsPage.values()).contains(DNS_RECORD1)); assertTrue(Lists.newArrayList(dnsPage.values()).contains(DNS_RECORD2)); - Integer size = (Integer) capturedOptions.getValue().get(DNS_RECORD_LIST_OPTIONS[0].rpcOption()); + Integer size = (Integer) capturedOptions.getValue().get(RECORD_SET_LIST_OPTIONS[0].rpcOption()); assertEquals(MAX_SIZE, size); String selector = (String) capturedOptions.getValue() - .get(DNS_RECORD_LIST_OPTIONS[1].rpcOption()); + .get(RECORD_SET_LIST_OPTIONS[1].rpcOption()); assertEquals(PAGE_TOKEN, selector); - selector = (String) capturedOptions.getValue().get(DNS_RECORD_LIST_OPTIONS[2].rpcOption()); + selector = (String) capturedOptions.getValue().get(RECORD_SET_LIST_OPTIONS[2].rpcOption()); assertTrue(selector.contains(Dns.RecordSetField.NAME.selector())); assertTrue(selector.contains(Dns.RecordSetField.TTL.selector())); - selector = (String) capturedOptions.getValue().get(DNS_RECORD_LIST_OPTIONS[3].rpcOption()); - assertEquals(DNS_RECORD_LIST_OPTIONS[3].value(), selector); - String type = (String) capturedOptions.getValue().get(DNS_RECORD_LIST_OPTIONS[4] + selector = (String) capturedOptions.getValue().get(RECORD_SET_LIST_OPTIONS[3].rpcOption()); + assertEquals(RECORD_SET_LIST_OPTIONS[3].value(), selector); + String type = (String) capturedOptions.getValue().get(RECORD_SET_LIST_OPTIONS[4] .rpcOption()); - assertEquals(DNS_RECORD_LIST_OPTIONS[4].value(), type); + assertEquals(RECORD_SET_LIST_OPTIONS[4].value(), type); } } 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 d6eabe09328b..26be9c2eeaba 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 @@ -23,7 +23,6 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import com.google.common.collect.ImmutableList; import com.google.cloud.Page; import com.google.cloud.dns.ChangeRequest; import com.google.cloud.dns.ChangeRequestInfo; @@ -87,7 +86,7 @@ public class ITDnsTest { .build(); private static final List ZONE_NAMES = ImmutableList.of(ZONE_NAME1, ZONE_NAME_EMPTY_DESCRIPTION); - + @Rule public Timeout globalTimeout = Timeout.seconds(300); @@ -974,7 +973,7 @@ public void testListZoneBatch() { assertTrue(batchResult.submitted()); Iterator iteratorBatch = batchResult.get().iterateAll(); Iterator iteratorList = DNS.listZones().iterateAll(); - while(iteratorBatch.hasNext()) { + while (iteratorBatch.hasNext()) { assertEquals(iteratorList.next(), iteratorBatch.next()); } } finally { @@ -983,5 +982,5 @@ public void testListZoneBatch() { } } - // todo(mderka) implement tests for other batch calls + // todo(mderka) implement tests for other batch calls, issue #874 } diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchResultTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchResultTest.java index 680208bb3277..ea3a526e309c 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchResultTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchResultTest.java @@ -28,39 +28,39 @@ public class DnsBatchResultTest { - private DnsBatchResult RESULT; + private DnsBatchResult result; @Before public void setUp() { - RESULT = new DnsBatchResult<>(); + result = new DnsBatchResult<>(); } @Test public void testSuccess() { - assertFalse(RESULT.submitted()); + assertFalse(result.submitted()); try { - RESULT.get(); + result.get(); fail("This was not submitted yet."); } catch (IllegalStateException ex) { // expected } - RESULT.success(true); - assertTrue(RESULT.get()); + result.success(true); + assertTrue(result.get()); } @Test public void testError() { - assertFalse(RESULT.submitted()); + assertFalse(result.submitted()); try { - RESULT.get(); + result.get(); fail("This was not submitted yet."); } catch (IllegalStateException ex) { // expected } DnsException ex = new DnsException(new IOException("some error")); - RESULT.error(ex); + result.error(ex); try { - RESULT.get(); + result.get(); fail("This is a failed operation and should have thrown a DnsException."); } catch (DnsException real) { assertSame(ex, real); diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java index 7363c530ef03..22368360c7c4 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java @@ -22,9 +22,12 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import com.google.api.services.dns.model.Change; +import com.google.api.services.dns.model.ChangesListResponse; import com.google.api.services.dns.model.ManagedZone; import com.google.api.services.dns.model.ManagedZonesListResponse; import com.google.api.services.dns.model.Project; +import com.google.api.services.dns.model.ResourceRecordSetsListResponse; import com.google.gcloud.Page; import com.google.gcloud.dns.spi.DnsRpc; import com.google.gcloud.dns.spi.RpcBatch; @@ -56,6 +59,31 @@ public class DnsBatchTest { Dns.ZoneListOption.fields(Dns.ZoneField.DESCRIPTION), Dns.ZoneListOption.dnsName(DNS_NAME)}; private static final ProjectInfo PROJECT_INFO = ProjectInfo.builder().build(); + private static final Dns.RecordSetListOption[] RECORD_SET_LIST_OPTIONS = { + Dns.RecordSetListOption.pageSize(MAX_SIZE), + Dns.RecordSetListOption.pageToken(PAGE_TOKEN), + Dns.RecordSetListOption.fields(Dns.RecordSetField.TTL), + Dns.RecordSetListOption.dnsName(DNS_NAME), + Dns.RecordSetListOption.type(RecordSet.Type.AAAA)}; + private static final RecordSet DNS_RECORD1 = + RecordSet.builder("Something", RecordSet.Type.AAAA).build(); + private static final ChangeRequestInfo CHANGE_REQUEST_PARTIAL = ChangeRequestInfo.builder() + .add(DNS_RECORD1) + .build(); + private static final String CHANGE_ID = "some change id"; + private static final ChangeRequestInfo CHANGE_REQUEST_COMPLETE = ChangeRequestInfo.builder() + .add(DNS_RECORD1) + .startTimeMillis(123L) + .status(ChangeRequest.Status.PENDING) + .generatedId(CHANGE_ID) + .build(); + private static final Dns.ChangeRequestListOption[] CHANGE_LIST_OPTIONS = { + Dns.ChangeRequestListOption.pageSize(MAX_SIZE), + Dns.ChangeRequestListOption.pageToken(PAGE_TOKEN), + Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.STATUS), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING)}; + private static final Dns.ChangeRequestOption CHANGE_GET_FIELDS = + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS); private DnsOptions optionsMock; private DnsRpc dnsRpcMock; @@ -93,7 +121,7 @@ public void testConstructor() { @Test public void testListZones() { EasyMock.reset(batchMock); - Capture> callback = Capture.newInstance(); + Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); batchMock.addListZones(EasyMock.capture(callback), EasyMock.capture(capturedOptions)); EasyMock.replay(batchMock); @@ -109,7 +137,7 @@ public void testListZones() { batchResult.error(new DnsException(new IOException("expected"))); try { batchResult.get(); - fail("TShould throw a DnsException on error."); + fail("Should throw a DnsException on error."); } catch (DnsException ex) { // expected } @@ -118,7 +146,7 @@ public void testListZones() { @Test public void testListZonesWithOptions() { EasyMock.reset(batchMock); - Capture> callback = Capture.newInstance(); + Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); batchMock.addListZones(EasyMock.capture(callback), EasyMock.capture(capturedOptions)); EasyMock.replay(batchMock); @@ -139,7 +167,7 @@ public void testListZonesWithOptions() { @Test public void testCreateZone() { EasyMock.reset(batchMock); - Capture> callback = Capture.newInstance(); + Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); Capture capturedZone = Capture.newInstance(); batchMock.addCreateZone(EasyMock.capture(capturedZone), EasyMock.capture(callback), @@ -159,7 +187,7 @@ public void testCreateZone() { batchResult.error(new DnsException(new IOException("expected"))); try { batchResult.get(); - fail("TShould throw a DnsException on error."); + fail("Should throw a DnsException on error."); } catch (DnsException ex) { // expected } @@ -171,7 +199,7 @@ public void testCreateZoneWithOptions() { EasyMock.reset(optionsMock); EasyMock.expect(optionsMock.service()).andReturn(dns); EasyMock.replay(optionsMock); - Capture> callback = Capture.newInstance(); + Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); Capture capturedZone = Capture.newInstance(); batchMock.addCreateZone(EasyMock.capture(capturedZone), EasyMock.capture(callback), @@ -183,7 +211,7 @@ public void testCreateZoneWithOptions() { String selector = (String) capturedOptions.getValue().get(ZONE_FIELDS.rpcOption()); assertTrue(selector.contains(Dns.ZoneField.CREATION_TIME.selector())); assertTrue(selector.contains(Dns.ZoneField.NAME.selector())); - DnsRpc.Callback capturedCallback = callback.getValue(); + RpcBatch.Callback capturedCallback = callback.getValue(); capturedCallback.onSuccess(ZONE_INFO.toPb()); assertEquals(ZONE_INFO.toPb(), batchResult.get().toPb()); } @@ -191,7 +219,7 @@ public void testCreateZoneWithOptions() { @Test public void testGetZone() { EasyMock.reset(batchMock); - Capture> callback = Capture.newInstance(); + Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); batchMock.addGetZone(EasyMock.eq(ZONE_NAME), EasyMock.capture(callback), EasyMock.capture(capturedOptions)); @@ -209,7 +237,7 @@ public void testGetZone() { batchResult.error(new DnsException(new IOException("expected"))); try { batchResult.get(); - fail("TShould throw a DnsException on error."); + fail("Should throw a DnsException on error."); } catch (DnsException ex) { // expected } @@ -219,7 +247,7 @@ public void testGetZone() { public void testGetZoneWithOptions() { EasyMock.reset(batchMock); EasyMock.reset(optionsMock); - Capture> callback = Capture.newInstance(); + Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); batchMock.addGetZone(EasyMock.eq(ZONE_NAME), EasyMock.capture(callback), EasyMock.capture(capturedOptions)); @@ -229,7 +257,7 @@ public void testGetZoneWithOptions() { String selector = (String) capturedOptions.getValue().get(ZONE_FIELDS.rpcOption()); assertTrue(selector.contains(Dns.ZoneField.CREATION_TIME.selector())); assertTrue(selector.contains(Dns.ZoneField.NAME.selector())); - DnsRpc.Callback capturedCallback = callback.getValue(); + RpcBatch.Callback capturedCallback = callback.getValue(); EasyMock.expect(optionsMock.service()).andReturn(dns); EasyMock.replay(optionsMock); capturedCallback.onSuccess(ZONE_INFO.toPb()); @@ -239,7 +267,7 @@ public void testGetZoneWithOptions() { @Test public void testDeleteZone() { EasyMock.reset(batchMock); - Capture> callback = Capture.newInstance(); + Capture> callback = Capture.newInstance(); batchMock.addDeleteZone(EasyMock.eq(ZONE_NAME), EasyMock.capture(callback)); EasyMock.replay(batchMock); DnsBatchResult batchResult = dnsBatch.deleteZone(ZONE_NAME); @@ -254,7 +282,7 @@ public void testDeleteZone() { batchResult.error(new DnsException(new IOException("expected"))); try { batchResult.get(); - fail("TShould throw a DnsException on error."); + fail("Should throw a DnsException on error."); } catch (DnsException ex) { // expected } @@ -263,12 +291,12 @@ public void testDeleteZone() { @Test public void testDeleteZoneOnSuccess() { EasyMock.reset(batchMock); - Capture> callback = Capture.newInstance(); + Capture> callback = Capture.newInstance(); batchMock.addDeleteZone(EasyMock.eq(ZONE_NAME), EasyMock.capture(callback)); EasyMock.replay(batchMock); DnsBatchResult batchResult = dnsBatch.deleteZone(ZONE_NAME); assertNotNull(callback.getValue()); - DnsRpc.Callback capturedCallback = callback.getValue(); + RpcBatch.Callback capturedCallback = callback.getValue(); Void result = null; capturedCallback.onSuccess(result); assertTrue(batchResult.get()); @@ -277,7 +305,7 @@ public void testDeleteZoneOnSuccess() { @Test public void testGetProject() { EasyMock.reset(batchMock); - Capture> callback = Capture.newInstance(); + Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); batchMock.addGetProject(EasyMock.capture(callback), EasyMock.capture(capturedOptions)); EasyMock.replay(batchMock); @@ -294,7 +322,7 @@ public void testGetProject() { batchResult.error(new DnsException(new IOException("expected"))); try { batchResult.get(); - fail("TShould throw a DnsException on error."); + fail("Should throw a DnsException on error."); } catch (DnsException ex) { // expected } @@ -303,7 +331,7 @@ public void testGetProject() { @Test public void testGetProjectWithOptions() { EasyMock.reset(batchMock); - Capture> callback = Capture.newInstance(); + Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); batchMock.addGetProject(EasyMock.capture(callback), EasyMock.capture(capturedOptions)); EasyMock.replay(batchMock); @@ -312,10 +340,211 @@ public void testGetProjectWithOptions() { String selector = (String) capturedOptions.getValue().get(PROJECT_FIELDS.rpcOption()); assertTrue(selector.contains(Dns.ProjectField.QUOTA.selector())); assertTrue(selector.contains(Dns.ProjectField.PROJECT_ID.selector())); - DnsRpc.Callback capturedCallback = callback.getValue(); + RpcBatch.Callback capturedCallback = callback.getValue(); capturedCallback.onSuccess(PROJECT_INFO.toPb()); assertEquals(PROJECT_INFO, batchResult.get()); } - // todo(mderka) test submit and other methods when implemented + @Test + public void testListRecordSets() { + EasyMock.reset(batchMock); + Capture> callback = Capture.newInstance(); + Capture> capturedOptions = Capture.newInstance(); + batchMock.addListRecordSets(EasyMock.eq(ZONE_NAME), EasyMock.capture(callback), + EasyMock.capture(capturedOptions)); + EasyMock.replay(batchMock); + DnsBatchResult> batchResult = dnsBatch.listRecordSets(ZONE_NAME); + assertEquals(0, capturedOptions.getValue().size()); + assertNotNull(callback.getValue()); + try { + batchResult.get(); + fail("No result available yet."); + } catch (IllegalStateException ex) { + // expected + } + batchResult.error(new DnsException(new IOException("expected"))); + try { + batchResult.get(); + fail("Should throw a DnsException on error."); + } catch (DnsException ex) { + // expected + } + } + + @Test + public void testListRecordSetsWithOptions() { + EasyMock.reset(batchMock); + Capture> callback = Capture.newInstance(); + Capture> capturedOptions = Capture.newInstance(); + batchMock.addListRecordSets(EasyMock.eq(ZONE_NAME), EasyMock.capture(callback), + EasyMock.capture(capturedOptions)); + EasyMock.replay(batchMock); + dnsBatch.listRecordSets(ZONE_NAME, RECORD_SET_LIST_OPTIONS); + assertNotNull(callback.getValue()); + Integer size = (Integer) capturedOptions.getValue().get(RECORD_SET_LIST_OPTIONS[0].rpcOption()); + assertEquals(MAX_SIZE, size); + String selector = (String) capturedOptions.getValue() + .get(RECORD_SET_LIST_OPTIONS[1].rpcOption()); + assertEquals(PAGE_TOKEN, selector); + selector = (String) capturedOptions.getValue().get(RECORD_SET_LIST_OPTIONS[2].rpcOption()); + assertTrue(selector.contains(Dns.RecordSetField.NAME.selector())); + assertTrue(selector.contains(Dns.RecordSetField.TTL.selector())); + selector = (String) capturedOptions.getValue().get(RECORD_SET_LIST_OPTIONS[3].rpcOption()); + assertEquals(RECORD_SET_LIST_OPTIONS[3].value(), selector); + String type = (String) capturedOptions.getValue().get(RECORD_SET_LIST_OPTIONS[4] + .rpcOption()); + assertEquals(RECORD_SET_LIST_OPTIONS[4].value(), type); + // cannot test callback success without ResourceRecordSetsListResponse which is final + } + + @Test + public void testListChangeRequests() { + EasyMock.reset(batchMock); + Capture> callback = Capture.newInstance(); + Capture> capturedOptions = Capture.newInstance(); + batchMock.addListChangeRequests(EasyMock.eq(ZONE_NAME), EasyMock.capture(callback), + EasyMock.capture(capturedOptions)); + EasyMock.replay(batchMock); + DnsBatchResult> batchResult = dnsBatch.listChangeRequests(ZONE_NAME); + assertNotNull(callback.getValue()); + assertEquals(0, capturedOptions.getValue().size()); + try { + batchResult.get(); + fail("No result available yet."); + } catch (IllegalStateException ex) { + // expected + } + batchResult.error(new DnsException(new IOException("expected"))); + try { + batchResult.get(); + fail("Should throw a DnsException on error."); + } catch (DnsException ex) { + // expected + } + } + + @Test + public void testListChangeRequestsWithOptions() { + EasyMock.reset(batchMock); + Capture> callback = Capture.newInstance(); + Capture> capturedOptions = Capture.newInstance(); + batchMock.addListChangeRequests(EasyMock.eq(ZONE_NAME), EasyMock.capture(callback), + EasyMock.capture(capturedOptions)); + EasyMock.replay(batchMock); + dnsBatch.listChangeRequests(ZONE_NAME, CHANGE_LIST_OPTIONS); + assertNotNull(callback.getValue()); + Integer size = (Integer) capturedOptions.getValue().get(CHANGE_LIST_OPTIONS[0].rpcOption()); + assertEquals(MAX_SIZE, size); + String selector = (String) capturedOptions.getValue().get(CHANGE_LIST_OPTIONS[1].rpcOption()); + assertEquals(PAGE_TOKEN, selector); + selector = (String) capturedOptions.getValue().get(CHANGE_LIST_OPTIONS[2].rpcOption()); + assertTrue(selector.contains(Dns.ChangeRequestField.STATUS.selector())); + assertTrue(selector.contains(Dns.ChangeRequestField.ID.selector())); + selector = (String) capturedOptions.getValue().get(CHANGE_LIST_OPTIONS[3].rpcOption()); + assertTrue(selector.contains(Dns.SortingOrder.ASCENDING.selector())); + // cannot test callback success without ChangesListResponse which is final + } + + @Test + public void testGetChangeRequest() { + EasyMock.reset(batchMock); + Capture> callback = Capture.newInstance(); + Capture> capturedOptions = Capture.newInstance(); + batchMock.addGetChangeRequest(EasyMock.eq(ZONE_NAME), + EasyMock.eq(CHANGE_REQUEST_COMPLETE.generatedId()), EasyMock.capture(callback), + EasyMock.capture(capturedOptions)); + EasyMock.replay(batchMock); + DnsBatchResult batchResult = dnsBatch.getChangeRequest(ZONE_NAME, + CHANGE_REQUEST_COMPLETE.generatedId()); + assertEquals(0, capturedOptions.getValue().size()); + assertNotNull(callback.getValue()); + try { + batchResult.get(); + fail("No result available yet."); + } catch (IllegalStateException ex) { + // expected + } + // testing error here, success is tested with options + batchResult.error(new DnsException(new IOException("expected"))); + try { + batchResult.get(); + fail("Should throw a DnsException on error."); + } catch (DnsException ex) { + // expected + } + } + + @Test + public void testGetChangeRequestWithOptions() { + EasyMock.reset(batchMock); + EasyMock.reset(optionsMock); + Capture> callback = Capture.newInstance(); + Capture> capturedOptions = Capture.newInstance(); + batchMock.addGetChangeRequest(EasyMock.eq(ZONE_NAME), + EasyMock.eq(CHANGE_REQUEST_COMPLETE.generatedId()), EasyMock.capture(callback), + EasyMock.capture(capturedOptions)); + EasyMock.replay(batchMock); + DnsBatchResult batchResult = dnsBatch.getChangeRequest(ZONE_NAME, + CHANGE_REQUEST_COMPLETE.generatedId(), CHANGE_GET_FIELDS); + assertNotNull(callback.getValue()); + String selector = (String) capturedOptions.getValue().get(CHANGE_GET_FIELDS.rpcOption()); + assertTrue(selector.contains(Dns.ChangeRequestField.STATUS.selector())); + assertTrue(selector.contains(Dns.ChangeRequestField.ID.selector())); + RpcBatch.Callback capturedCallback = callback.getValue(); + EasyMock.expect(optionsMock.service()).andReturn(dns); + EasyMock.replay(optionsMock); + capturedCallback.onSuccess(CHANGE_REQUEST_COMPLETE.toPb()); + assertEquals(CHANGE_REQUEST_COMPLETE.toPb(), batchResult.get().toPb()); + } + + @Test + public void testApplyChangeRequest() { + EasyMock.reset(batchMock); + Capture> callback = Capture.newInstance(); + Capture> capturedOptions = Capture.newInstance(); + batchMock.addApplyChangeRequest(EasyMock.eq(ZONE_NAME), + EasyMock.eq(CHANGE_REQUEST_PARTIAL.toPb()), EasyMock.capture(callback), + EasyMock.capture(capturedOptions)); + EasyMock.replay(batchMock); + DnsBatchResult batchResult = dnsBatch.applyChangeRequest(ZONE_INFO.name(), + CHANGE_REQUEST_PARTIAL); + assertEquals(0, capturedOptions.getValue().size()); + assertNotNull(callback.getValue()); + try { + batchResult.get(); + fail("No result available yet."); + } catch (IllegalStateException ex) { + // expected + } + // testing error here, success is tested with options + batchResult.error(new DnsException(new IOException("expected"))); + try { + batchResult.get(); + fail("Should throw a DnsException on error."); + } catch (DnsException ex) { + // expected + } + } + + @Test + public void testApplyChangeRequestWithOptions() { + EasyMock.reset(batchMock); + EasyMock.reset(optionsMock); + Capture> callback = Capture.newInstance(); + Capture> capturedOptions = Capture.newInstance(); + batchMock.addApplyChangeRequest(EasyMock.eq(ZONE_NAME), + EasyMock.eq(CHANGE_REQUEST_PARTIAL.toPb()), EasyMock.capture(callback), + EasyMock.capture(capturedOptions)); + EasyMock.replay(batchMock); + DnsBatchResult batchResult = dnsBatch.applyChangeRequest(ZONE_INFO.name(), + CHANGE_REQUEST_PARTIAL, CHANGE_GET_FIELDS); + String selector = (String) capturedOptions.getValue().get(CHANGE_GET_FIELDS.rpcOption()); + assertTrue(selector.contains(Dns.ChangeRequestField.STATUS.selector())); + assertTrue(selector.contains(Dns.ChangeRequestField.ID.selector())); + EasyMock.expect(optionsMock.service()).andReturn(dns); + EasyMock.replay(optionsMock); + RpcBatch.Callback capturedCallback = callback.getValue(); + capturedCallback.onSuccess(CHANGE_REQUEST_COMPLETE.toPb()); + assertEquals(CHANGE_REQUEST_COMPLETE.toPb(), batchResult.get().toPb()); + } } From f42e8dce9d1ca5441cadd4ceeb1c1edc8566336a Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Fri, 8 Apr 2016 15:41:22 -0700 Subject: [PATCH 11/21] Added tests for callbacks. Fixed documentation. --- .../google/cloud/dns/spi/DefaultDnsRpc.java | 32 +++--- .../java/com/google/gcloud/dns/DnsBatch.java | 4 +- .../com/google/gcloud/dns/spi/RpcBatch.java | 19 ++-- .../com/google/gcloud/dns/DnsBatchTest.java | 106 ++++++++++++++++-- 4 files changed, 126 insertions(+), 35 deletions(-) diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java index 7abd7fd42639..7c1a7a506bbb 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java @@ -57,7 +57,7 @@ public class DefaultDnsRpc implements DnsRpc { private class DefaultRpcBatch implements RpcBatch { - BatchRequest batch; + private BatchRequest batch; private DefaultRpcBatch(BatchRequest batch) { this.batch = batch; @@ -153,21 +153,6 @@ public void addApplyChangeRequest(String zoneName, Change change, } } - private JsonBatchCallback toJsonCallback(final RpcBatch.Callback callback) { - return new JsonBatchCallback() { - @Override - public void onSuccess(T response, HttpHeaders httpHeaders) throws IOException { - callback.onSuccess(response); - } - - @Override - public void onFailure(GoogleJsonError googleJsonError, HttpHeaders httpHeaders) - throws IOException { - callback.onFailure(googleJsonError); - } - }; - } - @Override public void submit() { try { @@ -178,6 +163,21 @@ public void submit() { } } + private static JsonBatchCallback toJsonCallback(final RpcBatch.Callback callback) { + return new JsonBatchCallback() { + @Override + public void onSuccess(T response, HttpHeaders httpHeaders) throws IOException { + callback.onSuccess(response); + } + + @Override + public void onFailure(GoogleJsonError googleJsonError, HttpHeaders httpHeaders) + throws IOException { + callback.onFailure(googleJsonError); + } + }; + } + private static DnsException translate(IOException exception) { return new DnsException(exception); } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java index 514be7fd5ed3..29ff3da6c91a 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java @@ -180,7 +180,7 @@ public DnsBatchResult> listChangeRequests(String zoneName, * returned {@link DnsBatchResult} will return the requested {@link ChangeRequest} upon calling * {@link DnsBatchResult#get()} on successful completion, {@code null} if the change request does * not exist, or it will throw a {@link DnsException} if the operation failed or the zone does not - * exists. + * exist. */ public DnsBatchResult getChangeRequest(String zoneName, String changeRequestId, Dns.ChangeRequestOption... options) { @@ -197,7 +197,7 @@ public DnsBatchResult getChangeRequest(String zoneName, String ch * in the same way as for {@link Dns#applyChangeRequest(String, ChangeRequestInfo, * Dns.ChangeRequestOption...)}. The returned {@link DnsBatchResult} will return the requested * {@link ChangeRequest} upon calling {@link DnsBatchResult#get()} on successful completion, or it - * will throw a {@link DnsException} if the operation failed or the zone does not exists. + * will throw a {@link DnsException} if the operation failed or the zone does not exist. */ public DnsBatchResult applyChangeRequest(String zoneName, ChangeRequestInfo changeRequest, Dns.ChangeRequestOption... options) { diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java index 7a99156ab208..6741c9b2d9f9 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java @@ -62,50 +62,53 @@ void addCreateZone(ManagedZone zone, Callback callback, /** * Adds a call to "get zone" to the batch with the provided {@code callback} and {@code options}. + * The zone to be retrieved is identified by {@code zoneName}. */ void addGetZone(String zoneName, Callback callback, Map options); /** - * Adds a call to "delete zone" to the batch with the provided {@code callback} and {@code + * Adds a call to "get project" to the batch with the provided {@code callback} and {@code * options}. */ void addGetProject(Callback callback, Map options); /** - * Adds a call to "get project" to the batch with the provided {@code callback} and {@code + * Adds a call to "delete zone" to the batch with the provided {@code callback} and {@code * options}. */ void addDeleteZone(String zoneName, Callback callback); /** * Adds a call to "list record sets" to the batch with the provided {@code callback} and {@code - * options}. + * options}. The zone whose record sets are to be listed is identified by {@code zoneName}. */ void addListRecordSets(String zoneName, Callback callback, Map options); /** * Adds a call to "list change requests" to the batch with the provided {@code callback} and - * {@code options}. + * {@code options}. The zone whose change requests are to be listed is identified by {@code + * zoneName}. */ void addListChangeRequests(String zoneName, Callback callback, Map options); /** - * Adds a call to "get change request" to the batch with the provided {@code callback} and - * {@code options}. + * Adds a call to "get change request" to the batch with the provided {@code callback} and {@code + * options}. The change request to be retrieved is identified by {@code changeRequestId} and the + * zone to be searched by {@code zoneName}. */ void addGetChangeRequest(String zoneName, String changeRequestId, Callback callback, Map options); /** * Adds a call to "apply change request" to the batch with the provided {@code callback} and - * {@code options}. + * {@code options}. The zone to which the change request should be applied is identified by {@code + * zoneName}. */ void addApplyChangeRequest(String zoneName, Change change, Callback callback, Map options); - /** * Submits a batch of requests for processing using a single HTTP request to Cloud DNS. */ diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java index 22368360c7c4..74883b11b095 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java @@ -27,7 +27,9 @@ import com.google.api.services.dns.model.ManagedZone; import com.google.api.services.dns.model.ManagedZonesListResponse; import com.google.api.services.dns.model.Project; +import com.google.api.services.dns.model.ResourceRecordSet; import com.google.api.services.dns.model.ResourceRecordSetsListResponse; +import com.google.common.collect.ImmutableList; import com.google.gcloud.Page; import com.google.gcloud.dns.spi.DnsRpc; import com.google.gcloud.dns.spi.RpcBatch; @@ -39,6 +41,8 @@ import org.junit.Test; import java.io.IOException; +import java.util.Iterator; +import java.util.List; import java.util.Map; public class DnsBatchTest { @@ -65,14 +69,14 @@ public class DnsBatchTest { Dns.RecordSetListOption.fields(Dns.RecordSetField.TTL), Dns.RecordSetListOption.dnsName(DNS_NAME), Dns.RecordSetListOption.type(RecordSet.Type.AAAA)}; - private static final RecordSet DNS_RECORD1 = + private static final RecordSet RECORD_SET = RecordSet.builder("Something", RecordSet.Type.AAAA).build(); private static final ChangeRequestInfo CHANGE_REQUEST_PARTIAL = ChangeRequestInfo.builder() - .add(DNS_RECORD1) + .add(RECORD_SET) .build(); private static final String CHANGE_ID = "some change id"; private static final ChangeRequestInfo CHANGE_REQUEST_COMPLETE = ChangeRequestInfo.builder() - .add(DNS_RECORD1) + .add(RECORD_SET) .startTimeMillis(123L) .status(ChangeRequest.Status.PENDING) .generatedId(CHANGE_ID) @@ -84,6 +88,12 @@ public class DnsBatchTest { Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING)}; private static final Dns.ChangeRequestOption CHANGE_GET_FIELDS = Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS); + private static final List RECORD_SET_LIST = ImmutableList.of( + RECORD_SET.toPb(), RECORD_SET.toPb(), RECORD_SET.toPb(), RECORD_SET.toPb()); + private static final List CHANGE_LIST = ImmutableList.of(CHANGE_REQUEST_COMPLETE.toPb(), + CHANGE_REQUEST_COMPLETE.toPb(), CHANGE_REQUEST_COMPLETE.toPb()); + private static final List ZONE_LIST = ImmutableList.of(ZONE_INFO.toPb(), + ZONE_INFO.toPb()); private DnsOptions optionsMock; private DnsRpc dnsRpcMock; @@ -101,6 +111,7 @@ public void setUp() { EasyMock.replay(optionsMock); EasyMock.replay(dnsRpcMock); EasyMock.replay(batchMock); + EasyMock.replay(dns); dnsBatch = new DnsBatch(optionsMock); } @@ -109,6 +120,7 @@ public void tearDown() { EasyMock.verify(batchMock); EasyMock.verify(dnsRpcMock); EasyMock.verify(optionsMock); + EasyMock.verify(dns); } @Test @@ -150,7 +162,7 @@ public void testListZonesWithOptions() { Capture> capturedOptions = Capture.newInstance(); batchMock.addListZones(EasyMock.capture(callback), EasyMock.capture(capturedOptions)); EasyMock.replay(batchMock); - dnsBatch.listZones(ZONE_LIST_OPTIONS); + DnsBatchResult> batchResult = dnsBatch.listZones(ZONE_LIST_OPTIONS); assertNotNull(callback.getValue()); Integer size = (Integer) capturedOptions.getValue().get(ZONE_LIST_OPTIONS[0].rpcOption()); assertEquals(MAX_SIZE, size); @@ -161,7 +173,30 @@ public void testListZonesWithOptions() { assertTrue(selector.contains(Dns.ZoneField.NAME.selector())); selector = (String) capturedOptions.getValue().get(ZONE_LIST_OPTIONS[3].rpcOption()); assertEquals(DNS_NAME, selector); - // cannot test callback success without ManagedZonesListResponse which is final + // check the callback and result + // check the callback + ManagedZonesListResponse response = new ManagedZonesListResponse() + .setManagedZones(ZONE_LIST) + .setNextPageToken(PAGE_TOKEN); + RpcBatch.Callback capturedCallback = callback.getValue(); + EasyMock.verify(optionsMock); + EasyMock.reset(optionsMock); + EasyMock.expect(optionsMock.service()).andReturn(dns).times(ZONE_LIST.size()); + EasyMock.replay(optionsMock); + capturedCallback.onSuccess(response); + Page page = batchResult.get(); + assertEquals(PAGE_TOKEN, page.nextPageCursor()); + Iterator iterator = page.values().iterator(); + int resultSize = 0; + EasyMock.verify(dns); + EasyMock.reset(dns); + EasyMock.expect(dns.options()).andReturn(optionsMock).times(ZONE_LIST.size()); + EasyMock.replay(dns); + while (iterator.hasNext()) { + assertEquals(ZONE_INFO.toPb(), iterator.next().toPb()); + resultSize++; + } + assertEquals(ZONE_LIST.size(), resultSize); } @Test @@ -195,6 +230,10 @@ public void testCreateZone() { @Test public void testCreateZoneWithOptions() { + EasyMock.verify(dns); + EasyMock.reset(dns); + EasyMock.expect(dns.options()).andReturn(optionsMock); + EasyMock.replay(dns); EasyMock.reset(batchMock); EasyMock.reset(optionsMock); EasyMock.expect(optionsMock.service()).andReturn(dns); @@ -245,6 +284,10 @@ public void testGetZone() { @Test public void testGetZoneWithOptions() { + EasyMock.verify(dns); + EasyMock.reset(dns); + EasyMock.expect(dns.options()).andReturn(optionsMock); + EasyMock.replay(dns); EasyMock.reset(batchMock); EasyMock.reset(optionsMock); Capture> callback = Capture.newInstance(); @@ -379,7 +422,8 @@ public void testListRecordSetsWithOptions() { batchMock.addListRecordSets(EasyMock.eq(ZONE_NAME), EasyMock.capture(callback), EasyMock.capture(capturedOptions)); EasyMock.replay(batchMock); - dnsBatch.listRecordSets(ZONE_NAME, RECORD_SET_LIST_OPTIONS); + DnsBatchResult> batchResult = + dnsBatch.listRecordSets(ZONE_NAME, RECORD_SET_LIST_OPTIONS); assertNotNull(callback.getValue()); Integer size = (Integer) capturedOptions.getValue().get(RECORD_SET_LIST_OPTIONS[0].rpcOption()); assertEquals(MAX_SIZE, size); @@ -394,7 +438,20 @@ public void testListRecordSetsWithOptions() { String type = (String) capturedOptions.getValue().get(RECORD_SET_LIST_OPTIONS[4] .rpcOption()); assertEquals(RECORD_SET_LIST_OPTIONS[4].value(), type); - // cannot test callback success without ResourceRecordSetsListResponse which is final + RpcBatch.Callback capturedCallback = callback.getValue(); + ResourceRecordSetsListResponse response = new ResourceRecordSetsListResponse() + .setRrsets(RECORD_SET_LIST) + .setNextPageToken(PAGE_TOKEN); + capturedCallback.onSuccess(response); + Page page = batchResult.get(); + assertEquals(PAGE_TOKEN, page.nextPageCursor()); + Iterator iterator = page.values().iterator(); + int resultSize = 0; + while (iterator.hasNext()) { + assertEquals(RECORD_SET, iterator.next()); + resultSize++; + } + assertEquals(RECORD_SET_LIST.size(), resultSize); } @Test @@ -431,7 +488,8 @@ public void testListChangeRequestsWithOptions() { batchMock.addListChangeRequests(EasyMock.eq(ZONE_NAME), EasyMock.capture(callback), EasyMock.capture(capturedOptions)); EasyMock.replay(batchMock); - dnsBatch.listChangeRequests(ZONE_NAME, CHANGE_LIST_OPTIONS); + DnsBatchResult> batchResult = + dnsBatch.listChangeRequests(ZONE_NAME, CHANGE_LIST_OPTIONS); assertNotNull(callback.getValue()); Integer size = (Integer) capturedOptions.getValue().get(CHANGE_LIST_OPTIONS[0].rpcOption()); assertEquals(MAX_SIZE, size); @@ -442,7 +500,29 @@ public void testListChangeRequestsWithOptions() { assertTrue(selector.contains(Dns.ChangeRequestField.ID.selector())); selector = (String) capturedOptions.getValue().get(CHANGE_LIST_OPTIONS[3].rpcOption()); assertTrue(selector.contains(Dns.SortingOrder.ASCENDING.selector())); - // cannot test callback success without ChangesListResponse which is final + // check the callback + ChangesListResponse response = new ChangesListResponse() + .setChanges(CHANGE_LIST) + .setNextPageToken(PAGE_TOKEN); + RpcBatch.Callback capturedCallback = callback.getValue(); + EasyMock.verify(optionsMock); + EasyMock.reset(optionsMock); + EasyMock.expect(optionsMock.service()).andReturn(dns); + EasyMock.replay(optionsMock); + capturedCallback.onSuccess(response); + Page page = batchResult.get(); + assertEquals(PAGE_TOKEN, page.nextPageCursor()); + Iterator iterator = page.values().iterator(); + int resultSize = 0; + EasyMock.verify(dns); + EasyMock.reset(dns); + EasyMock.expect(dns.options()).andReturn(optionsMock).times(CHANGE_LIST.size()); + EasyMock.replay(dns); + while (iterator.hasNext()) { + assertEquals(CHANGE_REQUEST_COMPLETE.toPb(), iterator.next().toPb()); + resultSize++; + } + assertEquals(CHANGE_LIST.size(), resultSize); } @Test @@ -476,6 +556,10 @@ public void testGetChangeRequest() { @Test public void testGetChangeRequestWithOptions() { + EasyMock.verify(dns); + EasyMock.reset(dns); + EasyMock.expect(dns.options()).andReturn(optionsMock); + EasyMock.replay(dns); EasyMock.reset(batchMock); EasyMock.reset(optionsMock); Capture> callback = Capture.newInstance(); @@ -528,6 +612,10 @@ public void testApplyChangeRequest() { @Test public void testApplyChangeRequestWithOptions() { + EasyMock.verify(dns); + EasyMock.reset(dns); + EasyMock.expect(dns.options()).andReturn(optionsMock); + EasyMock.replay(dns); EasyMock.reset(batchMock); EasyMock.reset(optionsMock); Capture> callback = Capture.newInstance(); From fcedb05b1e07474668340a9c3208c99cf109325b Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Mon, 11 Apr 2016 09:48:30 -0700 Subject: [PATCH 12/21] Added onFailure callback tests and fixed one doc string. --- .../com/google/gcloud/dns/spi/RpcBatch.java | 2 +- .../com/google/gcloud/dns/DnsBatchTest.java | 33 ++++++++++++------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java index 6741c9b2d9f9..9afaedc9f6af 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java @@ -96,7 +96,7 @@ void addListChangeRequests(String zoneName, Callback callba /** * Adds a call to "get change request" to the batch with the provided {@code callback} and {@code * options}. The change request to be retrieved is identified by {@code changeRequestId} and the - * zone to be searched by {@code zoneName}. + * zone to which the change request was applied is identified by {@code zoneName}. */ void addGetChangeRequest(String zoneName, String changeRequestId, Callback callback, Map options); diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java index 74883b11b095..674680d21c69 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java @@ -22,6 +22,7 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.services.dns.model.Change; import com.google.api.services.dns.model.ChangesListResponse; import com.google.api.services.dns.model.ManagedZone; @@ -146,7 +147,8 @@ public void testListZones() { } catch (IllegalStateException ex) { // expected } - batchResult.error(new DnsException(new IOException("expected"))); + RpcBatch.Callback capturedCallback = callback.getValue(); + capturedCallback.onFailure(new GoogleJsonError()); try { batchResult.get(); fail("Should throw a DnsException on error."); @@ -190,10 +192,11 @@ public void testListZonesWithOptions() { int resultSize = 0; EasyMock.verify(dns); EasyMock.reset(dns); - EasyMock.expect(dns.options()).andReturn(optionsMock).times(ZONE_LIST.size()); + EasyMock.expect(dns.options()).andReturn(optionsMock).times(ZONE_LIST.size() + 1); EasyMock.replay(dns); + Zone zoneInfoFunctional = new Zone(dns, new ZoneInfo.BuilderImpl(ZONE_INFO)); while (iterator.hasNext()) { - assertEquals(ZONE_INFO.toPb(), iterator.next().toPb()); + assertEquals(zoneInfoFunctional, iterator.next()); resultSize++; } assertEquals(ZONE_LIST.size(), resultSize); @@ -219,7 +222,8 @@ public void testCreateZone() { // expected } // testing error here, success is tested with options - batchResult.error(new DnsException(new IOException("expected"))); + RpcBatch.Callback capturedCallback = callback.getValue(); + capturedCallback.onFailure(new GoogleJsonError()); try { batchResult.get(); fail("Should throw a DnsException on error."); @@ -273,7 +277,8 @@ public void testGetZone() { // expected } // testing error here, success is tested with options - batchResult.error(new DnsException(new IOException("expected"))); + RpcBatch.Callback capturedCallback = callback.getValue(); + capturedCallback.onFailure(new GoogleJsonError()); try { batchResult.get(); fail("Should throw a DnsException on error."); @@ -322,7 +327,8 @@ public void testDeleteZone() { // expected } // testing error here, success is tested with options - batchResult.error(new DnsException(new IOException("expected"))); + RpcBatch.Callback capturedCallback = callback.getValue(); + capturedCallback.onFailure(new GoogleJsonError()); try { batchResult.get(); fail("Should throw a DnsException on error."); @@ -362,7 +368,8 @@ public void testGetProject() { // expected } // testing error here, success is tested with options - batchResult.error(new DnsException(new IOException("expected"))); + RpcBatch.Callback capturedCallback = callback.getValue(); + capturedCallback.onFailure(new GoogleJsonError()); try { batchResult.get(); fail("Should throw a DnsException on error."); @@ -405,7 +412,8 @@ public void testListRecordSets() { } catch (IllegalStateException ex) { // expected } - batchResult.error(new DnsException(new IOException("expected"))); + RpcBatch.Callback capturedCallback = callback.getValue(); + capturedCallback.onFailure(new GoogleJsonError()); try { batchResult.get(); fail("Should throw a DnsException on error."); @@ -471,7 +479,8 @@ public void testListChangeRequests() { } catch (IllegalStateException ex) { // expected } - batchResult.error(new DnsException(new IOException("expected"))); + RpcBatch.Callback capturedCallback = callback.getValue(); + capturedCallback.onFailure(new GoogleJsonError()); try { batchResult.get(); fail("Should throw a DnsException on error."); @@ -545,7 +554,8 @@ public void testGetChangeRequest() { // expected } // testing error here, success is tested with options - batchResult.error(new DnsException(new IOException("expected"))); + RpcBatch.Callback capturedCallback = callback.getValue(); + capturedCallback.onFailure(new GoogleJsonError()); try { batchResult.get(); fail("Should throw a DnsException on error."); @@ -601,7 +611,8 @@ public void testApplyChangeRequest() { // expected } // testing error here, success is tested with options - batchResult.error(new DnsException(new IOException("expected"))); + RpcBatch.Callback capturedCallback = callback.getValue(); + capturedCallback.onFailure(new GoogleJsonError()); try { batchResult.get(); fail("Should throw a DnsException on error."); From c2d36c8ebf5b132e0fee0c75f07156c04b620f79 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Mon, 11 Apr 2016 12:52:29 -0700 Subject: [PATCH 13/21] Extracted GoogleJsonError to a final attribute. --- .../com/google/gcloud/dns/DnsBatchTest.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java index 674680d21c69..e3cbfe71a765 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java @@ -41,7 +41,6 @@ import org.junit.Before; import org.junit.Test; -import java.io.IOException; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -95,6 +94,7 @@ public class DnsBatchTest { CHANGE_REQUEST_COMPLETE.toPb(), CHANGE_REQUEST_COMPLETE.toPb()); private static final List ZONE_LIST = ImmutableList.of(ZONE_INFO.toPb(), ZONE_INFO.toPb()); + private final GoogleJsonError GOOGLE_JSON_ERROR = new GoogleJsonError(); private DnsOptions optionsMock; private DnsRpc dnsRpcMock; @@ -148,7 +148,7 @@ public void testListZones() { // expected } RpcBatch.Callback capturedCallback = callback.getValue(); - capturedCallback.onFailure(new GoogleJsonError()); + capturedCallback.onFailure(GOOGLE_JSON_ERROR); try { batchResult.get(); fail("Should throw a DnsException on error."); @@ -223,7 +223,7 @@ public void testCreateZone() { } // testing error here, success is tested with options RpcBatch.Callback capturedCallback = callback.getValue(); - capturedCallback.onFailure(new GoogleJsonError()); + capturedCallback.onFailure(GOOGLE_JSON_ERROR); try { batchResult.get(); fail("Should throw a DnsException on error."); @@ -278,7 +278,7 @@ public void testGetZone() { } // testing error here, success is tested with options RpcBatch.Callback capturedCallback = callback.getValue(); - capturedCallback.onFailure(new GoogleJsonError()); + capturedCallback.onFailure(GOOGLE_JSON_ERROR); try { batchResult.get(); fail("Should throw a DnsException on error."); @@ -328,7 +328,7 @@ public void testDeleteZone() { } // testing error here, success is tested with options RpcBatch.Callback capturedCallback = callback.getValue(); - capturedCallback.onFailure(new GoogleJsonError()); + capturedCallback.onFailure(GOOGLE_JSON_ERROR); try { batchResult.get(); fail("Should throw a DnsException on error."); @@ -369,7 +369,7 @@ public void testGetProject() { } // testing error here, success is tested with options RpcBatch.Callback capturedCallback = callback.getValue(); - capturedCallback.onFailure(new GoogleJsonError()); + capturedCallback.onFailure(GOOGLE_JSON_ERROR); try { batchResult.get(); fail("Should throw a DnsException on error."); @@ -413,7 +413,7 @@ public void testListRecordSets() { // expected } RpcBatch.Callback capturedCallback = callback.getValue(); - capturedCallback.onFailure(new GoogleJsonError()); + capturedCallback.onFailure(GOOGLE_JSON_ERROR); try { batchResult.get(); fail("Should throw a DnsException on error."); @@ -480,7 +480,7 @@ public void testListChangeRequests() { // expected } RpcBatch.Callback capturedCallback = callback.getValue(); - capturedCallback.onFailure(new GoogleJsonError()); + capturedCallback.onFailure(GOOGLE_JSON_ERROR); try { batchResult.get(); fail("Should throw a DnsException on error."); @@ -555,7 +555,7 @@ public void testGetChangeRequest() { } // testing error here, success is tested with options RpcBatch.Callback capturedCallback = callback.getValue(); - capturedCallback.onFailure(new GoogleJsonError()); + capturedCallback.onFailure(GOOGLE_JSON_ERROR); try { batchResult.get(); fail("Should throw a DnsException on error."); @@ -612,7 +612,7 @@ public void testApplyChangeRequest() { } // testing error here, success is tested with options RpcBatch.Callback capturedCallback = callback.getValue(); - capturedCallback.onFailure(new GoogleJsonError()); + capturedCallback.onFailure(GOOGLE_JSON_ERROR); try { batchResult.get(); fail("Should throw a DnsException on error."); From 6d285a9921b17940036ffe8d9a57286503665312 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Tue, 12 Apr 2016 17:16:11 -0700 Subject: [PATCH 14/21] Fixed imports and implemented notify. --- .../google/{gcloud => cloud}/BatchResult.java | 37 ++++++++++++++----- .../{gcloud => cloud}/BatchResultTest.java | 32 +++++++++++++++- .../{gcloud => cloud}/dns/DnsBatch.java | 10 ++--- .../{gcloud => cloud}/dns/DnsBatchResult.java | 4 +- .../{gcloud => cloud}/dns/spi/RpcBatch.java | 2 +- .../dns/DnsBatchResultTest.java | 4 +- .../{gcloud => cloud}/dns/DnsBatchTest.java | 10 +++-- .../com/google/cloud/dns/it/ITDnsTest.java | 3 ++ 8 files changed, 77 insertions(+), 25 deletions(-) rename gcloud-java-core/src/main/java/com/google/{gcloud => cloud}/BatchResult.java (65%) rename gcloud-java-core/src/test/java/com/google/{gcloud => cloud}/BatchResultTest.java (64%) rename gcloud-java-dns/src/main/java/com/google/{gcloud => cloud}/dns/DnsBatch.java (98%) rename gcloud-java-dns/src/main/java/com/google/{gcloud => cloud}/dns/DnsBatchResult.java (93%) rename gcloud-java-dns/src/main/java/com/google/{gcloud => cloud}/dns/spi/RpcBatch.java (99%) rename gcloud-java-dns/src/test/java/com/google/{gcloud => cloud}/dns/DnsBatchResultTest.java (96%) rename gcloud-java-dns/src/test/java/com/google/{gcloud => cloud}/dns/DnsBatchTest.java (99%) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/BatchResult.java b/gcloud-java-core/src/main/java/com/google/cloud/BatchResult.java similarity index 65% rename from gcloud-java-core/src/main/java/com/google/gcloud/BatchResult.java rename to gcloud-java-core/src/main/java/com/google/cloud/BatchResult.java index 621fbb0278cc..29ee83c1c2c8 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/BatchResult.java +++ b/gcloud-java-core/src/main/java/com/google/cloud/BatchResult.java @@ -14,19 +14,24 @@ * limitations under the License. */ -package com.google.gcloud; +package com.google.cloud; +import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; +import java.util.LinkedList; +import java.util.List; + /** - * This class holds a single result of a batch call. {@code T} is the type of the result and - * {@code E} is the type of the service-dependent exception thrown when processing error occurs. + * This class holds a single result of a batch call. {@code T} is the type of the result and {@code + * E} is the type of the service-dependent exception thrown when processing error occurs. */ public abstract class BatchResult { private T result; private boolean submitted = false; private E error; + private List> toBeNotified = new LinkedList<>(); /** * Returns {@code true} if the batch has been submitted and the result is available; {@code false} @@ -51,27 +56,39 @@ public T get() throws E { } /** - * Registers a callback for the batch operation. + * Adds a callback for the batch operation. If the batch has been completed already, the callback + * will be invoked immediately. */ public void notify(Callback callback) { - // todo(mderka) implement - throw new UnsupportedOperationException("Not implemented yet"); + if (!submitted) { + toBeNotified.add(callback); + } else if (error != null) { + callback.error(error); + } else { + callback.success(result); + } } /** - * Sets an error and makes this submitted. + * Sets an error and makes this submitted. Notifies all callbacks. */ protected void error(E error) { - this.error = error; + this.error = checkNotNull(error); this.submitted = true; + for (Callback callback : toBeNotified) { + callback.error(error); + } } /** - * Sets a result and makes this submitted. + * Sets a result and makes this submitted. Notifies all callbacks. */ protected void success(T result) { - this.result = result; + this.result = checkNotNull(result); this.submitted = true; + for (Callback callback : toBeNotified) { + callback.success(result); + } } /** diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/BatchResultTest.java b/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java similarity index 64% rename from gcloud-java-core/src/test/java/com/google/gcloud/BatchResultTest.java rename to gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java index 93cf66ec118d..718a29b0a9ed 100644 --- a/gcloud-java-core/src/test/java/com/google/gcloud/BatchResultTest.java +++ b/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java @@ -14,13 +14,15 @@ * limitations under the License. */ -package com.google.gcloud; +package com.google.cloud; +import static org.easymock.EasyMock.expect; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import org.easymock.EasyMock; import org.junit.Before; import org.junit.Test; @@ -65,6 +67,32 @@ public void testError() { } } - // todo(mderka) test notify when implemented + @Test + public void testNotifyError() { + final BaseServiceException ex = new BaseServiceException(0, "message", "reason", false); + assertFalse(RESULT.submitted()); + BatchResult.Callback callback = + EasyMock.createStrictMock(BatchResult.Callback.class); + callback.error(ex); + callback.error(ex); + EasyMock.replay(callback); + RESULT.notify(callback); + RESULT.error(ex); + RESULT.notify(callback); + EasyMock.verify(callback); + } + @Test + public void testNotifySuccess() { + assertFalse(RESULT.submitted()); + BatchResult.Callback callback = + EasyMock.createStrictMock(BatchResult.Callback.class); + callback.success(true); + callback.success(true); + EasyMock.replay(callback); + RESULT.notify(callback); + RESULT.success(true); + RESULT.notify(callback); + EasyMock.verify(callback); + } } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsBatch.java similarity index 98% rename from gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java rename to gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsBatch.java index 29ff3da6c91a..db3472303457 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatch.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsBatch.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.google.gcloud.dns; +package com.google.cloud.dns; import static java.net.HttpURLConnection.HTTP_NOT_FOUND; @@ -28,10 +28,10 @@ import com.google.api.services.dns.model.ResourceRecordSetsListResponse; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; -import com.google.gcloud.Page; -import com.google.gcloud.PageImpl; -import com.google.gcloud.dns.spi.DnsRpc; -import com.google.gcloud.dns.spi.RpcBatch; +import com.google.cloud.Page; +import com.google.cloud.PageImpl; +import com.google.cloud.dns.spi.DnsRpc; +import com.google.cloud.dns.spi.RpcBatch; import java.util.List; import java.util.Map; diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatchResult.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsBatchResult.java similarity index 93% rename from gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatchResult.java rename to gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsBatchResult.java index b72ac34bdaed..41c217ddc60e 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsBatchResult.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsBatchResult.java @@ -14,9 +14,9 @@ * limitations under the License. */ -package com.google.gcloud.dns; +package com.google.cloud.dns; -import com.google.gcloud.BatchResult; +import com.google.cloud.BatchResult; /** * This class holds a single result of a batch call to the Cloud DNS. diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/RpcBatch.java similarity index 99% rename from gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java rename to gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/RpcBatch.java index 9afaedc9f6af..fe5b90d59306 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/RpcBatch.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/RpcBatch.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.google.gcloud.dns.spi; +package com.google.cloud.dns.spi; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.services.dns.model.Change; diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchResultTest.java b/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchResultTest.java similarity index 96% rename from gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchResultTest.java rename to gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchResultTest.java index ea3a526e309c..6012957bb2ec 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchResultTest.java +++ b/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchResultTest.java @@ -14,13 +14,15 @@ * limitations under the License. */ -package com.google.gcloud.dns; +package com.google.cloud.dns; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import com.google.cloud.dns.DnsBatchResult; + import org.junit.Before; import org.junit.Test; diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java b/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchTest.java similarity index 99% rename from gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java rename to gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchTest.java index e3cbfe71a765..2232f7186e9a 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsBatchTest.java +++ b/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.google.gcloud.dns; +package com.google.cloud.dns; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -30,10 +30,12 @@ import com.google.api.services.dns.model.Project; import com.google.api.services.dns.model.ResourceRecordSet; import com.google.api.services.dns.model.ResourceRecordSetsListResponse; +import com.google.cloud.dns.DnsBatch; +import com.google.cloud.dns.DnsBatchResult; import com.google.common.collect.ImmutableList; -import com.google.gcloud.Page; -import com.google.gcloud.dns.spi.DnsRpc; -import com.google.gcloud.dns.spi.RpcBatch; +import com.google.cloud.Page; +import com.google.cloud.dns.spi.DnsRpc; +import com.google.cloud.dns.spi.RpcBatch; import org.easymock.Capture; import org.easymock.EasyMock; 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 26be9c2eeaba..a2682cb129de 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,12 +27,15 @@ import com.google.cloud.dns.ChangeRequest; import com.google.cloud.dns.ChangeRequestInfo; import com.google.cloud.dns.Dns; +import com.google.cloud.dns.DnsBatch; +import com.google.cloud.dns.DnsBatchResult; import com.google.cloud.dns.DnsException; import com.google.cloud.dns.DnsOptions; import com.google.cloud.dns.ProjectInfo; import com.google.cloud.dns.RecordSet; import com.google.cloud.dns.Zone; import com.google.cloud.dns.ZoneInfo; +import com.google.common.collect.ImmutableList; import org.junit.AfterClass; import org.junit.BeforeClass; From 11ea45a7f070de01f79d7c755f1b179a500e9d3f Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Tue, 12 Apr 2016 17:21:32 -0700 Subject: [PATCH 15/21] Fixed import orders --- .../test/java/com/google/cloud/BatchResultTest.java | 1 - .../src/main/java/com/google/cloud/dns/Dns.java | 2 +- .../src/main/java/com/google/cloud/dns/DnsBatch.java | 5 +++-- .../main/java/com/google/cloud/dns/DnsException.java | 2 +- .../src/main/java/com/google/cloud/dns/DnsImpl.java | 12 ++++++------ .../main/java/com/google/cloud/dns/DnsOptions.java | 2 +- .../src/main/java/com/google/cloud/dns/Option.java | 2 +- .../java/com/google/cloud/dns/spi/DefaultDnsRpc.java | 1 + .../main/java/com/google/cloud/dns/spi/DnsRpc.java | 2 +- .../com/google/cloud/dns/testing/LocalDnsHelper.java | 2 +- .../com/google/cloud/dns/DnsBatchResultTest.java | 2 -- .../test/java/com/google/cloud/dns/DnsBatchTest.java | 4 +--- .../test/java/com/google/cloud/dns/DnsImplTest.java | 7 ++++--- .../java/com/google/cloud/dns/SerializationTest.java | 3 ++- .../src/test/java/com/google/cloud/dns/ZoneTest.java | 2 +- .../google/cloud/dns/testing/LocalDnsHelperTest.java | 6 +++--- 16 files changed, 27 insertions(+), 28 deletions(-) diff --git a/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java b/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java index 718a29b0a9ed..f1759bc3e059 100644 --- a/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java +++ b/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java @@ -16,7 +16,6 @@ package com.google.cloud; -import static org.easymock.EasyMock.expect; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/Dns.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/Dns.java index 2f8469f85635..38faad7854e7 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/Dns.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/Dns.java @@ -16,12 +16,12 @@ package com.google.cloud.dns; -import com.google.common.collect.ImmutableList; import com.google.cloud.FieldSelector; import com.google.cloud.FieldSelector.Helper; import com.google.cloud.Page; import com.google.cloud.Service; import com.google.cloud.dns.spi.DnsRpc; +import com.google.common.collect.ImmutableList; import java.util.List; diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsBatch.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsBatch.java index db3472303457..707c9ea8a9c2 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsBatch.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsBatch.java @@ -26,12 +26,13 @@ import com.google.api.services.dns.model.Project; import com.google.api.services.dns.model.ResourceRecordSet; import com.google.api.services.dns.model.ResourceRecordSetsListResponse; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Iterables; import com.google.cloud.Page; import com.google.cloud.PageImpl; import com.google.cloud.dns.spi.DnsRpc; import com.google.cloud.dns.spi.RpcBatch; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; + import java.util.List; import java.util.Map; diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsException.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsException.java index 06e7ea0a7200..563232e161d0 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsException.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsException.java @@ -17,10 +17,10 @@ package com.google.cloud.dns; import com.google.api.client.googleapis.json.GoogleJsonError; -import com.google.common.collect.ImmutableSet; import com.google.cloud.BaseServiceException; import com.google.cloud.RetryHelper.RetryHelperException; import com.google.cloud.RetryHelper.RetryInterruptedException; +import com.google.common.collect.ImmutableSet; import java.io.IOException; import java.util.Set; diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java index 108acb2b0ec3..f1dc99793009 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java @@ -16,23 +16,23 @@ package com.google.cloud.dns; -import static com.google.common.base.Preconditions.checkArgument; import static com.google.cloud.RetryHelper.runWithRetries; +import static com.google.common.base.Preconditions.checkArgument; import com.google.api.services.dns.model.Change; import com.google.api.services.dns.model.ManagedZone; import com.google.api.services.dns.model.Project; import com.google.api.services.dns.model.ResourceRecordSet; -import com.google.common.base.Function; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Iterables; -import com.google.common.collect.Maps; import com.google.cloud.BaseService; import com.google.cloud.Page; import com.google.cloud.PageImpl; import com.google.cloud.RetryHelper; import com.google.cloud.dns.spi.DnsRpc; +import com.google.common.base.Function; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Iterables; +import com.google.common.collect.Maps; import java.util.Map; import java.util.concurrent.Callable; diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsOptions.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsOptions.java index 059f7b212044..6d37a7af5694 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsOptions.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsOptions.java @@ -16,11 +16,11 @@ package com.google.cloud.dns; -import com.google.common.collect.ImmutableSet; import com.google.cloud.ServiceOptions; import com.google.cloud.dns.spi.DefaultDnsRpc; import com.google.cloud.dns.spi.DnsRpc; import com.google.cloud.dns.spi.DnsRpcFactory; +import com.google.common.collect.ImmutableSet; import java.util.Set; diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/Option.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/Option.java index b0e23220549c..7a1f9955be05 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/Option.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/Option.java @@ -18,8 +18,8 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.common.base.MoreObjects; import com.google.cloud.dns.spi.DnsRpc; +import com.google.common.base.MoreObjects; import java.io.Serializable; import java.util.Objects; diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java index 7c1a7a506bbb..531008a76a06 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.google.cloud.dns.spi; import static com.google.cloud.dns.spi.DnsRpc.ListResult.of; diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java index 5d5318df9810..5d0054d86379 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DnsRpc.java @@ -20,8 +20,8 @@ import com.google.api.services.dns.model.ManagedZone; import com.google.api.services.dns.model.Project; import com.google.api.services.dns.model.ResourceRecordSet; -import com.google.common.collect.ImmutableList; import com.google.cloud.dns.DnsException; +import com.google.common.collect.ImmutableList; import java.util.Map; diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/testing/LocalDnsHelper.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/testing/LocalDnsHelper.java index 46d116bdf640..1f35193409ee 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/testing/LocalDnsHelper.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/testing/LocalDnsHelper.java @@ -27,6 +27,7 @@ import com.google.api.services.dns.model.Project; import com.google.api.services.dns.model.Quota; import com.google.api.services.dns.model.ResourceRecordSet; +import com.google.cloud.dns.DnsOptions; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; @@ -36,7 +37,6 @@ import com.google.common.collect.Lists; import com.google.common.collect.Sets; import com.google.common.io.ByteStreams; -import com.google.cloud.dns.DnsOptions; import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; diff --git a/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchResultTest.java b/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchResultTest.java index 6012957bb2ec..db18dbae7f0c 100644 --- a/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchResultTest.java +++ b/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchResultTest.java @@ -21,8 +21,6 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import com.google.cloud.dns.DnsBatchResult; - import org.junit.Before; import org.junit.Test; diff --git a/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchTest.java b/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchTest.java index 2232f7186e9a..32888456aa73 100644 --- a/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchTest.java +++ b/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchTest.java @@ -30,12 +30,10 @@ import com.google.api.services.dns.model.Project; import com.google.api.services.dns.model.ResourceRecordSet; import com.google.api.services.dns.model.ResourceRecordSetsListResponse; -import com.google.cloud.dns.DnsBatch; -import com.google.cloud.dns.DnsBatchResult; -import com.google.common.collect.ImmutableList; import com.google.cloud.Page; import com.google.cloud.dns.spi.DnsRpc; import com.google.cloud.dns.spi.RpcBatch; +import com.google.common.collect.ImmutableList; import org.easymock.Capture; import org.easymock.EasyMock; diff --git a/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsImplTest.java b/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsImplTest.java index 4d57c8d16efb..5083cb23847f 100644 --- a/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsImplTest.java +++ b/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsImplTest.java @@ -22,14 +22,15 @@ import com.google.api.services.dns.model.Change; import com.google.api.services.dns.model.ManagedZone; import com.google.api.services.dns.model.ResourceRecordSet; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Lists; import com.google.cloud.Page; import com.google.cloud.RetryParams; import com.google.cloud.ServiceOptions; import com.google.cloud.dns.spi.DnsRpc; import com.google.cloud.dns.spi.DnsRpcFactory; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; + import org.easymock.Capture; import org.easymock.EasyMock; diff --git a/gcloud-java-dns/src/test/java/com/google/cloud/dns/SerializationTest.java b/gcloud-java-dns/src/test/java/com/google/cloud/dns/SerializationTest.java index 32b1bbbd0667..6f554df75d9b 100644 --- a/gcloud-java-dns/src/test/java/com/google/cloud/dns/SerializationTest.java +++ b/gcloud-java-dns/src/test/java/com/google/cloud/dns/SerializationTest.java @@ -16,12 +16,13 @@ package com.google.cloud.dns; -import com.google.common.collect.ImmutableList; import com.google.cloud.AuthCredentials; import com.google.cloud.BaseSerializationTest; import com.google.cloud.Restorable; import com.google.cloud.RetryParams; +import com.google.common.collect.ImmutableList; + import java.io.Serializable; import java.math.BigInteger; import java.util.concurrent.TimeUnit; diff --git a/gcloud-java-dns/src/test/java/com/google/cloud/dns/ZoneTest.java b/gcloud-java-dns/src/test/java/com/google/cloud/dns/ZoneTest.java index fc23316a899b..e1f6c8917647 100644 --- a/gcloud-java-dns/src/test/java/com/google/cloud/dns/ZoneTest.java +++ b/gcloud-java-dns/src/test/java/com/google/cloud/dns/ZoneTest.java @@ -30,8 +30,8 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import com.google.common.collect.ImmutableList; import com.google.cloud.Page; +import com.google.common.collect.ImmutableList; import org.junit.After; import org.junit.Before; diff --git a/gcloud-java-dns/src/test/java/com/google/cloud/dns/testing/LocalDnsHelperTest.java b/gcloud-java-dns/src/test/java/com/google/cloud/dns/testing/LocalDnsHelperTest.java index 2231d2f08974..fce958d3a126 100644 --- a/gcloud-java-dns/src/test/java/com/google/cloud/dns/testing/LocalDnsHelperTest.java +++ b/gcloud-java-dns/src/test/java/com/google/cloud/dns/testing/LocalDnsHelperTest.java @@ -27,13 +27,13 @@ import com.google.api.services.dns.model.ManagedZone; import com.google.api.services.dns.model.Project; import com.google.api.services.dns.model.ResourceRecordSet; +import com.google.cloud.dns.DnsException; +import com.google.cloud.dns.spi.DefaultDnsRpc; +import com.google.cloud.dns.spi.DnsRpc; import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; -import com.google.cloud.dns.DnsException; -import com.google.cloud.dns.spi.DefaultDnsRpc; -import com.google.cloud.dns.spi.DnsRpc; import org.junit.AfterClass; import org.junit.Before; From b5b8eb731a3bebe7b77c4343b0aa3ef2847a9138 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Tue, 12 Apr 2016 17:29:17 -0700 Subject: [PATCH 16/21] Annotated getters as @VisibleForTesting. --- .../src/test/java/com/google/cloud/BatchResultTest.java | 2 +- .../src/main/java/com/google/cloud/dns/DnsBatch.java | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java b/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java index f1759bc3e059..7bcdcac8f723 100644 --- a/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java +++ b/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Google Inc. All Rights Reserved. + * Copyright 2016 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsBatch.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsBatch.java index 707c9ea8a9c2..8a478171b3bb 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsBatch.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsBatch.java @@ -30,10 +30,10 @@ import com.google.cloud.PageImpl; import com.google.cloud.dns.spi.DnsRpc; import com.google.cloud.dns.spi.RpcBatch; +import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; - import java.util.List; import java.util.Map; @@ -52,14 +52,17 @@ public class DnsBatch { this.batch = dnsRpc.createBatch(); } + @VisibleForTesting Object batch() { return batch; } + @VisibleForTesting DnsRpc dnsRpc() { return dnsRpc; } + @VisibleForTesting DnsOptions options() { return options; } @@ -74,8 +77,8 @@ DnsOptions options() { public DnsBatchResult> listZones(Dns.ZoneListOption... options) { DnsBatchResult> result = new DnsBatchResult<>(); Map optionMap = DnsImpl.optionMap(options); - RpcBatch.Callback callback = createListZonesCallback(result, - optionMap); + RpcBatch.Callback callback = + createListZonesCallback(result, optionMap); batch.addListZones(callback, optionMap); return result; } From d6e89a7c0fdf6f90c0e2e27c532f177d67d3d9f5 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Wed, 13 Apr 2016 08:24:33 -0700 Subject: [PATCH 17/21] Fixed docs and renamed a few methods. Also consolidated mocks batch in tests. --- .../java/com/google/cloud/BatchResult.java | 24 +++---- .../com/google/cloud/BatchResultTest.java | 36 +++++------ .../java/com/google/cloud/dns/DnsBatch.java | 62 +++++++++---------- .../java/com/google/cloud/dns/DnsImpl.java | 6 +- .../google/cloud/dns/spi/DefaultDnsRpc.java | 7 +-- .../com/google/cloud/dns/spi/RpcBatch.java | 12 ++-- .../com/google/cloud/dns/DnsBatchTest.java | 41 ++++-------- .../com/google/cloud/dns/it/ITDnsTest.java | 3 +- 8 files changed, 84 insertions(+), 107 deletions(-) diff --git a/gcloud-java-core/src/main/java/com/google/cloud/BatchResult.java b/gcloud-java-core/src/main/java/com/google/cloud/BatchResult.java index 29ee83c1c2c8..1dba1f6c25be 100644 --- a/gcloud-java-core/src/main/java/com/google/cloud/BatchResult.java +++ b/gcloud-java-core/src/main/java/com/google/cloud/BatchResult.java @@ -24,31 +24,31 @@ /** * This class holds a single result of a batch call. {@code T} is the type of the result and {@code - * E} is the type of the service-dependent exception thrown when processing error occurs. + * E} is the type of the service-dependent exception thrown when a processing error occurs. */ public abstract class BatchResult { private T result; - private boolean submitted = false; + private boolean completed = false; private E error; private List> toBeNotified = new LinkedList<>(); /** - * Returns {@code true} if the batch has been submitted and the result is available; {@code false} + * Returns {@code true} if the batch has been completed and the result is available; {@code false} * otherwise. */ public boolean submitted() { - return submitted; + return completed; } /** - * Returns result of this call. + * Returns the result of this call. * - * @throws IllegalStateException if the batch has not been submitted yet + * @throws IllegalStateException if the batch has not been completed yet * @throws E if an error occurred when processing this request */ public T get() throws E { - checkState(submitted(), "Batch has not been submitted yet"); + checkState(submitted(), "Batch has not been completed yet"); if (error != null) { throw error; } @@ -60,7 +60,7 @@ public T get() throws E { * will be invoked immediately. */ public void notify(Callback callback) { - if (!submitted) { + if (!completed) { toBeNotified.add(callback); } else if (error != null) { callback.error(error); @@ -70,22 +70,22 @@ public void notify(Callback callback) { } /** - * Sets an error and makes this submitted. Notifies all callbacks. + * Sets an error and status as completed. Notifies all callbacks. */ protected void error(E error) { this.error = checkNotNull(error); - this.submitted = true; + this.completed = true; for (Callback callback : toBeNotified) { callback.error(error); } } /** - * Sets a result and makes this submitted. Notifies all callbacks. + * Sets a result and status as completed. Notifies all callbacks. */ protected void success(T result) { this.result = checkNotNull(result); - this.submitted = true; + this.completed = true; for (Callback callback : toBeNotified) { callback.success(result); } diff --git a/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java b/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java index 7bcdcac8f723..7dc63d046cd3 100644 --- a/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java +++ b/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java @@ -27,39 +27,39 @@ public class BatchResultTest { - private BatchResult RESULT; + private BatchResult result; @Before public void setUp() { - RESULT = new BatchResult() {}; + result = new BatchResult() {}; } @Test public void testSuccess() { - assertFalse(RESULT.submitted()); + assertFalse(result.submitted()); try { - RESULT.get(); + result.get(); fail("This was not submitted yet."); } catch (IllegalStateException ex) { // expected } - RESULT.success(true); - assertTrue(RESULT.get()); + result.success(true); + assertTrue(result.get()); } @Test public void testError() { - assertFalse(RESULT.submitted()); + assertFalse(result.submitted()); try { - RESULT.get(); + result.get(); fail("This was not submitted yet."); } catch (IllegalStateException ex) { // expected } BaseServiceException ex = new BaseServiceException(0, "message", "reason", false); - RESULT.error(ex); + result.error(ex); try { - RESULT.get(); + result.get(); fail("This is a failed operation and should have thrown a DnsException."); } catch (BaseServiceException real) { assertSame(ex, real); @@ -69,29 +69,29 @@ public void testError() { @Test public void testNotifyError() { final BaseServiceException ex = new BaseServiceException(0, "message", "reason", false); - assertFalse(RESULT.submitted()); + assertFalse(result.submitted()); BatchResult.Callback callback = EasyMock.createStrictMock(BatchResult.Callback.class); callback.error(ex); callback.error(ex); EasyMock.replay(callback); - RESULT.notify(callback); - RESULT.error(ex); - RESULT.notify(callback); + result.notify(callback); + result.error(ex); + result.notify(callback); EasyMock.verify(callback); } @Test public void testNotifySuccess() { - assertFalse(RESULT.submitted()); + assertFalse(result.submitted()); BatchResult.Callback callback = EasyMock.createStrictMock(BatchResult.Callback.class); callback.success(true); callback.success(true); EasyMock.replay(callback); - RESULT.notify(callback); - RESULT.success(true); - RESULT.notify(callback); + result.notify(callback); + result.success(true); + result.notify(callback); EasyMock.verify(callback); } } diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsBatch.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsBatch.java index 8a478171b3bb..cc8d656cf525 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsBatch.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsBatch.java @@ -70,9 +70,8 @@ DnsOptions options() { /** * Adds a request representing the "list zones" operation to this batch. The {@code options} can * be used to restrict the fields returned or provide page size limits in the same way as for - * {@link Dns#listZones(Dns.ZoneListOption...)}. The returned {@link DnsBatchResult} will return a - * page of zones upon calling {@link DnsBatchResult#get()} on successful completion, or it will - * throw a {@link DnsException} if the operation failed. + * {@link Dns#listZones(Dns.ZoneListOption...)}. Calling {@link DnsBatchResult#get()} on the + * return value yields a page of zones if successful and throws a {@link DnsException} otherwise. */ public DnsBatchResult> listZones(Dns.ZoneListOption... options) { DnsBatchResult> result = new DnsBatchResult<>(); @@ -86,9 +85,8 @@ public DnsBatchResult> listZones(Dns.ZoneListOption... options) { /** * Adds a request representing the "create zone" operation to this batch. The {@code options} can * be used to restrict the fields returned in the same way as for {@link Dns#create(ZoneInfo, - * Dns.ZoneOption...)}. The returned {@link DnsBatchResult} will return the created {@link Zone} - * upon calling {@link DnsBatchResult#get()} on successful completion, or it will throw a {@link - * DnsException} if the operation failed. + * Dns.ZoneOption...)}. Calling {@link DnsBatchResult#get()} on the return value yields the + * created {@link Zone} if successful and throws a {@link DnsException} otherwise. */ public DnsBatchResult createZone(ZoneInfo zone, Dns.ZoneOption... options) { DnsBatchResult result = new DnsBatchResult<>(); @@ -99,10 +97,9 @@ public DnsBatchResult createZone(ZoneInfo zone, Dns.ZoneOption... options) } /** - * Adds a request representing the "delete zone" operation to this batch. The returned {@link - * DnsBatchResult} will return {@code true} upon calling {@link DnsBatchResult#get()} on - * successful deletion, {@code false} if the zone was not found, or it will throw a {@link - * DnsException} if the operation failed. + * Adds a request representing the "delete zone" operation to this batch. Calling {@link + * DnsBatchResult#get()} on the return value yields {@code true} upon successful deletion, {@code + * false} if the zone was not found, or throws a {@link DnsException} if the operation failed. */ public DnsBatchResult deleteZone(String zoneName) { DnsBatchResult result = new DnsBatchResult<>(); @@ -114,9 +111,9 @@ public DnsBatchResult deleteZone(String zoneName) { /** * Adds a request representing the "get zone" operation to this batch. The {@code options} can be * used to restrict the fields returned in the same way as for {@link Dns#getZone(String, - * Dns.ZoneOption...)}. The returned {@link DnsBatchResult} will return the requested {@link Zone} - * upon calling {@link DnsBatchResult#get()} on successful completion, {@code null} if no such - * zone exists, or it will throw a {@link DnsException} if the operation failed. + * Dns.ZoneOption...)}. Calling {@link DnsBatchResult#get()} on the return value yields the + * requested {@link Zone} if successful, {@code null} if no such zone exists, or throws a + * {@link DnsException} if the operation failed. */ public DnsBatchResult getZone(String zoneName, Dns.ZoneOption... options) { DnsBatchResult result = new DnsBatchResult<>(); @@ -129,9 +126,9 @@ public DnsBatchResult getZone(String zoneName, Dns.ZoneOption... options) /** * Adds a request representing the "get project" operation to this batch. The {@code options} can * be used to restrict the fields returned in the same way as for {@link - * Dns#getProject(Dns.ProjectOption...)}. The returned {@link DnsBatchResult} will return the - * requested {@link ProjectInfo} upon calling {@link DnsBatchResult#get()} on successful - * completion, or it will throw a {@link DnsException} if the operation failed. + * Dns#getProject(Dns.ProjectOption...)}. Calling {@link DnsBatchResult#get()} on the return value + * yields the created {@link ProjectInfo} if successful and throws a {@link DnsException} if the + * operation failed. */ public DnsBatchResult getProject(Dns.ProjectOption... options) { DnsBatchResult result = new DnsBatchResult<>(); @@ -145,9 +142,9 @@ public DnsBatchResult getProject(Dns.ProjectOption... options) { * Adds a request representing the "list record sets" operation in the zone specified by {@code * zoneName} to this batch. The {@code options} can be used to restrict the fields returned or * provide page size limits in the same way as for {@link Dns#listRecordSets(String, - * Dns.RecordSetListOption...)}. The returned {@link DnsBatchResult} will return a page of record - * sets upon calling {@link DnsBatchResult#get()} on successful completion, or it will throw a - * {@link DnsException} if the operation failed or the zone does not exist. + * Dns.RecordSetListOption...)}. Calling {@link DnsBatchResult#get()} on the return value yields a + * page of record sets if successful and throws a {@link DnsException} if the operation failed or + * the zone does not exist. */ public DnsBatchResult> listRecordSets(String zoneName, Dns.RecordSetListOption... options) { @@ -163,9 +160,9 @@ public DnsBatchResult> listRecordSets(String zoneName, * Adds a request representing the "list change requests" operation in the zone specified by * {@code zoneName} to this batch. The {@code options} can be used to restrict the fields returned * or provide page size limits in the same way as for {@link Dns#listChangeRequests(String, - * Dns.ChangeRequestListOption...)}. The returned {@link DnsBatchResult} will return a page of - * change requests upon calling {@link DnsBatchResult#get()} on successful completion, or it will - * throw a {@link DnsException} if the operation failed or the zone does not exist. + * Dns.ChangeRequestListOption...)}. Calling {@link DnsBatchResult#get()} on the return value + * yields a page of change requests if successful and throws a {@link DnsException} if the + * operation failed or the zone does not exist. */ public DnsBatchResult> listChangeRequests(String zoneName, Dns.ChangeRequestListOption... options) { @@ -180,11 +177,10 @@ public DnsBatchResult> listChangeRequests(String zoneName, /** * Adds a request representing the "get change request" operation for the zone specified by {@code * zoneName} to this batch. The {@code options} can be used to restrict the fields returned in the - * same way as for {@link Dns#getChangeRequest(String, String, Dns.ChangeRequestOption...)}. The - * returned {@link DnsBatchResult} will return the requested {@link ChangeRequest} upon calling - * {@link DnsBatchResult#get()} on successful completion, {@code null} if the change request does - * not exist, or it will throw a {@link DnsException} if the operation failed or the zone does not - * exist. + * same way as for {@link Dns#getChangeRequest(String, String, Dns.ChangeRequestOption...)}. + * Calling {@link DnsBatchResult#get()} on the return value yields the requested {@link + * ChangeRequest} if successful, {@code null} if the change request does not exist, or throws a + * {@link DnsException} if the operation failed or the zone does not exist. */ public DnsBatchResult getChangeRequest(String zoneName, String changeRequestId, Dns.ChangeRequestOption... options) { @@ -199,9 +195,9 @@ public DnsBatchResult getChangeRequest(String zoneName, String ch * Adds a request representing the "apply change request" operation to the zone specified by * {@code zoneName} to this batch. The {@code options} can be used to restrict the fields returned * in the same way as for {@link Dns#applyChangeRequest(String, ChangeRequestInfo, - * Dns.ChangeRequestOption...)}. The returned {@link DnsBatchResult} will return the requested - * {@link ChangeRequest} upon calling {@link DnsBatchResult#get()} on successful completion, or it - * will throw a {@link DnsException} if the operation failed or the zone does not exist. + * Dns.ChangeRequestOption...)}. Calling {@link DnsBatchResult#get()} on the return value yields + * the created {@link ChangeRequest} if successful, {@code null} if the change request does not + * exist, or throws a {@link DnsException} if the operation failed or the zone does not exist. */ public DnsBatchResult applyChangeRequest(String zoneName, ChangeRequestInfo changeRequest, Dns.ChangeRequestOption... options) { @@ -228,7 +224,7 @@ public void onSuccess(ManagedZonesListResponse response) { Page zonePage = new PageImpl<>( new DnsImpl.ZonePageFetcher(options, response.getNextPageToken(), optionMap), response.getNextPageToken(), zones == null ? ImmutableList.of() - : Iterables.transform(zones, DnsImpl.pbToZoneFunction(options))); + : Iterables.transform(zones, DnsImpl.zoneFromPb(options))); result.success(zonePage); } @@ -259,7 +255,7 @@ public void onFailure(GoogleJsonError googleJsonError) { } /** - * A joint callback for both "get zone" and "create zone" operation. + * A joint callback for both "get zone" and "create zone" operations. */ private RpcBatch.Callback createZoneCallback(final DnsOptions serviceOptions, final DnsBatchResult result) { @@ -335,7 +331,7 @@ public void onFailure(GoogleJsonError googleJsonError) { } /** - * A joint callback for both "get change request" and "create change request" operation. + * A joint callback for both "get change request" and "create change request" operations. */ private RpcBatch.Callback createChangeRequestCallback(final String zoneName, final DnsBatchResult result) { diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java index f1dc99793009..7f657cb912f1 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/DnsImpl.java @@ -110,7 +110,7 @@ public Page nextPage() { dnsRpc = options.rpc(); } - static Function pbToZoneFunction(final DnsOptions options) { + static Function zoneFromPb(final DnsOptions options) { return new Function() { @Override public Zone apply( @@ -127,8 +127,6 @@ public Page listZones(ZoneListOption... options) { private static Page listZones(final DnsOptions serviceOptions, final Map optionsMap) { - // define transformation function - // this differs from the other list operations since zone is functional and requires dns service try { // get a list of managed zones final DnsRpc rpc = serviceOptions.rpc(); @@ -142,7 +140,7 @@ public DnsRpc.ListResult call() { String cursor = result.pageToken(); // transform that list into zone objects Iterable zones = result.results() == null ? ImmutableList.of() - : Iterables.transform(result.results(), pbToZoneFunction(serviceOptions)); + : Iterables.transform(result.results(), zoneFromPb(serviceOptions)); return new PageImpl<>(new ZonePageFetcher(serviceOptions, cursor, optionsMap), cursor, zones); } catch (RetryHelper.RetryHelperException e) { diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java index 531008a76a06..3d7ee9dfa017 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/DefaultDnsRpc.java @@ -68,7 +68,7 @@ private DefaultRpcBatch(BatchRequest batch) { public void addListZones(RpcBatch.Callback callback, Map options) { try { - zoneListCall(options).queue(batch, toJsonCallback(callback)); + listZonesCall(options).queue(batch, toJsonCallback(callback)); } catch (IOException ex) { throw translate(ex); } @@ -237,14 +237,14 @@ private Dns.ManagedZones.Get getZoneCall(String zoneName, Map options public ListResult listZones(Map options) throws DnsException { // fields, page token, page size try { - ManagedZonesListResponse zoneList = zoneListCall(options).execute(); + ManagedZonesListResponse zoneList = listZonesCall(options).execute(); return of(zoneList.getNextPageToken(), zoneList.getManagedZones()); } catch (IOException ex) { throw translate(ex); } } - private Dns.ManagedZones.List zoneListCall(Map options) throws IOException { + private Dns.ManagedZones.List listZonesCall(Map options) throws IOException { return dns.managedZones().list(this.options.projectId()) .setFields(FIELDS.getString(options)) .setMaxResults(PAGE_SIZE.getInt(options)) @@ -273,7 +273,6 @@ private Dns.ManagedZones.Delete deleteZoneCall(String zoneName) throws IOExcepti @Override public ListResult listRecordSets(String zoneName, Map options) throws DnsException { - try { ResourceRecordSetsListResponse response = listRecordSetsCall(zoneName, options).execute(); return of(response.getNextPageToken(), response.getRrsets()); diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/RpcBatch.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/RpcBatch.java index fe5b90d59306..154ffa4a3c99 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/RpcBatch.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/spi/RpcBatch.java @@ -73,8 +73,8 @@ void addCreateZone(ManagedZone zone, Callback callback, void addGetProject(Callback callback, Map options); /** - * Adds a call to "delete zone" to the batch with the provided {@code callback} and {@code - * options}. + * Adds a call to "delete zone" to the batch with the provided {@code callback}. The zone to be + * deleted is identified by {@code zoneName}. */ void addDeleteZone(String zoneName, Callback callback); @@ -95,16 +95,16 @@ void addListChangeRequests(String zoneName, Callback callba /** * Adds a call to "get change request" to the batch with the provided {@code callback} and {@code - * options}. The change request to be retrieved is identified by {@code changeRequestId} and the - * zone to which the change request was applied is identified by {@code zoneName}. + * options}. The change request to be retrieved is identified by {@code changeRequestId}. The zone + * to which the change request was applied is identified by {@code zoneName}. */ void addGetChangeRequest(String zoneName, String changeRequestId, Callback callback, Map options); /** * Adds a call to "apply change request" to the batch with the provided {@code callback} and - * {@code options}. The zone to which the change request should be applied is identified by {@code - * zoneName}. + * {@code options}. The parameter {@code change} is the change request to be applied. The zone to + * which the change request should be applied is identified by {@code zoneName}. */ void addApplyChangeRequest(String zoneName, Change change, Callback callback, Map options); diff --git a/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchTest.java b/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchTest.java index 32888456aa73..6e6642439ef2 100644 --- a/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchTest.java +++ b/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchTest.java @@ -94,7 +94,7 @@ public class DnsBatchTest { CHANGE_REQUEST_COMPLETE.toPb(), CHANGE_REQUEST_COMPLETE.toPb()); private static final List ZONE_LIST = ImmutableList.of(ZONE_INFO.toPb(), ZONE_INFO.toPb()); - private final GoogleJsonError GOOGLE_JSON_ERROR = new GoogleJsonError(); + private static final GoogleJsonError GOOGLE_JSON_ERROR = new GoogleJsonError(); private DnsOptions optionsMock; private DnsRpc dnsRpcMock; @@ -175,7 +175,6 @@ public void testListZonesWithOptions() { assertTrue(selector.contains(Dns.ZoneField.NAME.selector())); selector = (String) capturedOptions.getValue().get(ZONE_LIST_OPTIONS[3].rpcOption()); assertEquals(DNS_NAME, selector); - // check the callback and result // check the callback ManagedZonesListResponse response = new ManagedZonesListResponse() .setManagedZones(ZONE_LIST) @@ -235,19 +234,15 @@ public void testCreateZone() { @Test public void testCreateZoneWithOptions() { EasyMock.verify(dns); - EasyMock.reset(dns); + EasyMock.reset(dns, batchMock, optionsMock); EasyMock.expect(dns.options()).andReturn(optionsMock); - EasyMock.replay(dns); - EasyMock.reset(batchMock); - EasyMock.reset(optionsMock); EasyMock.expect(optionsMock.service()).andReturn(dns); - EasyMock.replay(optionsMock); Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); Capture capturedZone = Capture.newInstance(); batchMock.addCreateZone(EasyMock.capture(capturedZone), EasyMock.capture(callback), EasyMock.capture(capturedOptions)); - EasyMock.replay(batchMock); + EasyMock.replay(dns, batchMock, optionsMock); DnsBatchResult batchResult = dnsBatch.createZone(ZONE_INFO, ZONE_FIELDS); assertEquals(ZONE_INFO.toPb(), capturedZone.getValue()); assertNotNull(callback.getValue()); @@ -290,24 +285,20 @@ public void testGetZone() { @Test public void testGetZoneWithOptions() { EasyMock.verify(dns); - EasyMock.reset(dns); + EasyMock.reset(dns, batchMock, optionsMock); EasyMock.expect(dns.options()).andReturn(optionsMock); - EasyMock.replay(dns); - EasyMock.reset(batchMock); - EasyMock.reset(optionsMock); Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); batchMock.addGetZone(EasyMock.eq(ZONE_NAME), EasyMock.capture(callback), EasyMock.capture(capturedOptions)); - EasyMock.replay(batchMock); + EasyMock.expect(optionsMock.service()).andReturn(dns); + EasyMock.replay(dns, batchMock, optionsMock); DnsBatchResult batchResult = dnsBatch.getZone(ZONE_NAME, ZONE_FIELDS); assertNotNull(callback.getValue()); String selector = (String) capturedOptions.getValue().get(ZONE_FIELDS.rpcOption()); assertTrue(selector.contains(Dns.ZoneField.CREATION_TIME.selector())); assertTrue(selector.contains(Dns.ZoneField.NAME.selector())); RpcBatch.Callback capturedCallback = callback.getValue(); - EasyMock.expect(optionsMock.service()).andReturn(dns); - EasyMock.replay(optionsMock); capturedCallback.onSuccess(ZONE_INFO.toPb()); assertEquals(ZONE_INFO.toPb(), batchResult.get().toPb()); } @@ -567,17 +558,15 @@ public void testGetChangeRequest() { @Test public void testGetChangeRequestWithOptions() { EasyMock.verify(dns); - EasyMock.reset(dns); + EasyMock.reset(dns, batchMock, optionsMock); EasyMock.expect(dns.options()).andReturn(optionsMock); - EasyMock.replay(dns); - EasyMock.reset(batchMock); - EasyMock.reset(optionsMock); + EasyMock.expect(optionsMock.service()).andReturn(dns); Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); batchMock.addGetChangeRequest(EasyMock.eq(ZONE_NAME), EasyMock.eq(CHANGE_REQUEST_COMPLETE.generatedId()), EasyMock.capture(callback), EasyMock.capture(capturedOptions)); - EasyMock.replay(batchMock); + EasyMock.replay(dns, batchMock, optionsMock); DnsBatchResult batchResult = dnsBatch.getChangeRequest(ZONE_NAME, CHANGE_REQUEST_COMPLETE.generatedId(), CHANGE_GET_FIELDS); assertNotNull(callback.getValue()); @@ -585,8 +574,6 @@ public void testGetChangeRequestWithOptions() { assertTrue(selector.contains(Dns.ChangeRequestField.STATUS.selector())); assertTrue(selector.contains(Dns.ChangeRequestField.ID.selector())); RpcBatch.Callback capturedCallback = callback.getValue(); - EasyMock.expect(optionsMock.service()).andReturn(dns); - EasyMock.replay(optionsMock); capturedCallback.onSuccess(CHANGE_REQUEST_COMPLETE.toPb()); assertEquals(CHANGE_REQUEST_COMPLETE.toPb(), batchResult.get().toPb()); } @@ -624,24 +611,20 @@ public void testApplyChangeRequest() { @Test public void testApplyChangeRequestWithOptions() { EasyMock.verify(dns); - EasyMock.reset(dns); + EasyMock.reset(dns, batchMock, optionsMock); EasyMock.expect(dns.options()).andReturn(optionsMock); - EasyMock.replay(dns); - EasyMock.reset(batchMock); - EasyMock.reset(optionsMock); Capture> callback = Capture.newInstance(); Capture> capturedOptions = Capture.newInstance(); batchMock.addApplyChangeRequest(EasyMock.eq(ZONE_NAME), EasyMock.eq(CHANGE_REQUEST_PARTIAL.toPb()), EasyMock.capture(callback), EasyMock.capture(capturedOptions)); - EasyMock.replay(batchMock); + EasyMock.expect(optionsMock.service()).andReturn(dns); + EasyMock.replay(dns, batchMock, optionsMock); DnsBatchResult batchResult = dnsBatch.applyChangeRequest(ZONE_INFO.name(), CHANGE_REQUEST_PARTIAL, CHANGE_GET_FIELDS); String selector = (String) capturedOptions.getValue().get(CHANGE_GET_FIELDS.rpcOption()); assertTrue(selector.contains(Dns.ChangeRequestField.STATUS.selector())); assertTrue(selector.contains(Dns.ChangeRequestField.ID.selector())); - EasyMock.expect(optionsMock.service()).andReturn(dns); - EasyMock.replay(optionsMock); RpcBatch.Callback capturedCallback = callback.getValue(); capturedCallback.onSuccess(CHANGE_REQUEST_COMPLETE.toPb()); assertEquals(CHANGE_REQUEST_COMPLETE.toPb(), batchResult.get().toPb()); 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 a2682cb129de..7d3ccdf8946e 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 @@ -36,6 +36,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 org.junit.AfterClass; import org.junit.BeforeClass; @@ -977,7 +978,7 @@ public void testListZoneBatch() { Iterator iteratorBatch = batchResult.get().iterateAll(); Iterator iteratorList = DNS.listZones().iterateAll(); while (iteratorBatch.hasNext()) { - assertEquals(iteratorList.next(), iteratorBatch.next()); + assertTrue(Iterators.contains(iteratorList, iteratorBatch.next())); } } finally { DNS.delete(ZONE1.name()); From c06d68fa10170c87a7e1d8ec2f2affae41051dc4 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Wed, 13 Apr 2016 09:15:32 -0700 Subject: [PATCH 18/21] Added notify test --- .../google/cloud/dns/DnsBatchResultTest.java | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchResultTest.java b/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchResultTest.java index db18dbae7f0c..585441394a96 100644 --- a/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchResultTest.java +++ b/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchResultTest.java @@ -21,6 +21,10 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import com.google.cloud.BaseServiceException; +import com.google.cloud.BatchResult; + +import org.easymock.EasyMock; import org.junit.Before; import org.junit.Test; @@ -67,5 +71,32 @@ public void testError() { } } - // todo(mderka) test notify when implemented + @Test + public void testNotifyError() { + DnsException ex = new DnsException(new IOException("some error")); + assertFalse(result.submitted()); + BatchResult.Callback callback = + EasyMock.createStrictMock(BatchResult.Callback.class); + callback.error(ex); + callback.error(ex); + EasyMock.replay(callback); + result.notify(callback); + result.error(ex); + result.notify(callback); + EasyMock.verify(callback); + } + + @Test + public void testNotifySuccess() { + assertFalse(result.submitted()); + BatchResult.Callback callback = + EasyMock.createStrictMock(BatchResult.Callback.class); + callback.success(true); + callback.success(true); + EasyMock.replay(callback); + result.notify(callback); + result.success(true); + result.notify(callback); + EasyMock.verify(callback); + } } From e58f4fd35cf5164352bbf7c79c73bcc7cd3e53d0 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Wed, 13 Apr 2016 09:17:41 -0700 Subject: [PATCH 19/21] Renamed submitted() to completed(). --- .../src/main/java/com/google/cloud/BatchResult.java | 4 ++-- .../test/java/com/google/cloud/BatchResultTest.java | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/gcloud-java-core/src/main/java/com/google/cloud/BatchResult.java b/gcloud-java-core/src/main/java/com/google/cloud/BatchResult.java index 1dba1f6c25be..cb5d3bf2779f 100644 --- a/gcloud-java-core/src/main/java/com/google/cloud/BatchResult.java +++ b/gcloud-java-core/src/main/java/com/google/cloud/BatchResult.java @@ -37,7 +37,7 @@ public abstract class BatchResult { * Returns {@code true} if the batch has been completed and the result is available; {@code false} * otherwise. */ - public boolean submitted() { + public boolean completed() { return completed; } @@ -48,7 +48,7 @@ public boolean submitted() { * @throws E if an error occurred when processing this request */ public T get() throws E { - checkState(submitted(), "Batch has not been completed yet"); + checkState(completed(), "Batch has not been completed yet"); if (error != null) { throw error; } diff --git a/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java b/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java index 7dc63d046cd3..0a0fd1a6fef1 100644 --- a/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java +++ b/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java @@ -36,10 +36,10 @@ public void setUp() { @Test public void testSuccess() { - assertFalse(result.submitted()); + assertFalse(result.completed()); try { result.get(); - fail("This was not submitted yet."); + fail("This was not completed yet."); } catch (IllegalStateException ex) { // expected } @@ -49,10 +49,10 @@ public void testSuccess() { @Test public void testError() { - assertFalse(result.submitted()); + assertFalse(result.completed()); try { result.get(); - fail("This was not submitted yet."); + fail("This was not completed yet."); } catch (IllegalStateException ex) { // expected } @@ -69,7 +69,7 @@ public void testError() { @Test public void testNotifyError() { final BaseServiceException ex = new BaseServiceException(0, "message", "reason", false); - assertFalse(result.submitted()); + assertFalse(result.completed()); BatchResult.Callback callback = EasyMock.createStrictMock(BatchResult.Callback.class); callback.error(ex); @@ -83,7 +83,7 @@ public void testNotifyError() { @Test public void testNotifySuccess() { - assertFalse(result.submitted()); + assertFalse(result.completed()); BatchResult.Callback callback = EasyMock.createStrictMock(BatchResult.Callback.class); callback.success(true); From 5cbf03ecc53db80044beac79e1426bb20cfe513d Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Wed, 13 Apr 2016 10:39:35 -0700 Subject: [PATCH 20/21] Consolidated more tests and added exception to notify. --- .../java/com/google/cloud/BatchResult.java | 19 ++++++------- .../com/google/cloud/BatchResultTest.java | 16 ++++++++--- .../google/cloud/dns/DnsBatchResultTest.java | 28 ++++++++++++------- .../com/google/cloud/dns/DnsBatchTest.java | 14 ++-------- .../com/google/cloud/dns/it/ITDnsTest.java | 3 +- 5 files changed, 43 insertions(+), 37 deletions(-) diff --git a/gcloud-java-core/src/main/java/com/google/cloud/BatchResult.java b/gcloud-java-core/src/main/java/com/google/cloud/BatchResult.java index cb5d3bf2779f..3a5464c15de1 100644 --- a/gcloud-java-core/src/main/java/com/google/cloud/BatchResult.java +++ b/gcloud-java-core/src/main/java/com/google/cloud/BatchResult.java @@ -56,24 +56,23 @@ public T get() throws E { } /** - * Adds a callback for the batch operation. If the batch has been completed already, the callback - * will be invoked immediately. + * Adds a callback for the batch operation. + * + * @throws IllegalStateException if the batch has been completed already */ public void notify(Callback callback) { - if (!completed) { - toBeNotified.add(callback); - } else if (error != null) { - callback.error(error); - } else { - callback.success(result); + if (completed) { + throw new IllegalStateException("The batch has been completed. All the calls to the notify()" + + " method should be done prior to submitting the batch."); } + toBeNotified.add(callback); } /** * Sets an error and status as completed. Notifies all callbacks. */ protected void error(E error) { - this.error = checkNotNull(error); + this.error = error; this.completed = true; for (Callback callback : toBeNotified) { callback.error(error); @@ -84,7 +83,7 @@ protected void error(E error) { * Sets a result and status as completed. Notifies all callbacks. */ protected void success(T result) { - this.result = checkNotNull(result); + this.result = result; this.completed = true; for (Callback callback : toBeNotified) { callback.success(result); diff --git a/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java b/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java index 0a0fd1a6fef1..8af3b245ff50 100644 --- a/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java +++ b/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java @@ -73,11 +73,15 @@ public void testNotifyError() { BatchResult.Callback callback = EasyMock.createStrictMock(BatchResult.Callback.class); callback.error(ex); - callback.error(ex); EasyMock.replay(callback); result.notify(callback); result.error(ex); - result.notify(callback); + try { + result.notify(callback); + fail("The batch has been completed."); + } catch (IllegalStateException exception) { + // expected + } EasyMock.verify(callback); } @@ -87,11 +91,15 @@ public void testNotifySuccess() { BatchResult.Callback callback = EasyMock.createStrictMock(BatchResult.Callback.class); callback.success(true); - callback.success(true); EasyMock.replay(callback); result.notify(callback); result.success(true); - result.notify(callback); + try { + result.notify(callback); + fail("The batch has been completed."); + } catch (IllegalStateException exception) { + // expected + } EasyMock.verify(callback); } } diff --git a/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchResultTest.java b/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchResultTest.java index 585441394a96..5a96862e8e3a 100644 --- a/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchResultTest.java +++ b/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchResultTest.java @@ -41,10 +41,10 @@ public void setUp() { @Test public void testSuccess() { - assertFalse(result.submitted()); + assertFalse(result.completed()); try { result.get(); - fail("This was not submitted yet."); + fail("This was not completed yet."); } catch (IllegalStateException ex) { // expected } @@ -54,10 +54,10 @@ public void testSuccess() { @Test public void testError() { - assertFalse(result.submitted()); + assertFalse(result.completed()); try { result.get(); - fail("This was not submitted yet."); + fail("This was not completed yet."); } catch (IllegalStateException ex) { // expected } @@ -74,29 +74,37 @@ public void testError() { @Test public void testNotifyError() { DnsException ex = new DnsException(new IOException("some error")); - assertFalse(result.submitted()); + assertFalse(result.completed()); BatchResult.Callback callback = EasyMock.createStrictMock(BatchResult.Callback.class); callback.error(ex); - callback.error(ex); EasyMock.replay(callback); result.notify(callback); result.error(ex); - result.notify(callback); + try { + result.notify(callback); + fail("The batch has been completed."); + } catch (IllegalStateException exception) { + // expected + } EasyMock.verify(callback); } @Test public void testNotifySuccess() { - assertFalse(result.submitted()); + assertFalse(result.completed()); BatchResult.Callback callback = EasyMock.createStrictMock(BatchResult.Callback.class); callback.success(true); - callback.success(true); EasyMock.replay(callback); result.notify(callback); result.success(true); - result.notify(callback); + try { + result.notify(callback); + fail("The batch has been completed."); + } catch (IllegalStateException exception) { + // expected + } EasyMock.verify(callback); } } diff --git a/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchTest.java b/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchTest.java index 6e6642439ef2..7a5fd0d2602b 100644 --- a/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchTest.java +++ b/gcloud-java-dns/src/test/java/com/google/cloud/dns/DnsBatchTest.java @@ -109,19 +109,13 @@ public void setUp() { batchMock = EasyMock.createMock(RpcBatch.class); EasyMock.expect(optionsMock.rpc()).andReturn(dnsRpcMock); EasyMock.expect(dnsRpcMock.createBatch()).andReturn(batchMock); - EasyMock.replay(optionsMock); - EasyMock.replay(dnsRpcMock); - EasyMock.replay(batchMock); - EasyMock.replay(dns); + EasyMock.replay(optionsMock, dnsRpcMock, batchMock, dns); dnsBatch = new DnsBatch(optionsMock); } @After public void tearDown() { - EasyMock.verify(batchMock); - EasyMock.verify(dnsRpcMock); - EasyMock.verify(optionsMock); - EasyMock.verify(dns); + EasyMock.verify(batchMock, dnsRpcMock, optionsMock, dns); } @Test @@ -233,7 +227,6 @@ public void testCreateZone() { @Test public void testCreateZoneWithOptions() { - EasyMock.verify(dns); EasyMock.reset(dns, batchMock, optionsMock); EasyMock.expect(dns.options()).andReturn(optionsMock); EasyMock.expect(optionsMock.service()).andReturn(dns); @@ -284,7 +277,6 @@ public void testGetZone() { @Test public void testGetZoneWithOptions() { - EasyMock.verify(dns); EasyMock.reset(dns, batchMock, optionsMock); EasyMock.expect(dns.options()).andReturn(optionsMock); Capture> callback = Capture.newInstance(); @@ -557,7 +549,6 @@ public void testGetChangeRequest() { @Test public void testGetChangeRequestWithOptions() { - EasyMock.verify(dns); EasyMock.reset(dns, batchMock, optionsMock); EasyMock.expect(dns.options()).andReturn(optionsMock); EasyMock.expect(optionsMock.service()).andReturn(dns); @@ -610,7 +601,6 @@ public void testApplyChangeRequest() { @Test public void testApplyChangeRequestWithOptions() { - EasyMock.verify(dns); EasyMock.reset(dns, batchMock, optionsMock); EasyMock.expect(dns.options()).andReturn(optionsMock); Capture> callback = Capture.newInstance(); 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 7d3ccdf8946e..c511ef3672e2 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 @@ -974,9 +974,10 @@ public void testListZoneBatch() { DnsBatch batch = DNS.batch(); DnsBatchResult> batchResult = batch.listZones(); batch.submit(); - assertTrue(batchResult.submitted()); + 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())); } From 1c60ba0f1d16e18433af8dbaf86f1bb90498eeff Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Wed, 13 Apr 2016 10:42:13 -0700 Subject: [PATCH 21/21] Added test for null result in BatchResult. --- .../src/test/java/com/google/cloud/BatchResultTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java b/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java index 8af3b245ff50..85172170144d 100644 --- a/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java +++ b/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java @@ -45,6 +45,8 @@ public void testSuccess() { } result.success(true); assertTrue(result.get()); + // test that null is allowed + result.success(null); } @Test