Skip to content

Commit

Permalink
Implemented comments by @aozarov. Added test for option accessors.
Browse files Browse the repository at this point in the history
  • Loading branch information
mderka committed Feb 1, 2016
1 parent adf5c1c commit a9cc927
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 58 deletions.
86 changes: 31 additions & 55 deletions gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@
*/
public interface Dns extends Service<DnsOptions> {



/**
* The fields of a project.
*
* <p>These values can be used to specify the fields to include in a partial response when calling
* {@code Dns#getProjectInfo(ProjectGetOption...)}. Project ID is always returned, even if
* not specified.
* {@code Dns#getProjectInfo(ProjectGetOption...)}. Project ID is always returned, even if not
* specified.
*/
enum ProjectField {
PROJECT_ID("id"),
Expand Down Expand Up @@ -67,8 +69,8 @@ static String selector(ProjectField... fields) {
* The fields of a zone.
*
* <p>These values can be used to specify the fields to include in a partial response when calling
* {@code Dns#getZone(BigInteger, ZoneFieldOption...)} or {@code Dns#getZone(String,
* ZoneFieldOption...)}. The ID is always returned, even if not specified.
* {@code Dns#getZone(BigInteger, ZoneOption...)} or {@code Dns#getZone(String, ZoneOption...)}.
* The ID is always returned, even if not specified.
*/
enum ZoneField {
CREATION_TIME("creationTime"),
Expand Down Expand Up @@ -104,8 +106,8 @@ static String selector(ZoneField... fields) {
*
* <p>These values can be used to specify the fields to include in a partial response when calling
* {@code Dns#listDnsRecords(BigInteger, DnsRecordListOption...)} or {@code
* Dns#listDnsRecords(String, DnsRecordListOption...)}. The name is always returned even if
* not selected.
* Dns#listDnsRecords(String, DnsRecordListOption...)}. The name is always returned even if not
* selected.
*/
enum DnsRecordField {
DNS_RECORDS("rrdatas"),
Expand Down Expand Up @@ -137,9 +139,9 @@ static String selector(DnsRecordField... fields) {
* The fields of a change request.
*
* <p>These values can be used to specify the fields to include in a partial response when calling
* {@code Dns#applyChangeRequest(ChangeRequest, BigInteger, ChangeRequestOption...)}
* or {@code Dns#applyChangeRequest(ChangeRequest, String, ChangeRequestOption...)}
* The ID is always returned even if not selected.
* {@code Dns#applyChangeRequest(ChangeRequest, BigInteger, ChangeRequestOption...)} or {@code
* Dns#applyChangeRequest(ChangeRequest, String, ChangeRequestOption...)} The ID is always
* returned even if not selected.
*/
enum ChangeRequestField {
ID("id"),
Expand Down Expand Up @@ -168,24 +170,6 @@ static String selector(ChangeRequestField... fields) {
}
}

/**
* The sorting keys for listing change requests. The only currently supported sorting key is the
* when the change request was created.
*/
enum ChangeRequestSortingKey {
TIME_CREATED("changeSequence");

private final String selector;

ChangeRequestSortingKey(String selector) {
this.selector = selector;
}

String selector() {
return selector;
}
}

/**
* The sorting order for listing.
*/
Expand Down Expand Up @@ -261,24 +245,23 @@ public static DnsRecordListOption type(DnsRecord.Type type) {
/**
* Class for specifying zone field options.
*/
class ZoneFieldOption extends AbstractOption implements Serializable {
class ZoneOption extends AbstractOption implements Serializable {

private static final long serialVersionUID = -8065564464895945037L;

ZoneFieldOption(DnsRpc.Option option, Object value) {
ZoneOption(DnsRpc.Option option, Object value) {
super(option, value);
}

/**
* Returns an option to specify the zones's fields to be returned by the RPC call.
*
* <p>If this option is not provided all zone fields are returned. {@code
* ZoneFieldOption.fields} can be used to specify only the fields of interest. Zone ID is always
* returned, even if not specified. {@link ZoneField} provides a list of fields that can be
* used.
* <p>If this option is not provided all zone fields are returned. {@code ZoneOption.fields} can
* be used to specify only the fields of interest. Zone ID is always returned, even if not
* specified. {@link ZoneField} provides a list of fields that can be used.
*/
public static ZoneFieldOption fields(ZoneField... fields) {
return new ZoneFieldOption(DnsRpc.Option.FIELDS, ZoneField.selector(fields));
public static ZoneOption fields(ZoneField... fields) {
return new ZoneOption(DnsRpc.Option.FIELDS, ZoneField.selector(fields));
}
}

Expand All @@ -296,10 +279,9 @@ class ZoneListOption extends AbstractOption implements Serializable {
/**
* Returns an option to specify the zones's fields to be returned by the RPC call.
*
* <p>If this option is not provided all zone fields are returned. {@code
* ZoneFieldOption.fields} can be used to specify only the fields of interest. Zone ID is always
* returned, even if not specified. {@link ZoneField} provides a list of fields that can be
* used.
* <p>If this option is not provided all zone fields are returned. {@code ZoneOption.fields} can
* be used to specify only the fields of interest. Zone ID is always returned, even if not
* specified. {@link ZoneField} provides a list of fields that can be used.
*/
public static ZoneListOption fields(ZoneField... fields) {
return new ZoneListOption(DnsRpc.Option.FIELDS, ZoneField.selector(fields));
Expand Down Expand Up @@ -366,9 +348,9 @@ class ChangeRequestOption extends AbstractOption implements Serializable {
* service.
*
* <p>If this option is not provided all change request fields are returned. {@code
* ChangeRequestOption.fields} can be used to specify only the fields of interest. The ID
* of the change request is always returned, even if not specified. {@link ChangeRequestField}
* provides a list of fields that can be used.
* ChangeRequestOption.fields} can be used to specify only the fields of interest. The ID of the
* change request is always returned, even if not specified. {@link ChangeRequestField} provides
* a list of fields that can be used.
*/
public static ChangeRequestOption fields(ChangeRequestField... fields) {
return new ChangeRequestOption(
Expand All @@ -394,9 +376,9 @@ class ChangeRequestListOption extends AbstractOption implements Serializable {
* service.
*
* <p>If this option is not provided all change request fields are returned. {@code
* ChangeRequestOption.fields} can be used to specify only the fields of interest. The ID
* of the change request is always returned, even if not specified. {@link ChangeRequestField}
* provides a list of fields that can be used.
* ChangeRequestOption.fields} can be used to specify only the fields of interest. The ID of the
* change request is always returned, even if not specified. {@link ChangeRequestField} provides
* a list of fields that can be used.
*/
public static ChangeRequestListOption fields(ChangeRequestField... fields) {
return new ChangeRequestListOption(
Expand Down Expand Up @@ -427,16 +409,10 @@ public static ChangeRequestListOption pageSize(int pageSize) {
}

/**
* Returns an option for specifying the sorting criterion of change requests. Note the the only
* currently supported criterion is the change sequence.
*/
public static ChangeRequestListOption sortBy(ChangeRequestSortingKey key) {
return new ChangeRequestListOption(DnsRpc.Option.SORTING_KEY, key.selector());
}

/**
* Returns an option to specify whether the the change requests should be listed in ascending or
* descending order.
* Returns an option to specify whether the the change requests should be listed in ascending
* (most-recent last) or descending (most-recent first) order with respect to when the change
* request was accepted by the server. If this option is not provided, the listing order is
* undefined.
*/
public static ChangeRequestListOption sortOrder(SortingOrder order) {
return new ChangeRequestListOption(DnsRpc.Option.SORTING_ORDER, order.selector());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public class DnsOptions

private static final long serialVersionUID = -519128051411747771L;
private static final String GC_DNS_RW = "https://www.googleapis.com/auth/ndev.clouddns.readwrite";
private static final String GC_DNS_R = "https://www.googleapis.com/auth/ndev.clouddns.readonly";
private static final Set<String> SCOPES = ImmutableSet.of(GC_DNS_RW, GC_DNS_R);
private static final Set<String> SCOPES = ImmutableSet.of(GC_DNS_RW);

public static class DefaultDnsFactory implements DnsFactory {
private static final DnsFactory INSTANCE = new DefaultDnsFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ enum Option {
PAGE_TOKEN("pageToken"),
DNS_NAME("dnsName"),
DNS_TYPE("type"),
SORTING_KEY("sortBy"),
SORTING_ORDER("sortOrder");

private final String value;
Expand Down
141 changes: 141 additions & 0 deletions gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* 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.assertTrue;

import com.google.gcloud.spi.DnsRpc;

import org.junit.Test;

public class DnsTest {

private static final Integer PAGE_SIZE = 20;
private static final String PAGE_TOKEN = "page token";

@Test
public void testDnsRecordListOption() {
// dns name
String dnsName = "some name";
Dns.DnsRecordListOption dnsRecordListOption = Dns.DnsRecordListOption.dnsName(dnsName);
assertEquals(dnsName, dnsRecordListOption.value());
assertEquals(DnsRpc.Option.DNS_NAME, dnsRecordListOption.rpcOption());
// page token
dnsRecordListOption = Dns.DnsRecordListOption.pageToken(PAGE_TOKEN);
assertEquals(PAGE_TOKEN, dnsRecordListOption.value());
assertEquals(DnsRpc.Option.PAGE_TOKEN, dnsRecordListOption.rpcOption());
// page size
dnsRecordListOption = Dns.DnsRecordListOption.pageSize(PAGE_SIZE);
assertEquals(PAGE_SIZE, dnsRecordListOption.value());
assertEquals(DnsRpc.Option.PAGE_SIZE, dnsRecordListOption.rpcOption());
// record type
DnsRecord.Type recordType = DnsRecord.Type.AAAA;
dnsRecordListOption = Dns.DnsRecordListOption.type(recordType);
assertEquals(recordType, dnsRecordListOption.value());
assertEquals(DnsRpc.Option.DNS_TYPE, dnsRecordListOption.rpcOption());
// fields
dnsRecordListOption = Dns.DnsRecordListOption.fields(Dns.DnsRecordField.NAME,
Dns.DnsRecordField.TTL);
assertEquals(DnsRpc.Option.FIELDS, dnsRecordListOption.rpcOption());
assertTrue(dnsRecordListOption.value() instanceof String);
assertTrue(((String) dnsRecordListOption.value()).contains(
Dns.DnsRecordField.NAME.selector()));
assertTrue(((String) dnsRecordListOption.value()).contains(
Dns.DnsRecordField.TTL.selector()));
assertTrue(((String) dnsRecordListOption.value()).contains(
Dns.DnsRecordField.NAME.selector()));
}

@Test
public void testZoneOption() {
Dns.ZoneOption fields = Dns.ZoneOption.fields(Dns.ZoneField.CREATION_TIME,
Dns.ZoneField.DESCRIPTION);
assertEquals(DnsRpc.Option.FIELDS, fields.rpcOption());
assertTrue(fields.value() instanceof String);
assertTrue(((String) fields.value()).contains(Dns.ZoneField.CREATION_TIME.selector()));
assertTrue(((String) fields.value()).contains(Dns.ZoneField.DESCRIPTION.selector()));
}

@Test
public void testZoneList() {
// fields
Dns.ZoneListOption fields = Dns.ZoneListOption.fields(Dns.ZoneField.CREATION_TIME,
Dns.ZoneField.DESCRIPTION);
assertEquals(DnsRpc.Option.FIELDS, fields.rpcOption());
assertTrue(fields.value() instanceof String);
assertTrue(((String) fields.value()).contains(Dns.ZoneField.CREATION_TIME.selector()));
assertTrue(((String) fields.value()).contains(Dns.ZoneField.DESCRIPTION.selector()));
assertTrue(((String) fields.value()).contains(Dns.ZoneField.ZONE_ID.selector()));
// page token
Dns.ZoneListOption option = Dns.ZoneListOption.pageToken(PAGE_TOKEN);
assertEquals(PAGE_TOKEN, option.value());
assertEquals(DnsRpc.Option.PAGE_TOKEN, option.rpcOption());
// page size
option = Dns.ZoneListOption.pageSize(PAGE_SIZE);
assertEquals(PAGE_SIZE, option.value());
assertEquals(DnsRpc.Option.PAGE_SIZE, option.rpcOption());
}

@Test
public void testProjectGetOption() {
// fields
Dns.ProjectGetOption fields = Dns.ProjectGetOption.fields(Dns.ProjectField.QUOTA);
assertEquals(DnsRpc.Option.FIELDS, fields.rpcOption());
assertTrue(fields.value() instanceof String);
assertTrue(((String) fields.value()).contains(Dns.ProjectField.QUOTA.selector()));
assertTrue(((String) fields.value()).contains(Dns.ProjectField.PROJECT_ID.selector()));
}

@Test
public void testChangeRequestOption() {
// fields
Dns.ChangeRequestOption fields = Dns.ChangeRequestOption.fields(
Dns.ChangeRequestField.START_TIME, Dns.ChangeRequestField.STATUS);
assertEquals(DnsRpc.Option.FIELDS, fields.rpcOption());
assertTrue(fields.value() instanceof String);
assertTrue(((String) fields.value()).contains(
Dns.ChangeRequestField.START_TIME.selector()));
assertTrue(((String) fields.value()).contains(Dns.ChangeRequestField.STATUS.selector()));
assertTrue(((String) fields.value()).contains(Dns.ChangeRequestField.ID.selector()));
}

@Test
public void testChangeRequestListOption() {
// fields
Dns.ChangeRequestListOption fields = Dns.ChangeRequestListOption.fields(
Dns.ChangeRequestField.START_TIME, Dns.ChangeRequestField.STATUS);
assertEquals(DnsRpc.Option.FIELDS, fields.rpcOption());
assertTrue(fields.value() instanceof String);
assertTrue(((String) fields.value()).contains(
Dns.ChangeRequestField.START_TIME.selector()));
assertTrue(((String) fields.value()).contains(Dns.ChangeRequestField.STATUS.selector()));
assertTrue(((String) fields.value()).contains(Dns.ChangeRequestField.ID.selector()));
// page token
Dns.ChangeRequestListOption option = Dns.ChangeRequestListOption.pageToken(PAGE_TOKEN);
assertEquals(PAGE_TOKEN, option.value());
assertEquals(DnsRpc.Option.PAGE_TOKEN, option.rpcOption());
// page size
option = Dns.ChangeRequestListOption.pageSize(PAGE_SIZE);
assertEquals(PAGE_SIZE, option.value());
assertEquals(DnsRpc.Option.PAGE_SIZE, option.rpcOption());
// sort order
option = Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING);
assertEquals(DnsRpc.Option.SORTING_ORDER, option.rpcOption());
assertEquals(Dns.SortingOrder.ASCENDING.selector(), option.value());
}
}

0 comments on commit a9cc927

Please sign in to comment.