diff --git a/README.md b/README.md index 52229f6d5d34..6061d9dd4c8f 100644 --- a/README.md +++ b/README.md @@ -252,7 +252,7 @@ Zone zone = dns.create(zoneInfo); The second snippet shows how to create records inside a zone. The complete code can be found on [CreateOrUpdateRecordSets.java](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateOrUpdateRecordSets.java). ```java -import com.google.gcloud.dns.ChangeRequest; +import com.google.gcloud.dns.ChangeRequestInfo; import com.google.gcloud.dns.Dns; import com.google.gcloud.dns.DnsOptions; import com.google.gcloud.dns.RecordSet; @@ -269,7 +269,7 @@ RecordSet toCreate = RecordSet.builder("www.someexampledomain.com.", RecordSet.T .ttl(24, TimeUnit.HOURS) .addRecord(ip) .build(); -ChangeRequest.Builder changeBuilder = ChangeRequest.builder().add(toCreate); +ChangeRequestInfo.Builder changeBuilder = ChangeRequestInfo.builder().add(toCreate); // Verify that the record does not exist yet. // If it does exist, we will overwrite it with our prepared record. @@ -282,7 +282,7 @@ while (recordSetIterator.hasNext()) { } } -ChangeRequest changeRequest = changeBuilder.build(); +ChangeRequestInfo changeRequest = changeBuilder.build(); zone.applyChangeRequest(changeRequest); ``` diff --git a/gcloud-java-dns/README.md b/gcloud-java-dns/README.md index a2c3238d1f8f..d2e4c85b3b76 100644 --- a/gcloud-java-dns/README.md +++ b/gcloud-java-dns/README.md @@ -159,7 +159,7 @@ our zone that creates a record set of type A and points URL www.someexampledomai IP address 12.13.14.15. Start by adding ```java -import com.google.gcloud.dns.ChangeRequest; +import com.google.gcloud.dns.ChangeRequestInfo; import com.google.gcloud.dns.RecordSet; import java.util.concurrent.TimeUnit; @@ -176,7 +176,7 @@ RecordSet toCreate = RecordSet.builder("www." + zone.dnsName(), RecordSet.Type.A .build(); // Make a change -ChangeRequest changeRequest = ChangeRequest.builder().add(toCreate).build(); +ChangeRequestInfo changeRequest = ChangeRequestInfo.builder().add(toCreate).build(); // Build and apply the change request to our zone changeRequest = zone.applyChangeRequest(changeRequest); @@ -198,7 +198,7 @@ and in the code ```java // Make a change -ChangeRequest.Builder changeBuilder = ChangeRequest.builder().add(toCreate); +ChangeRequestInfo.Builder changeBuilder = ChangeRequestInfo.builder().add(toCreate); // Verify the type A record does not exist yet. // If it does exist, we will overwrite it with our prepared record. @@ -211,7 +211,7 @@ while (recordSetIterator.hasNext()) { } // Build and apply the change request to our zone -ChangeRequest changeRequest = changeBuilder.build(); +ChangeRequestInfo changeRequest = changeBuilder.build(); zone.applyChangeRequest(changeRequest); ``` You can find more information about changes in the [Cloud DNS documentation] (https://cloud.google.com/dns/what-is-cloud-dns#cloud_dns_api_concepts). @@ -220,7 +220,7 @@ When the change request is applied, it is registered with the Cloud DNS service can wait for its completion as follows: ```java -while (ChangeRequest.Status.PENDING.equals(changeRequest.status())) { +while (ChangeRequestInfo.Status.PENDING.equals(changeRequest.status())) { try { Thread.sleep(500L); } catch (InterruptedException e) { @@ -262,9 +262,17 @@ while (recordSetIterator.hasNext()) { } ``` -You can also list the history of change requests that were applied to a zone: +You can also list the history of change requests that were applied to a zone. +First add: ```java +import java.util.ChangeRequest; +``` + +and then: + +```java + // List the change requests applied to a particular zone Iterator changeIterator = zone.listChangeRequests().iterateAll(); System.out.println(String.format("The history of changes in %s:", zone.name())); @@ -280,7 +288,7 @@ First, you need to empty the zone by deleting all its records except for the def ```java // Make a change for deleting the record sets -changeBuilder = ChangeRequest.builder(); +changeBuilder = ChangeRequestInfo.builder(); while (recordIterator.hasNext()) { RecordSet current = recordIterator.next(); // SOA and NS records cannot be deleted @@ -290,14 +298,14 @@ while (recordIterator.hasNext()) { } // Build and apply the change request to our zone if it contains records to delete -ChangeRequest changeRequest = changeBuilder.build(); +ChangeRequestInfo changeRequest = changeBuilder.build(); if (!changeRequest.deletions().isEmpty()) { changeRequest = dns.applyChangeRequest(zoneName, changeRequest); // Wait for change to finish, but save data traffic by transferring only ID and status Dns.ChangeRequestOption option = Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS); - while (ChangeRequest.Status.PENDING.equals(changeRequest.status())) { + while (ChangeRequestInfo.Status.PENDING.equals(changeRequest.status())) { System.out.println("Waiting for change to complete. Going to sleep for 500ms..."); try { Thread.sleep(500); diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java index 757d844cc3da..4b6369976ca6 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java @@ -20,298 +20,178 @@ import com.google.api.services.dns.model.Change; import com.google.common.base.Function; -import com.google.common.base.MoreObjects; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Lists; -import org.joda.time.DateTime; -import org.joda.time.format.ISODateTimeFormat; - -import java.io.Serializable; -import java.util.LinkedList; +import java.io.IOException; +import java.io.ObjectInputStream; import java.util.List; import java.util.Objects; /** - * A class representing an atomic update to a collection of {@link RecordSet}s within a {@code - * Zone}. + * An immutable class representing an atomic update to a collection of {@link RecordSet}s within a + * {@code Zone}. * * @see Google Cloud DNS documentation */ -public class ChangeRequest implements Serializable { - - static final Function FROM_PB_FUNCTION = - new Function() { - @Override - public ChangeRequest apply(com.google.api.services.dns.model.Change pb) { - return ChangeRequest.fromPb(pb); - } - }; - private static final long serialVersionUID = -9027378042756366333L; - private final List additions; - private final List deletions; - private final String id; - private final Long startTimeMillis; - private final Status status; +public class ChangeRequest extends ChangeRequestInfo { - /** - * This enumerates the possible states of a {@code ChangeRequest}. - * - * @see Google Cloud DNS - * documentation - */ - public enum Status { - PENDING, - DONE - } + private static final long serialVersionUID = 5335667200595081449L; + private final DnsOptions options; + private final String zone; + private transient Dns dns; /** * A builder for {@code ChangeRequest}s. */ - public static class Builder { + public static class Builder extends ChangeRequestInfo.Builder { - private List additions = new LinkedList<>(); - private List deletions = new LinkedList<>(); - private String id; - private Long startTimeMillis; - private Status status; + private final Dns dns; + private final String zone; + private final ChangeRequestInfo.BuilderImpl infoBuilder; private Builder(ChangeRequest cr) { - this.additions = Lists.newLinkedList(cr.additions()); - this.deletions = Lists.newLinkedList(cr.deletions()); - this.id = cr.id(); - this.startTimeMillis = cr.startTimeMillis(); - this.status = cr.status(); + this.dns = cr.dns; + this.zone = cr.zone; + this.infoBuilder = new ChangeRequestInfo.BuilderImpl(cr); } - private Builder() { - } - - /** - * Sets a collection of {@link RecordSet}s which are to be added to the zone upon executing this - * {@code ChangeRequest}. - */ + @Override public Builder additions(List additions) { - this.additions = Lists.newLinkedList(checkNotNull(additions)); + infoBuilder.additions(additions); return this; } - /** - * Sets a collection of {@link RecordSet}s which are to be deleted from the zone upon executing - * this {@code ChangeRequest}. - */ + @Override public Builder deletions(List deletions) { - this.deletions = Lists.newLinkedList(checkNotNull(deletions)); + infoBuilder.deletions(deletions); return this; } - /** - * Adds a {@link RecordSet} to be added to the zone upon executing this {@code - * ChangeRequest}. - */ + @Override public Builder add(RecordSet recordSet) { - this.additions.add(checkNotNull(recordSet)); + infoBuilder.add(recordSet); return this; } - /** - * Adds a {@link RecordSet} to be deleted to the zone upon executing this - * {@code ChangeRequest}. - */ + @Override public Builder delete(RecordSet recordSet) { - this.deletions.add(checkNotNull(recordSet)); + infoBuilder.delete(recordSet); return this; } - /** - * Clears the collection of {@link RecordSet}s which are to be added to the zone upon executing - * this {@code ChangeRequest}. - */ + @Override public Builder clearAdditions() { - this.additions.clear(); + infoBuilder.clearAdditions(); return this; } - /** - * Clears the collection of {@link RecordSet}s which are to be deleted from the zone upon - * executing this {@code ChangeRequest}. - */ + @Override public Builder clearDeletions() { - this.deletions.clear(); + infoBuilder.clearDeletions(); return this; } - /** - * Removes a single {@link RecordSet} from the collection of records to be - * added to the zone upon executing this {@code ChangeRequest}. - */ + @Override public Builder removeAddition(RecordSet recordSet) { - this.additions.remove(recordSet); + infoBuilder.removeAddition(recordSet); return this; } - /** - * Removes a single {@link RecordSet} from the collection of records to be - * deleted from the zone upon executing this {@code ChangeRequest}. - */ + @Override public Builder removeDeletion(RecordSet recordSet) { - this.deletions.remove(recordSet); + infoBuilder.removeDeletion(recordSet); return this; } - /** - * Associates a server-assigned id to this {@code ChangeRequest}. - */ + @Override Builder id(String id) { - this.id = checkNotNull(id); + infoBuilder.id(id); return this; } - /** - * Sets the time when this {@code ChangeRequest} was started by a server. - */ + @Override Builder startTimeMillis(long startTimeMillis) { - this.startTimeMillis = startTimeMillis; + infoBuilder.startTimeMillis(startTimeMillis); return this; } - /** - * Sets the current status of this {@code ChangeRequest}. - */ + @Override Builder status(Status status) { - this.status = checkNotNull(status); + infoBuilder.status(status); return this; } - /** - * Creates a {@code ChangeRequest} instance populated by the values associated with this - * builder. - */ + @Override public ChangeRequest build() { - return new ChangeRequest(this); + return new ChangeRequest(dns, zone, infoBuilder); } } - private ChangeRequest(Builder builder) { - this.additions = ImmutableList.copyOf(builder.additions); - this.deletions = ImmutableList.copyOf(builder.deletions); - this.id = builder.id; - this.startTimeMillis = builder.startTimeMillis; - this.status = builder.status; - } - - /** - * Returns an empty builder for the {@code ChangeRequest} class. - */ - public static Builder builder() { - return new Builder(); - } - - /** - * Creates a builder populated with values of this {@code ChangeRequest}. - */ - public Builder toBuilder() { - return new Builder(this); - } - - /** - * Returns the list of {@link RecordSet}s to be added to the zone upon submitting this {@code - * ChangeRequest}. - */ - public List additions() { - return additions; + ChangeRequest(Dns dns, String zone, ChangeRequest.BuilderImpl infoBuilder) { + super(infoBuilder); + this.zone = checkNotNull(zone); + this.dns = checkNotNull(dns); + this.options = dns.options(); } /** - * Returns the list of {@link RecordSet}s to be deleted from the zone upon submitting this {@code - * ChangeRequest}. + * Returns the name of the {@link Zone} associated with this change request. */ - public List deletions() { - return deletions; + public String zone() { + return this.zone; } /** - * Returns the id assigned to this {@code ChangeRequest} by the server. + * Returns the change request's {@code Dns} object used to issue requests. */ - public String id() { - return id; + public Dns dns() { + return dns; } /** - * Returns the time when this {@code ChangeRequest} was started by the server. + * Applies this change request to the associated zone. */ - public Long startTimeMillis() { - return startTimeMillis; + public ChangeRequest applyTo(Dns.ChangeRequestOption... options) { + return dns.applyChangeRequest(zone, this, options); } - /** - * Returns the status of this {@code ChangeRequest}. - */ - public Status status() { - return status; + @Override + public Builder toBuilder() { + return new Builder(this); } - com.google.api.services.dns.model.Change toPb() { - com.google.api.services.dns.model.Change pb = - new com.google.api.services.dns.model.Change(); - // set id - if (id() != null) { - pb.setId(id()); - } - // set timestamp - if (startTimeMillis() != null) { - pb.setStartTime(ISODateTimeFormat.dateTime().withZoneUTC().print(startTimeMillis())); - } - // set status - if (status() != null) { - pb.setStatus(status().name().toLowerCase()); + @Override + public boolean equals(Object obj) { + if (obj == null || !obj.getClass().equals(ChangeRequest.class)) { + return false; + } else { + ChangeRequest other = (ChangeRequest) obj; + return Objects.equals(options, other.options) + && Objects.equals(zone, other.zone) + && Objects.equals(toPb(), other.toPb()); } - // set a list of additions - pb.setAdditions(Lists.transform(additions(), RecordSet.TO_PB_FUNCTION)); - // set a list of deletions - pb.setDeletions(Lists.transform(deletions(), RecordSet.TO_PB_FUNCTION)); - return pb; } - static ChangeRequest fromPb(com.google.api.services.dns.model.Change pb) { - Builder builder = builder(); - if (pb.getId() != null) { - builder.id(pb.getId()); - } - if (pb.getStartTime() != null) { - builder.startTimeMillis(DateTime.parse(pb.getStartTime()).getMillis()); - } - if (pb.getStatus() != null) { - // we are assuming that status indicated in pb is a lower case version of the enum name - builder.status(ChangeRequest.Status.valueOf(pb.getStatus().toUpperCase())); - } - if (pb.getDeletions() != null) { - builder.deletions(Lists.transform(pb.getDeletions(), RecordSet.FROM_PB_FUNCTION)); - } - if (pb.getAdditions() != null) { - builder.additions(Lists.transform(pb.getAdditions(), RecordSet.FROM_PB_FUNCTION)); - } - return builder.build(); + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), options, zone); } - @Override - public boolean equals(Object other) { - return (other instanceof ChangeRequest) && toPb().equals(((ChangeRequest) other).toPb()); + private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException { + input.defaultReadObject(); + this.dns = options.service(); } - @Override - public int hashCode() { - return Objects.hash(additions, deletions, id, startTimeMillis, status); + static ChangeRequest fromPb(Dns dns, String zoneName, Change pb) { + ChangeRequestInfo info = ChangeRequestInfo.fromPb(pb); + return new ChangeRequest(dns, zoneName, new ChangeRequestInfo.BuilderImpl(info)); } - @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .add("additions", additions) - .add("deletions", deletions) - .add("id", id) - .add("startTimeMillis", startTimeMillis) - .add("status", status) - .toString(); + static Function fromPbFunction(final Dns dns, final String zoneName) { + return new Function() { + @Override + public ChangeRequest apply(com.google.api.services.dns.model.Change pb) { + return ChangeRequest.fromPb(dns, zoneName, pb); + } + }; } } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequestInfo.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequestInfo.java new file mode 100644 index 000000000000..b63b4f4a0788 --- /dev/null +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequestInfo.java @@ -0,0 +1,358 @@ +/* + * 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 com.google.common.base.Preconditions.checkNotNull; + +import com.google.api.services.dns.model.Change; +import com.google.common.base.Function; +import com.google.common.base.MoreObjects; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; + +import org.joda.time.DateTime; +import org.joda.time.format.ISODateTimeFormat; + +import java.io.Serializable; +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; + +/** + * A class representing an atomic update to a collection of {@link RecordSet}s within a {@code + * Zone}. + * + * @see Google Cloud DNS documentation + */ +public class ChangeRequestInfo implements Serializable { + + static final Function FROM_PB_FUNCTION = + new Function() { + @Override + public ChangeRequestInfo apply(Change pb) { + return ChangeRequestInfo.fromPb(pb); + } + }; + private static final long serialVersionUID = -9027378042756366333L; + private final List additions; + private final List deletions; + private final String id; + private final Long startTimeMillis; + private final ChangeRequestInfo.Status status; + + /** + * This enumerates the possible states of a change request. + * + * @see Google Cloud DNS + * documentation + */ + public enum Status { + PENDING, + DONE + } + + /** + * A builder for {@code ChangeRequestInfo}. + */ + public abstract static class Builder { + + /** + * Sets a collection of {@link RecordSet}s which are to be added to the zone upon executing this + * {@code ChangeRequestInfo}. + */ + public abstract Builder additions(List additions); + + /** + * Sets a collection of {@link RecordSet}s which are to be deleted from the zone upon executing + * this {@code ChangeRequestInfo}. + */ + public abstract Builder deletions(List deletions); + + /** + * Adds a {@link RecordSet} to be added to the zone upon executing this {@code + * ChangeRequestInfo}. + */ + public abstract Builder add(RecordSet recordSet); + + /** + * Adds a {@link RecordSet} to be deleted to the zone upon executing this + * {@code ChangeRequestInfo}. + */ + public abstract Builder delete(RecordSet recordSet); + + /** + * Clears the collection of {@link RecordSet}s which are to be added to the zone upon executing + * this {@code ChangeRequestInfo}. + */ + public abstract Builder clearAdditions(); + + /** + * Clears the collection of {@link RecordSet}s which are to be deleted from the zone upon + * executing this {@code ChangeRequestInfo}. + */ + public abstract Builder clearDeletions(); + + /** + * Removes a single {@link RecordSet} from the collection of records to be + * added to the zone upon executing this {@code ChangeRequestInfo}. + */ + public abstract Builder removeAddition(RecordSet recordSet); + + /** + * Removes a single {@link RecordSet} from the collection of records to be + * deleted from the zone upon executing this {@code ChangeRequestInfo}. + */ + public abstract Builder removeDeletion(RecordSet recordSet); + + /** + * Associates a server-assigned id to this {@code ChangeRequestInfo}. + */ + abstract Builder id(String id); + + /** + * Sets the time when this change request was started by a server. + */ + abstract Builder startTimeMillis(long startTimeMillis); + + /** + * Sets the current status of this {@code ChangeRequest}. + */ + abstract Builder status(ChangeRequest.Status status); + + /** + * Creates a {@code ChangeRequestInfo} instance populated by the values associated with this + * builder. + */ + public abstract ChangeRequestInfo build(); + } + + static class BuilderImpl extends Builder { + private List additions; + private List deletions; + private String id; + private Long startTimeMillis; + private ChangeRequestInfo.Status status; + + BuilderImpl() { + this.additions = new LinkedList<>(); + this.deletions = new LinkedList<>(); + } + + BuilderImpl(ChangeRequestInfo info) { + this.additions = Lists.newLinkedList(info.additions()); + this.deletions = Lists.newLinkedList(info.deletions()); + this.id = info.id(); + this.startTimeMillis = info.startTimeMillis; + this.status = info.status; + } + + @Override + public Builder additions(List additions) { + this.additions = Lists.newLinkedList(checkNotNull(additions)); + return this; + } + + @Override + public Builder deletions(List deletions) { + this.deletions = Lists.newLinkedList(checkNotNull(deletions)); + return this; + } + + @Override + public Builder add(RecordSet recordSet) { + this.additions.add(checkNotNull(recordSet)); + return this; + } + + @Override + public Builder delete(RecordSet recordSet) { + this.deletions.add(checkNotNull(recordSet)); + return this; + } + + @Override + public Builder clearAdditions() { + this.additions.clear(); + return this; + } + + @Override + public Builder clearDeletions() { + this.deletions.clear(); + return this; + } + + @Override + public Builder removeAddition(RecordSet recordSet) { + this.additions.remove(recordSet); + return this; + } + + @Override + public Builder removeDeletion(RecordSet recordSet) { + this.deletions.remove(recordSet); + return this; + } + + @Override + public ChangeRequestInfo build() { + return new ChangeRequestInfo(this); + } + + @Override + Builder id(String id) { + this.id = checkNotNull(id); + return this; + } + + @Override + Builder startTimeMillis(long startTimeMillis) { + this.startTimeMillis = startTimeMillis; + return this; + } + + @Override + Builder status(ChangeRequestInfo.Status status) { + this.status = checkNotNull(status); + return this; + } + } + + ChangeRequestInfo(BuilderImpl builder) { + this.additions = ImmutableList.copyOf(builder.additions); + this.deletions = ImmutableList.copyOf(builder.deletions); + this.id = builder.id; + this.startTimeMillis = builder.startTimeMillis; + this.status = builder.status; + } + + /** + * Returns an empty builder for the {@code ChangeRequestInfo} class. + */ + public static Builder builder() { + return new BuilderImpl(); + } + + /** + * Creates a builder populated with values of this {@code ChangeRequestInfo}. + */ + public Builder toBuilder() { + return new BuilderImpl(this); + } + + /** + * Returns the list of {@link RecordSet}s to be added to the zone upon submitting this change + * request. + */ + public List additions() { + return additions; + } + + /** + * Returns the list of {@link RecordSet}s to be deleted from the zone upon submitting this change + * request. + */ + public List deletions() { + return deletions; + } + + /** + * Returns the id assigned to this {@code ChangeRequest} by the server. + */ + public String id() { + return id; + } + + /** + * Returns the time when this {@code ChangeRequest} was started by the server. + */ + public Long startTimeMillis() { + return startTimeMillis; + } + + /** + * Returns the status of this {@code ChangeRequest}. If the change request has not been applied + * yet, the status is {@code PENDING}. + */ + public ChangeRequestInfo.Status status() { + return status; + } + + Change toPb() { + Change pb = new Change(); + // set id + if (id() != null) { + pb.setId(id()); + } + // set timestamp + if (startTimeMillis() != null) { + pb.setStartTime(ISODateTimeFormat.dateTime().withZoneUTC().print(startTimeMillis())); + } + // set status + if (status() != null) { + pb.setStatus(status().name().toLowerCase()); + } + // set a list of additions + pb.setAdditions(Lists.transform(additions(), RecordSet.TO_PB_FUNCTION)); + // set a list of deletions + pb.setDeletions(Lists.transform(deletions(), RecordSet.TO_PB_FUNCTION)); + return pb; + } + + static ChangeRequestInfo fromPb(Change pb) { + Builder builder = builder(); + if (pb.getId() != null) { + builder.id(pb.getId()); + } + if (pb.getStartTime() != null) { + builder.startTimeMillis(DateTime.parse(pb.getStartTime()).getMillis()); + } + if (pb.getStatus() != null) { + // we are assuming that status indicated in pb is a lower case version of the enum name + builder.status(ChangeRequest.Status.valueOf(pb.getStatus().toUpperCase())); + } + if (pb.getDeletions() != null) { + builder.deletions(Lists.transform(pb.getDeletions(), RecordSet.FROM_PB_FUNCTION)); + } + if (pb.getAdditions() != null) { + builder.additions(Lists.transform(pb.getAdditions(), RecordSet.FROM_PB_FUNCTION)); + } + return builder.build(); + } + + @Override + public boolean equals(Object other) { + return other != null && other.getClass().equals(ChangeRequestInfo.class) + && other instanceof ChangeRequestInfo && toPb().equals(((ChangeRequestInfo) other).toPb()); + } + + @Override + public int hashCode() { + return Objects.hash(additions, deletions, id, startTimeMillis, status); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("additions", additions) + .add("deletions", deletions) + .add("id", id) + .add("startTimeMillis", startTimeMillis) + .add("status", status) + .toString(); + } +} diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java index f8614a8d6169..f2b42f30a9f6 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java @@ -138,7 +138,7 @@ static String selector(RecordSetField... fields) { * The fields of a change request. * *

