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 28e79104cf58..b48e4b0a90f2 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 @@ -18,14 +18,14 @@ import com.google.common.base.Joiner; import com.google.common.collect.Sets; +import com.google.gcloud.Page; import com.google.gcloud.Service; import com.google.gcloud.spi.DnsRpc; import java.io.Serializable; +import java.math.BigInteger; import java.util.Set; -import static com.google.gcloud.dns.Dns.ZoneField.selector; - /** * An interface for the Google Cloud DNS service. * @@ -285,7 +285,7 @@ class ZoneListOption extends AbstractOption implements Serializable { */ public static ZoneListOption fields(ZoneField... fields) { StringBuilder builder = new StringBuilder(); - builder.append("managedZones(").append(selector(fields)).append(')'); + builder.append("managedZones(").append(ZoneField.selector(fields)).append(')'); return new ZoneListOption(DnsRpc.Option.FIELDS, builder.toString()); } @@ -420,5 +420,191 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) { } } - // TODO(mderka) Add methods. Created issue #596. + /** + * Creates a new zone. + * + * @return ZoneInfo object representing the new zone's metadata. 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. + * @throws DnsException upon failure + * @see Cloud DNS Managed Zones: + * create + */ + ZoneInfo create(ZoneInfo zoneInfo); + + /** + * Retrieves the zone by the specified zone name. Returns {@code null} is the zone is not found. + * The returned fields can be optionally restricted by specifying {@code ZoneFieldOptions}. + * + * @throws DnsException upon failure + * @see Cloud DNS Managed Zones: + * get + */ + ZoneInfo getZone(String zoneName, ZoneOption... options); + + /** + * Retrieves the zone by the specified zone name. Returns {@code null} is the zone is not found. + * The returned fields can be optionally restricted by specifying {@code ZoneFieldOptions}. + * + * @throws DnsException upon failure + * @see Cloud DNS Managed Zones: + * get + */ + ZoneInfo getZone(BigInteger zoneId, ZoneOption... options); + + /** + * Lists the zoned inside the project. + * + *

This method returns zone in an unspecified order. New zones do not necessarily appear at the + * end of the list. Use {@link ZoneListOption} to restrict the listing to a domain name, set page + * size, and set page tokens. + * + * @return {@code Page}, a page of zones + * @throws DnsException upon failure + * @see Cloud DNS Managed Zones: + * list + */ + Page listZones(ZoneListOption... options); + + /** + * Deletes an existing zone identified by name. Returns true if the zone was successfully deleted + * and false otherwise. + * + * @return {@code true} if zone was found and deleted and false otherwise + * @throws DnsException upon failure + * @see Cloud DNS Managed Zones: + * delete + */ + boolean delete(String zoneName); // delete does not admit any options + + /** + * Deletes an existing zone identified by id. Returns true if the zone was successfully deleted + * and false otherwise. + * + * @return {@code true} if zone was found and deleted and 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. + * + *

