Skip to content

Commit

Permalink
Addressed codacy issues in DNS. This includes
Browse files Browse the repository at this point in the history
- removing static formatter from example
- removing and unifying imports and full qualification
- removing unused serializable import from Dns
- replacing assert not true with assertFalse in ITDnsTest
- removing unused method from LocalDnsHelperTest.
  • Loading branch information
mderka committed Apr 4, 2016
1 parent 59cdf37 commit 5be34a9
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ static ChangeRequest fromPb(Dns dns, String zoneName, Change pb) {
static Function<Change, ChangeRequest> fromPbFunction(final Dns dns, final String zoneName) {
return new Function<Change, ChangeRequest>() {
@Override
public ChangeRequest apply(com.google.api.services.dns.model.Change pb) {
public ChangeRequest apply(Change pb) {
return ChangeRequest.fromPb(dns, zoneName, pb);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.google.gcloud.Service;
import com.google.gcloud.dns.spi.DnsRpc;

import java.io.Serializable;
import java.util.List;

/**
Expand Down
54 changes: 25 additions & 29 deletions gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.google.gcloud.dns;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.gcloud.RetryHelper.RetryHelperException;
import static com.google.gcloud.RetryHelper.runWithRetries;

import com.google.api.services.dns.model.Change;
Expand Down Expand Up @@ -121,8 +120,7 @@ private static Page<Zone> listZones(final DnsOptions serviceOptions,
// this differs from the other list operations since zone is functional and requires dns service
Function<ManagedZone, Zone> pbToZoneFunction = new Function<ManagedZone, Zone>() {
@Override
public Zone apply(
com.google.api.services.dns.model.ManagedZone zonePb) {
public Zone apply(ManagedZone zonePb) {
return Zone.fromPb(serviceOptions.service(), zonePb);
}
};
Expand All @@ -142,7 +140,7 @@ public DnsRpc.ListResult<ManagedZone> call() {
? ImmutableList.<Zone>of() : Iterables.transform(result.results(), pbToZoneFunction);
return new PageImpl<>(new ZonePageFetcher(serviceOptions, cursor, optionsMap),
cursor, zones);
} catch (RetryHelperException e) {
} catch (RetryHelper.RetryHelperException e) {
throw DnsException.translateAndThrow(e);
}
}
Expand All @@ -169,10 +167,10 @@ public DnsRpc.ListResult<Change> call() {
Iterable<ChangeRequest> changes = result.results() == null
? ImmutableList.<ChangeRequest>of()
: Iterables.transform(result.results(),
ChangeRequest.fromPbFunction(serviceOptions.service(), zoneName));
ChangeRequest.fromPbFunction(serviceOptions.service(), zoneName));
return new PageImpl<>(new ChangeRequestPageFetcher(zoneName, serviceOptions, cursor,
optionsMap), cursor, changes);
} catch (RetryHelperException e) {
} catch (RetryHelper.RetryHelperException e) {
throw DnsException.translateAndThrow(e);
}
}
Expand Down Expand Up @@ -201,7 +199,7 @@ public DnsRpc.ListResult<ResourceRecordSet> call() {
: Iterables.transform(result.results(), RecordSet.FROM_PB_FUNCTION);
return new PageImpl<>(new DnsRecordPageFetcher(zoneName, serviceOptions, cursor, optionsMap),
cursor, recordSets);
} catch (RetryHelperException e) {
} catch (RetryHelper.RetryHelperException e) {
throw DnsException.translateAndThrow(e);
}
}
Expand All @@ -210,10 +208,10 @@ public DnsRpc.ListResult<ResourceRecordSet> call() {
public Zone create(final ZoneInfo zoneInfo, Dns.ZoneOption... options) {
final Map<DnsRpc.Option, ?> optionsMap = optionMap(options);
try {
com.google.api.services.dns.model.ManagedZone answer = runWithRetries(
new Callable<com.google.api.services.dns.model.ManagedZone>() {
ManagedZone answer = runWithRetries(
new Callable<ManagedZone>() {
@Override
public com.google.api.services.dns.model.ManagedZone call() {
public ManagedZone call() {
return dnsRpc.create(zoneInfo.toPb(), optionsMap);
}
}, options().retryParams(), EXCEPTION_HANDLER);
Expand All @@ -227,10 +225,10 @@ public com.google.api.services.dns.model.ManagedZone call() {
public Zone getZone(final String zoneName, Dns.ZoneOption... options) {
final Map<DnsRpc.Option, ?> optionsMap = optionMap(options);
try {
com.google.api.services.dns.model.ManagedZone answer = runWithRetries(
new Callable<com.google.api.services.dns.model.ManagedZone>() {
ManagedZone answer = runWithRetries(
new Callable<ManagedZone>() {
@Override
public com.google.api.services.dns.model.ManagedZone call() {
public ManagedZone call() {
return dnsRpc.getZone(zoneName, optionsMap);
}
}, options().retryParams(), EXCEPTION_HANDLER);
Expand Down Expand Up @@ -276,14 +274,13 @@ public ChangeRequest applyChangeRequest(final String zoneName,
final ChangeRequestInfo changeRequest, ChangeRequestOption... options) {
final Map<DnsRpc.Option, ?> optionsMap = optionMap(options);
try {
com.google.api.services.dns.model.Change answer =
runWithRetries(
new Callable<com.google.api.services.dns.model.Change>() {
@Override
public com.google.api.services.dns.model.Change call() {
return dnsRpc.applyChangeRequest(zoneName, changeRequest.toPb(), optionsMap);
}
}, options().retryParams(), EXCEPTION_HANDLER);
Change answer = runWithRetries(
new Callable<Change>() {
@Override
public Change call() {
return dnsRpc.applyChangeRequest(zoneName, changeRequest.toPb(), optionsMap);
}
}, options().retryParams(), EXCEPTION_HANDLER);
return answer == null ? null : ChangeRequest.fromPb(this, zoneName, answer); // not null
} catch (RetryHelper.RetryHelperException ex) {
throw DnsException.translateAndThrow(ex);
Expand All @@ -295,14 +292,13 @@ public ChangeRequest getChangeRequest(final String zoneName, final String change
Dns.ChangeRequestOption... options) {
final Map<DnsRpc.Option, ?> optionsMap = optionMap(options);
try {
com.google.api.services.dns.model.Change answer =
runWithRetries(
new Callable<com.google.api.services.dns.model.Change>() {
@Override
public com.google.api.services.dns.model.Change call() {
return dnsRpc.getChangeRequest(zoneName, changeRequestId, optionsMap);
}
}, options().retryParams(), EXCEPTION_HANDLER);
Change answer = runWithRetries(
new Callable<Change>() {
@Override
public Change call() {
return dnsRpc.getChangeRequest(zoneName, changeRequestId, optionsMap);
}
}, options().retryParams(), EXCEPTION_HANDLER);
return answer == null ? null : ChangeRequest.fromPb(this, zoneName, answer);
} catch (RetryHelper.RetryHelperException ex) {
throw DnsException.translateAndThrow(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,17 +285,16 @@ public boolean equals(Object obj) {
return obj instanceof RecordSet && Objects.equals(this.toPb(), ((RecordSet) obj).toPb());
}

com.google.api.services.dns.model.ResourceRecordSet toPb() {
com.google.api.services.dns.model.ResourceRecordSet pb =
new com.google.api.services.dns.model.ResourceRecordSet();
ResourceRecordSet toPb() {
ResourceRecordSet pb = new ResourceRecordSet();
pb.setName(this.name());
pb.setRrdatas(this.records());
pb.setTtl(this.ttl());
pb.setType(this.type().name());
return pb;
}

static RecordSet fromPb(com.google.api.services.dns.model.ResourceRecordSet pb) {
static RecordSet fromPb(ResourceRecordSet pb) {
Builder builder = builder(pb.getName(), Type.valueOf(pb.getType()));
if (pb.getRrdatas() != null) {
builder.records(pb.getRrdatas());
Expand Down
4 changes: 2 additions & 2 deletions gcloud-java-dns/src/main/java/com/google/gcloud/dns/Zone.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ public int hashCode() {
return Objects.hash(super.hashCode(), options);
}

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
this.dns = options.service();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.google.gcloud.dns;

import static com.google.gcloud.dns.RecordSet.builder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
Expand All @@ -33,13 +32,13 @@ public class RecordSetTest {
private static final TimeUnit UNIT = TimeUnit.HOURS;
private static final Integer UNIT_TTL = 1;
private static final RecordSet.Type TYPE = RecordSet.Type.AAAA;
private static final RecordSet recordSet = builder(NAME, TYPE)
private static final RecordSet recordSet = RecordSet.builder(NAME, TYPE)
.ttl(UNIT_TTL, UNIT)
.build();

@Test
public void testDefaultDnsRecord() {
RecordSet recordSet = builder(NAME, TYPE).build();
RecordSet recordSet = RecordSet.builder(NAME, TYPE).build();
assertEquals(0, recordSet.records().size());
assertEquals(TYPE, recordSet.type());
assertEquals(NAME, recordSet.name());
Expand All @@ -66,15 +65,15 @@ public void testBuilder() {
@Test
public void testValidTtl() {
try {
builder(NAME, TYPE).ttl(-1, TimeUnit.SECONDS);
RecordSet.builder(NAME, TYPE).ttl(-1, TimeUnit.SECONDS);
fail("A negative value is not acceptable for ttl.");
} catch (IllegalArgumentException e) {
// expected
}
builder(NAME, TYPE).ttl(0, TimeUnit.SECONDS);
builder(NAME, TYPE).ttl(Integer.MAX_VALUE, TimeUnit.SECONDS);
RecordSet.builder(NAME, TYPE).ttl(0, TimeUnit.SECONDS);
RecordSet.builder(NAME, TYPE).ttl(Integer.MAX_VALUE, TimeUnit.SECONDS);
try {
builder(NAME, TYPE).ttl(Integer.MAX_VALUE, TimeUnit.HOURS);
RecordSet.builder(NAME, TYPE).ttl(Integer.MAX_VALUE, TimeUnit.HOURS);
fail("This value is too large for int.");
} catch (IllegalArgumentException e) {
// expected
Expand Down Expand Up @@ -108,22 +107,22 @@ public void testSameHashCodeOnEquals() {
@Test
public void testToAndFromPb() {
assertEquals(recordSet, RecordSet.fromPb(recordSet.toPb()));
RecordSet partial = builder(NAME, TYPE).build();
RecordSet partial = RecordSet.builder(NAME, TYPE).build();
assertEquals(partial, RecordSet.fromPb(partial.toPb()));
partial = builder(NAME, TYPE).addRecord("test").build();
partial = RecordSet.builder(NAME, TYPE).addRecord("test").build();
assertEquals(partial, RecordSet.fromPb(partial.toPb()));
partial = builder(NAME, TYPE).ttl(15, TimeUnit.SECONDS).build();
partial = RecordSet.builder(NAME, TYPE).ttl(15, TimeUnit.SECONDS).build();
assertEquals(partial, RecordSet.fromPb(partial.toPb()));
}

@Test
public void testToBuilder() {
assertEquals(recordSet, recordSet.toBuilder().build());
RecordSet partial = builder(NAME, TYPE).build();
RecordSet partial = RecordSet.builder(NAME, TYPE).build();
assertEquals(partial, partial.toBuilder().build());
partial = builder(NAME, TYPE).addRecord("test").build();
partial = RecordSet.builder(NAME, TYPE).addRecord("test").build();
assertEquals(partial, partial.toBuilder().build());
partial = builder(NAME, TYPE).ttl(15, TimeUnit.SECONDS).build();
partial = RecordSet.builder(NAME, TYPE).ttl(15, TimeUnit.SECONDS).build();
assertEquals(partial, partial.toBuilder().build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ public class ITDnsTest {
.build();
private static final List<String> ZONE_NAMES = ImmutableList.of(ZONE_NAME1,
ZONE_NAME_EMPTY_DESCRIPTION);

@Rule
public Timeout globalTimeout = Timeout.seconds(300);

private static void clear() {
for (String zoneName : ZONE_NAMES) {
Expand Down Expand Up @@ -161,9 +164,6 @@ private static void waitForChangeToComplete(String zoneName, String changeId) {
}
}

@Rule
public Timeout globalTimeout = Timeout.seconds(300);

@Test
public void testCreateValidZone() {
try {
Expand Down Expand Up @@ -471,7 +471,7 @@ public void testListZones() {
assertNull(zone.dnsName());
assertNull(zone.description());
assertNull(zone.nameServerSet());
assertTrue(!zone.nameServers().isEmpty());
assertFalse(zone.nameServers().isEmpty());
assertNull(zone.generatedId());
assertFalse(zoneIterator.hasNext());
zoneIterator = DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,6 @@ private static void resetProjects() {
}
}

private static void assertEqChangesIgnoreStatus(Change expected, Change actual) {
assertEquals(expected.getAdditions(), actual.getAdditions());
assertEquals(expected.getDeletions(), actual.getDeletions());
assertEquals(expected.getId(), actual.getId());
assertEquals(expected.getStartTime(), actual.getStartTime());
}

@Test
public void testCreateZone() {
ManagedZone created = RPC.create(ZONE1, EMPTY_RPC_OPTIONS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
public class DnsExample {

private static final Map<String, DnsAction> ACTIONS = new HashMap<>();
private static final DateFormat FORMATTER = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");

private interface DnsAction {
void run(Dns dns, String... args);
Expand Down Expand Up @@ -332,11 +331,12 @@ public void run(Dns dns, String... args) {
}
if (iterator.hasNext()) {
System.out.printf("Change requests for zone %s:%n", zoneName);
DateFormat formatter = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
while (iterator.hasNext()) {
ChangeRequest change = iterator.next();
System.out.printf("%nID: %s%n", change.generatedId());
System.out.printf("Status: %s%n", change.status());
System.out.printf("Started: %s%n", FORMATTER.format(change.startTimeMillis()));
System.out.printf("Started: %s%n", formatter.format(change.startTimeMillis()));
System.out.printf("Deletions: %s%n", Joiner.on(", ").join(change.deletions()));
System.out.printf("Additions: %s%n", Joiner.on(", ").join(change.additions()));
}
Expand Down Expand Up @@ -441,7 +441,8 @@ private static void printZone(Zone zone) {
System.out.printf("%nName: %s%n", zone.name());
System.out.printf("ID: %s%n", zone.generatedId());
System.out.printf("Description: %s%n", zone.description());
System.out.printf("Created: %s%n", FORMATTER.format(new Date(zone.creationTimeMillis())));
DateFormat formatter = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
System.out.printf("Created: %s%n", formatter.format(new Date(zone.creationTimeMillis())));
System.out.printf("Name servers: %s%n", Joiner.on(", ").join(zone.nameServers()));
}

Expand Down

0 comments on commit 5be34a9

Please sign in to comment.