• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.cloud.dns;
18 
19 import static org.easymock.EasyMock.createStrictMock;
20 import static org.easymock.EasyMock.expect;
21 import static org.easymock.EasyMock.replay;
22 import static org.easymock.EasyMock.reset;
23 import static org.easymock.EasyMock.verify;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertSame;
30 import static org.junit.Assert.assertTrue;
31 import static org.junit.Assert.fail;
32 
33 import com.google.api.gax.paging.Page;
34 import com.google.common.collect.ImmutableList;
35 import java.math.BigInteger;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 
40 public class ZoneTest {
41 
42   private static final String ZONE_NAME = "dns-zone-name";
43   private static final String ZONE_ID = "123";
44   private static final ZoneInfo ZONE_INFO =
45       Zone.of(ZONE_NAME, "example.com", "description")
46           .toBuilder()
47           .setGeneratedId(ZONE_ID)
48           .setCreationTimeMillis(123478946464L)
49           .build();
50   private static final ZoneInfo NO_ID_INFO =
51       ZoneInfo.of(ZONE_NAME, "another-example.com", "description")
52           .toBuilder()
53           .setCreationTimeMillis(893123464L)
54           .build();
55   private static final Dns.ZoneOption ZONE_FIELD_OPTIONS =
56       Dns.ZoneOption.fields(Dns.ZoneField.CREATION_TIME);
57   private static final Dns.RecordSetListOption DNS_RECORD_OPTIONS =
58       Dns.RecordSetListOption.dnsName("some-dns");
59   private static final Dns.ChangeRequestOption CHANGE_REQUEST_FIELD_OPTIONS =
60       Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME);
61   private static final Dns.ChangeRequestListOption CHANGE_REQUEST_LIST_OPTIONS =
62       Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.START_TIME);
63   private static final ChangeRequestInfo CHANGE_REQUEST =
64       ChangeRequestInfo.newBuilder().setGeneratedId("someid").build();
65   private static final ChangeRequestInfo CHANGE_REQUEST_NO_ID =
66       ChangeRequestInfo.newBuilder().build();
67   private static final DnsException EXCEPTION = new DnsException(-1, "message", null);
68   private static final DnsOptions OPTIONS = createStrictMock(DnsOptions.class);
69 
70   private Dns dns;
71   private Zone zone;
72   private Zone zoneNoId;
73   private ChangeRequest changeRequestAfter;
74 
75   @Before
setUp()76   public void setUp() throws Exception {
77     dns = createStrictMock(Dns.class);
78     expect(dns.getOptions()).andReturn(OPTIONS).times(3);
79     replay(dns);
80     zone = new Zone(dns, new ZoneInfo.BuilderImpl(ZONE_INFO));
81     zoneNoId = new Zone(dns, new ZoneInfo.BuilderImpl(NO_ID_INFO));
82     changeRequestAfter =
83         new ChangeRequest(
84             dns,
85             ZONE_NAME,
86             new ChangeRequestInfo.BuilderImpl(
87                 CHANGE_REQUEST.toBuilder().setStartTime(123465L).build()));
88     reset(dns);
89   }
90 
91   @After
tearDown()92   public void tearDown() throws Exception {
93     verify(dns);
94   }
95 
96   @Test
testConstructor()97   public void testConstructor() {
98     replay(dns);
99     assertEquals(ZONE_INFO.toPb(), zone.toPb());
100     assertNotNull(zone.getDns());
101     assertEquals(dns, zone.getDns());
102   }
103 
104   @Test
deleteByNameAndFound()105   public void deleteByNameAndFound() {
106     expect(dns.delete(ZONE_NAME)).andReturn(true).times(2);
107     replay(dns);
108     boolean result = zone.delete();
109     assertTrue(result);
110     result = zoneNoId.delete();
111     assertTrue(result);
112   }
113 
114   @Test
deleteByNameAndNotFound()115   public void deleteByNameAndNotFound() {
116     expect(dns.delete(ZONE_NAME)).andReturn(false).times(2);
117     replay(dns);
118     boolean result = zoneNoId.delete();
119     assertFalse(result);
120     result = zone.delete();
121     assertFalse(result);
122   }
123 
124   @Test
listDnsRecordsByNameAndFound()125   public void listDnsRecordsByNameAndFound() {
126     @SuppressWarnings("unchecked")
127     Page<RecordSet> pageMock = createStrictMock(Page.class);
128     replay(pageMock);
129     expect(dns.listRecordSets(ZONE_NAME)).andReturn(pageMock).times(2);
130     // again for options
131     expect(dns.listRecordSets(ZONE_NAME, DNS_RECORD_OPTIONS)).andReturn(pageMock).times(2);
132     replay(dns);
133     Page<RecordSet> result = zone.listRecordSets();
134     assertSame(pageMock, result);
135     result = zoneNoId.listRecordSets();
136     assertSame(pageMock, result);
137     verify(pageMock);
138     zone.listRecordSets(DNS_RECORD_OPTIONS); // check options
139     zoneNoId.listRecordSets(DNS_RECORD_OPTIONS); // check options
140   }
141 
142   @Test
listDnsRecordsByNameAndNotFound()143   public void listDnsRecordsByNameAndNotFound() {
144     expect(dns.listRecordSets(ZONE_NAME)).andThrow(EXCEPTION).times(2);
145     // again for options
146     expect(dns.listRecordSets(ZONE_NAME, DNS_RECORD_OPTIONS)).andThrow(EXCEPTION).times(2);
147     replay(dns);
148     try {
149       zoneNoId.listRecordSets();
150       fail("Parent container not found, should throw an exception.");
151     } catch (DnsException e) {
152       // expected
153     }
154     try {
155       zone.listRecordSets();
156       fail("Parent container not found, should throw an exception.");
157     } catch (DnsException e) {
158       // expected
159     }
160     try {
161       zoneNoId.listRecordSets(DNS_RECORD_OPTIONS); // check options
162       fail("Parent container not found, should throw an exception.");
163     } catch (DnsException e) {
164       // expected
165     }
166     try {
167       zone.listRecordSets(DNS_RECORD_OPTIONS); // check options
168       fail("Parent container not found, should throw an exception.");
169     } catch (DnsException e) {
170       // expected
171     }
172   }
173 
174   @Test
reloadByNameAndFound()175   public void reloadByNameAndFound() {
176     expect(dns.getZone(ZONE_NAME)).andReturn(zone).times(2);
177     // again for options
178     expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(zoneNoId);
179     expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(zone);
180     replay(dns);
181     Zone result = zoneNoId.reload();
182     assertSame(zone.getDns(), result.getDns());
183     assertEquals(zone, result);
184     result = zone.reload();
185     assertSame(zone.getDns(), result.getDns());
186     assertEquals(zone, result);
187     zoneNoId.reload(ZONE_FIELD_OPTIONS); // check options
188     zone.reload(ZONE_FIELD_OPTIONS); // check options
189   }
190 
191   @Test
reloadByNameAndNotFound()192   public void reloadByNameAndNotFound() {
193     expect(dns.getZone(ZONE_NAME)).andReturn(null).times(2);
194     // again for options
195     expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(null).times(2);
196     replay(dns);
197     Zone result = zoneNoId.reload();
198     assertNull(result);
199     result = zone.reload();
200     assertNull(result);
201     zoneNoId.reload(ZONE_FIELD_OPTIONS); // for options
202     zone.reload(ZONE_FIELD_OPTIONS); // for options
203   }
204 
205   @Test
applyChangeByNameAndFound()206   public void applyChangeByNameAndFound() {
207     expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST)).andReturn(changeRequestAfter);
208     expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST)).andReturn(changeRequestAfter);
209     // again for options
210     expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS))
211         .andReturn(changeRequestAfter);
212     expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS))
213         .andReturn(changeRequestAfter);
214     replay(dns);
215     ChangeRequest result = zoneNoId.applyChangeRequest(CHANGE_REQUEST);
216     assertEquals(changeRequestAfter, result);
217     result = zone.applyChangeRequest(CHANGE_REQUEST);
218     assertEquals(changeRequestAfter, result);
219     // check options
220     result = zoneNoId.applyChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS);
221     assertEquals(changeRequestAfter, result);
222     result = zone.applyChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS);
223     assertEquals(changeRequestAfter, result);
224   }
225 
226   @Test
applyChangeByNameAndNotFound()227   public void applyChangeByNameAndNotFound() {
228     // ID is not set
229     expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST)).andThrow(EXCEPTION).times(2);
230     // again for options
231     expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS))
232         .andThrow(EXCEPTION)
233         .times(2);
234     replay(dns);
235     try {
236       zoneNoId.applyChangeRequest(CHANGE_REQUEST);
237       fail("Parent container not found, should throw an exception.");
238     } catch (DnsException e) {
239       // expected
240     }
241     try {
242       zone.applyChangeRequest(CHANGE_REQUEST);
243       fail("Parent container not found, should throw an exception.");
244     } catch (DnsException e) {
245       // expected
246     }
247     // check options
248     try {
249       zoneNoId.applyChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS);
250       fail("Parent container not found, should throw an exception.");
251     } catch (DnsException e) {
252       // expected
253     }
254     try {
255       zone.applyChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS);
256       fail("Parent container not found, should throw an exception.");
257     } catch (DnsException e) {
258       // expected
259     }
260   }
261 
262   @Test
applyNullChangeRequest()263   public void applyNullChangeRequest() {
264     replay(dns); // no calls expected
265     try {
266       zone.applyChangeRequest(null);
267       fail("Cannot apply null ChangeRequest.");
268     } catch (NullPointerException e) {
269       // expected
270     }
271     try {
272       zone.applyChangeRequest(null, CHANGE_REQUEST_FIELD_OPTIONS);
273       fail("Cannot apply null ChangeRequest.");
274     } catch (NullPointerException e) {
275       // expected
276     }
277     try {
278       zoneNoId.applyChangeRequest(null);
279       fail("Cannot apply null ChangeRequest.");
280     } catch (NullPointerException e) {
281       // expected
282     }
283     try {
284       zoneNoId.applyChangeRequest(null, CHANGE_REQUEST_FIELD_OPTIONS);
285       fail("Cannot apply null ChangeRequest.");
286     } catch (NullPointerException e) {
287       // expected
288     }
289   }
290 
291   @Test
getChangeAndZoneFoundByName()292   public void getChangeAndZoneFoundByName() {
293     expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.getGeneratedId()))
294         .andReturn(changeRequestAfter)
295         .times(2);
296     // again for options
297     expect(
298             dns.getChangeRequest(
299                 ZONE_NAME, CHANGE_REQUEST.getGeneratedId(), CHANGE_REQUEST_FIELD_OPTIONS))
300         .andReturn(changeRequestAfter)
301         .times(2);
302     replay(dns);
303     ChangeRequest result = zoneNoId.getChangeRequest(CHANGE_REQUEST.getGeneratedId());
304     assertEquals(changeRequestAfter, result);
305     result = zone.getChangeRequest(CHANGE_REQUEST.getGeneratedId());
306     assertEquals(changeRequestAfter, result);
307     // check options
308     result =
309         zoneNoId.getChangeRequest(CHANGE_REQUEST.getGeneratedId(), CHANGE_REQUEST_FIELD_OPTIONS);
310     assertEquals(changeRequestAfter, result);
311     result = zone.getChangeRequest(CHANGE_REQUEST.getGeneratedId(), CHANGE_REQUEST_FIELD_OPTIONS);
312     assertEquals(changeRequestAfter, result);
313   }
314 
315   @Test
getChangeAndZoneNotFoundByName()316   public void getChangeAndZoneNotFoundByName() {
317     expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.getGeneratedId()))
318         .andThrow(EXCEPTION)
319         .times(2);
320     // again for options
321     expect(
322             dns.getChangeRequest(
323                 ZONE_NAME, CHANGE_REQUEST.getGeneratedId(), CHANGE_REQUEST_FIELD_OPTIONS))
324         .andThrow(EXCEPTION)
325         .times(2);
326     replay(dns);
327     try {
328       zoneNoId.getChangeRequest(CHANGE_REQUEST.getGeneratedId());
329       fail("Parent container not found, should throw an exception.");
330     } catch (DnsException e) {
331       // expected
332     }
333     try {
334       zone.getChangeRequest(CHANGE_REQUEST.getGeneratedId());
335       fail("Parent container not found, should throw an exception.");
336     } catch (DnsException e) {
337       // expected
338     }
339     // check options
340     try {
341       zoneNoId.getChangeRequest(CHANGE_REQUEST.getGeneratedId(), CHANGE_REQUEST_FIELD_OPTIONS);
342       fail("Parent container not found, should throw an exception.");
343     } catch (DnsException e) {
344       // expected
345     }
346     try {
347       zone.getChangeRequest(CHANGE_REQUEST.getGeneratedId(), CHANGE_REQUEST_FIELD_OPTIONS);
348       fail("Parent container not found, should throw an exception.");
349     } catch (DnsException e) {
350       // expected
351     }
352   }
353 
354   @Test
getChangedWhichDoesNotExistZoneFound()355   public void getChangedWhichDoesNotExistZoneFound() {
356     expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.getGeneratedId()))
357         .andReturn(null)
358         .times(2);
359     // again for options
360     expect(
361             dns.getChangeRequest(
362                 ZONE_NAME, CHANGE_REQUEST.getGeneratedId(), CHANGE_REQUEST_FIELD_OPTIONS))
363         .andReturn(null)
364         .times(2);
365     replay(dns);
366     assertNull(zoneNoId.getChangeRequest(CHANGE_REQUEST.getGeneratedId()));
367     assertNull(zone.getChangeRequest(CHANGE_REQUEST.getGeneratedId()));
368     assertNull(
369         zoneNoId.getChangeRequest(CHANGE_REQUEST.getGeneratedId(), CHANGE_REQUEST_FIELD_OPTIONS));
370     assertNull(
371         zone.getChangeRequest(CHANGE_REQUEST.getGeneratedId(), CHANGE_REQUEST_FIELD_OPTIONS));
372   }
373 
374   @Test
getNullChangeRequest()375   public void getNullChangeRequest() {
376     replay(dns); // no calls expected
377     try {
378       zone.getChangeRequest(null);
379       fail("Cannot get null ChangeRequest.");
380     } catch (NullPointerException e) {
381       // expected
382     }
383     try {
384       zone.getChangeRequest(null, CHANGE_REQUEST_FIELD_OPTIONS);
385       fail("Cannot get null ChangeRequest.");
386     } catch (NullPointerException e) {
387       // expected
388     }
389     try {
390       zoneNoId.getChangeRequest(null);
391       fail("Cannot get null ChangeRequest.");
392     } catch (NullPointerException e) {
393       // expected
394     }
395     try {
396       zoneNoId.getChangeRequest(null, CHANGE_REQUEST_FIELD_OPTIONS);
397       fail("Cannot get null ChangeRequest.");
398     } catch (NullPointerException e) {
399       // expected
400     }
401   }
402 
403   @Test
getChangeRequestWithNoId()404   public void getChangeRequestWithNoId() {
405     replay(dns); // no calls expected
406     try {
407       zone.getChangeRequest(CHANGE_REQUEST_NO_ID.getGeneratedId());
408       fail("Cannot get ChangeRequest by null id.");
409     } catch (NullPointerException e) {
410       // expected
411     }
412     try {
413       zone.getChangeRequest(CHANGE_REQUEST_NO_ID.getGeneratedId(), CHANGE_REQUEST_FIELD_OPTIONS);
414       fail("Cannot get ChangeRequest by null id.");
415     } catch (NullPointerException e) {
416       // expected
417     }
418     try {
419       zoneNoId.getChangeRequest(CHANGE_REQUEST_NO_ID.getGeneratedId());
420       fail("Cannot get ChangeRequest by null id.");
421     } catch (NullPointerException e) {
422       // expected
423     }
424     try {
425       zoneNoId.getChangeRequest(
426           CHANGE_REQUEST_NO_ID.getGeneratedId(), CHANGE_REQUEST_FIELD_OPTIONS);
427       fail("Cannot get ChangeRequest by null id.");
428     } catch (NullPointerException e) {
429       // expected
430     }
431   }
432 
433   @Test
listChangeRequestsAndZoneFound()434   public void listChangeRequestsAndZoneFound() {
435     @SuppressWarnings("unchecked")
436     Page<ChangeRequest> pageMock = createStrictMock(Page.class);
437     replay(pageMock);
438     expect(dns.listChangeRequests(ZONE_NAME)).andReturn(pageMock).times(2);
439     // again for options
440     expect(dns.listChangeRequests(ZONE_NAME, CHANGE_REQUEST_LIST_OPTIONS))
441         .andReturn(pageMock)
442         .times(2);
443     replay(dns);
444     Page<ChangeRequest> result = zoneNoId.listChangeRequests();
445     assertSame(pageMock, result);
446     result = zone.listChangeRequests();
447     assertSame(pageMock, result);
448     verify(pageMock);
449     zoneNoId.listChangeRequests(CHANGE_REQUEST_LIST_OPTIONS); // check options
450     zone.listChangeRequests(CHANGE_REQUEST_LIST_OPTIONS); // check options
451   }
452 
453   @Test
listChangeRequestsAndZoneNotFound()454   public void listChangeRequestsAndZoneNotFound() {
455     expect(dns.listChangeRequests(ZONE_NAME)).andThrow(EXCEPTION).times(2);
456     // again for options
457     expect(dns.listChangeRequests(ZONE_NAME, CHANGE_REQUEST_LIST_OPTIONS))
458         .andThrow(EXCEPTION)
459         .times(2);
460     replay(dns);
461     try {
462       zoneNoId.listChangeRequests();
463       fail("Parent container not found, should throw an exception.");
464     } catch (DnsException e) {
465       // expected
466     }
467     try {
468       zone.listChangeRequests();
469       fail("Parent container not found, should throw an exception.");
470     } catch (DnsException e) {
471       // expected
472     }
473     try {
474       zoneNoId.listChangeRequests(CHANGE_REQUEST_LIST_OPTIONS); // check options
475       fail("Parent container not found, should throw an exception.");
476     } catch (DnsException e) {
477       // expected
478     }
479     try {
480       zone.listChangeRequests(CHANGE_REQUEST_LIST_OPTIONS); // check options
481       fail("Parent container not found, should throw an exception.");
482     } catch (DnsException e) {
483       // expected
484     }
485   }
486 
487   @Test
testFromPb()488   public void testFromPb() {
489     expect(dns.getOptions()).andReturn(OPTIONS);
490     replay(dns);
491     assertEquals(Zone.fromPb(dns, zone.toPb()), zone);
492   }
493 
494   @Test
testEqualsAndToBuilder()495   public void testEqualsAndToBuilder() {
496     expect(dns.getOptions()).andReturn(OPTIONS).times(2);
497     replay(dns);
498     assertEquals(zone, zone.toBuilder().build());
499     assertEquals(zone.hashCode(), zone.toBuilder().build().hashCode());
500   }
501 
502   @Test
testBuilder()503   public void testBuilder() {
504     // one for each build() call because it invokes a constructor
505     expect(dns.getOptions()).andReturn(OPTIONS).times(8);
506     replay(dns);
507     assertNotEquals(
508         zone,
509         zone.toBuilder()
510             .setGeneratedId(new BigInteger(zone.getGeneratedId()).add(BigInteger.ONE).toString())
511             .build());
512     assertNotEquals(zone, zone.toBuilder().setDnsName(zone.getName() + "aaaa").build());
513     assertNotEquals(
514         zone, zone.toBuilder().setNameServerSet(zone.getNameServerSet() + "aaaa").build());
515     assertNotEquals(
516         zone, zone.toBuilder().setNameServers(ImmutableList.of("nameserverpppp")).build());
517     assertNotEquals(zone, zone.toBuilder().setDnsName(zone.getDnsName() + "aaaa").build());
518     assertNotEquals(
519         zone, zone.toBuilder().setCreationTimeMillis(zone.getCreationTimeMillis() + 1).build());
520     Zone.Builder builder = zone.toBuilder();
521     builder
522         .setGeneratedId(ZONE_ID)
523         .setDnsName("example.com")
524         .setCreationTimeMillis(123478946464L)
525         .build();
526     assertEquals(zone, builder.build());
527   }
528 }
529