The fields to be returned, page size and page tokens can be specified using {@code + * DnsRecordOptions}. Returns null if the zone cannot be found. + * + * @throws DnsException upon failure + * @see Cloud DNS + * ResourceRecordSets: list + */ + 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 {@code + * DnsRecordOptions}. Returns null if the zone cannot be found. + * + * @throws DnsException upon failure + * @see Cloud DNS + * ResourceRecordSets: list + */ + Page listDnsRecords(BigInteger zoneId, DnsRecordListOption... options); + + /** + * Retrieves the metadata about the current project. The returned fields can be optionally + * restricted by specifying {@code ProjectOptions}. + * + * @throws DnsException upon failure + * @see Cloud DNS Projects: get + */ + ProjectInfo getProjectInfo(ProjectGetOption... fields); + + /** + * Returns the current project id. + */ + String getProjectId(); + + /** + * Returns the current project number. + */ + BigInteger getProjectNumber(); + + /** + * Submits a change requests for applying to the zone identified by ID to the service. 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 returned fields can be modified by {@code + * ChangeRequestFieldOptions}. Returns null if the zone is not found. + * + * @return ChangeRequest object representing the new change request or null if zone is not found + * @throws DnsException upon failure + * @see Cloud DNS Changes: create + */ + ChangeRequest applyChangeRequest(ChangeRequest changeRequest, BigInteger zoneId, + ChangeRequestOption... options); + + /** + * Submits a change requests for applying to the zone identified by name to the service. 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 returned fields can be modified by {@code + * ChangeRequestFieldOptions}. Returns null if the zone is not found. + * + * @return ChangeRequest object representing the new change request or null if zone is not found + * @throws DnsException upon failure + * @see Cloud DNS Changes: create + */ + ChangeRequest applyChangeRequest(ChangeRequest changeRequest, String zoneName, + ChangeRequestOption... options); + + /** + * Retrieves updated information about a change request previously submitted for a zone identified + * by ID. Returns null if the zone or request cannot be found. + * + *

The fields to be returned using {@code ChangeRequestFieldOptions}. + * + * @throws DnsException upon failure + * @see Cloud DNS Chages: get + */ + ChangeRequest getChangeRequest(ChangeRequest changeRequest, BigInteger zoneId, + ChangeRequestOption... options); + + /** + * Retrieves updated information about a change request previously submitted for a zone identified + * by name. Returns null if the zone or request cannot be found. + * + *

The fields to be returned using {@code ChangeRequestFieldOptions}. + * + * @throws DnsException upon failure + * @see Cloud DNS Chages: get + */ + ChangeRequest getChangeRequest(ChangeRequest changeRequest, String zoneName, + ChangeRequestOption... options); + + /** + * Lists the change requests for the zone identified by ID that were submitted to the service. + * + *

The sorting key for changes, fields to be returned, page and page tokens can be specified + * using {@code ChangeRequestListOptions}. Note that the only sorting key currently supported is + * the timestamp of submitting the change request to the service. + * + * @return {@code Page}, a page of change requests + * @throws DnsException upon failure + * @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. + * + *

The sorting key for changes, fields to be returned, page and page tokens can be specified + * using {@code ChangeRequestListOptions}. Note that the only sorting key currently supported is + * the timestamp of submitting the change request to the service. + * + * @return {@code Page}, a page of change requests + * @throws DnsException upon failure + * @see Cloud DNS Chages: list + */ + Page listChangeRequests(String zoneName, ChangeRequestListOption... options); } 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 new file mode 100644 index 000000000000..f4d9702ba12f --- /dev/null +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Zone.java @@ -0,0 +1,256 @@ +/* + * 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 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. + * + *

A zone is the container for all of your DNS records that share the same DNS name prefix, for + * example, example.com. Zones are automatically assigned a set of name servers when they are + * created to handle responding to DNS queries for that zone. A zone has quotas for the number of + * resource records that it can include. + * + * @see Google Cloud DNS managed zone + * documentation + */ +public class Zone implements Serializable { + + // TODO(mderka) Zone and zoneInfo to be merged. Opened issue #605. + + private static final long serialVersionUID = -4012581571095484813L; + private final ZoneInfo zoneInfo; + private final Dns dns; + + /** + * Constructs a {@code Zone} object that contains the given {@code zoneInfo}. + */ + public Zone(Dns dns, ZoneInfo zoneInfo) { + this.zoneInfo = checkNotNull(zoneInfo); + this.dns = checkNotNull(dns); + } + + /** + * Constructs a {@code Zone} object that contains meta information received from the Google Cloud + * DNS service for the provided zoneName. + * + * @param zoneName Name of the zone to be searched for + * @param options Optional restriction on what fields should be returned by the service + * @return Zone object containing metadata or null if not not found + * @throws DnsException upon failure + */ + public static Zone get(Dns dnsService, String zoneName, + Dns.ZoneOption... options) { + checkNotNull(zoneName); + checkNotNull(dnsService); + ZoneInfo zoneInfo = dnsService.getZone(zoneName, options); + return zoneInfo == null ? null : new Zone(dnsService, zoneInfo); + } + + /** + * Constructs a {@code Zone} object that contains meta information received from the Google Cloud + * DNS service for the provided zoneName. + * + * @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 metadata or 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. + * + * @param options Optional restriction on what fields should be fetched + * @return Zone object containing updated metadata or 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; + } + + /** + * 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. + * + * @return true is zone was found and deleted and 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; + } + + /** + * Lists all {@link DnsRecord}s associated with this zone. First searches for zone by ID and if + * not found then by name. + * + * @param options Optional restriction on listing and on what fields of {@link DnsRecord} should + * be returned by the service + * @return {@code Page}, a page of DNS records, or null if the zone is not found + * @throws DnsException upon failure + * @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; + } + + /** + * 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 null if the zone was not found. + * + * @param options Optional restriction on what fields of {@link ChangeRequest} should be returned + * by the service + * @return ChangeRequest with server-assigned ID or null if the zone was not found. + * @throws DnsException upon failure + * @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(changeRequest, zoneInfo.id(), options); + } + if (updated == null && zoneInfo.name() != null) { + // zone was not found by id or id is not set at all + updated = dns.applyChangeRequest(changeRequest, zoneInfo.name(), options); + } + return updated; + } + + /** + * 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 null is the zone or the change request was not found. + * + * @param options Optional restriction on what fields of {@link ChangeRequest} should be returned + * by the service + * @return ChangeRequest with updated information of null if the change request or zone was not + * found. + * @throws DnsException upon failure + * @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(ChangeRequest changeRequest, + Dns.ChangeRequestOption... options) { + checkNameOrIdNotNull(); + checkNotNull(changeRequest); + checkNotNull(changeRequest.id()); + ChangeRequest updated = null; + if (zoneInfo.id() != null) { + updated = dns.getChangeRequest(changeRequest, zoneInfo.id(), options); + } + if (updated == null && zoneInfo.name() != null) { + // zone was not found by id or id is not set at all + updated = dns.getChangeRequest(changeRequest, zoneInfo.name(), options); + } + return updated; + } + + /** + * 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 null if the zone is not found. + * + * @param options Optional restriction on listing and on what fields of {@link ChangeRequest}s + * should be returned by the service + * @return {@code Page}, a page of change requests, or null if the zone is not + * found + * @throws DnsException upon failure + * @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() { + if (zoneInfo != null && zoneInfo.id() == null && zoneInfo.name() == null) { + throw new NullPointerException("Both zoneInfo.id and zoneInfo.name are null. " + + "This is an inconsistent state which should never happen."); + } + } + + /** + * Returns the {@link ZoneInfo} object containing meta information about this managed zone. + */ + public ZoneInfo info() { + return this.zoneInfo; + } + + /** + * Returns the {@link Dns} service object associated with this managed zone. + */ + public Dns dns() { + return this.dns; + } + +} diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/AbstractOptionTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/AbstractOptionTest.java index e3f2896bd10b..09e35527879b 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/AbstractOptionTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/AbstractOptionTest.java @@ -16,14 +16,14 @@ package com.google.gcloud.dns; -import com.google.gcloud.spi.DnsRpc; - -import org.junit.Test; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.fail; +import com.google.gcloud.spi.DnsRpc; + +import org.junit.Test; + public class AbstractOptionTest { private static final DnsRpc.Option RPC_OPTION = DnsRpc.Option.DNS_TYPE; 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 new file mode 100644 index 000000000000..5f9d119e6150 --- /dev/null +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ZoneTest.java @@ -0,0 +1,750 @@ +/* + * 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.easymock.EasyMock.createStrictMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.google.gcloud.Page; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.math.BigInteger; + +public class ZoneTest { + + private static final String ZONE_NAME = "dns-zone-name"; + private static final BigInteger ZONE_ID = new BigInteger("123"); + private static final ZoneInfo ZONE_INFO = ZoneInfo.builder(ZONE_NAME, ZONE_ID) + .dnsName("example.com") + .creationTimeMillis(123478946464L) + .build(); + private static final ZoneInfo NO_ID_INFO = ZoneInfo.builder(ZONE_NAME) + .dnsName("anoter-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 = + Dns.DnsRecordListOption.dnsName("some-dns"); + private static final Dns.ChangeRequestOption CHANGE_REQUEST_FIELD_OPTIONS = + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME); + private static final Dns.ChangeRequestListOption CHANGE_REQUEST_LIST_OPTIONS = + Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.START_TIME); + private static final ChangeRequest CHANGE_REQUEST = ChangeRequest.builder().id("someid").build(); + 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 Dns dns; + private Zone zone; + private Zone zoneNoName; + private Zone zoneNoId; + + @Before + 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 + public void tearDown() throws Exception { + verify(dns); + } + + @Test + public void testConstructor() { + replay(dns); + assertNotNull(zone.info()); + assertEquals(ZONE_INFO, zone.info()); + assertNotNull(zone.dns()); + 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()); + BigInteger id = null; + try { + Zone.get(dns, id); + fail("Cannot get null zone."); + } catch (NullPointerException e) { + // expected + } + try { + Zone.get(null, id); + fail("Cannot get null zone."); + } 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 + replay(dns); + Zone retrieved = Zone.get(dns, ZONE_NAME); + assertSame(dns, retrieved.dns()); + assertEquals(ZONE_INFO, retrieved.info()); + String name = null; + try { + Zone.get(dns, name); + fail("Cannot get null zone."); + } catch (NullPointerException e) { + // expected + } + try { + Zone.get(null, name); + fail("Cannot get null zone."); + } 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); + 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(); + assertTrue(result); + } + + @Test + public void deleteByNameAndNotFound() { + expect(dns.delete(ZONE_NAME)).andReturn(true); + replay(dns); + boolean result = zoneNoId.delete(); + assertTrue(result); + } + + @Test + public void listDnsRecordsByIdAndFound() { + 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); + } + + @Test + public void listDnsRecordsByIdAndNotFoundAndNameSetAndFound() { + Page pageMock = createStrictMock(Page.class); + replay(pageMock); + expect(dns.listDnsRecords(ZONE_ID)).andReturn(null); + 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); + 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() { + 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(); + assertSame(pageMock, result); + verify(pageMock); + 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); + // 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 + replay(dns); + Zone result = zoneNoName.reload(); + assertNull(result); + zoneNoName.reload(ZONE_FIELD_OPTIONS); // for options + } + + @Test + public void reloadByNameAndFound() { + expect(dns.getZone(ZONE_NAME)).andReturn(zoneNoId.info()); + // again for options + expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(zoneNoId.info()); + replay(dns); + Zone result = zoneNoId.reload(); + assertSame(zoneNoId.dns(), result.dns()); + assertEquals(zoneNoId.info(), result.info()); + zoneNoId.reload(ZONE_FIELD_OPTIONS); // check options + } + + @Test + public void reloadByNameAndNotFound() { + expect(dns.getZone(ZONE_NAME)).andReturn(null); + // again for options + expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(null); + replay(dns); + Zone result = zoneNoId.reload(); + assertNull(result); + zoneNoId.reload(ZONE_FIELD_OPTIONS); // for options + } + + @Test + public void applyChangeByIdAndFound() { + expect(dns.applyChangeRequest(CHANGE_REQUEST, ZONE_ID)).andReturn(CHANGE_REQUEST_AFTER); + // again for options + expect(dns.applyChangeRequest(CHANGE_REQUEST, ZONE_ID, 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 applyChangeByIdAndNotFoundAndNameSetAndFound() { + expect(dns.applyChangeRequest(CHANGE_REQUEST, ZONE_ID)).andReturn(null); + expect(dns.applyChangeRequest(CHANGE_REQUEST, ZONE_NAME)) + .andReturn(CHANGE_REQUEST_AFTER); + // again for options + expect(dns.applyChangeRequest(CHANGE_REQUEST, ZONE_ID, CHANGE_REQUEST_FIELD_OPTIONS)) + .andReturn(null); + expect(dns.applyChangeRequest(CHANGE_REQUEST, ZONE_NAME, 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(CHANGE_REQUEST, ZONE_ID)).andReturn(null); + expect(dns.applyChangeRequest(CHANGE_REQUEST, ZONE_NAME)).andReturn(null); + // again with options + expect(dns.applyChangeRequest(CHANGE_REQUEST, ZONE_ID, CHANGE_REQUEST_FIELD_OPTIONS)) + .andReturn(null); + expect(dns.applyChangeRequest(CHANGE_REQUEST, ZONE_NAME, 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(CHANGE_REQUEST, ZONE_ID)).andReturn(null); + expect(dns.applyChangeRequest(CHANGE_REQUEST, ZONE_ID, 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(CHANGE_REQUEST, ZONE_NAME)) + .andReturn(CHANGE_REQUEST_AFTER); + // again for options + expect(dns.applyChangeRequest(CHANGE_REQUEST, ZONE_NAME, CHANGE_REQUEST_FIELD_OPTIONS)) + .andReturn(CHANGE_REQUEST_AFTER); + replay(dns); + ChangeRequest result = zoneNoId.applyChangeRequest(CHANGE_REQUEST); + assertEquals(CHANGE_REQUEST_AFTER, result); + // check options + result = zoneNoId.applyChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS); + assertEquals(CHANGE_REQUEST_AFTER, result); + } + + @Test + public void applyChangeByNameAndNotFound() { + // ID is not set + expect(dns.applyChangeRequest(CHANGE_REQUEST, ZONE_NAME)).andReturn(null); + // again for options + expect(dns.applyChangeRequest(CHANGE_REQUEST, ZONE_NAME, CHANGE_REQUEST_FIELD_OPTIONS)) + .andReturn(null); + replay(dns); + ChangeRequest result = zoneNoId.applyChangeRequest(CHANGE_REQUEST); + assertNull(result); + // check options + result = zoneNoId.applyChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS); + assertNull(result); + } + + @Test + public void applyNullChangeRequest() { + replay(dns); // no calls expected + try { + zone.applyChangeRequest(null); + fail("Cannot apply null ChangeRequest."); + } catch (NullPointerException e) { + // expected + } + try { + zone.applyChangeRequest(null, CHANGE_REQUEST_FIELD_OPTIONS); + fail("Cannot apply null ChangeRequest."); + } catch (NullPointerException e) { + // expected + } + try { + zoneNoId.applyChangeRequest(null); + fail("Cannot apply null ChangeRequest."); + } catch (NullPointerException e) { + // expected + } + try { + zoneNoId.applyChangeRequest(null, CHANGE_REQUEST_FIELD_OPTIONS); + fail("Cannot apply null ChangeRequest."); + } 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(CHANGE_REQUEST, ZONE_ID)).andReturn(CHANGE_REQUEST_AFTER); + // again for options + expect(dns.getChangeRequest(CHANGE_REQUEST, ZONE_ID, CHANGE_REQUEST_FIELD_OPTIONS)) + .andReturn(CHANGE_REQUEST_AFTER); + replay(dns); + ChangeRequest result = zone.getChangeRequest(CHANGE_REQUEST); + assertNotEquals(CHANGE_REQUEST, result); + assertEquals(CHANGE_REQUEST_AFTER, result); + // for options + result = zone.getChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS); + assertNotEquals(CHANGE_REQUEST, result); + assertEquals(CHANGE_REQUEST_AFTER, result); + // test no id + } + + @Test + public void getChangeByIdAndNotFoundAndNameSetAndFound() { + expect(dns.getChangeRequest(CHANGE_REQUEST, ZONE_ID)).andReturn(null); + expect(dns.getChangeRequest(CHANGE_REQUEST, ZONE_NAME)) + .andReturn(CHANGE_REQUEST_AFTER); + // again for options + expect(dns.getChangeRequest(CHANGE_REQUEST, ZONE_ID, CHANGE_REQUEST_FIELD_OPTIONS)) + .andReturn(null); + expect(dns.getChangeRequest(CHANGE_REQUEST, ZONE_NAME, CHANGE_REQUEST_FIELD_OPTIONS)) + .andReturn(CHANGE_REQUEST_AFTER); + replay(dns); + ChangeRequest result = zone.getChangeRequest(CHANGE_REQUEST); + assertNotEquals(CHANGE_REQUEST, result); + assertEquals(CHANGE_REQUEST_AFTER, result); + // for options + result = zone.getChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS); + assertNotEquals(CHANGE_REQUEST, result); + assertEquals(CHANGE_REQUEST_AFTER, result); + } + + @Test + public void getChangeIdAndNotFoundAndNameSetAndNotFound() { + expect(dns.getChangeRequest(CHANGE_REQUEST, ZONE_ID)).andReturn(null); + expect(dns.getChangeRequest(CHANGE_REQUEST, ZONE_NAME)).andReturn(null); + // again with options + expect(dns.getChangeRequest(CHANGE_REQUEST, ZONE_ID, CHANGE_REQUEST_FIELD_OPTIONS)) + .andReturn(null); + expect(dns.getChangeRequest(CHANGE_REQUEST, ZONE_NAME, CHANGE_REQUEST_FIELD_OPTIONS)) + .andReturn(null); + replay(dns); + ChangeRequest result = zone.getChangeRequest(CHANGE_REQUEST); + assertNull(result); + // again for options + result = zone.getChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS); + assertNull(result); + } + + @Test + public void getChangeRequestByIdAndNotFoundAndNameNotSet() { + expect(dns.getChangeRequest(CHANGE_REQUEST, ZONE_ID)).andReturn(null); + expect(dns.getChangeRequest(CHANGE_REQUEST, ZONE_ID, CHANGE_REQUEST_FIELD_OPTIONS)) + .andReturn(null); // for options + replay(dns); + ChangeRequest result = zoneNoName.getChangeRequest(CHANGE_REQUEST); + assertNull(result); + // again for options + result = zoneNoName.getChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS); + assertNull(result); + } + + @Test + public void getChangeByNameAndFound() { + // ID is not set + expect(dns.getChangeRequest(CHANGE_REQUEST, ZONE_NAME)) + .andReturn(CHANGE_REQUEST_AFTER); + // again for options + expect(dns.getChangeRequest(CHANGE_REQUEST, ZONE_NAME, CHANGE_REQUEST_FIELD_OPTIONS)) + .andReturn(CHANGE_REQUEST_AFTER); + replay(dns); + ChangeRequest result = zoneNoId.getChangeRequest(CHANGE_REQUEST); + assertEquals(CHANGE_REQUEST_AFTER, result); + // check options + result = zoneNoId.getChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS); + assertEquals(CHANGE_REQUEST_AFTER, result); + } + + @Test + public void getChangeByNameAndNotFound() { + // ID is not set + expect(dns.getChangeRequest(CHANGE_REQUEST, ZONE_NAME)).andReturn(null); + // again for options + expect(dns.getChangeRequest(CHANGE_REQUEST, ZONE_NAME, CHANGE_REQUEST_FIELD_OPTIONS)) + .andReturn(null); + replay(dns); + ChangeRequest result = zoneNoId.getChangeRequest(CHANGE_REQUEST); + assertNull(result); + // check options + result = zoneNoId.getChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS); + assertNull(result); + } + + @Test + public void getNullChangeRequest() { + replay(dns); // no calls expected + try { + zone.getChangeRequest(null); + fail("Cannot get null ChangeRequest."); + } catch (NullPointerException e) { + // expected + } + try { + zone.getChangeRequest(null, CHANGE_REQUEST_FIELD_OPTIONS); + fail("Cannot get null ChangeRequest."); + } catch (NullPointerException e) { + // expected + } + try { + zoneNoId.getChangeRequest(null); + fail("Cannot get null ChangeRequest."); + } catch (NullPointerException e) { + // expected + } + try { + zoneNoId.getChangeRequest(null, CHANGE_REQUEST_FIELD_OPTIONS); + fail("Cannot get null ChangeRequest."); + } 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 + public void getChangeRequestWithNoId() { + replay(dns); // no calls expected + try { + zone.getChangeRequest(CHANGE_REQUEST_NO_ID); + fail("Cannot get ChangeRequest with no id."); + } catch (NullPointerException e) { + // expected + } + try { + zone.getChangeRequest(CHANGE_REQUEST_NO_ID, CHANGE_REQUEST_FIELD_OPTIONS); + fail("Cannot get ChangeRequest with no id."); + } catch (NullPointerException e) { + // expected + } + try { + zoneNoId.getChangeRequest(CHANGE_REQUEST_NO_ID); + fail("Cannot get ChangeRequest with no id."); + } catch (NullPointerException e) { + // expected + } + try { + zoneNoId.getChangeRequest(CHANGE_REQUEST_NO_ID, CHANGE_REQUEST_FIELD_OPTIONS); + fail("Cannot get ChangeRequest with no id."); + } catch (NullPointerException e) { + // expected + } + try { + zoneNoName.getChangeRequest(CHANGE_REQUEST_NO_ID); + fail("Cannot get ChangeRequest with no id."); + } catch (NullPointerException e) { + // expected + } + try { + zoneNoName.getChangeRequest(CHANGE_REQUEST_NO_ID, CHANGE_REQUEST_FIELD_OPTIONS); + fail("Cannot get ChangeRequest with no id."); + } catch (NullPointerException e) { + // expected + } + } + + @Test + public void listChangeRequestsByIdAndFound() { + 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() { + Page pageMock = createStrictMock(Page.class); + replay(pageMock); + expect(dns.listChangeRequests(ZONE_ID)).andReturn(null); + 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() { + 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); + verify(pageMock); + zoneNoId.listChangeRequests(CHANGE_REQUEST_LIST_OPTIONS); // check options + } + + @Test + public void listChangeRequestsByNameAndNotFound() { + expect(dns.listChangeRequests(ZONE_NAME)).andReturn(null); + // again for options + expect(dns.listChangeRequests(ZONE_NAME, CHANGE_REQUEST_LIST_OPTIONS)).andReturn(null); + replay(dns); + Page result = zoneNoId.listChangeRequests(); + assertNull(result); + zoneNoId.listChangeRequests(CHANGE_REQUEST_LIST_OPTIONS); // check options + } +}