These values can be used to specify the fields to include in a partial response when calling - * {@link Dns#applyChangeRequest(String, ChangeRequest, ChangeRequestOption...)} The ID is always + * {@link Dns#applyChangeRequest(String, ChangeRequestInfo, ChangeRequestOption...)} The ID is always * returned even if not selected. */ enum ChangeRequestField { @@ -508,7 +508,7 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) { * @throws DnsException upon failure if zone is not found * @see Cloud DNS Changes: create */ - ChangeRequest applyChangeRequest(String zoneName, ChangeRequest changeRequest, + ChangeRequest applyChangeRequest(String zoneName, ChangeRequestInfo changeRequest, ChangeRequestOption... options); /** diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java index 2fbf4e8b5a79..51ab0bd92720 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java @@ -19,7 +19,6 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.gcloud.RetryHelper.RetryHelperException; import static com.google.gcloud.RetryHelper.runWithRetries; -import static com.google.gcloud.dns.ChangeRequest.fromPb; import com.google.api.services.dns.model.Change; import com.google.api.services.dns.model.ManagedZone; @@ -169,7 +168,8 @@ public DnsRpc.ListResult call() { // transform that list into change request objects Iterable changes = result.results() == null ? ImmutableList.of() - : Iterables.transform(result.results(), ChangeRequest.FROM_PB_FUNCTION); + : Iterables.transform(result.results(), + ChangeRequest.fromPbFunction(serviceOptions.service(), zoneName)); return new PageImpl<>(new ChangeRequestPageFetcher(zoneName, serviceOptions, cursor, optionsMap), cursor, changes); } catch (RetryHelperException e) { @@ -272,8 +272,8 @@ public com.google.api.services.dns.model.Project call() { } @Override - public ChangeRequest applyChangeRequest(final String zoneName, final ChangeRequest changeRequest, - Dns.ChangeRequestOption... options) { + public ChangeRequest applyChangeRequest(final String zoneName, + final ChangeRequestInfo changeRequest, ChangeRequestOption... options) { final Map optionsMap = optionMap(options); try { com.google.api.services.dns.model.Change answer = @@ -284,7 +284,7 @@ public com.google.api.services.dns.model.Change call() { return dnsRpc.applyChangeRequest(zoneName, changeRequest.toPb(), optionsMap); } }, options().retryParams(), EXCEPTION_HANDLER); - return answer == null ? null : fromPb(answer); // should never be null + return answer == null ? null : ChangeRequest.fromPb(this, zoneName, answer); // not null } catch (RetryHelper.RetryHelperException ex) { throw DnsException.translateAndThrow(ex); } @@ -303,7 +303,7 @@ public com.google.api.services.dns.model.Change call() { return dnsRpc.getChangeRequest(zoneName, changeRequestId, optionsMap); } }, options().retryParams(), EXCEPTION_HANDLER); - return answer == null ? null : fromPb(answer); + return answer == null ? null : ChangeRequest.fromPb(this, zoneName, answer); } catch (RetryHelper.RetryHelperException ex) { throw DnsException.translateAndThrow(ex); } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Zone.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Zone.java index aed99dbd0001..9930bfdbad67 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Zone.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Zone.java @@ -146,14 +146,14 @@ public Page listRecordSets(Dns.RecordSetListOption... options) { } /** - * Submits {@link ChangeRequest} to the service for it to applied to this zone. The method + * Submits {@link ChangeRequestInfo} to the service for it to applied to this zone. The method * searches for zone by name. * * @param options optional restriction on what fields of {@link ChangeRequest} should be returned * @return ChangeRequest with server-assigned ID * @throws DnsException upon failure or if the zone is not found */ - public ChangeRequest applyChangeRequest(ChangeRequest changeRequest, + public ChangeRequest applyChangeRequest(ChangeRequestInfo changeRequest, Dns.ChangeRequestOption... options) { checkNotNull(changeRequest); return dns.applyChangeRequest(name(), changeRequest, options); diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/package-info.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/package-info.java index 69bee930df62..36f41852400c 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/package-info.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/package-info.java @@ -46,7 +46,7 @@ * .ttl(24, TimeUnit.HOURS) * .addRecord(ip) * .build(); - * ChangeRequest changeRequest = ChangeRequest.builder().add(toCreate).build(); + * ChangeRequestInfo changeRequest = ChangeRequestInfo.builder().add(toCreate).build(); * zone.applyChangeRequest(changeRequest); * } * diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestInfoTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestInfoTest.java new file mode 100644 index 000000000000..55f2af0824ec --- /dev/null +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestInfoTest.java @@ -0,0 +1,218 @@ +/* + * 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.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.google.common.collect.ImmutableList; + +import org.junit.Test; + +import java.util.List; + +public class ChangeRequestInfoTest { + + private static final String ID = "cr-id-1"; + private static final Long START_TIME_MILLIS = 12334567890L; + private static final ChangeRequest.Status STATUS = ChangeRequest.Status.PENDING; + private static final String NAME1 = "dns1"; + private static final RecordSet.Type TYPE1 = RecordSet.Type.A; + private static final String NAME2 = "dns2"; + private static final RecordSet.Type TYPE2 = RecordSet.Type.AAAA; + private static final String NAME3 = "dns3"; + private static final RecordSet.Type TYPE3 = RecordSet.Type.MX; + private static final RecordSet RECORD1 = RecordSet.builder(NAME1, TYPE1).build(); + private static final RecordSet RECORD2 = RecordSet.builder(NAME2, TYPE2).build(); + private static final RecordSet RECORD3 = RecordSet.builder(NAME3, TYPE3).build(); + private static final List ADDITIONS = ImmutableList.of(RECORD1, RECORD2); + private static final List DELETIONS = ImmutableList.of(RECORD3); + private static final ChangeRequestInfo CHANGE = ChangeRequest.builder() + .add(RECORD1) + .add(RECORD2) + .delete(RECORD3) + .startTimeMillis(START_TIME_MILLIS) + .status(STATUS) + .id(ID) + .build(); + + @Test + public void testEmptyBuilder() { + ChangeRequestInfo cr = ChangeRequest.builder().build(); + assertNotNull(cr.deletions()); + assertTrue(cr.deletions().isEmpty()); + assertNotNull(cr.additions()); + assertTrue(cr.additions().isEmpty()); + } + + @Test + public void testBuilder() { + assertEquals(ID, CHANGE.id()); + assertEquals(STATUS, CHANGE.status()); + assertEquals(START_TIME_MILLIS, CHANGE.startTimeMillis()); + assertEquals(ADDITIONS, CHANGE.additions()); + assertEquals(DELETIONS, CHANGE.deletions()); + List recordList = ImmutableList.of(RECORD1); + ChangeRequestInfo another = CHANGE.toBuilder().additions(recordList).build(); + assertEquals(recordList, another.additions()); + assertEquals(CHANGE.deletions(), another.deletions()); + another = CHANGE.toBuilder().deletions(recordList).build(); + assertEquals(recordList, another.deletions()); + assertEquals(CHANGE.additions(), another.additions()); + } + + @Test + public void testEqualsAndNotEquals() { + ChangeRequestInfo clone = CHANGE.toBuilder().build(); + assertEquals(CHANGE, clone); + clone = ChangeRequest.fromPb(CHANGE.toPb()); + assertEquals(CHANGE, clone); + clone = CHANGE.toBuilder().id("some-other-id").build(); + assertNotEquals(CHANGE, clone); + clone = CHANGE.toBuilder().startTimeMillis(CHANGE.startTimeMillis() + 1).build(); + assertNotEquals(CHANGE, clone); + clone = CHANGE.toBuilder().add(RECORD3).build(); + assertNotEquals(CHANGE, clone); + clone = CHANGE.toBuilder().delete(RECORD1).build(); + assertNotEquals(CHANGE, clone); + ChangeRequestInfo empty = ChangeRequest.builder().build(); + assertNotEquals(CHANGE, empty); + assertEquals(empty, ChangeRequest.builder().build()); + } + + @Test + public void testSameHashCodeOnEquals() { + ChangeRequestInfo clone = CHANGE.toBuilder().build(); + assertEquals(CHANGE, clone); + assertEquals(CHANGE.hashCode(), clone.hashCode()); + ChangeRequestInfo empty = ChangeRequest.builder().build(); + assertEquals(empty.hashCode(), ChangeRequest.builder().build().hashCode()); + } + + @Test + public void testToAndFromPb() { + assertEquals(CHANGE, ChangeRequest.fromPb(CHANGE.toPb())); + ChangeRequestInfo partial = ChangeRequest.builder().build(); + assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); + partial = ChangeRequest.builder().id(ID).build(); + assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); + partial = ChangeRequest.builder().add(RECORD1).build(); + assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); + partial = ChangeRequest.builder().delete(RECORD1).build(); + assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); + partial = ChangeRequest.builder().additions(ADDITIONS).build(); + assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); + partial = ChangeRequest.builder().deletions(DELETIONS).build(); + assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); + partial = ChangeRequest.builder().startTimeMillis(START_TIME_MILLIS).build(); + assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); + partial = ChangeRequest.builder().status(STATUS).build(); + assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); + } + + @Test + public void testToBuilder() { + assertEquals(CHANGE, CHANGE.toBuilder().build()); + ChangeRequestInfo partial = ChangeRequest.builder().build(); + assertEquals(partial, partial.toBuilder().build()); + partial = ChangeRequest.builder().id(ID).build(); + assertEquals(partial, partial.toBuilder().build()); + partial = ChangeRequest.builder().add(RECORD1).build(); + assertEquals(partial, partial.toBuilder().build()); + partial = ChangeRequest.builder().delete(RECORD1).build(); + assertEquals(partial, partial.toBuilder().build()); + partial = ChangeRequest.builder().additions(ADDITIONS).build(); + assertEquals(partial, partial.toBuilder().build()); + partial = ChangeRequest.builder().deletions(DELETIONS).build(); + assertEquals(partial, partial.toBuilder().build()); + partial = ChangeRequest.builder().startTimeMillis(START_TIME_MILLIS).build(); + assertEquals(partial, partial.toBuilder().build()); + partial = ChangeRequest.builder().status(STATUS).build(); + assertEquals(partial, partial.toBuilder().build()); + } + + @Test + public void testClearAdditions() { + ChangeRequestInfo clone = CHANGE.toBuilder().clearAdditions().build(); + assertTrue(clone.additions().isEmpty()); + assertFalse(clone.deletions().isEmpty()); + } + + @Test + public void testAddAddition() { + try { + CHANGE.toBuilder().add(null); + fail("Should not be able to add null RecordSet."); + } catch (NullPointerException e) { + // expected + } + ChangeRequestInfo clone = CHANGE.toBuilder().add(RECORD1).build(); + assertEquals(CHANGE.additions().size() + 1, clone.additions().size()); + } + + @Test + public void testAddDeletion() { + try { + CHANGE.toBuilder().delete(null); + fail("Should not be able to delete null RecordSet."); + } catch (NullPointerException e) { + // expected + } + ChangeRequestInfo clone = CHANGE.toBuilder().delete(RECORD1).build(); + assertEquals(CHANGE.deletions().size() + 1, clone.deletions().size()); + } + + @Test + public void testClearDeletions() { + ChangeRequestInfo clone = CHANGE.toBuilder().clearDeletions().build(); + assertTrue(clone.deletions().isEmpty()); + assertFalse(clone.additions().isEmpty()); + } + + @Test + public void testRemoveAddition() { + ChangeRequestInfo clone = CHANGE.toBuilder().removeAddition(RECORD1).build(); + assertTrue(clone.additions().contains(RECORD2)); + assertFalse(clone.additions().contains(RECORD1)); + assertTrue(clone.deletions().contains(RECORD3)); + clone = CHANGE.toBuilder().removeAddition(RECORD2).removeAddition(RECORD1).build(); + assertFalse(clone.additions().contains(RECORD2)); + assertFalse(clone.additions().contains(RECORD1)); + assertTrue(clone.additions().isEmpty()); + assertTrue(clone.deletions().contains(RECORD3)); + } + + @Test + public void testRemoveDeletion() { + ChangeRequestInfo clone = CHANGE.toBuilder().removeDeletion(RECORD3).build(); + assertTrue(clone.deletions().isEmpty()); + } + + @Test + public void testDateParsing() { + String startTime = "2016-01-26T18:33:43.512Z"; // obtained from service + com.google.api.services.dns.model.Change change = CHANGE.toPb().setStartTime(startTime); + ChangeRequestInfo converted = ChangeRequest.fromPb(change); + assertNotNull(converted.startTimeMillis()); + assertEquals(change, converted.toPb()); + assertEquals(change.getStartTime(), converted.toPb().getStartTime()); + } +} diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java index fe726acb7c10..bfd1d0f512f4 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java @@ -16,203 +16,125 @@ package com.google.gcloud.dns; +import static org.easymock.EasyMock.createStrictMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.reset; +import static org.easymock.EasyMock.verify; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; 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.common.collect.ImmutableList; +import org.junit.After; +import org.junit.Before; import org.junit.Test; -import java.util.List; - public class ChangeRequestTest { - private static final String ID = "cr-id-1"; - private static final Long START_TIME_MILLIS = 12334567890L; - private static final ChangeRequest.Status STATUS = ChangeRequest.Status.PENDING; - private static final String NAME1 = "dns1"; - private static final RecordSet.Type TYPE1 = RecordSet.Type.A; - private static final String NAME2 = "dns2"; - private static final RecordSet.Type TYPE2 = RecordSet.Type.AAAA; - private static final String NAME3 = "dns3"; - private static final RecordSet.Type TYPE3 = RecordSet.Type.MX; - private static final RecordSet RECORD1 = RecordSet.builder(NAME1, TYPE1).build(); - private static final RecordSet RECORD2 = RecordSet.builder(NAME2, TYPE2).build(); - private static final RecordSet RECORD3 = RecordSet.builder(NAME3, TYPE3).build(); - private static final List ADDITIONS = ImmutableList.of(RECORD1, RECORD2); - private static final List DELETIONS = ImmutableList.of(RECORD3); - private static final ChangeRequest CHANGE = ChangeRequest.builder() - .add(RECORD1) - .add(RECORD2) - .delete(RECORD3) - .startTimeMillis(START_TIME_MILLIS) - .status(STATUS) - .id(ID) + private static final String ZONE_NAME = "dns-zone-name"; + private static final ChangeRequestInfo CHANGE_REQUEST_INFO = ChangeRequest.builder() + .add(RecordSet.builder("name", RecordSet.Type.A).build()) + .delete(RecordSet.builder("othername", RecordSet.Type.AAAA).build()) .build(); - - @Test - public void testEmptyBuilder() { - ChangeRequest cr = ChangeRequest.builder().build(); - assertNotNull(cr.deletions()); - assertTrue(cr.deletions().isEmpty()); - assertNotNull(cr.additions()); - assertTrue(cr.additions().isEmpty()); - } - - @Test - public void testBuilder() { - assertEquals(ID, CHANGE.id()); - assertEquals(STATUS, CHANGE.status()); - assertEquals(START_TIME_MILLIS, CHANGE.startTimeMillis()); - assertEquals(ADDITIONS, CHANGE.additions()); - assertEquals(DELETIONS, CHANGE.deletions()); - List recordList = ImmutableList.of(RECORD1); - ChangeRequest another = CHANGE.toBuilder().additions(recordList).build(); - assertEquals(recordList, another.additions()); - assertEquals(CHANGE.deletions(), another.deletions()); - another = CHANGE.toBuilder().deletions(recordList).build(); - assertEquals(recordList, another.deletions()); - assertEquals(CHANGE.additions(), another.additions()); - } - - @Test - public void testEqualsAndNotEquals() { - ChangeRequest clone = CHANGE.toBuilder().build(); - assertEquals(CHANGE, clone); - clone = ChangeRequest.fromPb(CHANGE.toPb()); - assertEquals(CHANGE, clone); - clone = CHANGE.toBuilder().id("some-other-id").build(); - assertNotEquals(CHANGE, clone); - clone = CHANGE.toBuilder().startTimeMillis(CHANGE.startTimeMillis() + 1).build(); - assertNotEquals(CHANGE, clone); - clone = CHANGE.toBuilder().add(RECORD3).build(); - assertNotEquals(CHANGE, clone); - clone = CHANGE.toBuilder().delete(RECORD1).build(); - assertNotEquals(CHANGE, clone); - ChangeRequest empty = ChangeRequest.builder().build(); - assertNotEquals(CHANGE, empty); - assertEquals(empty, ChangeRequest.builder().build()); - } - - @Test - public void testSameHashCodeOnEquals() { - ChangeRequest clone = CHANGE.toBuilder().build(); - assertEquals(CHANGE, clone); - assertEquals(CHANGE.hashCode(), clone.hashCode()); - ChangeRequest empty = ChangeRequest.builder().build(); - assertEquals(empty.hashCode(), ChangeRequest.builder().build().hashCode()); - } - - @Test - public void testToAndFromPb() { - assertEquals(CHANGE, ChangeRequest.fromPb(CHANGE.toPb())); - ChangeRequest partial = ChangeRequest.builder().build(); - assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); - partial = ChangeRequest.builder().id(ID).build(); - assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); - partial = ChangeRequest.builder().add(RECORD1).build(); - assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); - partial = ChangeRequest.builder().delete(RECORD1).build(); - assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); - partial = ChangeRequest.builder().additions(ADDITIONS).build(); - assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); - partial = ChangeRequest.builder().deletions(DELETIONS).build(); - assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); - partial = ChangeRequest.builder().startTimeMillis(START_TIME_MILLIS).build(); - assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); - partial = ChangeRequest.builder().status(STATUS).build(); - assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); - } - - @Test - public void testToBuilder() { - assertEquals(CHANGE, CHANGE.toBuilder().build()); - ChangeRequest partial = ChangeRequest.builder().build(); - assertEquals(partial, partial.toBuilder().build()); - partial = ChangeRequest.builder().id(ID).build(); - assertEquals(partial, partial.toBuilder().build()); - partial = ChangeRequest.builder().add(RECORD1).build(); - assertEquals(partial, partial.toBuilder().build()); - partial = ChangeRequest.builder().delete(RECORD1).build(); - assertEquals(partial, partial.toBuilder().build()); - partial = ChangeRequest.builder().additions(ADDITIONS).build(); - assertEquals(partial, partial.toBuilder().build()); - partial = ChangeRequest.builder().deletions(DELETIONS).build(); - assertEquals(partial, partial.toBuilder().build()); - partial = ChangeRequest.builder().startTimeMillis(START_TIME_MILLIS).build(); - assertEquals(partial, partial.toBuilder().build()); - partial = ChangeRequest.builder().status(STATUS).build(); - assertEquals(partial, partial.toBuilder().build()); - } - - @Test - public void testClearAdditions() { - ChangeRequest clone = CHANGE.toBuilder().clearAdditions().build(); - assertTrue(clone.additions().isEmpty()); - assertFalse(clone.deletions().isEmpty()); + private static final DnsOptions OPTIONS = createStrictMock(DnsOptions.class); + + private Dns dns; + private ChangeRequest changeRequest; + private ChangeRequest changeRequestPartial; + + @Before + public void setUp() throws Exception { + dns = createStrictMock(Dns.class); + expect(dns.options()).andReturn(OPTIONS).times(2); + replay(dns); + changeRequest = new ChangeRequest(dns, ZONE_NAME, new ChangeRequestInfo.BuilderImpl( + CHANGE_REQUEST_INFO.toBuilder() + .startTimeMillis(132L) + .id("12") + .status(ChangeRequest.Status.DONE) + .build())); + changeRequestPartial = new ChangeRequest(dns, ZONE_NAME, + new ChangeRequest.BuilderImpl(CHANGE_REQUEST_INFO)); + reset(dns); } - @Test - public void testAddAddition() { - try { - CHANGE.toBuilder().add(null); - fail("Should not be able to add null RecordSet."); - } catch (NullPointerException e) { - // expected - } - ChangeRequest clone = CHANGE.toBuilder().add(RECORD1).build(); - assertEquals(CHANGE.additions().size() + 1, clone.additions().size()); + @After + public void tearDown() throws Exception { + verify(dns); } @Test - public void testAddDeletion() { - try { - CHANGE.toBuilder().delete(null); - fail("Should not be able to delete null RecordSet."); - } catch (NullPointerException e) { - // expected - } - ChangeRequest clone = CHANGE.toBuilder().delete(RECORD1).build(); - assertEquals(CHANGE.deletions().size() + 1, clone.deletions().size()); + public void testConstructor() { + expect(dns.options()).andReturn(OPTIONS); + replay(dns); + assertEquals(new ChangeRequest(dns, ZONE_NAME, + new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_INFO)), changeRequestPartial); + assertNotNull(changeRequest.dns()); + assertEquals(ZONE_NAME, changeRequest.zone()); + assertSame(dns, changeRequestPartial.dns()); + assertEquals(ZONE_NAME, changeRequestPartial.zone()); } @Test - public void testClearDeletions() { - ChangeRequest clone = CHANGE.toBuilder().clearDeletions().build(); - assertTrue(clone.deletions().isEmpty()); - assertFalse(clone.additions().isEmpty()); + public void testFromPb() { + expect(dns.options()).andReturn(OPTIONS).times(2); + replay(dns); + assertEquals(changeRequest, ChangeRequest.fromPb(dns, ZONE_NAME, changeRequest.toPb())); + assertEquals(changeRequestPartial, + ChangeRequest.fromPb(dns, ZONE_NAME, changeRequestPartial.toPb())); } @Test - public void testRemoveAddition() { - ChangeRequest clone = CHANGE.toBuilder().removeAddition(RECORD1).build(); - assertTrue(clone.additions().contains(RECORD2)); - assertFalse(clone.additions().contains(RECORD1)); - assertTrue(clone.deletions().contains(RECORD3)); - clone = CHANGE.toBuilder().removeAddition(RECORD2).removeAddition(RECORD1).build(); - assertFalse(clone.additions().contains(RECORD2)); - assertFalse(clone.additions().contains(RECORD1)); - assertTrue(clone.additions().isEmpty()); - assertTrue(clone.deletions().contains(RECORD3)); + public void testEqualsAndToBuilder() { + expect(dns.options()).andReturn(OPTIONS).times(2); + replay(dns); + ChangeRequest compare = changeRequest.toBuilder().build(); + assertEquals(changeRequest, compare); + assertEquals(changeRequest.hashCode(), compare.hashCode()); + compare = changeRequestPartial.toBuilder().build(); + assertEquals(changeRequestPartial, compare); + assertEquals(changeRequestPartial.hashCode(), compare.hashCode()); } @Test - public void testRemoveDeletion() { - ChangeRequest clone = CHANGE.toBuilder().removeDeletion(RECORD3).build(); - assertTrue(clone.deletions().isEmpty()); + public void testBuilder() { + // one for each build() call because it invokes a constructor + expect(dns.options()).andReturn(OPTIONS).times(9); + replay(dns); + String id = changeRequest.id() + "aaa"; + assertEquals(id, changeRequest.toBuilder().id(id).build().id()); + ChangeRequest modified = + changeRequest.toBuilder().status(ChangeRequest.Status.PENDING).build(); + assertEquals(ChangeRequest.Status.PENDING, modified.status()); + modified = changeRequest.toBuilder().clearDeletions().build(); + assertTrue(modified.deletions().isEmpty()); + modified = changeRequest.toBuilder().clearAdditions().build(); + assertTrue(modified.additions().isEmpty()); + modified = changeRequest.toBuilder().additions(ImmutableList.of()).build(); + assertTrue(modified.additions().isEmpty()); + modified = changeRequest.toBuilder().deletions(ImmutableList.of()).build(); + assertTrue(modified.deletions().isEmpty()); + RecordSet cname = RecordSet.builder("last", RecordSet.Type.CNAME).build(); + modified = changeRequest.toBuilder().add(cname).build(); + assertTrue(modified.additions().contains(cname)); + modified = changeRequest.toBuilder().delete(cname).build(); + assertTrue(modified.deletions().contains(cname)); + modified = changeRequest.toBuilder().startTimeMillis(0L).build(); + assertEquals(Long.valueOf(0), modified.startTimeMillis()); } @Test - public void testDateParsing() { - String startTime = "2016-01-26T18:33:43.512Z"; // obtained from service - com.google.api.services.dns.model.Change change = CHANGE.toPb().setStartTime(startTime); - ChangeRequest converted = ChangeRequest.fromPb(change); - assertNotNull(converted.startTimeMillis()); - assertEquals(change, converted.toPb()); - assertEquals(change.getStartTime(), converted.toPb().getStartTime()); + public void testApplyTo() { + expect(dns.applyChangeRequest(ZONE_NAME, changeRequest)).andReturn(changeRequest); + expect(dns.applyChangeRequest(ZONE_NAME, changeRequest, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME))) + .andReturn(changeRequest); + replay(dns); + assertSame(changeRequest, changeRequest.applyTo()); + assertSame(changeRequest, + changeRequest.applyTo(Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME))); } } diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java index ab2dba0a566c..94ed4a3da3f7 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java @@ -53,10 +53,10 @@ public class DnsImplTest { private static final String PAGE_TOKEN = "some token"; private static final ZoneInfo ZONE_INFO = ZoneInfo.of(ZONE_NAME, DNS_NAME, DESCRIPTION); private static final ProjectInfo PROJECT_INFO = ProjectInfo.builder().build(); - private static final ChangeRequest CHANGE_REQUEST_PARTIAL = ChangeRequest.builder() + private static final ChangeRequestInfo CHANGE_REQUEST_PARTIAL = ChangeRequestInfo.builder() .add(DNS_RECORD1) .build(); - private static final ChangeRequest CHANGE_REQUEST_COMPLETE = ChangeRequest.builder() + private static final ChangeRequestInfo CHANGE_REQUEST_COMPLETE = ChangeRequestInfo.builder() .add(DNS_RECORD1) .startTimeMillis(123L) .status(ChangeRequest.Status.PENDING) @@ -221,7 +221,8 @@ public void testGetChangeRequest() { dns = options.service(); // creates DnsImpl ChangeRequest changeRequest = dns.getChangeRequest(ZONE_INFO.name(), CHANGE_REQUEST_COMPLETE.id()); - assertEquals(CHANGE_REQUEST_COMPLETE, changeRequest); + assertEquals(new ChangeRequest(dns, ZONE_INFO.name(), + new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_COMPLETE)), changeRequest); } @Test @@ -235,7 +236,8 @@ public void testGetChangeRequestWithOptions() { ChangeRequest changeRequest = dns.getChangeRequest(ZONE_INFO.name(), CHANGE_REQUEST_COMPLETE.id(), CHANGE_GET_FIELDS); String selector = (String) capturedOptions.getValue().get(CHANGE_GET_FIELDS.rpcOption()); - assertEquals(CHANGE_REQUEST_COMPLETE, changeRequest); + assertEquals(new ChangeRequest(dns, ZONE_INFO.name(), + new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_COMPLETE)), changeRequest); assertTrue(selector.contains(Dns.ChangeRequestField.STATUS.selector())); assertTrue(selector.contains(Dns.ChangeRequestField.ID.selector())); } @@ -248,7 +250,8 @@ public void testApplyChangeRequest() { dns = options.service(); // creates DnsImpl ChangeRequest changeRequest = dns.applyChangeRequest(ZONE_INFO.name(), CHANGE_REQUEST_PARTIAL); - assertEquals(CHANGE_REQUEST_COMPLETE, changeRequest); + assertEquals(new ChangeRequest(dns, ZONE_INFO.name(), + new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_COMPLETE)), changeRequest); } @Test @@ -262,7 +265,8 @@ public void testApplyChangeRequestWithOptions() { ChangeRequest changeRequest = dns.applyChangeRequest(ZONE_INFO.name(), CHANGE_REQUEST_PARTIAL, CHANGE_GET_FIELDS); String selector = (String) capturedOptions.getValue().get(CHANGE_GET_FIELDS.rpcOption()); - assertEquals(CHANGE_REQUEST_COMPLETE, changeRequest); + assertEquals(new ChangeRequest(dns, ZONE_INFO.name(), + new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_COMPLETE)), changeRequest); assertTrue(selector.contains(Dns.ChangeRequestField.STATUS.selector())); assertTrue(selector.contains(Dns.ChangeRequestField.ID.selector())); } @@ -275,8 +279,12 @@ public void testListChangeRequests() { EasyMock.replay(dnsRpcMock); dns = options.service(); // creates DnsImpl Page changeRequestPage = dns.listChangeRequests(ZONE_INFO.name()); - assertTrue(Lists.newArrayList(changeRequestPage.values()).contains(CHANGE_REQUEST_COMPLETE)); - assertTrue(Lists.newArrayList(changeRequestPage.values()).contains(CHANGE_REQUEST_PARTIAL)); + assertTrue(Lists.newArrayList(changeRequestPage.values()).contains( + new ChangeRequest(dns, ZONE_INFO.name(), + new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_COMPLETE)))); + assertTrue(Lists.newArrayList(changeRequestPage.values()).contains( + new ChangeRequest(dns, ZONE_INFO.name(), + new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_PARTIAL)))); assertEquals(2, Lists.newArrayList(changeRequestPage.values()).size()); } @@ -288,8 +296,12 @@ public void testListChangeRequestsWithOptions() { EasyMock.replay(dnsRpcMock); dns = options.service(); // creates DnsImpl Page changeRequestPage = dns.listChangeRequests(ZONE_NAME, CHANGE_LIST_OPTIONS); - assertTrue(Lists.newArrayList(changeRequestPage.values()).contains(CHANGE_REQUEST_COMPLETE)); - assertTrue(Lists.newArrayList(changeRequestPage.values()).contains(CHANGE_REQUEST_PARTIAL)); + assertTrue(Lists.newArrayList(changeRequestPage.values()).contains( + new ChangeRequest(dns, ZONE_INFO.name(), + new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_COMPLETE)))); + assertTrue(Lists.newArrayList(changeRequestPage.values()).contains( + new ChangeRequest(dns, ZONE_INFO.name(), + new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_PARTIAL)))); assertEquals(2, Lists.newArrayList(changeRequestPage.values()).size()); Integer size = (Integer) capturedOptions.getValue().get(CHANGE_LIST_OPTIONS[0].rpcOption()); assertEquals(MAX_SIZE, size); diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/SerializationTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/SerializationTest.java index c06cd096bf1e..ad25b31068dd 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/SerializationTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/SerializationTest.java @@ -63,7 +63,10 @@ public class SerializationTest extends BaseSerializationTest { private static final Zone FULL_ZONE = new Zone(DNS, new ZoneInfo.BuilderImpl(FULL_ZONE_INFO)); private static final Zone PARTIAL_ZONE = new Zone(DNS, new ZoneInfo.BuilderImpl(PARTIAL_ZONE_INFO)); - private static final ChangeRequest CHANGE_REQUEST_PARTIAL = ChangeRequest.builder().build(); + private static final ChangeRequestInfo CHANGE_REQUEST_INFO_PARTIAL = + ChangeRequest.builder().build(); + private static final ChangeRequest CHANGE_REQUEST_PARTIAL = new ChangeRequest(DNS, "name", + new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_INFO_PARTIAL)); private static final RecordSet RECORD_SET_PARTIAL = RecordSet.builder("www.www.com", RecordSet.Type.AAAA).build(); private static final RecordSet RECORD_SET_COMPLETE = @@ -71,13 +74,15 @@ public class SerializationTest extends BaseSerializationTest { .ttl(12, TimeUnit.HOURS) .addRecord("record") .build(); - private static final ChangeRequest CHANGE_REQUEST_COMPLETE = ChangeRequest.builder() + private static final ChangeRequestInfo CHANGE_REQUEST_INFO_COMPLETE = ChangeRequestInfo.builder() .add(RECORD_SET_COMPLETE) .delete(RECORD_SET_PARTIAL) .status(ChangeRequest.Status.PENDING) .id("some id") .startTimeMillis(132L) .build(); + private static final ChangeRequest CHANGE_REQUEST_COMPLETE = new ChangeRequest(DNS, "name", + new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_INFO_COMPLETE)); @Override protected Serializable[] serializableObjects() { @@ -91,8 +96,9 @@ protected Serializable[] serializableObjects() { return new Serializable[]{FULL_ZONE_INFO, PARTIAL_ZONE_INFO, ZONE_LIST_OPTION, RECORD_SET_LIST_OPTION, CHANGE_REQUEST_LIST_OPTION, ZONE_OPTION, CHANGE_REQUEST_OPTION, PROJECT_OPTION, PARTIAL_PROJECT_INFO, FULL_PROJECT_INFO, OPTIONS, FULL_ZONE, PARTIAL_ZONE, - OPTIONS, CHANGE_REQUEST_PARTIAL, RECORD_SET_PARTIAL, RECORD_SET_COMPLETE, - CHANGE_REQUEST_COMPLETE, options, otherOptions}; + OPTIONS, CHANGE_REQUEST_INFO_PARTIAL, CHANGE_REQUEST_PARTIAL, RECORD_SET_PARTIAL, + RECORD_SET_COMPLETE, CHANGE_REQUEST_INFO_COMPLETE, CHANGE_REQUEST_COMPLETE, options, + otherOptions}; } @Override diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ZoneTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ZoneTest.java index bd59f8c140e9..ba4493abfca8 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ZoneTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ZoneTest.java @@ -60,25 +60,27 @@ public class ZoneTest { Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME); private static final Dns.ChangeRequestListOption CHANGE_REQUEST_LIST_OPTIONS = Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.START_TIME); - private static final ChangeRequest CHANGE_REQUEST = ChangeRequest.builder().id("someid").build(); - private static final ChangeRequest CHANGE_REQUEST_AFTER = CHANGE_REQUEST.toBuilder() - .startTimeMillis(123465L).build(); - private static final ChangeRequest CHANGE_REQUEST_NO_ID = ChangeRequest.builder().build(); + private static final ChangeRequestInfo CHANGE_REQUEST = + ChangeRequestInfo.builder().id("someid").build(); + private static final ChangeRequestInfo CHANGE_REQUEST_NO_ID = + ChangeRequestInfo.builder().build(); private static final DnsException EXCEPTION = createStrictMock(DnsException.class); private static final DnsOptions OPTIONS = createStrictMock(DnsOptions.class); private Dns dns; private Zone zone; private Zone zoneNoId; + private ChangeRequest changeRequestAfter; @Before public void setUp() throws Exception { dns = createStrictMock(Dns.class); - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS).times(3); replay(dns); zone = new Zone(dns, new ZoneInfo.BuilderImpl(ZONE_INFO)); zoneNoId = new Zone(dns, new ZoneInfo.BuilderImpl(NO_ID_INFO)); + changeRequestAfter = new ChangeRequest(dns, ZONE_NAME, new ChangeRequestInfo.BuilderImpl( + CHANGE_REQUEST.toBuilder().startTimeMillis(123465L).build())); reset(dns); } @@ -97,8 +99,7 @@ public void testConstructor() { @Test public void deleteByNameAndFound() { - expect(dns.delete(ZONE_NAME)).andReturn(true); - expect(dns.delete(ZONE_NAME)).andReturn(true); + expect(dns.delete(ZONE_NAME)).andReturn(true).times(2); replay(dns); boolean result = zone.delete(); assertTrue(result); @@ -108,8 +109,7 @@ public void deleteByNameAndFound() { @Test public void deleteByNameAndNotFound() { - expect(dns.delete(ZONE_NAME)).andReturn(false); - expect(dns.delete(ZONE_NAME)).andReturn(false); + expect(dns.delete(ZONE_NAME)).andReturn(false).times(2); replay(dns); boolean result = zoneNoId.delete(); assertFalse(result); @@ -122,11 +122,9 @@ public void listDnsRecordsByNameAndFound() { @SuppressWarnings("unchecked") Page pageMock = createStrictMock(Page.class); replay(pageMock); - expect(dns.listRecordSets(ZONE_NAME)).andReturn(pageMock); - expect(dns.listRecordSets(ZONE_NAME)).andReturn(pageMock); + expect(dns.listRecordSets(ZONE_NAME)).andReturn(pageMock).times(2); // again for options - expect(dns.listRecordSets(ZONE_NAME, DNS_RECORD_OPTIONS)).andReturn(pageMock); - expect(dns.listRecordSets(ZONE_NAME, DNS_RECORD_OPTIONS)).andReturn(pageMock); + expect(dns.listRecordSets(ZONE_NAME, DNS_RECORD_OPTIONS)).andReturn(pageMock).times(2); replay(dns); Page result = zone.listRecordSets(); assertSame(pageMock, result); @@ -139,11 +137,9 @@ public void listDnsRecordsByNameAndFound() { @Test public void listDnsRecordsByNameAndNotFound() { - expect(dns.listRecordSets(ZONE_NAME)).andThrow(EXCEPTION); - expect(dns.listRecordSets(ZONE_NAME)).andThrow(EXCEPTION); + expect(dns.listRecordSets(ZONE_NAME)).andThrow(EXCEPTION).times(2); // again for options - expect(dns.listRecordSets(ZONE_NAME, DNS_RECORD_OPTIONS)).andThrow(EXCEPTION); - expect(dns.listRecordSets(ZONE_NAME, DNS_RECORD_OPTIONS)).andThrow(EXCEPTION); + expect(dns.listRecordSets(ZONE_NAME, DNS_RECORD_OPTIONS)).andThrow(EXCEPTION).times(2); replay(dns); try { zoneNoId.listRecordSets(); @@ -173,8 +169,7 @@ public void listDnsRecordsByNameAndNotFound() { @Test public void reloadByNameAndFound() { - expect(dns.getZone(ZONE_NAME)).andReturn(zone); - expect(dns.getZone(ZONE_NAME)).andReturn(zone); + expect(dns.getZone(ZONE_NAME)).andReturn(zone).times(2); // again for options expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(zoneNoId); expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(zone); @@ -191,11 +186,9 @@ public void reloadByNameAndFound() { @Test public void reloadByNameAndNotFound() { - expect(dns.getZone(ZONE_NAME)).andReturn(null); - expect(dns.getZone(ZONE_NAME)).andReturn(null); + expect(dns.getZone(ZONE_NAME)).andReturn(null).times(2); // again for options - expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(null); - expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(null); + expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(null).times(2); replay(dns); Zone result = zoneNoId.reload(); assertNull(result); @@ -208,36 +201,33 @@ public void reloadByNameAndNotFound() { @Test public void applyChangeByNameAndFound() { expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST)) - .andReturn(CHANGE_REQUEST_AFTER); + .andReturn(changeRequestAfter); expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST)) - .andReturn(CHANGE_REQUEST_AFTER); + .andReturn(changeRequestAfter); // again for options expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(CHANGE_REQUEST_AFTER); + .andReturn(changeRequestAfter); expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(CHANGE_REQUEST_AFTER); + .andReturn(changeRequestAfter); replay(dns); ChangeRequest result = zoneNoId.applyChangeRequest(CHANGE_REQUEST); - assertEquals(CHANGE_REQUEST_AFTER, result); + assertEquals(changeRequestAfter, result); result = zone.applyChangeRequest(CHANGE_REQUEST); - assertEquals(CHANGE_REQUEST_AFTER, result); + assertEquals(changeRequestAfter, result); // check options result = zoneNoId.applyChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS); - assertEquals(CHANGE_REQUEST_AFTER, result); + assertEquals(changeRequestAfter, result); result = zone.applyChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS); - assertEquals(CHANGE_REQUEST_AFTER, result); + assertEquals(changeRequestAfter, result); } @Test public void applyChangeByNameAndNotFound() { // ID is not set - expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST)).andThrow(EXCEPTION); - expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST)).andThrow(EXCEPTION); + expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST)).andThrow(EXCEPTION).times(2); // again for options expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS)) - .andThrow(EXCEPTION); - expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS)) - .andThrow(EXCEPTION); + .andThrow(EXCEPTION).times(2); replay(dns); try { zoneNoId.applyChangeRequest(CHANGE_REQUEST); @@ -298,35 +288,28 @@ public void applyNullChangeRequest() { @Test public void getChangeAndZoneFoundByName() { expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())) - .andReturn(CHANGE_REQUEST_AFTER); - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())) - .andReturn(CHANGE_REQUEST_AFTER); + .andReturn(changeRequestAfter).times(2); // again for options expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(CHANGE_REQUEST_AFTER); - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(CHANGE_REQUEST_AFTER); + .andReturn(changeRequestAfter).times(2); replay(dns); ChangeRequest result = zoneNoId.getChangeRequest(CHANGE_REQUEST.id()); - assertEquals(CHANGE_REQUEST_AFTER, result); + assertEquals(changeRequestAfter, result); result = zone.getChangeRequest(CHANGE_REQUEST.id()); - assertEquals(CHANGE_REQUEST_AFTER, result); + assertEquals(changeRequestAfter, result); // check options result = zoneNoId.getChangeRequest(CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS); - assertEquals(CHANGE_REQUEST_AFTER, result); + assertEquals(changeRequestAfter, result); result = zone.getChangeRequest(CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS); - assertEquals(CHANGE_REQUEST_AFTER, result); + assertEquals(changeRequestAfter, result); } @Test public void getChangeAndZoneNotFoundByName() { - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())).andThrow(EXCEPTION); - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())).andThrow(EXCEPTION); + expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())).andThrow(EXCEPTION).times(2); // again for options expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)) - .andThrow(EXCEPTION); - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)) - .andThrow(EXCEPTION); + .andThrow(EXCEPTION).times(2); replay(dns); try { zoneNoId.getChangeRequest(CHANGE_REQUEST.id()); @@ -357,13 +340,10 @@ public void getChangeAndZoneNotFoundByName() { @Test public void getChangedWhichDoesNotExistZoneFound() { - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())).andReturn(null); - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())).andReturn(null); + expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())).andReturn(null).times(2); // again for options expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(null); - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(null); + .andReturn(null).times(2); replay(dns); assertNull(zoneNoId.getChangeRequest(CHANGE_REQUEST.id())); assertNull(zone.getChangeRequest(CHANGE_REQUEST.id())); @@ -434,13 +414,10 @@ public void listChangeRequestsAndZoneFound() { @SuppressWarnings("unchecked") Page pageMock = createStrictMock(Page.class); replay(pageMock); - expect(dns.listChangeRequests(ZONE_NAME)).andReturn(pageMock); - expect(dns.listChangeRequests(ZONE_NAME)).andReturn(pageMock); + expect(dns.listChangeRequests(ZONE_NAME)).andReturn(pageMock).times(2); // again for options expect(dns.listChangeRequests(ZONE_NAME, CHANGE_REQUEST_LIST_OPTIONS)) - .andReturn(pageMock); - expect(dns.listChangeRequests(ZONE_NAME, CHANGE_REQUEST_LIST_OPTIONS)) - .andReturn(pageMock); + .andReturn(pageMock).times(2); replay(dns); Page result = zoneNoId.listChangeRequests(); assertSame(pageMock, result); @@ -453,11 +430,10 @@ public void listChangeRequestsAndZoneFound() { @Test public void listChangeRequestsAndZoneNotFound() { - expect(dns.listChangeRequests(ZONE_NAME)).andThrow(EXCEPTION); - expect(dns.listChangeRequests(ZONE_NAME)).andThrow(EXCEPTION); + expect(dns.listChangeRequests(ZONE_NAME)).andThrow(EXCEPTION).times(2); // again for options - expect(dns.listChangeRequests(ZONE_NAME, CHANGE_REQUEST_LIST_OPTIONS)).andThrow(EXCEPTION); - expect(dns.listChangeRequests(ZONE_NAME, CHANGE_REQUEST_LIST_OPTIONS)).andThrow(EXCEPTION); + expect(dns.listChangeRequests(ZONE_NAME, CHANGE_REQUEST_LIST_OPTIONS)).andThrow(EXCEPTION) + .times(2); replay(dns); try { zoneNoId.listChangeRequests(); @@ -494,8 +470,7 @@ public void testFromPb() { @Test public void testEqualsAndToBuilder() { - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS).times(2); replay(dns); assertEquals(zone, zone.toBuilder().build()); assertEquals(zone.hashCode(), zone.toBuilder().build().hashCode()); @@ -504,14 +479,7 @@ public void testEqualsAndToBuilder() { @Test public void testBuilder() { // one for each build() call because it invokes a constructor - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS).times(8); replay(dns); assertNotEquals(zone, zone.toBuilder() .id((new BigInteger(zone.id())).add(BigInteger.ONE).toString()) diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java index 8f7626a5ae0a..dd8e21043181 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java @@ -26,6 +26,7 @@ import com.google.common.collect.ImmutableList; import com.google.gcloud.Page; import com.google.gcloud.dns.ChangeRequest; +import com.google.gcloud.dns.ChangeRequestInfo; import com.google.gcloud.dns.Dns; import com.google.gcloud.dns.DnsException; import com.google.gcloud.dns.DnsOptions; @@ -76,11 +77,11 @@ public class ITDnsTest { .records(ImmutableList.of("ed:ed:12:aa:36:3:3:105")) .ttl(25, TimeUnit.SECONDS) .build(); - private static final ChangeRequest CHANGE_ADD_ZONE1 = ChangeRequest.builder() + private static final ChangeRequestInfo CHANGE_ADD_ZONE1 = ChangeRequest.builder() .add(A_RECORD_ZONE1) .add(AAAA_RECORD_ZONE1) .build(); - private static final ChangeRequest CHANGE_DELETE_ZONE1 = ChangeRequest.builder() + private static final ChangeRequestInfo CHANGE_DELETE_ZONE1 = ChangeRequest.builder() .delete(A_RECORD_ZONE1) .delete(AAAA_RECORD_ZONE1) .build(); @@ -593,7 +594,7 @@ public void testInvalidChangeRequest() { .records(ImmutableList.of("0.255.1.5")) .build(); try { - ChangeRequest validChange = ChangeRequest.builder().add(validA).build(); + ChangeRequestInfo validChange = ChangeRequest.builder().add(validA).build(); zone.applyChangeRequest(validChange); try { zone.applyChangeRequest(validChange); @@ -605,7 +606,7 @@ public void testInvalidChangeRequest() { } // delete with field mismatch RecordSet mismatch = validA.toBuilder().ttl(20, TimeUnit.SECONDS).build(); - ChangeRequest deletion = ChangeRequest.builder().delete(mismatch).build(); + ChangeRequestInfo deletion = ChangeRequest.builder().delete(mismatch).build(); try { zone.applyChangeRequest(deletion); fail("Deleted a record set without a complete match."); @@ -629,7 +630,7 @@ public void testInvalidChangeRequest() { } } deletion = deletion.toBuilder().deletions(deletions).build(); - ChangeRequest addition = ChangeRequest.builder().additions(additions).build(); + ChangeRequestInfo addition = ChangeRequest.builder().additions(additions).build(); try { zone.applyChangeRequest(deletion); fail("Deleted SOA."); @@ -647,7 +648,7 @@ public void testInvalidChangeRequest() { assertEquals(400, ex.code()); } } finally { - ChangeRequest deletion = ChangeRequest.builder().delete(validA).build(); + ChangeRequestInfo deletion = ChangeRequest.builder().delete(validA).build(); ChangeRequest request = zone.applyChangeRequest(deletion); waitForChangeToComplete(zone.name(), request.id()); zone.delete(); diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java index 40ce61b07281..a9e5c5d25377 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java @@ -19,6 +19,7 @@ import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import com.google.gcloud.dns.ChangeRequest; +import com.google.gcloud.dns.ChangeRequestInfo; import com.google.gcloud.dns.Dns; import com.google.gcloud.dns.DnsOptions; import com.google.gcloud.dns.ProjectInfo; @@ -207,7 +208,7 @@ public void run(Dns dns, String... args) { .records(ImmutableList.of(ip)) .ttl(ttl, TimeUnit.SECONDS) .build(); - ChangeRequest changeRequest = ChangeRequest.builder() + ChangeRequestInfo changeRequest = ChangeRequest.builder() .delete(recordSet) .build(); changeRequest = dns.applyChangeRequest(zoneName, changeRequest); @@ -254,7 +255,7 @@ public void run(Dns dns, String... args) { .records(ImmutableList.of(ip)) .ttl(ttl, TimeUnit.SECONDS) .build(); - ChangeRequest changeRequest = ChangeRequest.builder().add(recordSet).build(); + ChangeRequestInfo changeRequest = ChangeRequest.builder().add(recordSet).build(); changeRequest = dns.applyChangeRequest(zoneName, changeRequest); System.out.printf("The request for adding A record %s for zone %s was successfully " + "submitted and assigned ID %s.%n", recordName, zoneName, changeRequest.id()); @@ -444,9 +445,9 @@ private static void printZone(Zone zone) { System.out.printf("Name servers: %s%n", Joiner.on(", ").join(zone.nameServers())); } - private static ChangeRequest waitForChangeToFinish(Dns dns, String zoneName, - ChangeRequest request) { - ChangeRequest current = request; + private static ChangeRequestInfo waitForChangeToFinish(Dns dns, String zoneName, + ChangeRequestInfo request) { + ChangeRequestInfo current = request; while (current.status().equals(ChangeRequest.Status.PENDING)) { System.out.print("."); try { diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateOrUpdateRecordSets.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateOrUpdateRecordSets.java index 74647daf666e..e3ddbb10fc0f 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateOrUpdateRecordSets.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateOrUpdateRecordSets.java @@ -22,7 +22,7 @@ package com.google.gcloud.examples.dns.snippets; -import com.google.gcloud.dns.ChangeRequest; +import com.google.gcloud.dns.ChangeRequestInfo; import com.google.gcloud.dns.Dns; import com.google.gcloud.dns.DnsOptions; import com.google.gcloud.dns.RecordSet; @@ -55,7 +55,7 @@ public static void main(String... args) { .build(); // Make a change - ChangeRequest.Builder changeBuilder = ChangeRequest.builder().add(toCreate); + ChangeRequestInfo.Builder changeBuilder = ChangeRequestInfo.builder().add(toCreate); // Verify a www.. type A record does not exist yet. // If it does exist, we will overwrite it with our prepared record. @@ -68,7 +68,7 @@ public static void main(String... args) { } // Build and apply the change request to our zone - ChangeRequest changeRequest = changeBuilder.build(); + ChangeRequestInfo changeRequest = changeBuilder.build(); zone.applyChangeRequest(changeRequest); } } diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java index 27377345b62f..63f26eeebb2a 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java @@ -22,7 +22,7 @@ package com.google.gcloud.examples.dns.snippets; -import com.google.gcloud.dns.ChangeRequest; +import com.google.gcloud.dns.ChangeRequestInfo; import com.google.gcloud.dns.Dns; import com.google.gcloud.dns.DnsOptions; import com.google.gcloud.dns.RecordSet; @@ -47,7 +47,7 @@ public static void main(String... args) { Iterator recordIterator = dns.listRecordSets(zoneName).iterateAll(); // Make a change for deleting the records - ChangeRequest.Builder changeBuilder = ChangeRequest.builder(); + ChangeRequestInfo.Builder changeBuilder = ChangeRequestInfo.builder(); while (recordIterator.hasNext()) { RecordSet current = recordIterator.next(); // SOA and NS records cannot be deleted @@ -57,14 +57,14 @@ public static void main(String... args) { } // Build and apply the change request to our zone if it contains records to delete - ChangeRequest changeRequest = changeBuilder.build(); + ChangeRequestInfo changeRequest = changeBuilder.build(); if (!changeRequest.deletions().isEmpty()) { changeRequest = dns.applyChangeRequest(zoneName, changeRequest); // Wait for change to finish, but save data traffic by transferring only ID and status Dns.ChangeRequestOption option = Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS); - while (ChangeRequest.Status.PENDING.equals(changeRequest.status())) { + while (ChangeRequestInfo.Status.PENDING.equals(changeRequest.status())) { System.out.println("Waiting for change to complete. Going to sleep for 500ms..."); try { Thread.sleep(500); diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java index 6d9d09d704a6..9c9a9e77289c 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java @@ -23,6 +23,7 @@ package com.google.gcloud.examples.dns.snippets; import com.google.gcloud.dns.ChangeRequest; +import com.google.gcloud.dns.ChangeRequestInfo; import com.google.gcloud.dns.Dns; import com.google.gcloud.dns.DnsOptions; import com.google.gcloud.dns.RecordSet; @@ -66,7 +67,7 @@ public static void main(String... args) { .build(); // Make a change - ChangeRequest.Builder changeBuilder = ChangeRequest.builder().add(toCreate); + ChangeRequestInfo.Builder changeBuilder = ChangeRequestInfo.builder().add(toCreate); // Verify the type A record does not exist yet. // If it does exist, we will overwrite it with our prepared record. @@ -79,10 +80,10 @@ public static void main(String... args) { } // Build and apply the change request to our zone - ChangeRequest changeRequest = changeBuilder.build(); + ChangeRequestInfo changeRequest = changeBuilder.build(); zone.applyChangeRequest(changeRequest); - while (ChangeRequest.Status.PENDING.equals(changeRequest.status())) { + while (ChangeRequestInfo.Status.PENDING.equals(changeRequest.status())) { try { Thread.sleep(500L); } catch (InterruptedException e) { @@ -115,7 +116,7 @@ public static void main(String... args) { } // Make a change for deleting the record sets - changeBuilder = ChangeRequest.builder(); + changeBuilder = ChangeRequestInfo.builder(); while (recordSetIterator.hasNext()) { RecordSet current = recordSetIterator.next(); // SOA and NS records cannot be deleted