From 71f5ae22187569bf20461bcb6021761bc9864856 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Thu, 4 Feb 2016 13:54:44 -0800 Subject: [PATCH 1/4] Makes name of Zone mandatory and removes id-based methods. Also makes id a read-only string instead of BigInteger. Includes rewriting testsfor Zone. --- .../main/java/com/google/gcloud/dns/Dns.java | 81 +-- .../main/java/com/google/gcloud/dns/Zone.java | 127 +--- .../java/com/google/gcloud/dns/ZoneInfo.java | 35 +- .../com/google/gcloud/spi/DefaultDnsRpc.java | 2 +- .../com/google/gcloud/dns/ZoneInfoTest.java | 44 +- .../java/com/google/gcloud/dns/ZoneTest.java | 608 +++++------------- 6 files changed, 223 insertions(+), 674 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 1227a9e3b323..af0868ec17d6 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 @@ -23,7 +23,6 @@ import com.google.gcloud.spi.DnsRpc; import java.io.Serializable; -import java.math.BigInteger; import java.util.Set; /** @@ -69,8 +68,7 @@ static String selector(ProjectField... fields) { * The fields of a zone. * *

These values can be used to specify the fields to include in a partial response when calling - * {@link Dns#getZone(BigInteger, ZoneOption...)} or {@link Dns#getZone(String, ZoneOption...)}. - * The ID is always returned, even if not specified. + * {@link Dns#getZone(String, ZoneOption...)}. The ID is always returned, even if not specified. */ enum ZoneField { CREATION_TIME("creationTime"), @@ -105,9 +103,8 @@ static String selector(ZoneField... fields) { * The fields of a DNS record. * *

These values can be used to specify the fields to include in a partial response when calling - * {@link Dns#listDnsRecords(BigInteger, DnsRecordListOption...)} or {@link - * Dns#listDnsRecords(String, DnsRecordListOption...)}. The name is always returned even if not - * selected. + * {@link Dns#listDnsRecords(String, DnsRecordListOption...)}. The name is always returned even if + * not selected. */ enum DnsRecordField { DNS_RECORDS("rrdatas"), @@ -139,8 +136,7 @@ static String selector(DnsRecordField... 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(BigInteger, ChangeRequest, ChangeRequestOption...)} or {@link - * Dns#applyChangeRequest(String, ChangeRequest, ChangeRequestOption...)} The ID is always + * {@link Dns#applyChangeRequest(String, ChangeRequest, ChangeRequestOption...)} The ID is always * returned even if not selected. */ enum ChangeRequestField { @@ -444,16 +440,6 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) { */ ZoneInfo getZone(String zoneName, ZoneOption... options); - /** - * Returns the zone by the specified zone id. Returns {@code null} if the zone is not found. The - * returned fields can be optionally restricted by specifying {@link ZoneOption}s. - * - * @throws DnsException upon failure - * @see Cloud DNS Managed Zones: - * get - */ - ZoneInfo getZone(BigInteger zoneId, ZoneOption... options); - /** * Lists the zones inside the project. * @@ -479,17 +465,6 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) { */ boolean delete(String zoneName); // delete does not admit any options - /** - * Deletes an existing zone identified by id. Returns {@code true} if the zone was successfully - * deleted and {@code false} otherwise. - * - * @return {@code true} if zone was found and deleted and {@code false} otherwise - * @throws DnsException upon failure - * @see Cloud DNS Managed Zones: - * delete - */ - boolean delete(BigInteger zoneId); // delete does not admit any options - /** * Lists the DNS records in the zone identified by name. * @@ -502,18 +477,6 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) { */ Page listDnsRecords(String zoneName, DnsRecordListOption... options); - /** - * Lists the DNS records in the zone identified by ID. - * - *

The fields to be returned, page size and page tokens can be specified using {@link - * DnsRecordListOption}s. - * - * @throws DnsException upon failure or if the zone cannot be found - * @see Cloud DNS - * ResourceRecordSets: list - */ - Page listDnsRecords(BigInteger zoneId, DnsRecordListOption... options); - /** * Retrieves the information about the current project. The returned fields can be optionally * restricted by specifying {@link ProjectOption}s. @@ -523,18 +486,6 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) { */ ProjectInfo getProjectInfo(ProjectOption... fields); - /** - * Submits a change request for the specified zone. The returned object contains the following - * read-only fields supplied by the server: id, start time and status. time, id, and list of name - * servers. The fields to be returned can be selected by {@link ChangeRequestOption}s. - * - * @return the new {@link ChangeRequest} or {@code null} if zone is not found - * @throws DnsException upon failure - * @see Cloud DNS Changes: create - */ - ChangeRequest applyChangeRequest(BigInteger zoneId, ChangeRequest changeRequest, - ChangeRequestOption... options); - /** * Submits a change request for the specified zone. The returned object contains the following * read-only fields supplied by the server: id, start time and status. time, id, and list of name @@ -547,18 +498,6 @@ ChangeRequest applyChangeRequest(BigInteger zoneId, ChangeRequest changeRequest, ChangeRequest applyChangeRequest(String zoneName, ChangeRequest changeRequest, ChangeRequestOption... options); - /** - * Retrieves updated information about a change request previously submitted for a zone identified - * by ID. Returns {@code null} if the request cannot be found and throws an exception if the zone - * does not exist. The fields to be returned using can be specified using {@link - * ChangeRequestOption}s. - * - * @throws DnsException upon failure or if the zone cannot be found - * @see Cloud DNS Chages: get - */ - ChangeRequest getChangeRequest(BigInteger zoneId, String changeRequestId, - ChangeRequestOption... options); - /** * Retrieves updated information about a change request previously submitted for a zone identified * by ID. Returns {@code null} if the request cannot be found and throws an exception if the zone @@ -571,18 +510,6 @@ ChangeRequest getChangeRequest(BigInteger zoneId, String changeRequestId, ChangeRequest getChangeRequest(String zoneName, String changeRequestId, ChangeRequestOption... options); - /** - * Lists the change requests for the zone identified by ID that were submitted to the service. - * - *

The sorting order for changes (based on when they were received by the server), fields to be - * returned, page size and page token can be specified using {@link ChangeRequestListOption}s. - * - * @return A page of change requests - * @throws DnsException upon failure or if the zone cannot be found - * @see Cloud DNS Chages: list - */ - Page listChangeRequests(BigInteger zoneId, ChangeRequestListOption... options); - /** * Lists the change requests for the zone identified by name that were submitted to the service. * 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 86fc86bc3688..04edf332115d 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 @@ -16,13 +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.gcloud.Page; import java.io.Serializable; -import java.math.BigInteger; /** * A Google Cloud DNS Zone object. @@ -39,7 +37,7 @@ public class Zone implements Serializable { // TODO(mderka) Zone and zoneInfo to be merged. Opened issue #605. - private static final long serialVersionUID = -4012581571095484813L; + private static final long serialVersionUID = 6847890192129375500L; private final ZoneInfo zoneInfo; private final Dns dns; @@ -68,166 +66,81 @@ public static Zone get(Dns dnsService, String zoneName, Dns.ZoneOption... option } /** - * Constructs a {@code Zone} object that contains information received from the Google Cloud DNS - * service for the provided {@code zoneId}. - * - * @param zoneId ID of the zone to be searched for - * @param options optional restriction on what fields should be returned by the service - * @return zone object containing zone's information or {@code null} if not not found - * @throws DnsException upon failure - */ - public static Zone get(Dns dnsService, BigInteger zoneId, Dns.ZoneOption... options) { - checkNotNull(zoneId); - checkNotNull(dnsService); - ZoneInfo zoneInfo = dnsService.getZone(zoneId, options); - return zoneInfo == null ? null : new Zone(dnsService, zoneInfo); - } - - /** - * Retrieves the latest information about the zone. The method first attempts to retrieve the zone - * by ID and if not set or zone is not found, it searches by name. + * Retrieves the latest information about the zone. The method retrieves the zone by name which + * must always be initialized. * * @param options optional restriction on what fields should be fetched * @return zone object containing updated information or {@code null} if not not found * @throws DnsException upon failure - * @throws NullPointerException if both zone ID and name are not initialized */ public Zone reload(Dns.ZoneOption... options) { - checkNameOrIdNotNull(); - Zone zone = null; - if (zoneInfo.id() != null) { - zone = Zone.get(dns, zoneInfo.id(), options); - } - if (zone == null && zoneInfo.name() != null) { - // zone was not found by id or id is not set at all - zone = Zone.get(dns, zoneInfo.name(), options); - } - return zone; + return Zone.get(dns, zoneInfo.name(), options); } /** - * Deletes the zone. The method first attempts to delete the zone by ID. If the zone is not found - * or id is not set, it attempts to delete by name. + * Deletes the zone. The method first deletes the zone by name which must always be initialized. * * @return {@code true} is zone was found and deleted and {@code false} otherwise * @throws DnsException upon failure - * @throws NullPointerException if both zone ID and name are not initialized */ public boolean delete() { - checkNameOrIdNotNull(); - boolean deleted = false; - if (zoneInfo.id() != null) { - deleted = dns.delete(zoneInfo.id()); - } - if (!deleted && zoneInfo.name() != null) { - // zone was not found by id or id is not set at all - deleted = dns.delete(zoneInfo.name()); - } - return deleted; + return dns.delete(zoneInfo.name()); } /** - * Lists all {@link DnsRecord}s associated with this zone. First searches for zone by ID and if - * not found then by name. + * Lists all {@link DnsRecord}s associated with this zone. The method searches for zone by name + * which must always be initialized. * * @param options optional restriction on listing and fields of {@link DnsRecord}s returned * @return a page of DNS records * @throws DnsException upon failure or if the zone is not found - * @throws NullPointerException if both zone ID and name are not initialized */ public Page listDnsRecords(Dns.DnsRecordListOption... options) { - checkNameOrIdNotNull(); - Page page = null; - if (zoneInfo.id() != null) { - page = dns.listDnsRecords(zoneInfo.id(), options); - } - if (page == null && zoneInfo.name() != null) { - // zone was not found by id or id is not set at all - page = dns.listDnsRecords(zoneInfo.name(), options); - } - return page; + return dns.listDnsRecords(zoneInfo.name(), options); } /** - * Submits {@link ChangeRequest} to the service for it to applied to this zone. First searches for - * the zone by ID and if not found then by name. Returns a {@link ChangeRequest} with - * server-assigned ID or {@code null} if the zone was not found. + * Submits {@link ChangeRequest} to the service for it to applied to this zone. The method + * searches for zone by name which must always be initialized. * * @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 - * @throws NullPointerException if both zone ID and name are not initialized */ public ChangeRequest applyChangeRequest(ChangeRequest changeRequest, Dns.ChangeRequestOption... options) { - checkNameOrIdNotNull(); checkNotNull(changeRequest); - ChangeRequest updated = null; - if (zoneInfo.id() != null) { - updated = dns.applyChangeRequest(zoneInfo.id(), changeRequest, options); - } - if (updated == null && zoneInfo.name() != null) { - // zone was not found by id or id is not set at all - updated = dns.applyChangeRequest(zoneInfo.name(), changeRequest, options); - } - return updated; + return dns.applyChangeRequest(zoneInfo.name(), changeRequest, options); } /** * Retrieves an updated information about a change request previously submitted to be applied to - * this zone. First searches for the zone by ID and if not found then by name. Returns a {@link - * ChangeRequest} if found and {@code null} is the zone or the change request was not found. + * this zone. The method searches for zone by name which must always be initialized. Returns a + * {@link ChangeRequest} if and {@code null} if the change request was not found. Throws {@link + * DnsException} if the zone is not found. * * @param options optional restriction on what fields of {@link ChangeRequest} should be returned * @return updated ChangeRequest * @throws DnsException upon failure or if the zone is not found - * @throws NullPointerException if both zone ID and name are not initialized * @throws NullPointerException if the change request does not have initialized id */ public ChangeRequest getChangeRequest(String changeRequestId, Dns.ChangeRequestOption... options) { - checkNameOrIdNotNull(); checkNotNull(changeRequestId); - ChangeRequest updated = null; - if (zoneInfo.id() != null) { - updated = dns.getChangeRequest(zoneInfo.id(), changeRequestId, options); - } - if (updated == null && zoneInfo.name() != null) { - // zone was not found by id or id is not set at all - updated = dns.getChangeRequest(zoneInfo.name(), changeRequestId, options); - } - return updated; + return dns.getChangeRequest(zoneInfo.name(), changeRequestId, options); } /** - * Retrieves all change requests for this zone. First searches for the zone by ID and if not found - * then by name. Returns a page of {@link ChangeRequest}s or {@code null} if the zone is not - * found. + * Retrieves all change requests for this zone. The method searches for zone by name which must + * always be initialized. Returns a page of {@link ChangeRequest}s. Throws a {@link DnsException} + * if the zone is not found. * * @param options optional restriction on listing and fields to be returned * @return a page of change requests * @throws DnsException upon failure or if the zone is not found - * @throws NullPointerException if both zone ID and name are not initialized */ public Page listChangeRequests(Dns.ChangeRequestListOption... options) { - checkNameOrIdNotNull(); - Page changeRequests = null; - if (zoneInfo.id() != null) { - changeRequests = dns.listChangeRequests(zoneInfo.id(), options); - } - if (changeRequests == null && zoneInfo.name() != null) { - // zone was not found by id or id is not set at all - changeRequests = dns.listChangeRequests(zoneInfo.name(), options); - } - return changeRequests; - } - - /** - * Check that at least one of name and ID are initialized and throw and exception if not. - */ - private void checkNameOrIdNotNull() { - checkArgument(zoneInfo != null && (zoneInfo.id() != null || zoneInfo.name() != null), - "Both zoneInfo.id and zoneInfo.name are null. This is should never happen."); + return dns.listChangeRequests(zoneInfo.name(), options); } /** 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 524309eaa8e9..09945fb72138 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 @@ -41,7 +41,7 @@ public class ZoneInfo implements Serializable { private static final long serialVersionUID = 201601191647L; private final String name; - private final BigInteger id; + private final String id; private final Long creationTimeMillis; private final String dnsName; private final String description; @@ -53,7 +53,7 @@ public class ZoneInfo implements Serializable { */ public static class Builder { private String name; - private BigInteger id; + private String id; private Long creationTimeMillis; private String dnsName; private String description; @@ -66,19 +66,10 @@ public static class Builder { private Builder() { } - private Builder(BigInteger id) { - this.id = checkNotNull(id); - } - private Builder(String name) { this.name = checkNotNull(name); } - private Builder(String name, BigInteger id) { - this.name = checkNotNull(name); - this.id = checkNotNull(id); - } - /** * Creates a builder from an existing ZoneInfo object. */ @@ -103,7 +94,7 @@ public Builder name(String name) { /** * Sets an id for the zone which is assigned to the zone by the server. */ - Builder id(BigInteger id) { + Builder id(String id) { this.id = id; return this; } @@ -178,20 +169,6 @@ public static Builder builder(String name) { return new Builder(name); } - /** - * Returns a builder for {@code ZoneInfo} with an assigned {@code id}. - */ - public static Builder builder(BigInteger id) { - return new Builder(id); - } - - /** - * Returns a builder for {@code ZoneInfo} with an assigned {@code name} and {@code id}. - */ - public static Builder builder(String name, BigInteger id) { - return new Builder(name, id); - } - /** * Returns the user-defined name of the zone. */ @@ -202,7 +179,7 @@ public String name() { /** * Returns the read-only zone id assigned by the server. */ - public BigInteger id() { + public String id() { return id; } @@ -255,7 +232,7 @@ com.google.api.services.dns.model.ManagedZone toPb() { pb.setDescription(this.description()); pb.setDnsName(this.dnsName()); if (this.id() != null) { - pb.setId(this.id()); + pb.setId(new BigInteger(this.id())); } pb.setName(this.name()); pb.setNameServers(this.nameServers()); @@ -277,7 +254,7 @@ static ZoneInfo fromPb(com.google.api.services.dns.model.ManagedZone pb) { builder.dnsName(pb.getDnsName()); } if (pb.getId() != null) { - builder.id(pb.getId()); + builder.id(pb.getId().toString()); } if (pb.getName() != null) { builder.name(pb.getName()); diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DefaultDnsRpc.java b/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DefaultDnsRpc.java index 73e6ab4036ec..12596da02bf6 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DefaultDnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DefaultDnsRpc.java @@ -2,9 +2,9 @@ import static com.google.gcloud.spi.DnsRpc.ListResult.of; import static com.google.gcloud.spi.DnsRpc.Option.DNS_NAME; -import static com.google.gcloud.spi.DnsRpc.Option.NAME; import static com.google.gcloud.spi.DnsRpc.Option.DNS_TYPE; import static com.google.gcloud.spi.DnsRpc.Option.FIELDS; +import static com.google.gcloud.spi.DnsRpc.Option.NAME; import static com.google.gcloud.spi.DnsRpc.Option.PAGE_SIZE; import static com.google.gcloud.spi.DnsRpc.Option.PAGE_TOKEN; import static com.google.gcloud.spi.DnsRpc.Option.SORTING_ORDER; diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ZoneInfoTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ZoneInfoTest.java index 2c9fea8f7bde..227916b46f96 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ZoneInfoTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ZoneInfoTest.java @@ -26,14 +26,13 @@ import org.junit.Test; -import java.math.BigInteger; import java.util.LinkedList; import java.util.List; public class ZoneInfoTest { private static final String NAME = "mz-example.com"; - private static final BigInteger ID = BigInteger.valueOf(123L); + private static final String ID = "123456"; 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"; @@ -42,8 +41,9 @@ public class ZoneInfoTest { private static final String NS2 = "name server 2"; private static final String NS3 = "name server 3"; private static final List NAME_SERVERS = ImmutableList.of(NS1, NS2, NS3); - private static final ZoneInfo INFO = ZoneInfo.builder(NAME, ID) + private static final ZoneInfo INFO = ZoneInfo.builder(NAME) .creationTimeMillis(CREATION_TIME_MILLIS) + .id(ID) .dnsName(DNS_NAME) .description(DESCRIPTION) .nameServerSet(NAME_SERVER_SET) @@ -52,30 +52,14 @@ public class ZoneInfoTest { @Test public void testDefaultBuilders() { - ZoneInfo withName = ZoneInfo.builder(NAME).build(); - assertTrue(withName.nameServers().isEmpty()); - assertEquals(NAME, withName.name()); - assertNull(withName.id()); - assertNull(withName.creationTimeMillis()); - assertNull(withName.nameServerSet()); - assertNull(withName.description()); - assertNull(withName.dnsName()); - ZoneInfo withId = ZoneInfo.builder(ID).build(); - assertTrue(withId.nameServers().isEmpty()); - assertEquals(ID, withId.id()); - assertNull(withId.name()); - assertNull(withId.creationTimeMillis()); - assertNull(withId.nameServerSet()); - assertNull(withId.description()); - assertNull(withId.dnsName()); - ZoneInfo withBoth = ZoneInfo.builder(NAME, ID).build(); - assertTrue(withBoth.nameServers().isEmpty()); - assertEquals(ID, withBoth.id()); - assertEquals(NAME, withBoth.name()); - assertNull(withBoth.creationTimeMillis()); - assertNull(withBoth.nameServerSet()); - assertNull(withBoth.description()); - assertNull(withBoth.dnsName()); + ZoneInfo zone = ZoneInfo.builder(NAME).build(); + assertTrue(zone.nameServers().isEmpty()); + assertEquals(NAME, zone.name()); + assertNull(zone.id()); + assertNull(zone.creationTimeMillis()); + assertNull(zone.nameServerSet()); + assertNull(zone.description()); + assertNull(zone.dnsName()); } @Test @@ -109,7 +93,7 @@ public void testEqualsAndNotEquals() { assertNotEquals(INFO, clone); clone = INFO.toBuilder().dnsName(differentName).build(); assertNotEquals(INFO, clone); - clone = INFO.toBuilder().id(INFO.id().add(BigInteger.ONE)).build(); + clone = INFO.toBuilder().id(INFO.id() + "1111").build(); assertNotEquals(INFO, clone); clone = INFO.toBuilder().nameServerSet(INFO.nameServerSet() + "salt").build(); assertNotEquals(INFO, clone); @@ -127,7 +111,7 @@ public void testToBuilder() { assertEquals(INFO, INFO.toBuilder().build()); ZoneInfo partial = ZoneInfo.builder(NAME).build(); assertEquals(partial, partial.toBuilder().build()); - partial = ZoneInfo.builder(ID).build(); + partial = ZoneInfo.builder(NAME).id(ID).build(); assertEquals(partial, partial.toBuilder().build()); partial = ZoneInfo.builder(NAME).description(DESCRIPTION).build(); assertEquals(partial, partial.toBuilder().build()); @@ -148,7 +132,7 @@ public void testToAndFromPb() { assertEquals(INFO, ZoneInfo.fromPb(INFO.toPb())); ZoneInfo partial = ZoneInfo.builder(NAME).build(); assertEquals(partial, ZoneInfo.fromPb(partial.toPb())); - partial = ZoneInfo.builder(ID).build(); + partial = ZoneInfo.builder(NAME).id(ID).build(); assertEquals(partial, ZoneInfo.fromPb(partial.toPb())); partial = ZoneInfo.builder(NAME).description(DESCRIPTION).build(); assertEquals(partial, ZoneInfo.fromPb(partial.toPb())); 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 c746140ce599..59cb642167ed 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,7 +22,6 @@ 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; @@ -35,24 +34,19 @@ 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 BigInteger ZONE_ID = new BigInteger("123"); - private static final ZoneInfo ZONE_INFO = ZoneInfo.builder(ZONE_NAME, ZONE_ID) + private static final String ZONE_ID = "123"; + private static final ZoneInfo ZONE_INFO = ZoneInfo.builder(ZONE_NAME) + .id(ZONE_ID) .dnsName("example.com") .creationTimeMillis(123478946464L) .build(); private static final ZoneInfo NO_ID_INFO = ZoneInfo.builder(ZONE_NAME) - .dnsName("anoter-example.com") + .dnsName("another-example.com") .creationTimeMillis(893123464L) .build(); - private static final ZoneInfo NO_NAME_INFO = ZoneInfo.builder(ZONE_ID) - .dnsName("one-more-example.com") - .creationTimeMillis(875221546464L) - .build(); private static final Dns.ZoneOption ZONE_FIELD_OPTIONS = Dns.ZoneOption.fields(Dns.ZoneField.CREATION_TIME); private static final Dns.DnsRecordListOption DNS_RECORD_OPTIONS = @@ -65,10 +59,10 @@ public class ZoneTest { 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 DnsException EXCEPTION = createStrictMock(DnsException.class); private Dns dns; private Zone zone; - private Zone zoneNoName; private Zone zoneNoId; @Before @@ -76,7 +70,6 @@ public void setUp() throws Exception { dns = createStrictMock(Dns.class); zone = new Zone(dns, ZONE_INFO); zoneNoId = new Zone(dns, NO_ID_INFO); - zoneNoName = new Zone(dns, NO_NAME_INFO); } @After @@ -93,40 +86,18 @@ public void testConstructor() { assertEquals(dns, zone.dns()); } - @Test - public void testGetById() { - expect(dns.getZone(ZONE_ID)).andReturn(ZONE_INFO); - expect(dns.getZone(ZONE_ID, ZONE_FIELD_OPTIONS)).andReturn(ZONE_INFO); // for options - replay(dns); - Zone retrieved = Zone.get(dns, ZONE_ID); - assertSame(dns, retrieved.dns()); - assertEquals(ZONE_INFO, retrieved.info()); - try { - Zone.get(dns, (BigInteger) null); - fail("Cannot get by null id."); - } catch (NullPointerException e) { - // expected - } - try { - Zone.get(null, new BigInteger("12")); - fail("Cannot get anything from null service."); - } catch (NullPointerException e) { - // expected - } - // test passing options - Zone.get(dns, ZONE_ID, ZONE_FIELD_OPTIONS); - } - @Test public void testGetByName() { expect(dns.getZone(ZONE_NAME)).andReturn(ZONE_INFO); - expect(dns.getZone(ZONE_ID, ZONE_FIELD_OPTIONS)).andReturn(ZONE_INFO); // for options + expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(ZONE_INFO); // for options replay(dns); Zone retrieved = Zone.get(dns, ZONE_NAME); assertSame(dns, retrieved.dns()); assertEquals(ZONE_INFO, retrieved.info()); + // test passing options + Zone.get(dns, ZONE_NAME, ZONE_FIELD_OPTIONS); try { - Zone.get(dns, (String) null); + Zone.get(dns, null); fail("Cannot get by null name."); } catch (NullPointerException e) { // expected @@ -137,311 +108,177 @@ public void testGetByName() { } catch (NullPointerException e) { // expected } - // test passing options - Zone.get(dns, ZONE_ID, ZONE_FIELD_OPTIONS); } @Test - public void deleteByIdAndFound() { - expect(dns.delete(ZONE_ID)).andReturn(true); - replay(dns); - boolean result = zone.delete(); - assertTrue(result); - } - - @Test - public void deleteByIdAndNotFoundAndNameSetAndFound() { - expect(dns.delete(ZONE_ID)).andReturn(false); + public void deleteByNameAndFound() { + expect(dns.delete(ZONE_NAME)).andReturn(true); expect(dns.delete(ZONE_NAME)).andReturn(true); replay(dns); boolean result = zone.delete(); assertTrue(result); - } - - @Test - public void deleteByIdAndNotFoundAndNameSetAndNotFound() { - expect(dns.delete(ZONE_ID)).andReturn(false); - expect(dns.delete(ZONE_NAME)).andReturn(false); - replay(dns); - boolean result = zone.delete(); - assertFalse(result); - } - - @Test - public void deleteByIdAndNotFoundAndNameNotSet() { - expect(dns.delete(ZONE_ID)).andReturn(false); - replay(dns); - boolean result = zoneNoName.delete(); - assertFalse(result); - } - - @Test - public void deleteByNameAndFound() { - expect(dns.delete(ZONE_NAME)).andReturn(true); - replay(dns); - boolean result = zoneNoId.delete(); + result = zoneNoId.delete(); assertTrue(result); } @Test public void deleteByNameAndNotFound() { - expect(dns.delete(ZONE_NAME)).andReturn(true); + expect(dns.delete(ZONE_NAME)).andReturn(false); + expect(dns.delete(ZONE_NAME)).andReturn(false); replay(dns); boolean result = zoneNoId.delete(); - assertTrue(result); - } - - @Test - public void listDnsRecordsByIdAndFound() { - @SuppressWarnings("unchecked") - Page pageMock = createStrictMock(Page.class); - replay(pageMock); - expect(dns.listDnsRecords(ZONE_ID)).andReturn(pageMock); - // again for options - expect(dns.listDnsRecords(ZONE_ID, DNS_RECORD_OPTIONS)).andReturn(pageMock); - replay(dns); - Page result = zone.listDnsRecords(); - assertSame(pageMock, result); - verify(pageMock); - // verify options - zone.listDnsRecords(DNS_RECORD_OPTIONS); + assertFalse(result); + result = zone.delete(); + assertFalse(result); } @Test - public void listDnsRecordsByIdAndNotFoundAndNameSetAndFound() { + public void listDnsRecordsByNameAndFound() { @SuppressWarnings("unchecked") Page pageMock = createStrictMock(Page.class); replay(pageMock); - expect(dns.listDnsRecords(ZONE_ID)).andReturn(null); + expect(dns.listDnsRecords(ZONE_NAME)).andReturn(pageMock); expect(dns.listDnsRecords(ZONE_NAME)).andReturn(pageMock); // again for options - expect(dns.listDnsRecords(ZONE_ID, DNS_RECORD_OPTIONS)).andReturn(null); + expect(dns.listDnsRecords(ZONE_NAME, DNS_RECORD_OPTIONS)).andReturn(pageMock); expect(dns.listDnsRecords(ZONE_NAME, DNS_RECORD_OPTIONS)).andReturn(pageMock); replay(dns); Page result = zone.listDnsRecords(); assertSame(pageMock, result); - verify(pageMock); - // verify options - zone.listDnsRecords(DNS_RECORD_OPTIONS); - } - - @Test - public void listDnsRecordsByIdAndNotFoundAndNameSetAndNotFound() { - expect(dns.listDnsRecords(ZONE_ID)).andReturn(null); - expect(dns.listDnsRecords(ZONE_NAME)).andReturn(null); - // again for options - expect(dns.listDnsRecords(ZONE_ID, DNS_RECORD_OPTIONS)).andReturn(null); - expect(dns.listDnsRecords(ZONE_NAME, DNS_RECORD_OPTIONS)).andReturn(null); - replay(dns); - Page result = zone.listDnsRecords(); - assertNull(result); - // check options - zone.listDnsRecords(DNS_RECORD_OPTIONS); - } - - @Test - public void listDnsRecordsByIdAndNotFoundAndNameNotSet() { - expect(dns.listDnsRecords(ZONE_ID)).andReturn(null); - expect(dns.listDnsRecords(ZONE_ID, DNS_RECORD_OPTIONS)).andReturn(null); // for options - replay(dns); - Page result = zoneNoName.listDnsRecords(); - assertNull(result); - zoneNoName.listDnsRecords(DNS_RECORD_OPTIONS); // check options - } - - @Test - public void listDnsRecordsByNameAndFound() { - @SuppressWarnings("unchecked") - Page pageMock = createStrictMock(Page.class); - replay(pageMock); - expect(dns.listDnsRecords(ZONE_NAME)).andReturn(pageMock); - // again for options - expect(dns.listDnsRecords(ZONE_NAME, DNS_RECORD_OPTIONS)).andReturn(pageMock); - replay(dns); - Page result = zoneNoId.listDnsRecords(); + result = zoneNoId.listDnsRecords(); assertSame(pageMock, result); verify(pageMock); + zone.listDnsRecords(DNS_RECORD_OPTIONS); // check options zoneNoId.listDnsRecords(DNS_RECORD_OPTIONS); // check options } @Test public void listDnsRecordsByNameAndNotFound() { - expect(dns.listDnsRecords(ZONE_NAME)).andReturn(null); - // again for options - expect(dns.listDnsRecords(ZONE_NAME, DNS_RECORD_OPTIONS)).andReturn(null); - replay(dns); - Page result = zoneNoId.listDnsRecords(); - assertNull(result); - zoneNoId.listDnsRecords(DNS_RECORD_OPTIONS); // check options - } - - @Test - public void reloadByIdAndFound() { - expect(dns.getZone(ZONE_ID)).andReturn(zone.info()); - expect(dns.getZone(ZONE_ID, ZONE_FIELD_OPTIONS)).andReturn(zone.info()); // for options - replay(dns); - Zone result = zone.reload(); - assertSame(zone.dns(), result.dns()); - assertEquals(zone.info(), result.info()); - zone.reload(ZONE_FIELD_OPTIONS); // for options - } - - @Test - public void reloadByIdAndNotFoundAndNameSetAndFound() { - expect(dns.getZone(ZONE_ID)).andReturn(null); - expect(dns.getZone(ZONE_NAME)).andReturn(zone.info()); - // again for options - expect(dns.getZone(ZONE_ID, ZONE_FIELD_OPTIONS)).andReturn(null); - expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(zone.info()); - replay(dns); - Zone result = zone.reload(); - assertSame(zone.dns(), result.dns()); - assertEquals(zone.info(), result.info()); - zone.reload(ZONE_FIELD_OPTIONS); // for options - } - - @Test - public void reloadByIdAndNotFoundAndNameSetAndNotFound() { - expect(dns.getZone(ZONE_ID)).andReturn(null); - expect(dns.getZone(ZONE_NAME)).andReturn(null); - // again with options - expect(dns.getZone(ZONE_ID, ZONE_FIELD_OPTIONS)).andReturn(null); - expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(null); - replay(dns); - Zone result = zone.reload(); - assertNull(result); + expect(dns.listDnsRecords(ZONE_NAME)).andThrow(EXCEPTION); + expect(dns.listDnsRecords(ZONE_NAME)).andThrow(EXCEPTION); // again for options - zone.reload(ZONE_FIELD_OPTIONS); - } - - @Test - public void reloadByIdAndNotFoundAndNameNotSet() { - expect(dns.getZone(ZONE_ID)).andReturn(null); - expect(dns.getZone(ZONE_ID, ZONE_FIELD_OPTIONS)).andReturn(null); // for options + expect(dns.listDnsRecords(ZONE_NAME, DNS_RECORD_OPTIONS)).andThrow(EXCEPTION); + expect(dns.listDnsRecords(ZONE_NAME, DNS_RECORD_OPTIONS)).andThrow(EXCEPTION); replay(dns); - Zone result = zoneNoName.reload(); - assertNull(result); - zoneNoName.reload(ZONE_FIELD_OPTIONS); // for options + try { + zoneNoId.listDnsRecords(); + fail("Parent container not found, should throw an exception."); + } catch (DnsException e) { + // expected + } + try { + zone.listDnsRecords(); + fail("Parent container not found, should throw an exception."); + } catch (DnsException e) { + // expected + } + try { + zoneNoId.listDnsRecords(DNS_RECORD_OPTIONS); // check options + fail("Parent container not found, should throw an exception."); + } catch (DnsException e) { + // expected + } + try { + zone.listDnsRecords(DNS_RECORD_OPTIONS); // check options + fail("Parent container not found, should throw an exception."); + } catch (DnsException e) { + // expected + } } @Test public void reloadByNameAndFound() { expect(dns.getZone(ZONE_NAME)).andReturn(zoneNoId.info()); + expect(dns.getZone(ZONE_NAME)).andReturn(zone.info()); // 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()); replay(dns); Zone result = zoneNoId.reload(); assertSame(zoneNoId.dns(), result.dns()); assertEquals(zoneNoId.info(), result.info()); + result = zone.reload(); + assertSame(zone.dns(), result.dns()); + assertEquals(zone.info(), result.info()); zoneNoId.reload(ZONE_FIELD_OPTIONS); // check options + zone.reload(ZONE_FIELD_OPTIONS); // check options } @Test public void reloadByNameAndNotFound() { + expect(dns.getZone(ZONE_NAME)).andReturn(null); expect(dns.getZone(ZONE_NAME)).andReturn(null); // again for options expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(null); + expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(null); replay(dns); Zone result = zoneNoId.reload(); assertNull(result); + result = zone.reload(); + assertNull(result); zoneNoId.reload(ZONE_FIELD_OPTIONS); // for options + zone.reload(ZONE_FIELD_OPTIONS); // for options } @Test - public void applyChangeByIdAndFound() { - expect(dns.applyChangeRequest(ZONE_ID, CHANGE_REQUEST)).andReturn(CHANGE_REQUEST_AFTER); - // again for options - expect(dns.applyChangeRequest(ZONE_ID, CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS)) + public void applyChangeByNameAndFound() { + expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST)) .andReturn(CHANGE_REQUEST_AFTER); - replay(dns); - ChangeRequest result = zone.applyChangeRequest(CHANGE_REQUEST); - assertNotEquals(CHANGE_REQUEST, result); - assertEquals(CHANGE_REQUEST_AFTER, result); - // for options - result = zone.applyChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS); - assertNotEquals(CHANGE_REQUEST, result); - assertEquals(CHANGE_REQUEST_AFTER, result); - } - - @Test - public void applyChangeByIdAndNotFoundAndNameSetAndFound() { - expect(dns.applyChangeRequest(ZONE_ID, CHANGE_REQUEST)).andReturn(null); expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST)) .andReturn(CHANGE_REQUEST_AFTER); // again for options - expect(dns.applyChangeRequest(ZONE_ID, CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(null); expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS)) .andReturn(CHANGE_REQUEST_AFTER); - replay(dns); - ChangeRequest result = zone.applyChangeRequest(CHANGE_REQUEST); - assertNotEquals(CHANGE_REQUEST, result); - assertEquals(CHANGE_REQUEST_AFTER, result); - // for options - result = zone.applyChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS); - assertNotEquals(CHANGE_REQUEST, result); - assertEquals(CHANGE_REQUEST_AFTER, result); - } - - @Test - public void applyChangeIdAndNotFoundAndNameSetAndNotFound() { - expect(dns.applyChangeRequest(ZONE_ID, CHANGE_REQUEST)).andReturn(null); - expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST)).andReturn(null); - // again with options - expect(dns.applyChangeRequest(ZONE_ID, CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(null); - expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(null); - replay(dns); - ChangeRequest result = zone.applyChangeRequest(CHANGE_REQUEST); - assertNull(result); - // again for options - result = zone.applyChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS); - assertNull(result); - } - - @Test - public void applyChangeRequestByIdAndNotFoundAndNameNotSet() { - expect(dns.applyChangeRequest(ZONE_ID, CHANGE_REQUEST)).andReturn(null); - expect(dns.applyChangeRequest(ZONE_ID, CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(null); // for options - replay(dns); - ChangeRequest result = zoneNoName.applyChangeRequest(CHANGE_REQUEST); - assertNull(result); - // again for options - result = zoneNoName.applyChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS); - assertNull(result); - } - - @Test - public void applyChangeByNameAndFound() { - // ID is not set - expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST)) - .andReturn(CHANGE_REQUEST_AFTER); - // again for options expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS)) .andReturn(CHANGE_REQUEST_AFTER); replay(dns); ChangeRequest result = zoneNoId.applyChangeRequest(CHANGE_REQUEST); assertEquals(CHANGE_REQUEST_AFTER, result); + result = zone.applyChangeRequest(CHANGE_REQUEST); + assertEquals(CHANGE_REQUEST_AFTER, result); // check options result = zoneNoId.applyChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS); assertEquals(CHANGE_REQUEST_AFTER, result); + result = zone.applyChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS); + assertEquals(CHANGE_REQUEST_AFTER, result); } @Test public void applyChangeByNameAndNotFound() { // ID is not set - expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST)).andReturn(null); + expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST)).andThrow(EXCEPTION); + expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST)).andThrow(EXCEPTION); // again for options expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(null); + .andThrow(EXCEPTION); + expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS)) + .andThrow(EXCEPTION); replay(dns); - ChangeRequest result = zoneNoId.applyChangeRequest(CHANGE_REQUEST); - assertNull(result); + try { + zoneNoId.applyChangeRequest(CHANGE_REQUEST); + fail("Parent container not found, should throw an exception."); + } catch (DnsException e) { + // expected + } + try { + zone.applyChangeRequest(CHANGE_REQUEST); + fail("Parent container not found, should throw an exception."); + } catch (DnsException e) { + // expected + } // check options - result = zoneNoId.applyChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS); - assertNull(result); + try { + zoneNoId.applyChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS); + fail("Parent container not found, should throw an exception."); + } catch (DnsException e) { + // expected + } + try { + zone.applyChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS); + fail("Parent container not found, should throw an exception."); + } catch (DnsException e) { + // expected + } } @Test @@ -471,116 +308,82 @@ public void applyNullChangeRequest() { } catch (NullPointerException e) { // expected } - try { - zoneNoName.applyChangeRequest(null); - fail("Cannot apply null ChangeRequest."); - } catch (NullPointerException e) { - // expected - } - try { - zoneNoName.applyChangeRequest(null, CHANGE_REQUEST_FIELD_OPTIONS); - fail("Cannot apply null ChangeRequest."); - } catch (NullPointerException e) { - // expected - } } @Test - public void getChangeByIdAndFound() { - expect(dns.getChangeRequest(ZONE_ID, CHANGE_REQUEST.id())).andReturn(CHANGE_REQUEST_AFTER); - // again for options - expect(dns.getChangeRequest(ZONE_ID, CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)) + public void getChangeAndZoneFoundByName() { + expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())) .andReturn(CHANGE_REQUEST_AFTER); - replay(dns); - ChangeRequest result = zone.getChangeRequest(CHANGE_REQUEST.id()); - assertNotEquals(CHANGE_REQUEST, result); - assertEquals(CHANGE_REQUEST_AFTER, result); - // for options - result = zone.getChangeRequest(CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS); - assertNotEquals(CHANGE_REQUEST, result); - assertEquals(CHANGE_REQUEST_AFTER, result); - // test no id - } - - @Test - public void getChangeByIdAndNotFoundAndNameSetAndFound() { - expect(dns.getChangeRequest(ZONE_ID, CHANGE_REQUEST.id())).andReturn(null); expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())) .andReturn(CHANGE_REQUEST_AFTER); // again for options - expect(dns.getChangeRequest(ZONE_ID, CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(null); + 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); replay(dns); - ChangeRequest result = zone.getChangeRequest(CHANGE_REQUEST.id()); - assertNotEquals(CHANGE_REQUEST, result); + ChangeRequest result = zoneNoId.getChangeRequest(CHANGE_REQUEST.id()); assertEquals(CHANGE_REQUEST_AFTER, result); - // for options - result = zone.getChangeRequest(CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS); - assertNotEquals(CHANGE_REQUEST, result); + result = zone.getChangeRequest(CHANGE_REQUEST.id()); + assertEquals(CHANGE_REQUEST_AFTER, result); + // check options + result = zoneNoId.getChangeRequest(CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS); assertEquals(CHANGE_REQUEST_AFTER, result); - } - - @Test - public void getChangeIdAndNotFoundAndNameSetAndNotFound() { - expect(dns.getChangeRequest(ZONE_ID, CHANGE_REQUEST.id())).andReturn(null); - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())).andReturn(null); - // again with options - expect(dns.getChangeRequest(ZONE_ID, CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(null); - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(null); - replay(dns); - ChangeRequest result = zone.getChangeRequest(CHANGE_REQUEST.id()); - assertNull(result); - // again for options result = zone.getChangeRequest(CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS); - assertNull(result); - } - - @Test - public void getChangeRequestByIdAndNotFoundAndNameNotSet() { - expect(dns.getChangeRequest(ZONE_ID, CHANGE_REQUEST.id())).andReturn(null); - expect(dns.getChangeRequest(ZONE_ID, CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(null); // for options - replay(dns); - ChangeRequest result = zoneNoName.getChangeRequest(CHANGE_REQUEST.id()); - assertNull(result); - // again for options - result = zoneNoName.getChangeRequest(CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS); - assertNull(result); + assertEquals(CHANGE_REQUEST_AFTER, result); } @Test - public void getChangeByNameAndFound() { - // ID is not set - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())) - .andReturn(CHANGE_REQUEST_AFTER); + public void getChangeAndZoneNotFoundByName() { + expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())).andThrow(EXCEPTION); + expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())).andThrow(EXCEPTION); // again for options expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(CHANGE_REQUEST_AFTER); + .andThrow(EXCEPTION); + expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)) + .andThrow(EXCEPTION); replay(dns); - ChangeRequest result = zoneNoId.getChangeRequest(CHANGE_REQUEST.id()); - assertEquals(CHANGE_REQUEST_AFTER, result); + try { + ChangeRequest result = 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()); + fail("Parent container not found, should throw an exception."); + } catch (DnsException e) { + // expected + } // check options - result = zoneNoId.getChangeRequest(CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS); - assertEquals(CHANGE_REQUEST_AFTER, result); + try { + zoneNoId.getChangeRequest(CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS); + fail("Parent container not found, should throw an exception."); + } catch (DnsException e) { + // expected + } + try { + zone.getChangeRequest(CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS); + fail("Parent container not found, should throw an exception."); + } catch (DnsException e) { + // expected + } } @Test - public void getChangeByNameAndNotFound() { - // ID is not set + public void getChangedWhichDoesNotExistZoneFound() { + expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())).andReturn(null); expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())).andReturn(null); // 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); replay(dns); - ChangeRequest result = zoneNoId.getChangeRequest(CHANGE_REQUEST.id()); - assertNull(result); - // check options - result = zoneNoId.getChangeRequest(CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS); - assertNull(result); + assertNull(zoneNoId.getChangeRequest(CHANGE_REQUEST.id())); + assertNull(zone.getChangeRequest(CHANGE_REQUEST.id())); + assertNull(zoneNoId.getChangeRequest(CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)); + assertNull(zone.getChangeRequest(CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)); } @Test @@ -610,18 +413,6 @@ public void getNullChangeRequest() { } catch (NullPointerException e) { // expected } - try { - zoneNoName.getChangeRequest(null); - fail("Cannot get null ChangeRequest."); - } catch (NullPointerException e) { - // expected - } - try { - zoneNoName.getChangeRequest(null, CHANGE_REQUEST_FIELD_OPTIONS); - fail("Cannot get null ChangeRequest."); - } catch (NullPointerException e) { - // expected - } } @Test @@ -651,104 +442,61 @@ public void getChangeRequestWithNoId() { } catch (NullPointerException e) { // expected } - try { - zoneNoName.getChangeRequest(CHANGE_REQUEST_NO_ID.id()); - fail("Cannot get ChangeRequest by null id."); - } catch (NullPointerException e) { - // expected - } - try { - zoneNoName.getChangeRequest(CHANGE_REQUEST_NO_ID.id(), CHANGE_REQUEST_FIELD_OPTIONS); - fail("Cannot get ChangeRequest by null id."); - } catch (NullPointerException e) { - // expected - } - } - - @Test - public void listChangeRequestsByIdAndFound() { - @SuppressWarnings("unchecked") - Page pageMock = createStrictMock(Page.class); - replay(pageMock); - expect(dns.listChangeRequests(ZONE_ID)).andReturn(pageMock); - // again for options - expect(dns.listChangeRequests(ZONE_ID, CHANGE_REQUEST_LIST_OPTIONS)).andReturn(pageMock); - replay(dns); - Page result = zone.listChangeRequests(); - assertSame(pageMock, result); - verify(pageMock); - // verify options - zone.listChangeRequests(CHANGE_REQUEST_LIST_OPTIONS); } @Test - public void listChangeRequestsByIdAndNotFoundAndNameSetAndFound() { + public void listChangeRequestsAndZoneFound() { @SuppressWarnings("unchecked") Page pageMock = createStrictMock(Page.class); replay(pageMock); - expect(dns.listChangeRequests(ZONE_ID)).andReturn(null); + expect(dns.listChangeRequests(ZONE_NAME)).andReturn(pageMock); expect(dns.listChangeRequests(ZONE_NAME)).andReturn(pageMock); // again for options - expect(dns.listChangeRequests(ZONE_ID, CHANGE_REQUEST_LIST_OPTIONS)).andReturn(null); expect(dns.listChangeRequests(ZONE_NAME, CHANGE_REQUEST_LIST_OPTIONS)) .andReturn(pageMock); - replay(dns); - Page result = zone.listChangeRequests(); - assertSame(pageMock, result); - verify(pageMock); - // verify options - zone.listChangeRequests(CHANGE_REQUEST_LIST_OPTIONS); - } - - @Test - public void listChangeRequestsByIdAndNotFoundAndNameSetAndNotFound() { - expect(dns.listChangeRequests(ZONE_ID)).andReturn(null); - expect(dns.listChangeRequests(ZONE_NAME)).andReturn(null); - // again for options - expect(dns.listChangeRequests(ZONE_ID, CHANGE_REQUEST_LIST_OPTIONS)).andReturn(null); - expect(dns.listChangeRequests(ZONE_NAME, CHANGE_REQUEST_LIST_OPTIONS)).andReturn(null); - replay(dns); - Page result = zone.listChangeRequests(); - assertNull(result); - // check options - zone.listChangeRequests(CHANGE_REQUEST_LIST_OPTIONS); - } - - @Test - public void listChangeRequestsByIdAndNotFoundAndNameNotSet() { - expect(dns.listChangeRequests(ZONE_ID)).andReturn(null); - // again for options - expect(dns.listChangeRequests(ZONE_ID, CHANGE_REQUEST_LIST_OPTIONS)).andReturn(null); - replay(dns); - Page result = zoneNoName.listChangeRequests(); - assertNull(result); - zoneNoName.listChangeRequests(CHANGE_REQUEST_LIST_OPTIONS); // check options - } - - @Test - public void listChangeRequestsByNameAndFound() { - @SuppressWarnings("unchecked") - Page pageMock = createStrictMock(Page.class); - replay(pageMock); - expect(dns.listChangeRequests(ZONE_NAME)).andReturn(pageMock); - // again for options expect(dns.listChangeRequests(ZONE_NAME, CHANGE_REQUEST_LIST_OPTIONS)) .andReturn(pageMock); replay(dns); Page result = zoneNoId.listChangeRequests(); assertSame(pageMock, result); + result = zone.listChangeRequests(); + assertSame(pageMock, result); verify(pageMock); zoneNoId.listChangeRequests(CHANGE_REQUEST_LIST_OPTIONS); // check options + zone.listChangeRequests(CHANGE_REQUEST_LIST_OPTIONS); // check options } @Test - public void listChangeRequestsByNameAndNotFound() { - expect(dns.listChangeRequests(ZONE_NAME)).andReturn(null); + public void listChangeRequestsAndZoneNotFound() { + expect(dns.listChangeRequests(ZONE_NAME)).andThrow(EXCEPTION); + expect(dns.listChangeRequests(ZONE_NAME)).andThrow(EXCEPTION); // again for options - expect(dns.listChangeRequests(ZONE_NAME, CHANGE_REQUEST_LIST_OPTIONS)).andReturn(null); + expect(dns.listChangeRequests(ZONE_NAME, CHANGE_REQUEST_LIST_OPTIONS)).andThrow(EXCEPTION); + expect(dns.listChangeRequests(ZONE_NAME, CHANGE_REQUEST_LIST_OPTIONS)).andThrow(EXCEPTION); replay(dns); - Page result = zoneNoId.listChangeRequests(); - assertNull(result); - zoneNoId.listChangeRequests(CHANGE_REQUEST_LIST_OPTIONS); // check options + try { + zoneNoId.listChangeRequests(); + fail("Parent container not found, should throw an exception."); + } catch (DnsException e) { + // expected + } + try { + zone.listChangeRequests(); + fail("Parent container not found, should throw an exception."); + } catch (DnsException e) { + // expected + } + try { + zoneNoId.listChangeRequests(CHANGE_REQUEST_LIST_OPTIONS); // check options + fail("Parent container not found, should throw an exception."); + } catch (DnsException e) { + // expected + } + try { + zone.listChangeRequests(CHANGE_REQUEST_LIST_OPTIONS); // check options + fail("Parent container not found, should throw an exception."); + } catch (DnsException e) { + // expected + } } } From 3183f4a7445dcb5e8992132ec5463bbced65c07b Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Thu, 4 Feb 2016 14:04:45 -0800 Subject: [PATCH 2/4] Added field options for zone create method. --- .../src/main/java/com/google/gcloud/dns/Dns.java | 7 ++++--- .../src/main/java/com/google/gcloud/spi/DefaultDnsRpc.java | 7 +++++-- .../src/main/java/com/google/gcloud/spi/DnsRpc.java | 3 ++- 3 files changed, 11 insertions(+), 6 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 af0868ec17d6..f6649c28680c 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 @@ -421,14 +421,15 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) { * *

Returns {@link ZoneInfo} object representing the new zone's information. In addition to the * name, dns name and description (supplied by the user within the {@code zoneInfo} parameter), - * the returned object will include the following read-only fields supplied by the server: - * creation time, id, and list of name servers. + * the returned object can include the following read-only fields supplied by the server: creation + * time, id, and list of name servers. The returned fields can be optionally restricted by + * specifying {@link ZoneOption}s. * * @throws DnsException upon failure * @see Cloud DNS Managed Zones: * create */ - ZoneInfo create(ZoneInfo zoneInfo); + ZoneInfo create(ZoneInfo zoneInfo, ZoneOption... options); /** * Returns the zone by the specified zone name. Returns {@code null} if the zone is not found. The diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DefaultDnsRpc.java b/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DefaultDnsRpc.java index 12596da02bf6..6ed9c7e0f216 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DefaultDnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DefaultDnsRpc.java @@ -54,9 +54,12 @@ public DefaultDnsRpc(DnsOptions options) { } @Override - public ManagedZone create(ManagedZone zone) throws DnsException { + public ManagedZone create(ManagedZone zone, Map options) throws DnsException { try { - return dns.managedZones().create(this.options.projectId(), zone).execute(); + return dns.managedZones() + .create(this.options.projectId(), zone) + .setFields(FIELDS.getString(options)) + .execute(); } catch (IOException ex) { throw translate(ex); } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsRpc.java b/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsRpc.java index c3cd3c690177..addb69d24b40 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsRpc.java @@ -87,10 +87,11 @@ public String pageToken() { * Creates a new zone. * * @param zone a zone to be created + * @param options a map of options for the service call * @return Updated {@code ManagedZone} object * @throws DnsException upon failure */ - ManagedZone create(ManagedZone zone) throws DnsException; + ManagedZone create(ManagedZone zone, Map options) throws DnsException; /** * Retrieves and returns an existing zone. From 4057dcd93d2dee27c0a7f51e51723ca414daa5fd Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Thu, 4 Feb 2016 18:19:27 -0800 Subject: [PATCH 3/4] Fixed doc after refactoring. Made zone name mandatory in fields. --- .../src/main/java/com/google/gcloud/dns/Dns.java | 4 ++-- .../src/main/java/com/google/gcloud/dns/Zone.java | 15 ++++++--------- .../main/java/com/google/gcloud/dns/ZoneInfo.java | 11 +---------- .../test/java/com/google/gcloud/dns/DnsTest.java | 2 +- 4 files changed, 10 insertions(+), 22 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 f6649c28680c..040a97e5b253 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 @@ -68,7 +68,7 @@ static String selector(ProjectField... fields) { * The fields of a zone. * *

These values can be used to specify the fields to include in a partial response when calling - * {@link Dns#getZone(String, ZoneOption...)}. The ID is always returned, even if not specified. + * {@link Dns#getZone(String, ZoneOption...)}. The name is always returned, even if not specified. */ enum ZoneField { CREATION_TIME("creationTime"), @@ -91,7 +91,7 @@ String selector() { static String selector(ZoneField... fields) { Set fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1); - fieldStrings.add(ZONE_ID.selector()); + fieldStrings.add(NAME.selector()); for (ZoneField field : fields) { fieldStrings.add(field.selector()); } 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 04edf332115d..f5e0a8b4a63b 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 @@ -78,7 +78,7 @@ public Zone reload(Dns.ZoneOption... options) { } /** - * Deletes the zone. The method first deletes the zone by name which must always be initialized. + * Deletes the zone. The method deletes the zone by name. * * @return {@code true} is zone was found and deleted and {@code false} otherwise * @throws DnsException upon failure @@ -88,8 +88,7 @@ public boolean delete() { } /** - * Lists all {@link DnsRecord}s associated with this zone. The method searches for zone by name - * which must always be initialized. + * Lists all {@link DnsRecord}s associated with this zone. The method searches for zone by name. * * @param options optional restriction on listing and fields of {@link DnsRecord}s returned * @return a page of DNS records @@ -115,14 +114,13 @@ public ChangeRequest applyChangeRequest(ChangeRequest changeRequest, /** * Retrieves an updated information about a change request previously submitted to be applied to - * this zone. The method searches for zone by name which must always be initialized. Returns a - * {@link ChangeRequest} if and {@code null} if the change request was not found. Throws {@link - * DnsException} if the zone is not found. + * this zone. Returns a {@link ChangeRequest} or {@code null} if the change request was not + * found. Throws {@link DnsException} if the zone is not found. * * @param options optional restriction on what fields of {@link ChangeRequest} should be returned * @return updated ChangeRequest * @throws DnsException upon failure or if the zone is not found - * @throws NullPointerException if the change request does not have initialized id + * @throws NullPointerException if {@code changeRequestId} is null */ public ChangeRequest getChangeRequest(String changeRequestId, Dns.ChangeRequestOption... options) { @@ -132,8 +130,7 @@ public ChangeRequest getChangeRequest(String changeRequestId, /** * Retrieves all change requests for this zone. The method searches for zone by name which must - * always be initialized. Returns a page of {@link ChangeRequest}s. Throws a {@link DnsException} - * if the zone is not found. + * always be initialized. Returns a page of {@link ChangeRequest}s. * * @param options optional restriction on listing and fields to be returned * @return a page of change requests 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 09945fb72138..a15518ae166f 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 @@ -60,12 +60,6 @@ public static class Builder { private String nameServerSet; private List nameServers = new LinkedList<>(); - /** - * Returns an empty builder for {@code ZoneInfo}. We use it internally in {@code toPb()}. - */ - private Builder() { - } - private Builder(String name) { this.name = checkNotNull(name); } @@ -246,7 +240,7 @@ com.google.api.services.dns.model.ManagedZone toPb() { } static ZoneInfo fromPb(com.google.api.services.dns.model.ManagedZone pb) { - Builder builder = new Builder(); + Builder builder = new Builder(pb.getName()); if (pb.getDescription() != null) { builder.description(pb.getDescription()); } @@ -256,9 +250,6 @@ static ZoneInfo fromPb(com.google.api.services.dns.model.ManagedZone pb) { if (pb.getId() != null) { builder.id(pb.getId().toString()); } - if (pb.getName() != null) { - builder.name(pb.getName()); - } if (pb.getNameServers() != null) { builder.nameServers(pb.getNameServers()); } diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsTest.java index c2be251cea9e..74faca329884 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsTest.java @@ -80,7 +80,7 @@ public void testZoneList() { 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())); + assertTrue(((String) fields.value()).contains(Dns.ZoneField.NAME.selector())); // page token Dns.ZoneListOption option = Dns.ZoneListOption.pageToken(PAGE_TOKEN); assertEquals(PAGE_TOKEN, option.value()); From d7226350faa112b2f2e5310e7268ba61176729ee Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Fri, 5 Feb 2016 08:45:53 -0800 Subject: [PATCH 4/4] Javadoc fixed. --- .../src/main/java/com/google/gcloud/dns/Zone.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) 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 f5e0a8b4a63b..3f4ea8cab3e4 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 @@ -66,8 +66,7 @@ public static Zone get(Dns dnsService, String zoneName, Dns.ZoneOption... option } /** - * Retrieves the latest information about the zone. The method retrieves the zone by name which - * must always be initialized. + * Retrieves the latest information about the zone. The method retrieves the zone by name. * * @param options optional restriction on what fields should be fetched * @return zone object containing updated information or {@code null} if not not found @@ -100,7 +99,7 @@ public Page listDnsRecords(Dns.DnsRecordListOption... options) { /** * Submits {@link ChangeRequest} to the service for it to applied to this zone. The method - * searches for zone by name which must always be initialized. + * searches for zone by name. * * @param options optional restriction on what fields of {@link ChangeRequest} should be returned * @return ChangeRequest with server-assigned ID @@ -114,8 +113,8 @@ public ChangeRequest applyChangeRequest(ChangeRequest changeRequest, /** * Retrieves an updated information about a change request previously submitted to be applied to - * this zone. Returns a {@link ChangeRequest} or {@code null} if the change request was not - * found. Throws {@link DnsException} if the zone is not found. + * this zone. Returns a {@link ChangeRequest} or {@code null} if the change request was not found. + * Throws {@link DnsException} if the zone is not found. * * @param options optional restriction on what fields of {@link ChangeRequest} should be returned * @return updated ChangeRequest @@ -129,8 +128,8 @@ public ChangeRequest getChangeRequest(String changeRequestId, } /** - * Retrieves all change requests for this zone. The method searches for zone by name which must - * always be initialized. Returns a page of {@link ChangeRequest}s. + * Retrieves all change requests for this zone. The method searches for zone by name. Returns a + * page of {@link ChangeRequest}s. * * @param options optional restriction on listing and fields to be returned * @return a page of change requests