Skip to content

Commit

Permalink
Implemented comments by @aozarov.
Browse files Browse the repository at this point in the history
  • Loading branch information
mderka committed Jan 23, 2016
1 parent e8dd142 commit c95c3aa
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package com.google.gcloud.dns;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

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;
Expand All @@ -32,15 +32,16 @@
import java.util.Objects;

/**
* This class is a container for the managed zone metainformation. Managed zone is a resource that
* represents a DNS zone hosted by the Cloud DNS service. See <a href="https://cloud.google.com/dns/api/v1/managedZones">Google
* Cloud DNS documentation</a> for more information.
* A {@code ManagedZone} represents a DNS zone hosted by the Google Cloud DNS service. A zone is a
* subtree of the DNS namespace under one administrative responsibility. See <a
* href="https://cloud.google.com/dns/api/v1/managedZones">Google Cloud DNS documentation</a> for
* more information.
*/
public class ManagedZoneInfo implements Serializable {

private static final long serialVersionUID = 201601191647L;
private final String name;
private final Long id;
private final BigInteger id;
private final Long creationTimeMillis;
private final String dnsName;
private final String description;
Expand All @@ -52,25 +53,29 @@ public class ManagedZoneInfo implements Serializable {
*/
public static class Builder {
private String name;
private Long id;
private BigInteger id;
private Long creationTimeMillis;
private String dnsName;
private String description;
private String nameServerSet;
private List<String> nameServers = new LinkedList<>();

/**
* Returns an empty builder for {@code ManagedZoneInfo}. We use it internally in {@code
* toPb()}.
*/
private Builder() {
}

private Builder(Long id) {
private Builder(BigInteger id) {
this.id = checkNotNull(id);
}

private Builder(String name) {
this.name = checkNotNull(name);
}

private Builder(String name, Long id) {
private Builder(String name, BigInteger id) {
this.name = checkNotNull(name);
this.id = checkNotNull(id);
}
Expand Down Expand Up @@ -99,7 +104,7 @@ public Builder name(String name) {
/**
* Sets an id for the managed zone which is assigned to the managed zone by the server.
*/
public Builder id(long id) {
Builder id(BigInteger id) {
this.id = id;
return this;
}
Expand All @@ -108,7 +113,6 @@ public Builder id(long id) {
* Sets the time when this managed zone was created.
*/
Builder creationTimeMillis(long creationTimeMillis) {
checkArgument(creationTimeMillis >= 0, "The timestamp cannot be negative.");
this.creationTimeMillis = creationTimeMillis;
return this;
}
Expand All @@ -123,8 +127,7 @@ public Builder dnsName(String dnsName) {

/**
* Sets a mandatory description for this managed zone. The value is a string of at most 1024
* characters (this limit is posed by Google Cloud DNS; gcloud-java does not enforce the limit)
* which has no effect on the managed zone's function.
* characters which has no effect on the managed zone's function.
*/
public Builder description(String description) {
this.description = checkNotNull(description);
Expand All @@ -133,7 +136,8 @@ public Builder description(String description) {

/**
* Optionally specifies the NameServerSet for this managed zone. A NameServerSet is a set of DNS
* name servers that all host the same ManagedZones.
* name servers that all host the same ManagedZones. Most users will not need to specify this
* value.
*/
public Builder nameServerSet(String nameServerSet) {
// todo(mderka) add more to the doc when questions are answered by the service owner
Expand All @@ -146,20 +150,13 @@ public Builder nameServerSet(String nameServerSet) {
* provided by Google Cloud DNS and is read only.
*/
Builder nameServers(List<String> nameServers) {
this.nameServers.addAll(checkNotNull(nameServers));
checkNotNull(nameServers);
this.nameServers = Lists.newLinkedList(nameServers);
return this;
}

/**
* Removes all the nameservers from the list.
*/
Builder clearNameServers() {
this.nameServers.clear();
return this;
}

/**
* Builds the instance of ManagedZoneInfo based on the information set here.
* Builds the instance of {@code ManagedZoneInfo} based on the information set by this builder.
*/
public ManagedZoneInfo build() {
return new ManagedZoneInfo(this);
Expand All @@ -186,24 +183,17 @@ public static Builder builder(String name) {
/**
* Returns a builder for {@code ManagedZoneInfo} with an assigned {@code id}.
*/
public static Builder builder(Long id) {
public static Builder builder(BigInteger id) {
return new Builder(id);
}

/**
* Returns a builder for {@code ManagedZoneInfo} with an assigned {@code name} and {@code id}.
*/
public static Builder builder(String name, Long id) {
public static Builder builder(String name, BigInteger id) {
return new Builder(name, id);
}

/**
* Returns an empty builder for {@code ManagedZoneInfo}. We use it internally in {@code toPb()}.
*/
private static Builder builder() {
return new Builder();
}

/**
* Returns the user-defined name of the managed zone.
*/
Expand All @@ -214,7 +204,7 @@ public String name() {
/**
* Returns the read-only managed zone id assigned by the server.
*/
public Long id() {
public BigInteger id() {
return id;
}

Expand All @@ -233,8 +223,7 @@ public String dnsName() {
}

/**
* Returns the description of this managed zone. This is at most 1024 long mandatory string
* provided by the user.
* Returns the description of this managed zone.
*/
public String description() {
return description;
Expand Down Expand Up @@ -270,7 +259,7 @@ com.google.api.services.dns.model.ManagedZone toPb() {
pb.setDescription(this.description());
pb.setDnsName(this.dnsName());
if (this.id() != null) {
pb.setId(BigInteger.valueOf(this.id()));
pb.setId(this.id());
}
pb.setName(this.name());
pb.setNameServers(this.nameServers());
Expand All @@ -284,15 +273,15 @@ com.google.api.services.dns.model.ManagedZone toPb() {
}

static ManagedZoneInfo fromPb(com.google.api.services.dns.model.ManagedZone pb) {
Builder b = builder();
Builder b = new Builder();
if (pb.getDescription() != null) {
b.description(pb.getDescription());
}
if (pb.getDnsName() != null) {
b.dnsName(pb.getDnsName());
}
if (pb.getId() != null) {
b.id(pb.getId().longValue());
b.id(pb.getId());
}
if (pb.getName() != null) {
b.name(pb.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,48 +17,45 @@
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.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.google.common.collect.Lists;

import org.junit.BeforeClass;
import org.junit.Before;
import org.junit.Test;

import java.math.BigInteger;
import java.util.LinkedList;
import java.util.List;

public class ManagedZoneInfoTest {

private static final String NAME = "mz-example.com";
private static final Long ID = 123L;
private static final BigInteger ID = BigInteger.valueOf(123L);
private static final Long CREATION_TIME_MILLIS = 1123468321321L;
private static final String DNS_NAME = "example.com.";
private static final String DESCRIPTION = "description for the zone";
private static final String NAME_SERVER_SET = "some set";
private static final String NS1 = "name server 1";
private static final String NS2 = "name server 2";
private static final String NS3 = "name server 3";
private static List<String> nameServers = new LinkedList<>();
private static ManagedZoneInfo info;
private List<String> nameServers = new LinkedList<>();
private ManagedZoneInfo info;

@BeforeClass
public static void setUp() {
@Before
public void setUp() {
nameServers.add(NS1);
nameServers.add(NS2);
nameServers.add(NS3);
assertEquals(3, nameServers.size());
info = ManagedZoneInfo.builder(NAME, ID)
.creationTimeMillis(CREATION_TIME_MILLIS)
.dnsName(DNS_NAME)
.description(DESCRIPTION)
.nameServerSet(NAME_SERVER_SET)
.nameServers(nameServers)
.build();
System.out.println(info);
}

@Test
Expand Down Expand Up @@ -105,12 +102,7 @@ public void testBuilder() {

@Test
public void testValidCreationTime() {
try {
ManagedZoneInfo.builder(NAME).creationTimeMillis(-1);
fail("A negative value is not acceptable for creation time.");
} catch (IllegalArgumentException e) {
// expected
}
ManagedZoneInfo.builder(NAME).creationTimeMillis(-1);
ManagedZoneInfo.builder(NAME).creationTimeMillis(0);
ManagedZoneInfo.builder(NAME).creationTimeMillis(Long.MAX_VALUE);
}
Expand All @@ -132,7 +124,7 @@ public void testEqualsAndNotEquals() {
assertNotEquals(clone, info);
clone = info.toBuilder().dnsName(differentName).build();
assertNotEquals(clone, info);
clone = info.toBuilder().id(info.id() + 1).build();
clone = info.toBuilder().id(info.id().add(BigInteger.ONE)).build();
assertNotEquals(clone, info);
clone = info.toBuilder().nameServerSet(info.nameServerSet() + "salt").build();
assertNotEquals(clone, info);
Expand Down Expand Up @@ -188,11 +180,20 @@ public void testToAndFromPb() {
}

@Test
public void testClearNameServers() {
ManagedZoneInfo clone = info.toBuilder().build();
assertFalse(clone.nameServers().isEmpty());
clone = clone.toBuilder().clearNameServers().build();
public void testEmptyNameServers() {
ManagedZoneInfo clone = info.toBuilder().nameServers(new LinkedList<String>()).build();
assertTrue(clone.nameServers().isEmpty());
clone.toPb(); // test that this is allowed
}

@Test
public void testDateParsing() {
com.google.api.services.dns.model.ManagedZone pb =
info.toPb();
pb.setCreationTime("2016-01-19T18:00:12.854Z"); // a real value obtained from Google Cloud DNS
ManagedZoneInfo mz = ManagedZoneInfo.fromPb(pb); // parses the string timestamp to millis
com.google.api.services.dns.model.ManagedZone pbClone = mz.toPb(); // converts it back to string
assertEquals(pb, pbClone);
assertEquals(pb.getCreationTime(), pbClone.getCreationTime());
}
}

0 comments on commit c95c3aa

Please sign in to comment.