Skip to content

Commit

Permalink
Included options attribute to Zone. Fixed symmetry of toPb and fromPb.
Browse files Browse the repository at this point in the history
Finished DnsOptions. Fixed googleapis#595.
  • Loading branch information
mderka committed Mar 1, 2016
1 parent 8a7315c commit 991aa23
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 47 deletions.
44 changes: 22 additions & 22 deletions gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ public Page<DnsRecord> nextPage() {
}
}

@Override
public Page<Zone> listZones(ZoneListOption... options) {
return listZones(options(), optionMap(options));
}

private static Page<Zone> listZones(final DnsOptions serviceOptions,
final Map<DnsRpc.Option, ?> optionsMap) {
// define transformation function
Expand Down Expand Up @@ -151,9 +156,16 @@ public DnsRpc.ListResult<ManagedZone> call() {
}
}

@Override
public Page<ChangeRequest> listChangeRequests(String zoneName,
ChangeRequestListOption... options) {
return listChangeRequests(zoneName, options(), optionMap(options),
ChangeRequestPageFetcher.PB_TO_CHANGE_REQUEST);
}

private static Page<ChangeRequest> listChangeRequests(final String zoneName,
final DnsOptions serviceOptions, final Map<DnsRpc.Option, ?> optionsMap,
Function<Change, ChangeRequest> TRANSFORM_FUNCTION) {
Function<Change, ChangeRequest> transformFunction) {
try {
// get a list of changes
DnsRpc.ListResult<Change> result = runWithRetries(new Callable<DnsRpc.ListResult<Change>>() {
Expand All @@ -166,17 +178,23 @@ public DnsRpc.ListResult<Change> call() {
// transform that list into change request objects
Iterable<ChangeRequest> changes = result.results() == null
? ImmutableList.<ChangeRequest>of()
: Iterables.transform(result.results(), TRANSFORM_FUNCTION);
: Iterables.transform(result.results(), transformFunction);
return new PageImpl<>(new ChangeRequestPageFetcher(zoneName, serviceOptions, cursor,
optionsMap), cursor, changes);
} catch (RetryHelperException e) {
throw DnsException.translateAndThrow(e);
}
}

@Override
public Page<DnsRecord> listDnsRecords(String zoneName, DnsRecordListOption... options) {
return listDnsRecords(zoneName, options(), optionMap(options),
DnsRecordPageFetcher.PB_TO_DNS_RECORD);
}

private static Page<DnsRecord> listDnsRecords(final String zoneName,
final DnsOptions serviceOptions, final Map<DnsRpc.Option, ?> optionsMap,
Function<ResourceRecordSet, DnsRecord> TRANSFORM_FUNCTION) {
Function<ResourceRecordSet, DnsRecord> transformFunction) {
try {
// get a list of resource record sets
DnsRpc.ListResult<ResourceRecordSet> result = runWithRetries(
Expand All @@ -190,7 +208,7 @@ public DnsRpc.ListResult<ResourceRecordSet> call() {
// transform that list into dns records
Iterable<DnsRecord> records = result.results() == null
? ImmutableList.<DnsRecord>of()
: Iterables.transform(result.results(), TRANSFORM_FUNCTION);
: Iterables.transform(result.results(), transformFunction);
return new PageImpl<>(new DnsRecordPageFetcher(zoneName, serviceOptions, cursor, optionsMap),
cursor, records);
} catch (RetryHelperException e) {
Expand All @@ -203,24 +221,6 @@ public DnsRpc.ListResult<ResourceRecordSet> call() {
dnsRpc = options.rpc();
}

@Override
public Page<Zone> listZones(ZoneListOption... options) {
return listZones(options(), optionMap(options));
}

@Override
public Page<ChangeRequest> listChangeRequests(String zoneName,
ChangeRequestListOption... options) {
return listChangeRequests(zoneName, options(), optionMap(options),
ChangeRequestPageFetcher.PB_TO_CHANGE_REQUEST);
}

@Override
public Page<DnsRecord> listDnsRecords(String zoneName, DnsRecordListOption... options) {
return listDnsRecords(zoneName, options(), optionMap(options),
DnsRecordPageFetcher.PB_TO_DNS_RECORD);
}

@Override
public Zone create(final ZoneInfo zoneInfo, Dns.ZoneOption... options) {
final Map<DnsRpc.Option, ?> optionsMap = optionMap(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@

import java.util.Set;

public class DnsOptions
extends ServiceOptions<Dns, DnsRpc, DnsOptions> {
public class DnsOptions extends ServiceOptions<Dns, DnsRpc, DnsOptions> {

private static final long serialVersionUID = -519128051411747771L;
private static final String GC_DNS_RW = "https://www.googleapis.com/auth/ndev.clouddns.readwrite";
Expand All @@ -36,7 +35,6 @@ public static class DefaultDnsFactory implements DnsFactory {

@Override
public Dns create(DnsOptions options) {
// TODO(mderka) Implement when DnsImpl is available. Created issue #595.
return new DnsImpl(options);
}
}
Expand Down
17 changes: 14 additions & 3 deletions gcloud-java-dns/src/main/java/com/google/gcloud/dns/Zone.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import com.google.gcloud.Page;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;
import java.util.Objects;

Expand All @@ -36,7 +38,8 @@
*/
public class Zone extends ZoneInfo {

private static final long serialVersionUID = 564454483894599281L;
private static final long serialVersionUID = -5817771337847861598L;
private final DnsOptions options;
private transient Dns dns;

/**
Expand Down Expand Up @@ -102,6 +105,7 @@ public Zone build() {
Zone(Dns dns, ZoneInfo.BuilderImpl infoBuilder) {
super(infoBuilder);
this.dns = dns;
this.options = dns.options();
}

@Override
Expand All @@ -115,6 +119,7 @@ public Builder toBuilder() {
public Zone(Dns dns, ZoneInfo zoneInfo) {
super(new BuilderImpl(zoneInfo));
this.dns = dns;
this.options = dns.options();
}

/**
Expand Down Expand Up @@ -215,12 +220,18 @@ public Dns dns() {

@Override
public boolean equals(Object obj) {
return obj instanceof Zone && Objects.equals(toPb(), ((Zone) obj).toPb());
return obj instanceof Zone && Objects.equals(toPb(), ((Zone) obj).toPb())
&& Objects.equals(options, ((Zone) obj).options);
}

@Override
public int hashCode() {
return super.hashCode();
return Objects.hash(super.hashCode(), options);
}

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
this.dns = options.service();
}

static Zone fromPb(Dns dns, com.google.api.services.dns.model.ManagedZone zone) {
Expand Down
19 changes: 11 additions & 8 deletions gcloud-java-dns/src/main/java/com/google/gcloud/dns/ZoneInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import java.io.Serializable;
import java.math.BigInteger;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -97,14 +96,14 @@ public abstract static class Builder {
public abstract ZoneInfo build();
}

public static class BuilderImpl extends Builder {
static class BuilderImpl extends Builder {
private String name;
private String id;
private Long creationTimeMillis;
private String dnsName;
private String description;
private String nameServerSet;
private List<String> nameServers = new LinkedList<>();
private List<String> nameServers;

private BuilderImpl(String name) {
this.name = checkNotNull(name);
Expand All @@ -120,7 +119,9 @@ private BuilderImpl(String name) {
this.dnsName = info.dnsName;
this.description = info.description;
this.nameServerSet = info.nameServerSet;
this.nameServers.addAll(info.nameServers);
if (info.nameServers != null) {
this.nameServers = ImmutableList.copyOf(info.nameServers);
}
}

@Override
Expand Down Expand Up @@ -179,7 +180,8 @@ public ZoneInfo build() {
this.dnsName = builder.dnsName;
this.description = builder.description;
this.nameServerSet = builder.nameServerSet;
this.nameServers = ImmutableList.copyOf(builder.nameServers);
this.nameServers = builder.nameServers == null
? null : ImmutableList.copyOf(builder.nameServers);
}

/**
Expand Down Expand Up @@ -236,7 +238,7 @@ public String nameServerSet() {
* The nameservers that the zone should be delegated to. This is defined by the Google DNS cloud.
*/
public List<String> nameServers() {
return nameServers;
return nameServers == null ? ImmutableList.<String>of() : nameServers;
}

/**
Expand All @@ -255,7 +257,7 @@ com.google.api.services.dns.model.ManagedZone toPb() {
pb.setId(new BigInteger(this.id()));
}
pb.setName(this.name());
pb.setNameServers(this.nameServers());
pb.setNameServers(this.nameServers); // do use real attribute value which may be null
pb.setNameServerSet(this.nameServerSet());
if (this.creationTimeMillis() != null) {
pb.setCreationTime(ISODateTimeFormat.dateTime()
Expand Down Expand Up @@ -290,7 +292,8 @@ static ZoneInfo fromPb(com.google.api.services.dns.model.ManagedZone pb) {

@Override
public boolean equals(Object obj) {
return obj instanceof ZoneInfo && Objects.equals(toPb(), ((ZoneInfo) obj).toPb());
return obj != null && obj.getClass().equals(ZoneInfo.class)
&& Objects.equals(toPb(), ((ZoneInfo) obj).toPb());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ public void testCreateZone() {
.andReturn(ZONE_INFO.toPb());
EasyMock.replay(dnsRpcMock);
dns = options.service(); // creates DnsImpl
ZoneInfo zoneInfo = dns.create(ZONE_INFO);
assertEquals(ZONE_INFO, zoneInfo);
Zone zone = dns.create(ZONE_INFO);
assertEquals(new Zone(dns, ZONE_INFO), zone);
}

@Test
Expand All @@ -149,7 +149,7 @@ public void testCreateZoneWithOptions() {
dns = options.service(); // creates DnsImpl
Zone zone = dns.create(ZONE_INFO, ZONE_FIELDS);
String selector = (String) capturedOptions.getValue().get(ZONE_FIELDS.rpcOption());
assertEquals(ZONE_INFO, zone);
assertEquals(new Zone(dns, ZONE_INFO), zone);
assertTrue(selector.contains(Dns.ZoneField.CREATION_TIME.selector()));
assertTrue(selector.contains(Dns.ZoneField.NAME.selector()));
}
Expand All @@ -160,8 +160,8 @@ public void testGetZone() {
.andReturn(ZONE_INFO.toPb());
EasyMock.replay(dnsRpcMock);
dns = options.service(); // creates DnsImpl
ZoneInfo zoneInfo = dns.getZone(ZONE_INFO.name());
assertEquals(ZONE_INFO, zoneInfo);
Zone zone = dns.getZone(ZONE_INFO.name());
assertEquals(new Zone(dns, ZONE_INFO), zone);
}

@Test
Expand All @@ -171,9 +171,9 @@ public void testGetZoneWithOptions() {
EasyMock.capture(capturedOptions))).andReturn(ZONE_INFO.toPb());
EasyMock.replay(dnsRpcMock);
dns = options.service(); // creates DnsImpl
ZoneInfo zoneInfo = dns.getZone(ZONE_INFO.name(), ZONE_FIELDS);
Zone zone = dns.getZone(ZONE_INFO.name(), ZONE_FIELDS);
String selector = (String) capturedOptions.getValue().get(ZONE_FIELDS.rpcOption());
assertEquals(ZONE_INFO, zoneInfo);
assertEquals(new Zone(dns, ZONE_INFO), zone);
assertTrue(selector.contains(Dns.ZoneField.CREATION_TIME.selector()));
assertTrue(selector.contains(Dns.ZoneField.NAME.selector()));
}
Expand Down Expand Up @@ -308,7 +308,7 @@ public void testListZones() {
dns = options.service(); // creates DnsImpl
Page<Zone> zonePage = dns.listZones();
assertEquals(1, Lists.newArrayList(zonePage.values()).size());
assertEquals(ZONE_INFO, Lists.newArrayList(zonePage.values()).get(0));
assertEquals(new Zone(dns, ZONE_INFO), Lists.newArrayList(zonePage.values()).get(0));
}

@Test
Expand All @@ -320,7 +320,7 @@ public void testListZonesWithOptions() {
dns = options.service(); // creates DnsImpl
Page<Zone> zonePage = dns.listZones(ZONE_LIST_OPTIONS);
assertEquals(1, Lists.newArrayList(zonePage.values()).size());
assertEquals(ZONE_INFO, Lists.newArrayList(zonePage.values()).get(0));
assertEquals(new Zone(dns, ZONE_INFO), Lists.newArrayList(zonePage.values()).get(0));
Integer size = (Integer) capturedOptions.getValue().get(ZONE_LIST_OPTIONS[0].rpcOption());
assertEquals(MAX_SIZE, size);
String selector = (String) capturedOptions.getValue().get(ZONE_LIST_OPTIONS[1].rpcOption());
Expand Down
24 changes: 22 additions & 2 deletions gcloud-java-dns/src/test/java/com/google/gcloud/dns/ZoneTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand Down Expand Up @@ -64,16 +65,22 @@ public class ZoneTest {
.startTimeMillis(123465L).build();
private static final ChangeRequest CHANGE_REQUEST_NO_ID = ChangeRequest.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;


@Before
public void setUp() throws Exception {
dns = createStrictMock(Dns.class);
expect(dns.options()).andReturn(OPTIONS);
expect(dns.options()).andReturn(OPTIONS);
replay(dns);
zone = new Zone(dns, ZONE_INFO);
zoneNoId = new Zone(dns, NO_ID_INFO);
reset(dns);
}

@After
Expand Down Expand Up @@ -347,13 +354,13 @@ public void getChangeAndZoneNotFoundByName() {
.andThrow(EXCEPTION);
replay(dns);
try {
ChangeRequest result = zoneNoId.getChangeRequest(CHANGE_REQUEST.id());
zoneNoId.getChangeRequest(CHANGE_REQUEST.id());
fail("Parent container not found, should throw an exception.");
} catch (DnsException e) {
// expected
}
try {
ChangeRequest result = zone.getChangeRequest(CHANGE_REQUEST.id());
zone.getChangeRequest(CHANGE_REQUEST.id());
fail("Parent container not found, should throw an exception.");
} catch (DnsException e) {
// expected
Expand Down Expand Up @@ -505,18 +512,31 @@ public void listChangeRequestsAndZoneNotFound() {

@Test
public void testFromPb() {
expect(dns.options()).andReturn(OPTIONS);
replay(dns);
assertEquals(Zone.fromPb(dns, zone.toPb()), zone);
}

@Test
public void testEqualsAndToBuilder() {
expect(dns.options()).andReturn(OPTIONS);
expect(dns.options()).andReturn(OPTIONS);
replay(dns);
assertEquals(zone, zone.toBuilder().build());
assertEquals(zone.hashCode(), zone.toBuilder().build().hashCode());
}

@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);
replay(dns);
assertNotEquals(zone, zone.toBuilder()
.id((new BigInteger(zone.id())).add(BigInteger.ONE).toString())
Expand Down

0 comments on commit 991aa23

Please sign in to comment.