Skip to content

Commit f994057

Browse files
committed
Another round of comments from @aozarov.
1 parent 1c65715 commit f994057

2 files changed

Lines changed: 58 additions & 30 deletions

File tree

gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsRecord.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*
3434
* <p>A {@code DnsRecord} is the unit of data that will be returned by the DNS servers upon a DNS
3535
* request for a specific domain. The {@code DnsRecord} holds the current state of the DNS records
36-
* that make up a managed zone. You can read the records but you do not modify them directly.
36+
* that make up a managed zone. You can read the records but you cannot modify them directly.
3737
* Rather, you edit the records in a managed zone by creating a ChangeRequest.
3838
*
3939
* @see <a href="https://cloud.google.com/dns/api/v1/resourceRecordSets">Google Cloud DNS
@@ -45,7 +45,7 @@ public class DnsRecord implements Serializable {
4545
private final String name;
4646
private final List<String> rrdatas;
4747
private final Integer ttl;
48-
private final DnsRecordType type;
48+
private final Type type;
4949

5050
/**
5151
* Enum for the DNS record types supported by Cloud DNS.
@@ -56,7 +56,7 @@ public class DnsRecord implements Serializable {
5656
* @see <a href="https://cloud.google.com/dns/what-is-cloud-dns#supported_record_types">Cloud DNS
5757
* supported record types</a>
5858
*/
59-
public enum DnsRecordType {
59+
public enum Type {
6060
/**
6161
* Address record, which is used to map host names to their IPv4 address.
6262
*/
@@ -105,14 +105,19 @@ public enum DnsRecordType {
105105
TXT
106106
}
107107

108+
/**
109+
* A builder of {@link DnsRecord}.
110+
*/
108111
public static class Builder {
109112

110113
private List<String> rrdatas = new LinkedList<>();
111114
private String name;
112115
private Integer ttl;
113-
private DnsRecordType type;
116+
private Type type;
114117

115-
private Builder() {
118+
private Builder(String name, Type type) {
119+
this.name = checkNotNull(name);
120+
this.type = checkNotNull(type);
116121
}
117122

118123
/**
@@ -177,15 +182,15 @@ public Builder name(String name) {
177182
* @param ttl A non-negative number of seconds
178183
*/
179184
public Builder ttl(int ttl) {
180-
checkArgument(ttl >= 0, "TTL cannot be negative. The supplied value was " + ttl + ".");
185+
checkArgument(ttl >= 0, "TTL cannot be negative. The supplied value was %s.", ttl);
181186
this.ttl = ttl;
182187
return this;
183188
}
184189

185190
/**
186191
* The identifier of a supported record type, for example, A, AAAA, MX, TXT, and so on.
187192
*/
188-
public Builder type(DnsRecordType type) {
193+
public Builder type(Type type) {
189194
this.type = checkNotNull(type);
190195
return this;
191196
}
@@ -198,7 +203,7 @@ public DnsRecord build() {
198203
}
199204
}
200205

201-
DnsRecord(Builder builder) {
206+
private DnsRecord(Builder builder) {
202207
this.name = builder.name;
203208
this.rrdatas = ImmutableList.copyOf(builder.rrdatas);
204209
this.ttl = builder.ttl;
@@ -213,14 +218,14 @@ public Builder toBuilder() {
213218
}
214219

215220
/**
216-
* Creates a builder for {@code DnsRecord} with mandatorily set name and type of the record.
221+
* Creates a {@code DnsRecord} builder for the given {@code name} and {@code type}.
217222
*/
218-
public static Builder builder(String name, DnsRecordType type) {
219-
return new Builder().name(name).type(type);
223+
public static Builder builder(String name, Type type) {
224+
return new Builder(name, type);
220225
}
221226

222227
/**
223-
* Get the mandatory user assigned name of this DNS record.
228+
* Returns the user-assigned name of this DNS record.
224229
*/
225230
public String name() {
226231
return name;
@@ -243,7 +248,7 @@ public Integer ttl() {
243248
/**
244249
* Returns the type of this DNS record.
245250
*/
246-
public DnsRecordType type() {
251+
public Type type() {
247252
return type;
248253
}
249254

@@ -259,16 +264,16 @@ public boolean equals(Object obj) {
259264

260265
com.google.api.services.dns.model.ResourceRecordSet toPb() {
261266
com.google.api.services.dns.model.ResourceRecordSet pb =
262-
new com.google.api.services.dns.model.ResourceRecordSet();
267+
new com.google.api.services.dns.model.ResourceRecordSet();
263268
pb.setName(this.name());
264269
pb.setRrdatas(this.records());
265270
pb.setTtl(this.ttl());
266-
pb.setType(this.type() == null ? null : this.type().name());
271+
pb.setType(this.type().name());
267272
return pb;
268273
}
269274

270275
static DnsRecord fromPb(com.google.api.services.dns.model.ResourceRecordSet pb) {
271-
Builder b = builder(pb.getName(), DnsRecordType.valueOf(pb.getType()));
276+
Builder b = builder(pb.getName(), Type.valueOf(pb.getType()));
272277
if (pb.getRrdatas() != null) {
273278
b.records(pb.getRrdatas());
274279
}
@@ -281,10 +286,10 @@ static DnsRecord fromPb(com.google.api.services.dns.model.ResourceRecordSet pb)
281286
@Override
282287
public String toString() {
283288
return MoreObjects.toStringHelper(this)
284-
.add("name", name())
285-
.add("rrdatas", records())
286-
.add("ttl", ttl())
287-
.add("type", type())
288-
.toString();
289+
.add("name", name())
290+
.add("rrdatas", records())
291+
.add("ttl", ttl())
292+
.add("type", type())
293+
.toString();
289294
}
290295
}

gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsRecordTest.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,32 @@ public class DnsRecordTest {
2626

2727
private static final String NAME = "example.com.";
2828
private static final Integer TTL = 3600;
29-
private static final DnsRecord.DnsRecordType TYPE = DnsRecord.DnsRecordType.AAAA;
29+
private static final DnsRecord.Type TYPE = DnsRecord.Type.AAAA;
3030
private static final DnsRecord record = DnsRecord.builder(NAME, TYPE)
31-
.ttl(TTL)
32-
.build();
31+
.ttl(TTL)
32+
.build();
3333

3434
@Test
3535
public void testDefaultDnsRecord() {
3636
DnsRecord record = DnsRecord.builder(NAME, TYPE).build();
3737
assertEquals(0, record.records().size());
38+
assertEquals(TYPE, record.type());
39+
assertEquals(NAME, record.name());
3840
}
3941

4042
@Test
4143
public void testBuilder() {
4244
assertEquals(NAME, record.name());
4345
assertEquals(TTL, record.ttl());
46+
assertEquals(TYPE, record.type());
4447
assertEquals(0, record.records().size());
4548
// verify that one can add records to the record set
4649
String testingRecord = "Testing record";
4750
String anotherTestingRecord = "Another record 123";
4851
DnsRecord anotherRecord = record.toBuilder()
49-
.addRecord(testingRecord)
50-
.addRecord(anotherTestingRecord)
51-
.build();
52+
.addRecord(testingRecord)
53+
.addRecord(anotherTestingRecord)
54+
.build();
5255
assertEquals(2, anotherRecord.records().size());
5356
assertTrue(anotherRecord.records().contains(testingRecord));
5457
assertTrue(anotherRecord.records().contains(anotherTestingRecord));
@@ -72,12 +75,12 @@ public void testEqualsAndNotEquals() {
7275
assertEquals(clone, record);
7376
clone = record.toBuilder().addRecord("another record").build();
7477
assertNotEquals(clone, record);
75-
final String differentName = "totally different name";
78+
String differentName = "totally different name";
7679
clone = record.toBuilder().name(differentName).build();
7780
assertNotEquals(clone, record);
7881
clone = record.toBuilder().ttl(record.ttl() + 1).build();
7982
assertNotEquals(clone, record);
80-
clone = record.toBuilder().type(DnsRecord.DnsRecordType.TXT).build();
83+
clone = record.toBuilder().type(DnsRecord.Type.TXT).build();
8184
assertNotEquals(clone, record);
8285
}
8386

@@ -109,4 +112,24 @@ public void testToBuilder() {
109112
partial = DnsRecord.builder(NAME, TYPE).ttl(15).build();
110113
assertEquals(partial, partial.toBuilder().build());
111114
}
112-
}
115+
116+
@Test
117+
public void clearRecordSet() {
118+
// make sure that we are starting not empty
119+
DnsRecord clone = record.toBuilder().addRecord("record").addRecord("another").build();
120+
assertNotEquals(0, clone.records().size());
121+
clone = clone.toBuilder().clearRecords().build();
122+
assertEquals(0, clone.records().size());
123+
clone.toPb(); // verify that pb allows it
124+
}
125+
126+
@Test
127+
public void removeFromRecordSet() {
128+
String recordString = "record";
129+
// make sure that we are starting not empty
130+
DnsRecord clone = record.toBuilder().addRecord(recordString).build();
131+
assertNotEquals(0, clone.records().size());
132+
clone = clone.toBuilder().removeRecord(recordString).build();
133+
assertEquals(0, clone.records().size());
134+
}
135+
}

0 commit comments

Comments
 (0)