From 5be58b3e91cade1d349014122484a29e254ba8f3 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Sun, 7 Feb 2016 14:01:52 -0800 Subject: [PATCH] Makes Zone subclass of ZoneInfo. Fixes #605. --- .../main/java/com/google/gcloud/dns/Dns.java | 2 +- .../main/java/com/google/gcloud/dns/Zone.java | 121 ++++++++++++++---- .../java/com/google/gcloud/dns/ZoneInfo.java | 98 ++++++++------ .../java/com/google/gcloud/dns/ZoneTest.java | 61 +++++++-- 4 files changed, 209 insertions(+), 73 deletions(-) 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 040a97e5b253..3ea505b5e09b 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 @@ -439,7 +439,7 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) { * @see Cloud DNS Managed Zones: * get */ - ZoneInfo getZone(String zoneName, ZoneOption... options); + Zone getZone(String zoneName, ZoneOption... options); /** * Lists the zones inside the project. 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 3f4ea8cab3e4..2da67a8e9a01 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 @@ -20,7 +20,8 @@ import com.google.gcloud.Page; -import java.io.Serializable; +import java.util.List; +import java.util.Objects; /** * A Google Cloud DNS Zone object. @@ -33,20 +34,87 @@ * @see Google Cloud DNS managed zone * documentation */ -public class Zone implements Serializable { +public class Zone extends ZoneInfo { - // TODO(mderka) Zone and zoneInfo to be merged. Opened issue #605. + private static final long serialVersionUID = 564454483894599281L; + private transient Dns dns; - private static final long serialVersionUID = 6847890192129375500L; - private final ZoneInfo zoneInfo; - private final Dns dns; + /** + * Builder for {@code Zone}. + */ + public static class Builder extends ZoneInfo.Builder { + private final Dns dns; + private final ZoneInfo.BuilderImpl infoBuilder; + + private Builder(Zone zone) { + this.dns = zone.dns; + this.infoBuilder = new ZoneInfo.BuilderImpl(zone); + } + + @Override + public Builder name(String name) { + infoBuilder.name(name); + return this; + } + + @Override + Builder id(String id) { + infoBuilder.id(id); + return this; + } + + @Override + Builder creationTimeMillis(long creationTimeMillis) { + infoBuilder.creationTimeMillis(creationTimeMillis); + return this; + } + + @Override + public Builder dnsName(String dnsName) { + infoBuilder.dnsName(dnsName); + return this; + } + + @Override + public Builder description(String description) { + infoBuilder.description(description); + return this; + } + + @Override + public Builder nameServerSet(String nameServerSet) { + infoBuilder.nameServerSet(nameServerSet); + return this; + } + + @Override + Builder nameServers(List nameServers) { + infoBuilder.nameServers(nameServers); // infoBuilder makes a copy + return this; + } + + @Override + public Zone build() { + return new Zone(dns, infoBuilder); + } + } + + Zone(Dns dns, ZoneInfo.BuilderImpl infoBuilder) { + super(infoBuilder); + this.dns = dns; + } + + @Override + public Builder toBuilder() { + return new Builder(this); + } /** * Constructs a {@code Zone} object that contains the given {@code zoneInfo}. */ public Zone(Dns dns, ZoneInfo zoneInfo) { - this.zoneInfo = checkNotNull(zoneInfo); - this.dns = checkNotNull(dns); + super(new BuilderImpl(zoneInfo)); + this.dns = dns; } /** @@ -61,8 +129,7 @@ public Zone(Dns dns, ZoneInfo zoneInfo) { public static Zone get(Dns dnsService, String zoneName, Dns.ZoneOption... options) { checkNotNull(zoneName); checkNotNull(dnsService); - ZoneInfo zoneInfo = dnsService.getZone(zoneName, options); - return zoneInfo == null ? null : new Zone(dnsService, zoneInfo); + return dnsService.getZone(zoneName, options); } /** @@ -73,7 +140,7 @@ public static Zone get(Dns dnsService, String zoneName, Dns.ZoneOption... option * @throws DnsException upon failure */ public Zone reload(Dns.ZoneOption... options) { - return Zone.get(dns, zoneInfo.name(), options); + return dns.getZone(name(), options); } /** @@ -83,7 +150,7 @@ public Zone reload(Dns.ZoneOption... options) { * @throws DnsException upon failure */ public boolean delete() { - return dns.delete(zoneInfo.name()); + return dns.delete(name()); } /** @@ -94,7 +161,7 @@ public boolean delete() { * @throws DnsException upon failure or if the zone is not found */ public Page listDnsRecords(Dns.DnsRecordListOption... options) { - return dns.listDnsRecords(zoneInfo.name(), options); + return dns.listDnsRecords(name(), options); } /** @@ -108,7 +175,7 @@ public Page listDnsRecords(Dns.DnsRecordListOption... options) { public ChangeRequest applyChangeRequest(ChangeRequest changeRequest, Dns.ChangeRequestOption... options) { checkNotNull(changeRequest); - return dns.applyChangeRequest(zoneInfo.name(), changeRequest, options); + return dns.applyChangeRequest(name(), changeRequest, options); } /** @@ -124,7 +191,7 @@ public ChangeRequest applyChangeRequest(ChangeRequest changeRequest, public ChangeRequest getChangeRequest(String changeRequestId, Dns.ChangeRequestOption... options) { checkNotNull(changeRequestId); - return dns.getChangeRequest(zoneInfo.name(), changeRequestId, options); + return dns.getChangeRequest(name(), changeRequestId, options); } /** @@ -136,14 +203,7 @@ public ChangeRequest getChangeRequest(String changeRequestId, * @throws DnsException upon failure or if the zone is not found */ public Page listChangeRequests(Dns.ChangeRequestListOption... options) { - return dns.listChangeRequests(zoneInfo.name(), options); - } - - /** - * Returns the {@link ZoneInfo} object containing information about this zone. - */ - public ZoneInfo info() { - return zoneInfo; + return dns.listChangeRequests(name(), options); } /** @@ -152,4 +212,19 @@ public ZoneInfo info() { public Dns dns() { return this.dns; } + + @Override + public boolean equals(Object obj) { + return obj instanceof Zone && Objects.equals(toPb(), ((Zone) obj).toPb()); + } + + @Override + public int hashCode() { + return super.hashCode(); + } + + static Zone fromPb(Dns dns, com.google.api.services.dns.model.ManagedZone zone) { + ZoneInfo info = ZoneInfo.fromPb(zone); + return new Zone(dns, info); + } } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ZoneInfo.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ZoneInfo.java index a15518ae166f..e397e37a7fbf 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ZoneInfo.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ZoneInfo.java @@ -49,9 +49,55 @@ public class ZoneInfo implements Serializable { private final List nameServers; /** - * A builder for {@code ZoneInfo}. + * Builder for {@code ZoneInfo}. */ - public static class Builder { + public abstract static class Builder { + /** + * Sets a mandatory user-provided name for the zone. It must be unique within the project. + */ + public abstract Builder name(String name); + + /** + * Sets an id for the zone which is assigned to the zone by the server. + */ + abstract Builder id(String id); + + /** + * Sets the time when this zone was created. + */ + abstract Builder creationTimeMillis(long creationTimeMillis); + + /** + * Sets a mandatory DNS name of this zone, for instance "example.com.". + */ + public abstract Builder dnsName(String dnsName); + + /** + * Sets a mandatory description for this zone. The value is a string of at most 1024 characters + * which has no effect on the zone's function. + */ + public abstract Builder description(String description); + + /** + * Optionally specifies the NameServerSet for this zone. A NameServerSet is a set of DNS name + * servers that all host the same zones. Most users will not need to specify this value. + */ + public abstract Builder nameServerSet(String nameServerSet); + // todo(mderka) add more to the doc when questions are answered by the service owner + + /** + * Sets a list of servers that hold the information about the zone. This information is provided + * by Google Cloud DNS and is read only. + */ + abstract Builder nameServers(List nameServers); + + /** + * Builds the instance of {@code ZoneInfo} based on the information set by this builder. + */ + public abstract ZoneInfo build(); + } + + public static class BuilderImpl extends Builder { private String name; private String id; private Long creationTimeMillis; @@ -60,14 +106,14 @@ public static class Builder { private String nameServerSet; private List nameServers = new LinkedList<>(); - private Builder(String name) { + private BuilderImpl(String name) { this.name = checkNotNull(name); } /** * Creates a builder from an existing ZoneInfo object. */ - Builder(ZoneInfo info) { + BuilderImpl(ZoneInfo info) { this.name = info.name; this.id = info.id; this.creationTimeMillis = info.creationTimeMillis; @@ -77,76 +123,56 @@ private Builder(String name) { this.nameServers.addAll(info.nameServers); } - /** - * Sets a mandatory user-provided name for the zone. It must be unique within the project. - */ + @Override public Builder name(String name) { this.name = checkNotNull(name); return this; } - /** - * Sets an id for the zone which is assigned to the zone by the server. - */ + @Override Builder id(String id) { this.id = id; return this; } - /** - * Sets the time when this zone was created. - */ + @Override Builder creationTimeMillis(long creationTimeMillis) { this.creationTimeMillis = creationTimeMillis; return this; } - /** - * Sets a mandatory DNS name of this zone, for instance "example.com.". - */ + @Override public Builder dnsName(String dnsName) { this.dnsName = checkNotNull(dnsName); return this; } - /** - * Sets a mandatory description for this zone. The value is a string of at most 1024 characters - * which has no effect on the zone's function. - */ + @Override public Builder description(String description) { this.description = checkNotNull(description); return this; } - /** - * Optionally specifies the NameServerSet for this zone. A NameServerSet is a set of DNS name - * servers that all host the same zones. Most users will not need to specify this value. - */ + @Override public Builder nameServerSet(String nameServerSet) { - // todo(mderka) add more to the doc when questions are answered by the service owner this.nameServerSet = checkNotNull(nameServerSet); return this; } - /** - * Sets a list of servers that hold the information about the zone. This information is provided - * by Google Cloud DNS and is read only. - */ + @Override Builder nameServers(List nameServers) { checkNotNull(nameServers); this.nameServers = Lists.newLinkedList(nameServers); return this; } - /** - * Builds the instance of {@code ZoneInfo} based on the information set by this builder. - */ + @Override public ZoneInfo build() { return new ZoneInfo(this); } } - private ZoneInfo(Builder builder) { + ZoneInfo(BuilderImpl builder) { this.name = builder.name; this.id = builder.id; this.creationTimeMillis = builder.creationTimeMillis; @@ -160,7 +186,7 @@ private ZoneInfo(Builder builder) { * Returns a builder for {@code ZoneInfo} with an assigned {@code name}. */ public static Builder builder(String name) { - return new Builder(name); + return new BuilderImpl(name); } /** @@ -217,7 +243,7 @@ public List nameServers() { * Returns a builder for {@code ZoneInfo} prepopulated with the metadata of this zone. */ public Builder toBuilder() { - return new Builder(this); + return new BuilderImpl(this); } com.google.api.services.dns.model.ManagedZone toPb() { @@ -240,7 +266,7 @@ com.google.api.services.dns.model.ManagedZone toPb() { } static ZoneInfo fromPb(com.google.api.services.dns.model.ManagedZone pb) { - Builder builder = new Builder(pb.getName()); + Builder builder = new BuilderImpl(pb.getName()); if (pb.getDescription() != null) { builder.description(pb.getDescription()); } 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 59cb642167ed..1b09dca715a4 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 @@ -22,23 +22,27 @@ 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.assertNull; 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 com.google.gcloud.Page; import org.junit.After; import org.junit.Before; import org.junit.Test; +import java.math.BigInteger; + public class ZoneTest { private static final String ZONE_NAME = "dns-zone-name"; private static final String ZONE_ID = "123"; - private static final ZoneInfo ZONE_INFO = ZoneInfo.builder(ZONE_NAME) + private static final ZoneInfo ZONE_INFO = Zone.builder(ZONE_NAME) .id(ZONE_ID) .dnsName("example.com") .creationTimeMillis(123478946464L) @@ -80,20 +84,19 @@ public void tearDown() throws Exception { @Test public void testConstructor() { replay(dns); - assertNotNull(zone.info()); - assertEquals(ZONE_INFO, zone.info()); + assertEquals(ZONE_INFO.toPb(), zone.toPb()); assertNotNull(zone.dns()); assertEquals(dns, zone.dns()); } @Test public void testGetByName() { - expect(dns.getZone(ZONE_NAME)).andReturn(ZONE_INFO); - expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(ZONE_INFO); // for options + expect(dns.getZone(ZONE_NAME)).andReturn(zone); + expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(zone); // for options replay(dns); Zone retrieved = Zone.get(dns, ZONE_NAME); assertSame(dns, retrieved.dns()); - assertEquals(ZONE_INFO, retrieved.info()); + assertEquals(zone, retrieved); // test passing options Zone.get(dns, ZONE_NAME, ZONE_FIELD_OPTIONS); try { @@ -188,18 +191,18 @@ public void listDnsRecordsByNameAndNotFound() { @Test public void reloadByNameAndFound() { - expect(dns.getZone(ZONE_NAME)).andReturn(zoneNoId.info()); - expect(dns.getZone(ZONE_NAME)).andReturn(zone.info()); + expect(dns.getZone(ZONE_NAME)).andReturn(zone); + expect(dns.getZone(ZONE_NAME)).andReturn(zone); // again for options - expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(zoneNoId.info()); - expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(zone.info()); + expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(zoneNoId); + expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(zone); replay(dns); Zone result = zoneNoId.reload(); - assertSame(zoneNoId.dns(), result.dns()); - assertEquals(zoneNoId.info(), result.info()); + assertSame(zone.dns(), result.dns()); + assertEquals(zone, result); result = zone.reload(); assertSame(zone.dns(), result.dns()); - assertEquals(zone.info(), result.info()); + assertEquals(zone, result); zoneNoId.reload(ZONE_FIELD_OPTIONS); // check options zone.reload(ZONE_FIELD_OPTIONS); // check options } @@ -499,4 +502,36 @@ public void listChangeRequestsAndZoneNotFound() { // expected } } + + @Test + public void testFromPb() { + replay(dns); + assertEquals(Zone.fromPb(dns, zone.toPb()), zone); + } + + @Test + public void testEqualsAndToBuilder() { + replay(dns); + assertEquals(zone, zone.toBuilder().build()); + } + + @Test + public void testBuilder() { + replay(dns); + assertNotEquals(zone, zone.toBuilder() + .id((new BigInteger(zone.id())).add(BigInteger.ONE).toString()) + .build()); + assertNotEquals(zone, zone.toBuilder().dnsName(zone.name() + "aaaa").build()); + assertNotEquals(zone, zone.toBuilder().nameServerSet(zone.nameServerSet() + "aaaa").build()); + assertNotEquals(zone, zone.toBuilder().nameServers(ImmutableList.of("nameserverpppp")).build()); + assertNotEquals(zone, zone.toBuilder().dnsName(zone.dnsName() + "aaaa").build()); + assertNotEquals(zone, zone.toBuilder().creationTimeMillis(zone.creationTimeMillis() + 1) + .build()); + Zone.Builder builder = zone.toBuilder(); + builder.id(ZONE_ID) + .dnsName("example.com") + .creationTimeMillis(123478946464L) + .build(); + assertEquals(zone, builder.build()); + } }