From a77fd34b49d29413df9c5816541b742e1293dbaf Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 14 Sep 2015 09:16:15 -0700 Subject: [PATCH 01/60] Update values and keys to v1beta3 --- gcloud-java-datastore/pom.xml | 20 +++++ .../datastore/BaseDatastoreBatchWriter.java | 3 +- .../google/gcloud/datastore/BaseEntity.java | 12 ++- .../com/google/gcloud/datastore/BaseKey.java | 23 +++--- .../google/gcloud/datastore/BatchImpl.java | 4 +- .../google/gcloud/datastore/BlobValue.java | 8 +- .../google/gcloud/datastore/BooleanValue.java | 8 +- .../gcloud/datastore/DatastoreImpl.java | 15 ++-- .../com/google/gcloud/datastore/DateTime.java | 22 ++++-- .../gcloud/datastore/DateTimeValue.java | 15 ++-- .../google/gcloud/datastore/DoubleValue.java | 8 +- .../google/gcloud/datastore/EntityValue.java | 25 +++--- .../com/google/gcloud/datastore/GqlQuery.java | 7 +- .../gcloud/datastore/IncompleteKey.java | 21 ++--- .../java/com/google/gcloud/datastore/Key.java | 8 +- .../com/google/gcloud/datastore/KeyValue.java | 8 +- .../google/gcloud/datastore/ListValue.java | 27 +++---- .../google/gcloud/datastore/LongValue.java | 8 +- .../google/gcloud/datastore/NullValue.java | 6 +- .../google/gcloud/datastore/PathElement.java | 17 ++-- .../gcloud/datastore/ProjectionEntity.java | 4 +- .../com/google/gcloud/datastore/Query.java | 7 +- .../com/google/gcloud/datastore/RawValue.java | 30 +++---- .../google/gcloud/datastore/StringValue.java | 8 +- .../gcloud/datastore/StructuredQuery.java | 9 ++- .../gcloud/datastore/TransactionImpl.java | 4 +- .../com/google/gcloud/datastore/Value.java | 78 +++++++------------ .../google/gcloud/datastore/ValueBuilder.java | 8 +- .../gcloud/datastore/ValueMarshaller.java | 6 +- .../google/gcloud/datastore/package-info.java | 4 +- .../BaseDatastoreBatchWriterTest.java | 16 ++-- .../gcloud/datastore/BlobValueTest.java | 11 +-- .../gcloud/datastore/BooleanValueTest.java | 11 +-- .../gcloud/datastore/DatastoreTest.java | 15 ++-- .../gcloud/datastore/DateTimeValueTest.java | 11 +-- .../gcloud/datastore/DoubleValueTest.java | 11 +-- .../gcloud/datastore/EntityValueTest.java | 17 +--- .../google/gcloud/datastore/KeyValueTest.java | 11 +-- .../gcloud/datastore/ListValueTest.java | 15 +--- .../gcloud/datastore/LongValueTest.java | 11 +-- .../gcloud/datastore/NullValueTest.java | 12 +-- .../google/gcloud/datastore/RawValueTest.java | 16 ++-- .../gcloud/datastore/SerializationTest.java | 13 ++-- .../gcloud/datastore/StringValueTest.java | 12 +-- .../google/gcloud/datastore/ValueTest.java | 65 +++------------- 45 files changed, 294 insertions(+), 376 deletions(-) diff --git a/gcloud-java-datastore/pom.xml b/gcloud-java-datastore/pom.xml index 52b3f62232c4..8315836f67cd 100644 --- a/gcloud-java-datastore/pom.xml +++ b/gcloud-java-datastore/pom.xml @@ -13,12 +13,32 @@ gcloud-java-pom 0.0.8-SNAPSHOT + + + sonatype-snapshots + sonatype-snapshots + https://oss.sonatype.org/content/repositories/snapshots/ + + true + + + ${project.groupId} gcloud-java-core ${project.version} + + com.google.cloud.datastore + datastore-v1beta3-protos + 0.0.1-SNAPSHOT + + + com.google.cloud.datastore + datastore-v1beta3-proto-client + 0.0.1-SNAPSHOT + com.google.apis google-api-services-datastore-protobuf diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseDatastoreBatchWriter.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseDatastoreBatchWriter.java index 7eaf5c535f26..8d0eec117bdf 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseDatastoreBatchWriter.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseDatastoreBatchWriter.java @@ -214,7 +214,8 @@ protected DatastoreV1.Mutation.Builder toMutationPb() { mutationPb.addUpsert(entity.toPb()); } for (Key key : toDelete()) { - mutationPb.addDelete(key.toPb()); + // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + //mutationPb.addDelete(key.toPb()); } return mutationPb; } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseEntity.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseEntity.java index 3a79f3053a1e..665b4d8d24b9 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseEntity.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseEntity.java @@ -93,11 +93,13 @@ private B self() { protected B fill(DatastoreV1.Entity entityPb) { Map> copiedProperties = Maps.newHashMap(); for (DatastoreV1.Property property : entityPb.getPropertyList()) { - copiedProperties.put(property.getName(), Value.fromPb(property.getValue())); + // TODO(ajaykannan): Uncomment when possible in datastore v1beta3 transition + //copiedProperties.put(property.getName(), Value.fromPb(property.getValue())); } properties(copiedProperties); if (entityPb.hasKey()) { - key((K) IncompleteKey.fromPb(entityPb.getKey())); + // TODO(ajaykannan): Uncomment when possible in datastore v1beta3 transition + //key((K) IncompleteKey.fromPb(entityPb.getKey())); } return self(); } @@ -389,11 +391,13 @@ protected final DatastoreV1.Entity toPb() { for (Map.Entry> entry : properties.entrySet()) { DatastoreV1.Property.Builder propertyPb = DatastoreV1.Property.newBuilder(); propertyPb.setName(entry.getKey()); - propertyPb.setValue(entry.getValue().toPb()); + // TODO(ajaykannan): Uncomment when possible in datastore v1beta3 transition + //propertyPb.setValue(entry.getValue().toPb()); entityPb.addProperty(propertyPb.build()); } if (key != null) { - entityPb.setKey(key.toPb()); + // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + //entityPb.setKey(key.toPb()); } return entityPb.build(); } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseKey.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseKey.java index 865b95ed8518..fdc973c6c4cc 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseKey.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseKey.java @@ -20,8 +20,8 @@ import static com.google.gcloud.datastore.Validator.validateKind; import static com.google.gcloud.datastore.Validator.validateNamespace; -import com.google.api.services.datastore.DatastoreV1; import com.google.common.base.Preconditions; +import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; import java.util.LinkedList; @@ -31,7 +31,7 @@ /** * Base class for keys. */ -abstract class BaseKey extends Serializable { +abstract class BaseKey extends Serializable { private static final long serialVersionUID = -4671243265877410635L; @@ -172,20 +172,21 @@ public boolean equals(Object obj) { } @Override - protected DatastoreV1.Key toPb() { - DatastoreV1.Key.Builder keyPb = DatastoreV1.Key.newBuilder(); - DatastoreV1.PartitionId.Builder partitionIdPb = DatastoreV1.PartitionId.newBuilder(); - if (projectId != null) { - partitionIdPb.setDatasetId(projectId); + protected com.google.datastore.v1beta3.Key toPb() { + com.google.datastore.v1beta3.Key.Builder keyPb = com.google.datastore.v1beta3.Key.newBuilder(); + com.google.datastore.v1beta3.PartitionId.Builder partitionIdPb = + com.google.datastore.v1beta3.PartitionId.newBuilder(); + if (!Strings.isNullOrEmpty(projectId)) { + partitionIdPb.setProjectId(projectId); } - if (namespace != null) { - partitionIdPb.setNamespace(namespace); + if (!Strings.isNullOrEmpty(namespace)) { + partitionIdPb.setNamespaceId(namespace); } - if (partitionIdPb.hasDatasetId() || partitionIdPb.hasNamespace()) { + if (!partitionIdPb.getProjectId().isEmpty() || !partitionIdPb.getNamespaceId().isEmpty()) { keyPb.setPartitionId(partitionIdPb.build()); } for (PathElement pathEntry : path) { - keyPb.addPathElement(pathEntry.toPb()); + keyPb.addPath(pathEntry.toPb()); } return keyPb.build(); } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BatchImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BatchImpl.java index 8cb41304500b..95cdfbb10f01 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BatchImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BatchImpl.java @@ -43,7 +43,9 @@ public List generatedKeys() { return Lists.transform(response.getMutationResult().getInsertAutoIdKeyList(), new Function() { @Override public Key apply(DatastoreV1.Key keyPb) { - return Key.fromPb(keyPb); + // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + //return Key.fromPb(keyPb); + return Key.builder(null).build(); // TODO(ajaykannan): remove this line when possible } }); } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BlobValue.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BlobValue.java index fb61c0b9ad34..19d545e1790c 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BlobValue.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BlobValue.java @@ -16,9 +16,7 @@ package com.google.gcloud.datastore; -import static com.google.api.services.datastore.DatastoreV1.Value.BLOB_VALUE_FIELD_NUMBER; - -import com.google.api.services.datastore.DatastoreV1; +import static com.google.datastore.v1beta3.Value.BLOB_VALUE_FIELD_NUMBER; public final class BlobValue extends Value { @@ -40,12 +38,12 @@ public Builder newBuilder(Blob value) { } @Override - protected Blob getValue(DatastoreV1.Value from) { + protected Blob getValue(com.google.datastore.v1beta3.Value from) { return new Blob(from.getBlobValue()); } @Override - protected void setValue(BlobValue from, DatastoreV1.Value.Builder to) { + protected void setValue(BlobValue from, com.google.datastore.v1beta3.Value.Builder to) { to.setBlobValue(from.get().byteString()); } }; diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BooleanValue.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BooleanValue.java index 2dd98a5013ac..3e1bdc14e822 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BooleanValue.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BooleanValue.java @@ -16,9 +16,7 @@ package com.google.gcloud.datastore; -import static com.google.api.services.datastore.DatastoreV1.Value.BOOLEAN_VALUE_FIELD_NUMBER; - -import com.google.api.services.datastore.DatastoreV1; +import static com.google.datastore.v1beta3.Value.BOOLEAN_VALUE_FIELD_NUMBER; public final class BooleanValue extends Value { @@ -40,12 +38,12 @@ public Builder newBuilder(Boolean value) { } @Override - protected Boolean getValue(DatastoreV1.Value from) { + protected Boolean getValue(com.google.datastore.v1beta3.Value from) { return from.getBooleanValue(); } @Override - protected void setValue(BooleanValue from, DatastoreV1.Value.Builder to) { + protected void setValue(BooleanValue from, com.google.datastore.v1beta3.Value.Builder to) { to.setBooleanValue(from.get()); } }; diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java index 6f2454c62167..23d4f74a8da1 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java @@ -126,12 +126,14 @@ public List allocateId(IncompleteKey... keys) { } DatastoreV1.AllocateIdsRequest.Builder requestPb = DatastoreV1.AllocateIdsRequest.newBuilder(); for (IncompleteKey key : keys) { - requestPb.addKey(trimNameOrId(key).toPb()); + // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + //requestPb.addKey(trimNameOrId(key).toPb()); } DatastoreV1.AllocateIdsResponse responsePb = allocateIds(requestPb.build()); ImmutableList.Builder keyList = ImmutableList.builder(); for (DatastoreV1.Key keyPb : responsePb.getKeyList()) { - keyList.add(Key.fromPb(keyPb)); + // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + // keyList.add(Key.fromPb(keyPb)); } return keyList.build(); } @@ -193,7 +195,8 @@ public List add(FullEntity... entities) { if (completeEntity != null) { responseBuilder.add(completeEntity); } else { - responseBuilder.add(Entity.builder(Key.fromPb(allocatedKeys.next()), entity).build()); + // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + //responseBuilder.add(Entity.builder(Key.fromPb(allocatedKeys.next()), entity).build()); } } return responseBuilder.build(); @@ -223,7 +226,8 @@ Iterator get(DatastoreV1.ReadOptions readOptionsPb, final Key... keys) { requestPb.setReadOptions(readOptionsPb); } for (Key k : Sets.newLinkedHashSet(Arrays.asList(keys))) { - requestPb.addKey(k.toPb()); + // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + //requestPb.addKey(k.toPb()); } return new ResultsIterator(requestPb); } @@ -310,7 +314,8 @@ public void delete(Key... keys) { DatastoreV1.Mutation.Builder mutationPb = DatastoreV1.Mutation.newBuilder(); Set dedupKeys = new LinkedHashSet<>(Arrays.asList(keys)); for (Key key : dedupKeys) { - mutationPb.addDelete(key.toPb()); + // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + //mutationPb.addDelete(key.toPb()); } commitMutation(mutationPb); } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DateTime.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DateTime.java index af5a17ef7ef3..0d5c99d2b3fd 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DateTime.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DateTime.java @@ -18,8 +18,6 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.api.services.datastore.DatastoreV1; -import com.google.api.services.datastore.DatastoreV1.Value; import com.google.protobuf.InvalidProtocolBufferException; import org.joda.time.format.ISODateTimeFormat; @@ -34,7 +32,7 @@ * @see Google Cloud Datastore * Entities, Properties, and Keys */ -public final class DateTime extends Serializable +public final class DateTime extends Serializable implements Comparable { private static final long serialVersionUID = 7343324797621228378L; @@ -98,12 +96,24 @@ public static DateTime copyFrom(Calendar calendar) { } @Override - protected Value toPb() { - return DatastoreV1.Value.newBuilder().setIntegerValue(timestampMicroseconds).build(); + protected com.google.datastore.v1beta3.Value toPb() { + return com.google.datastore.v1beta3.Value.newBuilder() + .setTimestampValue(microsecondsToTimestampPb(timestampMicroseconds)).build(); } @Override protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException { - return new DateTime(DatastoreV1.Value.parseFrom(bytesPb).getIntegerValue()); + return new DateTime(timestampPbToMicroseconds(com.google.datastore.v1beta3.Value + .parseFrom(bytesPb).getTimestampValue())); + } + + protected static long timestampPbToMicroseconds(com.google.protobuf.Timestamp timestampPb) { + return timestampPb.getSeconds() * 10^6 + timestampPb.getNanos() / 10^3; + } + + protected static com.google.protobuf.Timestamp microsecondsToTimestampPb(long microseconds) { + long seconds = microseconds / 10^6; + int nanos = (int) (microseconds % 10^6) * 10^3; + return com.google.protobuf.Timestamp.newBuilder().setSeconds(seconds).setNanos(nanos).build(); } } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DateTimeValue.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DateTimeValue.java index 7aec5c7d3c47..e9c42ffabf4c 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DateTimeValue.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DateTimeValue.java @@ -16,9 +16,7 @@ package com.google.gcloud.datastore; -import static com.google.api.services.datastore.DatastoreV1.Value.TIMESTAMP_MICROSECONDS_VALUE_FIELD_NUMBER; - -import com.google.api.services.datastore.DatastoreV1; +import static com.google.datastore.v1beta3.Value.TIMESTAMP_VALUE_FIELD_NUMBER; public final class DateTimeValue extends Value { @@ -31,7 +29,7 @@ public final class DateTimeValue extends Value { @Override public int getProtoFieldId() { - return TIMESTAMP_MICROSECONDS_VALUE_FIELD_NUMBER; + return TIMESTAMP_VALUE_FIELD_NUMBER; } @Override @@ -40,13 +38,14 @@ public Builder newBuilder(DateTime value) { } @Override - protected DateTime getValue(DatastoreV1.Value from) { - return new DateTime(from.getTimestampMicrosecondsValue()); + protected DateTime getValue(com.google.datastore.v1beta3.Value from) { + return new DateTime(DateTime.timestampPbToMicroseconds(from.getTimestampValue())); } @Override - protected void setValue(DateTimeValue from, DatastoreV1.Value.Builder to) { - to.setTimestampMicrosecondsValue(from.get().timestampMicroseconds()); + protected void setValue(DateTimeValue from, com.google.datastore.v1beta3.Value.Builder to) { + to.setTimestampValue(DateTime.microsecondsToTimestampPb(from.get() + .timestampMicroseconds())); } }; diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DoubleValue.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DoubleValue.java index d12bbe317aef..cf543ad94b78 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DoubleValue.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DoubleValue.java @@ -16,7 +16,7 @@ package com.google.gcloud.datastore; -import static com.google.api.services.datastore.DatastoreV1.Value.DOUBLE_VALUE_FIELD_NUMBER; +import static com.google.datastore.v1beta3.Value.DOUBLE_VALUE_FIELD_NUMBER; import com.google.api.services.datastore.DatastoreV1; @@ -40,12 +40,12 @@ public Builder newBuilder(Double value) { } @Override - protected Double getValue(DatastoreV1.Value from) { + protected Double getValue(com.google.datastore.v1beta3.Value from) { return from.getDoubleValue(); } - + @Override - protected void setValue(DoubleValue from, DatastoreV1.Value.Builder to) { + protected void setValue(DoubleValue from, com.google.datastore.v1beta3.Value.Builder to) { to.setDoubleValue(from.get()); } }; diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/EntityValue.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/EntityValue.java index da32c8eb2462..3105f9fa0dd9 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/EntityValue.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/EntityValue.java @@ -16,10 +16,7 @@ package com.google.gcloud.datastore; -import static com.google.api.services.datastore.DatastoreV1.Value.ENTITY_VALUE_FIELD_NUMBER; - -import com.google.api.services.datastore.DatastoreV1; -import com.google.common.base.Preconditions; +import static com.google.datastore.v1beta3.Value.ENTITY_VALUE_FIELD_NUMBER; public class EntityValue extends Value> { @@ -41,13 +38,16 @@ public Builder newBuilder(FullEntity value) { } @Override - protected FullEntity getValue(DatastoreV1.Value from) { - return FullEntity.fromPb(from.getEntityValue()); + protected FullEntity getValue(com.google.datastore.v1beta3.Value from) { + // TODO(ajaykannan): uncomment this line when possible in datastore v1beta3 transition + //return FullEntity.fromPb(from.getEntityValue()); + return null; // TODO(ajaykannan): remove this line when possible } @Override - protected void setValue(EntityValue from, DatastoreV1.Value.Builder to) { - to.setEntityValue(from.get().toPb()); + protected void setValue(EntityValue from, com.google.datastore.v1beta3.Value.Builder to) { + // TODO(ajaykannan): uncomment this line when possible in datastore v1beta3 transition + //to.setEntityValue(from.get().toPb()); } }; @@ -57,13 +57,6 @@ private Builder() { super(ValueType.ENTITY); } - @Override - public Builder indexed(boolean indexed) { - // see issue #25 - Preconditions.checkArgument(!indexed, "EntityValue can't be indexed"); - return super.indexed(indexed); - } - @Override public EntityValue build() { return new EntityValue(this); @@ -88,6 +81,6 @@ public static EntityValue of(FullEntity entity) { } public static Builder builder(FullEntity entity) { - return new Builder().set(entity).indexed(false); + return new Builder().set(entity); } } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java index e9bd8e12cfd8..ff659805a34d 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java @@ -135,7 +135,8 @@ protected DatastoreV1.GqlQueryArg toPb() { argPb.setCursor(cursor.byteString()); } if (value != null) { - argPb.setValue(value.toPb()); + // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + //argPb.setValue(value.toPb()); } return argPb.build(); } @@ -150,7 +151,9 @@ static Binding fromPb(DatastoreV1.GqlQueryArg argPb) { if (argPb.hasCursor()) { return new Binding(name, new Cursor(argPb.getCursor())); } - return new Binding(name, Value.fromPb(argPb.getValue())); + // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + //return new Binding(name, Value.fromPb(argPb.getValue())); + return new Binding(name, new Cursor(null)); // TODO(ajaykannan): remove this line when possible } } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/IncompleteKey.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/IncompleteKey.java index 6134eed2905b..0eabefe32643 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/IncompleteKey.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/IncompleteKey.java @@ -16,7 +16,6 @@ package com.google.gcloud.datastore; -import com.google.api.services.datastore.DatastoreV1; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.protobuf.InvalidProtocolBufferException; @@ -55,25 +54,27 @@ public IncompleteKey build() { @Override protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException { - return fromPb(DatastoreV1.Key.parseFrom(bytesPb)); + return fromPb(com.google.datastore.v1beta3.Key.parseFrom(bytesPb)); } - static IncompleteKey fromPb(DatastoreV1.Key keyPb) { + static IncompleteKey fromPb(com.google.datastore.v1beta3.Key keyPb) { String projectId = null; String namespace = null; if (keyPb.hasPartitionId()) { - DatastoreV1.PartitionId partitionIdPb = keyPb.getPartitionId(); - if (partitionIdPb.hasDatasetId()) { - projectId = partitionIdPb.getDatasetId(); + com.google.datastore.v1beta3.PartitionId partitionIdPb = keyPb.getPartitionId(); + projectId = partitionIdPb.getProjectId(); + if (projectId.isEmpty()) { + projectId = null; } - if (partitionIdPb.hasNamespace()) { - namespace = partitionIdPb.getNamespace(); + namespace = partitionIdPb.getNamespaceId(); + if (namespace.isEmpty()) { + namespace = null; } } - List pathElementsPb = keyPb.getPathElementList(); + List pathElementsPb = keyPb.getPathList(); Preconditions.checkArgument(!pathElementsPb.isEmpty(), "Path must not be empty"); ImmutableList.Builder pathBuilder = ImmutableList.builder(); - for (DatastoreV1.Key.PathElement pathElementPb : pathElementsPb) { + for (com.google.datastore.v1beta3.Key.PathElement pathElementPb : pathElementsPb) { pathBuilder.add(PathElement.fromPb(pathElementPb)); } ImmutableList path = pathBuilder.build(); diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Key.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Key.java index c625c067f6c2..04dbdddf8e23 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Key.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Key.java @@ -18,7 +18,6 @@ import static java.nio.charset.StandardCharsets.UTF_8; -import com.google.api.services.datastore.DatastoreV1; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.protobuf.InvalidProtocolBufferException; @@ -153,7 +152,8 @@ public String toUrlSafe() { public static Key fromUrlSafe(String urlSafe) { try { String utf8Str = URLDecoder.decode(urlSafe, UTF_8.name()); - DatastoreV1.Key.Builder builder = DatastoreV1.Key.newBuilder(); + com.google.datastore.v1beta3.Key.Builder builder = + com.google.datastore.v1beta3.Key.newBuilder(); TextFormat.merge(utf8Str, builder); return fromPb(builder.build()); } catch (UnsupportedEncodingException e) { @@ -165,10 +165,10 @@ public static Key fromUrlSafe(String urlSafe) { @Override protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException { - return fromPb(DatastoreV1.Key.parseFrom(bytesPb)); + return fromPb(com.google.datastore.v1beta3.Key.parseFrom(bytesPb)); } - static Key fromPb(DatastoreV1.Key keyPb) { + static Key fromPb(com.google.datastore.v1beta3.Key keyPb) { IncompleteKey key = IncompleteKey.fromPb(keyPb); Preconditions.checkState(key instanceof Key, "Key is not complete"); return (Key) key; diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/KeyValue.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/KeyValue.java index 252f48ebc92a..fc1823730d12 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/KeyValue.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/KeyValue.java @@ -16,9 +16,7 @@ package com.google.gcloud.datastore; -import static com.google.api.services.datastore.DatastoreV1.Value.KEY_VALUE_FIELD_NUMBER; - -import com.google.api.services.datastore.DatastoreV1; +import static com.google.datastore.v1beta3.Value.KEY_VALUE_FIELD_NUMBER; public final class KeyValue extends Value { @@ -40,12 +38,12 @@ public Builder newBuilder(Key key) { } @Override - protected Key getValue(DatastoreV1.Value from) { + protected Key getValue(com.google.datastore.v1beta3.Value from) { return Key.fromPb(from.getKeyValue()); } @Override - protected void setValue(KeyValue from, DatastoreV1.Value.Builder to) { + protected void setValue(KeyValue from, com.google.datastore.v1beta3.Value.Builder to) { to.setKeyValue(from.get().toPb()); } }; diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ListValue.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ListValue.java index 41c7e82788b5..242a2956b682 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ListValue.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ListValue.java @@ -16,9 +16,8 @@ package com.google.gcloud.datastore; -import static com.google.api.services.datastore.DatastoreV1.Value.LIST_VALUE_FIELD_NUMBER; +import static com.google.datastore.v1beta3.Value.ARRAY_VALUE_FIELD_NUMBER; -import com.google.api.services.datastore.DatastoreV1; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; @@ -36,7 +35,7 @@ public final class ListValue extends Value>> { @Override public int getProtoFieldId() { - return LIST_VALUE_FIELD_NUMBER; + return ARRAY_VALUE_FIELD_NUMBER; } @Override @@ -45,19 +44,23 @@ public Builder newBuilder(List> values) { } @Override - protected List> getValue(DatastoreV1.Value from) { - List> properties = new ArrayList<>(from.getListValueCount()); - for (DatastoreV1.Value valuePb : from.getListValueList()) { + protected List> getValue(com.google.datastore.v1beta3.Value from) { + List> properties = new ArrayList<>(from.getArrayValue().getValuesCount()); + for (com.google.datastore.v1beta3.Value valuePb : from.getArrayValue().getValuesList()) { properties.add(Value.fromPb(valuePb)); } return properties; } @Override - protected void setValue(ListValue from, DatastoreV1.Value.Builder to) { + protected void setValue(ListValue from, com.google.datastore.v1beta3.Value.Builder to) { + List propertiesPb = + new ArrayList(); for (Value property : from.get()) { - to.addListValue(property.toPb()); + propertiesPb.add(property.toPb()); } + to.setArrayValue(com.google.datastore.v1beta3.ArrayValue.newBuilder() + .addAllValues(propertiesPb)); } }; @@ -71,7 +74,7 @@ private Builder() { } public Builder addValue(Value value) { - // see datastore_v1.proto definition for list_value + // see datastore.proto definition for list_value Preconditions.checkArgument(value.type() != ValueType.LIST, "Cannot contain another list"); listBuilder.add(value); return this; @@ -85,12 +88,6 @@ public Builder addValue(Value first, Value... other) { return this; } - @Override - public Builder indexed(boolean indexed) { - // see issue #26 - throw DatastoreException.throwInvalidRequest("ListValue can't specify index"); - } - /** * Copy the list of values. * diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/LongValue.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/LongValue.java index 43d139e90249..18cdead6280a 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/LongValue.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/LongValue.java @@ -16,9 +16,7 @@ package com.google.gcloud.datastore; -import static com.google.api.services.datastore.DatastoreV1.Value.INTEGER_VALUE_FIELD_NUMBER; - -import com.google.api.services.datastore.DatastoreV1; +import static com.google.datastore.v1beta3.Value.INTEGER_VALUE_FIELD_NUMBER; public final class LongValue extends Value { @@ -40,12 +38,12 @@ public Builder newBuilder(Long value) { } @Override - protected Long getValue(DatastoreV1.Value from) { + protected Long getValue(com.google.datastore.v1beta3.Value from) { return from.getIntegerValue(); } @Override - protected void setValue(LongValue from, DatastoreV1.Value.Builder to) { + protected void setValue(LongValue from, com.google.datastore.v1beta3.Value.Builder to) { to.setIntegerValue(from.get()); } }; diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/NullValue.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/NullValue.java index 58fed152ffd5..173ee3ef79f0 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/NullValue.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/NullValue.java @@ -18,8 +18,6 @@ import static com.google.common.base.Preconditions.checkArgument; -import com.google.api.services.datastore.DatastoreV1; - public final class NullValue extends Value { private static final long serialVersionUID = 8497300779013002270L; @@ -40,12 +38,12 @@ public int getProtoFieldId() { } @Override - protected Void getValue(DatastoreV1.Value from) { + protected Void getValue(com.google.datastore.v1beta3.Value from) { return null; } @Override - protected void setValue(NullValue from, DatastoreV1.Value.Builder to) { + protected void setValue(NullValue from, com.google.datastore.v1beta3.Value.Builder to) { // nothing to set } }; diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/PathElement.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/PathElement.java index 186ed97adcde..c0196a6326fe 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/PathElement.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/PathElement.java @@ -19,7 +19,6 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; -import com.google.api.services.datastore.DatastoreV1; import com.google.common.base.Strings; import com.google.protobuf.InvalidProtocolBufferException; @@ -28,7 +27,7 @@ /** * Represents a single element in a key's path. */ -public final class PathElement extends Serializable { +public final class PathElement extends Serializable { private static final long serialVersionUID = -7968078857690784595L; @@ -86,8 +85,8 @@ public boolean equals(Object obj) { } @Override - protected DatastoreV1.Key.PathElement toPb() { - DatastoreV1.Key.PathElement.Builder pathElementPb = DatastoreV1.Key.PathElement.newBuilder(); + protected com.google.datastore.v1beta3.Key.PathElement toPb() { + com.google.datastore.v1beta3.Key.PathElement.Builder pathElementPb = com.google.datastore.v1beta3.Key.PathElement.newBuilder(); pathElementPb.setKind(kind); if (id != null) { pathElementPb.setId(id); @@ -99,15 +98,17 @@ protected DatastoreV1.Key.PathElement toPb() { @Override protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException { - return fromPb(DatastoreV1.Key.PathElement.parseFrom(bytesPb)); + return fromPb(com.google.datastore.v1beta3.Key.PathElement.parseFrom(bytesPb)); } - static PathElement fromPb(DatastoreV1.Key.PathElement pathElementPb) { + static PathElement fromPb(com.google.datastore.v1beta3.Key.PathElement pathElementPb) { String kind = pathElementPb.getKind(); - if (pathElementPb.hasId()) { + if (pathElementPb.getIdTypeCase() == + com.google.datastore.v1beta3.Key.PathElement.IdTypeCase.ID) { return of(kind, pathElementPb.getId()); } - if (pathElementPb.hasName()) { + if (pathElementPb.getIdTypeCase() == + com.google.datastore.v1beta3.Key.PathElement.IdTypeCase.NAME) { return of(kind, pathElementPb.getName()); } return of(kind); diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ProjectionEntity.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ProjectionEntity.java index 1ba054b68161..46a528617a0c 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ProjectionEntity.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ProjectionEntity.java @@ -56,7 +56,7 @@ public ProjectionEntity build() { @Override public DateTime getDateTime(String name) { Value value = getValue(name); - if (value.hasMeaning() && value.meaning() == 18 && value instanceof LongValue) { + if (value.meaning() == 18 && value instanceof LongValue) { return new DateTime(getLong(name)); } return ((Value) value).get(); @@ -66,7 +66,7 @@ public DateTime getDateTime(String name) { @Override public Blob getBlob(String name) { Value value = getValue(name); - if (value.hasMeaning() && value.meaning() == 18 && value instanceof StringValue) { + if (value.meaning() == 18 && value instanceof StringValue) { return new Blob(ByteString.copyFromUtf8(getString(name))); } return ((Value) value).get(); diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java index 343535d94628..fdd120ccbe85 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java @@ -66,7 +66,8 @@ public abstract static class ResultType implements java.io.Serializable { if (!entityPb.hasKey()) { return null; } - return Key.fromPb(entityPb.getKey()); + //TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + //return Key.fromPb(entityPb.getKey()); } return ProjectionEntity.fromPb(entityPb); } @@ -88,7 +89,9 @@ public abstract static class ResultType implements java.io.Serializable { private static final long serialVersionUID = -8514289244104446252L; @Override protected Key convert(DatastoreV1.Entity entityPb) { - return Key.fromPb(entityPb.getKey()); + //TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + //return Key.fromPb(entityPb.getKey()); + return Key.builder(null).build(); // TODO(ajaykannan): remove this line when possible } }; diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/RawValue.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/RawValue.java index 9d447cf4289b..d60f215cd5f2 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/RawValue.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/RawValue.java @@ -16,19 +16,17 @@ package com.google.gcloud.datastore; -import com.google.api.services.datastore.DatastoreV1; - -public final class RawValue extends Value { +public final class RawValue extends Value { private static final long serialVersionUID = -3359604598651897941L; - static final BaseMarshaller MARSHALLER = - new BaseMarshaller() { + static final BaseMarshaller MARSHALLER = + new BaseMarshaller() { private static final long serialVersionUID = 5320642719486106244L; @Override - public Builder newBuilder(DatastoreV1.Value value) { + public Builder newBuilder(com.google.datastore.v1beta3.Value value) { return builder(value); } @@ -38,18 +36,18 @@ public int getProtoFieldId() { } @Override - protected DatastoreV1.Value getValue(DatastoreV1.Value from) { + protected com.google.datastore.v1beta3.Value getValue(com.google.datastore.v1beta3.Value from) { return from; } @Override - protected void setValue(RawValue from, DatastoreV1.Value.Builder to) { + protected void setValue(RawValue from, com.google.datastore.v1beta3.Value.Builder to) { to.mergeFrom(from.get()); } }; public static final class Builder - extends Value.BaseBuilder { + extends Value.BaseBuilder { private Builder() { super(ValueType.RAW_VALUE); @@ -65,7 +63,7 @@ private RawValue(Builder builder) { super(builder); } - RawValue(DatastoreV1.Value valuePb) { + RawValue(com.google.datastore.v1beta3.Value valuePb) { this(builder(valuePb)); } @@ -74,18 +72,14 @@ public Builder toBuilder() { return new Builder().mergeFrom(this); } - static RawValue of(DatastoreV1.Value valuePb) { + static RawValue of(com.google.datastore.v1beta3.Value valuePb) { return new RawValue(valuePb); } - static Builder builder(DatastoreV1.Value valuePb) { + static Builder builder(com.google.datastore.v1beta3.Value valuePb) { Builder builder = new Builder(); - if (valuePb.hasIndexed()) { - builder.indexed(valuePb.getIndexed()); - } - if (valuePb.hasMeaning()) { - builder.meaning(valuePb.getMeaning()); - } + builder.excludeFromIndexes(valuePb.getExcludeFromIndexes()); + builder.meaning(valuePb.getMeaning()); builder.set(valuePb); return builder; } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StringValue.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StringValue.java index 95a31e714876..4b2c8e123be7 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StringValue.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StringValue.java @@ -16,9 +16,7 @@ package com.google.gcloud.datastore; -import static com.google.api.services.datastore.DatastoreV1.Value.STRING_VALUE_FIELD_NUMBER; - -import com.google.api.services.datastore.DatastoreV1; +import static com.google.datastore.v1beta3.Value.STRING_VALUE_FIELD_NUMBER; public final class StringValue extends Value { @@ -40,12 +38,12 @@ public Builder newBuilder(String value) { } @Override - protected String getValue(DatastoreV1.Value from) { + protected String getValue(com.google.datastore.v1beta3.Value from) { return from.getStringValue(); } @Override - protected void setValue(StringValue from, DatastoreV1.Value.Builder to) { + protected void setValue(StringValue from, com.google.datastore.v1beta3.Value.Builder to) { to.setStringValue(from.get()); } }; diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java index ce596307d6da..b034b15995a3 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java @@ -228,8 +228,10 @@ private PropertyFilter(String property, Operator operator, Value value) { public static PropertyFilter fromPb(DatastoreV1.PropertyFilter propertyFilterPb) { String property = propertyFilterPb.getProperty().getName(); Operator operator = Operator.fromPb(propertyFilterPb.getOperator()); - Value value = Value.fromPb(propertyFilterPb.getValue()); - return new PropertyFilter(property, operator, value); + // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + //Value value = Value.fromPb(propertyFilterPb.getValue()); + //return new PropertyFilter(property, operator, value); + return new PropertyFilter(property, operator, null); // TODO(ajaykannan): remove this line when possible } @Override @@ -435,7 +437,8 @@ protected DatastoreV1.Filter toPb() { propertyFilterPb.getPropertyBuilder().setName(property); propertyFilterPb.setOperator(operator.toPb()); if (value != null) { - propertyFilterPb.setValue(value.toPb()); + // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + //propertyFilterPb.setValue(value.toPb()); } return filterPb.build(); } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionImpl.java index ae677aa005d6..62978ce73be6 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionImpl.java @@ -47,7 +47,9 @@ public List generatedKeys() { return Lists.transform(response.getMutationResult().getInsertAutoIdKeyList(), new Function() { @Override public Key apply(DatastoreV1.Key keyPb) { - return Key.fromPb(keyPb); + // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + //return Key.fromPb(keyPb); + return Key.builder(null).build(); //: TODO(ajaykannan) remove this placeholder line } }); } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Value.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Value.java index c5fc63a960b1..94e1d7de545f 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Value.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Value.java @@ -18,7 +18,6 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.api.services.datastore.DatastoreV1; import com.google.protobuf.Descriptors.FieldDescriptor; import com.google.protobuf.InvalidProtocolBufferException; @@ -33,14 +32,14 @@ * * @param the type of the content for this value */ -public abstract class Value extends Serializable { +public abstract class Value extends Serializable { private static final long serialVersionUID = -1899638277588872742L; private final transient ValueType valueType; - private final transient Boolean indexed; - private final transient Integer meaning; + private final transient boolean excludeFromIndexes; + private final transient int meaning; private final transient V value; interface BuilderFactory, B extends ValueBuilder> @@ -55,34 +54,26 @@ abstract static class BaseMarshaller, B extends ValueBuild @SuppressWarnings("deprecation") @Override - public final B fromProto(DatastoreV1.Value proto) { + public final B fromProto(com.google.datastore.v1beta3.Value proto) { B builder = newBuilder(getValue(proto)); - if (proto.hasIndexed()) { - builder.indexed(proto.getIndexed()); - } - if (proto.hasMeaning()) { - builder.meaning(proto.getMeaning()); - } + builder.excludeFromIndexes(proto.getExcludeFromIndexes()); + builder.meaning(proto.getMeaning()); return builder; } @SuppressWarnings("deprecation") @Override - public final DatastoreV1.Value toProto(P value) { - DatastoreV1.Value.Builder builder = DatastoreV1.Value.newBuilder(); - if (value.hasIndexed()) { - builder.setIndexed(value.indexed()); - } - if (value.hasMeaning()) { - builder.setMeaning(value.meaning()); - } + public final com.google.datastore.v1beta3.Value toProto(P value) { + com.google.datastore.v1beta3.Value.Builder builder = com.google.datastore.v1beta3.Value.newBuilder(); + builder.setExcludeFromIndexes(value.excludeFromIndexes()); + builder.setMeaning(value.meaning()); setValue(value, builder); return builder.build(); } - protected abstract V getValue(DatastoreV1.Value from); + protected abstract V getValue(com.google.datastore.v1beta3.Value from); - protected abstract void setValue(P from, DatastoreV1.Value.Builder to); + protected abstract void setValue(P from, com.google.datastore.v1beta3.Value.Builder to); } @SuppressWarnings("deprecation") @@ -90,8 +81,8 @@ abstract static class BaseBuilder, B extends BaseBuilder { private final ValueType valueType; - private Boolean indexed; - private Integer meaning; + private boolean excludeFromIndexes; + private int meaning; private V value; BaseBuilder(ValueType valueType) { @@ -105,30 +96,30 @@ public ValueType getValueType() { @Override public B mergeFrom(P other) { - indexed = other.indexed(); + excludeFromIndexes = other.excludeFromIndexes(); meaning = other.meaning(); set(other.get()); return self(); } @Override - public Boolean getIndexed() { - return indexed; + public boolean getExcludeFromIndexes() { + return excludeFromIndexes; } @Override - public B indexed(boolean indexed) { - this.indexed = indexed; + public B excludeFromIndexes(boolean excludeFromIndexes) { + this.excludeFromIndexes = excludeFromIndexes; return self(); } @Override - public Integer getMeaning() { + public int getMeaning() { return meaning; } @Override - public B meaning(Integer meaning) { + public B meaning(int meaning) { this.meaning = meaning; return self(); } @@ -155,7 +146,7 @@ private B self() {

, B extends BaseBuilder> Value(ValueBuilder builder) { valueType = builder.getValueType(); - indexed = builder.getIndexed(); + excludeFromIndexes = builder.getExcludeFromIndexes(); meaning = builder.getMeaning(); value = builder.get(); } @@ -164,21 +155,12 @@ public final ValueType type() { return valueType; } - public final boolean hasIndexed() { - return indexed != null; - } - - public final Boolean indexed() { - return indexed; - } - - @Deprecated - public final boolean hasMeaning() { - return meaning != null; + public final boolean excludeFromIndexes() { + return excludeFromIndexes; } @Deprecated - public final Integer meaning() { + public final int meaning() { return meaning; } @@ -190,7 +172,7 @@ public final V get() { @Override public int hashCode() { - return Objects.hash(valueType, indexed, meaning, value); + return Objects.hash(valueType, excludeFromIndexes, meaning, value); } @Override @@ -204,18 +186,18 @@ public boolean equals(Object obj) { } Value other = (Value) obj; return Objects.equals(valueType, other.valueType) - && Objects.equals(indexed, other.indexed) + && Objects.equals(excludeFromIndexes, other.excludeFromIndexes) && Objects.equals(meaning, other.meaning) && Objects.equals(value, other.value); } @Override @SuppressWarnings("unchecked") - protected DatastoreV1.Value toPb() { + protected com.google.datastore.v1beta3.Value toPb() { return type().getMarshaller().toProto(this); } - static Value fromPb(DatastoreV1.Value proto) { + static Value fromPb(com.google.datastore.v1beta3.Value proto) { for (Entry entry : proto.getAllFields().entrySet()) { FieldDescriptor descriptor = entry.getKey(); if (descriptor.getName().endsWith("_value")) { @@ -232,6 +214,6 @@ static Value fromPb(DatastoreV1.Value proto) { @Override protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException { - return fromPb(DatastoreV1.Value.parseFrom(bytesPb)); + return fromPb(com.google.datastore.v1beta3.Value.parseFrom(bytesPb)); } } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ValueBuilder.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ValueBuilder.java index f5b5d4c1319b..5094062e1ec8 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ValueBuilder.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ValueBuilder.java @@ -25,14 +25,14 @@ public interface ValueBuilder, B extends ValueBuilder, B extends ValueBuilder> extends java.io.Serializable { - B fromProto(DatastoreV1.Value proto); + B fromProto(com.google.datastore.v1beta3.Value proto); - DatastoreV1.Value toProto(P value); + com.google.datastore.v1beta3.Value toProto(P value); int getProtoFieldId(); } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/package-info.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/package-info.java index 3b402820e663..f63be902c319 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/package-info.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/package-info.java @@ -27,7 +27,7 @@ * if (entity == null) { * entity = Entity.builder(key) * .set("name", "John Do") - * .set("age", LongValue.builder(100).indexed(false).build()) + * .set("age", LongValue.builder(100).excludeFromIndexes(true).build()) * .set("updated", false) * .build(); * datastore.put(entity); @@ -37,7 +37,7 @@ * String[] name = entity.getString("name").split(" "); * entity = Entity.builder(entity) * .set("name", name[0]) - * .set("last_name", StringValue.builder(name[1]).indexed(false).build()) + * .set("last_name", StringValue.builder(name[1]).excludeFromIndexes(true).build()) * .set("updated", true) * .remove("old_property") * .set("new_property", 1.1) diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseDatastoreBatchWriterTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseDatastoreBatchWriterTest.java index ad8e0cee266a..28fea360ecf4 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseDatastoreBatchWriterTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseDatastoreBatchWriterTest.java @@ -272,10 +272,11 @@ public void testPutWhenNotActive() throws Exception { @Test public void testDelete() throws Exception { + // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() - .addDelete(KEY1.toPb()) - .addDelete(KEY2.toPb()) - .addDelete(KEY3.toPb()) + //.addDelete(KEY1.toPb()) + //.addDelete(KEY2.toPb()) + //.addDelete(KEY3.toPb()) .build(); batchWriter.delete(KEY1, KEY2); batchWriter.delete(KEY3); @@ -284,9 +285,10 @@ public void testDelete() throws Exception { @Test public void testDeleteAfterAdd() throws Exception { + // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() .addInsertAutoId(INCOMPLETE_ENTITY_1.toPb()) - .addDelete(KEY1.toPb()) + //.addDelete(KEY1.toPb()) .build(); batchWriter.add(ENTITY1); batchWriter.addWithDeferredIdAllocation(INCOMPLETE_ENTITY_1); @@ -296,8 +298,9 @@ public void testDeleteAfterAdd() throws Exception { @Test public void testDeleteAfterUpdate() throws Exception { + // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() - .addDelete(KEY1.toPb()) + //.addDelete(KEY1.toPb()) .build(); batchWriter.update(ENTITY1); batchWriter.delete(KEY1); @@ -306,8 +309,9 @@ public void testDeleteAfterUpdate() throws Exception { @Test public void testDeleteAfterPut() throws Exception { + // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() - .addDelete(KEY1.toPb()) + //.addDelete(KEY1.toPb()) .build(); batchWriter.put(ENTITY1); batchWriter.delete(KEY1); diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BlobValueTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BlobValueTest.java index 40d0299d8fb3..2a4c0dc956e3 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BlobValueTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BlobValueTest.java @@ -37,19 +37,16 @@ public void testToBuilder() throws Exception { public void testOf() throws Exception { BlobValue value = BlobValue.of(CONTENT); assertEquals(CONTENT, value.get()); - assertFalse(value.hasIndexed()); - assertFalse(value.hasMeaning()); + assertFalse(value.excludeFromIndexes()); } @SuppressWarnings("deprecation") @Test public void testBuilder() throws Exception { BlobValue.Builder builder = BlobValue.builder(CONTENT); - BlobValue value = builder.meaning(1).indexed(false).build(); + BlobValue value = builder.meaning(1).excludeFromIndexes(true).build(); assertEquals(CONTENT, value.get()); - assertTrue(value.hasMeaning()); - assertEquals(Integer.valueOf(1), value.meaning()); - assertTrue(value.hasIndexed()); - assertFalse(value.indexed()); + assertEquals(1, value.meaning()); + assertTrue(value.excludeFromIndexes()); } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BooleanValueTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BooleanValueTest.java index 16bbe9cbf518..bff3d67c9465 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BooleanValueTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BooleanValueTest.java @@ -35,19 +35,16 @@ public void testToBuilder() throws Exception { public void testOf() throws Exception { BooleanValue value = BooleanValue.of(false); assertFalse(value.get()); - assertFalse(value.hasIndexed()); - assertFalse(value.hasMeaning()); + assertFalse(value.excludeFromIndexes()); } @SuppressWarnings("deprecation") @Test public void testBuilder() throws Exception { BooleanValue.Builder builder = BooleanValue.builder(true); - BooleanValue value = builder.meaning(1).indexed(true).build(); + BooleanValue value = builder.meaning(1).excludeFromIndexes(true).build(); assertTrue(value.get()); - assertTrue(value.hasMeaning()); - assertEquals(Integer.valueOf(1), value.meaning()); - assertTrue(value.hasIndexed()); - assertTrue(value.indexed()); + assertEquals(1, value.meaning()); + assertTrue(value.excludeFromIndexes()); } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index 156f9684f8ba..65872abb79e7 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -59,7 +59,8 @@ public class DatastoreTest { private static final String KIND3 = "kind3"; private static final NullValue NULL_VALUE = NullValue.of(); private static final StringValue STR_VALUE = StringValue.of("str"); - private static final BooleanValue BOOL_VALUE = BooleanValue.builder(false).indexed(false).build(); + private static final BooleanValue BOOL_VALUE = BooleanValue.builder(false) + .excludeFromIndexes(true).build(); private static final IncompleteKey INCOMPLETE_KEY1 = IncompleteKey.builder(PROJECT_ID, KIND1).build(); private static final IncompleteKey INCOMPLETE_KEY2 = @@ -636,17 +637,19 @@ public void testKeyFactory() { @Test public void testRetires() throws Exception { - DatastoreV1.LookupRequest requestPb = - DatastoreV1.LookupRequest.newBuilder().addKey(KEY1.toPb()).build(); + // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + //DatastoreV1.LookupRequest requestPb = + // DatastoreV1.LookupRequest.newBuilder().addKey(KEY1.toPb()).build(); DatastoreV1.LookupResponse responsePb = DatastoreV1.LookupResponse.newBuilder() .addFound(EntityResult.newBuilder().setEntity(ENTITY1.toPb())).build(); DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class); DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class); EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) .andReturn(rpcMock); - EasyMock.expect(rpcMock.lookup(requestPb)) - .andThrow(new DatastoreRpc.DatastoreRpcException(Reason.UNAVAILABLE)) - .andReturn(responsePb); + // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + //EasyMock.expect(rpcMock.lookup(requestPb)) + // .andThrow(new DatastoreRpc.DatastoreRpcException(Reason.UNAVAILABLE)) + // .andReturn(responsePb); EasyMock.replay(rpcFactoryMock, rpcMock); DatastoreOptions options = this.options.toBuilder() .retryParams(RetryParams.getDefaultInstance()) diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DateTimeValueTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DateTimeValueTest.java index d7fef2ca69b9..c90e8c88ba52 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DateTimeValueTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DateTimeValueTest.java @@ -37,19 +37,16 @@ public void testToBuilder() throws Exception { public void testOf() throws Exception { DateTimeValue value = DateTimeValue.of(CONTENT); assertEquals(CONTENT, value.get()); - assertFalse(value.hasIndexed()); - assertFalse(value.hasMeaning()); + assertFalse(value.excludeFromIndexes()); } @SuppressWarnings("deprecation") @Test public void testBuilder() throws Exception { DateTimeValue.Builder builder = DateTimeValue.builder(CONTENT); - DateTimeValue value = builder.meaning(1).indexed(false).build(); + DateTimeValue value = builder.meaning(1).excludeFromIndexes(true).build(); assertEquals(CONTENT, value.get()); - assertTrue(value.hasMeaning()); - assertEquals(Integer.valueOf(1), value.meaning()); - assertTrue(value.hasIndexed()); - assertFalse(value.indexed()); + assertEquals(1, value.meaning()); + assertTrue(value.excludeFromIndexes()); } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DoubleValueTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DoubleValueTest.java index fa39511a45de..24d9113ce873 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DoubleValueTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DoubleValueTest.java @@ -37,19 +37,16 @@ public void testToBuilder() throws Exception { public void testOf() throws Exception { DoubleValue value = DoubleValue.of(CONTENT); assertEquals(CONTENT, value.get()); - assertFalse(value.hasIndexed()); - assertFalse(value.hasMeaning()); + assertFalse(value.excludeFromIndexes()); } @SuppressWarnings("deprecation") @Test public void testBuilder() throws Exception { DoubleValue.Builder builder = DoubleValue.builder(CONTENT); - DoubleValue value = builder.meaning(1).indexed(false).build(); + DoubleValue value = builder.meaning(1).excludeFromIndexes(true).build(); assertEquals(CONTENT, value.get()); - assertTrue(value.hasMeaning()); - assertEquals(Integer.valueOf(1), value.meaning()); - assertTrue(value.hasIndexed()); - assertFalse(value.indexed()); + assertEquals(1, value.meaning()); + assertTrue(value.excludeFromIndexes()); } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/EntityValueTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/EntityValueTest.java index cd1f7af38067..e7df91ac0016 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/EntityValueTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/EntityValueTest.java @@ -38,25 +38,16 @@ public void testToBuilder() throws Exception { public void testOf() throws Exception { EntityValue value = EntityValue.of(CONTENT); assertEquals(CONTENT, value.get()); - assertTrue(value.hasIndexed()); - assertFalse(value.indexed()); - assertFalse(value.hasMeaning()); - } - - @Test(expected = IllegalArgumentException.class) - public void testIndexedNotAllowed() { - EntityValue.builder(CONTENT).indexed(true); + assertFalse(value.excludeFromIndexes()); } @SuppressWarnings("deprecation") @Test public void testBuilder() throws Exception { EntityValue.Builder builder = EntityValue.builder(CONTENT); - EntityValue value = builder.meaning(1).indexed(false).build(); + EntityValue value = builder.meaning(1).excludeFromIndexes(true).build(); assertEquals(CONTENT, value.get()); - assertTrue(value.hasMeaning()); - assertEquals(Integer.valueOf(1), value.meaning()); - assertTrue(value.hasIndexed()); - assertFalse(value.indexed()); + assertEquals(1, value.meaning()); + assertTrue(value.excludeFromIndexes()); } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/KeyValueTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/KeyValueTest.java index 131a80462a62..b2e916983b8a 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/KeyValueTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/KeyValueTest.java @@ -37,19 +37,16 @@ public void testToBuilder() throws Exception { public void testOf() throws Exception { KeyValue value = KeyValue.of(CONTENT); assertEquals(CONTENT, value.get()); - assertFalse(value.hasIndexed()); - assertFalse(value.hasMeaning()); + assertFalse(value.excludeFromIndexes()); } @SuppressWarnings("deprecation") @Test public void testBuilder() throws Exception { KeyValue.Builder builder = KeyValue.builder(CONTENT); - KeyValue value = builder.meaning(1).indexed(false).build(); + KeyValue value = builder.meaning(1).excludeFromIndexes(true).build(); assertEquals(CONTENT, value.get()); - assertTrue(value.hasMeaning()); - assertEquals(Integer.valueOf(1), value.meaning()); - assertTrue(value.hasIndexed()); - assertFalse(value.indexed()); + assertEquals(1, value.meaning()); + assertTrue(value.excludeFromIndexes()); } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/ListValueTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/ListValueTest.java index 36e3571d49ac..23a9325f22de 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/ListValueTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/ListValueTest.java @@ -41,24 +41,17 @@ public void testToBuilder() throws Exception { public void testOf() throws Exception { ListValue value = ListValue.of(CONTENT); assertEquals(CONTENT, value.get()); - assertFalse(value.hasIndexed()); - assertFalse(value.hasMeaning()); - } - - @Test(expected = DatastoreException.class) - public void testIndexedCannotBeSpecified() { - ListValue.builder().indexed(false); + assertFalse(value.excludeFromIndexes()); } @SuppressWarnings("deprecation") @Test public void testBuilder() throws Exception { ListValue.Builder builder = ListValue.builder().set(CONTENT); - ListValue value = builder.meaning(1).build(); + ListValue value = builder.meaning(1).excludeFromIndexes(true).build(); assertEquals(CONTENT, value.get()); - assertTrue(value.hasMeaning()); - assertEquals(Integer.valueOf(1), value.meaning()); - assertFalse(value.hasIndexed()); + assertEquals(1, value.meaning()); + assertTrue(value.excludeFromIndexes()); builder = ListValue.builder(); for (Value v : CONTENT) { diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LongValueTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LongValueTest.java index c4c899785d68..717c1567bc45 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LongValueTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LongValueTest.java @@ -37,19 +37,16 @@ public void testToBuilder() throws Exception { public void testOf() throws Exception { LongValue value = LongValue.of(CONTENT); assertEquals(CONTENT, value.get()); - assertFalse(value.hasIndexed()); - assertFalse(value.hasMeaning()); + assertFalse(value.excludeFromIndexes()); } @SuppressWarnings("deprecation") @Test public void testBuilder() throws Exception { LongValue.Builder builder = LongValue.builder(CONTENT); - LongValue value = builder.meaning(1).indexed(false).build(); + LongValue value = builder.meaning(1).excludeFromIndexes(true).build(); assertEquals(CONTENT, value.get()); - assertTrue(value.hasMeaning()); - assertEquals(Integer.valueOf(1), value.meaning()); - assertTrue(value.hasIndexed()); - assertFalse(value.indexed()); + assertEquals(1, value.meaning()); + assertTrue(value.excludeFromIndexes()); } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/NullValueTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/NullValueTest.java index a42fdaf0229f..0856fced7992 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/NullValueTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/NullValueTest.java @@ -31,24 +31,20 @@ public void testToBuilder() throws Exception { assertEquals(value, value.toBuilder().build()); } - @SuppressWarnings("deprecation") @Test public void testOf() throws Exception { NullValue value = NullValue.of(); assertNull(value.get()); - assertFalse(value.hasIndexed()); - assertFalse(value.hasMeaning()); + assertFalse(value.excludeFromIndexes()); } @SuppressWarnings("deprecation") @Test public void testBuilder() throws Exception { NullValue.Builder builder = NullValue.builder(); - NullValue value = builder.meaning(1).indexed(false).build(); + NullValue value = builder.meaning(1).excludeFromIndexes(true).build(); assertNull(value.get()); - assertTrue(value.hasMeaning()); - assertEquals(Integer.valueOf(1), value.meaning()); - assertTrue(value.hasIndexed()); - assertFalse(value.indexed()); + assertEquals(1, value.meaning()); + assertTrue(value.excludeFromIndexes()); } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/RawValueTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/RawValueTest.java index 4d63bc89bacb..1ab1c36538de 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/RawValueTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/RawValueTest.java @@ -20,13 +20,11 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import com.google.api.services.datastore.DatastoreV1; - import org.junit.Test; public class RawValueTest { - private static final DatastoreV1.Value CONTENT = StringValue.of("hello").toPb(); + private static final com.google.datastore.v1beta3.Value CONTENT = StringValue.of("hello").toPb(); @Test public void testToBuilder() throws Exception { @@ -34,24 +32,20 @@ public void testToBuilder() throws Exception { assertEquals(value, value.toBuilder().build()); } - @SuppressWarnings("deprecation") @Test public void testOf() throws Exception { RawValue value = RawValue.of(CONTENT); assertEquals(CONTENT, value.get()); - assertFalse(value.hasIndexed()); - assertFalse(value.hasMeaning()); + assertFalse(value.excludeFromIndexes()); } @SuppressWarnings("deprecation") @Test public void testBuilder() throws Exception { RawValue.Builder builder = RawValue.builder(CONTENT); - RawValue value = builder.meaning(1).indexed(false).build(); + RawValue value = builder.meaning(1).excludeFromIndexes(true).build(); assertEquals(CONTENT, value.get()); - assertTrue(value.hasMeaning()); - assertEquals(Integer.valueOf(1), value.meaning()); - assertTrue(value.hasIndexed()); - assertFalse(value.indexed()); + assertEquals(1, value.meaning()); + assertTrue(value.excludeFromIndexes()); } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java index 5f3bfc036fa2..9dfdd33c873d 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java @@ -20,7 +20,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotSame; -import com.google.api.services.datastore.DatastoreV1; import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.Multimap; import com.google.gcloud.AuthCredentials; @@ -82,27 +81,27 @@ public class SerializationTest { .addOrderBy(OrderBy.asc("p")) .build(); private static final KeyValue KEY_VALUE = KeyValue.of(KEY1); - private static final NullValue NULL_VALUE = NullValue.builder().indexed(true).build(); + private static final NullValue NULL_VALUE = NullValue.builder().excludeFromIndexes(true).build(); private static final StringValue STRING_VALUE = StringValue.of("hello"); private static final LongValue LONG_VALUE = LongValue.of(123); private static final DoubleValue DOUBLE_VALUE = DoubleValue.of(12.34); private static final BooleanValue BOOLEAN_VALUE = BooleanValue.of(true); private static final DateTimeValue DATE_AND_TIME_VALUE = DateTimeValue.of(DateTime.now()); private static final BlobValue BLOB_VALUE = BlobValue.of(BLOB1); - private static final RawValue RAW_VALUE = RawValue.of( - DatastoreV1.Value.newBuilder().setBlobKeyValue("blob-key").setMeaning(18).build()); + private static final RawValue RAW_VALUE = RawValue.of(com.google.datastore.v1beta3.Value + .newBuilder().setStringValue("blob-key").setMeaning(18).build()); private static final Entity ENTITY1 = Entity.builder(KEY1).build(); private static final Entity ENTITY2 = Entity.builder(KEY2).set("null", NullValue.of()).build(); private static final Entity ENTITY3 = Entity.builder(KEY2) .set("p1", StringValue.builder("hi1").meaning(10).build()) - .set("p2", StringValue.builder("hi2").meaning(11).indexed(false).build()) - .set("p3", LongValue.builder(100).indexed(false).meaning(100).build()) + .set("p2", StringValue.builder("hi2").meaning(11).excludeFromIndexes(true).build()) + .set("p3", LongValue.builder(100).excludeFromIndexes(true).meaning(100).build()) .set("blob", BLOB1) .build(); private static final FullEntity EMBEDDED_ENTITY = Entity.builder(INCOMPLETE_KEY1) .set("p1", STRING_VALUE) - .set("p2", LongValue.builder(100).indexed(false).meaning(100).build()) + .set("p2", LongValue.builder(100).excludeFromIndexes(true).meaning(100).build()) .build(); private static final EntityValue EMBEDDED_ENTITY_VALUE1 = EntityValue.of(ENTITY1); private static final EntityValue EMBEDDED_ENTITY_VALUE2 = EntityValue.of(ENTITY2); diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/StringValueTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/StringValueTest.java index a2cacd6574aa..4f02568bf924 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/StringValueTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/StringValueTest.java @@ -32,24 +32,20 @@ public void testToBuilder() throws Exception { assertEquals(value, value.toBuilder().build()); } - @SuppressWarnings("deprecation") @Test public void testOf() throws Exception { StringValue value = StringValue.of(CONTENT); assertEquals(CONTENT, value.get()); - assertFalse(value.hasIndexed()); - assertFalse(value.hasMeaning()); + assertFalse(value.excludeFromIndexes()); } @SuppressWarnings("deprecation") @Test public void testBuilder() throws Exception { StringValue.Builder builder = StringValue.builder(CONTENT); - StringValue value = builder.meaning(1).indexed(false).build(); + StringValue value = builder.meaning(1).excludeFromIndexes(true).build(); assertEquals(CONTENT, value.get()); - assertTrue(value.hasMeaning()); - assertEquals(Integer.valueOf(1), value.meaning()); - assertTrue(value.hasIndexed()); - assertFalse(value.indexed()); + assertEquals(1, value.meaning()); + assertTrue(value.excludeFromIndexes()); } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/ValueTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/ValueTest.java index 973a3c3c0da4..12908add4c8e 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/ValueTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/ValueTest.java @@ -18,7 +18,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import com.google.common.collect.ImmutableList; @@ -116,67 +115,22 @@ public void testType() throws Exception { } @Test - public void testHasIndexed() throws Exception { + public void testExcludeFromIndexes() throws Exception { for (Map.Entry> entry : typeToValue.entrySet()) { - ValueType valueType = entry.getKey(); - Boolean indexed = entry.getValue().hasIndexed(); - switch (valueType) { - case ENTITY: - assertTrue(indexed); - break; - default: - assertFalse(indexed); - break; - } - } - - TestBuilder builder = new TestBuilder(); - assertFalse(builder.build().hasIndexed()); - assertTrue(builder.indexed(false).build().hasIndexed()); - assertTrue(builder.indexed(true).build().hasIndexed()); - } - - @Test - public void testIndexed() throws Exception { - for (Map.Entry> entry : typeToValue.entrySet()) { - ValueType valueType = entry.getKey(); - Boolean indexed = entry.getValue().indexed(); - switch (valueType) { - case ENTITY: - assertFalse(indexed); - break; - default: - assertNull(indexed); - break; - } - } - - TestBuilder builder = new TestBuilder(); - assertNull(builder.build().indexed()); - assertFalse(builder.indexed(false).build().indexed()); - assertTrue(builder.indexed(true).build().indexed()); - } - - @SuppressWarnings("deprecation") - @Test - public void testHasMeaning() throws Exception { - for (Value value: typeToValue.values()) { - assertFalse(value.hasMeaning()); + assertFalse(entry.getValue().excludeFromIndexes()); } TestBuilder builder = new TestBuilder(); - assertTrue(builder.meaning(10).build().hasMeaning()); + assertFalse(builder.build().excludeFromIndexes()); + assertTrue(builder.excludeFromIndexes(true).build().excludeFromIndexes()); + assertFalse(builder.excludeFromIndexes(false).build().excludeFromIndexes()); } @SuppressWarnings("deprecation") @Test public void testMeaning() throws Exception { - for (Value value: typeToValue.values()) { - assertNull(value.meaning()); - } - TestBuilder builder = new TestBuilder(); - assertEquals(Integer.valueOf(10), builder.meaning(10).build().meaning()); + assertEquals(10, builder.meaning(10).build().meaning()); } @Test @@ -197,12 +151,11 @@ public void testGet() throws Exception { public void testToBuilder() throws Exception { Set content = Collections.singleton("bla"); ValueBuilder builder = new TestBuilder(); - builder.meaning(1).set(content).indexed(true); + builder.meaning(1).set(content).excludeFromIndexes(true); Value value = builder.build(); builder = value.toBuilder(); - assertEquals(Integer.valueOf(1), value.meaning()); - assertTrue(value.hasIndexed()); - assertTrue(value.indexed()); + assertEquals(1, value.meaning()); + assertTrue(value.excludeFromIndexes()); assertEquals(ValueType.LIST, value.type()); assertEquals(content, value.get()); assertEquals(value, builder.build()); From 9f40a5f4b9a8d60e02e5cf4b004bee290b231eba Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 14 Sep 2015 15:25:12 -0700 Subject: [PATCH 02/60] Default projectId and namespace are empty strings, NullValue and DateTime are fixed, along with other minor edits --- .../datastore/BaseDatastoreBatchWriter.java | 2 +- .../google/gcloud/datastore/BaseEntity.java | 8 ++--- .../com/google/gcloud/datastore/BaseKey.java | 17 ++++------- .../google/gcloud/datastore/BatchImpl.java | 4 +-- .../gcloud/datastore/DatastoreImpl.java | 10 +++---- .../com/google/gcloud/datastore/DateTime.java | 17 +++++------ .../google/gcloud/datastore/DoubleValue.java | 4 +-- .../google/gcloud/datastore/EntityValue.java | 6 ++-- .../com/google/gcloud/datastore/GqlQuery.java | 6 ++-- .../gcloud/datastore/IncompleteKey.java | 10 ++----- .../google/gcloud/datastore/KeyFactory.java | 3 +- .../google/gcloud/datastore/NullValue.java | 9 ++++-- .../google/gcloud/datastore/PathElement.java | 18 +++++------ .../com/google/gcloud/datastore/Query.java | 6 ++-- .../com/google/gcloud/datastore/RawValue.java | 3 +- .../gcloud/datastore/StructuredQuery.java | 6 ++-- .../gcloud/datastore/TransactionImpl.java | 4 +-- .../google/gcloud/datastore/Validator.java | 12 ++++---- .../com/google/gcloud/datastore/Value.java | 24 +++++---------- .../google/gcloud/datastore/ValueType.java | 4 +++ .../BaseDatastoreBatchWriterTest.java | 30 ++++++++++++------- .../google/gcloud/datastore/BaseKeyTest.java | 3 +- .../gcloud/datastore/DatastoreTest.java | 15 +++++----- .../gcloud/datastore/KeyFactoryTest.java | 8 ++--- .../gcloud/datastore/ListValueTest.java | 2 +- .../gcloud/datastore/SerializationTest.java | 19 +++++++----- 26 files changed, 125 insertions(+), 125 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseDatastoreBatchWriter.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseDatastoreBatchWriter.java index 8d0eec117bdf..d94eafbb42f6 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseDatastoreBatchWriter.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseDatastoreBatchWriter.java @@ -214,7 +214,7 @@ protected DatastoreV1.Mutation.Builder toMutationPb() { mutationPb.addUpsert(entity.toPb()); } for (Key key : toDelete()) { - // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + // TODO(ajaykannan): fix me! //mutationPb.addDelete(key.toPb()); } return mutationPb; diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseEntity.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseEntity.java index 665b4d8d24b9..8608e4fcedc9 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseEntity.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseEntity.java @@ -93,12 +93,12 @@ private B self() { protected B fill(DatastoreV1.Entity entityPb) { Map> copiedProperties = Maps.newHashMap(); for (DatastoreV1.Property property : entityPb.getPropertyList()) { - // TODO(ajaykannan): Uncomment when possible in datastore v1beta3 transition + // TODO(ajaykannan): fix me! //copiedProperties.put(property.getName(), Value.fromPb(property.getValue())); } properties(copiedProperties); if (entityPb.hasKey()) { - // TODO(ajaykannan): Uncomment when possible in datastore v1beta3 transition + // TODO(ajaykannan): fix me! //key((K) IncompleteKey.fromPb(entityPb.getKey())); } return self(); @@ -391,12 +391,12 @@ protected final DatastoreV1.Entity toPb() { for (Map.Entry> entry : properties.entrySet()) { DatastoreV1.Property.Builder propertyPb = DatastoreV1.Property.newBuilder(); propertyPb.setName(entry.getKey()); - // TODO(ajaykannan): Uncomment when possible in datastore v1beta3 transition + // TODO(ajaykannan): fix me! //propertyPb.setValue(entry.getValue().toPb()); entityPb.addProperty(propertyPb.build()); } if (key != null) { - // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + // TODO(ajaykannan): fix me! //entityPb.setKey(key.toPb()); } return entityPb.build(); diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseKey.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseKey.java index fdc973c6c4cc..2e6264af9927 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseKey.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseKey.java @@ -21,7 +21,6 @@ import static com.google.gcloud.datastore.Validator.validateNamespace; import com.google.common.base.Preconditions; -import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; import java.util.LinkedList; @@ -41,8 +40,8 @@ abstract class BaseKey extends Serializable { abstract static class Builder> { - String projectId; - String namespace; + String projectId = ""; + String namespace = ""; String kind; final List ancestors; @@ -176,15 +175,9 @@ protected com.google.datastore.v1beta3.Key toPb() { com.google.datastore.v1beta3.Key.Builder keyPb = com.google.datastore.v1beta3.Key.newBuilder(); com.google.datastore.v1beta3.PartitionId.Builder partitionIdPb = com.google.datastore.v1beta3.PartitionId.newBuilder(); - if (!Strings.isNullOrEmpty(projectId)) { - partitionIdPb.setProjectId(projectId); - } - if (!Strings.isNullOrEmpty(namespace)) { - partitionIdPb.setNamespaceId(namespace); - } - if (!partitionIdPb.getProjectId().isEmpty() || !partitionIdPb.getNamespaceId().isEmpty()) { - keyPb.setPartitionId(partitionIdPb.build()); - } + partitionIdPb.setProjectId(projectId); + partitionIdPb.setNamespaceId(namespace); + keyPb.setPartitionId(partitionIdPb.build()); for (PathElement pathEntry : path) { keyPb.addPath(pathEntry.toPb()); } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BatchImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BatchImpl.java index 95cdfbb10f01..c86ae28b3344 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BatchImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BatchImpl.java @@ -43,9 +43,9 @@ public List generatedKeys() { return Lists.transform(response.getMutationResult().getInsertAutoIdKeyList(), new Function() { @Override public Key apply(DatastoreV1.Key keyPb) { - // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + // TODO(ajaykannan): fix me! //return Key.fromPb(keyPb); - return Key.builder(null).build(); // TODO(ajaykannan): remove this line when possible + return Key.builder(null).build(); // TODO(ajaykannan): fix me! } }); } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java index 23d4f74a8da1..c21c0c2a7087 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java @@ -126,13 +126,13 @@ public List allocateId(IncompleteKey... keys) { } DatastoreV1.AllocateIdsRequest.Builder requestPb = DatastoreV1.AllocateIdsRequest.newBuilder(); for (IncompleteKey key : keys) { - // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + // TODO(ajaykannan): fix me! //requestPb.addKey(trimNameOrId(key).toPb()); } DatastoreV1.AllocateIdsResponse responsePb = allocateIds(requestPb.build()); ImmutableList.Builder keyList = ImmutableList.builder(); for (DatastoreV1.Key keyPb : responsePb.getKeyList()) { - // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + // TODO(ajaykannan): fix me! // keyList.add(Key.fromPb(keyPb)); } return keyList.build(); @@ -195,7 +195,7 @@ public List add(FullEntity... entities) { if (completeEntity != null) { responseBuilder.add(completeEntity); } else { - // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + // TODO(ajaykannan): fix me! //responseBuilder.add(Entity.builder(Key.fromPb(allocatedKeys.next()), entity).build()); } } @@ -226,7 +226,7 @@ Iterator get(DatastoreV1.ReadOptions readOptionsPb, final Key... keys) { requestPb.setReadOptions(readOptionsPb); } for (Key k : Sets.newLinkedHashSet(Arrays.asList(keys))) { - // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + // TODO(ajaykannan): fix me! //requestPb.addKey(k.toPb()); } return new ResultsIterator(requestPb); @@ -314,7 +314,7 @@ public void delete(Key... keys) { DatastoreV1.Mutation.Builder mutationPb = DatastoreV1.Mutation.newBuilder(); Set dedupKeys = new LinkedHashSet<>(Arrays.asList(keys)); for (Key key : dedupKeys) { - // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + // TODO(ajaykannan): fix me! //mutationPb.addDelete(key.toPb()); } commitMutation(mutationPb); diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DateTime.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DateTime.java index 0d5c99d2b3fd..eca352f5ba0c 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DateTime.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DateTime.java @@ -32,7 +32,7 @@ * @see Google Cloud Datastore * Entities, Properties, and Keys */ -public final class DateTime extends Serializable +public final class DateTime extends Serializable implements Comparable { private static final long serialVersionUID = 7343324797621228378L; @@ -96,24 +96,23 @@ public static DateTime copyFrom(Calendar calendar) { } @Override - protected com.google.datastore.v1beta3.Value toPb() { - return com.google.datastore.v1beta3.Value.newBuilder() - .setTimestampValue(microsecondsToTimestampPb(timestampMicroseconds)).build(); + protected com.google.protobuf.Timestamp toPb() { + return microsecondsToTimestampPb(timestampMicroseconds); } @Override protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException { - return new DateTime(timestampPbToMicroseconds(com.google.datastore.v1beta3.Value - .parseFrom(bytesPb).getTimestampValue())); + return new DateTime(timestampPbToMicroseconds( + com.google.protobuf.Timestamp.parseFrom(bytesPb))); } protected static long timestampPbToMicroseconds(com.google.protobuf.Timestamp timestampPb) { - return timestampPb.getSeconds() * 10^6 + timestampPb.getNanos() / 10^3; + return timestampPb.getSeconds() * 1000000 + timestampPb.getNanos() / 1000; } protected static com.google.protobuf.Timestamp microsecondsToTimestampPb(long microseconds) { - long seconds = microseconds / 10^6; - int nanos = (int) (microseconds % 10^6) * 10^3; + long seconds = microseconds / 1000000; + int nanos = (int) (microseconds % 1000000) * 1000; return com.google.protobuf.Timestamp.newBuilder().setSeconds(seconds).setNanos(nanos).build(); } } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DoubleValue.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DoubleValue.java index cf543ad94b78..d7409d08fe63 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DoubleValue.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DoubleValue.java @@ -18,8 +18,6 @@ import static com.google.datastore.v1beta3.Value.DOUBLE_VALUE_FIELD_NUMBER; -import com.google.api.services.datastore.DatastoreV1; - public final class DoubleValue extends Value { private static final long serialVersionUID = -5096238337676649540L; @@ -43,7 +41,7 @@ public Builder newBuilder(Double value) { protected Double getValue(com.google.datastore.v1beta3.Value from) { return from.getDoubleValue(); } - + @Override protected void setValue(DoubleValue from, com.google.datastore.v1beta3.Value.Builder to) { to.setDoubleValue(from.get()); diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/EntityValue.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/EntityValue.java index 3105f9fa0dd9..16ca55aec4a6 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/EntityValue.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/EntityValue.java @@ -39,14 +39,14 @@ public Builder newBuilder(FullEntity value) { @Override protected FullEntity getValue(com.google.datastore.v1beta3.Value from) { - // TODO(ajaykannan): uncomment this line when possible in datastore v1beta3 transition + // TODO(ajaykannan): fix me! //return FullEntity.fromPb(from.getEntityValue()); - return null; // TODO(ajaykannan): remove this line when possible + return null; // TODO(ajaykannan): fix me! } @Override protected void setValue(EntityValue from, com.google.datastore.v1beta3.Value.Builder to) { - // TODO(ajaykannan): uncomment this line when possible in datastore v1beta3 transition + // TODO(ajaykannan): fix me! //to.setEntityValue(from.get().toPb()); } }; diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java index ff659805a34d..54110a89c3e0 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java @@ -135,7 +135,7 @@ protected DatastoreV1.GqlQueryArg toPb() { argPb.setCursor(cursor.byteString()); } if (value != null) { - // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + // TODO(ajaykannan): fix me! //argPb.setValue(value.toPb()); } return argPb.build(); @@ -151,9 +151,9 @@ static Binding fromPb(DatastoreV1.GqlQueryArg argPb) { if (argPb.hasCursor()) { return new Binding(name, new Cursor(argPb.getCursor())); } - // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + // TODO(ajaykannan): fix me! //return new Binding(name, Value.fromPb(argPb.getValue())); - return new Binding(name, new Cursor(null)); // TODO(ajaykannan): remove this line when possible + return new Binding(name, new Cursor(null)); // TODO(ajaykannan): fix me! } } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/IncompleteKey.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/IncompleteKey.java index 0eabefe32643..367bdbafed71 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/IncompleteKey.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/IncompleteKey.java @@ -58,18 +58,12 @@ protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException { } static IncompleteKey fromPb(com.google.datastore.v1beta3.Key keyPb) { - String projectId = null; - String namespace = null; + String projectId = ""; + String namespace = ""; if (keyPb.hasPartitionId()) { com.google.datastore.v1beta3.PartitionId partitionIdPb = keyPb.getPartitionId(); projectId = partitionIdPb.getProjectId(); - if (projectId.isEmpty()) { - projectId = null; - } namespace = partitionIdPb.getNamespaceId(); - if (namespace.isEmpty()) { - namespace = null; - } } List pathElementsPb = keyPb.getPathList(); Preconditions.checkArgument(!pathElementsPb.isEmpty(), "Path must not be empty"); diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/KeyFactory.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/KeyFactory.java index 28f852ed5355..c5b9d77a8cd7 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/KeyFactory.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/KeyFactory.java @@ -16,6 +16,7 @@ package com.google.gcloud.datastore; +import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableList; /** @@ -35,7 +36,7 @@ public KeyFactory(String projectId, String namespace) { super(projectId); namespace(namespace); this.pi = projectId; - this.ns = namespace; + this.ns = MoreObjects.firstNonNull(namespace, ""); } public IncompleteKey newKey() { diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/NullValue.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/NullValue.java index 173ee3ef79f0..ed314200b8bb 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/NullValue.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/NullValue.java @@ -18,6 +18,8 @@ import static com.google.common.base.Preconditions.checkArgument; +import static com.google.datastore.v1beta3.Value.NULL_VALUE_FIELD_NUMBER; + public final class NullValue extends Value { private static final long serialVersionUID = 8497300779013002270L; @@ -34,7 +36,7 @@ public Builder newBuilder(Void value) { @Override public int getProtoFieldId() { - return 0; + return NULL_VALUE_FIELD_NUMBER; } @Override @@ -44,11 +46,12 @@ protected Void getValue(com.google.datastore.v1beta3.Value from) { @Override protected void setValue(NullValue from, com.google.datastore.v1beta3.Value.Builder to) { - // nothing to set + to.setNullValue(com.google.protobuf.NullValue.NULL_VALUE); } }; - public static final class Builder extends Value.BaseBuilder { + public static final class Builder + extends Value.BaseBuilder { private Builder() { super(ValueType.NULL); diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/PathElement.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/PathElement.java index c0196a6326fe..b0eb9fd83855 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/PathElement.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/PathElement.java @@ -86,7 +86,8 @@ public boolean equals(Object obj) { @Override protected com.google.datastore.v1beta3.Key.PathElement toPb() { - com.google.datastore.v1beta3.Key.PathElement.Builder pathElementPb = com.google.datastore.v1beta3.Key.PathElement.newBuilder(); + com.google.datastore.v1beta3.Key.PathElement.Builder pathElementPb = + com.google.datastore.v1beta3.Key.PathElement.newBuilder(); pathElementPb.setKind(kind); if (id != null) { pathElementPb.setId(id); @@ -103,15 +104,14 @@ protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException { static PathElement fromPb(com.google.datastore.v1beta3.Key.PathElement pathElementPb) { String kind = pathElementPb.getKind(); - if (pathElementPb.getIdTypeCase() == - com.google.datastore.v1beta3.Key.PathElement.IdTypeCase.ID) { - return of(kind, pathElementPb.getId()); + switch (pathElementPb.getIdTypeCase()) { + case ID: + return of(kind, pathElementPb.getId()); + case NAME: + return of(kind, pathElementPb.getName()); + default: + return of(kind); } - if (pathElementPb.getIdTypeCase() == - com.google.datastore.v1beta3.Key.PathElement.IdTypeCase.NAME) { - return of(kind, pathElementPb.getName()); - } - return of(kind); } static PathElement of(String kind) { diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java index fdd120ccbe85..f3dd254ce9a3 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java @@ -66,7 +66,7 @@ public abstract static class ResultType implements java.io.Serializable { if (!entityPb.hasKey()) { return null; } - //TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + //TODO(ajaykannan): fix me! //return Key.fromPb(entityPb.getKey()); } return ProjectionEntity.fromPb(entityPb); @@ -89,9 +89,9 @@ public abstract static class ResultType implements java.io.Serializable { private static final long serialVersionUID = -8514289244104446252L; @Override protected Key convert(DatastoreV1.Entity entityPb) { - //TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + //TODO(ajaykannan): fix me! //return Key.fromPb(entityPb.getKey()); - return Key.builder(null).build(); // TODO(ajaykannan): remove this line when possible + return Key.builder(null).build(); // TODO(ajaykannan): fix me! } }; diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/RawValue.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/RawValue.java index d60f215cd5f2..cd64bcdeff05 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/RawValue.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/RawValue.java @@ -36,7 +36,8 @@ public int getProtoFieldId() { } @Override - protected com.google.datastore.v1beta3.Value getValue(com.google.datastore.v1beta3.Value from) { + protected com.google.datastore.v1beta3.Value getValue( + com.google.datastore.v1beta3.Value from) { return from; } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java index b034b15995a3..1dd35cb4f191 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java @@ -228,10 +228,10 @@ private PropertyFilter(String property, Operator operator, Value value) { public static PropertyFilter fromPb(DatastoreV1.PropertyFilter propertyFilterPb) { String property = propertyFilterPb.getProperty().getName(); Operator operator = Operator.fromPb(propertyFilterPb.getOperator()); - // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + // TODO(ajaykannan): fix me! //Value value = Value.fromPb(propertyFilterPb.getValue()); //return new PropertyFilter(property, operator, value); - return new PropertyFilter(property, operator, null); // TODO(ajaykannan): remove this line when possible + return new PropertyFilter(property, operator, null); // TODO(ajaykannan): fix me! } @Override @@ -437,7 +437,7 @@ protected DatastoreV1.Filter toPb() { propertyFilterPb.getPropertyBuilder().setName(property); propertyFilterPb.setOperator(operator.toPb()); if (value != null) { - // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + // TODO(ajaykannan): fix me! //propertyFilterPb.setValue(value.toPb()); } return filterPb.build(); diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionImpl.java index 62978ce73be6..ae9d0e50f0d7 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionImpl.java @@ -47,9 +47,9 @@ public List generatedKeys() { return Lists.transform(response.getMutationResult().getInsertAutoIdKeyList(), new Function() { @Override public Key apply(DatastoreV1.Key keyPb) { - // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition + // TODO(ajaykannan): fix me! //return Key.fromPb(keyPb); - return Key.builder(null).build(); //: TODO(ajaykannan) remove this placeholder line + return Key.builder(null).build(); // TODO(ajaykannan): fix me! } }); } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Validator.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Validator.java index 09a3fa7defcc..791adec53101 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Validator.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Validator.java @@ -45,13 +45,13 @@ static String validateDatabase(String projectId) { } static String validateNamespace(String namespace) { - if (namespace != null) { - checkArgument(!namespace.isEmpty(), "namespace must not be an empty string"); - checkArgument(namespace.length() <= MAX_NAMESPACE_LENGTH, - "namespace must not contain more than 100 characters"); - checkArgument(NAMESPACE_PATTERN.matcher(namespace).matches(), - "namespace must the following pattern: " + NAMESPACE_PATTERN.pattern()); + if (Strings.isNullOrEmpty(namespace)) { + return ""; } + checkArgument(namespace.length() <= MAX_NAMESPACE_LENGTH, + "namespace must not contain more than 100 characters"); + checkArgument(NAMESPACE_PATTERN.matcher(namespace).matches(), + "namespace must the following pattern: " + NAMESPACE_PATTERN.pattern()); return namespace; } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Value.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Value.java index 94e1d7de545f..9e4aae0fb6f5 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Value.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Value.java @@ -18,10 +18,9 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.protobuf.Descriptors.FieldDescriptor; +import com.google.datastore.v1beta3.Value.ValueTypeCase; import com.google.protobuf.InvalidProtocolBufferException; -import java.util.Map.Entry; import java.util.Objects; /** @@ -64,7 +63,8 @@ public final B fromProto(com.google.datastore.v1beta3.Value proto) { @SuppressWarnings("deprecation") @Override public final com.google.datastore.v1beta3.Value toProto(P value) { - com.google.datastore.v1beta3.Value.Builder builder = com.google.datastore.v1beta3.Value.newBuilder(); + com.google.datastore.v1beta3.Value.Builder builder = + com.google.datastore.v1beta3.Value.newBuilder(); builder.setExcludeFromIndexes(value.excludeFromIndexes()); builder.setMeaning(value.meaning()); setValue(value, builder); @@ -160,7 +160,7 @@ public final boolean excludeFromIndexes() { } @Deprecated - public final int meaning() { + final int meaning() { return meaning; } @@ -198,18 +198,10 @@ protected com.google.datastore.v1beta3.Value toPb() { } static Value fromPb(com.google.datastore.v1beta3.Value proto) { - for (Entry entry : proto.getAllFields().entrySet()) { - FieldDescriptor descriptor = entry.getKey(); - if (descriptor.getName().endsWith("_value")) { - ValueType valueType = ValueType.getByDescriptorId(descriptor.getNumber()); - if (valueType == null) { - // unsupported type - return RawValue.MARSHALLER.fromProto(proto).build(); - } - return valueType.getMarshaller().fromProto(proto).build(); - } - } - return NullValue.MARSHALLER.fromProto(proto).build(); + ValueTypeCase descriptorId = proto.getValueTypeCase(); + ValueType valueType = ValueType.getByDescriptorId(descriptorId.getNumber()); + return valueType == null ? RawValue.MARSHALLER.fromProto(proto).build() + : valueType.getMarshaller().fromProto(proto).build(); } @Override diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ValueType.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ValueType.java index b09583103a59..20c89a86e7a0 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ValueType.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ValueType.java @@ -80,6 +80,10 @@ public enum ValueType { */ RAW_VALUE(RawValue.MARSHALLER); + /** + * TODO(ajaykannan): add GEO_POINT_VALUE + * Will represent a geolocation value in latitude/longitude + */ private static final ImmutableMap DESCRIPTOR_TO_TYPE_MAP; diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseDatastoreBatchWriterTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseDatastoreBatchWriterTest.java index 28fea360ecf4..43df45d7a04e 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseDatastoreBatchWriterTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseDatastoreBatchWriterTest.java @@ -80,6 +80,8 @@ public void tearDown() { batchWriter.finish(); } + // TODO(ajaykannan): fix me! + /* @Test public void testAdd() throws Exception { Entity entity2 = @@ -98,6 +100,7 @@ public void testAdd() throws Exception { assertEquals(Entity.builder(KEY3, INCOMPLETE_ENTITY_2).build(), entities.get(2)); assertEquals(entity2, entities.get(3)); } + */ @Test public void testAddAfterDelete() throws Exception { @@ -133,6 +136,8 @@ public void testAddWhenNotActive() throws Exception { batchWriter.add(ENTITY1); } + // TODO(ajaykannan): fix me! + /* @Test public void testAddWithDeferredAllocation() throws Exception { DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() @@ -195,6 +200,7 @@ public void testUpdateAfterPut() throws Exception { batchWriter.update(entity); assertEquals(pb, batchWriter.toMutationPb().build()); } + */ @Test(expected = DatastoreException.class) public void testUpdateAfterDelete() throws Exception { @@ -208,6 +214,8 @@ public void testUpdateWhenNotActive() throws Exception { batchWriter.update(ENTITY1); } + // TODO(ajaykannan): fix me! + /* @Test public void testPut() throws Exception { DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() @@ -263,20 +271,21 @@ public void testPutAfterDelete() throws Exception { batchWriter.put(entity); assertEquals(pb, batchWriter.toMutationPb().build()); } + */ @Test(expected = DatastoreException.class) public void testPutWhenNotActive() throws Exception { batchWriter.deactivate(); batchWriter.put(ENTITY1); } - + // TODO(ajaykannan): fix me! + /* @Test public void testDelete() throws Exception { - // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() - //.addDelete(KEY1.toPb()) - //.addDelete(KEY2.toPb()) - //.addDelete(KEY3.toPb()) + .addDelete(KEY1.toPb()) + .addDelete(KEY2.toPb()) + .addDelete(KEY3.toPb()) .build(); batchWriter.delete(KEY1, KEY2); batchWriter.delete(KEY3); @@ -285,10 +294,9 @@ public void testDelete() throws Exception { @Test public void testDeleteAfterAdd() throws Exception { - // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() .addInsertAutoId(INCOMPLETE_ENTITY_1.toPb()) - //.addDelete(KEY1.toPb()) + .addDelete(KEY1.toPb()) .build(); batchWriter.add(ENTITY1); batchWriter.addWithDeferredIdAllocation(INCOMPLETE_ENTITY_1); @@ -296,11 +304,11 @@ public void testDeleteAfterAdd() throws Exception { assertEquals(pb, batchWriter.toMutationPb().build()); } + @Test public void testDeleteAfterUpdate() throws Exception { - // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() - //.addDelete(KEY1.toPb()) + .addDelete(KEY1.toPb()) .build(); batchWriter.update(ENTITY1); batchWriter.delete(KEY1); @@ -309,14 +317,14 @@ public void testDeleteAfterUpdate() throws Exception { @Test public void testDeleteAfterPut() throws Exception { - // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() - //.addDelete(KEY1.toPb()) + .addDelete(KEY1.toPb()) .build(); batchWriter.put(ENTITY1); batchWriter.delete(KEY1); assertEquals(pb, batchWriter.toMutationPb().build()); } + */ @Test(expected = DatastoreException.class) public void testDeleteWhenNotActive() throws Exception { diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseKeyTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseKeyTest.java index e99e7a60fd0b..79576748bb58 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseKeyTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseKeyTest.java @@ -78,7 +78,8 @@ public void testBadDatasetInSetter() throws Exception { public void testNamespace() throws Exception { Builder builder = new Builder("ds", "k"); BaseKey key = builder.build(); - assertNull(key.namespace()); + assertTrue(key.namespace() != null); + assertTrue(key.namespace().isEmpty()); key = builder.namespace("ns").build(); assertEquals("ns", key.namespace()); } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index 65872abb79e7..57342ed3e26d 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -14,6 +14,8 @@ * limitations under the License. */ +// TODO(ajaykannan): fix me! +/* package com.google.gcloud.datastore; import static org.junit.Assert.assertEquals; @@ -637,19 +639,17 @@ public void testKeyFactory() { @Test public void testRetires() throws Exception { - // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition - //DatastoreV1.LookupRequest requestPb = - // DatastoreV1.LookupRequest.newBuilder().addKey(KEY1.toPb()).build(); + DatastoreV1.LookupRequest requestPb = + DatastoreV1.LookupRequest.newBuilder().addKey(KEY1.toPb()).build(); DatastoreV1.LookupResponse responsePb = DatastoreV1.LookupResponse.newBuilder() .addFound(EntityResult.newBuilder().setEntity(ENTITY1.toPb())).build(); DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class); DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class); EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) .andReturn(rpcMock); - // TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition - //EasyMock.expect(rpcMock.lookup(requestPb)) - // .andThrow(new DatastoreRpc.DatastoreRpcException(Reason.UNAVAILABLE)) - // .andReturn(responsePb); + EasyMock.expect(rpcMock.lookup(requestPb)) + .andThrow(new DatastoreRpc.DatastoreRpcException(Reason.UNAVAILABLE)) + .andReturn(responsePb); EasyMock.replay(rpcFactoryMock, rpcMock); DatastoreOptions options = this.options.toBuilder() .retryParams(RetryParams.getDefaultInstance()) @@ -661,3 +661,4 @@ public void testRetires() throws Exception { EasyMock.verify(rpcFactoryMock, rpcMock); } } +*/ diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/KeyFactoryTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/KeyFactoryTest.java index 92851bd87efe..7acf8abbbf19 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/KeyFactoryTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/KeyFactoryTest.java @@ -55,7 +55,7 @@ public void testReset() { key = keyFactory.newKey(); assertEquals("k1", key.kind()); assertEquals(PROJECT_ID, key.projectId()); - assertNull(key.namespace()); + assertTrue(key.namespace().isEmpty()); assertTrue(key.ancestors().isEmpty()); keyFactory = new KeyFactory(PROJECT_ID, "ns1").kind("k"); @@ -75,9 +75,9 @@ public void testReset() { @Test public void testNewKey() throws Exception { Key key = keyFactory.newKey(1); - verifyKey(key, 1L, null); + verifyKey(key, 1L, ""); key = keyFactory.newKey("n"); - verifyKey(key, "n", null); + verifyKey(key, "n", ""); PathElement p1 = PathElement.of("k1", "n"); PathElement p2 = PathElement.of("k2", 10); key = keyFactory.namespace("ns").ancestors(p1, p2).newKey("k3"); @@ -87,7 +87,7 @@ public void testNewKey() throws Exception { @Test public void testNewIncompleteKey() throws Exception { IncompleteKey key = keyFactory.newKey(); - verifyIncompleteKey(key, null); + verifyIncompleteKey(key, ""); PathElement p1 = PathElement.of("k1", "n"); PathElement p2 = PathElement.of("k2", 10); key = keyFactory.namespace("ns").ancestors(p1, p2).newKey(); diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/ListValueTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/ListValueTest.java index 23a9325f22de..74b74facfe45 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/ListValueTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/ListValueTest.java @@ -28,7 +28,7 @@ public class ListValueTest { - private static final List> CONTENT = ImmutableList.of(NullValue.of(), StringValue.of("foo")); + private static final List> CONTENT = ImmutableList.of(NullValue.of(), StringValue.of("foo")); @Test public void testToBuilder() throws Exception { diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java index 9dfdd33c873d..624689cd420e 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java @@ -88,8 +88,11 @@ public class SerializationTest { private static final BooleanValue BOOLEAN_VALUE = BooleanValue.of(true); private static final DateTimeValue DATE_AND_TIME_VALUE = DateTimeValue.of(DateTime.now()); private static final BlobValue BLOB_VALUE = BlobValue.of(BLOB1); - private static final RawValue RAW_VALUE = RawValue.of(com.google.datastore.v1beta3.Value - .newBuilder().setStringValue("blob-key").setMeaning(18).build()); + private static final RawValue RAW_VALUE = + RawValue.of(com.google.datastore.v1beta3.Value.newBuilder() + .setGeoPointValue(com.google.type.LatLng.newBuilder() + .setLatitude(0.0).setLongitude(0.0).build()) + .setMeaning(18).build()); private static final Entity ENTITY1 = Entity.builder(KEY1).build(); private static final Entity ENTITY2 = Entity.builder(KEY2).set("null", NullValue.of()).build(); @@ -119,8 +122,9 @@ public class SerializationTest { .put(ValueType.NULL, NULL_VALUE) .put(ValueType.KEY, KEY_VALUE) .put(ValueType.STRING, STRING_VALUE) - .putAll(ValueType.ENTITY, EMBEDDED_ENTITY_VALUE1, EMBEDDED_ENTITY_VALUE2, - EMBEDDED_ENTITY_VALUE3) + // TODO(ajaykannan): fix me! + //.putAll(ValueType.ENTITY, EMBEDDED_ENTITY_VALUE1, EMBEDDED_ENTITY_VALUE2, + // EMBEDDED_ENTITY_VALUE3) .put(ValueType.LIST, LIST_VALUE) .put(ValueType.LONG, LONG_VALUE) .put(ValueType.DOUBLE, DOUBLE_VALUE) @@ -166,9 +170,10 @@ public void testValues() throws Exception { @Test public void testTypes() throws Exception { - Serializable[] types = { KEY1, KEY2, INCOMPLETE_KEY1, INCOMPLETE_KEY2, ENTITY1, ENTITY2, - ENTITY3, EMBEDDED_ENTITY, PROJECTION_ENTITY, DATE_TIME1, BLOB1, CURSOR1, GQL1, GQL2, - QUERY1, QUERY2, QUERY3}; + // TODO(ajaykannan): fix me! + Serializable[] types = { KEY1, KEY2, INCOMPLETE_KEY1, INCOMPLETE_KEY2, /*ENTITY1, ENTITY2, + ENTITY3, EMBEDDED_ENTITY, PROJECTION_ENTITY,*/ DATE_TIME1, BLOB1/*, CURSOR1, GQL1, GQL2, + QUERY1, QUERY2, QUERY3*/}; for (Serializable obj : types) { Object copy = serializeAndDeserialize(obj); assertEquals(obj, obj); From e0784699b1320f36b7df456666ea52e656f31d5b Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Tue, 22 Sep 2015 18:42:33 -0700 Subject: [PATCH 03/60] Updates to write-related code for datastore v1beta3 update --- .../datastore/BaseDatastoreBatchWriter.java | 34 ++-- .../google/gcloud/datastore/BaseEntity.java | 32 ++-- .../google/gcloud/datastore/BatchImpl.java | 54 +++--- .../google/gcloud/datastore/BatchOption.java | 58 ------ .../google/gcloud/datastore/Datastore.java | 7 +- .../gcloud/datastore/DatastoreHelper.java | 5 +- .../gcloud/datastore/DatastoreImpl.java | 165 ++++++++++-------- .../gcloud/datastore/DatastoreOptions.java | 26 +-- .../com/google/gcloud/datastore/Entity.java | 3 +- .../google/gcloud/datastore/EntityValue.java | 7 +- .../google/gcloud/datastore/FullEntity.java | 4 +- .../gcloud/datastore/ProjectionEntity.java | 3 +- .../com/google/gcloud/datastore/Query.java | 12 +- .../gcloud/datastore/QueryResultsImpl.java | 3 +- .../gcloud/datastore/TransactionImpl.java | 65 +++---- .../gcloud/datastore/TransactionOption.java | 114 ------------ .../com/google/gcloud/spi/DatastoreRpc.java | 31 ++-- .../gcloud/spi/DefaultDatastoreRpc.java | 94 +++++----- .../BaseDatastoreBatchWriterTest.java | 156 +++++++---------- .../google/gcloud/datastore/BaseKeyTest.java | 1 - .../datastore/DatastoreOptionsTest.java | 2 +- .../gcloud/datastore/DatastoreTest.java | 30 ++-- .../gcloud/datastore/KeyFactoryTest.java | 1 - .../gcloud/datastore/LocalGcdHelper.java | 12 +- 24 files changed, 349 insertions(+), 570 deletions(-) delete mode 100644 gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BatchOption.java delete mode 100644 gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionOption.java diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseDatastoreBatchWriter.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseDatastoreBatchWriter.java index d94eafbb42f6..6d8ac86834de 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseDatastoreBatchWriter.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseDatastoreBatchWriter.java @@ -16,11 +16,11 @@ package com.google.gcloud.datastore; -import com.google.api.services.datastore.DatastoreV1; import com.google.common.base.Preconditions; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; +import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashMap; @@ -88,9 +88,7 @@ public final List add(FullEntity... entities) { for (FullEntity entity : entities) { IncompleteKey key = entity.key(); Preconditions.checkArgument(key != null, "Entity must have a key"); - if (key instanceof Key) { - addInternal((FullEntity) entity); - } else { + if (!(key instanceof Key)) { incompleteKeys.add(key); } } @@ -104,6 +102,7 @@ public final List add(FullEntity... entities) { List answer = Lists.newArrayListWithExpectedSize(entities.length); for (FullEntity entity : entities) { if (entity.key() instanceof Key) { + addInternal((FullEntity) entity); answer.add(Entity.convert((FullEntity) entity)); } else { Entity entityWithAllocatedId = Entity.builder(allocated.next(), entity).build(); @@ -185,6 +184,10 @@ protected Set toDelete() { return toDelete; } + protected int numAutoAllocatedIds() { + return toAddAutoId.size(); + } + protected void deactivate() { active = false; } @@ -199,25 +202,30 @@ protected DatastoreException newInvalidRequest(String msg, Object... params) { return DatastoreException.throwInvalidRequest(String.format(msg, params)); } - protected DatastoreV1.Mutation.Builder toMutationPb() { - DatastoreV1.Mutation.Builder mutationPb = DatastoreV1.Mutation.newBuilder(); + protected List toMutationPbList() { + List mutationsPb = + new ArrayList<>(); for (FullEntity entity : toAddAutoId()) { - mutationPb.addInsertAutoId(entity.toPb()); + mutationsPb.add( + com.google.datastore.v1beta3.Mutation.newBuilder().setInsert(entity.toPb()).build()); } for (FullEntity entity : toAdd().values()) { - mutationPb.addInsert(entity.toPb()); + mutationsPb.add( + com.google.datastore.v1beta3.Mutation.newBuilder().setInsert(entity.toPb()).build()); } for (FullEntity entity : toUpdate().values()) { - mutationPb.addUpdate(entity.toPb()); + mutationsPb.add( + com.google.datastore.v1beta3.Mutation.newBuilder().setUpdate(entity.toPb()).build()); } for (FullEntity entity : toPut().values()) { - mutationPb.addUpsert(entity.toPb()); + mutationsPb.add( + com.google.datastore.v1beta3.Mutation.newBuilder().setUpsert(entity.toPb()).build()); } for (Key key : toDelete()) { - // TODO(ajaykannan): fix me! - //mutationPb.addDelete(key.toPb()); + mutationsPb.add( + com.google.datastore.v1beta3.Mutation.newBuilder().setDelete(key.toPb()).build()); } - return mutationPb; + return mutationsPb; } protected abstract Datastore datastore(); diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseEntity.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseEntity.java index 8608e4fcedc9..97718e708e83 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseEntity.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseEntity.java @@ -27,7 +27,6 @@ import static com.google.gcloud.datastore.NullValue.of; import static com.google.gcloud.datastore.StringValue.of; -import com.google.api.services.datastore.DatastoreV1; import com.google.common.collect.ImmutableSortedMap; import com.google.common.collect.Maps; import com.google.protobuf.InvalidProtocolBufferException; @@ -48,7 +47,8 @@ * @see Google Cloud Datastore * Entities, Properties, and Keys */ -public abstract class BaseEntity extends Serializable { +public abstract class BaseEntity + extends Serializable { private static final long serialVersionUID = 8175618724683792766L; @@ -90,16 +90,15 @@ private B self() { } @SuppressWarnings("unchecked") - protected B fill(DatastoreV1.Entity entityPb) { + protected B fill(com.google.datastore.v1beta3.Entity entityPb) { Map> copiedProperties = Maps.newHashMap(); - for (DatastoreV1.Property property : entityPb.getPropertyList()) { - // TODO(ajaykannan): fix me! - //copiedProperties.put(property.getName(), Value.fromPb(property.getValue())); + for (Map.Entry entry : + entityPb.getProperties().entrySet()) { + copiedProperties.put(entry.getKey(), Value.fromPb(entry.getValue())); } properties(copiedProperties); if (entityPb.hasKey()) { - // TODO(ajaykannan): fix me! - //key((K) IncompleteKey.fromPb(entityPb.getKey())); + key((K) IncompleteKey.fromPb(entityPb.getKey())); } return self(); } @@ -379,25 +378,22 @@ ImmutableSortedMap> properties() { @Override protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException { Builder builder = emptyBuilder(); - builder.fill(DatastoreV1.Entity.parseFrom(bytesPb)); + builder.fill(com.google.datastore.v1beta3.Entity.parseFrom(bytesPb)); return builder.build(); } protected abstract Builder emptyBuilder(); @Override - protected final DatastoreV1.Entity toPb() { - DatastoreV1.Entity.Builder entityPb = DatastoreV1.Entity.newBuilder(); + protected final com.google.datastore.v1beta3.Entity toPb() { + com.google.datastore.v1beta3.Entity.Builder entityPb = + com.google.datastore.v1beta3.Entity.newBuilder(); + Map propertiesPb = entityPb.getMutableProperties(); for (Map.Entry> entry : properties.entrySet()) { - DatastoreV1.Property.Builder propertyPb = DatastoreV1.Property.newBuilder(); - propertyPb.setName(entry.getKey()); - // TODO(ajaykannan): fix me! - //propertyPb.setValue(entry.getValue().toPb()); - entityPb.addProperty(propertyPb.build()); + propertiesPb.put(entry.getKey(), entry.getValue().toPb()); } if (key != null) { - // TODO(ajaykannan): fix me! - //entityPb.setKey(key.toPb()); + entityPb.setKey(key.toPb()); } return entityPb.build(); } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BatchImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BatchImpl.java index c86ae28b3344..a9eab9e9c3f1 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BatchImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BatchImpl.java @@ -16,65 +16,53 @@ package com.google.gcloud.datastore; -import com.google.api.services.datastore.DatastoreV1; -import com.google.common.base.Function; -import com.google.common.collect.Lists; -import com.google.gcloud.datastore.BatchOption.ForceWrites; - +import java.util.Iterator; +import java.util.LinkedList; import java.util.List; -import java.util.Map; class BatchImpl extends BaseDatastoreBatchWriter implements Batch { private final DatastoreImpl datastore; - private final boolean force; static class ResponseImpl implements Batch.Response { - private final DatastoreV1.CommitResponse response; + private final com.google.datastore.v1beta3.CommitResponse response; + private final int numAutoAllocatedIds; - ResponseImpl(DatastoreV1.CommitResponse response) { + ResponseImpl(com.google.datastore.v1beta3.CommitResponse response, int numAutoAllocatedIds) { this.response = response; + this.numAutoAllocatedIds = numAutoAllocatedIds; } @Override public List generatedKeys() { - return Lists.transform(response.getMutationResult().getInsertAutoIdKeyList(), - new Function() { - @Override public Key apply(DatastoreV1.Key keyPb) { - // TODO(ajaykannan): fix me! - //return Key.fromPb(keyPb); - return Key.builder(null).build(); // TODO(ajaykannan): fix me! - } - }); + Iterator results = + response.getMutationResultsList().iterator(); + List generated = new LinkedList(); + for (int i = 0; i < numAutoAllocatedIds; i++) { + generated.add(Key.fromPb(results.next().getKey())); + } + return generated; } } - BatchImpl(DatastoreImpl datastore, BatchOption... options) { + BatchImpl(DatastoreImpl datastore) { super("batch"); this.datastore = datastore; - Map, BatchOption> optionsMap = BatchOption.asImmutableMap(options); - if (optionsMap.containsKey(ForceWrites.class)) { - force = ((ForceWrites) optionsMap.get(ForceWrites.class)).force(); - } else { - force = datastore.options().force(); - } } @Override public Batch.Response submit() { validateActive(); - DatastoreV1.Mutation.Builder mutationPb = toMutationPb(); - if (force) { - mutationPb.setForce(force); - } - DatastoreV1.CommitRequest.Builder requestPb = DatastoreV1.CommitRequest.newBuilder(); - requestPb.setMode(DatastoreV1.CommitRequest.Mode.NON_TRANSACTIONAL); - requestPb.setMutation(mutationPb); - DatastoreV1.CommitResponse responsePb = datastore.commit(requestPb.build()); + List mutationsPb = toMutationPbList(); + com.google.datastore.v1beta3.CommitRequest.Builder requestPb = + com.google.datastore.v1beta3.CommitRequest.newBuilder(); + requestPb.setMode(com.google.datastore.v1beta3.CommitRequest.Mode.NON_TRANSACTIONAL); + requestPb.addAllMutations(mutationsPb); + com.google.datastore.v1beta3.CommitResponse responsePb = datastore.commit(requestPb.build()); deactivate(); - return new ResponseImpl(responsePb); + return new ResponseImpl(responsePb, numAutoAllocatedIds()); } @Override diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BatchOption.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BatchOption.java deleted file mode 100644 index 362a74e96c79..000000000000 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BatchOption.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2015 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.datastore; - -import com.google.common.collect.ImmutableMap; - -import java.util.Map; - -public abstract class BatchOption implements java.io.Serializable { - - private static final long serialVersionUID = -3932758377282659839L; - - public static final class ForceWrites extends BatchOption { - - private static final long serialVersionUID = 2555054296046232799L; - - private final boolean force; - - public ForceWrites(boolean force) { - this.force = force; - } - - public boolean force() { - return force; - } - } - - BatchOption() { - // package protected - } - - public static ForceWrites forceWrites() { - return new ForceWrites(true); - } - - static Map, BatchOption> asImmutableMap(BatchOption... options) { - ImmutableMap.Builder, BatchOption> builder = - ImmutableMap.builder(); - for (BatchOption option : options) { - builder.put(option.getClass(), option); - } - return builder.build(); - } -} diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Datastore.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Datastore.java index 870ed8d9474f..b49db1cacdfe 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Datastore.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Datastore.java @@ -30,7 +30,7 @@ public interface Datastore extends Service, DatastoreReaderWri * * @throws DatastoreException upon failure */ - Transaction newTransaction(TransactionOption... options); + Transaction newTransaction(); /** @@ -54,15 +54,14 @@ interface TransactionCallable { * as a {@link DatastoreException} with the original exception as its root cause. * * @param callable the callback to call with a newly created transactional readerWriter - * @param options the options for the created transaction * @throws DatastoreException upon failure */ - T runInTransaction(TransactionCallable callable, TransactionOption... options); + T runInTransaction(TransactionCallable callable); /** * Returns a new Batch for processing multiple write operations in one request. */ - Batch newBatch(BatchOption... options); + Batch newBatch(); /** * Allocate a unique id for the given key. diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreHelper.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreHelper.java index a74d06642740..5ea0f755d2f8 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreHelper.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreHelper.java @@ -69,9 +69,8 @@ static List fetch(DatastoreReader reader, Key... keys) { return list; } - static T runInTransaction(Datastore datastore, - Datastore.TransactionCallable callable, TransactionOption... options) { - Transaction transaction = datastore.newTransaction(options); + static T runInTransaction(Datastore datastore, Datastore.TransactionCallable callable) { + Transaction transaction = datastore.newTransaction(); try { T value = callable.run(transaction); transaction.commit(); diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java index c21c0c2a7087..262098e82f96 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java @@ -16,7 +16,6 @@ package com.google.gcloud.datastore; -import com.google.api.services.datastore.DatastoreV1; import com.google.common.base.MoreObjects; import com.google.common.base.Preconditions; import com.google.common.collect.AbstractIterator; @@ -33,6 +32,7 @@ import com.google.protobuf.ByteString; import java.util.Arrays; +import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashMap; @@ -79,18 +79,18 @@ public RetryResult beforeEval(Exception exception) { } @Override - public Batch newBatch(BatchOption... options) { - return new BatchImpl(this, options); + public Batch newBatch() { + return new BatchImpl(this); } @Override - public Transaction newTransaction(TransactionOption... options) { - return new TransactionImpl(this, options); + public Transaction newTransaction() { + return new TransactionImpl(this); } @Override - public T runInTransaction(TransactionCallable callable, TransactionOption... options) { - return DatastoreHelper.runInTransaction(this, callable, options); + public T runInTransaction(TransactionCallable callable) { + return DatastoreHelper.runInTransaction(this, callable); } @Override @@ -98,14 +98,19 @@ public QueryResults run(Query query) { return run(null, query); } - QueryResults run(DatastoreV1.ReadOptions readOptionsPb, Query query) { - return new QueryResultsImpl<>(this, readOptionsPb, query); + QueryResults run(com.google.datastore.v1beta3.ReadOptions readOptionsPb, Query query) { + // TODO(ajaykannan): fix me! + //return new QueryResultsImpl<>(this, readOptionsPb, query); + return null; // TODO(ajaykannan): fix me! } - DatastoreV1.RunQueryResponse runQuery(final DatastoreV1.RunQueryRequest requestPb) { + com.google.datastore.v1beta3.RunQueryResponse runQuery( + final com.google.datastore.v1beta3.RunQueryRequest requestPb) { try { - return RetryHelper.runWithRetries(new Callable() { - @Override public DatastoreV1.RunQueryResponse call() throws DatastoreRpcException { + return RetryHelper.runWithRetries( + new Callable() { + @Override public com.google.datastore.v1beta3.RunQueryResponse call() + throws DatastoreRpcException { return datastoreRpc.runQuery(requestPb); } }, retryParams, EXCEPTION_HANDLER); @@ -124,24 +129,26 @@ public List allocateId(IncompleteKey... keys) { if (keys.length == 0) { return Collections.emptyList(); } - DatastoreV1.AllocateIdsRequest.Builder requestPb = DatastoreV1.AllocateIdsRequest.newBuilder(); + com.google.datastore.v1beta3.AllocateIdsRequest.Builder requestPb = + com.google.datastore.v1beta3.AllocateIdsRequest.newBuilder(); for (IncompleteKey key : keys) { - // TODO(ajaykannan): fix me! - //requestPb.addKey(trimNameOrId(key).toPb()); + requestPb.addKeys(trimNameOrId(key).toPb()); } - DatastoreV1.AllocateIdsResponse responsePb = allocateIds(requestPb.build()); + com.google.datastore.v1beta3.AllocateIdsResponse responsePb = allocateIds(requestPb.build()); ImmutableList.Builder keyList = ImmutableList.builder(); - for (DatastoreV1.Key keyPb : responsePb.getKeyList()) { - // TODO(ajaykannan): fix me! - // keyList.add(Key.fromPb(keyPb)); + for (com.google.datastore.v1beta3.Key keyPb : responsePb.getKeysList()) { + keyList.add(Key.fromPb(keyPb)); } return keyList.build(); } - DatastoreV1.AllocateIdsResponse allocateIds(final DatastoreV1.AllocateIdsRequest requestPb) { + com.google.datastore.v1beta3.AllocateIdsResponse allocateIds( + final com.google.datastore.v1beta3.AllocateIdsRequest requestPb) { try { - return RetryHelper.runWithRetries(new Callable() { - @Override public DatastoreV1.AllocateIdsResponse call() throws DatastoreRpcException { + return RetryHelper.runWithRetries( + new Callable() { + @Override public com.google.datastore.v1beta3.AllocateIdsResponse call() + throws DatastoreRpcException { return datastoreRpc.allocateIds(requestPb); } }, retryParams, EXCEPTION_HANDLER); @@ -168,7 +175,7 @@ public List add(FullEntity... entities) { if (entities.length == 0) { return Collections.emptyList(); } - DatastoreV1.Mutation.Builder mutationPb = DatastoreV1.Mutation.newBuilder(); + List mutationsPb = new ArrayList<>(); Map completeEntities = new LinkedHashMap<>(); for (FullEntity entity : entities) { Entity completeEntity = null; @@ -180,23 +187,26 @@ public List add(FullEntity... entities) { throw DatastoreException.throwInvalidRequest( "Duplicate entity with the key %s", entity.key()); } - mutationPb.addInsert(completeEntity.toPb()); + mutationsPb.add(com.google.datastore.v1beta3.Mutation.newBuilder() + .setInsert(completeEntity.toPb()).build()); } else { Preconditions.checkArgument(entity.hasKey(), "entity %s is missing a key", entity); - mutationPb.addInsertAutoId(entity.toPb()); + mutationsPb.add(com.google.datastore.v1beta3.Mutation.newBuilder() + .setInsert(entity.toPb()).build()); } } - DatastoreV1.CommitResponse commitResponse = commitMutation(mutationPb); - Iterator allocatedKeys = - commitResponse.getMutationResult().getInsertAutoIdKeyList().iterator(); + com.google.datastore.v1beta3.CommitResponse commitResponse = commitMutation(mutationsPb); + Iterator mutationResults = + commitResponse.getMutationResultsList().iterator(); ImmutableList.Builder responseBuilder = ImmutableList.builder(); for (FullEntity entity : entities) { Entity completeEntity = completeEntities.get(entity.key()); if (completeEntity != null) { responseBuilder.add(completeEntity); + mutationResults.next(); } else { - // TODO(ajaykannan): fix me! - //responseBuilder.add(Entity.builder(Key.fromPb(allocatedKeys.next()), entity).build()); + responseBuilder.add( + Entity.builder(Key.fromPb(mutationResults.next().getKey()), entity).build()); } } return responseBuilder.build(); @@ -217,37 +227,37 @@ public List fetch(Key... keys) { return DatastoreHelper.fetch(this, keys); } - Iterator get(DatastoreV1.ReadOptions readOptionsPb, final Key... keys) { + Iterator get(com.google.datastore.v1beta3.ReadOptions readOptionsPb, final Key... keys) { if (keys.length == 0) { return Collections.emptyIterator(); } - DatastoreV1.LookupRequest.Builder requestPb = DatastoreV1.LookupRequest.newBuilder(); + com.google.datastore.v1beta3.LookupRequest.Builder requestPb = + com.google.datastore.v1beta3.LookupRequest.newBuilder(); if (readOptionsPb != null) { requestPb.setReadOptions(readOptionsPb); } for (Key k : Sets.newLinkedHashSet(Arrays.asList(keys))) { - // TODO(ajaykannan): fix me! - //requestPb.addKey(k.toPb()); + requestPb.addKeys(k.toPb()); } return new ResultsIterator(requestPb); } final class ResultsIterator extends AbstractIterator { - private final DatastoreV1.LookupRequest.Builder requestPb; - Iterator iter; + private final com.google.datastore.v1beta3.LookupRequest.Builder requestPb; + Iterator iter; - ResultsIterator(DatastoreV1.LookupRequest.Builder requestPb) { + ResultsIterator(com.google.datastore.v1beta3.LookupRequest.Builder requestPb) { this.requestPb = requestPb; loadResults(); } private void loadResults() { - DatastoreV1.LookupResponse responsePb = lookup(requestPb.build()); + com.google.datastore.v1beta3.LookupResponse responsePb = lookup(requestPb.build()); iter = responsePb.getFoundList().iterator(); - requestPb.clearKey(); + requestPb.clearKeys(); if (responsePb.getDeferredCount() > 0) { - requestPb.addAllKey(responsePb.getDeferredList()); + requestPb.addAllKeys(responsePb.getDeferredList()); } } @@ -255,7 +265,7 @@ private void loadResults() { @Override protected Entity computeNext() { while (!iter.hasNext()) { - if (requestPb.getKeyCount() == 0) { + if (requestPb.getKeysCount() == 0) { return endOfData(); } loadResults(); @@ -264,10 +274,13 @@ protected Entity computeNext() { } } - DatastoreV1.LookupResponse lookup(final DatastoreV1.LookupRequest requestPb) { + com.google.datastore.v1beta3.LookupResponse lookup( + final com.google.datastore.v1beta3.LookupRequest requestPb) { try { - return RetryHelper.runWithRetries(new Callable() { - @Override public DatastoreV1.LookupResponse call() throws DatastoreRpcException { + return RetryHelper.runWithRetries( + new Callable() { + @Override public com.google.datastore.v1beta3.LookupResponse call() + throws DatastoreRpcException { return datastoreRpc.lookup(requestPb); } }, retryParams, EXCEPTION_HANDLER); @@ -280,15 +293,17 @@ DatastoreV1.LookupResponse lookup(final DatastoreV1.LookupRequest requestPb) { @Override public final void update(Entity... entities) { if (entities.length > 0) { - DatastoreV1.Mutation.Builder mutationPb = DatastoreV1.Mutation.newBuilder(); + List mutationsPb = + new ArrayList<>(); Map dedupEntities = new LinkedHashMap<>(); for (Entity entity : entities) { dedupEntities.put(entity.key(), entity); } for (Entity entity : dedupEntities.values()) { - mutationPb.addUpdate(entity.toPb()); + mutationsPb.add( + com.google.datastore.v1beta3.Mutation.newBuilder().setUpdate(entity.toPb()).build()); } - commitMutation(mutationPb); + commitMutation(mutationsPb); } } @@ -296,28 +311,30 @@ public final void update(Entity... entities) { @Override public final void put(Entity... entities) { if (entities.length > 0) { - DatastoreV1.Mutation.Builder mutationPb = DatastoreV1.Mutation.newBuilder(); + List mutationsPb = + new ArrayList<>(); Map dedupEntities = new LinkedHashMap<>(); for (Entity entity : entities) { dedupEntities.put(entity.key(), entity); } for (Entity e : dedupEntities.values()) { - mutationPb.addUpsert(e.toPb()); + mutationsPb.add( + com.google.datastore.v1beta3.Mutation.newBuilder().setUpsert(e.toPb()).build()); } - commitMutation(mutationPb); + commitMutation(mutationsPb); } } @Override public void delete(Key... keys) { if (keys.length > 0) { - DatastoreV1.Mutation.Builder mutationPb = DatastoreV1.Mutation.newBuilder(); + List mutationsPb = new ArrayList<>(); Set dedupKeys = new LinkedHashSet<>(Arrays.asList(keys)); for (Key key : dedupKeys) { - // TODO(ajaykannan): fix me! - //mutationPb.addDelete(key.toPb()); + mutationsPb.add( + com.google.datastore.v1beta3.Mutation.newBuilder().setDelete(key.toPb()).build()); } - commitMutation(mutationPb); + commitMutation(mutationsPb); } } @@ -326,20 +343,22 @@ public KeyFactory newKeyFactory() { return DatastoreHelper.newKeyFactory(options()); } - private DatastoreV1.CommitResponse commitMutation(DatastoreV1.Mutation.Builder mutationPb) { - if (options().force()) { - mutationPb.setForce(true); - } - DatastoreV1.CommitRequest.Builder requestPb = DatastoreV1.CommitRequest.newBuilder(); - requestPb.setMode(DatastoreV1.CommitRequest.Mode.NON_TRANSACTIONAL); - requestPb.setMutation(mutationPb); + private com.google.datastore.v1beta3.CommitResponse commitMutation( + List mutationsPb) { + com.google.datastore.v1beta3.CommitRequest.Builder requestPb = + com.google.datastore.v1beta3.CommitRequest.newBuilder(); + requestPb.setMode(com.google.datastore.v1beta3.CommitRequest.Mode.NON_TRANSACTIONAL); + requestPb.addAllMutations(mutationsPb); return commit(requestPb.build()); } - DatastoreV1.CommitResponse commit(final DatastoreV1.CommitRequest requestPb) { + com.google.datastore.v1beta3.CommitResponse commit( + final com.google.datastore.v1beta3.CommitRequest requestPb) { try { - return RetryHelper.runWithRetries(new Callable() { - @Override public DatastoreV1.CommitResponse call() throws DatastoreRpcException { + return RetryHelper.runWithRetries( + new Callable() { + @Override public com.google.datastore.v1beta3.CommitResponse call() + throws DatastoreRpcException { return datastoreRpc.commit(requestPb); } }, retryParams, EXCEPTION_HANDLER); @@ -348,16 +367,19 @@ DatastoreV1.CommitResponse commit(final DatastoreV1.CommitRequest requestPb) { } } - ByteString requestTransactionId(DatastoreV1.BeginTransactionRequest.Builder requestPb) { + ByteString requestTransactionId( + com.google.datastore.v1beta3.BeginTransactionRequest.Builder requestPb) { return beginTransaction(requestPb.build()).getTransaction(); } - DatastoreV1.BeginTransactionResponse beginTransaction( - final DatastoreV1.BeginTransactionRequest requestPb) { + com.google.datastore.v1beta3.BeginTransactionResponse beginTransaction( + final com.google.datastore.v1beta3.BeginTransactionRequest requestPb) { try { - return RetryHelper.runWithRetries(new Callable() { + return RetryHelper.runWithRetries( + new Callable() { @Override - public DatastoreV1.BeginTransactionResponse call() throws DatastoreRpcException { + public com.google.datastore.v1beta3.BeginTransactionResponse call() + throws DatastoreRpcException { return datastoreRpc.beginTransaction(requestPb); } }, retryParams, EXCEPTION_HANDLER); @@ -367,12 +389,13 @@ public DatastoreV1.BeginTransactionResponse call() throws DatastoreRpcException } void rollbackTransaction(ByteString transaction) { - DatastoreV1.RollbackRequest.Builder requestPb = DatastoreV1.RollbackRequest.newBuilder(); + com.google.datastore.v1beta3.RollbackRequest.Builder requestPb = + com.google.datastore.v1beta3.RollbackRequest.newBuilder(); requestPb.setTransaction(transaction); rollback(requestPb.build()); } - void rollback(final DatastoreV1.RollbackRequest requestPb) { + void rollback(final com.google.datastore.v1beta3.RollbackRequest requestPb) { try { RetryHelper.runWithRetries(new Callable() { @Override public Void call() throws DatastoreRpcException { diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java index 7b0dd3a2a606..23128a856761 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java @@ -18,9 +18,7 @@ import static com.google.gcloud.datastore.Validator.validateNamespace; -import com.google.api.services.datastore.DatastoreV1; -import com.google.api.services.datastore.DatastoreV1.EntityResult; -import com.google.api.services.datastore.DatastoreV1.LookupResponse; +import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.gcloud.ServiceOptions; @@ -102,21 +100,23 @@ private DatastoreOptions normalize() { Builder builder = toBuilder(); builder.normalizeDataset(false); // Replace provided project-id with full project-id (s~xxx, e~xxx,...) - DatastoreV1.LookupRequest.Builder requestPb = DatastoreV1.LookupRequest.newBuilder(); - DatastoreV1.Key key = DatastoreV1.Key.newBuilder() - .addPathElement(DatastoreV1.Key.PathElement.newBuilder().setKind("__foo__").setName("bar")) + com.google.datastore.v1beta3.LookupRequest.Builder requestPb = + com.google.datastore.v1beta3.LookupRequest.newBuilder(); + com.google.datastore.v1beta3.Key key = com.google.datastore.v1beta3.Key.newBuilder() + .addPath(com.google.datastore.v1beta3.Key.PathElement.newBuilder() + .setKind("__foo__").setName("bar")) .build(); - requestPb.addKey(key); + requestPb.addKeys(key); try { - LookupResponse responsePb = datastoreRpc().lookup(requestPb.build()); + com.google.datastore.v1beta3.LookupResponse responsePb = datastoreRpc().lookup(requestPb.build()); if (responsePb.getDeferredCount() > 0) { key = responsePb.getDeferred(0); } else { - Iterator combinedIter = + Iterator combinedIter = Iterables.concat(responsePb.getMissingList(), responsePb.getFoundList()).iterator(); key = combinedIter.next().getEntity().getKey(); } - builder.projectId(key.getPartitionId().getDatasetId()); + builder.projectId(key.getPartitionId().getProjectId()); return new DatastoreOptions(builder); } catch (DatastoreRpcException e) { throw DatastoreException.translateAndThrow(e); @@ -149,10 +149,10 @@ private static String defaultNamespace() { Class clazz = Class.forName("com.google.appengine.api.NamespaceManager"); Method method = clazz.getMethod("get"); String namespace = (String) method.invoke(null); - return namespace == null || namespace.isEmpty() ? null : namespace; + return MoreObjects.firstNonNull(namespace, ""); } catch (Exception ignore) { - // return null (Datastore default namespace) if could not automatically determine - return null; + // return empty string (Datastore default namespace) if could not automatically determine + return ""; } } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Entity.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Entity.java index dc1af5b8a2d9..d012eff14422 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Entity.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Entity.java @@ -18,7 +18,6 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.api.services.datastore.DatastoreV1; import com.google.common.base.Preconditions; /** @@ -93,7 +92,7 @@ public static Builder builder(Key key, FullEntity copyFrom) { return new Builder(key, copyFrom); } - static Entity fromPb(DatastoreV1.Entity entityPb) { + static Entity fromPb(com.google.datastore.v1beta3.Entity entityPb) { return new Builder().fill(entityPb).build(); } } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/EntityValue.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/EntityValue.java index 16ca55aec4a6..4a327383afd4 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/EntityValue.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/EntityValue.java @@ -39,15 +39,12 @@ public Builder newBuilder(FullEntity value) { @Override protected FullEntity getValue(com.google.datastore.v1beta3.Value from) { - // TODO(ajaykannan): fix me! - //return FullEntity.fromPb(from.getEntityValue()); - return null; // TODO(ajaykannan): fix me! + return FullEntity.fromPb(from.getEntityValue()); } @Override protected void setValue(EntityValue from, com.google.datastore.v1beta3.Value.Builder to) { - // TODO(ajaykannan): fix me! - //to.setEntityValue(from.get().toPb()); + to.setEntityValue(from.get().toPb()); } }; diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/FullEntity.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/FullEntity.java index bb08fca12e3c..b1534984aeb0 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/FullEntity.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/FullEntity.java @@ -16,8 +16,6 @@ package com.google.gcloud.datastore; -import com.google.api.services.datastore.DatastoreV1; - /** * A full entity is a {@link BaseEntity} that with a complete set of properties. */ @@ -70,7 +68,7 @@ public static Builder builder(FullEntity copyFro } - static FullEntity fromPb(DatastoreV1.Entity entityPb) { + static FullEntity fromPb(com.google.datastore.v1beta3.Entity entityPb) { return new Builder<>().fill(entityPb).build(); } } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ProjectionEntity.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ProjectionEntity.java index 46a528617a0c..f8af814245ab 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ProjectionEntity.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ProjectionEntity.java @@ -16,7 +16,6 @@ package com.google.gcloud.datastore; -import com.google.api.services.datastore.DatastoreV1; import com.google.protobuf.ByteString; /** @@ -72,7 +71,7 @@ public Blob getBlob(String name) { return ((Value) value).get(); } - static ProjectionEntity fromPb(DatastoreV1.Entity entityPb) { + static ProjectionEntity fromPb(com.google.datastore.v1beta3.Entity entityPb) { return new Builder().fill(entityPb).build(); } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java index f3dd254ce9a3..7fae120dd402 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java @@ -69,7 +69,9 @@ public abstract static class ResultType implements java.io.Serializable { //TODO(ajaykannan): fix me! //return Key.fromPb(entityPb.getKey()); } - return ProjectionEntity.fromPb(entityPb); + // TODO(ajaykannan): fix me! + //return ProjectionEntity.fromPb(entityPb); + return ProjectionEntity.fromPb((com.google.datastore.v1beta3.Entity) null); // TODO(ajaykannan): fix me! } }; @@ -79,7 +81,9 @@ public abstract static class ResultType implements java.io.Serializable { private static final long serialVersionUID = 7712959777507168274L; @Override protected Entity convert(DatastoreV1.Entity entityPb) { - return Entity.fromPb(entityPb); + // TODO(ajaykannan): fix me! + //return Entity.fromPb(entityPb); + return Entity.fromPb((com.google.datastore.v1beta3.Entity) null); // TODO(ajaykannan): fix me! } }; @@ -102,7 +106,9 @@ public abstract static class ResultType implements java.io.Serializable { private static final long serialVersionUID = -7591409419690650246L; @Override protected ProjectionEntity convert(DatastoreV1.Entity entityPb) { - return ProjectionEntity.fromPb(entityPb); + // TODO(ajaykannan): fix me! + //return ProjectionEntity.fromPb(entityPb); + return ProjectionEntity.fromPb((com.google.datastore.v1beta3.Entity) null); // TODO(ajaykannan): fix me! } }; diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java index cd3fe9dd776b..caf5d7f8a84f 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java @@ -63,7 +63,8 @@ private void sendRequest() { } requestPb.setPartitionId(partitionIdPb); query.populatePb(requestPb); - queryResultBatchPb = datastore.runQuery(requestPb.build()).getBatch(); + // TODO(ajaykannan): fix me! + //queryResultBatchPb = datastore.runQuery(requestPb.build()).getBatch(); lastBatch = queryResultBatchPb.getMoreResults() != MoreResultsType.NOT_FINISHED; entityResultPbIter = queryResultBatchPb.getEntityResultList().iterator(); // cursor = resultPb.getSkippedCursor(); // available in v1beta3, use startCursor if not skipped diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionImpl.java index ae9d0e50f0d7..ff9ce5e50e9d 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionImpl.java @@ -16,58 +16,45 @@ package com.google.gcloud.datastore; -import com.google.api.services.datastore.DatastoreV1; -import com.google.common.base.Function; -import com.google.common.collect.Lists; -import com.google.gcloud.datastore.TransactionOption.ForceWrites; -import com.google.gcloud.datastore.TransactionOption.IsolationLevel; import com.google.protobuf.ByteString; import java.util.Iterator; +import java.util.LinkedList; import java.util.List; -import java.util.Map; final class TransactionImpl extends BaseDatastoreBatchWriter implements Transaction { private final DatastoreImpl datastore; private final ByteString transaction; - private final boolean force; private boolean rolledback; static class ResponseImpl implements Transaction.Response { - private final DatastoreV1.CommitResponse response; + private final com.google.datastore.v1beta3.CommitResponse response; + private final int numAutoAllocatedIds; - ResponseImpl(DatastoreV1.CommitResponse response) { + ResponseImpl(com.google.datastore.v1beta3.CommitResponse response, int numAutoAllocatedIds) { this.response = response; + this.numAutoAllocatedIds = numAutoAllocatedIds; } @Override public List generatedKeys() { - return Lists.transform(response.getMutationResult().getInsertAutoIdKeyList(), - new Function() { - @Override public Key apply(DatastoreV1.Key keyPb) { - // TODO(ajaykannan): fix me! - //return Key.fromPb(keyPb); - return Key.builder(null).build(); // TODO(ajaykannan): fix me! - } - }); + Iterator results = + response.getMutationResultsList().iterator(); + List generated = new LinkedList(); + for (int i = 0; i < numAutoAllocatedIds; i++) { + generated.add(Key.fromPb(results.next().getKey())); + } + return generated; } } - TransactionImpl(DatastoreImpl datastore, TransactionOption... options) { + TransactionImpl(DatastoreImpl datastore) { super("transaction"); this.datastore = datastore; - DatastoreV1.BeginTransactionRequest.Builder requestPb = - DatastoreV1.BeginTransactionRequest.newBuilder(); - Map, TransactionOption> optionsMap = - TransactionOption.asImmutableMap(options); - IsolationLevel isolationLevel = (IsolationLevel) optionsMap.get(IsolationLevel.class); - if (isolationLevel != null) { - requestPb.setIsolationLevel(isolationLevel.level().toPb()); - } - ForceWrites forceWrites = (ForceWrites) optionsMap.get(TransactionOption.ForceWrites.class); - force = forceWrites != null && forceWrites.force(); + com.google.datastore.v1beta3.BeginTransactionRequest.Builder requestPb = + com.google.datastore.v1beta3.BeginTransactionRequest.newBuilder(); transaction = datastore.requestTransactionId(requestPb); } @@ -79,7 +66,8 @@ public Entity get(Key key) { @Override public Iterator get(Key... keys) { validateActive(); - DatastoreV1.ReadOptions.Builder readOptionsPb = DatastoreV1.ReadOptions.newBuilder(); + com.google.datastore.v1beta3.ReadOptions.Builder readOptionsPb = + com.google.datastore.v1beta3.ReadOptions.newBuilder(); readOptionsPb.setTransaction(transaction); return datastore.get(readOptionsPb.build(), keys); } @@ -93,7 +81,8 @@ public List fetch(Key... keys) { @Override public QueryResults run(Query query) { validateActive(); - DatastoreV1.ReadOptions.Builder readOptionsPb = DatastoreV1.ReadOptions.newBuilder(); + com.google.datastore.v1beta3.ReadOptions.Builder readOptionsPb = + com.google.datastore.v1beta3.ReadOptions.newBuilder(); readOptionsPb.setTransaction(transaction); return datastore.run(readOptionsPb.build(), query); } @@ -101,17 +90,15 @@ public QueryResults run(Query query) { @Override public Transaction.Response commit() { validateActive(); - DatastoreV1.Mutation.Builder mutationPb = toMutationPb(); - if (force) { - mutationPb.setForce(force); - } - DatastoreV1.CommitRequest.Builder requestPb = DatastoreV1.CommitRequest.newBuilder(); - requestPb.setMode(DatastoreV1.CommitRequest.Mode.TRANSACTIONAL); + List mutationsPb = toMutationPbList(); + com.google.datastore.v1beta3.CommitRequest.Builder requestPb = + com.google.datastore.v1beta3.CommitRequest.newBuilder(); + requestPb.setMode(com.google.datastore.v1beta3.CommitRequest.Mode.TRANSACTIONAL); requestPb.setTransaction(transaction); - requestPb.setMutation(mutationPb); - DatastoreV1.CommitResponse responsePb = datastore.commit(requestPb.build()); + requestPb.addAllMutations(mutationsPb); + com.google.datastore.v1beta3.CommitResponse responsePb = datastore.commit(requestPb.build()); deactivate(); - return new ResponseImpl(responsePb); + return new ResponseImpl(responsePb, numAutoAllocatedIds()); } @Override diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionOption.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionOption.java deleted file mode 100644 index c1c8368213dc..000000000000 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionOption.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright 2015 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.datastore; - -import com.google.api.services.datastore.DatastoreV1; -import com.google.common.collect.ImmutableMap; - -import java.io.Serializable; -import java.util.Map; - - -public abstract class TransactionOption implements Serializable { - - private static final long serialVersionUID = -1862234444015690375L; - - public static final class ForceWrites extends TransactionOption { - - private static final long serialVersionUID = -6873967516988380886L; - - private final boolean force; - - public ForceWrites(boolean force) { - this.force = force; - } - - public boolean force() { - return force; - } - - @Override - BatchOption toBatchWriteOption() { - return new BatchOption.ForceWrites(force); - } - } - - public static final class IsolationLevel extends TransactionOption { - - private static final long serialVersionUID = -5592165378565409515L; - - private final Level level; - - public enum Level { - - SERIALIZABLE(DatastoreV1.BeginTransactionRequest.IsolationLevel.SERIALIZABLE), - SNAPSHOT(DatastoreV1.BeginTransactionRequest.IsolationLevel.SNAPSHOT); - - private final DatastoreV1.BeginTransactionRequest.IsolationLevel levelPb; - - Level(DatastoreV1.BeginTransactionRequest.IsolationLevel levelPb) { - this.levelPb = levelPb; - } - - DatastoreV1.BeginTransactionRequest.IsolationLevel toPb() { - return levelPb; - } - } - - public IsolationLevel(Level level) { - this.level = level; - } - - - public Level level() { - return level; - } - - @Override - BatchOption toBatchWriteOption() { - return null; - } - } - - TransactionOption() { - // package protected - } - - public static ForceWrites forceWrites() { - return new ForceWrites(true); - } - - public static IsolationLevel serializable() { - return new IsolationLevel(IsolationLevel.Level.SERIALIZABLE); - } - - public static IsolationLevel snapshot() { - return new IsolationLevel(IsolationLevel.Level.SNAPSHOT); - } - - static Map, TransactionOption> asImmutableMap( - TransactionOption... options) { - ImmutableMap.Builder, TransactionOption> builder = - ImmutableMap.builder(); - for (TransactionOption option : options) { - builder.put(option.getClass(), option); - } - return builder.build(); - } - - abstract BatchOption toBatchWriteOption(); -} diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DatastoreRpc.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DatastoreRpc.java index dffcc3f0e16f..329623a565f6 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DatastoreRpc.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DatastoreRpc.java @@ -15,19 +15,6 @@ */ package com.google.gcloud.spi; -import com.google.api.services.datastore.DatastoreV1.AllocateIdsRequest; -import com.google.api.services.datastore.DatastoreV1.AllocateIdsResponse; -import com.google.api.services.datastore.DatastoreV1.BeginTransactionRequest; -import com.google.api.services.datastore.DatastoreV1.BeginTransactionResponse; -import com.google.api.services.datastore.DatastoreV1.CommitRequest; -import com.google.api.services.datastore.DatastoreV1.CommitResponse; -import com.google.api.services.datastore.DatastoreV1.LookupRequest; -import com.google.api.services.datastore.DatastoreV1.LookupResponse; -import com.google.api.services.datastore.DatastoreV1.RollbackRequest; -import com.google.api.services.datastore.DatastoreV1.RollbackResponse; -import com.google.api.services.datastore.DatastoreV1.RunQueryRequest; -import com.google.api.services.datastore.DatastoreV1.RunQueryResponse; - /** * Provides access to the remote Datastore service. */ @@ -103,16 +90,22 @@ public boolean retryable() { } } - AllocateIdsResponse allocateIds(AllocateIdsRequest request) throws DatastoreRpcException; + com.google.datastore.v1beta3.AllocateIdsResponse allocateIds( + com.google.datastore.v1beta3.AllocateIdsRequest request) throws DatastoreRpcException; - BeginTransactionResponse beginTransaction(BeginTransactionRequest request) + com.google.datastore.v1beta3.BeginTransactionResponse beginTransaction( + com.google.datastore.v1beta3.BeginTransactionRequest request) throws DatastoreRpcException; - CommitResponse commit(CommitRequest request) throws DatastoreRpcException; + com.google.datastore.v1beta3.CommitResponse commit( + com.google.datastore.v1beta3.CommitRequest request) throws DatastoreRpcException; - LookupResponse lookup(LookupRequest request) throws DatastoreRpcException; + com.google.datastore.v1beta3.LookupResponse lookup( + com.google.datastore.v1beta3.LookupRequest request) throws DatastoreRpcException; - RollbackResponse rollback(RollbackRequest request) throws DatastoreRpcException; + com.google.datastore.v1beta3.RollbackResponse rollback( + com.google.datastore.v1beta3.RollbackRequest request) throws DatastoreRpcException; - RunQueryResponse runQuery(RunQueryRequest request) throws DatastoreRpcException; + com.google.datastore.v1beta3.RunQueryResponse runQuery( + com.google.datastore.v1beta3.RunQueryRequest request) throws DatastoreRpcException; } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java index 2f245260b325..1ae0b6eea22e 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java @@ -16,36 +16,16 @@ package com.google.gcloud.spi; -import com.google.api.services.datastore.DatastoreV1.AllocateIdsRequest; -import com.google.api.services.datastore.DatastoreV1.AllocateIdsResponse; -import com.google.api.services.datastore.DatastoreV1.BeginTransactionRequest; -import com.google.api.services.datastore.DatastoreV1.BeginTransactionResponse; -import com.google.api.services.datastore.DatastoreV1.CommitRequest; -import com.google.api.services.datastore.DatastoreV1.CommitResponse; -import com.google.api.services.datastore.DatastoreV1.LookupRequest; -import com.google.api.services.datastore.DatastoreV1.LookupResponse; -import com.google.api.services.datastore.DatastoreV1.RollbackRequest; -import com.google.api.services.datastore.DatastoreV1.RollbackResponse; -import com.google.api.services.datastore.DatastoreV1.RunQueryRequest; -import com.google.api.services.datastore.DatastoreV1.RunQueryResponse; -import com.google.api.services.datastore.client.Datastore; -import com.google.api.services.datastore.client.DatastoreException; -import com.google.api.services.datastore.client.DatastoreFactory; -import com.google.api.services.datastore.client.DatastoreOptions.Builder; import com.google.common.collect.ImmutableMap; import com.google.gcloud.datastore.DatastoreOptions; import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException.Reason; -import org.json.JSONException; -import org.json.JSONObject; -import org.json.JSONTokener; - import java.util.HashMap; import java.util.Map; public class DefaultDatastoreRpc implements DatastoreRpc { - private final Datastore client; + private final com.google.datastore.v1beta3.client.Datastore client; private static final ImmutableMap STR_TO_REASON; private static final ImmutableMap HTTP_STATUS_TO_REASON; @@ -62,26 +42,27 @@ public class DefaultDatastoreRpc implements DatastoreRpc { } public DefaultDatastoreRpc(DatastoreOptions options) { - client = DatastoreFactory.get().create( - new Builder() - .dataset(options.projectId()) - .host(options.host()) - .initializer(options.httpRequestInitializer()) - .build()); + if (options.host().contains("localhost")) { + client = com.google.datastore.v1beta3.client.DatastoreFactory.get().create( + new com.google.datastore.v1beta3.client.DatastoreOptions.Builder() + .projectId(options.projectId()) + .localHost(options.host()) + .initializer(options.httpRequestInitializer()) + .build()); + } else { + client = com.google.datastore.v1beta3.client.DatastoreFactory.get().create( + new com.google.datastore.v1beta3.client.DatastoreOptions.Builder() + .projectId(options.projectId()) + .initializer(options.httpRequestInitializer()) + .build()); + } } - private static DatastoreRpcException translate(DatastoreException exception) { - String message = exception.getMessage(); + private static DatastoreRpcException translate( + com.google.datastore.v1beta3.client.DatastoreException exception) { String reasonStr = ""; - if (message != null) { - try { - JSONObject json = new JSONObject(new JSONTokener(message)); - JSONObject error = json.getJSONObject("error").getJSONArray("errors").getJSONObject(0); - reasonStr = error.getString("reason"); - message = error.getString("message"); - } catch (JSONException ignore) { - // ignore - will be converted to unknown - } + if (exception.getCode() != null) { + reasonStr = exception.getCode().name(); } Reason reason = STR_TO_REASON.get(reasonStr); if (reason == null) { @@ -89,61 +70,68 @@ private static DatastoreRpcException translate(DatastoreException exception) { } return reason != null ? new DatastoreRpcException(reason) - : new DatastoreRpcException("Unknown", exception.getCode(), false, message); + : new DatastoreRpcException("Unknown", + exception.getCode().ordinal(), + false, + exception.getMessage()); } @Override - public AllocateIdsResponse allocateIds(AllocateIdsRequest request) - throws DatastoreRpcException { + public com.google.datastore.v1beta3.AllocateIdsResponse allocateIds( + com.google.datastore.v1beta3.AllocateIdsRequest request) throws DatastoreRpcException { try { return client.allocateIds(request); - } catch (DatastoreException ex) { + } catch (com.google.datastore.v1beta3.client.DatastoreException ex) { throw translate(ex); } } @Override - public BeginTransactionResponse beginTransaction(BeginTransactionRequest request) - throws DatastoreRpcException { + public com.google.datastore.v1beta3.BeginTransactionResponse beginTransaction( + com.google.datastore.v1beta3.BeginTransactionRequest request) throws DatastoreRpcException { try { return client.beginTransaction(request); - } catch (DatastoreException ex) { + } catch (com.google.datastore.v1beta3.client.DatastoreException ex) { throw translate(ex); } } @Override - public CommitResponse commit(CommitRequest request) throws DatastoreRpcException { + public com.google.datastore.v1beta3.CommitResponse commit( + com.google.datastore.v1beta3.CommitRequest request) throws DatastoreRpcException { try { return client.commit(request); - } catch (DatastoreException ex) { + } catch (com.google.datastore.v1beta3.client.DatastoreException ex) { throw translate(ex); } } @Override - public LookupResponse lookup(LookupRequest request) throws DatastoreRpcException { + public com.google.datastore.v1beta3.LookupResponse lookup( + com.google.datastore.v1beta3.LookupRequest request) throws DatastoreRpcException { try { return client.lookup(request); - } catch (DatastoreException ex) { + } catch (com.google.datastore.v1beta3.client.DatastoreException ex) { throw translate(ex); } } @Override - public RollbackResponse rollback(RollbackRequest request) throws DatastoreRpcException { + public com.google.datastore.v1beta3.RollbackResponse rollback( + com.google.datastore.v1beta3.RollbackRequest request) throws DatastoreRpcException { try { return client.rollback(request); - } catch (DatastoreException ex) { + } catch (com.google.datastore.v1beta3.client.DatastoreException ex) { throw translate(ex); } } @Override - public RunQueryResponse runQuery(RunQueryRequest request) throws DatastoreRpcException { + public com.google.datastore.v1beta3.RunQueryResponse runQuery( + com.google.datastore.v1beta3.RunQueryRequest request) throws DatastoreRpcException { try { return client.runQuery(request); - } catch (DatastoreException ex) { + } catch (com.google.datastore.v1beta3.client.DatastoreException ex) { throw translate(ex); } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseDatastoreBatchWriterTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseDatastoreBatchWriterTest.java index 43df45d7a04e..8996f4536931 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseDatastoreBatchWriterTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseDatastoreBatchWriterTest.java @@ -21,7 +21,6 @@ import static org.easymock.EasyMock.verify; import static org.junit.Assert.assertEquals; -import com.google.api.services.datastore.DatastoreV1; import com.google.common.collect.ImmutableList; import org.easymock.EasyMock; @@ -30,6 +29,7 @@ import org.junit.Test; import java.util.List; +import java.util.LinkedList; public class BaseDatastoreBatchWriterTest { @@ -80,36 +80,33 @@ public void tearDown() { batchWriter.finish(); } - // TODO(ajaykannan): fix me! - /* @Test public void testAdd() throws Exception { Entity entity2 = Entity.builder(ENTITY2).key(Key.builder(KEY1).name("name2").build()).build(); - DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() - .addInsert(ENTITY1.toPb()) - .addInsert(entity2.toPb()) - .addInsert(Entity.builder(KEY2, INCOMPLETE_ENTITY_1).build().toPb()) - .addInsert(Entity.builder(KEY3, INCOMPLETE_ENTITY_2).build().toPb()) - .build(); + List pbs = new LinkedList<>(); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder().setInsert(ENTITY1.toPb()).build()); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder() + .setInsert(Entity.builder(KEY2, INCOMPLETE_ENTITY_1).build().toPb()).build()); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder() + .setInsert(Entity.builder(KEY3, INCOMPLETE_ENTITY_2).build().toPb()).build()); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder().setInsert(entity2.toPb()).build()); List entities = batchWriter .add(ENTITY1, INCOMPLETE_ENTITY_1, INCOMPLETE_ENTITY_2, entity2); - assertEquals(pb, batchWriter.toMutationPb().build()); + assertEquals(pbs, batchWriter.toMutationPbList()); assertEquals(ENTITY1, entities.get(0)); assertEquals(Entity.builder(KEY2, INCOMPLETE_ENTITY_1).build(), entities.get(1)); assertEquals(Entity.builder(KEY3, INCOMPLETE_ENTITY_2).build(), entities.get(2)); assertEquals(entity2, entities.get(3)); } - */ @Test public void testAddAfterDelete() throws Exception { - DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() - .addUpsert(ENTITY1.toPb()) - .build(); + List pbs = new LinkedList<>(); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder().setUpsert(ENTITY1.toPb()).build()); batchWriter.delete(KEY1); batchWriter.add(ENTITY1); - assertEquals(pb, batchWriter.toMutationPb().build()); + assertEquals(pbs, batchWriter.toMutationPbList()); } @Test(expected = DatastoreException.class) @@ -136,18 +133,17 @@ public void testAddWhenNotActive() throws Exception { batchWriter.add(ENTITY1); } - // TODO(ajaykannan): fix me! - /* @Test public void testAddWithDeferredAllocation() throws Exception { - DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() - .addInsert(ENTITY1.toPb()) - .addInsertAutoId(INCOMPLETE_ENTITY_1.toPb()) - .addInsertAutoId(INCOMPLETE_ENTITY_2.toPb()) - .build(); + List pbs = new LinkedList<>(); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder() + .setInsert(INCOMPLETE_ENTITY_1.toPb()).build()); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder() + .setInsert(INCOMPLETE_ENTITY_2.toPb()).build()); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder().setInsert(ENTITY1.toPb()).build()); batchWriter.addWithDeferredIdAllocation(ENTITY1, INCOMPLETE_ENTITY_1); batchWriter.addWithDeferredIdAllocation(INCOMPLETE_ENTITY_2); - assertEquals(pb, batchWriter.toMutationPb().build()); + assertEquals(pbs, batchWriter.toMutationPbList()); } @Test(expected = DatastoreException.class) @@ -158,49 +154,44 @@ public void testAddWithDeferredAllocationWhenNotActive() throws Exception { @Test public void testUpdate() throws Exception { - DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() - .addUpdate(ENTITY1.toPb()) - .addUpdate(ENTITY2.toPb()) - .addUpdate(ENTITY3.toPb()) - .build(); + List pbs = new LinkedList<>(); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder().setUpdate(ENTITY1.toPb()).build()); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder().setUpdate(ENTITY2.toPb()).build()); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder().setUpdate(ENTITY3.toPb()).build()); batchWriter.update(ENTITY1, ENTITY2); batchWriter.update(ENTITY3); - assertEquals(pb, batchWriter.toMutationPb().build()); + assertEquals(pbs, batchWriter.toMutationPbList()); } @Test public void testUpdateAfterUpdate() throws Exception { Entity entity = Entity.builder(ENTITY1).set("foo", "bar").build(); - DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() - .addUpdate(entity.toPb()) - .build(); + List pbs = new LinkedList<>(); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder().setUpdate(entity.toPb()).build()); batchWriter.update(ENTITY1); batchWriter.update(entity); - assertEquals(pb, batchWriter.toMutationPb().build()); + assertEquals(pbs, batchWriter.toMutationPbList()); } @Test public void testUpdateAfterAdd() throws Exception { Entity entity = Entity.builder(ENTITY1).set("foo", "bar").build(); - DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() - .addUpsert(entity.toPb()) - .build(); + List pbs = new LinkedList<>(); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder().setUpsert(entity.toPb()).build()); batchWriter.add(ENTITY1); batchWriter.update(entity); - assertEquals(pb, batchWriter.toMutationPb().build()); + assertEquals(pbs, batchWriter.toMutationPbList()); } @Test public void testUpdateAfterPut() throws Exception { Entity entity = Entity.builder(ENTITY1).set("foo", "bar").build(); - DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() - .addUpsert(entity.toPb()) - .build(); + List pbs = new LinkedList<>(); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder().setUpsert(entity.toPb()).build()); batchWriter.put(ENTITY1); batchWriter.update(entity); - assertEquals(pb, batchWriter.toMutationPb().build()); + assertEquals(pbs, batchWriter.toMutationPbList()); } - */ @Test(expected = DatastoreException.class) public void testUpdateAfterDelete() throws Exception { @@ -214,117 +205,104 @@ public void testUpdateWhenNotActive() throws Exception { batchWriter.update(ENTITY1); } - // TODO(ajaykannan): fix me! - /* @Test public void testPut() throws Exception { - DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() - .addUpsert(ENTITY1.toPb()) - .addUpsert(ENTITY2.toPb()) - .addUpsert(ENTITY3.toPb()) - .build(); + List pbs = new LinkedList<>(); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder().setUpsert(ENTITY1.toPb()).build()); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder().setUpsert(ENTITY2.toPb()).build()); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder().setUpsert(ENTITY3.toPb()).build()); batchWriter.put(ENTITY1, ENTITY2); batchWriter.put(ENTITY3); - assertEquals(pb, batchWriter.toMutationPb().build()); + assertEquals(pbs, batchWriter.toMutationPbList()); } @Test public void testPutAfterPut() throws Exception { Entity entity = Entity.builder(ENTITY1).set("foo", "bar").build(); - DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() - .addUpsert(entity.toPb()) - .build(); + List pbs = new LinkedList<>(); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder().setUpsert(entity.toPb()).build()); batchWriter.put(ENTITY1); batchWriter.put(entity); - assertEquals(pb, batchWriter.toMutationPb().build()); + assertEquals(pbs, batchWriter.toMutationPbList()); } @Test public void testPutAfterAdd() throws Exception { Entity entity = Entity.builder(ENTITY1).set("foo", "bar").build(); - DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() - .addUpsert(entity.toPb()) - .build(); + List pbs = new LinkedList<>(); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder().setUpsert(entity.toPb()).build()); batchWriter.add(ENTITY1); batchWriter.put(entity); - assertEquals(pb, batchWriter.toMutationPb().build()); + assertEquals(pbs, batchWriter.toMutationPbList()); } @Test public void testPutAfterUpdate() throws Exception { Entity entity = Entity.builder(ENTITY1).set("foo", "bar").build(); - DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() - .addUpsert(entity.toPb()) - .build(); + List pbs = new LinkedList<>(); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder().setUpsert(entity.toPb()).build()); batchWriter.update(ENTITY1); batchWriter.put(entity); - assertEquals(pb, batchWriter.toMutationPb().build()); + assertEquals(pbs, batchWriter.toMutationPbList()); } @Test public void testPutAfterDelete() throws Exception { Entity entity = Entity.builder(ENTITY1).set("foo", "bar").build(); - DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() - .addUpsert(entity.toPb()) - .build(); + List pbs = new LinkedList<>(); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder().setUpsert(entity.toPb()).build()); batchWriter.delete(KEY1); batchWriter.put(entity); - assertEquals(pb, batchWriter.toMutationPb().build()); + assertEquals(pbs, batchWriter.toMutationPbList()); } - */ @Test(expected = DatastoreException.class) public void testPutWhenNotActive() throws Exception { batchWriter.deactivate(); batchWriter.put(ENTITY1); } - // TODO(ajaykannan): fix me! - /* + @Test public void testDelete() throws Exception { - DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() - .addDelete(KEY1.toPb()) - .addDelete(KEY2.toPb()) - .addDelete(KEY3.toPb()) - .build(); + List pbs = new LinkedList<>(); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder().setDelete(KEY1.toPb()).build()); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder().setDelete(KEY2.toPb()).build()); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder().setDelete(KEY3.toPb()).build()); batchWriter.delete(KEY1, KEY2); batchWriter.delete(KEY3); - assertEquals(pb, batchWriter.toMutationPb().build()); + assertEquals(pbs, batchWriter.toMutationPbList()); } @Test public void testDeleteAfterAdd() throws Exception { - DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() - .addInsertAutoId(INCOMPLETE_ENTITY_1.toPb()) - .addDelete(KEY1.toPb()) - .build(); + List pbs = new LinkedList<>(); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder() + .setInsert(INCOMPLETE_ENTITY_1.toPb()).build()); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder().setDelete(KEY1.toPb()).build()); batchWriter.add(ENTITY1); batchWriter.addWithDeferredIdAllocation(INCOMPLETE_ENTITY_1); batchWriter.delete(KEY1); - assertEquals(pb, batchWriter.toMutationPb().build()); + assertEquals(pbs, batchWriter.toMutationPbList()); } @Test public void testDeleteAfterUpdate() throws Exception { - DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() - .addDelete(KEY1.toPb()) - .build(); + List pbs = new LinkedList<>(); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder().setDelete(KEY1.toPb()).build()); batchWriter.update(ENTITY1); batchWriter.delete(KEY1); - assertEquals(pb, batchWriter.toMutationPb().build()); + assertEquals(pbs, batchWriter.toMutationPbList()); } @Test public void testDeleteAfterPut() throws Exception { - DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() - .addDelete(KEY1.toPb()) - .build(); + List pbs = new LinkedList<>(); + pbs.add(com.google.datastore.v1beta3.Mutation.newBuilder().setDelete(KEY1.toPb()).build()); batchWriter.put(ENTITY1); batchWriter.delete(KEY1); - assertEquals(pb, batchWriter.toMutationPb().build()); + assertEquals(pbs, batchWriter.toMutationPbList()); } - */ @Test(expected = DatastoreException.class) public void testDeleteWhenNotActive() throws Exception { diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseKeyTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseKeyTest.java index 79576748bb58..c8773243ed69 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseKeyTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseKeyTest.java @@ -17,7 +17,6 @@ package com.google.gcloud.datastore; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import com.google.common.collect.ImmutableList; diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java index e7dc71c50ff6..97aeec3ee129 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java @@ -65,7 +65,7 @@ public void testHost() throws Exception { @Test public void testNamespace() throws Exception { - assertNull(options.build().namespace()); + assertTrue(options.build().namespace().isEmpty()); assertEquals("ns1", options.namespace("ns1").build().namespace()); } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index 57342ed3e26d..689d2a7b8b6b 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -14,8 +14,6 @@ * limitations under the License. */ -// TODO(ajaykannan): fix me! -/* package com.google.gcloud.datastore; import static org.junit.Assert.assertEquals; @@ -27,8 +25,6 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import com.google.api.services.datastore.DatastoreV1; -import com.google.api.services.datastore.DatastoreV1.EntityResult; import com.google.common.collect.Iterators; import com.google.gcloud.RetryParams; import com.google.gcloud.datastore.Query.ResultType; @@ -116,12 +112,12 @@ public static void beforeClass() throws IOException, InterruptedException { public void setUp() throws IOException, InterruptedException { options = DatastoreOptions.builder() .projectId(PROJECT_ID) - .host("http://localhost:" + LocalGcdHelper.PORT) + .host("localhost:" + LocalGcdHelper.PORT) .build(); datastore = DatastoreFactory.instance().get(options); - StructuredQuery query = Query.keyQueryBuilder().build(); - QueryResults result = datastore.run(query); - datastore.delete(Iterators.toArray(result, Key.class)); + //StructuredQuery query = Query.keyQueryBuilder().build(); + //QueryResults result = datastore.run(query); + //datastore.delete(Iterators.toArray(result, Key.class)); datastore.add(ENTITY1, ENTITY2); } @@ -192,7 +188,7 @@ public void testTransactionWithRead() { assertEquals(DatastoreException.Code.ABORTED, expected.code()); } } - + /* TODO(ajaykannan): fix me! @Test public void testTransactionWithQuery() { Query query = Query.entityQueryBuilder() @@ -220,7 +216,7 @@ public void testTransactionWithQuery() { assertEquals(DatastoreException.Code.ABORTED, expected.code()); } } - + */ @Test public void testNewTransactionRollback() { Transaction transaction = datastore.newTransaction(); @@ -335,7 +331,7 @@ public void testNewBatch() { assertNull(entities.get(4)); assertEquals(5, entities.size()); } - + /* TODO(ajaykannan): fix me! @Test public void testRunGqlQueryNoCasting() { Query query1 = Query.gqlQueryBuilder(ResultType.ENTITY, "select * from " + KIND1).build(); @@ -452,7 +448,7 @@ public void testRunStructuredQuery() { assertFalse(results4.hasNext()); // TODO(ozarov): construct a test to verify nextQuery/pagination } - + */ @Test public void testAllocateId() { KeyFactory keyFactory = datastore.newKeyFactory().kind(KIND1); @@ -570,7 +566,6 @@ public void testAddEntity() { assertNotNull(datastore.get(entities.get(2).key())); } - @Test public void testUpdate() { List keys = datastore.fetch(ENTITY1.key(), ENTITY3.key()); @@ -639,10 +634,10 @@ public void testKeyFactory() { @Test public void testRetires() throws Exception { - DatastoreV1.LookupRequest requestPb = - DatastoreV1.LookupRequest.newBuilder().addKey(KEY1.toPb()).build(); - DatastoreV1.LookupResponse responsePb = DatastoreV1.LookupResponse.newBuilder() - .addFound(EntityResult.newBuilder().setEntity(ENTITY1.toPb())).build(); + com.google.datastore.v1beta3.LookupRequest requestPb = + com.google.datastore.v1beta3.LookupRequest.newBuilder().addKeys(KEY1.toPb()).build(); + com.google.datastore.v1beta3.LookupResponse responsePb = com.google.datastore.v1beta3.LookupResponse.newBuilder() + .addFound(com.google.datastore.v1beta3.EntityResult.newBuilder().setEntity(ENTITY1.toPb())).build(); DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class); DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class); EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) @@ -661,4 +656,3 @@ public void testRetires() throws Exception { EasyMock.verify(rpcFactoryMock, rpcMock); } } -*/ diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/KeyFactoryTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/KeyFactoryTest.java index 7acf8abbbf19..dacb348c2172 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/KeyFactoryTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/KeyFactoryTest.java @@ -17,7 +17,6 @@ package com.google.gcloud.datastore; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import org.junit.Before; diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java index 2d7f5802f247..0625f2afb38d 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java @@ -61,9 +61,9 @@ public class LocalGcdHelper { public static final String DEFAULT_PROJECT_ID = "projectid1"; public static final int PORT = 8080; - private static final String GCD = "gcd-v1beta2-rev1-2.1.2b"; + private static final String GCD = "gcd-v1beta3-0.0.1"; private static final String GCD_FILENAME = GCD + ".zip"; - private static final String MD5_CHECKSUM = "d84384cdfa8658e1204f4f8be51300e8"; + private static final String MD5_CHECKSUM = "496b16f32473d0de0c7a974bd0ee1461"; private static final URL GCD_URL; static { @@ -159,13 +159,13 @@ public void start() throws IOException, InterruptedException { } } // cleanup any possible data for the same project - File datasetFolder = new File(gcdFolder, GCD + '/' + projectId); + File datasetFolder = new File(gcdFolder, "gcd/" + projectId); deleteRecurse(datasetFolder.toPath()); // create the datastore for the project ProcessBuilder processBuilder = new ProcessBuilder() .redirectError(ProcessBuilder.Redirect.INHERIT) - .directory(new File(gcdFolder, GCD)); + .directory(new File(gcdFolder, "gcd")); if (isWindows()) { processBuilder.command("cmd", "/C", "gcd.cmd", "create", "-p", projectId, projectId); processBuilder.redirectOutput(new File("NULL:")); @@ -179,7 +179,7 @@ public void start() throws IOException, InterruptedException { // start the datastore for the project processBuilder = new ProcessBuilder() - .directory(new File(gcdFolder, GCD)) + .directory(new File(gcdFolder, "gcd")) .redirectErrorStream(true); if (isWindows()) { processBuilder.command("cmd", "/C", "gcd.cmd", "start", "--testing", @@ -309,7 +309,7 @@ public static void main(String... args) throws IOException, InterruptedException public static boolean isActive(String projectId) { try { StringBuilder urlBuilder = new StringBuilder("http://localhost:").append(PORT); - urlBuilder.append("/datastore/v1beta2/datasets/").append(projectId).append("/lookup"); + urlBuilder.append("/datastore/v1beta3/datasets/").append(projectId).append(":lookup"); URL url = new URL(urlBuilder.toString()); try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), UTF_8))) { From 959d67d9f9f940b3db62b556076b2a90ba2fb263 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Fri, 25 Sep 2015 09:22:16 -0700 Subject: [PATCH 04/60] cleanup v1beta3 changes and fix LocalGcdHelper bugs --- .../datastore/BaseDatastoreBatchWriter.java | 4 --- .../google/gcloud/datastore/BatchImpl.java | 6 ++-- .../gcloud/datastore/DatastoreImpl.java | 17 +++++---- .../gcloud/datastore/DatastoreOptions.java | 3 +- .../gcloud/datastore/TransactionImpl.java | 6 ++-- .../gcloud/spi/DefaultDatastoreRpc.java | 36 ++++++++++++------- .../datastore/DatastoreOptionsTest.java | 1 - .../gcloud/datastore/LocalGcdHelper.java | 2 +- 8 files changed, 40 insertions(+), 35 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseDatastoreBatchWriter.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseDatastoreBatchWriter.java index 6d8ac86834de..3aa0d38f45f1 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseDatastoreBatchWriter.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseDatastoreBatchWriter.java @@ -184,10 +184,6 @@ protected Set toDelete() { return toDelete; } - protected int numAutoAllocatedIds() { - return toAddAutoId.size(); - } - protected void deactivate() { active = false; } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BatchImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BatchImpl.java index a9eab9e9c3f1..303e9703f4cc 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BatchImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BatchImpl.java @@ -16,8 +16,8 @@ package com.google.gcloud.datastore; +import java.util.ArrayList; import java.util.Iterator; -import java.util.LinkedList; import java.util.List; @@ -39,7 +39,7 @@ static class ResponseImpl implements Batch.Response { public List generatedKeys() { Iterator results = response.getMutationResultsList().iterator(); - List generated = new LinkedList(); + List generated = new ArrayList<>(numAutoAllocatedIds); for (int i = 0; i < numAutoAllocatedIds; i++) { generated.add(Key.fromPb(results.next().getKey())); } @@ -62,7 +62,7 @@ public Batch.Response submit() { requestPb.addAllMutations(mutationsPb); com.google.datastore.v1beta3.CommitResponse responsePb = datastore.commit(requestPb.build()); deactivate(); - return new ResponseImpl(responsePb, numAutoAllocatedIds()); + return new ResponseImpl(responsePb, toAddAutoId().size()); } @Override diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java index 262098e82f96..f0238083b5fa 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java @@ -176,33 +176,32 @@ public List add(FullEntity... entities) { return Collections.emptyList(); } List mutationsPb = new ArrayList<>(); - Map completeEntities = new LinkedHashMap<>(); + Set completeEntities = new LinkedHashSet<>(); for (FullEntity entity : entities) { Entity completeEntity = null; if (entity.key() instanceof Key) { completeEntity = Entity.convert((FullEntity) entity); } if (completeEntity != null) { - if (completeEntities.put(completeEntity.key(), completeEntity) != null) { + if (completeEntities.contains(completeEntity)) { throw DatastoreException.throwInvalidRequest( "Duplicate entity with the key %s", entity.key()); } - mutationsPb.add(com.google.datastore.v1beta3.Mutation.newBuilder() - .setInsert(completeEntity.toPb()).build()); + completeEntities.add(completeEntity); } else { Preconditions.checkArgument(entity.hasKey(), "entity %s is missing a key", entity); - mutationsPb.add(com.google.datastore.v1beta3.Mutation.newBuilder() - .setInsert(entity.toPb()).build()); } + mutationsPb.add(com.google.datastore.v1beta3.Mutation.newBuilder() + .setInsert(entity.toPb()).build()); } com.google.datastore.v1beta3.CommitResponse commitResponse = commitMutation(mutationsPb); Iterator mutationResults = commitResponse.getMutationResultsList().iterator(); + Iterator completeEntitiesIt = completeEntities.iterator(); ImmutableList.Builder responseBuilder = ImmutableList.builder(); for (FullEntity entity : entities) { - Entity completeEntity = completeEntities.get(entity.key()); - if (completeEntity != null) { - responseBuilder.add(completeEntity); + if (completeEntities.contains(entity)) { + responseBuilder.add(completeEntitiesIt.next()); mutationResults.next(); } else { responseBuilder.add( diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java index 23128a856761..a354f7328959 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java @@ -108,7 +108,8 @@ private DatastoreOptions normalize() { .build(); requestPb.addKeys(key); try { - com.google.datastore.v1beta3.LookupResponse responsePb = datastoreRpc().lookup(requestPb.build()); + com.google.datastore.v1beta3.LookupResponse responsePb = + datastoreRpc().lookup(requestPb.build()); if (responsePb.getDeferredCount() > 0) { key = responsePb.getDeferred(0); } else { diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionImpl.java index ff9ce5e50e9d..5a422927005a 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionImpl.java @@ -18,8 +18,8 @@ import com.google.protobuf.ByteString; +import java.util.ArrayList; import java.util.Iterator; -import java.util.LinkedList; import java.util.List; final class TransactionImpl extends BaseDatastoreBatchWriter implements Transaction { @@ -42,7 +42,7 @@ static class ResponseImpl implements Transaction.Response { public List generatedKeys() { Iterator results = response.getMutationResultsList().iterator(); - List generated = new LinkedList(); + List generated = new ArrayList<>(numAutoAllocatedIds); for (int i = 0; i < numAutoAllocatedIds; i++) { generated.add(Key.fromPb(results.next().getKey())); } @@ -98,7 +98,7 @@ public Transaction.Response commit() { requestPb.addAllMutations(mutationsPb); com.google.datastore.v1beta3.CommitResponse responsePb = datastore.commit(requestPb.build()); deactivate(); - return new ResponseImpl(responsePb, numAutoAllocatedIds()); + return new ResponseImpl(responsePb, toAddAutoId().size()); } @Override diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java index 1ae0b6eea22e..3c82e2a93cc4 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java @@ -20,6 +20,10 @@ import com.google.gcloud.datastore.DatastoreOptions; import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException.Reason; +import java.net.InetAddress; +import java.net.MalformedURLException; +import java.net.UnknownHostException; +import java.net.URL; import java.util.HashMap; import java.util.Map; @@ -42,20 +46,26 @@ public class DefaultDatastoreRpc implements DatastoreRpc { } public DefaultDatastoreRpc(DatastoreOptions options) { - if (options.host().contains("localhost")) { - client = com.google.datastore.v1beta3.client.DatastoreFactory.get().create( - new com.google.datastore.v1beta3.client.DatastoreOptions.Builder() - .projectId(options.projectId()) - .localHost(options.host()) - .initializer(options.httpRequestInitializer()) - .build()); - } else { - client = com.google.datastore.v1beta3.client.DatastoreFactory.get().create( - new com.google.datastore.v1beta3.client.DatastoreOptions.Builder() - .projectId(options.projectId()) - .initializer(options.httpRequestInitializer()) - .build()); + com.google.datastore.v1beta3.client.DatastoreOptions.Builder clientBuilder = + new com.google.datastore.v1beta3.client.DatastoreOptions.Builder() + .projectId(options.projectId()) + .initializer(options.httpRequestInitializer()); + if (options.host() != null) { + try { + String normalizedHost = options.host(); + if (!normalizedHost.startsWith("http")) { + normalizedHost = "http://" + normalizedHost; + } + InetAddress hostAddr = InetAddress.getByName(new URL(normalizedHost).getHost()); + if (hostAddr.isAnyLocalAddress() || hostAddr.isLoopbackAddress()) { + clientBuilder = clientBuilder.localHost(options.host()); + } + } catch (UnknownHostException | MalformedURLException e) { + // ignore + } } + client = com.google.datastore.v1beta3.client.DatastoreFactory.get() + .create(clientBuilder.build()); } private static DatastoreRpcException translate( diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java index 97aeec3ee129..63657902d737 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java @@ -18,7 +18,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java index 0625f2afb38d..1d692040c015 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java @@ -309,7 +309,7 @@ public static void main(String... args) throws IOException, InterruptedException public static boolean isActive(String projectId) { try { StringBuilder urlBuilder = new StringBuilder("http://localhost:").append(PORT); - urlBuilder.append("/datastore/v1beta3/datasets/").append(projectId).append(":lookup"); + urlBuilder.append("/datastore/v1beta3/projects/").append(projectId).append(":lookup"); URL url = new URL(urlBuilder.toString()); try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), UTF_8))) { From 9463c5592e230e7384960664119781e642a87c45 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Wed, 30 Sep 2015 18:33:27 -0700 Subject: [PATCH 05/60] Fix handling of host in DatastoreOptions.java --- .../gcloud/datastore/DatastoreImpl.java | 11 +++---- .../gcloud/datastore/DatastoreOptions.java | 26 +++++----------- .../gcloud/spi/DefaultDatastoreRpc.java | 31 +++++++++++++++---- .../datastore/DatastoreOptionsTest.java | 9 +----- .../gcloud/datastore/SerializationTest.java | 1 - 5 files changed, 38 insertions(+), 40 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java index f0238083b5fa..59804831872c 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java @@ -176,18 +176,17 @@ public List add(FullEntity... entities) { return Collections.emptyList(); } List mutationsPb = new ArrayList<>(); - Set completeEntities = new LinkedHashSet<>(); + Map completeEntities = new LinkedHashMap<>(); for (FullEntity entity : entities) { Entity completeEntity = null; if (entity.key() instanceof Key) { completeEntity = Entity.convert((FullEntity) entity); } if (completeEntity != null) { - if (completeEntities.contains(completeEntity)) { + if (completeEntities.put(completeEntity.key(), completeEntity) != null) { throw DatastoreException.throwInvalidRequest( "Duplicate entity with the key %s", entity.key()); } - completeEntities.add(completeEntity); } else { Preconditions.checkArgument(entity.hasKey(), "entity %s is missing a key", entity); } @@ -197,11 +196,11 @@ public List add(FullEntity... entities) { com.google.datastore.v1beta3.CommitResponse commitResponse = commitMutation(mutationsPb); Iterator mutationResults = commitResponse.getMutationResultsList().iterator(); - Iterator completeEntitiesIt = completeEntities.iterator(); ImmutableList.Builder responseBuilder = ImmutableList.builder(); for (FullEntity entity : entities) { - if (completeEntities.contains(entity)) { - responseBuilder.add(completeEntitiesIt.next()); + Entity completeEntity = completeEntities.get(entity.key()); + if (completeEntity != null) { + responseBuilder.add(completeEntity); mutationResults.next(); } else { responseBuilder.add( diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java index a354f7328959..fe318ef8720f 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java @@ -35,14 +35,11 @@ public class DatastoreOptions extends ServiceOptions { private static final long serialVersionUID = -8636602944160689193L; - private static final String DATASET_ENV_NAME = "DATASTORE_DATASET"; - private static final String HOST_ENV_NAME = "DATASTORE_HOST"; private static final String DATASTORE_SCOPE = "https://www.googleapis.com/auth/datastore"; private static final String USERINFO_SCOPE = "https://www.googleapis.com/auth/userinfo.email"; private static final Set SCOPES = ImmutableSet.of(DATASTORE_SCOPE, USERINFO_SCOPE); private final String namespace; - private final boolean force; private final boolean normalizeDataset; private transient DatastoreRpc datastoreRpc; @@ -50,7 +47,6 @@ public static class Builder extends ServiceOptions.Builder { private String namespace; - private boolean force; private boolean normalizeDataset = true; private Builder() { @@ -58,7 +54,6 @@ private Builder() { private Builder(DatastoreOptions options) { super(options); - force = options.force; namespace = options.namespace; normalizeDataset = options.normalizeDataset; } @@ -74,11 +69,6 @@ public Builder namespace(String namespace) { return this; } - public Builder force(boolean force) { - this.force = force; - return this; - } - Builder normalizeDataset(boolean normalizeDataset) { this.normalizeDataset = normalizeDataset; return this; @@ -89,7 +79,6 @@ private DatastoreOptions(Builder builder) { super(builder); normalizeDataset = builder.normalizeDataset; namespace = builder.namespace != null ? builder.namespace : defaultNamespace(); - force = builder.force; } private DatastoreOptions normalize() { @@ -126,13 +115,17 @@ private DatastoreOptions normalize() { @Override protected String defaultHost() { - String host = System.getProperty(HOST_ENV_NAME, System.getenv(HOST_ENV_NAME)); + String host = System.getProperty( + com.google.datastore.v1beta3.client.DatastoreHelper.LOCAL_HOST_ENV_VAR, + System.getenv(com.google.datastore.v1beta3.client.DatastoreHelper.LOCAL_HOST_ENV_VAR)); return host != null ? host : super.defaultHost(); } @Override protected String defaultProject() { - String projectId = System.getProperty(DATASET_ENV_NAME, System.getenv(DATASET_ENV_NAME)); + String projectId = System.getProperty( + com.google.datastore.v1beta3.client.DatastoreHelper.PROJECT_ID_ENV_VAR, + System.getenv(com.google.datastore.v1beta3.client.DatastoreHelper.PROJECT_ID_ENV_VAR)); if (projectId == null) { projectId = appEngineAppId(); } @@ -157,10 +150,6 @@ private static String defaultNamespace() { } } - public boolean force() { - return force; - } - @Override protected Set scopes() { return SCOPES; @@ -173,7 +162,7 @@ public Builder toBuilder() { @Override public int hashCode() { - return baseHashCode() ^ Objects.hash(namespace, force, normalizeDataset); + return baseHashCode() ^ Objects.hash(namespace, normalizeDataset); } @Override @@ -183,7 +172,6 @@ public boolean equals(Object obj) { } DatastoreOptions other = (DatastoreOptions) obj; return baseEquals(other) && Objects.equals(namespace, other.namespace) - && Objects.equals(force, other.force) && Objects.equals(normalizeDataset, other.normalizeDataset); } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java index 3c82e2a93cc4..31fe2f8e0d66 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java @@ -50,22 +50,41 @@ public DefaultDatastoreRpc(DatastoreOptions options) { new com.google.datastore.v1beta3.client.DatastoreOptions.Builder() .projectId(options.projectId()) .initializer(options.httpRequestInitializer()); - if (options.host() != null) { + if (isLocalHost(options.host())) { + clientBuilder = clientBuilder.localHost(options.host()); + } else if (!options.host() + .equals(com.google.datastore.v1beta3.client.DatastoreFactory.DEFAULT_HOST)) { + String fullURL = options.host(); + if (fullURL.charAt(fullURL.length() - 1) != '/') { + fullURL = fullURL + '/'; + } + fullURL = fullURL + "datastore/v1beta3/projects/" + options.projectId(); + clientBuilder = clientBuilder.projectId(null).projectEndpoint(fullURL); + } + client = com.google.datastore.v1beta3.client.DatastoreFactory.get() + .create(clientBuilder.build()); + } + + private static boolean isLocalHost(String host) { + if (host != null) { try { - String normalizedHost = options.host(); - if (!normalizedHost.startsWith("http")) { + String normalizedHost = host; + if (!includesScheme(normalizedHost)) { normalizedHost = "http://" + normalizedHost; } InetAddress hostAddr = InetAddress.getByName(new URL(normalizedHost).getHost()); if (hostAddr.isAnyLocalAddress() || hostAddr.isLoopbackAddress()) { - clientBuilder = clientBuilder.localHost(options.host()); + return true; } } catch (UnknownHostException | MalformedURLException e) { // ignore } } - client = com.google.datastore.v1beta3.client.DatastoreFactory.get() - .create(clientBuilder.build()); + return false; + } + + private static boolean includesScheme(String url) { + return url.startsWith("http://") || url.startsWith("https://"); } private static DatastoreRpcException translate( diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java index 63657902d737..161dfc078f6c 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java @@ -68,12 +68,6 @@ public void testNamespace() throws Exception { assertEquals("ns1", options.namespace("ns1").build().namespace()); } - @Test - public void testForce() throws Exception { - assertFalse(options.build().force()); - assertTrue(options.force(true).build().force()); - } - @Test public void testDatastore() throws Exception { assertSame(datastoreRpcFactory, options.build().serviceRpcFactory()); @@ -82,12 +76,11 @@ public void testDatastore() throws Exception { @Test public void testToBuilder() throws Exception { - DatastoreOptions original = options.namespace("ns1").force(true).build(); + DatastoreOptions original = options.namespace("ns1").build(); DatastoreOptions copy = original.toBuilder().build(); assertEquals(original.projectId(), copy.projectId()); assertEquals(original.namespace(), copy.namespace()); assertEquals(original.host(), copy.host()); - assertEquals(original.force(), copy.force()); assertEquals(original.retryParams(), copy.retryParams()); assertEquals(original.authCredentials(), copy.authCredentials()); } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java index 624689cd420e..bd4a683f9c9c 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java @@ -148,7 +148,6 @@ public void testServiceOptions() throws Exception { .namespace("ns1") .retryParams(RetryParams.getDefaultInstance()) .authCredentials(AuthCredentials.noCredentials()) - .force(true) .build(); serializedCopy = serializeAndDeserialize(options); assertEquals(options, serializedCopy); From 4d2fe25b88f32abb9cc4c3d4bc01fbfde1221a79 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 5 Oct 2015 08:03:49 -0700 Subject: [PATCH 06/60] Remove scheme from localhost and check if user-provided host is null/empty --- .../gcloud/spi/DefaultDatastoreRpc.java | 24 +++++++++++++------ .../gcloud/datastore/LocalGcdHelper.java | 3 ++- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java index 31fe2f8e0d66..c6e3f396e574 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java @@ -16,6 +16,7 @@ package com.google.gcloud.spi; +import com.google.common.base.Strings; import com.google.common.collect.ImmutableMap; import com.google.gcloud.datastore.DatastoreOptions; import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException.Reason; @@ -51,14 +52,16 @@ public DefaultDatastoreRpc(DatastoreOptions options) { .projectId(options.projectId()) .initializer(options.httpRequestInitializer()); if (isLocalHost(options.host())) { - clientBuilder = clientBuilder.localHost(options.host()); - } else if (!options.host() - .equals(com.google.datastore.v1beta3.client.DatastoreFactory.DEFAULT_HOST)) { + clientBuilder = clientBuilder.localHost(removeScheme(options.host())); + } else if (!com.google.datastore.v1beta3.client.DatastoreFactory.DEFAULT_HOST + .equals(options.host()) && !Strings.isNullOrEmpty(options.host())) { String fullURL = options.host(); if (fullURL.charAt(fullURL.length() - 1) != '/') { fullURL = fullURL + '/'; } - fullURL = fullURL + "datastore/v1beta3/projects/" + options.projectId(); + fullURL = fullURL + "datastore/" + + com.google.datastore.v1beta3.client.DatastoreFactory.VERSION + "/projects/" + + options.projectId(); clientBuilder = clientBuilder.projectId(null).projectEndpoint(fullURL); } client = com.google.datastore.v1beta3.client.DatastoreFactory.get() @@ -73,9 +76,7 @@ private static boolean isLocalHost(String host) { normalizedHost = "http://" + normalizedHost; } InetAddress hostAddr = InetAddress.getByName(new URL(normalizedHost).getHost()); - if (hostAddr.isAnyLocalAddress() || hostAddr.isLoopbackAddress()) { - return true; - } + return hostAddr.isAnyLocalAddress() || hostAddr.isLoopbackAddress(); } catch (UnknownHostException | MalformedURLException e) { // ignore } @@ -87,6 +88,15 @@ private static boolean includesScheme(String url) { return url.startsWith("http://") || url.startsWith("https://"); } + private static String removeScheme(String url) { + if (url.startsWith("https://")) { + return url.substring("https://".length()); + } else if (url.startsWith("http://")) { + return url.substring("http://".length()); + } + return url; + } + private static DatastoreRpcException translate( com.google.datastore.v1beta3.client.DatastoreException exception) { String reasonStr = ""; diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java index 1d692040c015..1054b461ab0d 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java @@ -309,7 +309,8 @@ public static void main(String... args) throws IOException, InterruptedException public static boolean isActive(String projectId) { try { StringBuilder urlBuilder = new StringBuilder("http://localhost:").append(PORT); - urlBuilder.append("/datastore/v1beta3/projects/").append(projectId).append(":lookup"); + urlBuilder.append("/datastore/" + com.google.datastore.v1beta3.client.DatastoreFactory.VERSION + + "/projects/").append(projectId).append(":lookup"); URL url = new URL(urlBuilder.toString()); try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), UTF_8))) { From e0aaaba871960a006fb82dc5972c015ea987c980 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 5 Oct 2015 09:27:23 -0700 Subject: [PATCH 07/60] Fix version in LocalGcdHelper and check for null values when removing URL scheme --- .../com/google/gcloud/spi/DefaultDatastoreRpc.java | 10 ++++++---- .../com/google/gcloud/datastore/LocalGcdHelper.java | 3 +-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java index c6e3f396e574..3063dda6eca3 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java @@ -89,10 +89,12 @@ private static boolean includesScheme(String url) { } private static String removeScheme(String url) { - if (url.startsWith("https://")) { - return url.substring("https://".length()); - } else if (url.startsWith("http://")) { - return url.substring("http://".length()); + if (url != null) { + if (url.startsWith("https://")) { + return url.substring("https://".length()); + } else if (url.startsWith("http://")) { + return url.substring("http://".length()); + } } return url; } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java index 1054b461ab0d..1d692040c015 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java @@ -309,8 +309,7 @@ public static void main(String... args) throws IOException, InterruptedException public static boolean isActive(String projectId) { try { StringBuilder urlBuilder = new StringBuilder("http://localhost:").append(PORT); - urlBuilder.append("/datastore/" + com.google.datastore.v1beta3.client.DatastoreFactory.VERSION - + "/projects/").append(projectId).append(":lookup"); + urlBuilder.append("/datastore/v1beta3/projects/").append(projectId).append(":lookup"); URL url = new URL(urlBuilder.toString()); try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), UTF_8))) { From c029c378fd2d09928802c75f8ca9c21e2aafb477 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 5 Oct 2015 15:55:10 -0700 Subject: [PATCH 08/60] Update query-related datastore code to v1beta3 --- .../com/google/gcloud/datastore/Blob.java | 10 +- .../com/google/gcloud/datastore/Cursor.java | 15 +- .../gcloud/datastore/DatastoreImpl.java | 4 +- .../com/google/gcloud/datastore/GqlQuery.java | 129 ++++++------ .../com/google/gcloud/datastore/Query.java | 59 +++--- .../google/gcloud/datastore/QueryResults.java | 1 - .../gcloud/datastore/QueryResultsImpl.java | 44 ++-- .../gcloud/datastore/StructuredQuery.java | 191 ++++++++---------- .../gcloud/datastore/DatastoreTest.java | 19 +- .../gcloud/datastore/SerializationTest.java | 12 +- 10 files changed, 221 insertions(+), 263 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Blob.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Blob.java index 5a759240be38..fbe2887d9b35 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Blob.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Blob.java @@ -18,8 +18,6 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.api.services.datastore.DatastoreV1; -import com.google.api.services.datastore.DatastoreV1.Value; import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects.ToStringHelper; import com.google.protobuf.ByteString; @@ -37,7 +35,7 @@ * * @see Google Cloud Datastore Entities, Properties, and Keys */ -public final class Blob extends Serializable { +public final class Blob extends Serializable { private static final long serialVersionUID = 3835421019618247721L; @@ -146,12 +144,12 @@ public static Blob copyFrom(InputStream input) throws IOException { } @Override - protected Value toPb() { - return DatastoreV1.Value.newBuilder().setBlobValue(byteString).build(); + protected com.google.datastore.v1beta3.Value toPb() { + return com.google.datastore.v1beta3.Value.newBuilder().setBlobValue(byteString).build(); } @Override protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException { - return new Blob(DatastoreV1.Value.parseFrom(bytesPb).getBlobValue()); + return new Blob(com.google.datastore.v1beta3.Value.parseFrom(bytesPb).getBlobValue()); } } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Cursor.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Cursor.java index 42a8cee8e5a2..df237e4d897c 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Cursor.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Cursor.java @@ -19,8 +19,6 @@ import static com.google.common.base.Preconditions.checkNotNull; import static java.nio.charset.StandardCharsets.UTF_8; -import com.google.api.services.datastore.DatastoreV1; -import com.google.api.services.datastore.DatastoreV1.Value; import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects.ToStringHelper; import com.google.common.base.Preconditions; @@ -37,7 +35,7 @@ * A Google Cloud Datastore cursor. * The cursor can be used to as a starting point or an ending point for a {@link Query} */ -public final class Cursor extends Serializable { +public final class Cursor extends Serializable { private static final long serialVersionUID = -1423744878777486541L; @@ -89,7 +87,8 @@ public String toUrlSafe() { public static Cursor fromUrlSafe(String urlSafe) { try { String utf8Str = URLDecoder.decode(urlSafe, UTF_8.name()); - DatastoreV1.Value.Builder builder = DatastoreV1.Value.newBuilder(); + com.google.datastore.v1beta3.Value.Builder builder = + com.google.datastore.v1beta3.Value.newBuilder(); TextFormat.merge(utf8Str, builder); return fromPb(builder.build()); } catch (UnsupportedEncodingException | ParseException e) { @@ -102,16 +101,16 @@ public static Cursor copyFrom(byte[] bytes) { } @Override - protected Value toPb() { - return DatastoreV1.Value.newBuilder().setBlobValue(byteString).build(); + protected com.google.datastore.v1beta3.Value toPb() { + return com.google.datastore.v1beta3.Value.newBuilder().setBlobValue(byteString).build(); } @Override protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException { - return fromPb(DatastoreV1.Value.parseFrom(bytesPb)); + return fromPb(com.google.datastore.v1beta3.Value.parseFrom(bytesPb)); } - static Cursor fromPb(DatastoreV1.Value valuePb) { + static Cursor fromPb(com.google.datastore.v1beta3.Value valuePb) { return new Cursor(valuePb.getBlobValue()); } } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java index 59804831872c..fdd8917d11e2 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java @@ -99,9 +99,7 @@ public QueryResults run(Query query) { } QueryResults run(com.google.datastore.v1beta3.ReadOptions readOptionsPb, Query query) { - // TODO(ajaykannan): fix me! - //return new QueryResultsImpl<>(this, readOptionsPb, query); - return null; // TODO(ajaykannan): fix me! + return new QueryResultsImpl<>(this, readOptionsPb, query); } com.google.datastore.v1beta3.RunQueryResponse runQuery( diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java index 54110a89c3e0..2c02aa7b970c 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java @@ -19,7 +19,6 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.gcloud.datastore.Validator.validateNamespace; -import com.google.api.services.datastore.DatastoreV1; import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -75,25 +74,22 @@ public final class GqlQuery extends Query { private final transient String queryString; private final transient boolean allowLiteral; - private final transient ImmutableList namedBindings; + private final transient ImmutableMap namedBindings; private final transient ImmutableList positionalBindings; - static final class Binding extends Serializable { + static final class Binding extends Serializable { private static final long serialVersionUID = 1976895435257636275L; - private final transient String name; private final transient Cursor cursor; private final transient Value value; - Binding(String name, Cursor cursor) { - this.name = name; + Binding(Cursor cursor) { this.cursor = checkNotNull(cursor); value = null; } - Binding(String name, Value value) { - this.name = name; + Binding(Value value) { this.value = checkNotNull(value); cursor = null; } @@ -102,13 +98,9 @@ Object cursorOrValue() { return MoreObjects.firstNonNull(cursor, value); } - String name() { - return name; - } - @Override public int hashCode() { - return Objects.hash(name, cursor, value); + return Objects.hash(cursor, value); } @Override @@ -120,40 +112,36 @@ public boolean equals(Object obj) { return false; } Binding other = (Binding) obj; - return Objects.equals(name, other.name) - && Objects.equals(cursor, other.cursor) - && Objects.equals(value, other.value); + return Objects.equals(cursor, other.cursor) && Objects.equals(value, other.value); } @Override - protected DatastoreV1.GqlQueryArg toPb() { - DatastoreV1.GqlQueryArg.Builder argPb = DatastoreV1.GqlQueryArg.newBuilder(); - if (name != null) { - argPb.setName(name); - } + protected com.google.datastore.v1beta3.GqlQueryParameter toPb() { + com.google.datastore.v1beta3.GqlQueryParameter.Builder argPb = + com.google.datastore.v1beta3.GqlQueryParameter.newBuilder(); if (cursor != null) { argPb.setCursor(cursor.byteString()); } if (value != null) { - // TODO(ajaykannan): fix me! - //argPb.setValue(value.toPb()); + argPb.setValue(value.toPb()); } return argPb.build(); } @Override protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException { - return fromPb(DatastoreV1.GqlQueryArg.parseFrom(bytesPb)); + return fromPb(com.google.datastore.v1beta3.GqlQueryParameter.parseFrom(bytesPb)); } - static Binding fromPb(DatastoreV1.GqlQueryArg argPb) { - String name = argPb.hasName() ? argPb.getName() : null; - if (argPb.hasCursor()) { - return new Binding(name, new Cursor(argPb.getCursor())); + static Binding fromPb(com.google.datastore.v1beta3.GqlQueryParameter argPb) { + switch (argPb.getParameterTypeCase()) { + case CURSOR: + return new Binding(new Cursor(argPb.getCursor())); + case VALUE: + return new Binding(Value.fromPb(argPb.getValue())); + default: + return null; } - // TODO(ajaykannan): fix me! - //return new Binding(name, Value.fromPb(argPb.getValue())); - return new Binding(name, new Cursor(null)); // TODO(ajaykannan): fix me! } } @@ -196,52 +184,52 @@ public Builder clearBindings() { } public Builder setBinding(String name, Cursor cursor) { - namedBindings.put(name, new Binding(name, cursor)); + namedBindings.put(name, new Binding(cursor)); return this; } public Builder setBinding(String name, String... value) { - namedBindings.put(name, toBinding(name, StringValue.MARSHALLER, Arrays.asList(value))); + namedBindings.put(name, toBinding(StringValue.MARSHALLER, Arrays.asList(value))); return this; } public Builder setBinding(String name, long... value) { - namedBindings.put(name, toBinding(name, LongValue.MARSHALLER, Longs.asList(value))); + namedBindings.put(name, toBinding(LongValue.MARSHALLER, Longs.asList(value))); return this; } public Builder setBinding(String name, double... value) { - namedBindings.put(name, toBinding(name, DoubleValue.MARSHALLER, Doubles.asList(value))); + namedBindings.put(name, toBinding(DoubleValue.MARSHALLER, Doubles.asList(value))); return this; } public Builder setBinding(String name, boolean... value) { - namedBindings.put(name, toBinding(name, BooleanValue.MARSHALLER, Booleans.asList(value))); + namedBindings.put(name, toBinding(BooleanValue.MARSHALLER, Booleans.asList(value))); return this; } public Builder setBinding(String name, DateTime... value) { - namedBindings.put(name, toBinding(name, DateTimeValue.MARSHALLER, Arrays.asList(value))); + namedBindings.put(name, toBinding(DateTimeValue.MARSHALLER, Arrays.asList(value))); return this; } public Builder setBinding(String name, Key... value) { - namedBindings.put(name, toBinding(name, KeyValue.MARSHALLER, Arrays.asList(value))); + namedBindings.put(name, toBinding(KeyValue.MARSHALLER, Arrays.asList(value))); return this; } public Builder setBinding(String name, FullEntity... value) { - namedBindings.put(name, toBinding(name, EntityValue.MARSHALLER, Arrays.asList(value))); + namedBindings.put(name, toBinding(EntityValue.MARSHALLER, Arrays.asList(value))); return this; } public Builder setBinding(String name, Blob... value) { - namedBindings.put(name, toBinding(name, BlobValue.MARSHALLER, Arrays.asList(value))); + namedBindings.put(name, toBinding(BlobValue.MARSHALLER, Arrays.asList(value))); return this; } public Builder addBinding(Cursor cursor) { - positionalBindings.add(new Binding(null, cursor)); + positionalBindings.add(new Binding(cursor)); return this; } @@ -289,11 +277,7 @@ public GqlQuery build() { return new GqlQuery<>(this); } - private static Binding toBinding(Value.BuilderFactory builderFactory, List values) { - return toBinding(null, builderFactory, values); - } - - private static Binding toBinding(String name, Value.BuilderFactory builderFactory, + private static Binding toBinding(Value.BuilderFactory builderFactory, List values) { List> list = new ArrayList<>(values.size()); for (Object object : values) { @@ -309,7 +293,7 @@ private static Binding toBinding(String name, Value.BuilderFactory } else { value = new ListValue(list); } - return new Binding(name, value); + return new Binding(value); } } @@ -317,7 +301,7 @@ private GqlQuery(Builder builder) { super(builder.resultType, builder.namespace); queryString = builder.queryString; allowLiteral = builder.allowLiteral; - namedBindings = ImmutableList.copyOf(builder.namedBindings.values()); + namedBindings = ImmutableMap.copyOf(builder.namedBindings); positionalBindings = ImmutableList.copyOf(builder.positionalBindings); } @@ -334,8 +318,8 @@ public boolean allowLiteral() { */ public Map namedBindings() { ImmutableMap.Builder builder = ImmutableSortedMap.naturalOrder(); - for (Binding binding : namedBindings) { - builder.put(binding.name(), binding.cursorOrValue()); + for (Map.Entry binding : namedBindings.entrySet()) { + builder.put(binding.getKey(), binding.getValue().cursorOrValue()); } return builder.build(); } @@ -373,26 +357,29 @@ public boolean equals(Object obj) { } @Override - protected DatastoreV1.GqlQuery toPb() { - DatastoreV1.GqlQuery.Builder queryPb = DatastoreV1.GqlQuery.newBuilder(); + protected com.google.datastore.v1beta3.GqlQuery toPb() { + com.google.datastore.v1beta3.GqlQuery.Builder queryPb = + com.google.datastore.v1beta3.GqlQuery.newBuilder(); queryPb.setQueryString(queryString); - queryPb.setAllowLiteral(allowLiteral); - for (Binding argument : namedBindings) { - queryPb.addNameArg(argument.toPb()); + queryPb.setAllowLiterals(allowLiteral); + Map namedBindingsPb = + queryPb.getMutableNamedBindings(); + for (Map.Entry entry : namedBindings.entrySet()) { + namedBindingsPb.put(entry.getKey(), entry.getValue().toPb()); } for (Binding argument : positionalBindings) { - queryPb.addNumberArg(argument.toPb()); + queryPb.addPositionalBindings(argument.toPb()); } return queryPb.build(); } @Override - protected void populatePb(DatastoreV1.RunQueryRequest.Builder requestPb) { + protected void populatePb(com.google.datastore.v1beta3.RunQueryRequest.Builder requestPb) { requestPb.setGqlQuery(toPb()); } @Override - protected GqlQuery nextQuery(DatastoreV1.QueryResultBatch responsePb) { + protected GqlQuery nextQuery(com.google.datastore.v1beta3.QueryResultBatch responsePb) { // See issue #17 throw new UnsupportedOperationException("paging for this query is not implemented yet"); } @@ -400,23 +387,27 @@ protected GqlQuery nextQuery(DatastoreV1.QueryResultBatch responsePb) { @Override protected Object fromPb(ResultType resultType, String namespace, byte[] bytesPb) throws InvalidProtocolBufferException { - return fromPb(resultType, namespace, DatastoreV1.GqlQuery.parseFrom(bytesPb)); + return fromPb(resultType, namespace, com.google.datastore.v1beta3.GqlQuery.parseFrom(bytesPb)); } private static GqlQuery fromPb( - ResultType resultType, String ns, DatastoreV1.GqlQuery queryPb) { + ResultType resultType, String ns, com.google.datastore.v1beta3.GqlQuery queryPb) { Builder builder = new Builder<>(resultType, queryPb.getQueryString()); builder.namespace(ns); - if (queryPb.hasAllowLiteral()) { - builder.allowLiteral = queryPb.getAllowLiteral(); - } - for (DatastoreV1.GqlQueryArg nameArg : queryPb.getNameArgList()) { - Binding argument = Binding.fromPb(nameArg); - builder.namedBindings.put(argument.name(), argument); + builder.allowLiteral = queryPb.getAllowLiterals(); + for (Map.Entry nameArg + : queryPb.getNamedBindings().entrySet()) { + Binding currBinding = Binding.fromPb(nameArg.getValue()); + if (currBinding != null) { + builder.namedBindings.put(nameArg.getKey(), currBinding); + } } - for (DatastoreV1.GqlQueryArg numberArg : queryPb.getNumberArgList()) { - Binding argument = Binding.fromPb(numberArg); - builder.positionalBindings.add(argument); + for (com.google.datastore.v1beta3.GqlQueryParameter numberArg + : queryPb.getPositionalBindingsList()) { + Binding currBinding = Binding.fromPb(numberArg); + if (currBinding != null) { + builder.positionalBindings.add(currBinding); + } } return builder.build(); } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java index 7fae120dd402..6e962d92fb2f 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java @@ -18,7 +18,6 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.api.services.datastore.DatastoreV1; import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects.ToStringHelper; import com.google.common.collect.Maps; @@ -54,68 +53,65 @@ public abstract class Query extends Serializable { public abstract static class ResultType implements java.io.Serializable { private static final long serialVersionUID = 2104157695425806623L; - private static final Map> - PB_TO_INSTANCE = Maps.newEnumMap(DatastoreV1.EntityResult.ResultType.class); + private static final Map> + PB_TO_INSTANCE = Maps.newEnumMap( + com.google.datastore.v1beta3.EntityResult.ResultType.class); static final ResultType UNKNOWN = new ResultType(null, Object.class) { private static final long serialVersionUID = 1602329532153860907L; - @Override protected Object convert(DatastoreV1.Entity entityPb) { - if (entityPb.getPropertyCount() == 0) { + @Override protected Object convert(com.google.datastore.v1beta3.Entity entityPb) { + if (entityPb.getProperties().size() == 0) { if (!entityPb.hasKey()) { return null; } - //TODO(ajaykannan): fix me! - //return Key.fromPb(entityPb.getKey()); + return Key.fromPb(entityPb.getKey()); } - // TODO(ajaykannan): fix me! - //return ProjectionEntity.fromPb(entityPb); - return ProjectionEntity.fromPb((com.google.datastore.v1beta3.Entity) null); // TODO(ajaykannan): fix me! + return ProjectionEntity.fromPb(entityPb); } }; public static final ResultType ENTITY = - new ResultType(DatastoreV1.EntityResult.ResultType.FULL, Entity.class) { + new ResultType( + com.google.datastore.v1beta3.EntityResult.ResultType.FULL, Entity.class) { private static final long serialVersionUID = 7712959777507168274L; - @Override protected Entity convert(DatastoreV1.Entity entityPb) { - // TODO(ajaykannan): fix me! - //return Entity.fromPb(entityPb); - return Entity.fromPb((com.google.datastore.v1beta3.Entity) null); // TODO(ajaykannan): fix me! + @Override protected Entity convert(com.google.datastore.v1beta3.Entity entityPb) { + return Entity.fromPb(entityPb); } }; public static final ResultType KEY = - new ResultType(DatastoreV1.EntityResult.ResultType.KEY_ONLY, Key.class) { + new ResultType( + com.google.datastore.v1beta3.EntityResult.ResultType.KEY_ONLY, Key.class) { private static final long serialVersionUID = -8514289244104446252L; - @Override protected Key convert(DatastoreV1.Entity entityPb) { - //TODO(ajaykannan): fix me! - //return Key.fromPb(entityPb.getKey()); - return Key.builder(null).build(); // TODO(ajaykannan): fix me! + @Override protected Key convert(com.google.datastore.v1beta3.Entity entityPb) { + return Key.fromPb(entityPb.getKey()); } }; public static final ResultType PROJECTION_ENTITY = - new ResultType(DatastoreV1.EntityResult.ResultType.PROJECTION, + new ResultType( + com.google.datastore.v1beta3.EntityResult.ResultType.PROJECTION, ProjectionEntity.class) { private static final long serialVersionUID = -7591409419690650246L; - @Override protected ProjectionEntity convert(DatastoreV1.Entity entityPb) { - // TODO(ajaykannan): fix me! - //return ProjectionEntity.fromPb(entityPb); - return ProjectionEntity.fromPb((com.google.datastore.v1beta3.Entity) null); // TODO(ajaykannan): fix me! + @Override protected ProjectionEntity convert( + com.google.datastore.v1beta3.Entity entityPb) { + return ProjectionEntity.fromPb(entityPb); } }; private final Class resultClass; - private final DatastoreV1.EntityResult.ResultType queryType; + private final com.google.datastore.v1beta3.EntityResult.ResultType queryType; - private ResultType(DatastoreV1.EntityResult.ResultType queryType, Class resultClass) { + private ResultType(com.google.datastore.v1beta3.EntityResult.ResultType queryType, + Class resultClass) { this.queryType = queryType; this.resultClass = resultClass; if (queryType != null) { @@ -156,9 +152,9 @@ boolean isAssignableFrom(ResultType otherResultType) { return resultClass.isAssignableFrom(otherResultType.resultClass); } - protected abstract V convert(DatastoreV1.Entity entityPb); + protected abstract V convert(com.google.datastore.v1beta3.Entity entityPb); - static ResultType fromPb(DatastoreV1.EntityResult.ResultType typePb) { + static ResultType fromPb(com.google.datastore.v1beta3.EntityResult.ResultType typePb) { return MoreObjects.firstNonNull(PB_TO_INSTANCE.get(typePb), UNKNOWN); } } @@ -193,9 +189,10 @@ protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException { protected abstract Object fromPb(ResultType resultType, String namespace, byte[] bytesPb) throws InvalidProtocolBufferException; - protected abstract void populatePb(DatastoreV1.RunQueryRequest.Builder requestPb); + protected abstract void populatePb( + com.google.datastore.v1beta3.RunQueryRequest.Builder requestPb); - protected abstract Query nextQuery(DatastoreV1.QueryResultBatch responsePb); + protected abstract Query nextQuery(com.google.datastore.v1beta3.QueryResultBatch responsePb); /** * Returns a new {@link GqlQuery} builder. diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResults.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResults.java index b23c56a7c395..5b4f71da3963 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResults.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResults.java @@ -36,7 +36,6 @@ public interface QueryResults extends Iterator { /** * Returns the Cursor for point after the value returned in the last {@link #next} call. - * Not currently implemented (depends on v1beta3). */ Cursor cursorAfter(); } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java index caf5d7f8a84f..b3c51ceb9eeb 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java @@ -16,11 +16,11 @@ package com.google.gcloud.datastore; -import com.google.api.services.datastore.DatastoreV1; -import com.google.api.services.datastore.DatastoreV1.QueryResultBatch.MoreResultsType; +import com.google.datastore.v1beta3.QueryResultBatch.MoreResultsType; import com.google.common.base.Preconditions; import com.google.common.collect.AbstractIterator; import com.google.gcloud.datastore.Query.ResultType; +import com.google.protobuf.ByteString; import java.util.Iterator; import java.util.Objects; @@ -28,46 +28,49 @@ class QueryResultsImpl extends AbstractIterator implements QueryResults { private final DatastoreImpl datastore; - private final DatastoreV1.ReadOptions readOptionsPb; - private final DatastoreV1.PartitionId partitionIdPb; + private final com.google.datastore.v1beta3.ReadOptions readOptionsPb; + private final com.google.datastore.v1beta3.PartitionId partitionIdPb; private final ResultType queryResultType; private Query query; private ResultType actualResultType; - private DatastoreV1.QueryResultBatch queryResultBatchPb; + private com.google.datastore.v1beta3.QueryResultBatch queryResultBatchPb; private boolean lastBatch; - private Iterator entityResultPbIter; - //private ByteString cursor; // only available in v1beta3 + private Iterator entityResultPbIter; + private ByteString cursor; - QueryResultsImpl(DatastoreImpl datastore, DatastoreV1.ReadOptions readOptionsPb, + QueryResultsImpl(DatastoreImpl datastore, com.google.datastore.v1beta3.ReadOptions readOptionsPb, Query query) { this.datastore = datastore; this.readOptionsPb = readOptionsPb; this.query = query; queryResultType = query.type(); - DatastoreV1.PartitionId.Builder pbBuilder = DatastoreV1.PartitionId.newBuilder(); - pbBuilder.setDatasetId(datastore.options().projectId()); + com.google.datastore.v1beta3.PartitionId.Builder pbBuilder = + com.google.datastore.v1beta3.PartitionId.newBuilder(); + pbBuilder.setProjectId(datastore.options().projectId()); if (query.namespace() != null) { - pbBuilder.setNamespace(query.namespace()); + pbBuilder.setNamespaceId(query.namespace()); } else if (datastore.options().namespace() != null) { - pbBuilder.setNamespace(datastore.options().namespace()); + pbBuilder.setNamespaceId(datastore.options().namespace()); } partitionIdPb = pbBuilder.build(); sendRequest(); } private void sendRequest() { - DatastoreV1.RunQueryRequest.Builder requestPb = DatastoreV1.RunQueryRequest.newBuilder(); + com.google.datastore.v1beta3.RunQueryRequest.Builder requestPb = + com.google.datastore.v1beta3.RunQueryRequest.newBuilder(); if (readOptionsPb != null) { requestPb.setReadOptions(readOptionsPb); } requestPb.setPartitionId(partitionIdPb); query.populatePb(requestPb); - // TODO(ajaykannan): fix me! - //queryResultBatchPb = datastore.runQuery(requestPb.build()).getBatch(); + queryResultBatchPb = datastore.runQuery(requestPb.build()).getBatch(); lastBatch = queryResultBatchPb.getMoreResults() != MoreResultsType.NOT_FINISHED; - entityResultPbIter = queryResultBatchPb.getEntityResultList().iterator(); - // cursor = resultPb.getSkippedCursor(); // available in v1beta3, use startCursor if not skipped + entityResultPbIter = queryResultBatchPb.getEntityResultsList().iterator(); + if (queryResultBatchPb.getSkippedResults() > 0) { + cursor = queryResultBatchPb.getSkippedCursor(); + } actualResultType = ResultType.fromPb(queryResultBatchPb.getEntityResultType()); if (Objects.equals(queryResultType, ResultType.PROJECTION_ENTITY)) { // projection entity can represent all type of results @@ -86,8 +89,8 @@ protected T computeNext() { if (!entityResultPbIter.hasNext()) { return endOfData(); } - DatastoreV1.EntityResult entityResultPb = entityResultPbIter.next(); - //cursor = entityResultPb.getCursor(); // only available in v1beta3 + com.google.datastore.v1beta3.EntityResult entityResultPb = entityResultPbIter.next(); + cursor = entityResultPb.getCursor(); @SuppressWarnings("unchecked") T result = (T) actualResultType.convert(entityResultPb.getEntity()); return result; @@ -100,7 +103,6 @@ public Class resultClass() { @Override public Cursor cursorAfter() { - //return new Cursor(cursor); // only available in v1beta3 - return null; + return new Cursor(cursor); } } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java index 1dd35cb4f191..7c1cf815879e 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java @@ -25,7 +25,6 @@ import static com.google.gcloud.datastore.LongValue.of; import static com.google.gcloud.datastore.StringValue.of; -import com.google.api.services.datastore.DatastoreV1; import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects.ToStringHelper; import com.google.common.base.Preconditions; @@ -103,13 +102,17 @@ public abstract static class Filter implements Serializable { Filter() { } - protected abstract DatastoreV1.Filter toPb(); + protected abstract com.google.datastore.v1beta3.Filter toPb(); - static Filter fromPb(DatastoreV1.Filter filterPb) { - if (filterPb.hasCompositeFilter()) { - return CompositeFilter.fromPb(filterPb.getCompositeFilter()); + static Filter fromPb(com.google.datastore.v1beta3.Filter filterPb) { + switch (filterPb.getFilterTypeCase()) { + case COMPOSITE_FILTER: + return CompositeFilter.fromPb(filterPb.getCompositeFilter()); + case PROPERTY_FILTER: + return PropertyFilter.fromPb(filterPb.getPropertyFilter()); + default: + return null; } - return PropertyFilter.fromPb(filterPb.getPropertyFilter()); } } @@ -122,11 +125,11 @@ public static final class CompositeFilter extends Filter { enum Operator { AND; - DatastoreV1.CompositeFilter.Operator toPb() { - return DatastoreV1.CompositeFilter.Operator.valueOf(name()); + com.google.datastore.v1beta3.CompositeFilter.Operator toPb() { + return com.google.datastore.v1beta3.CompositeFilter.Operator.valueOf(name()); } - static Operator fromPb(DatastoreV1.CompositeFilter.Operator operatorPb) { + static Operator fromPb(com.google.datastore.v1beta3.CompositeFilter.Operator operatorPb) { return valueOf(operatorPb.name()); } } @@ -169,11 +172,14 @@ public boolean equals(Object obj) { && filters.equals(other.filters); } - static CompositeFilter fromPb(DatastoreV1.CompositeFilter compositeFilterPb) { - Operator operator = Operator.fromPb(compositeFilterPb.getOperator()); + static CompositeFilter fromPb(com.google.datastore.v1beta3.CompositeFilter compositeFilterPb) { + Operator operator = Operator.fromPb(compositeFilterPb.getOp()); ImmutableList.Builder filters = ImmutableList.builder(); - for (DatastoreV1.Filter filterPb : compositeFilterPb.getFilterList()) { - filters.add(Filter.fromPb(filterPb)); + for (com.google.datastore.v1beta3.Filter filterPb : compositeFilterPb.getFiltersList()) { + Filter currFilter = Filter.fromPb(filterPb); + if (currFilter != null) { + filters.add(currFilter); + } } return new CompositeFilter(operator, filters.build()); } @@ -183,12 +189,14 @@ public static CompositeFilter and(Filter first, Filter... other) { } @Override - protected DatastoreV1.Filter toPb() { - DatastoreV1.Filter.Builder filterPb = DatastoreV1.Filter.newBuilder(); - DatastoreV1.CompositeFilter.Builder compositeFilterPb = filterPb.getCompositeFilterBuilder(); - compositeFilterPb.setOperator(operator.toPb()); + protected com.google.datastore.v1beta3.Filter toPb() { + com.google.datastore.v1beta3.Filter.Builder filterPb = + com.google.datastore.v1beta3.Filter.newBuilder(); + com.google.datastore.v1beta3.CompositeFilter.Builder compositeFilterPb = + filterPb.getCompositeFilterBuilder(); + compositeFilterPb.setOp(operator.toPb()); for (Filter filter : filters) { - compositeFilterPb.addFilter(filter.toPb()); + compositeFilterPb.addFilters(filter.toPb()); } return filterPb.build(); } @@ -210,11 +218,11 @@ enum Operator { EQUAL, HAS_ANCESTOR; - DatastoreV1.PropertyFilter.Operator toPb() { - return DatastoreV1.PropertyFilter.Operator.valueOf(name()); + com.google.datastore.v1beta3.PropertyFilter.Operator toPb() { + return com.google.datastore.v1beta3.PropertyFilter.Operator.valueOf(name()); } - static Operator fromPb(DatastoreV1.PropertyFilter.Operator operatorPb) { + static Operator fromPb(com.google.datastore.v1beta3.PropertyFilter.Operator operatorPb) { return valueOf(operatorPb.name()); } } @@ -225,13 +233,11 @@ private PropertyFilter(String property, Operator operator, Value value) { this.value = checkNotNull(value); } - public static PropertyFilter fromPb(DatastoreV1.PropertyFilter propertyFilterPb) { + public static PropertyFilter fromPb(com.google.datastore.v1beta3.PropertyFilter propertyFilterPb) { String property = propertyFilterPb.getProperty().getName(); - Operator operator = Operator.fromPb(propertyFilterPb.getOperator()); - // TODO(ajaykannan): fix me! - //Value value = Value.fromPb(propertyFilterPb.getValue()); - //return new PropertyFilter(property, operator, value); - return new PropertyFilter(property, operator, null); // TODO(ajaykannan): fix me! + Operator operator = Operator.fromPb(propertyFilterPb.getOp()); + Value value = Value.fromPb(propertyFilterPb.getValue()); + return new PropertyFilter(property, operator, value); } @Override @@ -431,14 +437,15 @@ public static PropertyFilter isNull(String property) { } @Override - protected DatastoreV1.Filter toPb() { - DatastoreV1.Filter.Builder filterPb = DatastoreV1.Filter.newBuilder(); - DatastoreV1.PropertyFilter.Builder propertyFilterPb = filterPb.getPropertyFilterBuilder(); + protected com.google.datastore.v1beta3.Filter toPb() { + com.google.datastore.v1beta3.Filter.Builder filterPb = + com.google.datastore.v1beta3.Filter.newBuilder(); + com.google.datastore.v1beta3.PropertyFilter.Builder propertyFilterPb = + filterPb.getPropertyFilterBuilder(); propertyFilterPb.getPropertyBuilder().setName(property); - propertyFilterPb.setOperator(operator.toPb()); + propertyFilterPb.setOp(operator.toPb()); if (value != null) { - // TODO(ajaykannan): fix me! - //propertyFilterPb.setValue(value.toPb()); + propertyFilterPb.setValue(value.toPb()); } return filterPb.build(); } @@ -455,11 +462,11 @@ public enum Direction { ASCENDING, DESCENDING; - DatastoreV1.PropertyOrder.Direction toPb() { - return DatastoreV1.PropertyOrder.Direction.valueOf(name()); + com.google.datastore.v1beta3.PropertyOrder.Direction toPb() { + return com.google.datastore.v1beta3.PropertyOrder.Direction.valueOf(name()); } - static Direction fromPb(DatastoreV1.PropertyOrder.Direction directionPb) { + static Direction fromPb(com.google.datastore.v1beta3.PropertyOrder.Direction directionPb) { return valueOf(directionPb.name()); } } @@ -495,10 +502,11 @@ public Direction direction() { return direction; } - DatastoreV1.PropertyOrder toPb() { - return DatastoreV1.PropertyOrder.newBuilder() + com.google.datastore.v1beta3.PropertyOrder toPb() { + return com.google.datastore.v1beta3.PropertyOrder.newBuilder() .setDirection(direction.toPb()) - .setProperty(DatastoreV1.PropertyReference.newBuilder().setName(property).build()) + .setProperty(com.google.datastore.v1beta3.PropertyReference.newBuilder() + .setName(property).build()) .build(); } @@ -510,7 +518,7 @@ public static OrderBy desc(String property) { return new OrderBy(property, OrderBy.Direction.DESCENDING); } - static OrderBy fromPb(DatastoreV1.PropertyOrder propertyOrderPb) { + static OrderBy fromPb(com.google.datastore.v1beta3.PropertyOrder propertyOrderPb) { String property = propertyOrderPb.getProperty().getName(); Direction direction = Direction.fromPb(propertyOrderPb.getDirection()); return new OrderBy(property, direction); @@ -521,30 +529,15 @@ public static final class Projection implements Serializable { private static final long serialVersionUID = 3083707957256279470L; - private final Aggregate aggregate; private final String property; - public enum Aggregate { - - FIRST; - - DatastoreV1.PropertyExpression.AggregationFunction toPb() { - return DatastoreV1.PropertyExpression.AggregationFunction.valueOf(name()); - } - - static Aggregate fromPb(DatastoreV1.PropertyExpression.AggregationFunction aggregatePb) { - return valueOf(aggregatePb.name()); - } - } - - private Projection(Aggregate aggregate, String property) { - this.aggregate = aggregate; + private Projection(String property) { this.property = property; } @Override public int hashCode() { - return Objects.hash(property, aggregate); + return Objects.hash(property); } @Override @@ -555,51 +548,31 @@ public boolean equals(Object obj) { if (!(obj instanceof Projection)) { return false; } - Projection other = (Projection) obj; - return Objects.equals(property, other.property) - && Objects.equals(aggregate, other.aggregate); + return Objects.equals(property, ((Projection) obj).property); } @Override public String toString() { ToStringHelper toStringHelper = MoreObjects.toStringHelper(this); toStringHelper.add("property", property); - if (aggregate != null) { - toStringHelper.add("aggregate", aggregate); - } return toStringHelper.toString(); } - DatastoreV1.PropertyExpression toPb() { - DatastoreV1.PropertyExpression.Builder expressionPb = - DatastoreV1.PropertyExpression.newBuilder(); - if (aggregate != null) { - expressionPb.setAggregationFunction(aggregate.toPb()); - } + com.google.datastore.v1beta3.Projection toPb() { + com.google.datastore.v1beta3.Projection.Builder expressionPb = + com.google.datastore.v1beta3.Projection.newBuilder(); expressionPb.setProperty( - DatastoreV1.PropertyReference.newBuilder().setName(property).build()); + com.google.datastore.v1beta3.PropertyReference.newBuilder().setName(property).build()); return expressionPb.build(); } - public static Projection fromPb(DatastoreV1.PropertyExpression propertyExpressionPb) { - String property = propertyExpressionPb.getProperty().getName(); - Aggregate aggregate = null; - if (propertyExpressionPb.hasAggregationFunction()) { - aggregate = Aggregate.fromPb(propertyExpressionPb.getAggregationFunction()); - } - return new Projection(aggregate, property); + public static Projection fromPb( + com.google.datastore.v1beta3.Projection projectionPb) { + return new Projection(projectionPb.getProperty().getName()); } public static Projection property(String property) { - return new Projection(null, property); - } - - public static Projection aggregate(Aggregate aggregate, String property) { - return new Projection(aggregate, property); - } - - public static Projection first(String property) { - return new Projection(Aggregate.FIRST, property); + return new Projection(property); } } @@ -714,32 +687,34 @@ B addGroupBy(String property, String... others) { return self(); } - B mergeFrom(DatastoreV1.Query queryPb) { + B mergeFrom(com.google.datastore.v1beta3.Query queryPb) { if (queryPb.getKindCount() > 0) { kind(queryPb.getKind(0).getName()); } - if (queryPb.hasStartCursor()) { + if (!queryPb.getStartCursor().isEmpty()) { startCursor(new Cursor(queryPb.getStartCursor())); } - if (queryPb.hasEndCursor()) { + if (!queryPb.getEndCursor().isEmpty()) { endCursor(new Cursor(queryPb.getEndCursor())); } - if (queryPb.hasOffset()) { - offset(queryPb.getOffset()); - } + offset(queryPb.getOffset()); if (queryPb.hasLimit()) { - limit(queryPb.getLimit()); + limit(queryPb.getLimit().getValue()); } if (queryPb.hasFilter()) { - filter(Filter.fromPb(queryPb.getFilter())); + Filter currFilter = Filter.fromPb(queryPb.getFilter()); + if (currFilter != null) { + filter(currFilter); + } } - for (DatastoreV1.PropertyOrder orderByPb : queryPb.getOrderList()) { + for (com.google.datastore.v1beta3.PropertyOrder orderByPb : queryPb.getOrderList()) { addOrderBy(OrderBy.fromPb(orderByPb)); } - for (DatastoreV1.PropertyExpression projectionPb : queryPb.getProjectionList()) { + for (com.google.datastore.v1beta3.Projection projectionPb + : queryPb.getProjectionList()) { addProjection(Projection.fromPb(projectionPb)); } - for (DatastoreV1.PropertyReference groupByPb : queryPb.getGroupByList()) { + for (com.google.datastore.v1beta3.PropertyReference groupByPb : queryPb.getDistinctOnList()) { addGroupBy(groupByPb.getName()); } return self(); @@ -777,7 +752,7 @@ public static final class KeyQueryBuilder extends BaseBuilder nextQuery(DatastoreV1.QueryResultBatch responsePb) { + protected StructuredQuery nextQuery(com.google.datastore.v1beta3.QueryResultBatch responsePb) { Builder builder = new Builder<>(type()); builder.mergeFrom(toPb()); builder.startCursor(new Cursor(responsePb.getEndCursor())); @@ -929,15 +904,16 @@ protected StructuredQuery nextQuery(DatastoreV1.QueryResultBatch responsePb) } else { builder.offset(0); if (limit != null) { - builder.limit(limit - responsePb.getEntityResultCount()); + builder.limit(limit - responsePb.getEntityResultsCount()); } } return builder.build(); } @Override - protected DatastoreV1.Query toPb() { - DatastoreV1.Query.Builder queryPb = DatastoreV1.Query.newBuilder(); + protected com.google.datastore.v1beta3.Query toPb() { + com.google.datastore.v1beta3.Query.Builder queryPb = + com.google.datastore.v1beta3.Query.newBuilder(); if (kind != null) { queryPb.addKindBuilder().setName(kind); } @@ -951,7 +927,7 @@ protected DatastoreV1.Query toPb() { queryPb.setOffset(offset); } if (limit != null) { - queryPb.setLimit(limit); + queryPb.setLimit(com.google.protobuf.Int32Value.newBuilder().setValue(limit.intValue())); } if (filter != null) { queryPb.setFilter(filter.toPb()); @@ -960,7 +936,8 @@ protected DatastoreV1.Query toPb() { queryPb.addOrder(value.toPb()); } for (String value : groupBy) { - queryPb.addGroupBy(DatastoreV1.PropertyReference.newBuilder().setName(value).build()); + queryPb.addDistinctOn(com.google.datastore.v1beta3.PropertyReference.newBuilder() + .setName(value).build()); } for (Projection value : projection) { queryPb.addProjection(value.toPb()); @@ -971,11 +948,11 @@ protected DatastoreV1.Query toPb() { @Override protected Object fromPb(ResultType resultType, String namespace, byte[] bytesPb) throws InvalidProtocolBufferException { - return fromPb(resultType, namespace, DatastoreV1.Query.parseFrom(bytesPb)); + return fromPb(resultType, namespace, com.google.datastore.v1beta3.Query.parseFrom(bytesPb)); } private static StructuredQuery fromPb(ResultType resultType, String namespace, - DatastoreV1.Query queryPb) { + com.google.datastore.v1beta3.Query queryPb) { BaseBuilder builder; if (resultType.equals(ResultType.ENTITY)) { builder = new EntityQueryBuilder(); diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index 689d2a7b8b6b..e3a71d8a8aa2 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -115,9 +115,9 @@ public void setUp() throws IOException, InterruptedException { .host("localhost:" + LocalGcdHelper.PORT) .build(); datastore = DatastoreFactory.instance().get(options); - //StructuredQuery query = Query.keyQueryBuilder().build(); - //QueryResults result = datastore.run(query); - //datastore.delete(Iterators.toArray(result, Key.class)); + StructuredQuery query = Query.keyQueryBuilder().build(); + QueryResults result = datastore.run(query); + datastore.delete(Iterators.toArray(result, Key.class)); datastore.add(ENTITY1, ENTITY2); } @@ -188,7 +188,7 @@ public void testTransactionWithRead() { assertEquals(DatastoreException.Code.ABORTED, expected.code()); } } - /* TODO(ajaykannan): fix me! + @Test public void testTransactionWithQuery() { Query query = Query.entityQueryBuilder() @@ -216,7 +216,7 @@ public void testTransactionWithQuery() { assertEquals(DatastoreException.Code.ABORTED, expected.code()); } } - */ + @Test public void testNewTransactionRollback() { Transaction transaction = datastore.newTransaction(); @@ -331,7 +331,7 @@ public void testNewBatch() { assertNull(entities.get(4)); assertEquals(5, entities.size()); } - /* TODO(ajaykannan): fix me! + @Test public void testRunGqlQueryNoCasting() { Query query1 = Query.gqlQueryBuilder(ResultType.ENTITY, "select * from " + KIND1).build(); @@ -431,7 +431,7 @@ public void testRunStructuredQuery() { StructuredQuery projectionQuery = Query.projectionEntityQueryBuilder() .kind(KIND2) - .projection(Projection.property("age"), Projection.first("name")) + .projection(Projection.property("age")) .filter(PropertyFilter.gt("age", 18)) .groupBy("age") .orderBy(OrderBy.asc("age")) @@ -443,12 +443,11 @@ public void testRunStructuredQuery() { ProjectionEntity entity = results4.next(); assertEquals(ENTITY2.key(), entity.key()); assertEquals(20, entity.getLong("age")); - assertEquals("Dan", entity.getString("name")); - assertEquals(2, entity.properties().size()); + assertEquals(1, entity.properties().size()); assertFalse(results4.hasNext()); // TODO(ozarov): construct a test to verify nextQuery/pagination } - */ + @Test public void testAllocateId() { KeyFactory keyFactory = datastore.newKeyFactory().kind(KIND1); diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java index bd4a683f9c9c..0c65ab76c681 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java @@ -122,9 +122,8 @@ public class SerializationTest { .put(ValueType.NULL, NULL_VALUE) .put(ValueType.KEY, KEY_VALUE) .put(ValueType.STRING, STRING_VALUE) - // TODO(ajaykannan): fix me! - //.putAll(ValueType.ENTITY, EMBEDDED_ENTITY_VALUE1, EMBEDDED_ENTITY_VALUE2, - // EMBEDDED_ENTITY_VALUE3) + .putAll(ValueType.ENTITY, EMBEDDED_ENTITY_VALUE1, EMBEDDED_ENTITY_VALUE2, + EMBEDDED_ENTITY_VALUE3) .put(ValueType.LIST, LIST_VALUE) .put(ValueType.LONG, LONG_VALUE) .put(ValueType.DOUBLE, DOUBLE_VALUE) @@ -169,10 +168,9 @@ public void testValues() throws Exception { @Test public void testTypes() throws Exception { - // TODO(ajaykannan): fix me! - Serializable[] types = { KEY1, KEY2, INCOMPLETE_KEY1, INCOMPLETE_KEY2, /*ENTITY1, ENTITY2, - ENTITY3, EMBEDDED_ENTITY, PROJECTION_ENTITY,*/ DATE_TIME1, BLOB1/*, CURSOR1, GQL1, GQL2, - QUERY1, QUERY2, QUERY3*/}; + Serializable[] types = { KEY1, KEY2, INCOMPLETE_KEY1, INCOMPLETE_KEY2, ENTITY1, ENTITY2, + ENTITY3, EMBEDDED_ENTITY, PROJECTION_ENTITY, DATE_TIME1, BLOB1, CURSOR1, GQL1, GQL2, + QUERY1, QUERY2, QUERY3}; for (Serializable obj : types) { Object copy = serializeAndDeserialize(obj); assertEquals(obj, obj); From 16afb0d0ba2f67f61f8027391440b9686479423f Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Tue, 13 Oct 2015 12:11:55 -0700 Subject: [PATCH 09/60] fix distinct and cursor code --- .../com/google/gcloud/datastore/GqlQuery.java | 15 +- .../com/google/gcloud/datastore/Query.java | 2 +- .../gcloud/datastore/QueryResultsImpl.java | 4 + .../gcloud/datastore/StructuredQuery.java | 150 +++++++----------- .../gcloud/datastore/DatastoreTest.java | 7 +- .../gcloud/datastore/SerializationTest.java | 5 +- 6 files changed, 73 insertions(+), 110 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java index 2c02aa7b970c..8e023c0c224e 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java @@ -20,6 +20,7 @@ import static com.google.gcloud.datastore.Validator.validateNamespace; import com.google.common.base.MoreObjects; +import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSortedMap; @@ -137,10 +138,8 @@ static Binding fromPb(com.google.datastore.v1beta3.GqlQueryParameter argPb) { switch (argPb.getParameterTypeCase()) { case CURSOR: return new Binding(new Cursor(argPb.getCursor())); - case VALUE: - return new Binding(Value.fromPb(argPb.getValue())); default: - return null; + return new Binding(Value.fromPb(argPb.getValue())); } } } @@ -398,16 +397,14 @@ private static GqlQuery fromPb( for (Map.Entry nameArg : queryPb.getNamedBindings().entrySet()) { Binding currBinding = Binding.fromPb(nameArg.getValue()); - if (currBinding != null) { - builder.namedBindings.put(nameArg.getKey(), currBinding); - } + Preconditions.checkState(currBinding != null); + builder.namedBindings.put(nameArg.getKey(), currBinding); } for (com.google.datastore.v1beta3.GqlQueryParameter numberArg : queryPb.getPositionalBindingsList()) { Binding currBinding = Binding.fromPb(numberArg); - if (currBinding != null) { - builder.positionalBindings.add(currBinding); - } + Preconditions.checkState(currBinding != null); + builder.positionalBindings.add(currBinding); } return builder.build(); } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java index 6e962d92fb2f..c45efde3e30d 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java @@ -62,7 +62,7 @@ public abstract static class ResultType implements java.io.Serializable { private static final long serialVersionUID = 1602329532153860907L; @Override protected Object convert(com.google.datastore.v1beta3.Entity entityPb) { - if (entityPb.getProperties().size() == 0) { + if (entityPb.getProperties().isEmpty()) { if (!entityPb.hasKey()) { return null; } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java index b3c51ceb9eeb..40f1aee9dc5e 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java @@ -70,6 +70,10 @@ private void sendRequest() { entityResultPbIter = queryResultBatchPb.getEntityResultsList().iterator(); if (queryResultBatchPb.getSkippedResults() > 0) { cursor = queryResultBatchPb.getSkippedCursor(); + } else if (entityResultPbIter.hasNext()) { + cursor = queryResultBatchPb.getEntityResults(0).getCursor(); + } else { + cursor = queryResultBatchPb.getEndCursor(); } actualResultType = ResultType.fromPb(queryResultBatchPb.getEntityResultType()); if (Objects.equals(queryResultType, ResultType.PROJECTION_ENTITY)) { diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java index 7c1cf815879e..4151ae4b462c 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java @@ -68,7 +68,7 @@ * .kind(kind) * .projection(Projection.property("age"), Projection.first("name")) * .filter(PropertyFilter.gt("age", 18)) - * .groupBy("age") + * .distinct("age") * .orderBy(OrderBy.asc("age")) * .limit(10) * .build(); @@ -86,9 +86,9 @@ public class StructuredQuery extends Query { private static final String KEY_PROPERTY_NAME = "__key__"; private final transient String kind; - private final ImmutableList projection; + private final ImmutableList projection; private final transient Filter filter; - private final ImmutableList groupBy; + private final ImmutableList distinctOn; private final transient ImmutableList orderBy; private final transient Cursor startCursor; private final transient Cursor endCursor; @@ -108,10 +108,8 @@ static Filter fromPb(com.google.datastore.v1beta3.Filter filterPb) { switch (filterPb.getFilterTypeCase()) { case COMPOSITE_FILTER: return CompositeFilter.fromPb(filterPb.getCompositeFilter()); - case PROPERTY_FILTER: - return PropertyFilter.fromPb(filterPb.getPropertyFilter()); default: - return null; + return PropertyFilter.fromPb(filterPb.getPropertyFilter()); } } } @@ -525,65 +523,15 @@ static OrderBy fromPb(com.google.datastore.v1beta3.PropertyOrder propertyOrderPb } } - public static final class Projection implements Serializable { - - private static final long serialVersionUID = 3083707957256279470L; - - private final String property; - - private Projection(String property) { - this.property = property; - } - - @Override - public int hashCode() { - return Objects.hash(property); - } - - @Override - public boolean equals(Object obj) { - if (obj == this) { - return true; - } - if (!(obj instanceof Projection)) { - return false; - } - return Objects.equals(property, ((Projection) obj).property); - } - - @Override - public String toString() { - ToStringHelper toStringHelper = MoreObjects.toStringHelper(this); - toStringHelper.add("property", property); - return toStringHelper.toString(); - } - - com.google.datastore.v1beta3.Projection toPb() { - com.google.datastore.v1beta3.Projection.Builder expressionPb = - com.google.datastore.v1beta3.Projection.newBuilder(); - expressionPb.setProperty( - com.google.datastore.v1beta3.PropertyReference.newBuilder().setName(property).build()); - return expressionPb.build(); - } - - public static Projection fromPb( - com.google.datastore.v1beta3.Projection projectionPb) { - return new Projection(projectionPb.getProperty().getName()); - } - - public static Projection property(String property) { - return new Projection(property); - } - } - static class BaseBuilder> { private final ResultType resultType; private String namespace; private String kind; - private final List projection = new LinkedList<>(); + private final List projection = new LinkedList<>(); private Filter filter; - private final List groupBy = new LinkedList<>(); + private boolean distinctOnAll = false; + private final List distinctOn = new LinkedList<>(); private final List orderBy = new LinkedList<>(); private Cursor startCursor; private Cursor endCursor; @@ -658,32 +606,39 @@ B clearProjection() { return self(); } - B projection(Projection projection, Projection... others) { + B projection(String projection, String... others) { clearProjection(); addProjection(projection, others); return self(); } - B addProjection(Projection projection, Projection... others) { + B addProjection(String projection, String... others) { this.projection.add(projection); Collections.addAll(this.projection, others); return self(); } - B clearGroupBy() { - groupBy.clear(); + B clearDistinct() { + distinctOn.clear(); + distinctOnAll = false; return self(); } - B groupBy(String property, String... others) { - clearGroupBy(); - addGroupBy(property, others); + B distinct(String... properties) { + clearDistinct(); + if (properties.length == 0) { + this.distinctOnAll = true; + } else if (properties.length == 1) { + addDistinct(properties[0]); + } else { + addDistinct(properties[0], Arrays.copyOfRange(properties, 1, properties.length)); + } return self(); } - B addGroupBy(String property, String... others) { - this.groupBy.add(property); - Collections.addAll(this.groupBy, others); + B addDistinct(String property, String... others) { + this.distinctOn.add(property); + Collections.addAll(this.distinctOn, others); return self(); } @@ -712,15 +667,20 @@ B mergeFrom(com.google.datastore.v1beta3.Query queryPb) { } for (com.google.datastore.v1beta3.Projection projectionPb : queryPb.getProjectionList()) { - addProjection(Projection.fromPb(projectionPb)); + addProjection(projectionPb.getProperty().getName()); } - for (com.google.datastore.v1beta3.PropertyReference groupByPb : queryPb.getDistinctOnList()) { - addGroupBy(groupByPb.getName()); + for (com.google.datastore.v1beta3.PropertyReference distinctOnPb : queryPb.getDistinctOnList()) { + addDistinct(distinctOnPb.getName()); } + distinctOnAll = false; return self(); } public StructuredQuery build() { + if (distinctOnAll) { + clearDistinct(); + this.distinctOn.addAll(this.projection); + } return new StructuredQuery<>(this); } } @@ -748,14 +708,14 @@ public static final class KeyQueryBuilder extends BaseBuilder projection() { + public List projection() { return projection; } @@ -865,8 +825,8 @@ public Filter filter() { return filter; } - public List groupBy() { - return groupBy; + public List distinct() { + return distinctOn; } public ImmutableList orderBy() { @@ -935,12 +895,16 @@ protected com.google.datastore.v1beta3.Query toPb() { for (OrderBy value : orderBy) { queryPb.addOrder(value.toPb()); } - for (String value : groupBy) { + for (String value : distinctOn) { queryPb.addDistinctOn(com.google.datastore.v1beta3.PropertyReference.newBuilder() .setName(value).build()); } - for (Projection value : projection) { - queryPb.addProjection(value.toPb()); + for (String value : projection) { + com.google.datastore.v1beta3.Projection.Builder expressionPb = + com.google.datastore.v1beta3.Projection.newBuilder(); + expressionPb.setProperty( + com.google.datastore.v1beta3.PropertyReference.newBuilder().setName(value).build()); + queryPb.addProjection(expressionPb.build()); } return queryPb.build(); } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index e3a71d8a8aa2..41f0c4df8d92 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -29,7 +29,6 @@ import com.google.gcloud.RetryParams; import com.google.gcloud.datastore.Query.ResultType; import com.google.gcloud.datastore.StructuredQuery.OrderBy; -import com.google.gcloud.datastore.StructuredQuery.Projection; import com.google.gcloud.datastore.StructuredQuery.PropertyFilter; import com.google.gcloud.spi.DatastoreRpc; import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException.Reason; @@ -421,7 +420,7 @@ public void testRunStructuredQuery() { StructuredQuery keyOnlyProjectionQuery = Query.projectionEntityQueryBuilder() - .kind(KIND1).projection(Projection.property("__key__")).build(); + .kind(KIND1).projection("__key__").build(); QueryResults results3 = datastore.run(keyOnlyProjectionQuery); assertTrue(results3.hasNext()); ProjectionEntity projectionEntity = results3.next(); @@ -431,9 +430,9 @@ public void testRunStructuredQuery() { StructuredQuery projectionQuery = Query.projectionEntityQueryBuilder() .kind(KIND2) - .projection(Projection.property("age")) + .projection("age") .filter(PropertyFilter.gt("age", 18)) - .groupBy("age") + .distinct("age") .orderBy(OrderBy.asc("age")) .limit(10) .build(); diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java index 0c65ab76c681..d946246db279 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java @@ -26,7 +26,6 @@ import com.google.gcloud.RetryParams; import com.google.gcloud.datastore.StructuredQuery.CompositeFilter; import com.google.gcloud.datastore.StructuredQuery.OrderBy; -import com.google.gcloud.datastore.StructuredQuery.Projection; import com.google.gcloud.datastore.StructuredQuery.PropertyFilter; import org.junit.Test; @@ -71,13 +70,13 @@ public class SerializationTest { private static final Query QUERY3 = Query.projectionEntityQueryBuilder() .kind("k") .namespace("ns1") - .projection(Projection.property("p")) + .projection("p") .limit(100) .offset(5) .startCursor(CURSOR1) .endCursor(CURSOR2) .filter(CompositeFilter.and(PropertyFilter.gt("p1", 10), PropertyFilter.eq("a", "v"))) - .addGroupBy("p") + .addDistinct("p") .addOrderBy(OrderBy.asc("p")) .build(); private static final KeyValue KEY_VALUE = KeyValue.of(KEY1); From acf2c4207f384d9e4d8f7ce958a1d7ac2263ff7a Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Wed, 14 Oct 2015 18:55:30 -0700 Subject: [PATCH 10/60] Include cursor workaround, fix distinct in StructuredQuery, and remove extra checks --- .../com/google/gcloud/datastore/GqlQuery.java | 7 +++---- .../gcloud/datastore/QueryResultsImpl.java | 14 ++++++------- .../gcloud/datastore/StructuredQuery.java | 20 ++++++++++--------- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java index 8e023c0c224e..7fcb562fea89 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java @@ -20,7 +20,6 @@ import static com.google.gcloud.datastore.Validator.validateNamespace; import com.google.common.base.MoreObjects; -import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSortedMap; @@ -138,8 +137,10 @@ static Binding fromPb(com.google.datastore.v1beta3.GqlQueryParameter argPb) { switch (argPb.getParameterTypeCase()) { case CURSOR: return new Binding(new Cursor(argPb.getCursor())); - default: + case VALUE: return new Binding(Value.fromPb(argPb.getValue())); + default: + throw new AssertionError("Unexpected enum value " + argPb.getParameterTypeCase()); } } } @@ -397,13 +398,11 @@ private static GqlQuery fromPb( for (Map.Entry nameArg : queryPb.getNamedBindings().entrySet()) { Binding currBinding = Binding.fromPb(nameArg.getValue()); - Preconditions.checkState(currBinding != null); builder.namedBindings.put(nameArg.getKey(), currBinding); } for (com.google.datastore.v1beta3.GqlQueryParameter numberArg : queryPb.getPositionalBindingsList()) { Binding currBinding = Binding.fromPb(numberArg); - Preconditions.checkState(currBinding != null); builder.positionalBindings.add(currBinding); } return builder.build(); diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java index 40f1aee9dc5e..38eb3dd1c393 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java @@ -16,9 +16,9 @@ package com.google.gcloud.datastore; -import com.google.datastore.v1beta3.QueryResultBatch.MoreResultsType; import com.google.common.base.Preconditions; import com.google.common.collect.AbstractIterator; +import com.google.datastore.v1beta3.QueryResultBatch.MoreResultsType; import com.google.gcloud.datastore.Query.ResultType; import com.google.protobuf.ByteString; @@ -55,6 +55,11 @@ class QueryResultsImpl extends AbstractIterator implements QueryResults } partitionIdPb = pbBuilder.build(); sendRequest(); + if (queryResultBatchPb.getSkippedResults() > 0) { + cursor = queryResultBatchPb.getSkippedCursor(); + } else { + cursor = ByteString.EMPTY; + } } private void sendRequest() { @@ -68,13 +73,6 @@ private void sendRequest() { queryResultBatchPb = datastore.runQuery(requestPb.build()).getBatch(); lastBatch = queryResultBatchPb.getMoreResults() != MoreResultsType.NOT_FINISHED; entityResultPbIter = queryResultBatchPb.getEntityResultsList().iterator(); - if (queryResultBatchPb.getSkippedResults() > 0) { - cursor = queryResultBatchPb.getSkippedCursor(); - } else if (entityResultPbIter.hasNext()) { - cursor = queryResultBatchPb.getEntityResults(0).getCursor(); - } else { - cursor = queryResultBatchPb.getEndCursor(); - } actualResultType = ResultType.fromPb(queryResultBatchPb.getEntityResultType()); if (Objects.equals(queryResultType, ResultType.PROJECTION_ENTITY)) { // projection entity can represent all type of results diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java index 4151ae4b462c..c49ddddc9c02 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java @@ -108,8 +108,10 @@ static Filter fromPb(com.google.datastore.v1beta3.Filter filterPb) { switch (filterPb.getFilterTypeCase()) { case COMPOSITE_FILTER: return CompositeFilter.fromPb(filterPb.getCompositeFilter()); - default: + case PROPERTY_FILTER: return PropertyFilter.fromPb(filterPb.getPropertyFilter()); + default: + throw new AssertionError("Unexpected enum value " + filterPb.getFilterTypeCase()); } } } @@ -530,7 +532,7 @@ static class BaseBuilder> { private String kind; private final List projection = new LinkedList<>(); private Filter filter; - private boolean distinctOnAll = false; + private boolean distinct = false; private final List distinctOn = new LinkedList<>(); private final List orderBy = new LinkedList<>(); private Cursor startCursor; @@ -620,14 +622,16 @@ B addProjection(String projection, String... others) { B clearDistinct() { distinctOn.clear(); - distinctOnAll = false; + distinct = false; return self(); } B distinct(String... properties) { clearDistinct(); if (properties.length == 0) { - this.distinctOnAll = true; + clearDistinct(); + this.distinct = true; + this.distinctOn.addAll(this.projection); } else if (properties.length == 1) { addDistinct(properties[0]); } else { @@ -637,6 +641,9 @@ B distinct(String... properties) { } B addDistinct(String property, String... others) { + if (this.distinct) { + throw new IllegalStateException("\"distinct()\" is currently set."); + } this.distinctOn.add(property); Collections.addAll(this.distinctOn, others); return self(); @@ -672,15 +679,10 @@ B mergeFrom(com.google.datastore.v1beta3.Query queryPb) { for (com.google.datastore.v1beta3.PropertyReference distinctOnPb : queryPb.getDistinctOnList()) { addDistinct(distinctOnPb.getName()); } - distinctOnAll = false; return self(); } public StructuredQuery build() { - if (distinctOnAll) { - clearDistinct(); - this.distinctOn.addAll(this.projection); - } return new StructuredQuery<>(this); } } From 0c2f9e93ffb78aadb55c3abf558e47c616336c18 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Thu, 15 Oct 2015 15:52:50 -0700 Subject: [PATCH 11/60] standardize distinctOn naming --- .../gcloud/datastore/StructuredQuery.java | 45 +++++++------------ .../gcloud/datastore/DatastoreTest.java | 17 +++---- .../gcloud/datastore/SerializationTest.java | 25 ++++++----- 3 files changed, 38 insertions(+), 49 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java index c49ddddc9c02..1b82991cb8c8 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java @@ -68,7 +68,7 @@ * .kind(kind) * .projection(Projection.property("age"), Projection.first("name")) * .filter(PropertyFilter.gt("age", 18)) - * .distinct("age") + * .distinctOn("age") * .orderBy(OrderBy.asc("age")) * .limit(10) * .build(); @@ -532,7 +532,6 @@ static class BaseBuilder> { private String kind; private final List projection = new LinkedList<>(); private Filter filter; - private boolean distinct = false; private final List distinctOn = new LinkedList<>(); private final List orderBy = new LinkedList<>(); private Cursor startCursor; @@ -620,30 +619,18 @@ B addProjection(String projection, String... others) { return self(); } - B clearDistinct() { + B clearDistinctOn() { distinctOn.clear(); - distinct = false; return self(); } - B distinct(String... properties) { - clearDistinct(); - if (properties.length == 0) { - clearDistinct(); - this.distinct = true; - this.distinctOn.addAll(this.projection); - } else if (properties.length == 1) { - addDistinct(properties[0]); - } else { - addDistinct(properties[0], Arrays.copyOfRange(properties, 1, properties.length)); - } + B distinctOn(String property, String... others) { + clearDistinctOn(); + addDistinctOn(property, others); return self(); } - B addDistinct(String property, String... others) { - if (this.distinct) { - throw new IllegalStateException("\"distinct()\" is currently set."); - } + B addDistinctOn(String property, String... others) { this.distinctOn.add(property); Collections.addAll(this.distinctOn, others); return self(); @@ -677,7 +664,7 @@ B mergeFrom(com.google.datastore.v1beta3.Query queryPb) { addProjection(projectionPb.getProperty().getName()); } for (com.google.datastore.v1beta3.PropertyReference distinctOnPb : queryPb.getDistinctOnList()) { - addDistinct(distinctOnPb.getName()); + addDistinctOn(distinctOnPb.getName()); } return self(); } @@ -717,7 +704,7 @@ public static final class KeyQueryBuilder extends BaseBuilder distinct() { + public List distinctOn() { return distinctOn; } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index 41f0c4df8d92..f0fbd4ab96c0 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -428,14 +428,15 @@ public void testRunStructuredQuery() { assertTrue(projectionEntity.names().isEmpty()); assertFalse(results2.hasNext()); - StructuredQuery projectionQuery = Query.projectionEntityQueryBuilder() - .kind(KIND2) - .projection("age") - .filter(PropertyFilter.gt("age", 18)) - .distinct("age") - .orderBy(OrderBy.asc("age")) - .limit(10) - .build(); + StructuredQuery projectionQuery = + Query.projectionEntityQueryBuilder() + .kind(KIND2) + .projection("age") + .filter(PropertyFilter.gt("age", 18)) + .distinctOn("age") + .orderBy(OrderBy.asc("age")) + .limit(10) + .build(); QueryResults results4 = datastore.run(projectionQuery); assertTrue(results4.hasNext()); diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java index d946246db279..2e083250f35e 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java @@ -67,18 +67,19 @@ public class SerializationTest { .kind("k") .filter(PropertyFilter.eq("p1", "hello")) .build(); - private static final Query QUERY3 = Query.projectionEntityQueryBuilder() - .kind("k") - .namespace("ns1") - .projection("p") - .limit(100) - .offset(5) - .startCursor(CURSOR1) - .endCursor(CURSOR2) - .filter(CompositeFilter.and(PropertyFilter.gt("p1", 10), PropertyFilter.eq("a", "v"))) - .addDistinct("p") - .addOrderBy(OrderBy.asc("p")) - .build(); + private static final Query QUERY3 = + Query.projectionEntityQueryBuilder() + .kind("k") + .namespace("ns1") + .projection("p") + .limit(100) + .offset(5) + .startCursor(CURSOR1) + .endCursor(CURSOR2) + .filter(CompositeFilter.and(PropertyFilter.gt("p1", 10), PropertyFilter.eq("a", "v"))) + .addDistinctOn("p") + .addOrderBy(OrderBy.asc("p")) + .build(); private static final KeyValue KEY_VALUE = KeyValue.of(KEY1); private static final NullValue NULL_VALUE = NullValue.builder().excludeFromIndexes(true).build(); private static final StringValue STRING_VALUE = StringValue.of("hello"); From 35ea69a5a4102de920f7732b52e29ebc8372d974 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Fri, 16 Oct 2015 11:06:08 -0700 Subject: [PATCH 12/60] Add missing case for start_cursor --- .../com/google/gcloud/datastore/QueryResultsImpl.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java index 38eb3dd1c393..bb2b65fcc646 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java @@ -34,6 +34,7 @@ class QueryResultsImpl extends AbstractIterator implements QueryResults private Query query; private ResultType actualResultType; private com.google.datastore.v1beta3.QueryResultBatch queryResultBatchPb; + private com.google.datastore.v1beta3.Query mostRecentQueryPb; private boolean lastBatch; private Iterator entityResultPbIter; private ByteString cursor; @@ -58,7 +59,7 @@ class QueryResultsImpl extends AbstractIterator implements QueryResults if (queryResultBatchPb.getSkippedResults() > 0) { cursor = queryResultBatchPb.getSkippedCursor(); } else { - cursor = ByteString.EMPTY; + cursor = mostRecentQueryPb.getStartCursor(); } } @@ -70,7 +71,13 @@ private void sendRequest() { } requestPb.setPartitionId(partitionIdPb); query.populatePb(requestPb); - queryResultBatchPb = datastore.runQuery(requestPb.build()).getBatch(); + com.google.datastore.v1beta3.RunQueryResponse runQueryResponsePb = + datastore.runQuery(requestPb.build()); + queryResultBatchPb = runQueryResponsePb.getBatch(); + mostRecentQueryPb = runQueryResponsePb.getQuery(); + if (mostRecentQueryPb == null) { + mostRecentQueryPb = requestPb.getQuery(); + } lastBatch = queryResultBatchPb.getMoreResults() != MoreResultsType.NOT_FINISHED; entityResultPbIter = queryResultBatchPb.getEntityResultsList().iterator(); actualResultType = ResultType.fromPb(queryResultBatchPb.getEntityResultType()); From 02a56a831bd04734dd171a553dc569637d2059c6 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Fri, 16 Oct 2015 14:47:39 -0700 Subject: [PATCH 13/60] Don't allow user to set project id or namespace to null --- .../src/main/java/com/google/gcloud/ServiceOptions.java | 3 ++- .../src/main/java/com/google/gcloud/datastore/KeyFactory.java | 2 +- .../src/main/java/com/google/gcloud/datastore/Validator.java | 4 +--- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java index 206e1fecaa58..066116e59c2f 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java @@ -122,7 +122,8 @@ protected B self() { } public B projectId(String projectId) { - this.projectId = projectId; + this.projectId = + checkNotNull(projectId, "Project ID cannot be set to null. Leave unset for default."); return self(); } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/KeyFactory.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/KeyFactory.java index c5b9d77a8cd7..7d645cf21945 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/KeyFactory.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/KeyFactory.java @@ -29,7 +29,7 @@ public final class KeyFactory extends BaseKey.Builder { private final String ns; public KeyFactory(String projectId) { - this(projectId, null); + this(projectId, ""); } public KeyFactory(String projectId, String namespace) { diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Validator.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Validator.java index 791adec53101..dc7069cebf62 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Validator.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Validator.java @@ -45,9 +45,7 @@ static String validateDatabase(String projectId) { } static String validateNamespace(String namespace) { - if (Strings.isNullOrEmpty(namespace)) { - return ""; - } + checkArgument(namespace != null, "Namespace cannot be null. Leave unset for default."); checkArgument(namespace.length() <= MAX_NAMESPACE_LENGTH, "namespace must not contain more than 100 characters"); checkArgument(NAMESPACE_PATTERN.matcher(namespace).matches(), From a98d09640198c6d7effb6a8379a915664fb41b3f Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Fri, 16 Oct 2015 16:52:28 -0700 Subject: [PATCH 14/60] remove extra firstNonNull in KeyFactory --- .../src/main/java/com/google/gcloud/datastore/KeyFactory.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/KeyFactory.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/KeyFactory.java index 7d645cf21945..7c4efb91762d 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/KeyFactory.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/KeyFactory.java @@ -16,7 +16,6 @@ package com.google.gcloud.datastore; -import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableList; /** @@ -36,7 +35,7 @@ public KeyFactory(String projectId, String namespace) { super(projectId); namespace(namespace); this.pi = projectId; - this.ns = MoreObjects.firstNonNull(namespace, ""); + this.ns = namespace; } public IncompleteKey newKey() { From f6eedddbb5d0f8a297633a524cf0eee08046c53c Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 19 Oct 2015 13:08:54 -0700 Subject: [PATCH 15/60] Support GeoPoint value in Datastore v1beta3 --- .../google/gcloud/datastore/BaseEntity.java | 17 ++++ .../com/google/gcloud/datastore/GeoPoint.java | 83 ++++++++++++++++++ .../gcloud/datastore/GeoPointValue.java | 84 +++++++++++++++++++ .../com/google/gcloud/datastore/GqlQuery.java | 10 +++ .../gcloud/datastore/StructuredQuery.java | 5 ++ .../google/gcloud/datastore/ValueType.java | 6 +- .../gcloud/datastore/BaseEntityTest.java | 21 +++-- .../gcloud/datastore/DatastoreTest.java | 28 ++++--- .../google/gcloud/datastore/GeoPointTest.java | 77 +++++++++++++++++ .../gcloud/datastore/GeoPointValueTest.java | 52 ++++++++++++ .../gcloud/datastore/SerializationTest.java | 38 ++++----- .../google/gcloud/datastore/ValueTest.java | 2 + 12 files changed, 384 insertions(+), 39 deletions(-) create mode 100644 gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPoint.java create mode 100644 gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPointValue.java create mode 100644 gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/GeoPointTest.java create mode 100644 gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/GeoPointValueTest.java diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseEntity.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseEntity.java index 97718e708e83..06ed0f419ac7 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseEntity.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseEntity.java @@ -21,6 +21,7 @@ import static com.google.gcloud.datastore.DateTimeValue.of; import static com.google.gcloud.datastore.DoubleValue.of; import static com.google.gcloud.datastore.EntityValue.of; +import static com.google.gcloud.datastore.GeoPointValue.of; import static com.google.gcloud.datastore.KeyValue.of; import static com.google.gcloud.datastore.ListValue.of; import static com.google.gcloud.datastore.LongValue.of; @@ -159,6 +160,11 @@ public B set(String name, DateTime value) { return self(); } + public B set(String name, GeoPoint value) { + properties.put(name, of(value)); + return self(); + } + public B set(String name, Key value) { properties.put(name, of(value)); return self(); @@ -320,6 +326,17 @@ public DateTime getDateTime(String name) { return ((Value) getValue(name)).get(); } + /** + * Returns the property value as a GeoPoint. + * + * @throws DatastoreException if not such property. + * @throws ClassCastException if value is not a GeoPoint. + */ + @SuppressWarnings("unchecked") + public GeoPoint getGeoPoint(String name) { + return ((Value) getValue(name)).get(); + } + /** * Returns the property value as a Key. * diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPoint.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPoint.java new file mode 100644 index 000000000000..5d7d87ebc121 --- /dev/null +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPoint.java @@ -0,0 +1,83 @@ +/* + * Copyright 2015 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.datastore; + +import static com.google.common.base.Preconditions.checkArgument; + +import com.google.protobuf.InvalidProtocolBufferException; + +import java.util.Objects; + +/** + * A Google Cloud Datastore GeoPoint (represented by latitude and longitude in degrees). + * This class is immutable. + * + * @see Google Cloud Datastore + * Entities, Properties, and Keys + */ +public final class GeoPoint extends Serializable { + + private static final long serialVersionUID = 9077060962655752073L; + + private final transient double latitude; + private final transient double longitude; + + GeoPoint(double latitude, double longitude) { + checkArgument( + latitude >= -90.0 && latitude <= 90.0, "latitude must be in the range [-90, 90] degrees"); + checkArgument( + longitude >= -180.0 && longitude <= 180.0, + "latitude must be in the range [-180, 180] degrees"); + this.latitude = latitude; + this.longitude = longitude; + } + + @Override + public String toString() { + return Double.toString(latitude) + ", " + Double.toString(longitude); + } + + @Override + public int hashCode() { + return Objects.hash(latitude, longitude); + } + + @Override + public boolean equals(Object obj) { + return obj == this + || (obj instanceof GeoPoint && new Double(this.latitude).equals(((GeoPoint) obj).latitude)) + && new Double(this.longitude).equals(((GeoPoint) obj).longitude); + } + + public static GeoPoint of(double latitude, double longitude) { + return new GeoPoint(latitude, longitude); + } + + @Override + protected com.google.type.LatLng toPb() { + return com.google.type.LatLng.newBuilder() + .setLatitude(latitude) + .setLongitude(longitude) + .build(); + } + + @Override + protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException { + com.google.type.LatLng parsedLatLng = com.google.type.LatLng.parseFrom(bytesPb); + return new GeoPoint(parsedLatLng.getLatitude(), parsedLatLng.getLongitude()); + } +} diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPointValue.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPointValue.java new file mode 100644 index 000000000000..0f6b5c2003e6 --- /dev/null +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPointValue.java @@ -0,0 +1,84 @@ +/* + * Copyright 2015 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.datastore; + +import static com.google.datastore.v1beta3.Value.GEO_POINT_VALUE_FIELD_NUMBER; + +public final class GeoPointValue extends Value { + + private static final long serialVersionUID = -5810614280642405898L; + + static final BaseMarshaller MARSHALLER = + new BaseMarshaller() { + + private static final long serialVersionUID = -3550567536035178649L; + + @Override + public int getProtoFieldId() { + return GEO_POINT_VALUE_FIELD_NUMBER; + } + + @Override + public Builder newBuilder(GeoPoint value) { + return builder(value); + } + + @Override + protected GeoPoint getValue(com.google.datastore.v1beta3.Value from) { + return new GeoPoint( + from.getGeoPointValue().getLatitude(), from.getGeoPointValue().getLongitude()); + } + + @Override + protected void setValue(GeoPointValue from, com.google.datastore.v1beta3.Value.Builder to) { + to.setGeoPointValue(from.get().toPb()); + } + }; + + public static final class Builder extends Value.BaseBuilder { + + private Builder() { + super(ValueType.GEO_POINT); + } + + @Override + public GeoPointValue build() { + return new GeoPointValue(this); + } + } + + public GeoPointValue(GeoPoint value) { + this(builder(value)); + } + + private GeoPointValue(Builder builder) { + super(builder); + } + + @Override + public Builder toBuilder() { + return new Builder().mergeFrom(this); + } + + public static GeoPointValue of(GeoPoint value) { + return new GeoPointValue(value); + } + + public static Builder builder(GeoPoint value) { + return new Builder().set(value); + } +} diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java index 7fcb562fea89..2205c7c270a3 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java @@ -212,6 +212,11 @@ public Builder setBinding(String name, DateTime... value) { namedBindings.put(name, toBinding(DateTimeValue.MARSHALLER, Arrays.asList(value))); return this; } + + public Builder setBinding(String name, GeoPoint... value) { + namedBindings.put(name, toBinding(GeoPointValue.MARSHALLER, Arrays.asList(value))); + return this; + } public Builder setBinding(String name, Key... value) { namedBindings.put(name, toBinding(KeyValue.MARSHALLER, Arrays.asList(value))); @@ -258,6 +263,11 @@ public Builder addBinding(DateTime... value) { return this; } + public Builder addBinding(GeoPoint... value) { + positionalBindings.add(toBinding(GeoPointValue.MARSHALLER, Arrays.asList(value))); + return this; + } + public Builder addBinding(Key... value) { positionalBindings.add(toBinding(KeyValue.MARSHALLER, Arrays.asList(value))); return this; diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java index 1b82991cb8c8..87f92f6d05b4 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java @@ -21,6 +21,7 @@ import static com.google.gcloud.datastore.BooleanValue.of; import static com.google.gcloud.datastore.DateTimeValue.of; import static com.google.gcloud.datastore.DoubleValue.of; +import static com.google.gcloud.datastore.GeoPointValue.of; import static com.google.gcloud.datastore.KeyValue.of; import static com.google.gcloud.datastore.LongValue.of; import static com.google.gcloud.datastore.StringValue.of; @@ -420,6 +421,10 @@ public static PropertyFilter eq(String property, DateTime value) { return new PropertyFilter(property, Operator.EQUAL, of(value)); } + public static PropertyFilter eq(String property, GeoPoint value) { + return new PropertyFilter(property, Operator.EQUAL, of(value)); + } + public static PropertyFilter eq(String property, Key value) { return new PropertyFilter(property, Operator.EQUAL, of(value)); } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ValueType.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ValueType.java index 20c89a86e7a0..8326001c8fbc 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ValueType.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ValueType.java @@ -78,12 +78,12 @@ public enum ValueType { /** * Represents a raw/unparsed value. */ - RAW_VALUE(RawValue.MARSHALLER); + RAW_VALUE(RawValue.MARSHALLER), /** - * TODO(ajaykannan): add GEO_POINT_VALUE - * Will represent a geolocation value in latitude/longitude + * Represents a {@link GeoPoint} value */ + GEO_POINT(GeoPointValue.MARSHALLER); private static final ImmutableMap DESCRIPTOR_TO_TYPE_MAP; diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseEntityTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseEntityTest.java index 5ece01508d3a..8053b2d80b1d 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseEntityTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseEntityTest.java @@ -35,6 +35,7 @@ public class BaseEntityTest { private static final Blob BLOB = Blob.copyFrom(new byte[]{1, 2}); private static final DateTime DATE_TIME = DateTime.now(); + private static final GeoPoint GEO_POINT = new GeoPoint(30.5, -40.5); private static final Key KEY = Key.builder("ds1", "k1", "n1").build(); private static final Entity ENTITY = Entity.builder(KEY).set("name", "foo").build(); private static final IncompleteKey INCOMPLETE_KEY = IncompleteKey.builder("ds1", "k1").build(); @@ -62,9 +63,9 @@ public void setUp() { builder = new Builder(); builder.set("blob", BLOB).set("boolean", true).set("dateTime", DATE_TIME); builder.set("double", 1.25).set("key", KEY).set("string", "hello world"); - builder.set("long", 125).setNull("null").set("entity", ENTITY); + builder.set("long", 125).setNull("null").set("entity", ENTITY).set("geoPoint", GEO_POINT); builder.set("partialEntity", PARTIAL_ENTITY).set("stringValue", StringValue.of("bla")); - builder.set("list1", NullValue.of(), StringValue.of("foo")); + builder.set("list1", NullValue.of(), StringValue.of("foo"), GeoPointValue.of(GEO_POINT)); builder.set("list2", ImmutableList.of(LongValue.of(10), DoubleValue.of(2))); builder.set("list3", Collections.singletonList(BooleanValue.of(true))); } @@ -149,6 +150,12 @@ public void testGetDateTime() throws Exception { assertEquals(dateTime, entity.getDateTime("dateTime")); } + @Test + public void testGetGeoPoint() throws Exception { + BaseEntity entity = builder.build(); + assertEquals(GEO_POINT, entity.getGeoPoint("geoPoint")); + } + @Test public void testGetKey() throws Exception { BaseEntity entity = builder.build(); @@ -171,9 +178,10 @@ public void testGetEntity() throws Exception { public void testGetList() throws Exception { BaseEntity entity = builder.build(); List> list = entity.getList("list1"); - assertEquals(2, list.size()); + assertEquals(3, list.size()); assertEquals(NullValue.of(), list.get(0)); assertEquals("foo", list.get(1).get()); + assertEquals(GEO_POINT, list.get(2).get()); list = entity.getList("list2"); assertEquals(2, list.size()); assertEquals(Long.valueOf(10), list.get(0).get()); @@ -196,9 +204,10 @@ public void testGetBlob() throws Exception { @Test public void testNames() throws Exception { - Set names = ImmutableSet.builder() - .add("string", "stringValue", "boolean", "double", "long", "list1", "list2", "list3") - .add("entity", "partialEntity", "null", "dateTime", "blob", "key") + Set names = + ImmutableSet.builder() + .add("string", "stringValue", "boolean", "double", "long", "list1", "list2", "list3") + .add("entity", "partialEntity", "null", "dateTime", "geoPoint", "blob", "key") .build(); BaseEntity entity = builder.build(); assertEquals(names, entity.names()); diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index f0fbd4ab96c0..320c4f3d67c8 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -74,6 +74,7 @@ public class DatastoreTest { .build(); private static final ListValue LIST_VALUE2 = ListValue.of(Collections.singletonList(KEY_VALUE)); private static final DateTimeValue DATE_TIME_VALUE = new DateTimeValue(DateTime.now()); + private static final GeoPointValue GEO_POINT_VALUE = new GeoPointValue(new GeoPoint(30.5, 40.5)); private static final FullEntity PARTIAL_ENTITY1 = FullEntity.builder(INCOMPLETE_KEY2).set("str", STR_VALUE).set("bool", BOOL_VALUE) .set("list", LIST_VALUE1).build(); @@ -83,13 +84,15 @@ public class DatastoreTest { private static final FullEntity PARTIAL_ENTITY3 = FullEntity.builder(PARTIAL_ENTITY1).key(IncompleteKey.builder(PROJECT_ID, KIND3).build()) .build(); - private static final Entity ENTITY1 = Entity.builder(KEY1) - .set("str", STR_VALUE) - .set("date", DATE_TIME_VALUE) - .set("bool", BOOL_VALUE) - .set("partial1", EntityValue.of(PARTIAL_ENTITY1)) - .set("list", LIST_VALUE2) - .build(); + private static final Entity ENTITY1 = + Entity.builder(KEY1) + .set("str", STR_VALUE) + .set("date", DATE_TIME_VALUE) + .set("geoPoint", GEO_POINT_VALUE) + .set("bool", BOOL_VALUE) + .set("partial1", EntityValue.of(PARTIAL_ENTITY1)) + .set("list", LIST_VALUE2) + .build(); private static final Entity ENTITY2 = Entity.builder(ENTITY1).key(KEY2).remove("str") .set("name", "Dan").setNull("null").set("age", 20).build(); private static final Entity ENTITY3 = Entity.builder(ENTITY1).key(KEY3).remove("str") @@ -504,9 +507,11 @@ public void testGet() { assertEquals(LIST_VALUE2, value3); DateTimeValue value4 = entity.getValue("date"); assertEquals(DATE_TIME_VALUE, value4); - FullEntity value5 = entity.getEntity("partial1"); - assertEquals(PARTIAL_ENTITY1, value5); - assertEquals(5, entity.names().size()); + GeoPointValue value5 = entity.getValue("geoPoint"); + assertEquals(GEO_POINT_VALUE, value5); + FullEntity value6 = entity.getEntity("partial1"); + assertEquals(PARTIAL_ENTITY1, value6); + assertEquals(6, entity.names().size()); assertFalse(entity.contains("bla")); } @@ -528,7 +533,8 @@ public void testGetArray() { assertEquals(PARTIAL_ENTITY2, partial1); assertEquals(ENTITY2, partial2); assertEquals(ValueType.BOOLEAN, entity3.getValue("bool").type()); - assertEquals(6, entity3.names().size()); + assertEquals(GEO_POINT_VALUE, entity3.getValue("geoPoint")); + assertEquals(7, entity3.names().size()); assertFalse(entity3.contains("bla")); try { entity3.getString("str"); diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/GeoPointTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/GeoPointTest.java new file mode 100644 index 000000000000..61bcf0fd4f26 --- /dev/null +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/GeoPointTest.java @@ -0,0 +1,77 @@ +/* + * Copyright 2015 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.datastore; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class GeoPointTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private static GeoPoint gp1 = new GeoPoint(10.5, 20.5); + private static GeoPoint gp2 = new GeoPoint(10.5, 30.5); + + private static final String INVALID_LAT_MESSAGE = + "latitude must be in the range [-90, 90] degrees"; + private static final String INVALID_LNG_MESSAGE = + "latitude must be in the range [-180, 180] degrees"; + + @Test + public void testEquals() { + assertEquals(gp1, gp1); + assertNotEquals(gp1, gp2); + } + + @Test + public void testUpperLatRange() { + new GeoPoint(90, 0); + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage(INVALID_LAT_MESSAGE); + new GeoPoint(91, 0); + } + + @Test + public void testLowerLatRange() { + new GeoPoint(-90, 0); + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage(INVALID_LAT_MESSAGE); + new GeoPoint(-91, 0); + } + + @Test + public void testUpperLngRange() { + new GeoPoint(0, 180); + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage(INVALID_LNG_MESSAGE); + new GeoPoint(0, 181); + } + + @Test + public void testLowerLngRange() { + new GeoPoint(0, 180); + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage(INVALID_LNG_MESSAGE); + new GeoPoint(0, -181); + } +} + diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/GeoPointValueTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/GeoPointValueTest.java new file mode 100644 index 000000000000..82380efca8d3 --- /dev/null +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/GeoPointValueTest.java @@ -0,0 +1,52 @@ +/* + * Copyright 2015 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.datastore; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class GeoPointValueTest { + +private static final GeoPoint CONTENT = new GeoPoint(37.4, -122.1); + + @Test + public void testToBuilder() throws Exception { + GeoPointValue value = GeoPointValue.of(CONTENT); + assertEquals(value, value.toBuilder().build()); + } + + @SuppressWarnings("deprecation") + @Test + public void testOf() throws Exception { + GeoPointValue value = GeoPointValue.of(CONTENT); + assertEquals(CONTENT, value.get()); + assertFalse(value.excludeFromIndexes()); + } + + @SuppressWarnings("deprecation") + @Test + public void testBuilder() throws Exception { + GeoPointValue.Builder builder = GeoPointValue.builder(CONTENT); + GeoPointValue value = builder.meaning(1).excludeFromIndexes(true).build(); + assertEquals(CONTENT, value.get()); + assertEquals(1, value.meaning()); + assertTrue(value.excludeFromIndexes()); + } +} diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java index 2e083250f35e..fe2be0a04b76 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java @@ -45,6 +45,7 @@ public class SerializationTest { IncompleteKey.builder(KEY1, "v").ancestors(PathElement.of("p", 1)).build(); private static final Key KEY2 = Key.builder(KEY1, "v", 2).build(); private static final DateTime DATE_TIME1 = DateTime.now(); + private static final GeoPoint GEO_POINT = new GeoPoint(30.5, 40.5); private static final Blob BLOB1 = Blob.copyFrom(UTF_8.encode("hello world")); private static final Cursor CURSOR1 = Cursor.copyFrom(new byte[] {1,2}); private static final Cursor CURSOR2 = Cursor.copyFrom(new byte[]{10}); @@ -89,10 +90,8 @@ public class SerializationTest { private static final DateTimeValue DATE_AND_TIME_VALUE = DateTimeValue.of(DateTime.now()); private static final BlobValue BLOB_VALUE = BlobValue.of(BLOB1); private static final RawValue RAW_VALUE = - RawValue.of(com.google.datastore.v1beta3.Value.newBuilder() - .setGeoPointValue(com.google.type.LatLng.newBuilder() - .setLatitude(0.0).setLongitude(0.0).build()) - .setMeaning(18).build()); + RawValue.of(com.google.datastore.v1beta3.Value.newBuilder().setMeaning(18).build()); + private static final GeoPointValue GEO_POINT_VALUE = GeoPointValue.of(GEO_POINT); private static final Entity ENTITY1 = Entity.builder(KEY1).build(); private static final Entity ENTITY2 = Entity.builder(KEY2).set("null", NullValue.of()).build(); @@ -119,19 +118,20 @@ public class SerializationTest { @SuppressWarnings("rawtypes") private static final Multimap TYPE_TO_VALUES = ImmutableMultimap.builder() - .put(ValueType.NULL, NULL_VALUE) - .put(ValueType.KEY, KEY_VALUE) - .put(ValueType.STRING, STRING_VALUE) - .putAll(ValueType.ENTITY, EMBEDDED_ENTITY_VALUE1, EMBEDDED_ENTITY_VALUE2, - EMBEDDED_ENTITY_VALUE3) - .put(ValueType.LIST, LIST_VALUE) - .put(ValueType.LONG, LONG_VALUE) - .put(ValueType.DOUBLE, DOUBLE_VALUE) - .put(ValueType.BOOLEAN, BOOLEAN_VALUE) - .put(ValueType.DATE_TIME, DATE_AND_TIME_VALUE) - .put(ValueType.BLOB, BLOB_VALUE) - .put(ValueType.RAW_VALUE, RAW_VALUE) - .build(); + .put(ValueType.NULL, NULL_VALUE) + .put(ValueType.KEY, KEY_VALUE) + .put(ValueType.STRING, STRING_VALUE) + .putAll(ValueType.ENTITY, EMBEDDED_ENTITY_VALUE1, EMBEDDED_ENTITY_VALUE2, + EMBEDDED_ENTITY_VALUE3) + .put(ValueType.LIST, LIST_VALUE) + .put(ValueType.LONG, LONG_VALUE) + .put(ValueType.DOUBLE, DOUBLE_VALUE) + .put(ValueType.BOOLEAN, BOOLEAN_VALUE) + .put(ValueType.DATE_TIME, DATE_AND_TIME_VALUE) + .put(ValueType.BLOB, BLOB_VALUE) + .put(ValueType.RAW_VALUE, RAW_VALUE) + .put(ValueType.GEO_POINT, GEO_POINT_VALUE) + .build(); @Test public void testServiceOptions() throws Exception { @@ -169,8 +169,8 @@ public void testValues() throws Exception { @Test public void testTypes() throws Exception { Serializable[] types = { KEY1, KEY2, INCOMPLETE_KEY1, INCOMPLETE_KEY2, ENTITY1, ENTITY2, - ENTITY3, EMBEDDED_ENTITY, PROJECTION_ENTITY, DATE_TIME1, BLOB1, CURSOR1, GQL1, GQL2, - QUERY1, QUERY2, QUERY3}; + ENTITY3, EMBEDDED_ENTITY, PROJECTION_ENTITY, DATE_TIME1, BLOB1, GEO_POINT, CURSOR1, GQL1, + GQL2, QUERY1, QUERY2, QUERY3}; for (Serializable obj : types) { Object copy = serializeAndDeserialize(obj); assertEquals(obj, obj); diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/ValueTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/ValueTest.java index 12908add4c8e..771b3c556a12 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/ValueTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/ValueTest.java @@ -41,6 +41,7 @@ public class ValueTest { private static final NullValue NULL_VALUE = NullValue.of(); private static final StringValue STRING_VALUE = StringValue.of("hello"); private static final RawValue RAW_VALUE = RawValue.of(STRING_VALUE.toPb()); + private static final GeoPointValue GEO_POINT_VALUE = GeoPointValue.of(new GeoPoint(30.5, 40.5)); private static final ImmutableMap TYPES = ImmutableMap.builder() .put(ValueType.NULL, new Object[] {NullValue.class, NULL_VALUE.get()}) .put(ValueType.KEY, new Object[] {KeyValue.class, KEY}) @@ -53,6 +54,7 @@ public class ValueTest { new Object[] {ListValue.class, ImmutableList.of(NULL_VALUE, STRING_VALUE, RAW_VALUE)}) .put(ValueType.LONG, new Object[] {LongValue.class, 123L}) .put(ValueType.RAW_VALUE, new Object[] {RawValue.class, RAW_VALUE.get()}) + .put(ValueType.GEO_POINT, new Object[] {GeoPointValue.class, GEO_POINT_VALUE.get()}) .put(ValueType.STRING, new Object[] {StringValue.class, STRING_VALUE.get()}) .build(); From 3ccc7ed1660a1cc5eb0853bbdd2a38ba10e996e8 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 19 Oct 2015 17:01:57 -0700 Subject: [PATCH 16/60] Add getters, remove filtering --- .../java/com/google/gcloud/datastore/GeoPoint.java | 13 ++++++++++--- .../java/com/google/gcloud/datastore/GqlQuery.java | 10 ---------- .../google/gcloud/datastore/StructuredQuery.java | 5 ----- .../com/google/gcloud/datastore/BaseEntityTest.java | 2 +- .../com/google/gcloud/datastore/DatastoreTest.java | 3 ++- .../com/google/gcloud/datastore/GeoPointTest.java | 4 ++-- .../google/gcloud/datastore/SerializationTest.java | 2 +- 7 files changed, 16 insertions(+), 23 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPoint.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPoint.java index 5d7d87ebc121..39dde9a9f85b 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPoint.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPoint.java @@ -46,6 +46,14 @@ public final class GeoPoint extends Serializable { this.longitude = longitude; } + public double getLatitude() { + return latitude; + } + + public double getLongitude() { + return longitude; + } + @Override public String toString() { return Double.toString(latitude) + ", " + Double.toString(longitude); @@ -58,9 +66,8 @@ public int hashCode() { @Override public boolean equals(Object obj) { - return obj == this - || (obj instanceof GeoPoint && new Double(this.latitude).equals(((GeoPoint) obj).latitude)) - && new Double(this.longitude).equals(((GeoPoint) obj).longitude); + return obj == this || (obj instanceof GeoPoint && this.latitude == ((GeoPoint) obj).latitude + && this.longitude == ((GeoPoint) obj).longitude); } public static GeoPoint of(double latitude, double longitude) { diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java index 2205c7c270a3..7fcb562fea89 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java @@ -212,11 +212,6 @@ public Builder setBinding(String name, DateTime... value) { namedBindings.put(name, toBinding(DateTimeValue.MARSHALLER, Arrays.asList(value))); return this; } - - public Builder setBinding(String name, GeoPoint... value) { - namedBindings.put(name, toBinding(GeoPointValue.MARSHALLER, Arrays.asList(value))); - return this; - } public Builder setBinding(String name, Key... value) { namedBindings.put(name, toBinding(KeyValue.MARSHALLER, Arrays.asList(value))); @@ -263,11 +258,6 @@ public Builder addBinding(DateTime... value) { return this; } - public Builder addBinding(GeoPoint... value) { - positionalBindings.add(toBinding(GeoPointValue.MARSHALLER, Arrays.asList(value))); - return this; - } - public Builder addBinding(Key... value) { positionalBindings.add(toBinding(KeyValue.MARSHALLER, Arrays.asList(value))); return this; diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java index 87f92f6d05b4..1b82991cb8c8 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java @@ -21,7 +21,6 @@ import static com.google.gcloud.datastore.BooleanValue.of; import static com.google.gcloud.datastore.DateTimeValue.of; import static com.google.gcloud.datastore.DoubleValue.of; -import static com.google.gcloud.datastore.GeoPointValue.of; import static com.google.gcloud.datastore.KeyValue.of; import static com.google.gcloud.datastore.LongValue.of; import static com.google.gcloud.datastore.StringValue.of; @@ -421,10 +420,6 @@ public static PropertyFilter eq(String property, DateTime value) { return new PropertyFilter(property, Operator.EQUAL, of(value)); } - public static PropertyFilter eq(String property, GeoPoint value) { - return new PropertyFilter(property, Operator.EQUAL, of(value)); - } - public static PropertyFilter eq(String property, Key value) { return new PropertyFilter(property, Operator.EQUAL, of(value)); } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseEntityTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseEntityTest.java index 8053b2d80b1d..5ca68bf848ce 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseEntityTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseEntityTest.java @@ -35,7 +35,7 @@ public class BaseEntityTest { private static final Blob BLOB = Blob.copyFrom(new byte[]{1, 2}); private static final DateTime DATE_TIME = DateTime.now(); - private static final GeoPoint GEO_POINT = new GeoPoint(30.5, -40.5); + private static final GeoPoint GEO_POINT = new GeoPoint(37.422035, -122.084124); private static final Key KEY = Key.builder("ds1", "k1", "n1").build(); private static final Entity ENTITY = Entity.builder(KEY).set("name", "foo").build(); private static final IncompleteKey INCOMPLETE_KEY = IncompleteKey.builder("ds1", "k1").build(); diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index 320c4f3d67c8..c890a6b67aa8 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -74,7 +74,8 @@ public class DatastoreTest { .build(); private static final ListValue LIST_VALUE2 = ListValue.of(Collections.singletonList(KEY_VALUE)); private static final DateTimeValue DATE_TIME_VALUE = new DateTimeValue(DateTime.now()); - private static final GeoPointValue GEO_POINT_VALUE = new GeoPointValue(new GeoPoint(30.5, 40.5)); + private static final GeoPointValue GEO_POINT_VALUE = + new GeoPointValue(new GeoPoint(37.422035, -122.084124)); private static final FullEntity PARTIAL_ENTITY1 = FullEntity.builder(INCOMPLETE_KEY2).set("str", STR_VALUE).set("bool", BOOL_VALUE) .set("list", LIST_VALUE1).build(); diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/GeoPointTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/GeoPointTest.java index 61bcf0fd4f26..2f076d915c15 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/GeoPointTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/GeoPointTest.java @@ -28,8 +28,8 @@ public class GeoPointTest { @Rule public ExpectedException thrown = ExpectedException.none(); - private static GeoPoint gp1 = new GeoPoint(10.5, 20.5); - private static GeoPoint gp2 = new GeoPoint(10.5, 30.5); + private static GeoPoint gp1 = new GeoPoint(37.422035, -122.084124); + private static GeoPoint gp2 = new GeoPoint(0.0, 0.0); private static final String INVALID_LAT_MESSAGE = "latitude must be in the range [-90, 90] degrees"; diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java index fe2be0a04b76..b2ee24862688 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java @@ -45,7 +45,7 @@ public class SerializationTest { IncompleteKey.builder(KEY1, "v").ancestors(PathElement.of("p", 1)).build(); private static final Key KEY2 = Key.builder(KEY1, "v", 2).build(); private static final DateTime DATE_TIME1 = DateTime.now(); - private static final GeoPoint GEO_POINT = new GeoPoint(30.5, 40.5); + private static final GeoPoint GEO_POINT = new GeoPoint(37.422035, -122.084124); private static final Blob BLOB1 = Blob.copyFrom(UTF_8.encode("hello world")); private static final Cursor CURSOR1 = Cursor.copyFrom(new byte[] {1,2}); private static final Cursor CURSOR2 = Cursor.copyFrom(new byte[]{10}); From d75b0da95b0da4419423a56c437830e668ebbcc6 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 19 Oct 2015 19:43:47 -0700 Subject: [PATCH 17/60] rename accessors in geopoint --- .../src/main/java/com/google/gcloud/datastore/GeoPoint.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPoint.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPoint.java index 39dde9a9f85b..34532a30d927 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPoint.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPoint.java @@ -46,11 +46,11 @@ public final class GeoPoint extends Serializable { this.longitude = longitude; } - public double getLatitude() { + public double latitude() { return latitude; } - public double getLongitude() { + public double longitude() { return longitude; } From 241960f2a24d4dbec27288589ccf72f91c431816 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Tue, 20 Oct 2015 08:27:04 -0700 Subject: [PATCH 18/60] rename geopoint to latlng --- .../google/gcloud/datastore/BaseEntity.java | 12 +++---- .../datastore/{GeoPoint.java => LatLng.java} | 16 ++++----- .../{GeoPointValue.java => LatLngValue.java} | 32 ++++++++--------- .../google/gcloud/datastore/ValueType.java | 4 +-- .../gcloud/datastore/BaseEntityTest.java | 14 ++++---- .../gcloud/datastore/DatastoreTest.java | 12 +++---- .../{GeoPointTest.java => LatLngTest.java} | 22 ++++++------ ...intValueTest.java => LatLngValueTest.java} | 12 +++---- .../gcloud/datastore/SerializationTest.java | 8 ++--- .../google/gcloud/datastore/ValueTest.java | 34 ++++++++++--------- 10 files changed, 84 insertions(+), 82 deletions(-) rename gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/{GeoPoint.java => LatLng.java} (79%) rename gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/{GeoPointValue.java => LatLngValue.java} (63%) rename gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/{GeoPointTest.java => LatLngTest.java} (83%) rename gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/{GeoPointValueTest.java => LatLngValueTest.java} (78%) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseEntity.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseEntity.java index 06ed0f419ac7..1846c2a0e7ae 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseEntity.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseEntity.java @@ -21,7 +21,7 @@ import static com.google.gcloud.datastore.DateTimeValue.of; import static com.google.gcloud.datastore.DoubleValue.of; import static com.google.gcloud.datastore.EntityValue.of; -import static com.google.gcloud.datastore.GeoPointValue.of; +import static com.google.gcloud.datastore.LatLngValue.of; import static com.google.gcloud.datastore.KeyValue.of; import static com.google.gcloud.datastore.ListValue.of; import static com.google.gcloud.datastore.LongValue.of; @@ -160,7 +160,7 @@ public B set(String name, DateTime value) { return self(); } - public B set(String name, GeoPoint value) { + public B set(String name, LatLng value) { properties.put(name, of(value)); return self(); } @@ -327,14 +327,14 @@ public DateTime getDateTime(String name) { } /** - * Returns the property value as a GeoPoint. + * Returns the property value as a LatLng. * * @throws DatastoreException if not such property. - * @throws ClassCastException if value is not a GeoPoint. + * @throws ClassCastException if value is not a LatLng. */ @SuppressWarnings("unchecked") - public GeoPoint getGeoPoint(String name) { - return ((Value) getValue(name)).get(); + public LatLng getLatLng(String name) { + return ((Value) getValue(name)).get(); } /** diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPoint.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/LatLng.java similarity index 79% rename from gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPoint.java rename to gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/LatLng.java index 34532a30d927..7e2b42fac4d3 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPoint.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/LatLng.java @@ -23,20 +23,20 @@ import java.util.Objects; /** - * A Google Cloud Datastore GeoPoint (represented by latitude and longitude in degrees). + * A Google Cloud Datastore LatLng (represented by latitude and longitude in degrees). * This class is immutable. * * @see Google Cloud Datastore * Entities, Properties, and Keys */ -public final class GeoPoint extends Serializable { +public final class LatLng extends Serializable { private static final long serialVersionUID = 9077060962655752073L; private final transient double latitude; private final transient double longitude; - GeoPoint(double latitude, double longitude) { + LatLng(double latitude, double longitude) { checkArgument( latitude >= -90.0 && latitude <= 90.0, "latitude must be in the range [-90, 90] degrees"); checkArgument( @@ -66,12 +66,12 @@ public int hashCode() { @Override public boolean equals(Object obj) { - return obj == this || (obj instanceof GeoPoint && this.latitude == ((GeoPoint) obj).latitude - && this.longitude == ((GeoPoint) obj).longitude); + return obj == this || (obj instanceof LatLng && this.latitude == ((LatLng) obj).latitude + && this.longitude == ((LatLng) obj).longitude); } - public static GeoPoint of(double latitude, double longitude) { - return new GeoPoint(latitude, longitude); + public static LatLng of(double latitude, double longitude) { + return new LatLng(latitude, longitude); } @Override @@ -85,6 +85,6 @@ protected com.google.type.LatLng toPb() { @Override protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException { com.google.type.LatLng parsedLatLng = com.google.type.LatLng.parseFrom(bytesPb); - return new GeoPoint(parsedLatLng.getLatitude(), parsedLatLng.getLongitude()); + return new LatLng(parsedLatLng.getLatitude(), parsedLatLng.getLongitude()); } } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPointValue.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/LatLngValue.java similarity index 63% rename from gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPointValue.java rename to gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/LatLngValue.java index 0f6b5c2003e6..91723f710816 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPointValue.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/LatLngValue.java @@ -18,12 +18,12 @@ import static com.google.datastore.v1beta3.Value.GEO_POINT_VALUE_FIELD_NUMBER; -public final class GeoPointValue extends Value { +public final class LatLngValue extends Value { private static final long serialVersionUID = -5810614280642405898L; - static final BaseMarshaller MARSHALLER = - new BaseMarshaller() { + static final BaseMarshaller MARSHALLER = + new BaseMarshaller() { private static final long serialVersionUID = -3550567536035178649L; @@ -33,39 +33,39 @@ public int getProtoFieldId() { } @Override - public Builder newBuilder(GeoPoint value) { + public Builder newBuilder(LatLng value) { return builder(value); } @Override - protected GeoPoint getValue(com.google.datastore.v1beta3.Value from) { - return new GeoPoint( + protected LatLng getValue(com.google.datastore.v1beta3.Value from) { + return new LatLng( from.getGeoPointValue().getLatitude(), from.getGeoPointValue().getLongitude()); } @Override - protected void setValue(GeoPointValue from, com.google.datastore.v1beta3.Value.Builder to) { + protected void setValue(LatLngValue from, com.google.datastore.v1beta3.Value.Builder to) { to.setGeoPointValue(from.get().toPb()); } }; - public static final class Builder extends Value.BaseBuilder { + public static final class Builder extends Value.BaseBuilder { private Builder() { - super(ValueType.GEO_POINT); + super(ValueType.LAT_LNG); } @Override - public GeoPointValue build() { - return new GeoPointValue(this); + public LatLngValue build() { + return new LatLngValue(this); } } - public GeoPointValue(GeoPoint value) { + public LatLngValue(LatLng value) { this(builder(value)); } - private GeoPointValue(Builder builder) { + private LatLngValue(Builder builder) { super(builder); } @@ -74,11 +74,11 @@ public Builder toBuilder() { return new Builder().mergeFrom(this); } - public static GeoPointValue of(GeoPoint value) { - return new GeoPointValue(value); + public static LatLngValue of(LatLng value) { + return new LatLngValue(value); } - public static Builder builder(GeoPoint value) { + public static Builder builder(LatLng value) { return new Builder().set(value); } } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ValueType.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ValueType.java index 8326001c8fbc..ab16126336f5 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ValueType.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ValueType.java @@ -81,9 +81,9 @@ public enum ValueType { RAW_VALUE(RawValue.MARSHALLER), /** - * Represents a {@link GeoPoint} value + * Represents a {@link LatLng} value */ - GEO_POINT(GeoPointValue.MARSHALLER); + LAT_LNG(LatLngValue.MARSHALLER); private static final ImmutableMap DESCRIPTOR_TO_TYPE_MAP; diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseEntityTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseEntityTest.java index 5ca68bf848ce..4be8b6c0d96a 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseEntityTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseEntityTest.java @@ -35,7 +35,7 @@ public class BaseEntityTest { private static final Blob BLOB = Blob.copyFrom(new byte[]{1, 2}); private static final DateTime DATE_TIME = DateTime.now(); - private static final GeoPoint GEO_POINT = new GeoPoint(37.422035, -122.084124); + private static final LatLng LAT_LNG = new LatLng(37.422035, -122.084124); private static final Key KEY = Key.builder("ds1", "k1", "n1").build(); private static final Entity ENTITY = Entity.builder(KEY).set("name", "foo").build(); private static final IncompleteKey INCOMPLETE_KEY = IncompleteKey.builder("ds1", "k1").build(); @@ -63,9 +63,9 @@ public void setUp() { builder = new Builder(); builder.set("blob", BLOB).set("boolean", true).set("dateTime", DATE_TIME); builder.set("double", 1.25).set("key", KEY).set("string", "hello world"); - builder.set("long", 125).setNull("null").set("entity", ENTITY).set("geoPoint", GEO_POINT); + builder.set("long", 125).setNull("null").set("entity", ENTITY).set("latLng", LAT_LNG); builder.set("partialEntity", PARTIAL_ENTITY).set("stringValue", StringValue.of("bla")); - builder.set("list1", NullValue.of(), StringValue.of("foo"), GeoPointValue.of(GEO_POINT)); + builder.set("list1", NullValue.of(), StringValue.of("foo"), LatLngValue.of(LAT_LNG)); builder.set("list2", ImmutableList.of(LongValue.of(10), DoubleValue.of(2))); builder.set("list3", Collections.singletonList(BooleanValue.of(true))); } @@ -151,9 +151,9 @@ public void testGetDateTime() throws Exception { } @Test - public void testGetGeoPoint() throws Exception { + public void testGetLatLng() throws Exception { BaseEntity entity = builder.build(); - assertEquals(GEO_POINT, entity.getGeoPoint("geoPoint")); + assertEquals(LAT_LNG, entity.getLatLng("latLng")); } @Test @@ -181,7 +181,7 @@ public void testGetList() throws Exception { assertEquals(3, list.size()); assertEquals(NullValue.of(), list.get(0)); assertEquals("foo", list.get(1).get()); - assertEquals(GEO_POINT, list.get(2).get()); + assertEquals(LAT_LNG, list.get(2).get()); list = entity.getList("list2"); assertEquals(2, list.size()); assertEquals(Long.valueOf(10), list.get(0).get()); @@ -207,7 +207,7 @@ public void testNames() throws Exception { Set names = ImmutableSet.builder() .add("string", "stringValue", "boolean", "double", "long", "list1", "list2", "list3") - .add("entity", "partialEntity", "null", "dateTime", "geoPoint", "blob", "key") + .add("entity", "partialEntity", "null", "dateTime", "latLng", "blob", "key") .build(); BaseEntity entity = builder.build(); assertEquals(names, entity.names()); diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index c890a6b67aa8..152aeea0a75e 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -74,8 +74,8 @@ public class DatastoreTest { .build(); private static final ListValue LIST_VALUE2 = ListValue.of(Collections.singletonList(KEY_VALUE)); private static final DateTimeValue DATE_TIME_VALUE = new DateTimeValue(DateTime.now()); - private static final GeoPointValue GEO_POINT_VALUE = - new GeoPointValue(new GeoPoint(37.422035, -122.084124)); + private static final LatLngValue LAT_LNG_VALUE = + new LatLngValue(new LatLng(37.422035, -122.084124)); private static final FullEntity PARTIAL_ENTITY1 = FullEntity.builder(INCOMPLETE_KEY2).set("str", STR_VALUE).set("bool", BOOL_VALUE) .set("list", LIST_VALUE1).build(); @@ -89,7 +89,7 @@ public class DatastoreTest { Entity.builder(KEY1) .set("str", STR_VALUE) .set("date", DATE_TIME_VALUE) - .set("geoPoint", GEO_POINT_VALUE) + .set("latLng", LAT_LNG_VALUE) .set("bool", BOOL_VALUE) .set("partial1", EntityValue.of(PARTIAL_ENTITY1)) .set("list", LIST_VALUE2) @@ -508,8 +508,8 @@ public void testGet() { assertEquals(LIST_VALUE2, value3); DateTimeValue value4 = entity.getValue("date"); assertEquals(DATE_TIME_VALUE, value4); - GeoPointValue value5 = entity.getValue("geoPoint"); - assertEquals(GEO_POINT_VALUE, value5); + LatLngValue value5 = entity.getValue("latLng"); + assertEquals(LAT_LNG_VALUE, value5); FullEntity value6 = entity.getEntity("partial1"); assertEquals(PARTIAL_ENTITY1, value6); assertEquals(6, entity.names().size()); @@ -534,7 +534,7 @@ public void testGetArray() { assertEquals(PARTIAL_ENTITY2, partial1); assertEquals(ENTITY2, partial2); assertEquals(ValueType.BOOLEAN, entity3.getValue("bool").type()); - assertEquals(GEO_POINT_VALUE, entity3.getValue("geoPoint")); + assertEquals(LAT_LNG_VALUE, entity3.getValue("latLng")); assertEquals(7, entity3.names().size()); assertFalse(entity3.contains("bla")); try { diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/GeoPointTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LatLngTest.java similarity index 83% rename from gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/GeoPointTest.java rename to gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LatLngTest.java index 2f076d915c15..1955ec236300 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/GeoPointTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LatLngTest.java @@ -23,13 +23,13 @@ import org.junit.Test; import org.junit.rules.ExpectedException; -public class GeoPointTest { +public class LatLngTest { @Rule public ExpectedException thrown = ExpectedException.none(); - private static GeoPoint gp1 = new GeoPoint(37.422035, -122.084124); - private static GeoPoint gp2 = new GeoPoint(0.0, 0.0); + private static LatLng gp1 = new LatLng(37.422035, -122.084124); + private static LatLng gp2 = new LatLng(0.0, 0.0); private static final String INVALID_LAT_MESSAGE = "latitude must be in the range [-90, 90] degrees"; @@ -44,34 +44,34 @@ public void testEquals() { @Test public void testUpperLatRange() { - new GeoPoint(90, 0); + new LatLng(90, 0); thrown.expect(IllegalArgumentException.class); thrown.expectMessage(INVALID_LAT_MESSAGE); - new GeoPoint(91, 0); + new LatLng(91, 0); } @Test public void testLowerLatRange() { - new GeoPoint(-90, 0); + new LatLng(-90, 0); thrown.expect(IllegalArgumentException.class); thrown.expectMessage(INVALID_LAT_MESSAGE); - new GeoPoint(-91, 0); + new LatLng(-91, 0); } @Test public void testUpperLngRange() { - new GeoPoint(0, 180); + new LatLng(0, 180); thrown.expect(IllegalArgumentException.class); thrown.expectMessage(INVALID_LNG_MESSAGE); - new GeoPoint(0, 181); + new LatLng(0, 181); } @Test public void testLowerLngRange() { - new GeoPoint(0, 180); + new LatLng(0, 180); thrown.expect(IllegalArgumentException.class); thrown.expectMessage(INVALID_LNG_MESSAGE); - new GeoPoint(0, -181); + new LatLng(0, -181); } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/GeoPointValueTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LatLngValueTest.java similarity index 78% rename from gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/GeoPointValueTest.java rename to gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LatLngValueTest.java index 82380efca8d3..3a25078237d4 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/GeoPointValueTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LatLngValueTest.java @@ -22,20 +22,20 @@ import org.junit.Test; -public class GeoPointValueTest { +public class LatLngValueTest { -private static final GeoPoint CONTENT = new GeoPoint(37.4, -122.1); +private static final LatLng CONTENT = new LatLng(37.4, -122.1); @Test public void testToBuilder() throws Exception { - GeoPointValue value = GeoPointValue.of(CONTENT); + LatLngValue value = LatLngValue.of(CONTENT); assertEquals(value, value.toBuilder().build()); } @SuppressWarnings("deprecation") @Test public void testOf() throws Exception { - GeoPointValue value = GeoPointValue.of(CONTENT); + LatLngValue value = LatLngValue.of(CONTENT); assertEquals(CONTENT, value.get()); assertFalse(value.excludeFromIndexes()); } @@ -43,8 +43,8 @@ public void testOf() throws Exception { @SuppressWarnings("deprecation") @Test public void testBuilder() throws Exception { - GeoPointValue.Builder builder = GeoPointValue.builder(CONTENT); - GeoPointValue value = builder.meaning(1).excludeFromIndexes(true).build(); + LatLngValue.Builder builder = LatLngValue.builder(CONTENT); + LatLngValue value = builder.meaning(1).excludeFromIndexes(true).build(); assertEquals(CONTENT, value.get()); assertEquals(1, value.meaning()); assertTrue(value.excludeFromIndexes()); diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java index b2ee24862688..a8cbcb294b39 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java @@ -45,7 +45,7 @@ public class SerializationTest { IncompleteKey.builder(KEY1, "v").ancestors(PathElement.of("p", 1)).build(); private static final Key KEY2 = Key.builder(KEY1, "v", 2).build(); private static final DateTime DATE_TIME1 = DateTime.now(); - private static final GeoPoint GEO_POINT = new GeoPoint(37.422035, -122.084124); + private static final LatLng LAT_LNG = new LatLng(37.422035, -122.084124); private static final Blob BLOB1 = Blob.copyFrom(UTF_8.encode("hello world")); private static final Cursor CURSOR1 = Cursor.copyFrom(new byte[] {1,2}); private static final Cursor CURSOR2 = Cursor.copyFrom(new byte[]{10}); @@ -91,7 +91,7 @@ public class SerializationTest { private static final BlobValue BLOB_VALUE = BlobValue.of(BLOB1); private static final RawValue RAW_VALUE = RawValue.of(com.google.datastore.v1beta3.Value.newBuilder().setMeaning(18).build()); - private static final GeoPointValue GEO_POINT_VALUE = GeoPointValue.of(GEO_POINT); + private static final LatLngValue LAT_LNG_VALUE = LatLngValue.of(LAT_LNG); private static final Entity ENTITY1 = Entity.builder(KEY1).build(); private static final Entity ENTITY2 = Entity.builder(KEY2).set("null", NullValue.of()).build(); @@ -130,7 +130,7 @@ public class SerializationTest { .put(ValueType.DATE_TIME, DATE_AND_TIME_VALUE) .put(ValueType.BLOB, BLOB_VALUE) .put(ValueType.RAW_VALUE, RAW_VALUE) - .put(ValueType.GEO_POINT, GEO_POINT_VALUE) + .put(ValueType.LAT_LNG, LAT_LNG_VALUE) .build(); @Test @@ -169,7 +169,7 @@ public void testValues() throws Exception { @Test public void testTypes() throws Exception { Serializable[] types = { KEY1, KEY2, INCOMPLETE_KEY1, INCOMPLETE_KEY2, ENTITY1, ENTITY2, - ENTITY3, EMBEDDED_ENTITY, PROJECTION_ENTITY, DATE_TIME1, BLOB1, GEO_POINT, CURSOR1, GQL1, + ENTITY3, EMBEDDED_ENTITY, PROJECTION_ENTITY, DATE_TIME1, BLOB1, LAT_LNG, CURSOR1, GQL1, GQL2, QUERY1, QUERY2, QUERY3}; for (Serializable obj : types) { Object copy = serializeAndDeserialize(obj); diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/ValueTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/ValueTest.java index 771b3c556a12..e433ca9a0666 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/ValueTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/ValueTest.java @@ -41,22 +41,24 @@ public class ValueTest { private static final NullValue NULL_VALUE = NullValue.of(); private static final StringValue STRING_VALUE = StringValue.of("hello"); private static final RawValue RAW_VALUE = RawValue.of(STRING_VALUE.toPb()); - private static final GeoPointValue GEO_POINT_VALUE = GeoPointValue.of(new GeoPoint(30.5, 40.5)); - private static final ImmutableMap TYPES = ImmutableMap.builder() - .put(ValueType.NULL, new Object[] {NullValue.class, NULL_VALUE.get()}) - .put(ValueType.KEY, new Object[] {KeyValue.class, KEY}) - .put(ValueType.BLOB, new Object[] {BlobValue.class, BLOB}) - .put(ValueType.BOOLEAN, new Object[] {BooleanValue.class, Boolean.TRUE}) - .put(ValueType.DATE_TIME, new Object[] {DateTimeValue.class, DATE_TIME}) - .put(ValueType.DOUBLE, new Object[] {DoubleValue.class, 1.25D}) - .put(ValueType.ENTITY, new Object[] {EntityValue.class, ENTITY}) - .put(ValueType.LIST, - new Object[] {ListValue.class, ImmutableList.of(NULL_VALUE, STRING_VALUE, RAW_VALUE)}) - .put(ValueType.LONG, new Object[] {LongValue.class, 123L}) - .put(ValueType.RAW_VALUE, new Object[] {RawValue.class, RAW_VALUE.get()}) - .put(ValueType.GEO_POINT, new Object[] {GeoPointValue.class, GEO_POINT_VALUE.get()}) - .put(ValueType.STRING, new Object[] {StringValue.class, STRING_VALUE.get()}) - .build(); + private static final LatLngValue LAT_LNG_VALUE = + LatLngValue.of(new LatLng(37.422035, -122.084124)); + private static final ImmutableMap TYPES = + ImmutableMap.builder() + .put(ValueType.NULL, new Object[] {NullValue.class, NULL_VALUE.get()}) + .put(ValueType.KEY, new Object[] {KeyValue.class, KEY}) + .put(ValueType.BLOB, new Object[] {BlobValue.class, BLOB}) + .put(ValueType.BOOLEAN, new Object[] {BooleanValue.class, Boolean.TRUE}) + .put(ValueType.DATE_TIME, new Object[] {DateTimeValue.class, DATE_TIME}) + .put(ValueType.DOUBLE, new Object[] {DoubleValue.class, 1.25D}) + .put(ValueType.ENTITY, new Object[] {EntityValue.class, ENTITY}) + .put(ValueType.LIST, new Object[] { + ListValue.class, ImmutableList.of(NULL_VALUE, STRING_VALUE, RAW_VALUE)}) + .put(ValueType.LONG, new Object[] {LongValue.class, 123L}) + .put(ValueType.RAW_VALUE, new Object[] {RawValue.class, RAW_VALUE.get()}) + .put(ValueType.LAT_LNG, new Object[] {LatLngValue.class, LAT_LNG_VALUE.get()}) + .put(ValueType.STRING, new Object[] {StringValue.class, STRING_VALUE.get()}) + .build(); private ImmutableMap> typeToValue; From 50a5a66b851831ce50d17b04aea16f4c54c71f65 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 19 Oct 2015 15:50:24 -0700 Subject: [PATCH 19/60] Query continuation implemented for GqlQuery + tests --- .../com/google/gcloud/datastore/GqlQuery.java | 8 +- .../com/google/gcloud/datastore/Query.java | 2 +- .../gcloud/datastore/QueryResultsImpl.java | 18 ++- .../gcloud/datastore/StructuredQuery.java | 29 +++-- .../gcloud/datastore/DatastoreTest.java | 113 +++++++++++++++++- 5 files changed, 142 insertions(+), 28 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java index 7fcb562fea89..e6f713c76a1c 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java @@ -379,9 +379,11 @@ protected void populatePb(com.google.datastore.v1beta3.RunQueryRequest.Builder r } @Override - protected GqlQuery nextQuery(com.google.datastore.v1beta3.QueryResultBatch responsePb) { - // See issue #17 - throw new UnsupportedOperationException("paging for this query is not implemented yet"); + protected StructuredQuery nextQuery(com.google.datastore.v1beta3.RunQueryResponse responsePb) { + return new StructuredQuery.Builder<>(type()) + .mergeFrom(responsePb.getQuery()) + .prepareNext(responsePb.getBatch()) + .build(); } @Override diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java index c45efde3e30d..cd7d1015deda 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java @@ -192,7 +192,7 @@ protected abstract Object fromPb(ResultType resultType, String namespace, byt protected abstract void populatePb( com.google.datastore.v1beta3.RunQueryRequest.Builder requestPb); - protected abstract Query nextQuery(com.google.datastore.v1beta3.QueryResultBatch responsePb); + protected abstract Query nextQuery(com.google.datastore.v1beta3.RunQueryResponse responsePb); /** * Returns a new {@link GqlQuery} builder. diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java index bb2b65fcc646..10f3eeda58ac 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java @@ -33,7 +33,7 @@ class QueryResultsImpl extends AbstractIterator implements QueryResults private final ResultType queryResultType; private Query query; private ResultType actualResultType; - private com.google.datastore.v1beta3.QueryResultBatch queryResultBatchPb; + private com.google.datastore.v1beta3.RunQueryResponse runQueryResponsePb; private com.google.datastore.v1beta3.Query mostRecentQueryPb; private boolean lastBatch; private Iterator entityResultPbIter; @@ -56,8 +56,8 @@ class QueryResultsImpl extends AbstractIterator implements QueryResults } partitionIdPb = pbBuilder.build(); sendRequest(); - if (queryResultBatchPb.getSkippedResults() > 0) { - cursor = queryResultBatchPb.getSkippedCursor(); + if (runQueryResponsePb.getBatch().getSkippedResults() > 0) { + cursor = runQueryResponsePb.getBatch().getSkippedCursor(); } else { cursor = mostRecentQueryPb.getStartCursor(); } @@ -71,16 +71,14 @@ private void sendRequest() { } requestPb.setPartitionId(partitionIdPb); query.populatePb(requestPb); - com.google.datastore.v1beta3.RunQueryResponse runQueryResponsePb = - datastore.runQuery(requestPb.build()); - queryResultBatchPb = runQueryResponsePb.getBatch(); + runQueryResponsePb = datastore.runQuery(requestPb.build()); mostRecentQueryPb = runQueryResponsePb.getQuery(); if (mostRecentQueryPb == null) { mostRecentQueryPb = requestPb.getQuery(); } - lastBatch = queryResultBatchPb.getMoreResults() != MoreResultsType.NOT_FINISHED; - entityResultPbIter = queryResultBatchPb.getEntityResultsList().iterator(); - actualResultType = ResultType.fromPb(queryResultBatchPb.getEntityResultType()); + lastBatch = runQueryResponsePb.getBatch().getMoreResults() != MoreResultsType.NOT_FINISHED; + entityResultPbIter = runQueryResponsePb.getBatch().getEntityResultsList().iterator(); + actualResultType = ResultType.fromPb(runQueryResponsePb.getBatch().getEntityResultType()); if (Objects.equals(queryResultType, ResultType.PROJECTION_ENTITY)) { // projection entity can represent all type of results actualResultType = ResultType.PROJECTION_ENTITY; @@ -92,7 +90,7 @@ private void sendRequest() { @Override protected T computeNext() { while (!entityResultPbIter.hasNext() && !lastBatch) { - query = query.nextQuery(queryResultBatchPb); + query = query.nextQuery(runQueryResponsePb); sendRequest(); } if (!entityResultPbIter.hasNext()) { diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java index 1b82991cb8c8..dcf4cef4721f 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java @@ -669,9 +669,23 @@ B mergeFrom(com.google.datastore.v1beta3.Query queryPb) { return self(); } + B prepareNext(com.google.datastore.v1beta3.QueryResultBatch queryResultBatchPb) { + startCursor(new Cursor(queryResultBatchPb.getEndCursor())); + if (offset > 0 && queryResultBatchPb.getSkippedResults() < offset) { + offset(offset - queryResultBatchPb.getSkippedResults()); + } else { + offset(0); + if (limit != null) { + limit(limit - queryResultBatchPb.getEntityResultsCount()); + } + } + return self(); + } + public StructuredQuery build() { return new StructuredQuery<>(this); } + } static final class Builder extends BaseBuilder> { @@ -844,19 +858,8 @@ protected void populatePb(com.google.datastore.v1beta3.RunQueryRequest.Builder r } @Override - protected StructuredQuery nextQuery(com.google.datastore.v1beta3.QueryResultBatch responsePb) { - Builder builder = new Builder<>(type()); - builder.mergeFrom(toPb()); - builder.startCursor(new Cursor(responsePb.getEndCursor())); - if (offset > 0 && responsePb.getSkippedResults() < offset) { - builder.offset(offset - responsePb.getSkippedResults()); - } else { - builder.offset(0); - if (limit != null) { - builder.limit(limit - responsePb.getEntityResultsCount()); - } - } - return builder.build(); + protected StructuredQuery nextQuery(com.google.datastore.v1beta3.RunQueryResponse responsePb) { + return new Builder<>(type()).mergeFrom(toPb()).prepareNext(responsePb.getBatch()).build(); } @Override diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index f0fbd4ab96c0..6bf907422a0f 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -31,6 +31,7 @@ import com.google.gcloud.datastore.StructuredQuery.OrderBy; import com.google.gcloud.datastore.StructuredQuery.PropertyFilter; import com.google.gcloud.spi.DatastoreRpc; +import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException; import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException.Reason; import com.google.gcloud.spi.DatastoreRpcFactory; @@ -43,6 +44,7 @@ import org.junit.runners.JUnit4; import java.io.IOException; +import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -403,6 +405,42 @@ public void testRunGqlQueryWithCasting() { assertFalse(results3.hasNext()); } + @Test + public void testGqlQueryPagination() { + DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class); + DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class); + EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) + .andReturn(rpcMock); + List responses = + buildResponsesForQueryPagination(); + for (int i = 0; i < responses.size(); i++) { + try { + EasyMock + .expect(rpcMock.runQuery( + EasyMock.anyObject(com.google.datastore.v1beta3.RunQueryRequest.class))) + .andReturn(responses.get(i)); + } catch (DatastoreRpcException e) { + fail("Unexpected DatastoreRpcException"); + } + } + EasyMock.replay(rpcFactoryMock, rpcMock); + DatastoreOptions options = + this.options.toBuilder() + .retryParams(RetryParams.getDefaultInstance()) + .serviceRpcFactory(rpcFactoryMock) + .build(); + Datastore mockDatastore = DatastoreFactory.instance().get(options); + QueryResults results = + mockDatastore.run(Query.gqlQueryBuilder(ResultType.KEY, "select __key__ from *").build()); + int count = 0; + while (results.hasNext()) { + count += 1; + results.next(); + } + assertEquals(count, responses.size()); + EasyMock.verify(rpcFactoryMock, rpcMock); + } + @Test public void testRunStructuredQuery() { Query query = @@ -445,7 +483,80 @@ public void testRunStructuredQuery() { assertEquals(20, entity.getLong("age")); assertEquals(1, entity.properties().size()); assertFalse(results4.hasNext()); - // TODO(ozarov): construct a test to verify nextQuery/pagination + } + + @Test + public void testStructuredQueryPagination() { + DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class); + DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class); + EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) + .andReturn(rpcMock); + List responses = + buildResponsesForQueryPagination(); + for (int i = 0; i < responses.size(); i++) { + try { + EasyMock + .expect(rpcMock.runQuery( + EasyMock.anyObject(com.google.datastore.v1beta3.RunQueryRequest.class))) + .andReturn(responses.get(i)); + } catch (DatastoreRpcException e) { + fail("Unexpected DatastoreRpcException"); + } + } + EasyMock.replay(rpcFactoryMock, rpcMock); + DatastoreOptions options = + this.options.toBuilder() + .retryParams(RetryParams.getDefaultInstance()) + .serviceRpcFactory(rpcFactoryMock) + .build(); + Datastore mockDatastore = DatastoreFactory.instance().get(options); + QueryResults results = mockDatastore.run(Query.keyQueryBuilder().build()); + int count = 0; + while (results.hasNext()) { + count += 1; + results.next(); + } + assertEquals(count, responses.size()); + EasyMock.verify(rpcFactoryMock, rpcMock); + } + + private List buildResponsesForQueryPagination() { + List responses = new ArrayList<>(); + Query query = Query.keyQueryBuilder().build(); + com.google.datastore.v1beta3.RunQueryRequest.Builder requestPb = + com.google.datastore.v1beta3.RunQueryRequest.newBuilder(); + query.populatePb(requestPb); + com.google.datastore.v1beta3.QueryResultBatch queryResultBatchPb = + com.google.datastore.v1beta3.RunQueryResponse.newBuilder() + .mergeFrom(((DatastoreImpl) datastore).runQuery(requestPb.build())) + .getBatch(); + com.google.datastore.v1beta3.QueryResultBatch queryResultBatchPb1 = + com.google.datastore.v1beta3.QueryResultBatch.newBuilder() + .mergeFrom(queryResultBatchPb) + .setMoreResults( + com.google.datastore.v1beta3.QueryResultBatch.MoreResultsType.NOT_FINISHED) + .clearEntityResults() + .addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(0, 1)) + .setEndCursor(queryResultBatchPb.getEntityResultsList().get(0).getCursor()) + .build(); + responses.add( + com.google.datastore.v1beta3.RunQueryResponse.newBuilder() + .setBatch(queryResultBatchPb1) + .build()); + com.google.datastore.v1beta3.QueryResultBatch queryResultBatchPb2 = + com.google.datastore.v1beta3.QueryResultBatch.newBuilder() + .mergeFrom(queryResultBatchPb) + .setMoreResults( + com.google.datastore.v1beta3.QueryResultBatch.MoreResultsType.NO_MORE_RESULTS) + .clearEntityResults() + .addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(1, 2)) + .setEndCursor(queryResultBatchPb.getEntityResultsList().get(1).getCursor()) + .build(); + responses.add( + com.google.datastore.v1beta3.RunQueryResponse.newBuilder() + .setBatch(queryResultBatchPb2) + .build()); + return responses; } @Test From b270c64f96c5cad1e07c717aab1120b10dd7025d Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Wed, 21 Oct 2015 13:08:18 -0700 Subject: [PATCH 20/60] Set namespace in Gql's nextQuery and add another batch to tests --- .../com/google/gcloud/datastore/GqlQuery.java | 1 + .../gcloud/datastore/DatastoreTest.java | 38 +++++++++++-------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java index e6f713c76a1c..ecb0aef1e701 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java @@ -381,6 +381,7 @@ protected void populatePb(com.google.datastore.v1beta3.RunQueryRequest.Builder r @Override protected StructuredQuery nextQuery(com.google.datastore.v1beta3.RunQueryResponse responsePb) { return new StructuredQuery.Builder<>(type()) + .namespace(namespace()) .mergeFrom(responsePb.getQuery()) .prepareNext(responsePb.getBatch()) .build(); diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index 6bf907422a0f..6ea4e789ee34 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -406,7 +406,7 @@ public void testRunGqlQueryWithCasting() { } @Test - public void testGqlQueryPagination() { + public void testGqlQueryPagination() throws DatastoreRpcException { DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class); DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class); EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) @@ -414,14 +414,10 @@ public void testGqlQueryPagination() { List responses = buildResponsesForQueryPagination(); for (int i = 0; i < responses.size(); i++) { - try { EasyMock .expect(rpcMock.runQuery( EasyMock.anyObject(com.google.datastore.v1beta3.RunQueryRequest.class))) .andReturn(responses.get(i)); - } catch (DatastoreRpcException e) { - fail("Unexpected DatastoreRpcException"); - } } EasyMock.replay(rpcFactoryMock, rpcMock); DatastoreOptions options = @@ -437,7 +433,7 @@ public void testGqlQueryPagination() { count += 1; results.next(); } - assertEquals(count, responses.size()); + assertEquals(count, 5); EasyMock.verify(rpcFactoryMock, rpcMock); } @@ -486,7 +482,7 @@ public void testRunStructuredQuery() { } @Test - public void testStructuredQueryPagination() { + public void testStructuredQueryPagination() throws DatastoreRpcException { DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class); DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class); EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) @@ -494,14 +490,10 @@ public void testStructuredQueryPagination() { List responses = buildResponsesForQueryPagination(); for (int i = 0; i < responses.size(); i++) { - try { EasyMock .expect(rpcMock.runQuery( EasyMock.anyObject(com.google.datastore.v1beta3.RunQueryRequest.class))) .andReturn(responses.get(i)); - } catch (DatastoreRpcException e) { - fail("Unexpected DatastoreRpcException"); - } } EasyMock.replay(rpcFactoryMock, rpcMock); DatastoreOptions options = @@ -516,11 +508,14 @@ public void testStructuredQueryPagination() { count += 1; results.next(); } - assertEquals(count, responses.size()); + assertEquals(count, 5); EasyMock.verify(rpcFactoryMock, rpcMock); } private List buildResponsesForQueryPagination() { + Entity entity4 = Entity.builder(KEY4).set("value", StringValue.of("value")).build(); + Entity entity5 = Entity.builder(KEY5).set("value", "value").build(); + datastore.add(ENTITY3, entity4, entity5); List responses = new ArrayList<>(); Query query = Query.keyQueryBuilder().build(); com.google.datastore.v1beta3.RunQueryRequest.Builder requestPb = @@ -547,15 +542,28 @@ private List buildResponsesForQue com.google.datastore.v1beta3.QueryResultBatch.newBuilder() .mergeFrom(queryResultBatchPb) .setMoreResults( - com.google.datastore.v1beta3.QueryResultBatch.MoreResultsType.NO_MORE_RESULTS) + com.google.datastore.v1beta3.QueryResultBatch.MoreResultsType.NOT_FINISHED) .clearEntityResults() - .addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(1, 2)) - .setEndCursor(queryResultBatchPb.getEntityResultsList().get(1).getCursor()) + .addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(1, 3)) + .setEndCursor(queryResultBatchPb.getEntityResultsList().get(2).getCursor()) .build(); responses.add( com.google.datastore.v1beta3.RunQueryResponse.newBuilder() .setBatch(queryResultBatchPb2) .build()); + com.google.datastore.v1beta3.QueryResultBatch queryResultBatchPb3 = + com.google.datastore.v1beta3.QueryResultBatch.newBuilder() + .mergeFrom(queryResultBatchPb) + .setMoreResults( + com.google.datastore.v1beta3.QueryResultBatch.MoreResultsType.NO_MORE_RESULTS) + .clearEntityResults() + .addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(3, 5)) + .setEndCursor(queryResultBatchPb.getEntityResultsList().get(4).getCursor()) + .build(); + responses.add( + com.google.datastore.v1beta3.RunQueryResponse.newBuilder() + .setBatch(queryResultBatchPb3) + .build()); return responses; } From 6ffe9273596c10d38cef62db73a77b38ac8b8c3c Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Wed, 21 Oct 2015 15:47:34 -0700 Subject: [PATCH 21/60] remove changes to structured query's builder and instead use fromPb in GqlQuery's nextQuery --- .../com/google/gcloud/datastore/GqlQuery.java | 9 ++--- .../gcloud/datastore/StructuredQuery.java | 35 +++++++++---------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java index ecb0aef1e701..6c746c7924d7 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java @@ -379,12 +379,9 @@ protected void populatePb(com.google.datastore.v1beta3.RunQueryRequest.Builder r } @Override - protected StructuredQuery nextQuery(com.google.datastore.v1beta3.RunQueryResponse responsePb) { - return new StructuredQuery.Builder<>(type()) - .namespace(namespace()) - .mergeFrom(responsePb.getQuery()) - .prepareNext(responsePb.getBatch()) - .build(); + protected Query nextQuery(com.google.datastore.v1beta3.RunQueryResponse responsePb) { + return StructuredQuery.fromPb(type(), namespace(), responsePb.getQuery()) + .nextQuery(responsePb); } @Override diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java index dcf4cef4721f..e519ce4fc3ff 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java @@ -669,23 +669,9 @@ B mergeFrom(com.google.datastore.v1beta3.Query queryPb) { return self(); } - B prepareNext(com.google.datastore.v1beta3.QueryResultBatch queryResultBatchPb) { - startCursor(new Cursor(queryResultBatchPb.getEndCursor())); - if (offset > 0 && queryResultBatchPb.getSkippedResults() < offset) { - offset(offset - queryResultBatchPb.getSkippedResults()); - } else { - offset(0); - if (limit != null) { - limit(limit - queryResultBatchPb.getEntityResultsCount()); - } - } - return self(); - } - public StructuredQuery build() { return new StructuredQuery<>(this); } - } static final class Builder extends BaseBuilder> { @@ -858,8 +844,19 @@ protected void populatePb(com.google.datastore.v1beta3.RunQueryRequest.Builder r } @Override - protected StructuredQuery nextQuery(com.google.datastore.v1beta3.RunQueryResponse responsePb) { - return new Builder<>(type()).mergeFrom(toPb()).prepareNext(responsePb.getBatch()).build(); + protected Query nextQuery(com.google.datastore.v1beta3.RunQueryResponse responsePb) { + Builder builder = new Builder<>(type()); + builder.mergeFrom(toPb()); + builder.startCursor(new Cursor(responsePb.getBatch().getEndCursor())); + if (offset > 0 && responsePb.getBatch().getSkippedResults() < offset) { + builder.offset(offset - responsePb.getBatch().getSkippedResults()); + } else { + builder.offset(0); + if (limit != null) { + builder.limit(limit - responsePb.getBatch().getEntityResultsCount()); + } + } + return builder.build(); } @Override @@ -907,7 +904,9 @@ protected Object fromPb(ResultType resultType, String namespace, byte[] bytes return fromPb(resultType, namespace, com.google.datastore.v1beta3.Query.parseFrom(bytesPb)); } - private static StructuredQuery fromPb(ResultType resultType, String namespace, + @SuppressWarnings("unchecked") + static StructuredQuery fromPb( + ResultType resultType, String namespace, com.google.datastore.v1beta3.Query queryPb) { BaseBuilder builder; if (resultType.equals(ResultType.ENTITY)) { @@ -917,6 +916,6 @@ private static StructuredQuery fromPb(ResultType resultType, String namesp } else { builder = new ProjectionEntityQueryBuilder(); } - return builder.namespace(namespace).mergeFrom(queryPb).build(); + return (StructuredQuery) builder.namespace(namespace).mergeFrom(queryPb).build(); } } From 3d7ea2c955bb4886ffc1954badd98ce0eff9a8a4 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Fri, 15 Jan 2016 15:26:57 -0800 Subject: [PATCH 22/60] update afterCursor javadoc --- .../google/gcloud/datastore/QueryResults.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResults.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResults.java index 5b4f71da3963..110add0bbbe4 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResults.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResults.java @@ -35,7 +35,21 @@ public interface QueryResults extends Iterator { Class resultClass(); /** - * Returns the Cursor for point after the value returned in the last {@link #next} call. + * Returns the Cursor for the point after the value returned in the last {@link #next} call. This + * cursor can be used to issue subsequent queries (with the same constraints) that may return + * additional results. + * + *

A simple use case: + *

 {@code
+   * Query query = Query.entityQueryBuilder()
+   *     .kind("Person")
+   *     .filter(PropertyFilter.eq("favoriteFood", "pizza"))
+   *     .build();
+   * QueryResults results = datastore.run(query);
+   * // Consume some results (using results.next()) and do any other actions as necessary.
+   * query = query.toBuilder().startCursor(results.cursorAfter()).build();
+   * results = datastore.run(query); // now we will iterate over all entities not yet consumed
+   * 
*/ Cursor cursorAfter(); } From 71d91aa5b6f603e02285207662e5faae5b1c8a7f Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Thu, 28 Jan 2016 18:22:03 -0800 Subject: [PATCH 23/60] Fixed missed merge conflict --- .../java/com/google/gcloud/datastore/QueryResults.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResults.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResults.java index cd851f690ca5..a6e5971936dd 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResults.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResults.java @@ -36,7 +36,6 @@ public interface QueryResults extends Iterator { Class resultClass(); /** -<<<<<<< HEAD * Returns the Cursor for the point after the value returned in the last {@link #next} call. This * cursor can be used to issue subsequent queries (with the same constraints) that may return * additional results. @@ -51,11 +50,7 @@ public interface QueryResults extends Iterator { * // Consume some results (using results.next()) and do any other actions as necessary. * query = query.toBuilder().startCursor(results.cursorAfter()).build(); * results = datastore.run(query); // now we will iterate over all entities not yet consumed - * -======= - * Returns the Cursor for the point after the value returned in the last {@link #next} call. - * Currently, {@code cursorAfter} returns null in all cases but the last result. ->>>>>>> upstream/master + * } */ Cursor cursorAfter(); } From d27d567606f198822c5ec2b4a5e8c592e263abec Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 1 Feb 2016 17:35:06 -0800 Subject: [PATCH 24/60] Fix ServiceOptions merge bug, remove unnecessary protected modifier from DateTime, set cursorAfter to endCursor where appropriate, and fix error codes for retries. --- .../com/google/gcloud/ServiceOptions.java | 4 +--- .../gcloud/datastore/DatastoreException.java | 4 +--- .../com/google/gcloud/datastore/DateTime.java | 4 ++-- .../gcloud/datastore/QueryResultsImpl.java | 1 + .../datastore/DatastoreExceptionTest.java | 20 +++++++++---------- .../gcloud/datastore/DatastoreTest.java | 2 +- 6 files changed, 16 insertions(+), 19 deletions(-) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java index 34280dbf92e5..31e543809464 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java @@ -18,7 +18,6 @@ import static com.google.common.base.MoreObjects.firstNonNull; import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; import static java.nio.charset.StandardCharsets.UTF_8; import com.google.api.client.extensions.appengine.http.UrlFetchTransport; @@ -229,8 +228,7 @@ public B clock(Clock clock) { * @return the builder */ public B projectId(String projectId) { - this.projectId = - checkNotNull(projectId, "Project ID cannot be set to null. Leave unset for default."); + this.projectId = projectId; return self(); } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreException.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreException.java index ecad69ac635b..a940fe573f93 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreException.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreException.java @@ -34,9 +34,7 @@ public class DatastoreException extends BaseServiceException { // see https://cloud.google.com/datastore/docs/concepts/errors#Error_Codes" private static final Set RETRYABLE_ERRORS = ImmutableSet.of( - new Error(409, "ABORTED"), - new Error(403, "DEADLINE_EXCEEDED"), - new Error(503, "UNAVAILABLE")); + new Error(10, "ABORTED"), new Error(4, "DEADLINE_EXCEEDED"), new Error(14, "UNAVAILABLE")); private static final long serialVersionUID = 2663750991205874435L; public DatastoreException(int code, String message, String reason, Throwable cause) { diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DateTime.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DateTime.java index 45347ed41dcb..5e8664395802 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DateTime.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DateTime.java @@ -106,11 +106,11 @@ Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException { com.google.protobuf.Timestamp.parseFrom(bytesPb))); } - protected static long timestampPbToMicroseconds(com.google.protobuf.Timestamp timestampPb) { + static long timestampPbToMicroseconds(com.google.protobuf.Timestamp timestampPb) { return timestampPb.getSeconds() * 1000000 + timestampPb.getNanos() / 1000; } - protected static com.google.protobuf.Timestamp microsecondsToTimestampPb(long microseconds) { + static com.google.protobuf.Timestamp microsecondsToTimestampPb(long microseconds) { long seconds = microseconds / 1000000; int nanos = (int) (microseconds % 1000000) * 1000; return com.google.protobuf.Timestamp.newBuilder().setSeconds(seconds).setNanos(nanos).build(); diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java index 25dde18a1a1b..ec3a652c6131 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java @@ -93,6 +93,7 @@ protected T computeNext() { sendRequest(); } if (!entityResultPbIter.hasNext()) { + cursor = runQueryResponsePb.getBatch().getEndCursor(); return endOfData(); } com.google.datastore.v1beta3.EntityResult entityResultPb = entityResultPbIter.next(); diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java index 4d62224172f9..301a863476b6 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java @@ -38,29 +38,29 @@ public class DatastoreExceptionTest { @Test public void testDatastoreException() throws Exception { - DatastoreException exception = new DatastoreException(409, "message", "ABORTED"); - assertEquals(409, exception.code()); + DatastoreException exception = new DatastoreException(10, "message", "ABORTED"); + assertEquals(10, exception.code()); assertEquals("ABORTED", exception.reason()); assertEquals("message", exception.getMessage()); assertTrue(exception.retryable()); assertTrue(exception.idempotent()); - exception = new DatastoreException(403, "message", "DEADLINE_EXCEEDED"); - assertEquals(403, exception.code()); + exception = new DatastoreException(4, "message", "DEADLINE_EXCEEDED"); + assertEquals(4, exception.code()); assertEquals("DEADLINE_EXCEEDED", exception.reason()); assertEquals("message", exception.getMessage()); assertTrue(exception.retryable()); assertTrue(exception.idempotent()); - exception = new DatastoreException(503, "message", "UNAVAILABLE"); - assertEquals(503, exception.code()); + exception = new DatastoreException(14, "message", "UNAVAILABLE"); + assertEquals(14, exception.code()); assertEquals("UNAVAILABLE", exception.reason()); assertEquals("message", exception.getMessage()); assertTrue(exception.retryable()); assertTrue(exception.idempotent()); - exception = new DatastoreException(500, "message", "INTERNAL"); - assertEquals(500, exception.code()); + exception = new DatastoreException(2, "message", "INTERNAL"); + assertEquals(2, exception.code()); assertEquals("INTERNAL", exception.reason()); assertEquals("message", exception.getMessage()); assertFalse(exception.retryable()); @@ -77,14 +77,14 @@ public void testDatastoreException() throws Exception { @Test public void testTranslateAndThrow() throws Exception { - DatastoreException cause = new DatastoreException(503, "message", "UNAVAILABLE"); + DatastoreException cause = new DatastoreException(14, "message", "UNAVAILABLE"); RetryHelper.RetryHelperException exceptionMock = createMock(RetryHelper.RetryHelperException.class); expect(exceptionMock.getCause()).andReturn(cause).times(2); replay(exceptionMock); try { DatastoreException.translateAndThrow(exceptionMock); } catch (BaseServiceException ex) { - assertEquals(503, ex.code()); + assertEquals(14, ex.code()); assertEquals("message", ex.getMessage()); assertTrue(ex.retryable()); assertTrue(ex.idempotent()); diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index a39952c448eb..002e5f6df04f 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -986,7 +986,7 @@ public void testRetryableException() throws Exception { EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) .andReturn(rpcMock); EasyMock.expect(rpcMock.lookup(requestPb)) - .andThrow(new DatastoreException(503, "UNAVAILABLE", "UNAVAILABLE", null)) + .andThrow(new DatastoreException(14, "UNAVAILABLE", "UNAVAILABLE", null)) .andReturn(responsePb); EasyMock.replay(rpcFactoryMock, rpcMock); DatastoreOptions options = this.options.toBuilder() From 7e498c1ef6ef00a29ba17714e732bf58e6cace83 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 29 Feb 2016 18:20:11 -0800 Subject: [PATCH 25/60] Remove userinfo scope from DatastoreOptions --- .../java/com/google/gcloud/datastore/DatastoreOptions.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java index 112d0e8d2602..d47aa5fa2915 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java @@ -35,8 +35,7 @@ public class DatastoreOptions extends ServiceOptions SCOPES = ImmutableSet.of(DATASTORE_SCOPE, USERINFO_SCOPE); + private static final Set SCOPES = ImmutableSet.of(DATASTORE_SCOPE); private final String namespace; private final boolean normalizeDataset; From c95e5e7dbecaeb613e77385f62258f9c43b32af2 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Thu, 18 Feb 2016 08:10:00 -0800 Subject: [PATCH 26/60] Allow eventual consistency for Datastore reads --- gcloud-java-datastore/pom.xml | 12 ---- .../google/gcloud/datastore/Datastore.java | 39 ++++++++++- .../gcloud/datastore/DatastoreHelper.java | 31 ++++++-- .../gcloud/datastore/DatastoreImpl.java | 39 +++++++++++ .../gcloud/datastore/DatastoreReader.java | 10 +-- .../google/gcloud/datastore/ReadOption.java | 66 +++++++++++++++++ .../gcloud/datastore/TransactionImpl.java | 8 +-- .../gcloud/datastore/DatastoreHelperTest.java | 65 ++++++++++++++--- .../gcloud/datastore/DatastoreTest.java | 70 +++++++++++++++++++ 9 files changed, 303 insertions(+), 37 deletions(-) create mode 100644 gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ReadOption.java diff --git a/gcloud-java-datastore/pom.xml b/gcloud-java-datastore/pom.xml index b33f66a19682..ee633fd57883 100644 --- a/gcloud-java-datastore/pom.xml +++ b/gcloud-java-datastore/pom.xml @@ -42,18 +42,6 @@ datastore-v1beta3-proto-client 0.0.1-SNAPSHOT - - com.google.apis - google-api-services-datastore-protobuf - v1beta2-rev1-2.1.2 - compile - - - com.google.api-client - google-api-client - - - junit junit diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Datastore.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Datastore.java index b49db1cacdfe..00b914a36678 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Datastore.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Datastore.java @@ -18,6 +18,7 @@ import com.google.gcloud.Service; +import java.util.Iterator; import java.util.List; /** @@ -32,7 +33,6 @@ public interface Datastore extends Service, DatastoreReaderWri */ Transaction newTransaction(); - /** * A callback for running with a transactional * {@link com.google.gcloud.datastore.DatastoreReaderWriter}. @@ -45,7 +45,6 @@ interface TransactionCallable { T run(DatastoreReaderWriter readerWriter) throws Exception; } - /** * Invokes the callback's {@link Datastore.TransactionCallable#run} method with a * {@link DatastoreReaderWriter} that is associated with a new transaction. @@ -105,4 +104,40 @@ interface TransactionCallable { * Returns a new KeyFactory for this service */ KeyFactory newKeyFactory(); + + /** + * Returns an {@link Entity} for the given {@link Key} or {@code null} if it doesn't exist. + * {@link ReadOption}s can be specified if desired. + * + * @throws DatastoreException upon failure + */ + Entity get(Key key, ReadOption... options); + + /** + * Returns an {@link Entity} for each given {@link Key} that exists in the Datastore. + * The order of the result is unspecified. + * Results are loaded lazily, so it is possible to get a {@code DatastoreException} + * from the returned {@code Iterator}'s {@link Iterator#hasNext hasNext} or + * {@link Iterator#next next} methods. {@link ReadOption}s can be specified if desired. + * + * @throws DatastoreException upon failure + * @see #get(Key) + */ + Iterator get(Iterable keys, ReadOption... options); + + /** + * Returns a list with a value for each given key (ordered by input). + * {@code null} values are returned for nonexistent keys. + * When possible prefer using {@link #get(Key...)} to avoid eagerly loading the results. + * {@link ReadOption}s can be specified if desired. + */ + List fetch(Iterable keys, ReadOption... options); + + /** + * Submits a {@link Query} and returns its result. + * {@link ReadOption}s can be specified if desired. + * + * @throws DatastoreException upon failure + */ + QueryResults run(Query query, ReadOption... options); } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreHelper.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreHelper.java index 5ea0f755d2f8..2873e2f8aafa 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreHelper.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreHelper.java @@ -20,6 +20,8 @@ import com.google.common.collect.Maps; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -33,13 +35,16 @@ class DatastoreHelper { private DatastoreHelper() { } - static Key allocateId(Datastore service, IncompleteKey key) { return service.allocateId(new IncompleteKey[]{key}).get(0); } - static Entity get(DatastoreReader reader, Key key) { - return Iterators.getNext(reader.get(new Key[]{key}), null); + static Entity get(Transaction reader, Key key) { + return Iterators.getNext(reader.get(new Key[] {key}), null); + } + + static Entity get(Datastore reader, Key key, ReadOption... options) { + return Iterators.getNext(reader.get(Collections.singletonList(key), options), null); } static Entity add(DatastoreWriter writer, FullEntity entity) { @@ -52,10 +57,24 @@ static KeyFactory newKeyFactory(DatastoreOptions options) { /** * Returns a list with a value for each given key (ordered by input). - * A {@code null} would be returned for non-existing keys. + * {@code null} values are returned for nonexistent keys. */ - static List fetch(DatastoreReader reader, Key... keys) { + static List fetch(Transaction reader, Key... keys) { Iterator entities = reader.get(keys); + return compileEntities(keys, entities); + } + + /** + * Returns a list with a value for each given key (ordered by input). + * {@code null} values are returned for nonexistent keys. + */ + static List fetch(Datastore reader, Key[] keys, ReadOption... options) { + Iterator entities; + entities = reader.get(Arrays.asList(keys), options); + return compileEntities(keys, entities); + } + + private static List compileEntities(Key[] keys, Iterator entities) { Map map = Maps.newHashMapWithExpectedSize(keys.length); while (entities.hasNext()) { Entity entity = entities.next(); @@ -63,7 +82,7 @@ static List fetch(DatastoreReader reader, Key... keys) { } List list = new ArrayList<>(keys.length); for (Key key : keys) { - // this will include nulls for non-existing keys + // this will include nulls for nonexistent keys list.add(map.get(key)); } return list; diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java index df4e22149fd5..73d25ab140d7 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java @@ -20,11 +20,14 @@ import com.google.common.base.Preconditions; import com.google.common.collect.AbstractIterator; import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; import com.google.common.collect.Sets; +import com.google.datastore.v1beta3.ReadOptions.ReadConsistency; import com.google.gcloud.BaseService; import com.google.gcloud.RetryHelper; import com.google.gcloud.RetryHelper.RetryHelperException; import com.google.gcloud.RetryParams; +import com.google.gcloud.datastore.ReadOption.EventualConsistency; import com.google.gcloud.spi.DatastoreRpc; import com.google.protobuf.ByteString; @@ -70,6 +73,11 @@ public QueryResults run(Query query) { return run(null, query); } + @Override + public QueryResults run(Query query, ReadOption... options) { + return run(toReadOptionsPb(options), query); + } + QueryResults run(com.google.datastore.v1beta3.ReadOptions readOptionsPb, Query query) { return new QueryResultsImpl<>(this, readOptionsPb, query); } @@ -185,16 +193,47 @@ public Entity get(Key key) { return DatastoreHelper.get(this, key); } + @Override + public Entity get(Key key, ReadOption... options) { + return DatastoreHelper.get(this, key, options); + } + @Override public Iterator get(Key... keys) { return get(null, keys); } + @Override + public Iterator get(Iterable keys, ReadOption... options) { + return get(toReadOptionsPb(options), Iterables.toArray(keys, Key.class)); + } + + private static com.google.datastore.v1beta3.ReadOptions toReadOptionsPb(ReadOption... options) { + com.google.datastore.v1beta3.ReadOptions readOptionsPb = null; + if (options != null) { + Map, ReadOption> optionsMap = ReadOption.asImmutableMap(options); + EventualConsistency eventualConsistency = + (EventualConsistency) optionsMap.get(EventualConsistency.class); + if (eventualConsistency != null) { + readOptionsPb = + com.google.datastore.v1beta3.ReadOptions.newBuilder() + .setReadConsistency(ReadConsistency.EVENTUAL) + .build(); + } + } + return readOptionsPb; + } + @Override public List fetch(Key... keys) { return DatastoreHelper.fetch(this, keys); } + @Override + public List fetch(Iterable keys, ReadOption... options) { + return DatastoreHelper.fetch(this, Iterables.toArray(keys, Key.class), options); + } + Iterator get(com.google.datastore.v1beta3.ReadOptions readOptionsPb, final Key... keys) { if (keys.length == 0) { return Collections.emptyIterator(); diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreReader.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreReader.java index 4852dd53e16c..7ba5089ae4eb 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreReader.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreReader.java @@ -25,7 +25,7 @@ public interface DatastoreReader { /** - * Returns an {@link Entity} for the given {@link Key} or {@code null} if does not exists. + * Returns an {@link Entity} for the given {@link Key} or {@code null} if it doesn't exist. * * @throws DatastoreException upon failure */ @@ -34,7 +34,7 @@ public interface DatastoreReader { /** * Returns an {@link Entity} for each given {@link Key} that exists in the Datastore. * The order of the result is unspecified. - * Results are loaded lazily therefore it is possible to get a {@code DatastoreException} + * Results are loaded lazily, so it is possible to get a {@code DatastoreException} * from the returned {@code Iterator}'s {@link Iterator#hasNext hasNext} or * {@link Iterator#next next} methods. * @@ -45,13 +45,13 @@ public interface DatastoreReader { /** * Returns a list with a value for each given key (ordered by input). - * A {@code null} would be returned for non-existing keys. - * When possible prefer using {@link #get(Key...)} which does not eagerly loads the results. + * {@code null} values are returned for nonexistent keys. + * When possible prefer using {@link #get(Key...)} to avoid eagerly loading the results. */ List fetch(Key... keys); /** - * Submit a {@link Query} and returns its result. + * Submits a {@link Query} and returns its result. * * @throws DatastoreException upon failure */ diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ReadOption.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ReadOption.java new file mode 100644 index 000000000000..86214fddbf02 --- /dev/null +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ReadOption.java @@ -0,0 +1,66 @@ +/* + * 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.datastore; + +import com.google.common.collect.ImmutableMap; + +import java.io.Serializable; +import java.util.Map; + +/** + * Specifies options for read operations in Datastore, namely getting/fetching entities and running + * queries. + */ +public abstract class ReadOption implements Serializable { + + private static final long serialVersionUID = -4406964829189800528L; + + /** + * Specifies eventual consistency for reads from Datastore. + */ + public static final class EventualConsistency extends ReadOption { + + private static final long serialVersionUID = -6959530217724666172L; + + private final boolean eventualConsistency; + + private EventualConsistency(boolean eventualConsistency) { + this.eventualConsistency = eventualConsistency; + } + + public boolean isEventual() { + return eventualConsistency; + } + } + + ReadOption() {} + + /** + * Returns a {@code ReadOption} that specifies eventual consistency. + */ + public static EventualConsistency eventualConsistency() { + return new EventualConsistency(true); + } + + static Map, ReadOption> asImmutableMap(ReadOption... options) { + ImmutableMap.Builder, ReadOption> builder = ImmutableMap.builder(); + for (ReadOption option : options) { + builder.put(option.getClass(), option); + } + return builder.build(); + } +} diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionImpl.java index 5a422927005a..469c14e1c78a 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/TransactionImpl.java @@ -40,7 +40,7 @@ static class ResponseImpl implements Transaction.Response { @Override public List generatedKeys() { - Iterator results = + Iterator results = response.getMutationResultsList().iterator(); List generated = new ArrayList<>(numAutoAllocatedIds); for (int i = 0; i < numAutoAllocatedIds; i++) { @@ -66,7 +66,7 @@ public Entity get(Key key) { @Override public Iterator get(Key... keys) { validateActive(); - com.google.datastore.v1beta3.ReadOptions.Builder readOptionsPb = + com.google.datastore.v1beta3.ReadOptions.Builder readOptionsPb = com.google.datastore.v1beta3.ReadOptions.newBuilder(); readOptionsPb.setTransaction(transaction); return datastore.get(readOptionsPb.build(), keys); @@ -81,7 +81,7 @@ public List fetch(Key... keys) { @Override public QueryResults run(Query query) { validateActive(); - com.google.datastore.v1beta3.ReadOptions.Builder readOptionsPb = + com.google.datastore.v1beta3.ReadOptions.Builder readOptionsPb = com.google.datastore.v1beta3.ReadOptions.newBuilder(); readOptionsPb.setTransaction(transaction); return datastore.run(readOptionsPb.build(), query); @@ -91,7 +91,7 @@ public QueryResults run(Query query) { public Transaction.Response commit() { validateActive(); List mutationsPb = toMutationPbList(); - com.google.datastore.v1beta3.CommitRequest.Builder requestPb = + com.google.datastore.v1beta3.CommitRequest.Builder requestPb = com.google.datastore.v1beta3.CommitRequest.newBuilder(); requestPb.setMode(com.google.datastore.v1beta3.CommitRequest.Mode.TRANSACTIONAL); requestPb.setTransaction(transaction); diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreHelperTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreHelperTest.java index 55c8d0cf3ce6..61b266a9abc2 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreHelperTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreHelperTest.java @@ -27,6 +27,7 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterators; import com.google.gcloud.datastore.Datastore.TransactionCallable; @@ -66,52 +67,100 @@ public void testAllocateId() throws Exception { } @Test - public void testGet() throws Exception { + public void testGetWithDatastore() throws Exception { Datastore datastore = createStrictMock(Datastore.class); IncompleteKey pKey1 = IncompleteKey.builder("ds", "k").build(); Key key1 = Key.builder(pKey1, 1).build(); Entity entity1 = Entity.builder(key1).build(); Key key2 = Key.builder(pKey1, 2).build(); - expect(datastore.get(new Key[]{key1})) + ReadOption eventualConsistency = ReadOption.eventualConsistency(); + expect(datastore.get(Collections.singletonList(key1))) .andReturn(Collections.singletonList(entity1).iterator()); - expect(datastore.get(new Key[]{key2})) + expect(datastore.get(Collections.singletonList(key2))) .andReturn(Collections.emptyIterator()); + expect(datastore.get(Collections.singletonList(key1), eventualConsistency)) + .andReturn(Collections.singletonList(entity1).iterator()); replay(datastore); assertEquals(entity1, DatastoreHelper.get(datastore, key1)); assertNull(DatastoreHelper.get(datastore, key2)); + assertEquals(entity1, DatastoreHelper.get(datastore, key1, eventualConsistency)); verify(datastore); } + @Test + public void testGetWithTransaction() throws Exception { + Transaction transaction = createStrictMock(Transaction.class); + IncompleteKey pKey1 = IncompleteKey.builder("ds", "k").build(); + Key key1 = Key.builder(pKey1, 1).build(); + Entity entity1 = Entity.builder(key1).build(); + Key key2 = Key.builder(pKey1, 2).build(); + expect(transaction.get(new Key[] {key1})) + .andReturn(Collections.singletonList(entity1).iterator()); + expect(transaction.get(new Key[] {key2})).andReturn(Collections.emptyIterator()); + replay(transaction); + assertEquals(entity1, DatastoreHelper.get(transaction, key1)); + assertNull(DatastoreHelper.get(transaction, key2)); + verify(transaction); + } + @Test public void testAdd() throws Exception { Datastore datastore = createStrictMock(Datastore.class); IncompleteKey pKey = IncompleteKey.builder("ds", "k").build(); Key key = Key.builder(pKey, 1).build(); Entity entity = Entity.builder(key).build(); - expect(datastore.add(new Entity[]{entity})) - .andReturn(Collections.singletonList(entity)); + expect(datastore.add(new Entity[] {entity})).andReturn(Collections.singletonList(entity)); replay(datastore); assertEquals(entity, DatastoreHelper.add(datastore, entity)); verify(datastore); } @Test - public void testFetch() throws Exception { + public void testFetchWithDatastore() throws Exception { Datastore datastore = createStrictMock(Datastore.class); IncompleteKey pKey1 = IncompleteKey.builder("ds", "k").build(); Key key1 = Key.builder(pKey1, 1).build(); Key key2 = Key.builder(pKey1, "a").build(); Entity entity1 = Entity.builder(key1).build(); Entity entity2 = Entity.builder(key2).build(); - expect(datastore.get(key1, key2)).andReturn(Iterators.forArray(entity1, entity2)).once(); + ReadOption eventualConsistency = ReadOption.eventualConsistency(); + expect(datastore.get(ImmutableList.of(key1, key2))) + .andReturn(Iterators.forArray(entity1, entity2)) + .once(); + expect(datastore.get(ImmutableList.of(key1, key2), eventualConsistency)) + .andReturn(Iterators.forArray(entity1, entity2)) + .once(); replay(datastore); - List values = DatastoreHelper.fetch(datastore, key1, key2); + List values = DatastoreHelper.fetch(datastore, new Key[] {key1, key2}); + assertEquals(2, values.size()); + assertEquals(entity1, values.get(0)); + assertEquals(entity2, values.get(1)); + values = DatastoreHelper.fetch(datastore, new Key[] {key1, key2}, eventualConsistency); assertEquals(2, values.size()); assertEquals(entity1, values.get(0)); assertEquals(entity2, values.get(1)); verify(datastore); } + @Test + public void testFetchWithTransaction() throws Exception { + Transaction transaction = createStrictMock(Transaction.class); + IncompleteKey pKey1 = IncompleteKey.builder("ds", "k").build(); + Key key1 = Key.builder(pKey1, 1).build(); + Key key2 = Key.builder(pKey1, "a").build(); + Entity entity1 = Entity.builder(key1).build(); + Entity entity2 = Entity.builder(key2).build(); + expect(transaction.get(new Key[] {key1, key2})) + .andReturn(Iterators.forArray(entity1, entity2)) + .once(); + replay(transaction); + List values = DatastoreHelper.fetch(transaction, new Key[] {key1, key2}); + assertEquals(2, values.size()); + assertEquals(entity1, values.get(0)); + assertEquals(entity2, values.get(1)); + verify(transaction); + } + @Test public void testRunInTransaction() throws Exception { final Datastore datastore = createStrictMock(Datastore.class); diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index 002e5f6df04f..957ab881e7c4 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -25,7 +25,12 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterators; +import com.google.datastore.v1beta3.LookupResponse; +import com.google.datastore.v1beta3.PartitionId; +import com.google.datastore.v1beta3.ReadOptions.ReadConsistency; +import com.google.datastore.v1beta3.RunQueryResponse; import com.google.gcloud.RetryParams; import com.google.gcloud.datastore.Query.ResultType; import com.google.gcloud.datastore.StructuredQuery.OrderBy; @@ -690,6 +695,37 @@ public void testQueryPaginationWithLimit() throws DatastoreException { return responses; } + @Test + public void testEventuallyConsistentQuery() { + DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class); + DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class); + EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) + .andReturn(rpcMock); + com.google.datastore.v1beta3.ReadOptions readOption = + com.google.datastore.v1beta3.ReadOptions.newBuilder() + .setReadConsistencyValue(ReadConsistency.EVENTUAL_VALUE) + .build(); + com.google.datastore.v1beta3.GqlQuery query = + com.google.datastore.v1beta3.GqlQuery.newBuilder() + .setQueryString("FROM * SELECT *") + .build(); + com.google.datastore.v1beta3.RunQueryRequest.Builder expectedRequest = + com.google.datastore.v1beta3.RunQueryRequest.newBuilder() + .setReadOptions(readOption) + .setGqlQuery(query) + .setPartitionId(PartitionId.newBuilder().setProjectId(PROJECT_ID).build()); + EasyMock.expect(rpcMock.runQuery(expectedRequest.build())) + .andReturn(RunQueryResponse.newBuilder().build()); + EasyMock.replay(rpcFactoryMock, rpcMock); + Datastore mockDatastore = options.toBuilder() + .retryParams(RetryParams.defaultInstance()) + .serviceRpcFactory(rpcFactoryMock) + .build() + .service(); + mockDatastore.run( + Query.gqlQueryBuilder("FROM * SELECT *").build(), ReadOption.eventualConsistency()); + } + @Test public void testToUrlSafe() { byte[][] invalidUtf8 = @@ -765,6 +801,40 @@ public void testGet() { assertFalse(entity.contains("bla")); } + @Test + public void testLookupEventualConsistency() { + DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class); + DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class); + EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) + .andReturn(rpcMock); + com.google.datastore.v1beta3.ReadOptions readOption = + com.google.datastore.v1beta3.ReadOptions.newBuilder() + .setReadConsistencyValue(ReadConsistency.EVENTUAL_VALUE) + .build(); + com.google.datastore.v1beta3.Key key = com.google.datastore.v1beta3.Key.newBuilder() + .setPartitionId(PartitionId.newBuilder().setProjectId(PROJECT_ID).build()) + .addPath(com.google.datastore.v1beta3.Key.PathElement.newBuilder() + .setKind("kind1").setName("name").build()) + .build(); + com.google.datastore.v1beta3.LookupRequest lookupRequest = + com.google.datastore.v1beta3.LookupRequest.newBuilder() + .setReadOptions(readOption) + .addKeys(key) + .build(); + EasyMock.expect(rpcMock.lookup(lookupRequest)) + .andReturn(LookupResponse.newBuilder().build()) + .times(3); + EasyMock.replay(rpcFactoryMock, rpcMock); + Datastore mockDatastore = options.toBuilder() + .retryParams(RetryParams.defaultInstance()) + .serviceRpcFactory(rpcFactoryMock) + .build() + .service(); + mockDatastore.get(KEY1, ReadOption.eventualConsistency()); + mockDatastore.get(ImmutableList.of(KEY1), ReadOption.eventualConsistency()); + mockDatastore.fetch(ImmutableList.of(KEY1), ReadOption.eventualConsistency()); + } + @Test public void testGetArrayNoDeferredResults() { datastore.put(ENTITY3); From 76e3b3ef5ead06e8741d887ca8c337451470692a Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Tue, 1 Mar 2016 08:13:53 -0800 Subject: [PATCH 27/60] Clean up test code and javadoc formatting --- .../google/gcloud/datastore/Datastore.java | 21 +- .../gcloud/datastore/DatastoreHelper.java | 15 +- .../gcloud/datastore/DatastoreImpl.java | 15 +- .../gcloud/datastore/DatastoreReader.java | 15 +- .../google/gcloud/datastore/ReadOption.java | 2 +- .../gcloud/datastore/DatastoreTest.java | 452 +++++++----------- 6 files changed, 190 insertions(+), 330 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Datastore.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Datastore.java index 00b914a36678..c0efaa52b4e8 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Datastore.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Datastore.java @@ -114,11 +114,11 @@ interface TransactionCallable { Entity get(Key key, ReadOption... options); /** - * Returns an {@link Entity} for each given {@link Key} that exists in the Datastore. - * The order of the result is unspecified. - * Results are loaded lazily, so it is possible to get a {@code DatastoreException} - * from the returned {@code Iterator}'s {@link Iterator#hasNext hasNext} or - * {@link Iterator#next next} methods. {@link ReadOption}s can be specified if desired. + * Returns an {@link Entity} for each given {@link Key} that exists in the Datastore. The order of + * the result is unspecified. Results are loaded lazily, so it is possible to get a + * {@code DatastoreException} from the returned {@code Iterator}'s + * {@link Iterator#hasNext hasNext} or {@link Iterator#next next} methods. {@link ReadOption}s can + * be specified if desired. * * @throws DatastoreException upon failure * @see #get(Key) @@ -126,16 +126,15 @@ interface TransactionCallable { Iterator get(Iterable keys, ReadOption... options); /** - * Returns a list with a value for each given key (ordered by input). - * {@code null} values are returned for nonexistent keys. - * When possible prefer using {@link #get(Key...)} to avoid eagerly loading the results. - * {@link ReadOption}s can be specified if desired. + * Returns a list with a value for each given key (ordered by input). {@code null} values are + * returned for nonexistent keys. When possible prefer using {@link #get(Key...)} to avoid eagerly + * loading the results. {@link ReadOption}s can be specified if desired. */ List fetch(Iterable keys, ReadOption... options); /** - * Submits a {@link Query} and returns its result. - * {@link ReadOption}s can be specified if desired. + * Submits a {@link Query} and returns its result. {@link ReadOption}s can be specified if + * desired. * * @throws DatastoreException upon failure */ diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreHelper.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreHelper.java index 2873e2f8aafa..e3cf9c055576 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreHelper.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreHelper.java @@ -56,22 +56,19 @@ static KeyFactory newKeyFactory(DatastoreOptions options) { } /** - * Returns a list with a value for each given key (ordered by input). - * {@code null} values are returned for nonexistent keys. + * Returns a list with a value for each given key (ordered by input). {@code null} values are + * returned for nonexistent keys. */ static List fetch(Transaction reader, Key... keys) { - Iterator entities = reader.get(keys); - return compileEntities(keys, entities); + return compileEntities(keys, reader.get(keys)); } /** - * Returns a list with a value for each given key (ordered by input). - * {@code null} values are returned for nonexistent keys. + * Returns a list with a value for each given key (ordered by input). {@code null} values are + * returned for nonexistent keys. */ static List fetch(Datastore reader, Key[] keys, ReadOption... options) { - Iterator entities; - entities = reader.get(Arrays.asList(keys), options); - return compileEntities(keys, entities); + return compileEntities(keys, reader.get(Arrays.asList(keys), options)); } private static List compileEntities(Key[] keys, Iterator entities) { diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java index 73d25ab140d7..80cf59980de1 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java @@ -210,16 +210,11 @@ public Iterator get(Iterable keys, ReadOption... options) { private static com.google.datastore.v1beta3.ReadOptions toReadOptionsPb(ReadOption... options) { com.google.datastore.v1beta3.ReadOptions readOptionsPb = null; - if (options != null) { - Map, ReadOption> optionsMap = ReadOption.asImmutableMap(options); - EventualConsistency eventualConsistency = - (EventualConsistency) optionsMap.get(EventualConsistency.class); - if (eventualConsistency != null) { - readOptionsPb = - com.google.datastore.v1beta3.ReadOptions.newBuilder() - .setReadConsistency(ReadConsistency.EVENTUAL) - .build(); - } + if (options != null + && ReadOption.asImmutableMap(options).containsKey(EventualConsistency.class)) { + readOptionsPb = com.google.datastore.v1beta3.ReadOptions.newBuilder() + .setReadConsistency(ReadConsistency.EVENTUAL) + .build(); } return readOptionsPb; } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreReader.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreReader.java index 7ba5089ae4eb..3d6e5ec73243 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreReader.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreReader.java @@ -32,11 +32,10 @@ public interface DatastoreReader { Entity get(Key key); /** - * Returns an {@link Entity} for each given {@link Key} that exists in the Datastore. - * The order of the result is unspecified. - * Results are loaded lazily, so it is possible to get a {@code DatastoreException} - * from the returned {@code Iterator}'s {@link Iterator#hasNext hasNext} or - * {@link Iterator#next next} methods. + * Returns an {@link Entity} for each given {@link Key} that exists in the Datastore. The order of + * the result is unspecified. Results are loaded lazily, so it is possible to get a + * {@code DatastoreException} from the returned {@code Iterator}'s + * {@link Iterator#hasNext hasNext} or {@link Iterator#next next} methods. * * @throws DatastoreException upon failure * @see #get(Key) @@ -44,9 +43,9 @@ public interface DatastoreReader { Iterator get(Key... key); /** - * Returns a list with a value for each given key (ordered by input). - * {@code null} values are returned for nonexistent keys. - * When possible prefer using {@link #get(Key...)} to avoid eagerly loading the results. + * Returns a list with a value for each given key (ordered by input). {@code null} values are + * returned for nonexistent keys. When possible prefer using {@link #get(Key...)} to avoid eagerly + * loading the results. */ List fetch(Key... keys); diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ReadOption.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ReadOption.java index 86214fddbf02..f8905716e564 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ReadOption.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ReadOption.java @@ -47,7 +47,7 @@ public boolean isEventual() { } } - ReadOption() {} + private ReadOption() {} /** * Returns a {@code ReadOption} that specifies eventual consistency. diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index 957ab881e7c4..23c22ac4f645 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -27,9 +27,14 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterators; +import com.google.datastore.v1beta3.EntityResult; +import com.google.datastore.v1beta3.LookupRequest; import com.google.datastore.v1beta3.LookupResponse; import com.google.datastore.v1beta3.PartitionId; +import com.google.datastore.v1beta3.QueryResultBatch; +import com.google.datastore.v1beta3.ReadOptions; import com.google.datastore.v1beta3.ReadOptions.ReadConsistency; +import com.google.datastore.v1beta3.RunQueryRequest; import com.google.datastore.v1beta3.RunQueryResponse; import com.google.gcloud.RetryParams; import com.google.gcloud.datastore.Query.ResultType; @@ -96,22 +101,24 @@ public class DatastoreTest { private static final FullEntity PARTIAL_ENTITY3 = FullEntity.builder(PARTIAL_ENTITY1).key(IncompleteKey.builder(PROJECT_ID, KIND3).build()) .build(); - private static final Entity ENTITY1 = - Entity.builder(KEY1) - .set("str", STR_VALUE) - .set("date", DATE_TIME_VALUE) - .set("latLng", LAT_LNG_VALUE) - .set("bool", BOOL_VALUE) - .set("partial1", EntityValue.of(PARTIAL_ENTITY1)) - .set("list", LIST_VALUE2) - .build(); + private static final Entity ENTITY1 = Entity.builder(KEY1) + .set("str", STR_VALUE) + .set("date", DATE_TIME_VALUE) + .set("latLng", LAT_LNG_VALUE) + .set("bool", BOOL_VALUE) + .set("partial1", EntityValue.of(PARTIAL_ENTITY1)) + .set("list", LIST_VALUE2) + .build(); private static final Entity ENTITY2 = Entity.builder(ENTITY1).key(KEY2).remove("str") .set("name", "Dan").setNull("null").set("age", 20).build(); private static final Entity ENTITY3 = Entity.builder(ENTITY1).key(KEY3).remove("str") .set("null", NULL_VALUE).set("partial1", PARTIAL_ENTITY2).set("partial2", ENTITY2).build(); private DatastoreOptions options; + private DatastoreOptions rpcMockOptions; private Datastore datastore; + private DatastoreRpcFactory rpcFactoryMock; + private DatastoreRpc rpcMock; private static LocalGcdHelper gcdHelper; private static final int PORT = LocalGcdHelper.findAvailablePort(LocalGcdHelper.DEFAULT_PORT); @@ -134,6 +141,14 @@ public void setUp() { .retryParams(RetryParams.noRetries()) .build(); datastore = options.service(); + rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class); + rpcMock = EasyMock.createStrictMock(DatastoreRpc.class); + rpcMockOptions = options + .toBuilder() + .retryParams(RetryParams.defaultInstance()) + .serviceRpcFactory(rpcFactoryMock) + .build(); + EasyMock.expect(rpcFactoryMock.create(rpcMockOptions)).andReturn(rpcMock); StructuredQuery query = Query.keyQueryBuilder().build(); QueryResults result = datastore.run(query); datastore.delete(Iterators.toArray(result, Key.class)); @@ -426,24 +441,13 @@ public void testRunGqlQueryWithCasting() { @Test public void testGqlQueryPagination() throws DatastoreException { - DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class); - DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class); - EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) - .andReturn(rpcMock); - List responses = - buildResponsesForQueryPagination(); + List responses = buildResponsesForQueryPagination(); for (int i = 0; i < responses.size(); i++) { - EasyMock - .expect(rpcMock.runQuery( - EasyMock.anyObject(com.google.datastore.v1beta3.RunQueryRequest.class))) - .andReturn(responses.get(i)); + EasyMock.expect(rpcMock.runQuery(EasyMock.anyObject(RunQueryRequest.class))) + .andReturn(responses.get(i)); } EasyMock.replay(rpcFactoryMock, rpcMock); - DatastoreOptions options = this.options.toBuilder() - .retryParams(RetryParams.defaultInstance()) - .serviceRpcFactory(rpcFactoryMock) - .build(); - Datastore mockDatastore = options.service(); + Datastore mockDatastore = rpcMockOptions.service(); QueryResults results = mockDatastore.run(Query.gqlQueryBuilder(ResultType.KEY, "select __key__ from *").build()); int count = 0; @@ -480,15 +484,14 @@ public void testRunStructuredQuery() { assertTrue(projectionEntity.names().isEmpty()); assertFalse(results2.hasNext()); - StructuredQuery projectionQuery = - Query.projectionEntityQueryBuilder() - .kind(KIND2) - .projection("age") - .filter(PropertyFilter.gt("age", 18)) - .distinctOn("age") - .orderBy(OrderBy.asc("age")) - .limit(10) - .build(); + StructuredQuery projectionQuery = Query.projectionEntityQueryBuilder() + .kind(KIND2) + .projection("age") + .filter(PropertyFilter.gt("age", 18)) + .distinctOn("age") + .orderBy(OrderBy.asc("age")) + .limit(10) + .build(); QueryResults results4 = datastore.run(projectionQuery); assertTrue(results4.hasNext()); @@ -501,25 +504,14 @@ public void testRunStructuredQuery() { @Test public void testStructuredQueryPagination() throws DatastoreException { - DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class); - DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class); - EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) - .andReturn(rpcMock); - List responses = - buildResponsesForQueryPagination(); + List responses = buildResponsesForQueryPagination(); for (int i = 0; i < responses.size(); i++) { - EasyMock - .expect(rpcMock.runQuery( - EasyMock.anyObject(com.google.datastore.v1beta3.RunQueryRequest.class))) - .andReturn(responses.get(i)); + EasyMock.expect(rpcMock.runQuery(EasyMock.anyObject(RunQueryRequest.class))) + .andReturn(responses.get(i)); } EasyMock.replay(rpcFactoryMock, rpcMock); - DatastoreOptions options = this.options.toBuilder() - .retryParams(RetryParams.defaultInstance()) - .serviceRpcFactory(rpcFactoryMock) - .build(); - Datastore mockDatastore = options.service(); - QueryResults results = mockDatastore.run(Query.keyQueryBuilder().build()); + Datastore datastore = rpcMockOptions.service(); + QueryResults results = datastore.run(Query.keyQueryBuilder().build()); int count = 0; while (results.hasNext()) { count += 1; @@ -529,85 +521,57 @@ public void testStructuredQueryPagination() throws DatastoreException { EasyMock.verify(rpcFactoryMock, rpcMock); } - private List buildResponsesForQueryPagination() { + private List buildResponsesForQueryPagination() { Entity entity4 = Entity.builder(KEY4).set("value", StringValue.of("value")).build(); Entity entity5 = Entity.builder(KEY5).set("value", "value").build(); datastore.add(ENTITY3, entity4, entity5); - List responses = new ArrayList<>(); + List responses = new ArrayList<>(); Query query = Query.keyQueryBuilder().build(); - com.google.datastore.v1beta3.RunQueryRequest.Builder requestPb = - com.google.datastore.v1beta3.RunQueryRequest.newBuilder(); + RunQueryRequest.Builder requestPb = RunQueryRequest.newBuilder(); query.populatePb(requestPb); - com.google.datastore.v1beta3.QueryResultBatch queryResultBatchPb = - com.google.datastore.v1beta3.RunQueryResponse.newBuilder() - .mergeFrom(((DatastoreImpl) datastore).runQuery(requestPb.build())) - .getBatch(); - com.google.datastore.v1beta3.QueryResultBatch queryResultBatchPb1 = - com.google.datastore.v1beta3.QueryResultBatch.newBuilder() - .mergeFrom(queryResultBatchPb) - .setMoreResults( - com.google.datastore.v1beta3.QueryResultBatch.MoreResultsType.NOT_FINISHED) - .clearEntityResults() - .addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(0, 1)) - .setEndCursor(queryResultBatchPb.getEntityResultsList().get(0).getCursor()) - .build(); - responses.add( - com.google.datastore.v1beta3.RunQueryResponse.newBuilder() - .setBatch(queryResultBatchPb1) - .build()); - com.google.datastore.v1beta3.QueryResultBatch queryResultBatchPb2 = - com.google.datastore.v1beta3.QueryResultBatch.newBuilder() - .mergeFrom(queryResultBatchPb) - .setMoreResults( - com.google.datastore.v1beta3.QueryResultBatch.MoreResultsType.NOT_FINISHED) - .clearEntityResults() - .addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(1, 3)) - .setEndCursor(queryResultBatchPb.getEntityResultsList().get(2).getCursor()) - .build(); - responses.add( - com.google.datastore.v1beta3.RunQueryResponse.newBuilder() - .setBatch(queryResultBatchPb2) - .build()); - com.google.datastore.v1beta3.QueryResultBatch queryResultBatchPb3 = - com.google.datastore.v1beta3.QueryResultBatch.newBuilder() - .mergeFrom(queryResultBatchPb) - .setMoreResults( - com.google.datastore.v1beta3.QueryResultBatch.MoreResultsType.NO_MORE_RESULTS) - .clearEntityResults() - .addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(3, 5)) - .setEndCursor(queryResultBatchPb.getEntityResultsList().get(4).getCursor()) - .build(); - responses.add( - com.google.datastore.v1beta3.RunQueryResponse.newBuilder() - .setBatch(queryResultBatchPb3) - .build()); + QueryResultBatch queryResultBatchPb = RunQueryResponse.newBuilder() + .mergeFrom(((DatastoreImpl) datastore).runQuery(requestPb.build())) + .getBatch(); + QueryResultBatch queryResultBatchPb1 = QueryResultBatch.newBuilder() + .mergeFrom(queryResultBatchPb) + .setMoreResults(QueryResultBatch.MoreResultsType.NOT_FINISHED) + .clearEntityResults() + .addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(0, 1)) + .setEndCursor(queryResultBatchPb.getEntityResultsList().get(0).getCursor()) + .build(); + responses.add(RunQueryResponse.newBuilder().setBatch(queryResultBatchPb1).build()); + QueryResultBatch queryResultBatchPb2 = QueryResultBatch.newBuilder() + .mergeFrom(queryResultBatchPb) + .setMoreResults(QueryResultBatch.MoreResultsType.NOT_FINISHED) + .clearEntityResults() + .addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(1, 3)) + .setEndCursor(queryResultBatchPb.getEntityResultsList().get(2).getCursor()) + .build(); + responses.add(RunQueryResponse.newBuilder().setBatch(queryResultBatchPb2).build()); + QueryResultBatch queryResultBatchPb3 = QueryResultBatch.newBuilder() + .mergeFrom(queryResultBatchPb) + .setMoreResults(QueryResultBatch.MoreResultsType.NO_MORE_RESULTS) + .clearEntityResults() + .addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(3, 5)) + .setEndCursor(queryResultBatchPb.getEntityResultsList().get(4).getCursor()) + .build(); + responses.add(RunQueryResponse.newBuilder().setBatch(queryResultBatchPb3).build()); return responses; } public void testQueryPaginationWithLimit() throws DatastoreException { - DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class); - DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class); - EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) - .andReturn(rpcMock); - List responses = - buildResponsesForQueryPaginationWithLimit(); + List responses = buildResponsesForQueryPaginationWithLimit(); for (int i = 0; i < responses.size(); i++) { - EasyMock.expect( - rpcMock.runQuery( - EasyMock.anyObject(com.google.datastore.v1beta3.RunQueryRequest.class))) + EasyMock.expect(rpcMock.runQuery(EasyMock.anyObject(RunQueryRequest.class))) .andReturn(responses.get(i)); } EasyMock.replay(rpcFactoryMock, rpcMock); - Datastore mockDatastore = options.toBuilder() - .retryParams(RetryParams.defaultInstance()) - .serviceRpcFactory(rpcFactoryMock) - .build() - .service(); + Datastore datastore = rpcMockOptions.service(); int limit = 2; int totalCount = 0; StructuredQuery query = Query.entityQueryBuilder().limit(limit).build(); while (true) { - QueryResults results = mockDatastore.run(query); + QueryResults results = datastore.run(query); int resultCount = 0; while (results.hasNext()) { results.next(); @@ -623,107 +587,72 @@ public void testQueryPaginationWithLimit() throws DatastoreException { EasyMock.verify(rpcFactoryMock, rpcMock); } - private List - buildResponsesForQueryPaginationWithLimit() { + private List buildResponsesForQueryPaginationWithLimit() { Entity entity4 = Entity.builder(KEY4).set("value", StringValue.of("value")).build(); Entity entity5 = Entity.builder(KEY5).set("value", "value").build(); datastore.add(ENTITY3, entity4, entity5); - List responses = new ArrayList<>(); + List responses = new ArrayList<>(); Query query = Query.entityQueryBuilder().build(); - com.google.datastore.v1beta3.RunQueryRequest.Builder requestPb = - com.google.datastore.v1beta3.RunQueryRequest.newBuilder(); + RunQueryRequest.Builder requestPb = RunQueryRequest.newBuilder(); query.populatePb(requestPb); - com.google.datastore.v1beta3.QueryResultBatch queryResultBatchPb = - com.google.datastore.v1beta3.RunQueryResponse.newBuilder() - .mergeFrom(((DatastoreImpl) datastore).runQuery(requestPb.build())) - .getBatch(); - com.google.datastore.v1beta3.QueryResultBatch queryResultBatchPb1 = - com.google.datastore.v1beta3.QueryResultBatch.newBuilder() - .mergeFrom(queryResultBatchPb) - .setMoreResults( - com.google.datastore.v1beta3.QueryResultBatch.MoreResultsType.NOT_FINISHED) - .clearEntityResults() - .addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(0, 1)) - .setEndCursor(queryResultBatchPb.getEntityResultsList().get(0).getCursor()) - .build(); - responses.add( - com.google.datastore.v1beta3.RunQueryResponse.newBuilder() - .setBatch(queryResultBatchPb1) - .build()); - com.google.datastore.v1beta3.QueryResultBatch queryResultBatchPb2 = - com.google.datastore.v1beta3.QueryResultBatch.newBuilder() - .mergeFrom(queryResultBatchPb) - .setMoreResults( - com.google.datastore.v1beta3.QueryResultBatch.MoreResultsType - .MORE_RESULTS_AFTER_LIMIT) - .clearEntityResults() - .addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(1, 2)) - .setEndCursor( - ByteString.copyFrom(new byte[] {(byte) 0x80})) // test invalid UTF-8 string - .build(); - responses.add( - com.google.datastore.v1beta3.RunQueryResponse.newBuilder() - .setBatch(queryResultBatchPb2) - .build()); - com.google.datastore.v1beta3.QueryResultBatch queryResultBatchPb3 = - com.google.datastore.v1beta3.QueryResultBatch.newBuilder() - .mergeFrom(queryResultBatchPb) - .setMoreResults( - com.google.datastore.v1beta3.QueryResultBatch.MoreResultsType - .MORE_RESULTS_AFTER_LIMIT) - .clearEntityResults() - .addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(2, 4)) - .setEndCursor(queryResultBatchPb.getEntityResultsList().get(3).getCursor()) - .build(); - responses.add( - com.google.datastore.v1beta3.RunQueryResponse.newBuilder() - .setBatch(queryResultBatchPb3) - .build()); - com.google.datastore.v1beta3.QueryResultBatch queryResultBatchPb4 = - com.google.datastore.v1beta3.QueryResultBatch.newBuilder() - .mergeFrom(queryResultBatchPb) - .setMoreResults( - com.google.datastore.v1beta3.QueryResultBatch.MoreResultsType.NO_MORE_RESULTS) - .clearEntityResults() - .addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(4, 5)) - .setEndCursor(queryResultBatchPb.getEntityResultsList().get(4).getCursor()) - .build(); - responses.add( - com.google.datastore.v1beta3.RunQueryResponse.newBuilder() - .setBatch(queryResultBatchPb4) - .build()); + QueryResultBatch queryResultBatchPb = RunQueryResponse.newBuilder() + .mergeFrom(((DatastoreImpl) datastore).runQuery(requestPb.build())) + .getBatch(); + QueryResultBatch queryResultBatchPb1 = QueryResultBatch.newBuilder() + .mergeFrom(queryResultBatchPb) + .setMoreResults(QueryResultBatch.MoreResultsType.NOT_FINISHED) + .clearEntityResults() + .addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(0, 1)) + .setEndCursor(queryResultBatchPb.getEntityResultsList().get(0).getCursor()) + .build(); + responses.add(RunQueryResponse.newBuilder().setBatch(queryResultBatchPb1).build()); + QueryResultBatch queryResultBatchPb2 = QueryResultBatch.newBuilder() + .mergeFrom(queryResultBatchPb) + .setMoreResults(QueryResultBatch.MoreResultsType.MORE_RESULTS_AFTER_LIMIT) + .clearEntityResults() + .addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(1, 2)) + .setEndCursor( + ByteString.copyFrom(new byte[] {(byte) 0x80})) // test invalid UTF-8 string + .build(); + responses.add(RunQueryResponse.newBuilder().setBatch(queryResultBatchPb2).build()); + QueryResultBatch queryResultBatchPb3 = QueryResultBatch.newBuilder() + .mergeFrom(queryResultBatchPb) + .setMoreResults(QueryResultBatch.MoreResultsType.MORE_RESULTS_AFTER_LIMIT) + .clearEntityResults() + .addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(2, 4)) + .setEndCursor(queryResultBatchPb.getEntityResultsList().get(3).getCursor()) + .build(); + responses.add(RunQueryResponse.newBuilder().setBatch(queryResultBatchPb3).build()); + QueryResultBatch queryResultBatchPb4 = QueryResultBatch.newBuilder() + .mergeFrom(queryResultBatchPb) + .setMoreResults(QueryResultBatch.MoreResultsType.NO_MORE_RESULTS) + .clearEntityResults() + .addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(4, 5)) + .setEndCursor(queryResultBatchPb.getEntityResultsList().get(4).getCursor()) + .build(); + responses.add(RunQueryResponse.newBuilder().setBatch(queryResultBatchPb4).build()); return responses; } @Test - public void testEventuallyConsistentQuery() { - DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class); - DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class); - EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) - .andReturn(rpcMock); - com.google.datastore.v1beta3.ReadOptions readOption = - com.google.datastore.v1beta3.ReadOptions.newBuilder() - .setReadConsistencyValue(ReadConsistency.EVENTUAL_VALUE) - .build(); - com.google.datastore.v1beta3.GqlQuery query = - com.google.datastore.v1beta3.GqlQuery.newBuilder() - .setQueryString("FROM * SELECT *") - .build(); - com.google.datastore.v1beta3.RunQueryRequest.Builder expectedRequest = - com.google.datastore.v1beta3.RunQueryRequest.newBuilder() - .setReadOptions(readOption) - .setGqlQuery(query) - .setPartitionId(PartitionId.newBuilder().setProjectId(PROJECT_ID).build()); + public void testEventualConsistencyQuery() { + ReadOptions readOption = + ReadOptions.newBuilder().setReadConsistencyValue(ReadConsistency.EVENTUAL_VALUE).build(); + com.google.datastore.v1beta3.GqlQuery query = com.google.datastore.v1beta3.GqlQuery.newBuilder() + .setQueryString("FROM * SELECT *") + .build(); + RunQueryRequest.Builder expectedRequest = RunQueryRequest.newBuilder() + .setReadOptions(readOption) + .setGqlQuery(query) + .setPartitionId(PartitionId.newBuilder().setProjectId(PROJECT_ID) + .build()); EasyMock.expect(rpcMock.runQuery(expectedRequest.build())) .andReturn(RunQueryResponse.newBuilder().build()); EasyMock.replay(rpcFactoryMock, rpcMock); - Datastore mockDatastore = options.toBuilder() - .retryParams(RetryParams.defaultInstance()) - .serviceRpcFactory(rpcFactoryMock) - .build() - .service(); - mockDatastore.run( + Datastore datastore = rpcMockOptions.service(); + datastore.run( Query.gqlQueryBuilder("FROM * SELECT *").build(), ReadOption.eventualConsistency()); + EasyMock.verify(rpcFactoryMock, rpcMock); } @Test @@ -803,36 +732,24 @@ public void testGet() { @Test public void testLookupEventualConsistency() { - DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class); - DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class); - EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) - .andReturn(rpcMock); - com.google.datastore.v1beta3.ReadOptions readOption = - com.google.datastore.v1beta3.ReadOptions.newBuilder() - .setReadConsistencyValue(ReadConsistency.EVENTUAL_VALUE) - .build(); + ReadOptions readOption = + ReadOptions.newBuilder().setReadConsistencyValue(ReadConsistency.EVENTUAL_VALUE).build(); com.google.datastore.v1beta3.Key key = com.google.datastore.v1beta3.Key.newBuilder() .setPartitionId(PartitionId.newBuilder().setProjectId(PROJECT_ID).build()) .addPath(com.google.datastore.v1beta3.Key.PathElement.newBuilder() .setKind("kind1").setName("name").build()) .build(); - com.google.datastore.v1beta3.LookupRequest lookupRequest = - com.google.datastore.v1beta3.LookupRequest.newBuilder() - .setReadOptions(readOption) - .addKeys(key) - .build(); + LookupRequest lookupRequest = + LookupRequest.newBuilder().setReadOptions(readOption).addKeys(key).build(); EasyMock.expect(rpcMock.lookup(lookupRequest)) .andReturn(LookupResponse.newBuilder().build()) .times(3); EasyMock.replay(rpcFactoryMock, rpcMock); - Datastore mockDatastore = options.toBuilder() - .retryParams(RetryParams.defaultInstance()) - .serviceRpcFactory(rpcFactoryMock) - .build() - .service(); - mockDatastore.get(KEY1, ReadOption.eventualConsistency()); - mockDatastore.get(ImmutableList.of(KEY1), ReadOption.eventualConsistency()); - mockDatastore.fetch(ImmutableList.of(KEY1), ReadOption.eventualConsistency()); + Datastore datastore = rpcMockOptions.service(); + datastore.get(KEY1, ReadOption.eventualConsistency()); + datastore.get(ImmutableList.of(KEY1), ReadOption.eventualConsistency()); + datastore.fetch(ImmutableList.of(KEY1), ReadOption.eventualConsistency()); + EasyMock.verify(rpcFactoryMock, rpcMock); } @Test @@ -898,57 +815,41 @@ private Datastore createDatastoreForDeferredLookup() throws DatastoreException { keysPb.add(KEY3.toPb()); keysPb.add(KEY4.toPb()); keysPb.add(KEY5.toPb()); - List lookupRequests = new ArrayList<>(); + List lookupRequests = new ArrayList<>(); + lookupRequests.add(LookupRequest.newBuilder().addAllKeys(keysPb).build()); lookupRequests.add( - com.google.datastore.v1beta3.LookupRequest.newBuilder().addAllKeys(keysPb).build()); - lookupRequests.add( - com.google.datastore.v1beta3.LookupRequest.newBuilder() + LookupRequest.newBuilder() .addKeys(keysPb.get(2)) .addKeys(keysPb.get(3)) .addKeys(keysPb.get(5)) .build()); - lookupRequests.add( - com.google.datastore.v1beta3.LookupRequest.newBuilder().addKeys(keysPb.get(5)).build()); + lookupRequests.add(LookupRequest.newBuilder().addKeys(keysPb.get(5)).build()); Entity entity4 = Entity.builder(KEY4).set("value", StringValue.of("value")).build(); Entity entity5 = Entity.builder(KEY5).set("value", "value").build(); - List lookupResponses = new ArrayList<>(); + List lookupResponses = new ArrayList<>(); lookupResponses.add( - com.google.datastore.v1beta3.LookupResponse.newBuilder() - .addFound( - com.google.datastore.v1beta3.EntityResult.newBuilder().setEntity(ENTITY1.toPb())) - .addFound( - com.google.datastore.v1beta3.EntityResult.newBuilder().setEntity(entity4.toPb())) + LookupResponse.newBuilder() + .addFound(EntityResult.newBuilder().setEntity(ENTITY1.toPb())) + .addFound(EntityResult.newBuilder().setEntity(entity4.toPb())) .addDeferred(keysPb.get(2)) .addDeferred(keysPb.get(3)) .addDeferred(keysPb.get(5)) .build()); lookupResponses.add( - com.google.datastore.v1beta3.LookupResponse.newBuilder() - .addFound( - com.google.datastore.v1beta3.EntityResult.newBuilder().setEntity(ENTITY3.toPb())) - .addFound( - com.google.datastore.v1beta3.EntityResult.newBuilder().setEntity(entity4.toPb())) + LookupResponse.newBuilder() + .addFound(EntityResult.newBuilder().setEntity(ENTITY3.toPb())) + .addFound(EntityResult.newBuilder().setEntity(entity4.toPb())) .addDeferred(keysPb.get(5)) .build()); lookupResponses.add( - com.google.datastore.v1beta3.LookupResponse.newBuilder() - .addFound( - com.google.datastore.v1beta3.EntityResult.newBuilder().setEntity(entity5.toPb())) + LookupResponse.newBuilder() + .addFound(EntityResult.newBuilder().setEntity(entity5.toPb())) .build()); - DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class); - DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class); - EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) - .andReturn(rpcMock); for (int i = 0; i < lookupRequests.size(); i++) { EasyMock.expect(rpcMock.lookup(lookupRequests.get(i))).andReturn(lookupResponses.get(i)); } EasyMock.replay(rpcFactoryMock, rpcMock); - DatastoreOptions options = - this.options.toBuilder() - .retryParams(RetryParams.defaultInstance()) - .serviceRpcFactory(rpcFactoryMock) - .build(); - return options.service(); + return rpcMockOptions.service(); } @Test @@ -1044,26 +945,15 @@ public void testKeyFactory() { @Test public void testRetryableException() throws Exception { - com.google.datastore.v1beta3.LookupRequest requestPb = - com.google.datastore.v1beta3.LookupRequest.newBuilder().addKeys(KEY1.toPb()).build(); - com.google.datastore.v1beta3.LookupResponse responsePb = - com.google.datastore.v1beta3.LookupResponse.newBuilder() - .addFound( - com.google.datastore.v1beta3.EntityResult.newBuilder().setEntity(ENTITY1.toPb())) - .build(); - DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class); - DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class); - EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) - .andReturn(rpcMock); + LookupRequest requestPb = LookupRequest.newBuilder().addKeys(KEY1.toPb()).build(); + LookupResponse responsePb = LookupResponse.newBuilder() + .addFound(EntityResult.newBuilder().setEntity(ENTITY1.toPb())) + .build(); EasyMock.expect(rpcMock.lookup(requestPb)) .andThrow(new DatastoreException(14, "UNAVAILABLE", "UNAVAILABLE", null)) .andReturn(responsePb); EasyMock.replay(rpcFactoryMock, rpcMock); - DatastoreOptions options = this.options.toBuilder() - .retryParams(RetryParams.defaultInstance()) - .serviceRpcFactory(rpcFactoryMock) - .build(); - Datastore datastore = options.service(); + Datastore datastore = rpcMockOptions.service(); Entity entity = datastore.get(KEY1); assertEquals(ENTITY1, entity); EasyMock.verify(rpcFactoryMock, rpcMock); @@ -1071,23 +961,13 @@ public void testRetryableException() throws Exception { @Test public void testNonRetryableException() throws Exception { - com.google.datastore.v1beta3.LookupRequest requestPb = - com.google.datastore.v1beta3.LookupRequest.newBuilder().addKeys(KEY1.toPb()).build(); - DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class); - DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class); - EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) - .andReturn(rpcMock); + LookupRequest requestPb = LookupRequest.newBuilder().addKeys(KEY1.toPb()).build(); EasyMock.expect(rpcMock.lookup(requestPb)) .andThrow( new DatastoreException(DatastoreException.UNKNOWN_CODE, "denied", "PERMISSION_DENIED")) .times(1); EasyMock.replay(rpcFactoryMock, rpcMock); - RetryParams retryParams = RetryParams.builder().retryMinAttempts(2).build(); - DatastoreOptions options = this.options.toBuilder() - .retryParams(retryParams) - .serviceRpcFactory(rpcFactoryMock) - .build(); - Datastore datastore = options.service(); + Datastore datastore = rpcMockOptions.service(); thrown.expect(DatastoreException.class); thrown.expectMessage("denied"); datastore.get(KEY1); @@ -1096,21 +976,11 @@ public void testNonRetryableException() throws Exception { @Test public void testRuntimeException() throws Exception { - com.google.datastore.v1beta3.LookupRequest requestPb = - com.google.datastore.v1beta3.LookupRequest.newBuilder().addKeys(KEY1.toPb()).build(); - DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class); - DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class); - EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) - .andReturn(rpcMock); + LookupRequest requestPb = LookupRequest.newBuilder().addKeys(KEY1.toPb()).build(); String exceptionMessage = "Artificial runtime exception"; - EasyMock.expect(rpcMock.lookup(requestPb)) - .andThrow(new RuntimeException(exceptionMessage)); + EasyMock.expect(rpcMock.lookup(requestPb)).andThrow(new RuntimeException(exceptionMessage)); EasyMock.replay(rpcFactoryMock, rpcMock); - DatastoreOptions options = this.options.toBuilder() - .retryParams(RetryParams.defaultInstance()) - .serviceRpcFactory(rpcFactoryMock) - .build(); - Datastore datastore = options.service(); + Datastore datastore = rpcMockOptions.service(); thrown.expect(DatastoreException.class); thrown.expectMessage(exceptionMessage); datastore.get(KEY1); From 0d794eaf55f61ad3156d3ecbce9b64562e870868 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Tue, 1 Mar 2016 10:08:37 -0800 Subject: [PATCH 28/60] Improve eventual consistency javadoc --- .../main/java/com/google/gcloud/datastore/ReadOption.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ReadOption.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ReadOption.java index f8905716e564..f0de06d1651d 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ReadOption.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ReadOption.java @@ -30,7 +30,8 @@ public abstract class ReadOption implements Serializable { private static final long serialVersionUID = -4406964829189800528L; /** - * Specifies eventual consistency for reads from Datastore. + * Specifies eventual consistency for reads from Datastore. Lookups and ancestor queries using + * this option permit Datastore to return stale results. */ public static final class EventualConsistency extends ReadOption { @@ -50,7 +51,8 @@ public boolean isEventual() { private ReadOption() {} /** - * Returns a {@code ReadOption} that specifies eventual consistency. + * Returns a {@code ReadOption} that specifies eventual consistency, allowing Datastore to return + * stale results from gets, fetches, and ancestor queries. */ public static EventualConsistency eventualConsistency() { return new EventualConsistency(true); From 1a5aade24d7cf0b7d0ee64eb80c3726174800446 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Tue, 22 Mar 2016 17:08:11 -0700 Subject: [PATCH 29/60] Renamed DnsRecord to RecordSet. Fixes #779. --- README.md | 12 +- gcloud-java-dns/README.md | 70 ++++---- .../com/google/gcloud/dns/ChangeRequest.java | 62 +++---- .../main/java/com/google/gcloud/dns/Dns.java | 67 +++---- .../java/com/google/gcloud/dns/DnsImpl.java | 26 +-- .../com/google/gcloud/dns/ProjectInfo.java | 23 +-- .../dns/{DnsRecord.java => RecordSet.java} | 56 +++--- .../main/java/com/google/gcloud/dns/Zone.java | 14 +- .../com/google/gcloud/dns/package-info.java | 2 +- .../google/gcloud/dns/spi/DefaultDnsRpc.java | 2 +- .../com/google/gcloud/dns/spi/DnsRpc.java | 4 +- .../gcloud/dns/testing/LocalDnsHelper.java | 44 ++--- .../gcloud/dns/testing/OptionParsers.java | 12 +- .../google/gcloud/dns/ChangeRequestTest.java | 22 +-- .../com/google/gcloud/dns/DnsImplTest.java | 36 ++-- .../java/com/google/gcloud/dns/DnsTest.java | 48 ++--- ...{DnsRecordTest.java => RecordSetTest.java} | 75 ++++---- .../google/gcloud/dns/SerializationTest.java | 20 +-- .../java/com/google/gcloud/dns/ZoneTest.java | 42 ++--- .../com/google/gcloud/dns/it/ITDnsTest.java | 170 +++++++++--------- .../dns/testing/LocalDnsHelperTest.java | 44 ++--- gcloud-java-examples/README.md | 2 +- .../gcloud/examples/dns/DnsExample.java | 36 ++-- ...rds.java => CreateOrUpdateRecordSets.java} | 16 +- .../examples/dns/snippets/DeleteZone.java | 10 +- ...java => ManipulateZonesAndRecordSets.java} | 32 ++-- 26 files changed, 477 insertions(+), 470 deletions(-) rename gcloud-java-dns/src/main/java/com/google/gcloud/dns/{DnsRecord.java => RecordSet.java} (83%) rename gcloud-java-dns/src/test/java/com/google/gcloud/dns/{DnsRecordTest.java => RecordSetTest.java} (63%) rename gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/{CreateOrUpdateDnsRecords.java => CreateOrUpdateRecordSets.java} (83%) rename gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/{ManipulateZonesAndRecords.java => ManipulateZonesAndRecordSets.java} (84%) diff --git a/README.md b/README.md index a753a96c8b21..52229f6d5d34 100644 --- a/README.md +++ b/README.md @@ -249,13 +249,13 @@ ZoneInfo zoneInfo = ZoneInfo.of(zoneName, domainName, description); Zone zone = dns.create(zoneInfo); ``` -The second snippet shows how to create records inside a zone. The complete code can be found on [CreateOrUpdateDnsRecords.java](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateOrUpdateDnsRecords.java). +The second snippet shows how to create records inside a zone. The complete code can be found on [CreateOrUpdateRecordSets.java](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateOrUpdateRecordSets.java). ```java import com.google.gcloud.dns.ChangeRequest; import com.google.gcloud.dns.Dns; import com.google.gcloud.dns.DnsOptions; -import com.google.gcloud.dns.DnsRecord; +import com.google.gcloud.dns.RecordSet; import com.google.gcloud.dns.Zone; import java.util.Iterator; @@ -265,7 +265,7 @@ Dns dns = DnsOptions.defaultInstance().service(); String zoneName = "my-unique-zone"; Zone zone = dns.getZone(zoneName); String ip = "12.13.14.15"; -DnsRecord toCreate = DnsRecord.builder("www.someexampledomain.com.", DnsRecord.Type.A) +RecordSet toCreate = RecordSet.builder("www.someexampledomain.com.", RecordSet.Type.A) .ttl(24, TimeUnit.HOURS) .addRecord(ip) .build(); @@ -273,9 +273,9 @@ ChangeRequest.Builder changeBuilder = ChangeRequest.builder().add(toCreate); // Verify that the record does not exist yet. // If it does exist, we will overwrite it with our prepared record. -Iterator recordIterator = zone.listDnsRecords().iterateAll(); -while (recordIterator.hasNext()) { - DnsRecord current = recordIterator.next(); +Iterator recordSetIterator = zone.listRecordSets().iterateAll(); +while (recordSetIterator.hasNext()) { + RecordSet current = recordSetIterator.next(); if (toCreate.name().equals(current.name()) && toCreate.type().equals(current.type())) { changeBuilder.delete(current); diff --git a/gcloud-java-dns/README.md b/gcloud-java-dns/README.md index 4f65d8e3b814..a2c3238d1f8f 100644 --- a/gcloud-java-dns/README.md +++ b/gcloud-java-dns/README.md @@ -92,7 +92,7 @@ Dns dns = DnsOptions.defaultInstance().service(); For other authentication options, see the [Authentication](https://github.com/GoogleCloudPlatform/gcloud-java#authentication) page. #### Managing Zones -DNS records in `gcloud-java-dns` are managed inside containers called "zones". `ZoneInfo` is a class +Record sets in `gcloud-java-dns` are managed inside containers called "zones". `ZoneInfo` is a class which encapsulates metadata that describe a zone in Google Cloud DNS. `Zone`, a subclass of `ZoneInfo`, adds service-related functionality over `ZoneInfo`. @@ -100,7 +100,7 @@ functionality over `ZoneInfo`. exists within your project, you'll get a helpful error message telling you to choose another name. In the code below, replace "my-unique-zone" with a unique zone name. See more about naming rules [here](https://cloud.google.com/dns/api/v1/managedZones#name).* -In this code snippet, we create a new zone to manage DNS records for domain `someexampledomain.com.` +In this code snippet, we create a new zone to manage record sets for domain `someexampledomain.com.` *Important: The service may require that you verify ownership of the domain for which you are creating a zone. Hence, we recommend that you do so beforehand. You can verify ownership of @@ -128,8 +128,8 @@ Zone zone = dns.create(zoneInfo); System.out.printf("Zone was created and assigned ID %s.%n", zone.id()); ``` -You now have an empty zone hosted in Google Cloud DNS which is ready to be populated with DNS -records for domain name `someexampledomain.com.` Upon creating the zone, the cloud service +You now have an empty zone hosted in Google Cloud DNS which is ready to be populated with +record sets for domain name `someexampledomain.com.` Upon creating the zone, the cloud service assigned a set of DNS servers to host records for this zone and created the required SOA and NS records for the domain. The following snippet prints the list of servers assigned to the zone created above. First, import @@ -152,15 +152,15 @@ You can now instruct your domain registrar to [update your domain name servers] As soon as this happens and the change propagates through cached values in DNS resolvers, all the DNS queries will be directed to and answered by the Google Cloud DNS service. -#### Creating DNS Records -Now that we have a zone, we can add some DNS records. The DNS records held within zones are +#### Creating Record Sets +Now that we have a zone, we can add some record sets. The record sets held within zones are modified by "change requests". In this example, we create and apply a change request to -our zone that creates a DNS record of type A and points URL www.someexampledomain.com to +our zone that creates a record set of type A and points URL www.someexampledomain.com to IP address 12.13.14.15. Start by adding ```java import com.google.gcloud.dns.ChangeRequest; -import com.google.gcloud.dns.DnsRecord; +import com.google.gcloud.dns.RecordSet; import java.util.concurrent.TimeUnit; ``` @@ -168,9 +168,9 @@ import java.util.concurrent.TimeUnit; and proceed with: ```java -// Prepare a www.someexampledomain.com. type A record with ttl of 24 hours +// Prepare a www.someexampledomain.com. type A record set with ttl of 24 hours String ip = "12.13.14.15"; -DnsRecord toCreate = DnsRecord.builder("www.someexampledomain.com.", DnsRecord.Type.A) +RecordSet toCreate = RecordSet.builder("www." + zone.dnsName(), RecordSet.Type.A) .ttl(24, TimeUnit.HOURS) .addRecord(ip) .build(); @@ -182,12 +182,12 @@ ChangeRequest changeRequest = ChangeRequest.builder().add(toCreate).build(); changeRequest = zone.applyChangeRequest(changeRequest); ``` -The `addRecord` method of `DnsRecord.Builder` accepts records in the form of -strings. The format of the strings depends on the type of the DNS record to be added. -More information on the supported DNS record types and record formats can be found [here](https://cloud.google.com/dns/what-is-cloud-dns#supported_record_types). +The `addRecord` method of `RecordSet.Builder` accepts records in the form of +strings. The format of the strings depends on the type of the record sets to be added. +More information on the supported record set types and record formats can be found [here](https://cloud.google.com/dns/what-is-cloud-dns#supported_record_types). -If you already have a DNS record, Cloud DNS will return an error upon an attempt to create a duplicate of it. -You can modify the code above to create a DNS record or update it if it already exists by making the +If you already have a record set, Cloud DNS will return an error upon an attempt to create a duplicate of it. +You can modify the code above to create a record set or update it if it already exists by making the following adjustment in your imports ```java @@ -202,9 +202,9 @@ ChangeRequest.Builder changeBuilder = ChangeRequest.builder().add(toCreate); // Verify the type A record does not exist yet. // If it does exist, we will overwrite it with our prepared record. -Iterator recordIterator = zone.listDnsRecords().iterateAll(); -while (recordIterator.hasNext()) { - DnsRecord current = recordIterator.next(); +Iterator recordSetIterator = zone.listRecordSets().iterateAll(); +while (recordSetIterator.hasNext()) { + RecordSet current = recordSetIterator.next(); if (toCreate.name().equals(current.name()) && toCreate.type().equals(current.type())) { changeBuilder.delete(current); } @@ -235,15 +235,15 @@ Change requests are applied atomically to all the assigned DNS servers at once. happens, it may still take a while for the change to be registered by the DNS cache resolvers. See more on this topic [here](https://cloud.google.com/dns/monitoring). -#### Listing Zones and DNS Records -Suppose that you have added more zones and DNS records, and now you want to list them. +#### Listing Zones and Record Sets +Suppose that you have added more zones and record sets, and now you want to list them. First, import the following (unless you have done so in the previous section): ```java import java.util.Iterator; ``` -Then add the following code to list all your zones and DNS records. +Then add the following code to list all your zones and record sets. ```java // List all your zones @@ -254,11 +254,11 @@ while (zoneIterator.hasNext()) { counter++; } -// List the DNS records in a particular zone -Iterator recordIterator = zone.listDnsRecords().iterateAll(); -System.out.println(String.format("DNS records inside %s:", zone.name())); -while (recordIterator.hasNext()) { - System.out.println(recordIterator.next()); +// List the record sets in a particular zone +recordSetIterator = zone.listRecordSets().iterateAll(); +System.out.println(String.format("Record sets inside %s:", zone.name())); +while (recordSetIterator.hasNext()) { + System.out.println(recordSetIterator.next()); } ``` @@ -276,15 +276,15 @@ while (changeIterator.hasNext()) { #### Deleting Zones If you no longer want to host a zone in Cloud DNS, you can delete it. -First, you need to empty the zone by deleting all its records except for the default SOA and NS records. +First, you need to empty the zone by deleting all its records except for the default SOA and NS record sets. ```java -// Make a change for deleting the records -ChangeRequest.Builder changeBuilder = ChangeRequest.builder(); +// Make a change for deleting the record sets +changeBuilder = ChangeRequest.builder(); while (recordIterator.hasNext()) { - DnsRecord current = recordIterator.next(); + RecordSet current = recordIterator.next(); // SOA and NS records cannot be deleted - if (!DnsRecord.Type.SOA.equals(current.type()) && !DnsRecord.Type.NS.equals(current.type())) { + if (!RecordSet.Type.SOA.equals(current.type()) && !RecordSet.Type.NS.equals(current.type())) { changeBuilder.delete(current); } } @@ -324,11 +324,11 @@ if (result) { We composed some of the aforementioned snippets into complete executable code samples. In [CreateZones.java](../gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateZone.java) -we create a zone. In [CreateOrUpdateDnsRecords.java](../gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateOrUpdateDnsRecords.java) -we create a type A record for a zone, or update an existing type A record to a new IP address. We +we create a zone. In [CreateOrUpdateRecordSets.java](../gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateOrUpdateRecordSets.java) +we create a type A record set for a zone, or update an existing type A record set to a new IP address. We demonstrate how to delete a zone in [DeleteZone.java](../gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java). -Finally, in [ManipulateZonesAndRecords.java](../gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecords.java) -we assemble all the code snippets together and create zone, create or update a DNS record, list zones, list DNS records, list changes, and +Finally, in [ManipulateZonesAndRecordSets.java](../gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java) +we assemble all the code snippets together and create zone, create or update a record set, list zones, list record sets, list changes, and delete a zone. The applications assume that they are running on Compute Engine or from your own desktop. To run any of these examples on App Engine, simply move the code from the main method to your application's servlet class and change the print statements to display on your webpage. diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java index 76d231b704c4..757d844cc3da 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java @@ -33,7 +33,7 @@ import java.util.Objects; /** - * A class representing an atomic update to a collection of {@link DnsRecord}s within a {@code + * A class representing an atomic update to a collection of {@link RecordSet}s within a {@code * Zone}. * * @see Google Cloud DNS documentation @@ -48,8 +48,8 @@ public ChangeRequest apply(com.google.api.services.dns.model.Change pb) { } }; private static final long serialVersionUID = -9027378042756366333L; - private final List additions; - private final List deletions; + private final List additions; + private final List deletions; private final String id; private final Long startTimeMillis; private final Status status; @@ -70,8 +70,8 @@ public enum Status { */ public static class Builder { - private List additions = new LinkedList<>(); - private List deletions = new LinkedList<>(); + private List additions = new LinkedList<>(); + private List deletions = new LinkedList<>(); private String id; private Long startTimeMillis; private Status status; @@ -88,43 +88,43 @@ private Builder() { } /** - * Sets a collection of {@link DnsRecord}s which are to be added to the zone upon executing this + * Sets a collection of {@link RecordSet}s which are to be added to the zone upon executing this * {@code ChangeRequest}. */ - public Builder additions(List additions) { + public Builder additions(List additions) { this.additions = Lists.newLinkedList(checkNotNull(additions)); return this; } /** - * Sets a collection of {@link DnsRecord}s which are to be deleted from the zone upon executing + * Sets a collection of {@link RecordSet}s which are to be deleted from the zone upon executing * this {@code ChangeRequest}. */ - public Builder deletions(List deletions) { + public Builder deletions(List deletions) { this.deletions = Lists.newLinkedList(checkNotNull(deletions)); return this; } /** - * Adds a {@link DnsRecord} to be added to the zone upon executing this {@code + * Adds a {@link RecordSet} to be added to the zone upon executing this {@code * ChangeRequest}. */ - public Builder add(DnsRecord record) { - this.additions.add(checkNotNull(record)); + public Builder add(RecordSet recordSet) { + this.additions.add(checkNotNull(recordSet)); return this; } /** - * Adds a {@link DnsRecord} to be deleted to the zone upon executing this + * Adds a {@link RecordSet} to be deleted to the zone upon executing this * {@code ChangeRequest}. */ - public Builder delete(DnsRecord record) { - this.deletions.add(checkNotNull(record)); + public Builder delete(RecordSet recordSet) { + this.deletions.add(checkNotNull(recordSet)); return this; } /** - * Clears the collection of {@link DnsRecord}s which are to be added to the zone upon executing + * Clears the collection of {@link RecordSet}s which are to be added to the zone upon executing * this {@code ChangeRequest}. */ public Builder clearAdditions() { @@ -133,7 +133,7 @@ public Builder clearAdditions() { } /** - * Clears the collection of {@link DnsRecord}s which are to be deleted from the zone upon + * Clears the collection of {@link RecordSet}s which are to be deleted from the zone upon * executing this {@code ChangeRequest}. */ public Builder clearDeletions() { @@ -142,20 +142,20 @@ public Builder clearDeletions() { } /** - * Removes a single {@link DnsRecord} from the collection of records to be + * Removes a single {@link RecordSet} from the collection of records to be * added to the zone upon executing this {@code ChangeRequest}. */ - public Builder removeAddition(DnsRecord record) { - this.additions.remove(record); + public Builder removeAddition(RecordSet recordSet) { + this.additions.remove(recordSet); return this; } /** - * Removes a single {@link DnsRecord} from the collection of records to be + * Removes a single {@link RecordSet} from the collection of records to be * deleted from the zone upon executing this {@code ChangeRequest}. */ - public Builder removeDeletion(DnsRecord record) { - this.deletions.remove(record); + public Builder removeDeletion(RecordSet recordSet) { + this.deletions.remove(recordSet); return this; } @@ -215,18 +215,18 @@ public Builder toBuilder() { } /** - * Returns the list of {@link DnsRecord}s to be added to the zone upon submitting this {@code + * Returns the list of {@link RecordSet}s to be added to the zone upon submitting this {@code * ChangeRequest}. */ - public List additions() { + public List additions() { return additions; } /** - * Returns the list of {@link DnsRecord}s to be deleted from the zone upon submitting this {@code + * Returns the list of {@link RecordSet}s to be deleted from the zone upon submitting this {@code * ChangeRequest}. */ - public List deletions() { + public List deletions() { return deletions; } @@ -267,9 +267,9 @@ com.google.api.services.dns.model.Change toPb() { pb.setStatus(status().name().toLowerCase()); } // set a list of additions - pb.setAdditions(Lists.transform(additions(), DnsRecord.TO_PB_FUNCTION)); + pb.setAdditions(Lists.transform(additions(), RecordSet.TO_PB_FUNCTION)); // set a list of deletions - pb.setDeletions(Lists.transform(deletions(), DnsRecord.TO_PB_FUNCTION)); + pb.setDeletions(Lists.transform(deletions(), RecordSet.TO_PB_FUNCTION)); return pb; } @@ -286,10 +286,10 @@ static ChangeRequest fromPb(com.google.api.services.dns.model.Change pb) { builder.status(ChangeRequest.Status.valueOf(pb.getStatus().toUpperCase())); } if (pb.getDeletions() != null) { - builder.deletions(Lists.transform(pb.getDeletions(), DnsRecord.FROM_PB_FUNCTION)); + builder.deletions(Lists.transform(pb.getDeletions(), RecordSet.FROM_PB_FUNCTION)); } if (pb.getAdditions() != null) { - builder.additions(Lists.transform(pb.getAdditions(), DnsRecord.FROM_PB_FUNCTION)); + builder.additions(Lists.transform(pb.getAdditions(), RecordSet.FROM_PB_FUNCTION)); } return builder.build(); } 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 6ce6b4c19994..f8614a8d6169 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 @@ -101,13 +101,13 @@ static String selector(ZoneField... fields) { } /** - * The fields of a DNS record. + * The fields of a record set. * *

These values can be used to specify the fields to include in a partial response when calling - * {@link Dns#listDnsRecords(String, DnsRecordListOption...)}. The name and type are always + * {@link Dns#listRecordSets(String, RecordSetListOption...)}. The name and type are always * returned even if not selected. */ - enum DnsRecordField { + enum RecordSetField { DNS_RECORDS("rrdatas"), NAME("name"), TTL("ttl"), @@ -115,7 +115,7 @@ enum DnsRecordField { private final String selector; - DnsRecordField(String selector) { + RecordSetField(String selector) { this.selector = selector; } @@ -123,11 +123,11 @@ String selector() { return selector; } - static String selector(DnsRecordField... fields) { + static String selector(RecordSetField... fields) { Set fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1); fieldStrings.add(NAME.selector()); fieldStrings.add(TYPE.selector()); - for (DnsRecordField field : fields) { + for (RecordSetField field : fields) { fieldStrings.add(field.selector()); } return Joiner.on(',').join(fieldStrings); @@ -180,28 +180,29 @@ public String selector() { } /** - * Class that for specifying DNS record options. + * Class for specifying record set listing options. */ - class DnsRecordListOption extends AbstractOption implements Serializable { + class RecordSetListOption extends AbstractOption implements Serializable { private static final long serialVersionUID = 1009627025381096098L; - DnsRecordListOption(DnsRpc.Option option, Object value) { + RecordSetListOption(DnsRpc.Option option, Object value) { super(option, value); } /** - * Returns an option to specify the DNS record's fields to be returned by the RPC call. + * Returns an option to specify the record set's fields to be returned by the RPC call. * *

If this option is not provided all record fields are returned. {@code - * DnsRecordField.fields} can be used to specify only the fields of interest. The name of the - * DNS record always returned, even if not specified. {@link DnsRecordField} provides a list of - * fields that can be used. + * RecordSetField.fields} can be used to specify only the fields of interest. The name of the + * record set in always returned, even if not specified. {@link RecordSetField} provides a list + * of fields that can be used. */ - public static DnsRecordListOption fields(DnsRecordField... fields) { + public static RecordSetListOption fields(RecordSetField... fields) { StringBuilder builder = new StringBuilder(); - builder.append("nextPageToken,rrsets(").append(DnsRecordField.selector(fields)).append(')'); - return new DnsRecordListOption(DnsRpc.Option.FIELDS, builder.toString()); + builder.append("nextPageToken,rrsets(").append(RecordSetField.selector(fields)) + .append(')'); + return new RecordSetListOption(DnsRpc.Option.FIELDS, builder.toString()); } /** @@ -210,33 +211,33 @@ public static DnsRecordListOption fields(DnsRecordField... fields) { *

The page token (returned from a previous call to list) indicates from where listing should * continue. */ - public static DnsRecordListOption pageToken(String pageToken) { - return new DnsRecordListOption(DnsRpc.Option.PAGE_TOKEN, pageToken); + public static RecordSetListOption pageToken(String pageToken) { + return new RecordSetListOption(DnsRpc.Option.PAGE_TOKEN, pageToken); } /** - * The maximum number of DNS records to return per RPC. + * The maximum number of record sets to return per RPC. * - *

The server can return fewer records than requested. When there are more results than the - * page size, the server will return a page token that can be used to fetch other results. + *

The server can return fewer record sets than requested. When there are more results than + * the page size, the server will return a page token that can be used to fetch other results. */ - public static DnsRecordListOption pageSize(int pageSize) { - return new DnsRecordListOption(DnsRpc.Option.PAGE_SIZE, pageSize); + public static RecordSetListOption pageSize(int pageSize) { + return new RecordSetListOption(DnsRpc.Option.PAGE_SIZE, pageSize); } /** - * Restricts the list to only DNS records with this fully qualified domain name. + * Restricts the list to only record sets with this fully qualified domain name. */ - public static DnsRecordListOption dnsName(String dnsName) { - return new DnsRecordListOption(DnsRpc.Option.NAME, dnsName); + public static RecordSetListOption dnsName(String dnsName) { + return new RecordSetListOption(DnsRpc.Option.NAME, dnsName); } /** - * Restricts the list to return only records of this type. If present, {@link - * Dns.DnsRecordListOption#dnsName(String)} must also be present. + * Restricts the list to return only record sets of this type. If present, {@link + * RecordSetListOption#dnsName(String)} must also be present. */ - public static DnsRecordListOption type(DnsRecord.Type type) { - return new DnsRecordListOption(DnsRpc.Option.DNS_TYPE, type.name()); + public static RecordSetListOption type(RecordSet.Type type) { + return new RecordSetListOption(DnsRpc.Option.DNS_TYPE, type.name()); } } @@ -478,16 +479,16 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) { boolean delete(String zoneName); // delete does not admit any options /** - * Lists the DNS records in the zone identified by name. + * Lists the record sets in the zone identified by name. * *

The fields to be returned, page size and page tokens can be specified using {@link - * DnsRecordListOption}s. + * RecordSetListOption}s. * * @throws DnsException upon failure or if the zone cannot be found * @see Cloud DNS * ResourceRecordSets: list */ - Page listDnsRecords(String zoneName, DnsRecordListOption... options); + Page listRecordSets(String zoneName, RecordSetListOption... options); /** * Retrieves the information about the current project. The returned fields can be optionally diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java index a60cfd9151da..2fbf4e8b5a79 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java @@ -85,7 +85,7 @@ public Page nextPage() { } } - private static class DnsRecordPageFetcher implements PageImpl.NextPageFetcher { + private static class DnsRecordPageFetcher implements PageImpl.NextPageFetcher { private static final long serialVersionUID = -6039369212511530846L; private final Map requestOptions; @@ -101,8 +101,8 @@ private static class DnsRecordPageFetcher implements PageImpl.NextPageFetcher nextPage() { - return listDnsRecords(zoneName, serviceOptions, requestOptions); + public Page nextPage() { + return listRecordSets(zoneName, serviceOptions, requestOptions); } } @@ -178,29 +178,29 @@ public DnsRpc.ListResult call() { } @Override - public Page listDnsRecords(String zoneName, DnsRecordListOption... options) { - return listDnsRecords(zoneName, options(), optionMap(options)); + public Page listRecordSets(String zoneName, RecordSetListOption... options) { + return listRecordSets(zoneName, options(), optionMap(options)); } - private static Page listDnsRecords(final String zoneName, + private static Page listRecordSets(final String zoneName, final DnsOptions serviceOptions, final Map optionsMap) { try { - // get a list of resource record sets + // get a list of record sets final DnsRpc rpc = serviceOptions.rpc(); DnsRpc.ListResult result = runWithRetries( new Callable>() { @Override public DnsRpc.ListResult call() { - return rpc.listDnsRecords(zoneName, optionsMap); + return rpc.listRecordSets(zoneName, optionsMap); } }, serviceOptions.retryParams(), EXCEPTION_HANDLER); String cursor = result.pageToken(); - // transform that list into dns records - Iterable records = result.results() == null - ? ImmutableList.of() - : Iterables.transform(result.results(), DnsRecord.FROM_PB_FUNCTION); + // transform that list into record sets + Iterable recordSets = result.results() == null + ? ImmutableList.of() + : Iterables.transform(result.results(), RecordSet.FROM_PB_FUNCTION); return new PageImpl<>(new DnsRecordPageFetcher(zoneName, serviceOptions, cursor, optionsMap), - cursor, records); + cursor, recordSets); } catch (RetryHelperException e) { throw DnsException.translateAndThrow(e); } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ProjectInfo.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ProjectInfo.java index f7b12b94bae8..319f06ad2444 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ProjectInfo.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ProjectInfo.java @@ -26,8 +26,8 @@ /** * The class provides the Google Cloud DNS information associated with this project. A project is a - * top level container for resources including {@code Zone}s. Projects can be created only in - * the APIs console. + * top level container for resources including {@code Zone}s. Projects can be created only in the + * APIs console. * * @see Google Cloud DNS documentation */ @@ -62,11 +62,11 @@ public static class Quota implements Serializable { * builder. */ Quota(int zones, - int resourceRecordsPerRrset, - int rrsetAdditionsPerChange, - int rrsetDeletionsPerChange, - int rrsetsPerZone, - int totalRrdataSizePerChange) { + int resourceRecordsPerRrset, + int rrsetAdditionsPerChange, + int rrsetDeletionsPerChange, + int rrsetsPerZone, + int totalRrdataSizePerChange) { this.zones = zones; this.resourceRecordsPerRrset = resourceRecordsPerRrset; this.rrsetAdditionsPerChange = rrsetAdditionsPerChange; @@ -83,21 +83,22 @@ public int zones() { } /** - * Returns the maximum allowed number of records per {@link DnsRecord}. + * Returns the maximum allowed number of records per {@link RecordSet}. */ public int resourceRecordsPerRrset() { return resourceRecordsPerRrset; } /** - * Returns the maximum allowed number of {@link DnsRecord}s to add per {@link ChangeRequest}. + * Returns the maximum allowed number of {@link RecordSet}s to add per {@link + * ChangeRequest}. */ public int rrsetAdditionsPerChange() { return rrsetAdditionsPerChange; } /** - * Returns the maximum allowed number of {@link DnsRecord}s to delete per {@link + * Returns the maximum allowed number of {@link RecordSet}s to delete per {@link * ChangeRequest}. */ public int rrsetDeletionsPerChange() { @@ -105,7 +106,7 @@ public int rrsetDeletionsPerChange() { } /** - * Returns the maximum allowed number of {@link DnsRecord}s per {@link ZoneInfo} in the + * Returns the maximum allowed number of {@link RecordSet}s per {@link ZoneInfo} in the * project. */ public int rrsetsPerZone() { diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsRecord.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/RecordSet.java similarity index 83% rename from gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsRecord.java rename to gcloud-java-dns/src/main/java/com/google/gcloud/dns/RecordSet.java index c4e710bd0365..dc6d956406c3 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsRecord.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/RecordSet.java @@ -35,28 +35,28 @@ /** * A class that represents a Google Cloud DNS record set. * - *

A {@code DnsRecord} is the unit of data that will be returned by the DNS servers upon a DNS - * request for a specific domain. The {@code DnsRecord} holds the current state of the DNS records + *

A {@code RecordSet} is the unit of data that will be returned by the DNS servers upon a DNS + * request for a specific domain. The {@code RecordSet} holds the current state of the DNS records * that make up a zone. You can read the records but you cannot modify them directly. Rather, you - * edit the records in a zone by creating a ChangeRequest. + * edit the records in a zone by creating a {@link ChangeRequest}. * * @see Google Cloud DNS * documentation */ -public class DnsRecord implements Serializable { +public class RecordSet implements Serializable { - static final Function FROM_PB_FUNCTION = - new Function() { + static final Function FROM_PB_FUNCTION = + new Function() { @Override - public DnsRecord apply(com.google.api.services.dns.model.ResourceRecordSet pb) { - return DnsRecord.fromPb(pb); + public RecordSet apply(ResourceRecordSet pb) { + return RecordSet.fromPb(pb); } }; - static final Function TO_PB_FUNCTION = - new Function() { + static final Function TO_PB_FUNCTION = + new Function() { @Override - public com.google.api.services.dns.model.ResourceRecordSet apply(DnsRecord error) { - return error.toPb(); + public ResourceRecordSet apply(RecordSet recordSet) { + return recordSet.toPb(); } }; private static final long serialVersionUID = 8148009870800115261L; @@ -124,7 +124,7 @@ public enum Type { } /** - * A builder of {@link DnsRecord}. + * A builder for {@link RecordSet}. */ public static class Builder { @@ -140,9 +140,9 @@ private Builder(String name, Type type) { /** * Creates a builder and pre-populates attributes with the values from the provided {@code - * DnsRecord} instance. + * RecordSet} instance. */ - private Builder(DnsRecord record) { + private Builder(RecordSet record) { this.name = record.name; this.ttl = record.ttl; this.type = record.type; @@ -186,7 +186,7 @@ public Builder records(List records) { } /** - * Sets name for this DNS record set. For example, www.example.com. + * Sets the name for this record set. For example, www.example.com. */ public Builder name(String name) { this.name = checkNotNull(name); @@ -198,7 +198,7 @@ public Builder name(String name) { * The maximum duration must be equivalent to at most {@link Integer#MAX_VALUE} seconds. * * @param duration A non-negative number of time units - * @param unit The unit of the ttl parameter + * @param unit The unit of the ttl parameter */ public Builder ttl(int duration, TimeUnit unit) { checkArgument(duration >= 0, @@ -219,14 +219,14 @@ public Builder type(Type type) { } /** - * Builds the DNS record. + * Builds the record set. */ - public DnsRecord build() { - return new DnsRecord(this); + public RecordSet build() { + return new RecordSet(this); } } - private DnsRecord(Builder builder) { + private RecordSet(Builder builder) { this.name = builder.name; this.rrdatas = ImmutableList.copyOf(builder.rrdatas); this.ttl = builder.ttl; @@ -241,35 +241,35 @@ public Builder toBuilder() { } /** - * Creates a {@code DnsRecord} builder for the given {@code name} and {@code type}. + * Creates a {@code RecordSet} builder for the given {@code name} and {@code type}. */ public static Builder builder(String name, Type type) { return new Builder(name, type); } /** - * Returns the user-assigned name of this DNS record. + * Returns the user-assigned name of this record set. */ public String name() { return name; } /** - * Returns a list of DNS record stored in this record set. + * Returns a list of records stored in this record set. */ public List records() { return rrdatas; } /** - * Returns the number of seconds that this DnsResource can be cached by resolvers. + * Returns the number of seconds that this record set can be cached by resolvers. */ public Integer ttl() { return ttl; } /** - * Returns the type of this DNS record. + * Returns the type of this record set. */ public Type type() { return type; @@ -282,7 +282,7 @@ public int hashCode() { @Override public boolean equals(Object obj) { - return (obj instanceof DnsRecord) && Objects.equals(this.toPb(), ((DnsRecord) obj).toPb()); + return obj instanceof RecordSet && Objects.equals(this.toPb(), ((RecordSet) obj).toPb()); } com.google.api.services.dns.model.ResourceRecordSet toPb() { @@ -295,7 +295,7 @@ com.google.api.services.dns.model.ResourceRecordSet toPb() { return pb; } - static DnsRecord fromPb(com.google.api.services.dns.model.ResourceRecordSet pb) { + static RecordSet fromPb(com.google.api.services.dns.model.ResourceRecordSet pb) { Builder builder = builder(pb.getName(), Type.valueOf(pb.getType())); if (pb.getRrdatas() != null) { builder.records(pb.getRrdatas()); 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 41507647543a..aed99dbd0001 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 @@ -28,10 +28,10 @@ /** * 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 + *

A zone is the container for all of your record sets 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. + * record sets that it can include. * * @see Google Cloud DNS managed zone * documentation @@ -135,14 +135,14 @@ public boolean delete() { } /** - * Lists all {@link DnsRecord}s associated with this zone. The method searches for zone by name. + * Lists all {@link RecordSet}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 + * @param options optional restriction on listing and fields of {@link RecordSet}s returned + * @return a page of record sets * @throws DnsException upon failure or if the zone is not found */ - public Page listDnsRecords(Dns.DnsRecordListOption... options) { - return dns.listDnsRecords(name(), options); + public Page listRecordSets(Dns.RecordSetListOption... options) { + return dns.listRecordSets(name(), options); } /** diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/package-info.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/package-info.java index 8a137285c357..69bee930df62 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/package-info.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/package-info.java @@ -42,7 +42,7 @@ * String zoneName = "my-unique-zone"; * Zone zone = dns.getZone(zoneName); * String ip = "12.13.14.15"; - * DnsRecord toCreate = DnsRecord.builder("www.someexampledomain.com.", DnsRecord.Type.A) + * RecordSet toCreate = RecordSet.builder("www.someexampledomain.com.", RecordSet.Type.A) * .ttl(24, TimeUnit.HOURS) * .addRecord(ip) * .build(); diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/DefaultDnsRpc.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/DefaultDnsRpc.java index f8b8adb87ada..cbebd19d0d73 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/DefaultDnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/DefaultDnsRpc.java @@ -112,7 +112,7 @@ public boolean deleteZone(String zoneName) throws DnsException { } @Override - public ListResult listDnsRecords(String zoneName, Map options) + public ListResult listRecordSets(String zoneName, Map options) throws DnsException { // options are fields, page token, dns name, type try { diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/DnsRpc.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/DnsRpc.java index bde93b99bfdd..c7478016db27 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/DnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/spi/DnsRpc.java @@ -120,13 +120,13 @@ public String pageToken() { boolean deleteZone(String zoneName) throws DnsException; /** - * Lists DNS records for a given zone. + * Lists record sets for a given zone. * * @param zoneName name of the zone to be listed * @param options a map of options for the service call * @throws DnsException upon failure or if zone was not found */ - ListResult listDnsRecords(String zoneName, Map options) + ListResult listRecordSets(String zoneName, Map options) throws DnsException; /** diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/LocalDnsHelper.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/LocalDnsHelper.java index 3b18ec5ce55b..0ae2c37b9b4d 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/LocalDnsHelper.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/LocalDnsHelper.java @@ -90,8 +90,8 @@ * privileges to manipulate any project. Similarly, the local simulation does not require * verification of domain name ownership. Any request for creating a managed zone will be approved. * The mock does not track quota and will allow the user to exceed it. The mock provides only basic - * validation of the DNS data for records of type A and AAAA. It does not validate any other record - * types. + * validation of the DNS data for record sets of type A and AAAA. It does not validate any other + * record set types. */ public class LocalDnsHelper { @@ -470,7 +470,7 @@ static Response toListResponse(List serializedObjects, String context, S } /** - * Prepares DNS records that are created by default for each zone. + * Prepares record sets that are created by default for each zone. */ private static ImmutableSortedMap defaultRecords(ManagedZone zone) { ResourceRecordSet soa = new ResourceRecordSet(); @@ -508,7 +508,7 @@ static List randomNameservers() { } /** - * Returns a hex string id (used for a dns record) unique within the set of ids. + * Returns a hex string id (used for a record set) unique within the set of ids. */ @VisibleForTesting static String getUniqueId(Set ids) { @@ -521,14 +521,14 @@ static String getUniqueId(Set ids) { } /** - * Tests if a DNS record matches name and type (if provided). Used for filtering. + * Tests if a record set matches name and type (if provided). Used for filtering. */ @VisibleForTesting - static boolean matchesCriteria(ResourceRecordSet record, String name, String type) { - if (type != null && !record.getType().equals(type)) { + static boolean matchesCriteria(ResourceRecordSet recordSet, String name, String type) { + if (type != null && !recordSet.getType().equals(type)) { return false; } - return name == null || record.getName().equals(name); + return name == null || recordSet.getName().equals(name); } /** @@ -871,7 +871,7 @@ Response listZones(String projectId, String query) { } /** - * Lists DNS records for a zone. Next page token is the ID of the last record listed. + * Lists record sets for a zone. Next page token is the ID of the last record listed. */ @VisibleForTesting Response listDnsRecords(String projectId, String zoneName, String query) { @@ -898,18 +898,18 @@ Response listDnsRecords(String projectId, String zoneName, String query) { boolean hasMorePages = false; LinkedList serializedRrsets = new LinkedList<>(); String lastRecordId = null; - for (String recordId : fragment.keySet()) { - ResourceRecordSet record = fragment.get(recordId); - if (matchesCriteria(record, name, type)) { + for (String recordSetId : fragment.keySet()) { + ResourceRecordSet recordSet = fragment.get(recordSetId); + if (matchesCriteria(recordSet, name, type)) { if (sizeReached) { // we do not add this, just note that there would be more and there should be a token hasMorePages = true; break; } else { - lastRecordId = recordId; + lastRecordId = recordSetId; try { serializedRrsets.addLast(jsonFactory.toString( - OptionParsers.extractFields(record, fields))); + OptionParsers.extractFields(recordSet, fields))); } catch (IOException e) { return Error.INTERNAL_ERROR.response(String.format( "Error when serializing resource record set in managed zone %s in project %s", @@ -1128,8 +1128,8 @@ static Response checkRrset(ResourceRecordSet rrset, ZoneContainer zone, int inde } /** - * Checks against duplicate additions (for each record to be added that already exists, we must - * have a matching deletion. Furthermore, check that mandatory SOA and NS records stay. + * Checks against duplicate additions (for each record set to be added that already exists, we + * must have a matching deletion. Furthermore, check that mandatory SOA and NS records stay. */ static Response checkAdditionsDeletions(List additions, List deletions, ZoneContainer zone) { @@ -1139,7 +1139,7 @@ static Response checkAdditionsDeletions(List additions, for (ResourceRecordSet wrappedRrset : zone.dnsRecords().get().values()) { if (rrset.getName().equals(wrappedRrset.getName()) && rrset.getType().equals(wrappedRrset.getType()) - // such a record exist and we must have a deletion + // such a record set exists and we must have a deletion && (deletions == null || !deletions.contains(wrappedRrset))) { return Error.ALREADY_EXISTS.response(String.format( "The 'entity.change.additions[%s]' resource named '%s (%s)' already exists.", @@ -1181,10 +1181,10 @@ static Response checkAdditionsDeletions(List additions, /** * Helper for searching rrsets in a collection. */ - private static ResourceRecordSet findByNameAndType(Iterable records, + private static ResourceRecordSet findByNameAndType(Iterable recordSets, String name, String type) { - if (records != null) { - for (ResourceRecordSet rrset : records) { + if (recordSets != null) { + for (ResourceRecordSet rrset : recordSets) { if ((name == null || name.equals(rrset.getName())) && (type == null || type.equals(rrset.getType()))) { return rrset; @@ -1195,7 +1195,7 @@ private static ResourceRecordSet findByNameAndType(Iterable r } /** - * We only provide the most basic validation for A and AAAA records. + * We only provide the most basic validation for A and AAAA record sets. */ static boolean checkRrData(String data, String type) { switch (type) { @@ -1233,7 +1233,7 @@ static Response checkListOptions(Map options) { return Error.INVALID.response(String.format( "Invalid value for 'parameters.dnsName': '%s'", dnsName)); } - // for listing dns records, name must be fully qualified + // for listing record sets, name must be fully qualified String name = (String) options.get("name"); if (name != null && !name.endsWith(".")) { return Error.INVALID.response(String.format( diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/OptionParsers.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/OptionParsers.java index ecd7e8179efe..578a0b52db3d 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/OptionParsers.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/OptionParsers.java @@ -166,26 +166,26 @@ static ResourceRecordSet extractFields(ResourceRecordSet fullRecord, String... f if (fields == null || fields.length == 0) { return fullRecord; } - ResourceRecordSet record = new ResourceRecordSet(); + ResourceRecordSet recordSet = new ResourceRecordSet(); for (String field : fields) { switch (field) { case "name": - record.setName(fullRecord.getName()); + recordSet.setName(fullRecord.getName()); break; case "rrdatas": - record.setRrdatas(fullRecord.getRrdatas()); + recordSet.setRrdatas(fullRecord.getRrdatas()); break; case "type": - record.setType(fullRecord.getType()); + recordSet.setType(fullRecord.getType()); break; case "ttl": - record.setTtl(fullRecord.getTtl()); + recordSet.setTtl(fullRecord.getTtl()); break; default: break; } } - return record; + return recordSet; } static Map parseListChangesOptions(String query) { diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java index 8b40a4dcff34..fe726acb7c10 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java @@ -35,16 +35,16 @@ public class ChangeRequestTest { private static final Long START_TIME_MILLIS = 12334567890L; private static final ChangeRequest.Status STATUS = ChangeRequest.Status.PENDING; private static final String NAME1 = "dns1"; - private static final DnsRecord.Type TYPE1 = DnsRecord.Type.A; + private static final RecordSet.Type TYPE1 = RecordSet.Type.A; private static final String NAME2 = "dns2"; - private static final DnsRecord.Type TYPE2 = DnsRecord.Type.AAAA; + private static final RecordSet.Type TYPE2 = RecordSet.Type.AAAA; private static final String NAME3 = "dns3"; - private static final DnsRecord.Type TYPE3 = DnsRecord.Type.MX; - private static final DnsRecord RECORD1 = DnsRecord.builder(NAME1, TYPE1).build(); - private static final DnsRecord RECORD2 = DnsRecord.builder(NAME2, TYPE2).build(); - private static final DnsRecord RECORD3 = DnsRecord.builder(NAME3, TYPE3).build(); - private static final List ADDITIONS = ImmutableList.of(RECORD1, RECORD2); - private static final List DELETIONS = ImmutableList.of(RECORD3); + private static final RecordSet.Type TYPE3 = RecordSet.Type.MX; + private static final RecordSet RECORD1 = RecordSet.builder(NAME1, TYPE1).build(); + private static final RecordSet RECORD2 = RecordSet.builder(NAME2, TYPE2).build(); + private static final RecordSet RECORD3 = RecordSet.builder(NAME3, TYPE3).build(); + private static final List ADDITIONS = ImmutableList.of(RECORD1, RECORD2); + private static final List DELETIONS = ImmutableList.of(RECORD3); private static final ChangeRequest CHANGE = ChangeRequest.builder() .add(RECORD1) .add(RECORD2) @@ -70,7 +70,7 @@ public void testBuilder() { assertEquals(START_TIME_MILLIS, CHANGE.startTimeMillis()); assertEquals(ADDITIONS, CHANGE.additions()); assertEquals(DELETIONS, CHANGE.deletions()); - List recordList = ImmutableList.of(RECORD1); + List recordList = ImmutableList.of(RECORD1); ChangeRequest another = CHANGE.toBuilder().additions(recordList).build(); assertEquals(recordList, another.additions()); assertEquals(CHANGE.deletions(), another.deletions()); @@ -160,7 +160,7 @@ public void testClearAdditions() { public void testAddAddition() { try { CHANGE.toBuilder().add(null); - fail("Should not be able to add null DnsRecord."); + fail("Should not be able to add null RecordSet."); } catch (NullPointerException e) { // expected } @@ -172,7 +172,7 @@ public void testAddAddition() { public void testAddDeletion() { try { CHANGE.toBuilder().delete(null); - fail("Should not be able to delete null DnsRecord."); + fail("Should not be able to delete null RecordSet."); } catch (NullPointerException e) { // expected } diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java index a97c9c408036..ab2dba0a566c 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java @@ -21,7 +21,6 @@ import com.google.api.services.dns.model.Change; import com.google.api.services.dns.model.ManagedZone; -import com.google.api.services.dns.model.ResourceRecordSet; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; @@ -46,10 +45,10 @@ public class DnsImplTest { private static final String DNS_NAME = "example.com."; private static final String DESCRIPTION = "desc"; private static final String CHANGE_ID = "some change id"; - private static final DnsRecord DNS_RECORD1 = DnsRecord.builder("Something", DnsRecord.Type.AAAA) - .build(); - private static final DnsRecord DNS_RECORD2 = DnsRecord.builder("Different", DnsRecord.Type.AAAA) - .build(); + private static final RecordSet DNS_RECORD1 = + RecordSet.builder("Something", RecordSet.Type.AAAA).build(); + private static final RecordSet DNS_RECORD2 = + RecordSet.builder("Different", RecordSet.Type.AAAA).build(); private static final Integer MAX_SIZE = 20; private static final String PAGE_TOKEN = "some token"; private static final ZoneInfo ZONE_INFO = ZoneInfo.of(ZONE_NAME, DNS_NAME, DESCRIPTION); @@ -70,7 +69,8 @@ public class DnsImplTest { CHANGE_REQUEST_PARTIAL.toPb())); private static final DnsRpc.ListResult LIST_RESULT_OF_PB_ZONES = DnsRpc.ListResult.of("cursor", ImmutableList.of(ZONE_INFO.toPb())); - private static final DnsRpc.ListResult LIST_OF_PB_DNS_RECORDS = + private static final DnsRpc.ListResult + LIST_OF_PB_DNS_RECORDS = DnsRpc.ListResult.of("cursor", ImmutableList.of(DNS_RECORD1.toPb(), DNS_RECORD2.toPb())); // Field options @@ -91,12 +91,12 @@ public class DnsImplTest { Dns.ChangeRequestListOption.pageToken(PAGE_TOKEN), Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.STATUS), Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING)}; - private static final Dns.DnsRecordListOption[] DNS_RECORD_LIST_OPTIONS = { - Dns.DnsRecordListOption.pageSize(MAX_SIZE), - Dns.DnsRecordListOption.pageToken(PAGE_TOKEN), - Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TTL), - Dns.DnsRecordListOption.dnsName(DNS_NAME), - Dns.DnsRecordListOption.type(DnsRecord.Type.AAAA)}; + private static final Dns.RecordSetListOption[] DNS_RECORD_LIST_OPTIONS = { + Dns.RecordSetListOption.pageSize(MAX_SIZE), + Dns.RecordSetListOption.pageToken(PAGE_TOKEN), + Dns.RecordSetListOption.fields(Dns.RecordSetField.TTL), + Dns.RecordSetListOption.dnsName(DNS_NAME), + Dns.RecordSetListOption.type(RecordSet.Type.AAAA)}; // Other private static final Map EMPTY_RPC_OPTIONS = ImmutableMap.of(); @@ -338,11 +338,11 @@ public void testListZonesWithOptions() { @Test public void testListDnsRecords() { - EasyMock.expect(dnsRpcMock.listDnsRecords(ZONE_INFO.name(), EMPTY_RPC_OPTIONS)) + EasyMock.expect(dnsRpcMock.listRecordSets(ZONE_INFO.name(), EMPTY_RPC_OPTIONS)) .andReturn(LIST_OF_PB_DNS_RECORDS); EasyMock.replay(dnsRpcMock); dns = options.service(); // creates DnsImpl - Page dnsPage = dns.listDnsRecords(ZONE_INFO.name()); + Page dnsPage = dns.listRecordSets(ZONE_INFO.name()); assertEquals(2, Lists.newArrayList(dnsPage.values()).size()); assertTrue(Lists.newArrayList(dnsPage.values()).contains(DNS_RECORD1)); assertTrue(Lists.newArrayList(dnsPage.values()).contains(DNS_RECORD2)); @@ -351,11 +351,11 @@ public void testListDnsRecords() { @Test public void testListDnsRecordsWithOptions() { Capture> capturedOptions = Capture.newInstance(); - EasyMock.expect(dnsRpcMock.listDnsRecords(EasyMock.eq(ZONE_NAME), + EasyMock.expect(dnsRpcMock.listRecordSets(EasyMock.eq(ZONE_NAME), EasyMock.capture(capturedOptions))).andReturn(LIST_OF_PB_DNS_RECORDS); EasyMock.replay(dnsRpcMock); dns = options.service(); // creates DnsImpl - Page dnsPage = dns.listDnsRecords(ZONE_NAME, DNS_RECORD_LIST_OPTIONS); + Page dnsPage = dns.listRecordSets(ZONE_NAME, DNS_RECORD_LIST_OPTIONS); assertEquals(2, Lists.newArrayList(dnsPage.values()).size()); assertTrue(Lists.newArrayList(dnsPage.values()).contains(DNS_RECORD1)); assertTrue(Lists.newArrayList(dnsPage.values()).contains(DNS_RECORD2)); @@ -365,8 +365,8 @@ public void testListDnsRecordsWithOptions() { .get(DNS_RECORD_LIST_OPTIONS[1].rpcOption()); assertEquals(PAGE_TOKEN, selector); selector = (String) capturedOptions.getValue().get(DNS_RECORD_LIST_OPTIONS[2].rpcOption()); - assertTrue(selector.contains(Dns.DnsRecordField.NAME.selector())); - assertTrue(selector.contains(Dns.DnsRecordField.TTL.selector())); + assertTrue(selector.contains(Dns.RecordSetField.NAME.selector())); + assertTrue(selector.contains(Dns.RecordSetField.TTL.selector())); selector = (String) capturedOptions.getValue().get(DNS_RECORD_LIST_OPTIONS[3].rpcOption()); assertEquals(DNS_RECORD_LIST_OPTIONS[3].value(), selector); String type = (String) capturedOptions.getValue().get(DNS_RECORD_LIST_OPTIONS[4] 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 2e233e2df62a..df86d6ebd495 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 @@ -30,36 +30,36 @@ public class DnsTest { private static final String DNS_NAME = "www.example.com."; @Test - public void testDnsRecordListOption() { + public void testRecordSetListOption() { // dns name String dnsName = "some name"; - Dns.DnsRecordListOption dnsRecordListOption = Dns.DnsRecordListOption.dnsName(dnsName); - assertEquals(dnsName, dnsRecordListOption.value()); - assertEquals(DnsRpc.Option.NAME, dnsRecordListOption.rpcOption()); + Dns.RecordSetListOption recordSetListOption = Dns.RecordSetListOption.dnsName(dnsName); + assertEquals(dnsName, recordSetListOption.value()); + assertEquals(DnsRpc.Option.NAME, recordSetListOption.rpcOption()); // page token - dnsRecordListOption = Dns.DnsRecordListOption.pageToken(PAGE_TOKEN); - assertEquals(PAGE_TOKEN, dnsRecordListOption.value()); - assertEquals(DnsRpc.Option.PAGE_TOKEN, dnsRecordListOption.rpcOption()); + recordSetListOption = Dns.RecordSetListOption.pageToken(PAGE_TOKEN); + assertEquals(PAGE_TOKEN, recordSetListOption.value()); + assertEquals(DnsRpc.Option.PAGE_TOKEN, recordSetListOption.rpcOption()); // page size - dnsRecordListOption = Dns.DnsRecordListOption.pageSize(PAGE_SIZE); - assertEquals(PAGE_SIZE, dnsRecordListOption.value()); - assertEquals(DnsRpc.Option.PAGE_SIZE, dnsRecordListOption.rpcOption()); + recordSetListOption = Dns.RecordSetListOption.pageSize(PAGE_SIZE); + assertEquals(PAGE_SIZE, recordSetListOption.value()); + assertEquals(DnsRpc.Option.PAGE_SIZE, recordSetListOption.rpcOption()); // record type - DnsRecord.Type recordType = DnsRecord.Type.AAAA; - dnsRecordListOption = Dns.DnsRecordListOption.type(recordType); - assertEquals(recordType.name(), dnsRecordListOption.value()); - assertEquals(DnsRpc.Option.DNS_TYPE, dnsRecordListOption.rpcOption()); + RecordSet.Type recordType = RecordSet.Type.AAAA; + recordSetListOption = Dns.RecordSetListOption.type(recordType); + assertEquals(recordType.name(), recordSetListOption.value()); + assertEquals(DnsRpc.Option.DNS_TYPE, recordSetListOption.rpcOption()); // fields - dnsRecordListOption = Dns.DnsRecordListOption.fields(Dns.DnsRecordField.NAME, - Dns.DnsRecordField.TTL); - assertEquals(DnsRpc.Option.FIELDS, dnsRecordListOption.rpcOption()); - assertTrue(dnsRecordListOption.value() instanceof String); - assertTrue(((String) dnsRecordListOption.value()).contains( - Dns.DnsRecordField.NAME.selector())); - assertTrue(((String) dnsRecordListOption.value()).contains( - Dns.DnsRecordField.TTL.selector())); - assertTrue(((String) dnsRecordListOption.value()).contains( - Dns.DnsRecordField.NAME.selector())); + recordSetListOption = Dns.RecordSetListOption.fields(Dns.RecordSetField.NAME, + Dns.RecordSetField.TTL); + assertEquals(DnsRpc.Option.FIELDS, recordSetListOption.rpcOption()); + assertTrue(recordSetListOption.value() instanceof String); + assertTrue(((String) recordSetListOption.value()).contains( + Dns.RecordSetField.NAME.selector())); + assertTrue(((String) recordSetListOption.value()).contains( + Dns.RecordSetField.TTL.selector())); + assertTrue(((String) recordSetListOption.value()).contains( + Dns.RecordSetField.NAME.selector())); } @Test diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsRecordTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/RecordSetTest.java similarity index 63% rename from gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsRecordTest.java rename to gcloud-java-dns/src/test/java/com/google/gcloud/dns/RecordSetTest.java index 5fc972cede78..369e078a48c7 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsRecordTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/RecordSetTest.java @@ -16,7 +16,7 @@ package com.google.gcloud.dns; -import static com.google.gcloud.dns.DnsRecord.builder; +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; @@ -26,35 +26,35 @@ import java.util.concurrent.TimeUnit; -public class DnsRecordTest { +public class RecordSetTest { private static final String NAME = "example.com."; private static final Integer TTL = 3600; private static final TimeUnit UNIT = TimeUnit.HOURS; private static final Integer UNIT_TTL = 1; - private static final DnsRecord.Type TYPE = DnsRecord.Type.AAAA; - private static final DnsRecord record = builder(NAME, TYPE) + private static final RecordSet.Type TYPE = RecordSet.Type.AAAA; + private static final RecordSet recordSet = builder(NAME, TYPE) .ttl(UNIT_TTL, UNIT) .build(); @Test public void testDefaultDnsRecord() { - DnsRecord record = builder(NAME, TYPE).build(); - assertEquals(0, record.records().size()); - assertEquals(TYPE, record.type()); - assertEquals(NAME, record.name()); + RecordSet recordSet = builder(NAME, TYPE).build(); + assertEquals(0, recordSet.records().size()); + assertEquals(TYPE, recordSet.type()); + assertEquals(NAME, recordSet.name()); } @Test public void testBuilder() { - assertEquals(NAME, record.name()); - assertEquals(TTL, record.ttl()); - assertEquals(TYPE, record.type()); - assertEquals(0, record.records().size()); + assertEquals(NAME, recordSet.name()); + assertEquals(TTL, recordSet.ttl()); + assertEquals(TYPE, recordSet.type()); + assertEquals(0, recordSet.records().size()); // verify that one can add records to the record set - String testingRecord = "Testing record"; - String anotherTestingRecord = "Another record 123"; - DnsRecord anotherRecord = record.toBuilder() + String testingRecord = "Testing recordSet"; + String anotherTestingRecord = "Another recordSet 123"; + RecordSet anotherRecord = recordSet.toBuilder() .addRecord(testingRecord) .addRecord(anotherTestingRecord) .build(); @@ -79,47 +79,47 @@ public void testValidTtl() { } catch (IllegalArgumentException e) { // expected } - DnsRecord record = DnsRecord.builder(NAME, TYPE).ttl(UNIT_TTL, UNIT).build(); + RecordSet record = RecordSet.builder(NAME, TYPE).ttl(UNIT_TTL, UNIT).build(); assertEquals(TTL, record.ttl()); } @Test public void testEqualsAndNotEquals() { - DnsRecord clone = record.toBuilder().build(); - assertEquals(record, clone); - clone = record.toBuilder().addRecord("another record").build(); - assertNotEquals(record, clone); + RecordSet clone = recordSet.toBuilder().build(); + assertEquals(recordSet, clone); + clone = recordSet.toBuilder().addRecord("another recordSet").build(); + assertNotEquals(recordSet, clone); String differentName = "totally different name"; - clone = record.toBuilder().name(differentName).build(); - assertNotEquals(record, clone); - clone = record.toBuilder().ttl(record.ttl() + 1, TimeUnit.SECONDS).build(); - assertNotEquals(record, clone); - clone = record.toBuilder().type(DnsRecord.Type.TXT).build(); - assertNotEquals(record, clone); + clone = recordSet.toBuilder().name(differentName).build(); + assertNotEquals(recordSet, clone); + clone = recordSet.toBuilder().ttl(recordSet.ttl() + 1, TimeUnit.SECONDS).build(); + assertNotEquals(recordSet, clone); + clone = recordSet.toBuilder().type(RecordSet.Type.TXT).build(); + assertNotEquals(recordSet, clone); } @Test public void testSameHashCodeOnEquals() { - int hash = record.hashCode(); - DnsRecord clone = record.toBuilder().build(); + int hash = recordSet.hashCode(); + RecordSet clone = recordSet.toBuilder().build(); assertEquals(clone.hashCode(), hash); } @Test public void testToAndFromPb() { - assertEquals(record, DnsRecord.fromPb(record.toPb())); - DnsRecord partial = builder(NAME, TYPE).build(); - assertEquals(partial, DnsRecord.fromPb(partial.toPb())); + assertEquals(recordSet, RecordSet.fromPb(recordSet.toPb())); + RecordSet partial = builder(NAME, TYPE).build(); + assertEquals(partial, RecordSet.fromPb(partial.toPb())); partial = builder(NAME, TYPE).addRecord("test").build(); - assertEquals(partial, DnsRecord.fromPb(partial.toPb())); + assertEquals(partial, RecordSet.fromPb(partial.toPb())); partial = builder(NAME, TYPE).ttl(15, TimeUnit.SECONDS).build(); - assertEquals(partial, DnsRecord.fromPb(partial.toPb())); + assertEquals(partial, RecordSet.fromPb(partial.toPb())); } @Test public void testToBuilder() { - assertEquals(record, record.toBuilder().build()); - DnsRecord partial = builder(NAME, TYPE).build(); + assertEquals(recordSet, recordSet.toBuilder().build()); + RecordSet partial = builder(NAME, TYPE).build(); assertEquals(partial, partial.toBuilder().build()); partial = builder(NAME, TYPE).addRecord("test").build(); assertEquals(partial, partial.toBuilder().build()); @@ -130,7 +130,8 @@ public void testToBuilder() { @Test public void clearRecordSet() { // make sure that we are starting not empty - DnsRecord clone = record.toBuilder().addRecord("record").addRecord("another").build(); + RecordSet clone = + recordSet.toBuilder().addRecord("record").addRecord("another").build(); assertNotEquals(0, clone.records().size()); clone = clone.toBuilder().clearRecords().build(); assertEquals(0, clone.records().size()); @@ -141,7 +142,7 @@ public void clearRecordSet() { public void removeFromRecordSet() { String recordString = "record"; // make sure that we are starting not empty - DnsRecord clone = record.toBuilder().addRecord(recordString).build(); + RecordSet clone = recordSet.toBuilder().addRecord(recordString).build(); assertNotEquals(0, clone.records().size()); clone = clone.toBuilder().removeRecord(recordString).build(); assertEquals(0, clone.records().size()); diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/SerializationTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/SerializationTest.java index 7a0c32036878..c06cd096bf1e 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/SerializationTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/SerializationTest.java @@ -45,8 +45,8 @@ public class SerializationTest extends BaseSerializationTest { .build(); private static final Dns.ZoneListOption ZONE_LIST_OPTION = Dns.ZoneListOption.dnsName("www.example.com."); - private static final Dns.DnsRecordListOption DNS_REOCRD_LIST_OPTION = - Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TTL); + private static final Dns.RecordSetListOption RECORD_SET_LIST_OPTION = + Dns.RecordSetListOption.fields(Dns.RecordSetField.TTL); private static final Dns.ChangeRequestListOption CHANGE_REQUEST_LIST_OPTION = Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.STATUS); private static final Dns.ZoneOption ZONE_OPTION = @@ -64,16 +64,16 @@ public class SerializationTest extends BaseSerializationTest { private static final Zone PARTIAL_ZONE = new Zone(DNS, new ZoneInfo.BuilderImpl(PARTIAL_ZONE_INFO)); private static final ChangeRequest CHANGE_REQUEST_PARTIAL = ChangeRequest.builder().build(); - private static final DnsRecord DNS_RECORD_PARTIAL = - DnsRecord.builder("www.www.com", DnsRecord.Type.AAAA).build(); - private static final DnsRecord DNS_RECORD_COMPLETE = - DnsRecord.builder("www.sadfa.com", DnsRecord.Type.A) + private static final RecordSet RECORD_SET_PARTIAL = + RecordSet.builder("www.www.com", RecordSet.Type.AAAA).build(); + private static final RecordSet RECORD_SET_COMPLETE = + RecordSet.builder("www.sadfa.com", RecordSet.Type.A) .ttl(12, TimeUnit.HOURS) .addRecord("record") .build(); private static final ChangeRequest CHANGE_REQUEST_COMPLETE = ChangeRequest.builder() - .add(DNS_RECORD_COMPLETE) - .delete(DNS_RECORD_PARTIAL) + .add(RECORD_SET_COMPLETE) + .delete(RECORD_SET_PARTIAL) .status(ChangeRequest.Status.PENDING) .id("some id") .startTimeMillis(132L) @@ -89,9 +89,9 @@ protected Serializable[] serializableObjects() { .authCredentials(null) .build(); return new Serializable[]{FULL_ZONE_INFO, PARTIAL_ZONE_INFO, ZONE_LIST_OPTION, - DNS_REOCRD_LIST_OPTION, CHANGE_REQUEST_LIST_OPTION, ZONE_OPTION, CHANGE_REQUEST_OPTION, + RECORD_SET_LIST_OPTION, CHANGE_REQUEST_LIST_OPTION, ZONE_OPTION, CHANGE_REQUEST_OPTION, PROJECT_OPTION, PARTIAL_PROJECT_INFO, FULL_PROJECT_INFO, OPTIONS, FULL_ZONE, PARTIAL_ZONE, - OPTIONS, CHANGE_REQUEST_PARTIAL, DNS_RECORD_PARTIAL, DNS_RECORD_COMPLETE, + OPTIONS, CHANGE_REQUEST_PARTIAL, RECORD_SET_PARTIAL, RECORD_SET_COMPLETE, CHANGE_REQUEST_COMPLETE, options, otherOptions}; } 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 759c34fc1167..bd59f8c140e9 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 @@ -50,12 +50,12 @@ public class ZoneTest { .build(); private static final ZoneInfo NO_ID_INFO = ZoneInfo.of(ZONE_NAME, "another-example.com", "description").toBuilder() - .creationTimeMillis(893123464L) - .build(); + .creationTimeMillis(893123464L) + .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.RecordSetListOption DNS_RECORD_OPTIONS = + Dns.RecordSetListOption.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 = @@ -120,51 +120,51 @@ public void deleteByNameAndNotFound() { @Test public void listDnsRecordsByNameAndFound() { @SuppressWarnings("unchecked") - Page pageMock = createStrictMock(Page.class); + Page pageMock = createStrictMock(Page.class); replay(pageMock); - expect(dns.listDnsRecords(ZONE_NAME)).andReturn(pageMock); - expect(dns.listDnsRecords(ZONE_NAME)).andReturn(pageMock); + expect(dns.listRecordSets(ZONE_NAME)).andReturn(pageMock); + expect(dns.listRecordSets(ZONE_NAME)).andReturn(pageMock); // again for options - expect(dns.listDnsRecords(ZONE_NAME, DNS_RECORD_OPTIONS)).andReturn(pageMock); - expect(dns.listDnsRecords(ZONE_NAME, DNS_RECORD_OPTIONS)).andReturn(pageMock); + expect(dns.listRecordSets(ZONE_NAME, DNS_RECORD_OPTIONS)).andReturn(pageMock); + expect(dns.listRecordSets(ZONE_NAME, DNS_RECORD_OPTIONS)).andReturn(pageMock); replay(dns); - Page result = zone.listDnsRecords(); + Page result = zone.listRecordSets(); assertSame(pageMock, result); - result = zoneNoId.listDnsRecords(); + result = zoneNoId.listRecordSets(); assertSame(pageMock, result); verify(pageMock); - zone.listDnsRecords(DNS_RECORD_OPTIONS); // check options - zoneNoId.listDnsRecords(DNS_RECORD_OPTIONS); // check options + zone.listRecordSets(DNS_RECORD_OPTIONS); // check options + zoneNoId.listRecordSets(DNS_RECORD_OPTIONS); // check options } @Test public void listDnsRecordsByNameAndNotFound() { - expect(dns.listDnsRecords(ZONE_NAME)).andThrow(EXCEPTION); - expect(dns.listDnsRecords(ZONE_NAME)).andThrow(EXCEPTION); + expect(dns.listRecordSets(ZONE_NAME)).andThrow(EXCEPTION); + expect(dns.listRecordSets(ZONE_NAME)).andThrow(EXCEPTION); // again for options - expect(dns.listDnsRecords(ZONE_NAME, DNS_RECORD_OPTIONS)).andThrow(EXCEPTION); - expect(dns.listDnsRecords(ZONE_NAME, DNS_RECORD_OPTIONS)).andThrow(EXCEPTION); + expect(dns.listRecordSets(ZONE_NAME, DNS_RECORD_OPTIONS)).andThrow(EXCEPTION); + expect(dns.listRecordSets(ZONE_NAME, DNS_RECORD_OPTIONS)).andThrow(EXCEPTION); replay(dns); try { - zoneNoId.listDnsRecords(); + zoneNoId.listRecordSets(); fail("Parent container not found, should throw an exception."); } catch (DnsException e) { // expected } try { - zone.listDnsRecords(); + zone.listRecordSets(); fail("Parent container not found, should throw an exception."); } catch (DnsException e) { // expected } try { - zoneNoId.listDnsRecords(DNS_RECORD_OPTIONS); // check options + zoneNoId.listRecordSets(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 + zone.listRecordSets(DNS_RECORD_OPTIONS); // check options fail("Parent container not found, should throw an exception."); } catch (DnsException e) { // expected diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java index bfea46dfe039..8f7626a5ae0a 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java @@ -29,8 +29,8 @@ import com.google.gcloud.dns.Dns; import com.google.gcloud.dns.DnsException; import com.google.gcloud.dns.DnsOptions; -import com.google.gcloud.dns.DnsRecord; import com.google.gcloud.dns.ProjectInfo; +import com.google.gcloud.dns.RecordSet; import com.google.gcloud.dns.Zone; import com.google.gcloud.dns.ZoneInfo; @@ -66,13 +66,13 @@ public class ITDnsTest { ZoneInfo.of(ZONE_NAME_TOO_LONG, ZONE_DNS_NAME1, ZONE_DESCRIPTION1); private static final ZoneInfo ZONE_DNS_NO_PERIOD = ZoneInfo.of(ZONE_NAME1, ZONE_DNS_NAME_NO_PERIOD, ZONE_DESCRIPTION1); - private static final DnsRecord A_RECORD_ZONE1 = - DnsRecord.builder("www." + ZONE1.dnsName(), DnsRecord.Type.A) + private static final RecordSet A_RECORD_ZONE1 = + RecordSet.builder("www." + ZONE1.dnsName(), RecordSet.Type.A) .records(ImmutableList.of("123.123.55.1")) .ttl(25, TimeUnit.SECONDS) .build(); - private static final DnsRecord AAAA_RECORD_ZONE1 = - DnsRecord.builder("www." + ZONE1.dnsName(), DnsRecord.Type.AAAA) + private static final RecordSet AAAA_RECORD_ZONE1 = + RecordSet.builder("www." + ZONE1.dnsName(), RecordSet.Type.AAAA) .records(ImmutableList.of("ed:ed:12:aa:36:3:3:105")) .ttl(25, TimeUnit.SECONDS) .build(); @@ -98,12 +98,13 @@ private static void clear() { while (iterator.hasNext()) { waitForChangeToComplete(zoneName, iterator.next().id()); } - Iterator recordIterator = zone.listDnsRecords().iterateAll(); - List toDelete = new LinkedList<>(); - while (recordIterator.hasNext()) { - DnsRecord record = recordIterator.next(); - if (!ImmutableList.of(DnsRecord.Type.NS, DnsRecord.Type.SOA).contains(record.type())) { - toDelete.add(record); + Iterator recordSetIterator = zone.listRecordSets().iterateAll(); + List toDelete = new LinkedList<>(); + while (recordSetIterator.hasNext()) { + RecordSet recordSet = recordSetIterator.next(); + if (!ImmutableList.of(RecordSet.Type.NS, RecordSet.Type.SOA) + .contains(recordSet.type())) { + toDelete.add(recordSet); } } if (!toDelete.isEmpty()) { @@ -587,41 +588,42 @@ public void testCreateChange() { @Test public void testInvalidChangeRequest() { Zone zone = DNS.create(ZONE1); - DnsRecord validA = DnsRecord.builder("subdomain." + zone.dnsName(), DnsRecord.Type.A) - .records(ImmutableList.of("0.255.1.5")) - .build(); + RecordSet validA = + RecordSet.builder("subdomain." + zone.dnsName(), RecordSet.Type.A) + .records(ImmutableList.of("0.255.1.5")) + .build(); try { ChangeRequest validChange = ChangeRequest.builder().add(validA).build(); zone.applyChangeRequest(validChange); try { zone.applyChangeRequest(validChange); - fail("Created a record which already exists."); + fail("Created a record set which already exists."); } catch (DnsException ex) { // expected assertFalse(ex.retryable()); assertEquals(409, ex.code()); } // delete with field mismatch - DnsRecord mismatch = validA.toBuilder().ttl(20, TimeUnit.SECONDS).build(); + RecordSet mismatch = validA.toBuilder().ttl(20, TimeUnit.SECONDS).build(); ChangeRequest deletion = ChangeRequest.builder().delete(mismatch).build(); try { zone.applyChangeRequest(deletion); - fail("Deleted a record without a complete match."); + fail("Deleted a record set without a complete match."); } catch (DnsException ex) { // expected assertEquals(412, ex.code()); assertFalse(ex.retryable()); } // delete and add SOA - Iterator recordIterator = zone.listDnsRecords().iterateAll(); - LinkedList deletions = new LinkedList<>(); - LinkedList additions = new LinkedList<>(); - while (recordIterator.hasNext()) { - DnsRecord record = recordIterator.next(); - if (record.type() == DnsRecord.Type.SOA) { - deletions.add(record); + Iterator recordSetIterator = zone.listRecordSets().iterateAll(); + LinkedList deletions = new LinkedList<>(); + LinkedList additions = new LinkedList<>(); + while (recordSetIterator.hasNext()) { + RecordSet recordSet = recordSetIterator.next(); + if (recordSet.type() == RecordSet.Type.SOA) { + deletions.add(recordSet); // the subdomain is necessary to get 400 instead of 412 - DnsRecord copy = record.toBuilder().name("x." + record.name()).build(); + RecordSet copy = recordSet.toBuilder().name("x." + recordSet.name()).build(); additions.add(copy); break; } @@ -831,92 +833,93 @@ public void testGetProject() { public void testListDnsRecords() { try { Zone zone = DNS.create(ZONE1); - ImmutableList dnsRecords = ImmutableList.copyOf( - DNS.listDnsRecords(zone.name()).iterateAll()); - assertEquals(2, dnsRecords.size()); - ImmutableList defaultRecords = - ImmutableList.of(DnsRecord.Type.NS, DnsRecord.Type.SOA); - for (DnsRecord record : dnsRecords) { - assertTrue(defaultRecords.contains(record.type())); + ImmutableList recordSets = ImmutableList.copyOf( + DNS.listRecordSets(zone.name()).iterateAll()); + assertEquals(2, recordSets.size()); + ImmutableList defaultRecords = + ImmutableList.of(RecordSet.Type.NS, RecordSet.Type.SOA); + for (RecordSet recordSet : recordSets) { + assertTrue(defaultRecords.contains(recordSet.type())); } // field options - Iterator dnsRecordIterator = DNS.listDnsRecords(zone.name(), - Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TTL)).iterateAll(); + Iterator recordSetIterator = DNS.listRecordSets(zone.name(), + Dns.RecordSetListOption.fields(Dns.RecordSetField.TTL)).iterateAll(); int counter = 0; - while (dnsRecordIterator.hasNext()) { - DnsRecord record = dnsRecordIterator.next(); - assertEquals(dnsRecords.get(counter).ttl(), record.ttl()); - assertEquals(dnsRecords.get(counter).name(), record.name()); - assertEquals(dnsRecords.get(counter).type(), record.type()); - assertTrue(record.records().isEmpty()); + while (recordSetIterator.hasNext()) { + RecordSet recordSet = recordSetIterator.next(); + assertEquals(recordSets.get(counter).ttl(), recordSet.ttl()); + assertEquals(recordSets.get(counter).name(), recordSet.name()); + assertEquals(recordSets.get(counter).type(), recordSet.type()); + assertTrue(recordSet.records().isEmpty()); counter++; } assertEquals(2, counter); - dnsRecordIterator = DNS.listDnsRecords(zone.name(), - Dns.DnsRecordListOption.fields(Dns.DnsRecordField.NAME)).iterateAll(); + recordSetIterator = DNS.listRecordSets(zone.name(), + Dns.RecordSetListOption.fields(Dns.RecordSetField.NAME)).iterateAll(); counter = 0; - while (dnsRecordIterator.hasNext()) { - DnsRecord record = dnsRecordIterator.next(); - assertEquals(dnsRecords.get(counter).name(), record.name()); - assertEquals(dnsRecords.get(counter).type(), record.type()); - assertTrue(record.records().isEmpty()); - assertNull(record.ttl()); + while (recordSetIterator.hasNext()) { + RecordSet recordSet = recordSetIterator.next(); + assertEquals(recordSets.get(counter).name(), recordSet.name()); + assertEquals(recordSets.get(counter).type(), recordSet.type()); + assertTrue(recordSet.records().isEmpty()); + assertNull(recordSet.ttl()); counter++; } assertEquals(2, counter); - dnsRecordIterator = DNS.listDnsRecords(zone.name(), - Dns.DnsRecordListOption.fields(Dns.DnsRecordField.DNS_RECORDS)).iterateAll(); + recordSetIterator = DNS.listRecordSets(zone.name(), + Dns.RecordSetListOption.fields(Dns.RecordSetField.DNS_RECORDS)) + .iterateAll(); counter = 0; - while (dnsRecordIterator.hasNext()) { - DnsRecord record = dnsRecordIterator.next(); - assertEquals(dnsRecords.get(counter).records(), record.records()); - assertEquals(dnsRecords.get(counter).name(), record.name()); - assertEquals(dnsRecords.get(counter).type(), record.type()); - assertNull(record.ttl()); + while (recordSetIterator.hasNext()) { + RecordSet recordSet = recordSetIterator.next(); + assertEquals(recordSets.get(counter).records(), recordSet.records()); + assertEquals(recordSets.get(counter).name(), recordSet.name()); + assertEquals(recordSets.get(counter).type(), recordSet.type()); + assertNull(recordSet.ttl()); counter++; } assertEquals(2, counter); - dnsRecordIterator = DNS.listDnsRecords(zone.name(), - Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TYPE), - Dns.DnsRecordListOption.pageSize(1)).iterateAll(); // also test paging + recordSetIterator = DNS.listRecordSets(zone.name(), + Dns.RecordSetListOption.fields(Dns.RecordSetField.TYPE), + Dns.RecordSetListOption.pageSize(1)).iterateAll(); // also test paging counter = 0; - while (dnsRecordIterator.hasNext()) { - DnsRecord record = dnsRecordIterator.next(); - assertEquals(dnsRecords.get(counter).type(), record.type()); - assertEquals(dnsRecords.get(counter).name(), record.name()); - assertTrue(record.records().isEmpty()); - assertNull(record.ttl()); + while (recordSetIterator.hasNext()) { + RecordSet recordSet = recordSetIterator.next(); + assertEquals(recordSets.get(counter).type(), recordSet.type()); + assertEquals(recordSets.get(counter).name(), recordSet.name()); + assertTrue(recordSet.records().isEmpty()); + assertNull(recordSet.ttl()); counter++; } assertEquals(2, counter); // test page size - Page dnsRecordPage = DNS.listDnsRecords(zone.name(), - Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TYPE), - Dns.DnsRecordListOption.pageSize(1)); - assertEquals(1, ImmutableList.copyOf(dnsRecordPage.values().iterator()).size()); + Page recordSetPage = DNS.listRecordSets(zone.name(), + Dns.RecordSetListOption.fields(Dns.RecordSetField.TYPE), + Dns.RecordSetListOption.pageSize(1)); + assertEquals(1, ImmutableList.copyOf(recordSetPage.values().iterator()).size()); // test name filter ChangeRequest change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); waitForChangeToComplete(ZONE1.name(), change.id()); - dnsRecordIterator = DNS.listDnsRecords(ZONE1.name(), - Dns.DnsRecordListOption.dnsName(A_RECORD_ZONE1.name())).iterateAll(); + recordSetIterator = DNS.listRecordSets(ZONE1.name(), + Dns.RecordSetListOption.dnsName(A_RECORD_ZONE1.name())).iterateAll(); counter = 0; - while (dnsRecordIterator.hasNext()) { - DnsRecord record = dnsRecordIterator.next(); + while (recordSetIterator.hasNext()) { + RecordSet recordSet = recordSetIterator.next(); assertTrue(ImmutableList.of(A_RECORD_ZONE1.type(), AAAA_RECORD_ZONE1.type()) - .contains(record.type())); + .contains(recordSet.type())); counter++; } assertEquals(2, counter); // test type filter waitForChangeToComplete(ZONE1.name(), change.id()); - dnsRecordIterator = DNS.listDnsRecords(ZONE1.name(), - Dns.DnsRecordListOption.dnsName(A_RECORD_ZONE1.name()), - Dns.DnsRecordListOption.type(A_RECORD_ZONE1.type())) + recordSetIterator = DNS.listRecordSets(ZONE1.name(), + Dns.RecordSetListOption.dnsName(A_RECORD_ZONE1.name()), + Dns.RecordSetListOption.type(A_RECORD_ZONE1.type())) .iterateAll(); counter = 0; - while (dnsRecordIterator.hasNext()) { - DnsRecord record = dnsRecordIterator.next(); - assertEquals(A_RECORD_ZONE1, record); + while (recordSetIterator.hasNext()) { + RecordSet recordSet = recordSetIterator.next(); + assertEquals(A_RECORD_ZONE1, recordSet); counter++; } assertEquals(1, counter); @@ -924,7 +927,8 @@ public void testListDnsRecords() { // check wrong arguments try { // name is not set - DNS.listDnsRecords(ZONE1.name(), Dns.DnsRecordListOption.type(A_RECORD_ZONE1.type())); + DNS.listRecordSets(ZONE1.name(), + Dns.RecordSetListOption.type(A_RECORD_ZONE1.type())); fail(); } catch (DnsException ex) { // expected @@ -932,7 +936,7 @@ public void testListDnsRecords() { assertFalse(ex.retryable()); } try { - DNS.listDnsRecords(ZONE1.name(), Dns.DnsRecordListOption.pageSize(0)); + DNS.listRecordSets(ZONE1.name(), Dns.RecordSetListOption.pageSize(0)); fail(); } catch (DnsException ex) { // expected @@ -940,7 +944,7 @@ public void testListDnsRecords() { assertFalse(ex.retryable()); } try { - DNS.listDnsRecords(ZONE1.name(), Dns.DnsRecordListOption.pageSize(-1)); + DNS.listRecordSets(ZONE1.name(), Dns.RecordSetListOption.pageSize(-1)); fail(); } catch (DnsException ex) { // expected diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/testing/LocalDnsHelperTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/testing/LocalDnsHelperTest.java index 59002131cc9d..44516f47c657 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/testing/LocalDnsHelperTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/testing/LocalDnsHelperTest.java @@ -131,7 +131,7 @@ public void testCreateZone() { ManagedZone created = RPC.create(ZONE1, EMPTY_RPC_OPTIONS); // check that default records were created DnsRpc.ListResult listResult - = RPC.listDnsRecords(ZONE1.getName(), EMPTY_RPC_OPTIONS); + = RPC.listRecordSets(ZONE1.getName(), EMPTY_RPC_OPTIONS); ImmutableList defaultTypes = ImmutableList.of("SOA", "NS"); Iterator iterator = listResult.results().iterator(); assertTrue(defaultTypes.contains(iterator.next().getType())); @@ -395,7 +395,7 @@ private static void executeCreateAndApplyChangeTest(DnsRpc rpc) { rpc.applyChangeRequest(ZONE1.getName(), CHANGE_KEEP, EMPTY_RPC_OPTIONS); waitForChangeToComplete(rpc, ZONE1.getName(), "3"); Iterable results = - rpc.listDnsRecords(ZONE1.getName(), EMPTY_RPC_OPTIONS).results(); + rpc.listRecordSets(ZONE1.getName(), EMPTY_RPC_OPTIONS).results(); List defaults = ImmutableList.of("SOA", "NS"); boolean rrsetKeep = false; boolean rrset1 = false; @@ -692,7 +692,7 @@ public void testListZones() { public void testListDnsRecords() { // no zone exists try { - RPC.listDnsRecords(ZONE_NAME1, EMPTY_RPC_OPTIONS); + RPC.listRecordSets(ZONE_NAME1, EMPTY_RPC_OPTIONS); fail(); } catch (DnsException ex) { // expected @@ -702,19 +702,19 @@ public void testListDnsRecords() { // zone exists but has no records RPC.create(ZONE1, EMPTY_RPC_OPTIONS); Iterable results = - RPC.listDnsRecords(ZONE_NAME1, EMPTY_RPC_OPTIONS).results(); + RPC.listRecordSets(ZONE_NAME1, EMPTY_RPC_OPTIONS).results(); ImmutableList records = ImmutableList.copyOf(results); assertEquals(2, records.size()); // contains default NS and SOA // zone has records RPC.applyChangeRequest(ZONE_NAME1, CHANGE_KEEP, EMPTY_RPC_OPTIONS); - results = RPC.listDnsRecords(ZONE_NAME1, EMPTY_RPC_OPTIONS).results(); + results = RPC.listRecordSets(ZONE_NAME1, EMPTY_RPC_OPTIONS).results(); records = ImmutableList.copyOf(results); assertEquals(3, records.size()); // error in options Map options = new HashMap<>(); options.put(DnsRpc.Option.PAGE_SIZE, 0); try { - RPC.listDnsRecords(ZONE1.getName(), options); + RPC.listRecordSets(ZONE1.getName(), options); fail(); } catch (DnsException ex) { // expected @@ -723,7 +723,7 @@ public void testListDnsRecords() { } options.put(DnsRpc.Option.PAGE_SIZE, -1); try { - RPC.listDnsRecords(ZONE1.getName(), options); + RPC.listRecordSets(ZONE1.getName(), options); fail(); } catch (DnsException ex) { // expected @@ -731,14 +731,14 @@ public void testListDnsRecords() { assertTrue(ex.getMessage().contains("parameters.maxResults")); } options.put(DnsRpc.Option.PAGE_SIZE, 15); - results = RPC.listDnsRecords(ZONE1.getName(), options).results(); + results = RPC.listRecordSets(ZONE1.getName(), options).results(); records = ImmutableList.copyOf(results); assertEquals(3, records.size()); // dnsName filter options = new HashMap<>(); options.put(DnsRpc.Option.NAME, "aaa"); try { - RPC.listDnsRecords(ZONE1.getName(), options); + RPC.listRecordSets(ZONE1.getName(), options); fail(); } catch (DnsException ex) { // expected @@ -746,13 +746,13 @@ public void testListDnsRecords() { assertTrue(ex.getMessage().contains("parameters.name")); } options.put(DnsRpc.Option.NAME, "aaa."); - results = RPC.listDnsRecords(ZONE1.getName(), options).results(); + results = RPC.listRecordSets(ZONE1.getName(), options).results(); records = ImmutableList.copyOf(results); assertEquals(0, records.size()); options.put(DnsRpc.Option.NAME, null); options.put(DnsRpc.Option.DNS_TYPE, "A"); try { - RPC.listDnsRecords(ZONE1.getName(), options); + RPC.listRecordSets(ZONE1.getName(), options); fail(); } catch (DnsException ex) { // expected @@ -762,7 +762,7 @@ public void testListDnsRecords() { options.put(DnsRpc.Option.NAME, "aaa."); options.put(DnsRpc.Option.DNS_TYPE, "a"); try { - RPC.listDnsRecords(ZONE1.getName(), options); + RPC.listRecordSets(ZONE1.getName(), options); fail(); } catch (DnsException ex) { // expected @@ -771,14 +771,14 @@ public void testListDnsRecords() { } options.put(DnsRpc.Option.NAME, DNS_NAME); options.put(DnsRpc.Option.DNS_TYPE, "SOA"); - results = RPC.listDnsRecords(ZONE1.getName(), options).results(); + results = RPC.listRecordSets(ZONE1.getName(), options).results(); records = ImmutableList.copyOf(results); assertEquals(1, records.size()); // field options options = new HashMap<>(); options.put(DnsRpc.Option.FIELDS, "rrsets(name)"); DnsRpc.ListResult listResult = - RPC.listDnsRecords(ZONE1.getName(), options); + RPC.listRecordSets(ZONE1.getName(), options); records = ImmutableList.copyOf(listResult.results()); ResourceRecordSet record = records.get(0); assertNotNull(record.getName()); @@ -787,7 +787,7 @@ public void testListDnsRecords() { assertNull(record.getTtl()); assertNull(listResult.pageToken()); options.put(DnsRpc.Option.FIELDS, "rrsets(rrdatas)"); - listResult = RPC.listDnsRecords(ZONE1.getName(), options); + listResult = RPC.listRecordSets(ZONE1.getName(), options); records = ImmutableList.copyOf(listResult.results()); record = records.get(0); assertNull(record.getName()); @@ -796,7 +796,7 @@ record = records.get(0); assertNull(record.getTtl()); assertNull(listResult.pageToken()); options.put(DnsRpc.Option.FIELDS, "rrsets(ttl)"); - listResult = RPC.listDnsRecords(ZONE1.getName(), options); + listResult = RPC.listRecordSets(ZONE1.getName(), options); records = ImmutableList.copyOf(listResult.results()); record = records.get(0); assertNull(record.getName()); @@ -805,7 +805,7 @@ record = records.get(0); assertNotNull(record.getTtl()); assertNull(listResult.pageToken()); options.put(DnsRpc.Option.FIELDS, "rrsets(type)"); - listResult = RPC.listDnsRecords(ZONE1.getName(), options); + listResult = RPC.listRecordSets(ZONE1.getName(), options); records = ImmutableList.copyOf(listResult.results()); record = records.get(0); assertNull(record.getName()); @@ -814,7 +814,7 @@ record = records.get(0); assertNull(record.getTtl()); assertNull(listResult.pageToken()); options.put(DnsRpc.Option.FIELDS, "nextPageToken"); - listResult = RPC.listDnsRecords(ZONE1.getName(), options); + listResult = RPC.listRecordSets(ZONE1.getName(), options); records = ImmutableList.copyOf(listResult.results()); record = records.get(0); assertNull(record.getName()); @@ -824,7 +824,7 @@ record = records.get(0); assertNull(listResult.pageToken()); options.put(DnsRpc.Option.FIELDS, "nextPageToken,rrsets(name,rrdatas)"); options.put(DnsRpc.Option.PAGE_SIZE, 1); - listResult = RPC.listDnsRecords(ZONE1.getName(), options); + listResult = RPC.listRecordSets(ZONE1.getName(), options); records = ImmutableList.copyOf(listResult.results()); assertEquals(1, records.size()); record = records.get(0); @@ -973,15 +973,15 @@ public void testListChanges() { public void testDnsRecordPaging() { RPC.create(ZONE1, EMPTY_RPC_OPTIONS); List complete = ImmutableList.copyOf( - RPC.listDnsRecords(ZONE1.getName(), EMPTY_RPC_OPTIONS).results()); + RPC.listRecordSets(ZONE1.getName(), EMPTY_RPC_OPTIONS).results()); Map options = new HashMap<>(); options.put(DnsRpc.Option.PAGE_SIZE, 1); - DnsRpc.ListResult listResult = RPC.listDnsRecords(ZONE1.getName(), options); + DnsRpc.ListResult listResult = RPC.listRecordSets(ZONE1.getName(), options); ImmutableList records = ImmutableList.copyOf(listResult.results()); assertEquals(1, records.size()); assertEquals(complete.get(0), records.get(0)); options.put(DnsRpc.Option.PAGE_TOKEN, listResult.pageToken()); - listResult = RPC.listDnsRecords(ZONE1.getName(), options); + listResult = RPC.listRecordSets(ZONE1.getName(), options); records = ImmutableList.copyOf(listResult.results()); assertEquals(1, records.size()); assertEquals(complete.get(1), records.get(0)); diff --git a/gcloud-java-examples/README.md b/gcloud-java-examples/README.md index fc9ce9ef653d..8084cee562f0 100644 --- a/gcloud-java-examples/README.md +++ b/gcloud-java-examples/README.md @@ -75,7 +75,7 @@ To run examples from your command line: Note that you have to enable the Google Cloud DNS API on the [Google Developers Console][developers-console] before running the following commands. You will need to replace the domain name `elaborateexample.com` with your own domain name with [verified ownership] (https://www.google.com/webmasters/verification/home). - Also, note that the example creates and deletes DNS records of type A only. Operations with other record types are not implemented in the example. + Also, note that the example creates and deletes record sets of type A only. Operations with other record types are not implemented in the example. ``` mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.dns.DnsExample" -Dexec.args="create some-sample-zone elaborateexample.com. description" mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.dns.DnsExample" -Dexec.args="list" diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java index 1b6ba8f179da..40ce61b07281 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java @@ -21,8 +21,8 @@ import com.google.gcloud.dns.ChangeRequest; import com.google.gcloud.dns.Dns; import com.google.gcloud.dns.DnsOptions; -import com.google.gcloud.dns.DnsRecord; import com.google.gcloud.dns.ProjectInfo; +import com.google.gcloud.dns.RecordSet; import com.google.gcloud.dns.Zone; import com.google.gcloud.dns.ZoneInfo; @@ -38,8 +38,8 @@ /** * An example of using Google Cloud DNS. * - *

This example creates, deletes, gets, and lists zones. It also creates and deletes DNS records - * of type A, and lists DNS records. + *

This example creates, deletes, gets, and lists zones. It also creates and deletes + * record sets of type A, and lists record sets. * *

Steps needed for running the example: *

    @@ -203,12 +203,12 @@ public void run(Dns dns, String... args) { if (args.length > 3) { ttl = Integer.parseInt(args[3]); } - DnsRecord record = DnsRecord.builder(recordName, DnsRecord.Type.A) + RecordSet recordSet = RecordSet.builder(recordName, RecordSet.Type.A) .records(ImmutableList.of(ip)) .ttl(ttl, TimeUnit.SECONDS) .build(); ChangeRequest changeRequest = ChangeRequest.builder() - .delete(record) + .delete(recordSet) .build(); changeRequest = dns.applyChangeRequest(zoneName, changeRequest); System.out.printf("The request for deleting A record %s for zone %s was successfully " @@ -238,7 +238,7 @@ public boolean check(String... args) { private static class AddDnsRecordAction implements DnsAction { /** - * Adds a DNS record of type A. The last parameter is ttl and is not required. If ttl is not + * Adds a record set of type A. The last parameter is ttl and is not required. If ttl is not * provided, a default value of 0 will be used. */ @Override @@ -250,11 +250,11 @@ public void run(Dns dns, String... args) { if (args.length > 3) { ttl = Integer.parseInt(args[3]); } - DnsRecord record = DnsRecord.builder(recordName, DnsRecord.Type.A) + RecordSet recordSet = RecordSet.builder(recordName, RecordSet.Type.A) .records(ImmutableList.of(ip)) .ttl(ttl, TimeUnit.SECONDS) .build(); - ChangeRequest changeRequest = ChangeRequest.builder().add(record).build(); + ChangeRequest changeRequest = ChangeRequest.builder().add(recordSet).build(); changeRequest = dns.applyChangeRequest(zoneName, changeRequest); System.out.printf("The request for adding A record %s for zone %s was successfully " + "submitted and assigned ID %s.%n", recordName, zoneName, changeRequest.id()); @@ -283,21 +283,21 @@ public boolean check(String... args) { private static class ListDnsRecordsAction implements DnsAction { /** - * Lists all the DNS records in the given zone. + * Lists all the record sets in the given zone. */ @Override public void run(Dns dns, String... args) { String zoneName = args[0]; - Iterator iterator = dns.listDnsRecords(zoneName).iterateAll(); + Iterator iterator = dns.listRecordSets(zoneName).iterateAll(); if (iterator.hasNext()) { - System.out.printf("DNS records for zone %s:%n", zoneName); + System.out.printf("Record sets for zone %s:%n", zoneName); while (iterator.hasNext()) { - DnsRecord record = iterator.next(); - System.out.printf("%nRecord name: %s%nTTL: %s%nRecords: %s%n", record.name(), - record.ttl(), Joiner.on(", ").join(record.records())); + RecordSet recordSet = iterator.next(); + System.out.printf("%nRecord name: %s%nTTL: %s%nRecords: %s%n", recordSet.name(), + recordSet.ttl(), Joiner.on(", ").join(recordSet.records())); } } else { - System.out.printf("Zone %s has no DNS records.%n", zoneName); + System.out.printf("Zone %s has no record sets records.%n", zoneName); } } @@ -361,8 +361,8 @@ private static class ListAction implements DnsAction { /** * Invokes a list action. If no parameter is provided, lists all zones. If zone name is the only - * parameter provided, lists both DNS records and changes. Otherwise, invokes listing changes or - * zones based on the parameter provided. + * parameter provided, lists both record sets and changes. Otherwise, invokes listing + * changes or zones based on the parameter provided. */ @Override public void run(Dns dns, String... args) { @@ -406,7 +406,7 @@ public void run(Dns dns, String... args) { ProjectInfo.Quota quota = project.quota(); System.out.printf("Project id: %s%nQuota:%n", dns.options().projectId()); System.out.printf("\tZones: %d%n", quota.zones()); - System.out.printf("\tDNS records per zone: %d%n", quota.rrsetsPerZone()); + System.out.printf("\tRecord sets per zone: %d%n", quota.rrsetsPerZone()); System.out.printf("\tRecord sets per DNS record: %d%n", quota.resourceRecordsPerRrset()); System.out.printf("\tAdditions per change: %d%n", quota.rrsetAdditionsPerChange()); diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateOrUpdateDnsRecords.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateOrUpdateRecordSets.java similarity index 83% rename from gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateOrUpdateDnsRecords.java rename to gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateOrUpdateRecordSets.java index 71327ba98a96..74647daf666e 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateOrUpdateDnsRecords.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateOrUpdateRecordSets.java @@ -25,16 +25,16 @@ import com.google.gcloud.dns.ChangeRequest; import com.google.gcloud.dns.Dns; import com.google.gcloud.dns.DnsOptions; -import com.google.gcloud.dns.DnsRecord; +import com.google.gcloud.dns.RecordSet; import com.google.gcloud.dns.Zone; import java.util.Iterator; import java.util.concurrent.TimeUnit; /** - * A snippet for Google Cloud DNS showing how to create and update a DNS record. + * A snippet for Google Cloud DNS showing how to create and update a resource record set. */ -public class CreateOrUpdateDnsRecords { +public class CreateOrUpdateRecordSets { public static void main(String... args) { // Create a service object. @@ -47,9 +47,9 @@ public static void main(String... args) { // Get zone from the service Zone zone = dns.getZone(zoneName); - // Prepare a www.. type A record with ttl of 24 hours + // Prepare a www.. type A record set with ttl of 24 hours String ip = "12.13.14.15"; - DnsRecord toCreate = DnsRecord.builder("www." + zone.dnsName(), DnsRecord.Type.A) + RecordSet toCreate = RecordSet.builder("www." + zone.dnsName(), RecordSet.Type.A) .ttl(24, TimeUnit.HOURS) .addRecord(ip) .build(); @@ -59,9 +59,9 @@ public static void main(String... args) { // Verify a www.. type A record does not exist yet. // If it does exist, we will overwrite it with our prepared record. - Iterator recordIterator = zone.listDnsRecords().iterateAll(); - while (recordIterator.hasNext()) { - DnsRecord current = recordIterator.next(); + Iterator recordSetIterator = zone.listRecordSets().iterateAll(); + while (recordSetIterator.hasNext()) { + RecordSet current = recordSetIterator.next(); if (toCreate.name().equals(current.name()) && toCreate.type().equals(current.type())) { changeBuilder.delete(current); } diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java index e841a4cd54ed..27377345b62f 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java @@ -25,7 +25,7 @@ import com.google.gcloud.dns.ChangeRequest; import com.google.gcloud.dns.Dns; import com.google.gcloud.dns.DnsOptions; -import com.google.gcloud.dns.DnsRecord; +import com.google.gcloud.dns.RecordSet; import java.util.Iterator; @@ -43,15 +43,15 @@ public static void main(String... args) { // Change this to a zone name that exists within your project and that you want to delete. String zoneName = "my-unique-zone"; - // Get iterator for the existing records which have to be deleted before deleting the zone - Iterator recordIterator = dns.listDnsRecords(zoneName).iterateAll(); + // Get iterator for the existing record sets which have to be deleted before deleting the zone + Iterator recordIterator = dns.listRecordSets(zoneName).iterateAll(); // Make a change for deleting the records ChangeRequest.Builder changeBuilder = ChangeRequest.builder(); while (recordIterator.hasNext()) { - DnsRecord current = recordIterator.next(); + RecordSet current = recordIterator.next(); // SOA and NS records cannot be deleted - if (!DnsRecord.Type.SOA.equals(current.type()) && !DnsRecord.Type.NS.equals(current.type())) { + if (!RecordSet.Type.SOA.equals(current.type()) && !RecordSet.Type.NS.equals(current.type())) { changeBuilder.delete(current); } } diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecords.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java similarity index 84% rename from gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecords.java rename to gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java index 4de262386d53..6d9d09d704a6 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecords.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java @@ -25,7 +25,7 @@ import com.google.gcloud.dns.ChangeRequest; import com.google.gcloud.dns.Dns; import com.google.gcloud.dns.DnsOptions; -import com.google.gcloud.dns.DnsRecord; +import com.google.gcloud.dns.RecordSet; import com.google.gcloud.dns.Zone; import com.google.gcloud.dns.ZoneInfo; @@ -35,9 +35,9 @@ /** * A complete snippet for Google Cloud DNS showing how to create and delete a zone. It also shows - * how to create, list and delete DNS records, and how to list changes. + * how to create, list and delete record sets, and how to list changes. */ -public class ManipulateZonesAndRecords { +public class ManipulateZonesAndRecordSets { public static void main(String... args) { Dns dns = DnsOptions.defaultInstance().service(); @@ -60,7 +60,7 @@ public static void main(String... args) { // Prepare a www.someexampledomain.com. type A record with ttl of 24 hours String ip = "12.13.14.15"; - DnsRecord toCreate = DnsRecord.builder("www.someexampledomain.com.", DnsRecord.Type.A) + RecordSet toCreate = RecordSet.builder("www.someexampledomain.com.", RecordSet.Type.A) .ttl(24, TimeUnit.HOURS) .addRecord(ip) .build(); @@ -70,9 +70,9 @@ public static void main(String... args) { // Verify the type A record does not exist yet. // If it does exist, we will overwrite it with our prepared record. - Iterator recordIterator = zone.listDnsRecords().iterateAll(); - while (recordIterator.hasNext()) { - DnsRecord current = recordIterator.next(); + Iterator recordSetIterator = zone.listRecordSets().iterateAll(); + while (recordSetIterator.hasNext()) { + RecordSet current = recordSetIterator.next(); if (toCreate.name().equals(current.name()) && toCreate.type().equals(current.type())) { changeBuilder.delete(current); } @@ -100,11 +100,11 @@ public static void main(String... args) { counter++; } - // List the DNS records in a particular zone - recordIterator = zone.listDnsRecords().iterateAll(); - System.out.println(String.format("DNS records inside %s:", zone.name())); - while (recordIterator.hasNext()) { - System.out.println(recordIterator.next()); + // List the record sets in a particular zone + recordSetIterator = zone.listRecordSets().iterateAll(); + System.out.println(String.format("Record sets inside %s:", zone.name())); + while (recordSetIterator.hasNext()) { + System.out.println(recordSetIterator.next()); } // List the change requests applied to a particular zone @@ -114,12 +114,12 @@ public static void main(String... args) { System.out.println(changeIterator.next()); } - // Make a change for deleting the records + // Make a change for deleting the record sets changeBuilder = ChangeRequest.builder(); - while (recordIterator.hasNext()) { - DnsRecord current = recordIterator.next(); + while (recordSetIterator.hasNext()) { + RecordSet current = recordSetIterator.next(); // SOA and NS records cannot be deleted - if (!DnsRecord.Type.SOA.equals(current.type()) && !DnsRecord.Type.NS.equals(current.type())) { + if (!RecordSet.Type.SOA.equals(current.type()) && !RecordSet.Type.NS.equals(current.type())) { changeBuilder.delete(current); } } From 01e23100fb12bf855f31b2ab8c4d12e43de9aab8 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Thu, 24 Mar 2016 16:46:09 -0700 Subject: [PATCH 30/60] Turned ChangeRequest into a functional object. --- .../com/google/gcloud/dns/ChangeRequest.java | 265 ++++--------- .../google/gcloud/dns/ChangeRequestInfo.java | 347 ++++++++++++++++++ .../main/java/com/google/gcloud/dns/Dns.java | 4 +- .../java/com/google/gcloud/dns/DnsImpl.java | 11 +- .../main/java/com/google/gcloud/dns/Zone.java | 2 +- .../gcloud/dns/ChangeRequestInfoTest.java | 218 +++++++++++ .../google/gcloud/dns/ChangeRequestTest.java | 269 +++++--------- .../com/google/gcloud/dns/DnsImplTest.java | 4 +- .../google/gcloud/dns/SerializationTest.java | 14 +- .../java/com/google/gcloud/dns/ZoneTest.java | 44 ++- .../com/google/gcloud/dns/it/ITDnsTest.java | 13 +- 11 files changed, 791 insertions(+), 400 deletions(-) create mode 100644 gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequestInfo.java create mode 100644 gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestInfoTest.java diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java index 757d844cc3da..ad1c65f7cb0f 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java @@ -16,19 +16,11 @@ package com.google.gcloud.dns; -import static com.google.common.base.Preconditions.checkNotNull; - import com.google.api.services.dns.model.Change; import com.google.common.base.Function; -import com.google.common.base.MoreObjects; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Lists; - -import org.joda.time.DateTime; -import org.joda.time.format.ISODateTimeFormat; -import java.io.Serializable; -import java.util.LinkedList; +import java.io.IOException; +import java.io.ObjectInputStream; import java.util.List; import java.util.Objects; @@ -38,21 +30,12 @@ * * @see Google Cloud DNS documentation */ -public class ChangeRequest implements Serializable { +public class ChangeRequest extends ChangeRequestInfo { - static final Function FROM_PB_FUNCTION = - new Function() { - @Override - public ChangeRequest apply(com.google.api.services.dns.model.Change pb) { - return ChangeRequest.fromPb(pb); - } - }; private static final long serialVersionUID = -9027378042756366333L; - private final List additions; - private final List deletions; - private final String id; - private final Long startTimeMillis; - private final Status status; + private final DnsOptions options; + private final String zoneName; + private transient Dns dns; /** * This enumerates the possible states of a {@code ChangeRequest}. @@ -68,250 +51,152 @@ public enum Status { /** * A builder for {@code ChangeRequest}s. */ - public static class Builder { + public static class Builder extends ChangeRequestInfo.Builder { - private List additions = new LinkedList<>(); - private List deletions = new LinkedList<>(); - private String id; - private Long startTimeMillis; - private Status status; + private final Dns dns; + private final String zoneName; + private final ChangeRequestInfo.BuilderImpl infoBuilder; private Builder(ChangeRequest cr) { - this.additions = Lists.newLinkedList(cr.additions()); - this.deletions = Lists.newLinkedList(cr.deletions()); - this.id = cr.id(); - this.startTimeMillis = cr.startTimeMillis(); - this.status = cr.status(); + this.dns = cr.dns; + this.zoneName = cr.zoneName; + this.infoBuilder = new ChangeRequestInfo.BuilderImpl(cr); } - private Builder() { - } - - /** - * Sets a collection of {@link RecordSet}s which are to be added to the zone upon executing this - * {@code ChangeRequest}. - */ + @Override public Builder additions(List additions) { - this.additions = Lists.newLinkedList(checkNotNull(additions)); + infoBuilder.additions(additions); return this; } - /** - * Sets a collection of {@link RecordSet}s which are to be deleted from the zone upon executing - * this {@code ChangeRequest}. - */ + @Override public Builder deletions(List deletions) { - this.deletions = Lists.newLinkedList(checkNotNull(deletions)); + infoBuilder.deletions(deletions); return this; } - /** - * Adds a {@link RecordSet} to be added to the zone upon executing this {@code - * ChangeRequest}. - */ + @Override public Builder add(RecordSet recordSet) { - this.additions.add(checkNotNull(recordSet)); + infoBuilder.add(recordSet); return this; } - /** - * Adds a {@link RecordSet} to be deleted to the zone upon executing this - * {@code ChangeRequest}. - */ + @Override public Builder delete(RecordSet recordSet) { - this.deletions.add(checkNotNull(recordSet)); + infoBuilder.delete(recordSet); return this; } - /** - * Clears the collection of {@link RecordSet}s which are to be added to the zone upon executing - * this {@code ChangeRequest}. - */ + @Override public Builder clearAdditions() { - this.additions.clear(); + infoBuilder.clearAdditions(); return this; } - /** - * Clears the collection of {@link RecordSet}s which are to be deleted from the zone upon - * executing this {@code ChangeRequest}. - */ + @Override public Builder clearDeletions() { - this.deletions.clear(); + infoBuilder.clearDeletions(); return this; } - /** - * Removes a single {@link RecordSet} from the collection of records to be - * added to the zone upon executing this {@code ChangeRequest}. - */ + @Override public Builder removeAddition(RecordSet recordSet) { - this.additions.remove(recordSet); + infoBuilder.removeAddition(recordSet); return this; } - /** - * Removes a single {@link RecordSet} from the collection of records to be - * deleted from the zone upon executing this {@code ChangeRequest}. - */ + @Override public Builder removeDeletion(RecordSet recordSet) { - this.deletions.remove(recordSet); + infoBuilder.removeDeletion(recordSet); return this; } - /** - * Associates a server-assigned id to this {@code ChangeRequest}. - */ + @Override Builder id(String id) { - this.id = checkNotNull(id); + infoBuilder.id(id); return this; } - /** - * Sets the time when this {@code ChangeRequest} was started by a server. - */ + @Override Builder startTimeMillis(long startTimeMillis) { - this.startTimeMillis = startTimeMillis; + infoBuilder.startTimeMillis(startTimeMillis); return this; } - /** - * Sets the current status of this {@code ChangeRequest}. - */ + @Override Builder status(Status status) { - this.status = checkNotNull(status); + infoBuilder.status(status); return this; } - /** - * Creates a {@code ChangeRequest} instance populated by the values associated with this - * builder. - */ + @Override public ChangeRequest build() { - return new ChangeRequest(this); + return new ChangeRequest(dns, zoneName, infoBuilder); } } - private ChangeRequest(Builder builder) { - this.additions = ImmutableList.copyOf(builder.additions); - this.deletions = ImmutableList.copyOf(builder.deletions); - this.id = builder.id; - this.startTimeMillis = builder.startTimeMillis; - this.status = builder.status; - } - - /** - * Returns an empty builder for the {@code ChangeRequest} class. - */ - public static Builder builder() { - return new Builder(); - } - - /** - * Creates a builder populated with values of this {@code ChangeRequest}. - */ - public Builder toBuilder() { - return new Builder(this); - } - - /** - * Returns the list of {@link RecordSet}s to be added to the zone upon submitting this {@code - * ChangeRequest}. - */ - public List additions() { - return additions; + ChangeRequest(Dns dns, String zoneName, ChangeRequest.BuilderImpl infoBuilder) { + super(infoBuilder); + this.zoneName = zoneName; + this.dns = dns; + this.options = dns.options(); } - /** - * Returns the list of {@link RecordSet}s to be deleted from the zone upon submitting this {@code - * ChangeRequest}. - */ - public List deletions() { - return deletions; + static Function fromPbFunction(final Dns dns, final String zoneName) { + return new Function() { + @Override + public ChangeRequest apply(com.google.api.services.dns.model.Change pb) { + return ChangeRequest.fromPb(dns, zoneName, pb); + } + }; } /** - * Returns the id assigned to this {@code ChangeRequest} by the server. + * Returns the name of the {@link Zone} associated with this change request. */ - public String id() { - return id; + public String zoneName() { + return this.zoneName; } /** - * Returns the time when this {@code ChangeRequest} was started by the server. + * Returns the {@link Dns} service object associated with this change request. */ - public Long startTimeMillis() { - return startTimeMillis; + public Dns dns() { + return dns; } /** - * Returns the status of this {@code ChangeRequest}. + * Applies this change request to a zone that it is associated with. */ - public Status status() { - return status; + public ChangeRequest applyTo(Dns.ChangeRequestOption... options) { + return dns.applyChangeRequest(zoneName, this, options); } - com.google.api.services.dns.model.Change toPb() { - com.google.api.services.dns.model.Change pb = - new com.google.api.services.dns.model.Change(); - // set id - if (id() != null) { - pb.setId(id()); - } - // set timestamp - if (startTimeMillis() != null) { - pb.setStartTime(ISODateTimeFormat.dateTime().withZoneUTC().print(startTimeMillis())); - } - // set status - if (status() != null) { - pb.setStatus(status().name().toLowerCase()); - } - // set a list of additions - pb.setAdditions(Lists.transform(additions(), RecordSet.TO_PB_FUNCTION)); - // set a list of deletions - pb.setDeletions(Lists.transform(deletions(), RecordSet.TO_PB_FUNCTION)); - return pb; - } - - static ChangeRequest fromPb(com.google.api.services.dns.model.Change pb) { - Builder builder = builder(); - if (pb.getId() != null) { - builder.id(pb.getId()); - } - if (pb.getStartTime() != null) { - builder.startTimeMillis(DateTime.parse(pb.getStartTime()).getMillis()); - } - if (pb.getStatus() != null) { - // we are assuming that status indicated in pb is a lower case version of the enum name - builder.status(ChangeRequest.Status.valueOf(pb.getStatus().toUpperCase())); - } - if (pb.getDeletions() != null) { - builder.deletions(Lists.transform(pb.getDeletions(), RecordSet.FROM_PB_FUNCTION)); - } - if (pb.getAdditions() != null) { - builder.additions(Lists.transform(pb.getAdditions(), RecordSet.FROM_PB_FUNCTION)); - } - return builder.build(); + @Override + public Builder toBuilder() { + return new Builder(this); } @Override - public boolean equals(Object other) { - return (other instanceof ChangeRequest) && toPb().equals(((ChangeRequest) other).toPb()); + public boolean equals(Object obj) { + return obj instanceof ChangeRequest && Objects.equals(toPb(), ((ChangeRequest) obj).toPb()) + && Objects.equals(options, ((ChangeRequest) obj).options) + && Objects.equals(zoneName, ((ChangeRequest) obj).zoneName); } @Override public int hashCode() { - return Objects.hash(additions, deletions, id, startTimeMillis, status); + return Objects.hash(super.hashCode(), options, zoneName); } - @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .add("additions", additions) - .add("deletions", deletions) - .add("id", id) - .add("startTimeMillis", startTimeMillis) - .add("status", status) - .toString(); + private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { + in.defaultReadObject(); + this.dns = options.service(); + } + + static ChangeRequest fromPb(Dns dns, String zoneName, + com.google.api.services.dns.model.Change pb) { + ChangeRequestInfo info = ChangeRequestInfo.fromPb(pb); + return new ChangeRequest(dns, zoneName, new ChangeRequestInfo.BuilderImpl(info)); } } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequestInfo.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequestInfo.java new file mode 100644 index 000000000000..25b915521b13 --- /dev/null +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequestInfo.java @@ -0,0 +1,347 @@ +/* + * 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.api.services.dns.model.Change; +import com.google.common.base.Function; +import com.google.common.base.MoreObjects; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; + +import org.joda.time.DateTime; +import org.joda.time.format.ISODateTimeFormat; + +import java.io.Serializable; +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; + +/** + * A class representing an atomic update to a collection of {@link RecordSet}s within a {@code + * Zone}. + * + * @see Google Cloud DNS documentation + */ +public class ChangeRequestInfo implements Serializable { + + static final Function FROM_PB_FUNCTION = + new Function() { + @Override + public ChangeRequestInfo apply(Change pb) { + return ChangeRequestInfo.fromPb(pb); + } + }; + private static final long serialVersionUID = -9027378042756366333L; + private final List additions; + private final List deletions; + private final String id; + private final Long startTimeMillis; + private final ChangeRequest.Status status; + + /** + * A builder for {@code ChangeRequestInfo}. + */ + public abstract static class Builder { + + /** + * Sets a collection of {@link RecordSet}s which are to be added to the zone upon executing this + * {@code ChangeRequestInfo}. + */ + public abstract Builder additions(List additions); + + /** + * Sets a collection of {@link RecordSet}s which are to be deleted from the zone upon executing + * this {@code ChangeRequestInfo}. + */ + public abstract Builder deletions(List deletions); + + /** + * Adds a {@link RecordSet} to be added to the zone upon executing this {@code + * ChangeRequestInfo}. + */ + public abstract Builder add(RecordSet recordSet); + + /** + * Adds a {@link RecordSet} to be deleted to the zone upon executing this + * {@code ChangeRequestInfo}. + */ + public abstract Builder delete(RecordSet recordSet); + + /** + * Clears the collection of {@link RecordSet}s which are to be added to the zone upon executing + * this {@code ChangeRequestInfo}. + */ + public abstract Builder clearAdditions(); + + /** + * Clears the collection of {@link RecordSet}s which are to be deleted from the zone upon + * executing this {@code ChangeRequestInfo}. + */ + public abstract Builder clearDeletions(); + + /** + * Removes a single {@link RecordSet} from the collection of records to be + * added to the zone upon executing this {@code ChangeRequestInfo}. + */ + public abstract Builder removeAddition(RecordSet recordSet); + + /** + * Removes a single {@link RecordSet} from the collection of records to be + * deleted from the zone upon executing this {@code ChangeRequestInfo}. + */ + public abstract Builder removeDeletion(RecordSet recordSet); + + /** + * Associates a server-assigned id to this {@code ChangeRequestInfo}. + */ + abstract Builder id(String id); + + /** + * Sets the time when this change request was started by a server. + */ + abstract Builder startTimeMillis(long startTimeMillis); + + /** + * Sets the current status of this {@code ChangeRequest}. + */ + abstract Builder status(ChangeRequest.Status status); + + /** + * Creates a {@code ChangeRequestInfo} instance populated by the values associated with this + * builder. + */ + public abstract ChangeRequestInfo build(); + } + + static class BuilderImpl extends Builder { + private List additions; + private List deletions; + private String id; + private Long startTimeMillis; + private ChangeRequest.Status status; + + BuilderImpl() { + this.additions = new LinkedList<>(); + this.deletions = new LinkedList<>(); + } + + BuilderImpl(ChangeRequestInfo info) { + this.additions = Lists.newLinkedList(info.additions()); + this.deletions = Lists.newLinkedList(info.deletions()); + this.id = info.id(); + this.startTimeMillis = info.startTimeMillis; + this.status = info.status; + } + + @Override + public Builder additions(List additions) { + this.additions = Lists.newLinkedList(checkNotNull(additions)); + return this; + } + + @Override + public Builder deletions(List deletions) { + this.deletions = Lists.newLinkedList(checkNotNull(deletions)); + return this; + } + + @Override + public Builder add(RecordSet recordSet) { + this.additions.add(checkNotNull(recordSet)); + return this; + } + + @Override + public Builder delete(RecordSet recordSet) { + this.deletions.add(checkNotNull(recordSet)); + return this; + } + + @Override + public Builder clearAdditions() { + this.additions.clear(); + return this; + } + + @Override + public Builder clearDeletions() { + this.deletions.clear(); + return this; + } + + @Override + public Builder removeAddition(RecordSet recordSet) { + this.additions.remove(recordSet); + return this; + } + + @Override + public Builder removeDeletion(RecordSet recordSet) { + this.deletions.remove(recordSet); + return this; + } + + @Override + public ChangeRequestInfo build() { + return new ChangeRequestInfo(this); + } + + @Override + Builder id(String id) { + this.id = checkNotNull(id); + return this; + } + + @Override + Builder startTimeMillis(long startTimeMillis) { + this.startTimeMillis = startTimeMillis; + return this; + } + + @Override + Builder status(ChangeRequest.Status status) { + this.status = checkNotNull(status); + return this; + } + } + + ChangeRequestInfo(BuilderImpl builder) { + this.additions = ImmutableList.copyOf(builder.additions); + this.deletions = ImmutableList.copyOf(builder.deletions); + this.id = builder.id; + this.startTimeMillis = builder.startTimeMillis; + this.status = builder.status; + } + + /** + * Returns an empty builder for the {@code ChangeRequestInfo} class. + */ + public static Builder builder() { + return new BuilderImpl(); + } + + /** + * Creates a builder populated with values of this {@code ChangeRequestInfo}. + */ + public Builder toBuilder() { + return new BuilderImpl(this); + } + + /** + * Returns the list of {@link RecordSet}s to be added to the zone upon submitting this change + * request. + */ + public List additions() { + return additions; + } + + /** + * Returns the list of {@link RecordSet}s to be deleted from the zone upon submitting this change + * request. + */ + public List deletions() { + return deletions; + } + + /** + * Returns the id assigned to this {@code ChangeRequest} by the server. + */ + public String id() { + return id; + } + + /** + * Returns the time when this {@code ChangeRequest} was started by the server. + */ + public Long startTimeMillis() { + return startTimeMillis; + } + + /** + * Returns the status of this {@code ChangeRequest}. + */ + public ChangeRequest.Status status() { + return status; + } + + com.google.api.services.dns.model.Change toPb() { + com.google.api.services.dns.model.Change pb = + new com.google.api.services.dns.model.Change(); + // set id + if (id() != null) { + pb.setId(id()); + } + // set timestamp + if (startTimeMillis() != null) { + pb.setStartTime(ISODateTimeFormat.dateTime().withZoneUTC().print(startTimeMillis())); + } + // set status + if (status() != null) { + pb.setStatus(status().name().toLowerCase()); + } + // set a list of additions + pb.setAdditions(Lists.transform(additions(), RecordSet.TO_PB_FUNCTION)); + // set a list of deletions + pb.setDeletions(Lists.transform(deletions(), RecordSet.TO_PB_FUNCTION)); + return pb; + } + + static ChangeRequestInfo fromPb(com.google.api.services.dns.model.Change pb) { + Builder builder = builder(); + if (pb.getId() != null) { + builder.id(pb.getId()); + } + if (pb.getStartTime() != null) { + builder.startTimeMillis(DateTime.parse(pb.getStartTime()).getMillis()); + } + if (pb.getStatus() != null) { + // we are assuming that status indicated in pb is a lower case version of the enum name + builder.status(ChangeRequest.Status.valueOf(pb.getStatus().toUpperCase())); + } + if (pb.getDeletions() != null) { + builder.deletions(Lists.transform(pb.getDeletions(), RecordSet.FROM_PB_FUNCTION)); + } + if (pb.getAdditions() != null) { + builder.additions(Lists.transform(pb.getAdditions(), RecordSet.FROM_PB_FUNCTION)); + } + return builder.build(); + } + + @Override + public boolean equals(Object other) { + return (other instanceof ChangeRequestInfo) + && toPb().equals(((ChangeRequestInfo) other).toPb()); + } + + @Override + public int hashCode() { + return Objects.hash(additions, deletions, id, startTimeMillis, status); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("additions", additions) + .add("deletions", deletions) + .add("id", id) + .add("startTimeMillis", startTimeMillis) + .add("status", status) + .toString(); + } +} 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 f8614a8d6169..f2b42f30a9f6 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 @@ -138,7 +138,7 @@ static String selector(RecordSetField... 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(String, ChangeRequest, ChangeRequestOption...)} The ID is always + * {@link Dns#applyChangeRequest(String, ChangeRequestInfo, ChangeRequestOption...)} The ID is always * returned even if not selected. */ enum ChangeRequestField { @@ -508,7 +508,7 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) { * @throws DnsException upon failure if zone is not found * @see Cloud DNS Changes: create */ - ChangeRequest applyChangeRequest(String zoneName, ChangeRequest changeRequest, + ChangeRequest applyChangeRequest(String zoneName, ChangeRequestInfo changeRequest, ChangeRequestOption... options); /** diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java index 2fbf4e8b5a79..4e2bd1b207d5 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java @@ -169,7 +169,8 @@ public DnsRpc.ListResult call() { // transform that list into change request objects Iterable changes = result.results() == null ? ImmutableList.of() - : Iterables.transform(result.results(), ChangeRequest.FROM_PB_FUNCTION); + : Iterables.transform(result.results(), + ChangeRequest.fromPbFunction(serviceOptions.service(), zoneName)); return new PageImpl<>(new ChangeRequestPageFetcher(zoneName, serviceOptions, cursor, optionsMap), cursor, changes); } catch (RetryHelperException e) { @@ -272,8 +273,8 @@ public com.google.api.services.dns.model.Project call() { } @Override - public ChangeRequest applyChangeRequest(final String zoneName, final ChangeRequest changeRequest, - Dns.ChangeRequestOption... options) { + public ChangeRequest applyChangeRequest(final String zoneName, + final ChangeRequestInfo changeRequest, ChangeRequestOption... options) { final Map optionsMap = optionMap(options); try { com.google.api.services.dns.model.Change answer = @@ -284,7 +285,7 @@ public com.google.api.services.dns.model.Change call() { return dnsRpc.applyChangeRequest(zoneName, changeRequest.toPb(), optionsMap); } }, options().retryParams(), EXCEPTION_HANDLER); - return answer == null ? null : fromPb(answer); // should never be null + return answer == null ? null : fromPb(this, zoneName, answer); // should never be null } catch (RetryHelper.RetryHelperException ex) { throw DnsException.translateAndThrow(ex); } @@ -303,7 +304,7 @@ public com.google.api.services.dns.model.Change call() { return dnsRpc.getChangeRequest(zoneName, changeRequestId, optionsMap); } }, options().retryParams(), EXCEPTION_HANDLER); - return answer == null ? null : fromPb(answer); + return answer == null ? null : ChangeRequest.fromPb(this, zoneName, answer); } catch (RetryHelper.RetryHelperException ex) { throw DnsException.translateAndThrow(ex); } 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 aed99dbd0001..88b9e7273e9c 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 @@ -153,7 +153,7 @@ public Page listRecordSets(Dns.RecordSetListOption... options) { * @return ChangeRequest with server-assigned ID * @throws DnsException upon failure or if the zone is not found */ - public ChangeRequest applyChangeRequest(ChangeRequest changeRequest, + public ChangeRequest applyChangeRequest(ChangeRequestInfo changeRequest, Dns.ChangeRequestOption... options) { checkNotNull(changeRequest); return dns.applyChangeRequest(name(), changeRequest, options); diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestInfoTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestInfoTest.java new file mode 100644 index 000000000000..55f2af0824ec --- /dev/null +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestInfoTest.java @@ -0,0 +1,218 @@ +/* + * 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.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.assertTrue; +import static org.junit.Assert.fail; + +import com.google.common.collect.ImmutableList; + +import org.junit.Test; + +import java.util.List; + +public class ChangeRequestInfoTest { + + private static final String ID = "cr-id-1"; + private static final Long START_TIME_MILLIS = 12334567890L; + private static final ChangeRequest.Status STATUS = ChangeRequest.Status.PENDING; + private static final String NAME1 = "dns1"; + private static final RecordSet.Type TYPE1 = RecordSet.Type.A; + private static final String NAME2 = "dns2"; + private static final RecordSet.Type TYPE2 = RecordSet.Type.AAAA; + private static final String NAME3 = "dns3"; + private static final RecordSet.Type TYPE3 = RecordSet.Type.MX; + private static final RecordSet RECORD1 = RecordSet.builder(NAME1, TYPE1).build(); + private static final RecordSet RECORD2 = RecordSet.builder(NAME2, TYPE2).build(); + private static final RecordSet RECORD3 = RecordSet.builder(NAME3, TYPE3).build(); + private static final List ADDITIONS = ImmutableList.of(RECORD1, RECORD2); + private static final List DELETIONS = ImmutableList.of(RECORD3); + private static final ChangeRequestInfo CHANGE = ChangeRequest.builder() + .add(RECORD1) + .add(RECORD2) + .delete(RECORD3) + .startTimeMillis(START_TIME_MILLIS) + .status(STATUS) + .id(ID) + .build(); + + @Test + public void testEmptyBuilder() { + ChangeRequestInfo cr = ChangeRequest.builder().build(); + assertNotNull(cr.deletions()); + assertTrue(cr.deletions().isEmpty()); + assertNotNull(cr.additions()); + assertTrue(cr.additions().isEmpty()); + } + + @Test + public void testBuilder() { + assertEquals(ID, CHANGE.id()); + assertEquals(STATUS, CHANGE.status()); + assertEquals(START_TIME_MILLIS, CHANGE.startTimeMillis()); + assertEquals(ADDITIONS, CHANGE.additions()); + assertEquals(DELETIONS, CHANGE.deletions()); + List recordList = ImmutableList.of(RECORD1); + ChangeRequestInfo another = CHANGE.toBuilder().additions(recordList).build(); + assertEquals(recordList, another.additions()); + assertEquals(CHANGE.deletions(), another.deletions()); + another = CHANGE.toBuilder().deletions(recordList).build(); + assertEquals(recordList, another.deletions()); + assertEquals(CHANGE.additions(), another.additions()); + } + + @Test + public void testEqualsAndNotEquals() { + ChangeRequestInfo clone = CHANGE.toBuilder().build(); + assertEquals(CHANGE, clone); + clone = ChangeRequest.fromPb(CHANGE.toPb()); + assertEquals(CHANGE, clone); + clone = CHANGE.toBuilder().id("some-other-id").build(); + assertNotEquals(CHANGE, clone); + clone = CHANGE.toBuilder().startTimeMillis(CHANGE.startTimeMillis() + 1).build(); + assertNotEquals(CHANGE, clone); + clone = CHANGE.toBuilder().add(RECORD3).build(); + assertNotEquals(CHANGE, clone); + clone = CHANGE.toBuilder().delete(RECORD1).build(); + assertNotEquals(CHANGE, clone); + ChangeRequestInfo empty = ChangeRequest.builder().build(); + assertNotEquals(CHANGE, empty); + assertEquals(empty, ChangeRequest.builder().build()); + } + + @Test + public void testSameHashCodeOnEquals() { + ChangeRequestInfo clone = CHANGE.toBuilder().build(); + assertEquals(CHANGE, clone); + assertEquals(CHANGE.hashCode(), clone.hashCode()); + ChangeRequestInfo empty = ChangeRequest.builder().build(); + assertEquals(empty.hashCode(), ChangeRequest.builder().build().hashCode()); + } + + @Test + public void testToAndFromPb() { + assertEquals(CHANGE, ChangeRequest.fromPb(CHANGE.toPb())); + ChangeRequestInfo partial = ChangeRequest.builder().build(); + assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); + partial = ChangeRequest.builder().id(ID).build(); + assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); + partial = ChangeRequest.builder().add(RECORD1).build(); + assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); + partial = ChangeRequest.builder().delete(RECORD1).build(); + assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); + partial = ChangeRequest.builder().additions(ADDITIONS).build(); + assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); + partial = ChangeRequest.builder().deletions(DELETIONS).build(); + assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); + partial = ChangeRequest.builder().startTimeMillis(START_TIME_MILLIS).build(); + assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); + partial = ChangeRequest.builder().status(STATUS).build(); + assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); + } + + @Test + public void testToBuilder() { + assertEquals(CHANGE, CHANGE.toBuilder().build()); + ChangeRequestInfo partial = ChangeRequest.builder().build(); + assertEquals(partial, partial.toBuilder().build()); + partial = ChangeRequest.builder().id(ID).build(); + assertEquals(partial, partial.toBuilder().build()); + partial = ChangeRequest.builder().add(RECORD1).build(); + assertEquals(partial, partial.toBuilder().build()); + partial = ChangeRequest.builder().delete(RECORD1).build(); + assertEquals(partial, partial.toBuilder().build()); + partial = ChangeRequest.builder().additions(ADDITIONS).build(); + assertEquals(partial, partial.toBuilder().build()); + partial = ChangeRequest.builder().deletions(DELETIONS).build(); + assertEquals(partial, partial.toBuilder().build()); + partial = ChangeRequest.builder().startTimeMillis(START_TIME_MILLIS).build(); + assertEquals(partial, partial.toBuilder().build()); + partial = ChangeRequest.builder().status(STATUS).build(); + assertEquals(partial, partial.toBuilder().build()); + } + + @Test + public void testClearAdditions() { + ChangeRequestInfo clone = CHANGE.toBuilder().clearAdditions().build(); + assertTrue(clone.additions().isEmpty()); + assertFalse(clone.deletions().isEmpty()); + } + + @Test + public void testAddAddition() { + try { + CHANGE.toBuilder().add(null); + fail("Should not be able to add null RecordSet."); + } catch (NullPointerException e) { + // expected + } + ChangeRequestInfo clone = CHANGE.toBuilder().add(RECORD1).build(); + assertEquals(CHANGE.additions().size() + 1, clone.additions().size()); + } + + @Test + public void testAddDeletion() { + try { + CHANGE.toBuilder().delete(null); + fail("Should not be able to delete null RecordSet."); + } catch (NullPointerException e) { + // expected + } + ChangeRequestInfo clone = CHANGE.toBuilder().delete(RECORD1).build(); + assertEquals(CHANGE.deletions().size() + 1, clone.deletions().size()); + } + + @Test + public void testClearDeletions() { + ChangeRequestInfo clone = CHANGE.toBuilder().clearDeletions().build(); + assertTrue(clone.deletions().isEmpty()); + assertFalse(clone.additions().isEmpty()); + } + + @Test + public void testRemoveAddition() { + ChangeRequestInfo clone = CHANGE.toBuilder().removeAddition(RECORD1).build(); + assertTrue(clone.additions().contains(RECORD2)); + assertFalse(clone.additions().contains(RECORD1)); + assertTrue(clone.deletions().contains(RECORD3)); + clone = CHANGE.toBuilder().removeAddition(RECORD2).removeAddition(RECORD1).build(); + assertFalse(clone.additions().contains(RECORD2)); + assertFalse(clone.additions().contains(RECORD1)); + assertTrue(clone.additions().isEmpty()); + assertTrue(clone.deletions().contains(RECORD3)); + } + + @Test + public void testRemoveDeletion() { + ChangeRequestInfo clone = CHANGE.toBuilder().removeDeletion(RECORD3).build(); + assertTrue(clone.deletions().isEmpty()); + } + + @Test + public void testDateParsing() { + String startTime = "2016-01-26T18:33:43.512Z"; // obtained from service + com.google.api.services.dns.model.Change change = CHANGE.toPb().setStartTime(startTime); + ChangeRequestInfo converted = ChangeRequest.fromPb(change); + assertNotNull(converted.startTimeMillis()); + assertEquals(change, converted.toPb()); + assertEquals(change.getStartTime(), converted.toPb().getStartTime()); + } +} diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java index fe726acb7c10..8d6cc799cad8 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java @@ -16,203 +16,132 @@ 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.reset; +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.assertTrue; -import static org.junit.Assert.fail; import com.google.common.collect.ImmutableList; +import org.junit.After; +import org.junit.Before; import org.junit.Test; -import java.util.List; - public class ChangeRequestTest { - private static final String ID = "cr-id-1"; - private static final Long START_TIME_MILLIS = 12334567890L; - private static final ChangeRequest.Status STATUS = ChangeRequest.Status.PENDING; - private static final String NAME1 = "dns1"; - private static final RecordSet.Type TYPE1 = RecordSet.Type.A; - private static final String NAME2 = "dns2"; - private static final RecordSet.Type TYPE2 = RecordSet.Type.AAAA; - private static final String NAME3 = "dns3"; - private static final RecordSet.Type TYPE3 = RecordSet.Type.MX; - private static final RecordSet RECORD1 = RecordSet.builder(NAME1, TYPE1).build(); - private static final RecordSet RECORD2 = RecordSet.builder(NAME2, TYPE2).build(); - private static final RecordSet RECORD3 = RecordSet.builder(NAME3, TYPE3).build(); - private static final List ADDITIONS = ImmutableList.of(RECORD1, RECORD2); - private static final List DELETIONS = ImmutableList.of(RECORD3); - private static final ChangeRequest CHANGE = ChangeRequest.builder() - .add(RECORD1) - .add(RECORD2) - .delete(RECORD3) - .startTimeMillis(START_TIME_MILLIS) - .status(STATUS) - .id(ID) + private static final String ZONE_NAME = "dns-zone-name"; + private static final ChangeRequestInfo CHANGE_REQUEST_INFO = ChangeRequest.builder() + .add(RecordSet.builder("name", RecordSet.Type.A).build()) + .delete(RecordSet.builder("othername", RecordSet.Type.AAAA).build()) .build(); - - @Test - public void testEmptyBuilder() { - ChangeRequest cr = ChangeRequest.builder().build(); - assertNotNull(cr.deletions()); - assertTrue(cr.deletions().isEmpty()); - assertNotNull(cr.additions()); - assertTrue(cr.additions().isEmpty()); - } - - @Test - public void testBuilder() { - assertEquals(ID, CHANGE.id()); - assertEquals(STATUS, CHANGE.status()); - assertEquals(START_TIME_MILLIS, CHANGE.startTimeMillis()); - assertEquals(ADDITIONS, CHANGE.additions()); - assertEquals(DELETIONS, CHANGE.deletions()); - List recordList = ImmutableList.of(RECORD1); - ChangeRequest another = CHANGE.toBuilder().additions(recordList).build(); - assertEquals(recordList, another.additions()); - assertEquals(CHANGE.deletions(), another.deletions()); - another = CHANGE.toBuilder().deletions(recordList).build(); - assertEquals(recordList, another.deletions()); - assertEquals(CHANGE.additions(), another.additions()); - } - - @Test - public void testEqualsAndNotEquals() { - ChangeRequest clone = CHANGE.toBuilder().build(); - assertEquals(CHANGE, clone); - clone = ChangeRequest.fromPb(CHANGE.toPb()); - assertEquals(CHANGE, clone); - clone = CHANGE.toBuilder().id("some-other-id").build(); - assertNotEquals(CHANGE, clone); - clone = CHANGE.toBuilder().startTimeMillis(CHANGE.startTimeMillis() + 1).build(); - assertNotEquals(CHANGE, clone); - clone = CHANGE.toBuilder().add(RECORD3).build(); - assertNotEquals(CHANGE, clone); - clone = CHANGE.toBuilder().delete(RECORD1).build(); - assertNotEquals(CHANGE, clone); - ChangeRequest empty = ChangeRequest.builder().build(); - assertNotEquals(CHANGE, empty); - assertEquals(empty, ChangeRequest.builder().build()); - } - - @Test - public void testSameHashCodeOnEquals() { - ChangeRequest clone = CHANGE.toBuilder().build(); - assertEquals(CHANGE, clone); - assertEquals(CHANGE.hashCode(), clone.hashCode()); - ChangeRequest empty = ChangeRequest.builder().build(); - assertEquals(empty.hashCode(), ChangeRequest.builder().build().hashCode()); - } - - @Test - public void testToAndFromPb() { - assertEquals(CHANGE, ChangeRequest.fromPb(CHANGE.toPb())); - ChangeRequest partial = ChangeRequest.builder().build(); - assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); - partial = ChangeRequest.builder().id(ID).build(); - assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); - partial = ChangeRequest.builder().add(RECORD1).build(); - assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); - partial = ChangeRequest.builder().delete(RECORD1).build(); - assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); - partial = ChangeRequest.builder().additions(ADDITIONS).build(); - assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); - partial = ChangeRequest.builder().deletions(DELETIONS).build(); - assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); - partial = ChangeRequest.builder().startTimeMillis(START_TIME_MILLIS).build(); - assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); - partial = ChangeRequest.builder().status(STATUS).build(); - assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); - } - - @Test - public void testToBuilder() { - assertEquals(CHANGE, CHANGE.toBuilder().build()); - ChangeRequest partial = ChangeRequest.builder().build(); - assertEquals(partial, partial.toBuilder().build()); - partial = ChangeRequest.builder().id(ID).build(); - assertEquals(partial, partial.toBuilder().build()); - partial = ChangeRequest.builder().add(RECORD1).build(); - assertEquals(partial, partial.toBuilder().build()); - partial = ChangeRequest.builder().delete(RECORD1).build(); - assertEquals(partial, partial.toBuilder().build()); - partial = ChangeRequest.builder().additions(ADDITIONS).build(); - assertEquals(partial, partial.toBuilder().build()); - partial = ChangeRequest.builder().deletions(DELETIONS).build(); - assertEquals(partial, partial.toBuilder().build()); - partial = ChangeRequest.builder().startTimeMillis(START_TIME_MILLIS).build(); - assertEquals(partial, partial.toBuilder().build()); - partial = ChangeRequest.builder().status(STATUS).build(); - assertEquals(partial, partial.toBuilder().build()); - } - - @Test - public void testClearAdditions() { - ChangeRequest clone = CHANGE.toBuilder().clearAdditions().build(); - assertTrue(clone.additions().isEmpty()); - assertFalse(clone.deletions().isEmpty()); + private static final DnsOptions OPTIONS = createStrictMock(DnsOptions.class); + + private Dns dns; + private ChangeRequest changeRequest; + private ChangeRequest changeRequestPartial; + + @Before + public void setUp() throws Exception { + dns = createStrictMock(Dns.class); + expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS); + replay(dns); + changeRequest = new ChangeRequest(dns, ZONE_NAME, new ChangeRequestInfo.BuilderImpl( + CHANGE_REQUEST_INFO.toBuilder() + .startTimeMillis(132L) + .id("12") + .status(ChangeRequest.Status.DONE) + .build())); + changeRequestPartial = new ChangeRequest(dns, ZONE_NAME, + new ChangeRequest.BuilderImpl(CHANGE_REQUEST_INFO)); + reset(dns); } - @Test - public void testAddAddition() { - try { - CHANGE.toBuilder().add(null); - fail("Should not be able to add null RecordSet."); - } catch (NullPointerException e) { - // expected - } - ChangeRequest clone = CHANGE.toBuilder().add(RECORD1).build(); - assertEquals(CHANGE.additions().size() + 1, clone.additions().size()); + @After + public void tearDown() throws Exception { + verify(dns); } @Test - public void testAddDeletion() { - try { - CHANGE.toBuilder().delete(null); - fail("Should not be able to delete null RecordSet."); - } catch (NullPointerException e) { - // expected - } - ChangeRequest clone = CHANGE.toBuilder().delete(RECORD1).build(); - assertEquals(CHANGE.deletions().size() + 1, clone.deletions().size()); + public void testConstructor() { + replay(dns); + assertEquals(CHANGE_REQUEST_INFO.toPb(), changeRequestPartial.toPb()); + assertNotNull(changeRequest.dns()); + assertEquals(ZONE_NAME, changeRequest.zoneName()); + assertNotNull(changeRequestPartial.dns()); + assertEquals(ZONE_NAME, changeRequestPartial.zoneName()); } @Test - public void testClearDeletions() { - ChangeRequest clone = CHANGE.toBuilder().clearDeletions().build(); - assertTrue(clone.deletions().isEmpty()); - assertFalse(clone.additions().isEmpty()); + public void testFromPb() { + expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS); + replay(dns); + assertEquals(ChangeRequest.fromPb(dns, ZONE_NAME, changeRequest.toPb()), changeRequest); + assertEquals(ChangeRequest.fromPb(dns, ZONE_NAME, changeRequestPartial.toPb()), + changeRequestPartial); } @Test - public void testRemoveAddition() { - ChangeRequest clone = CHANGE.toBuilder().removeAddition(RECORD1).build(); - assertTrue(clone.additions().contains(RECORD2)); - assertFalse(clone.additions().contains(RECORD1)); - assertTrue(clone.deletions().contains(RECORD3)); - clone = CHANGE.toBuilder().removeAddition(RECORD2).removeAddition(RECORD1).build(); - assertFalse(clone.additions().contains(RECORD2)); - assertFalse(clone.additions().contains(RECORD1)); - assertTrue(clone.additions().isEmpty()); - assertTrue(clone.deletions().contains(RECORD3)); + public void testEqualsAndToBuilder() { + expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS); + replay(dns); + ChangeRequest compare = changeRequest.toBuilder().build(); + assertEquals(changeRequest, compare); + assertEquals(changeRequest.hashCode(), compare.hashCode()); + compare = changeRequestPartial.toBuilder().build(); + assertEquals(changeRequestPartial, compare); + assertEquals(changeRequestPartial.hashCode(), compare.hashCode()); } @Test - public void testRemoveDeletion() { - ChangeRequest clone = CHANGE.toBuilder().removeDeletion(RECORD3).build(); - assertTrue(clone.deletions().isEmpty()); + public void testBuilder() { + // one for each build() call because it invokes a constructor + expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS); + replay(dns); + String id = changeRequest.id() + "aaa"; + assertEquals(id, changeRequest.toBuilder().id(id).build().id()); + ChangeRequest modified = + changeRequest.toBuilder().status(ChangeRequest.Status.PENDING).build(); + assertEquals(ChangeRequest.Status.PENDING, modified.status()); + modified = changeRequest.toBuilder().clearDeletions().build(); + assertTrue(modified.deletions().isEmpty()); + modified = changeRequest.toBuilder().clearAdditions().build(); + assertTrue(modified.additions().isEmpty()); + modified = changeRequest.toBuilder().additions(ImmutableList.of()).build(); + assertTrue(modified.additions().isEmpty()); + modified = changeRequest.toBuilder().deletions(ImmutableList.of()).build(); + assertTrue(modified.deletions().isEmpty()); + RecordSet cname = RecordSet.builder("last", RecordSet.Type.CNAME).build(); + modified = changeRequest.toBuilder().add(cname).build(); + assertTrue(modified.additions().contains(cname)); + modified = changeRequest.toBuilder().delete(cname).build(); + assertTrue(modified.deletions().contains(cname)); + modified = changeRequest.toBuilder().startTimeMillis(0L).build(); + assertEquals(new Long(0), modified.startTimeMillis()); } @Test - public void testDateParsing() { - String startTime = "2016-01-26T18:33:43.512Z"; // obtained from service - com.google.api.services.dns.model.Change change = CHANGE.toPb().setStartTime(startTime); - ChangeRequest converted = ChangeRequest.fromPb(change); - assertNotNull(converted.startTimeMillis()); - assertEquals(change, converted.toPb()); - assertEquals(change.getStartTime(), converted.toPb().getStartTime()); + public void testApplyTo() { + expect(dns.applyChangeRequest(ZONE_NAME, changeRequest)).andReturn(changeRequest); + expect(dns.applyChangeRequest(ZONE_NAME, changeRequest, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME))) + .andReturn(changeRequest); + replay(dns); + changeRequest.applyTo(); + changeRequest.applyTo(Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); } } diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java index ab2dba0a566c..73548e9cbb91 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java @@ -53,10 +53,10 @@ public class DnsImplTest { private static final String PAGE_TOKEN = "some token"; private static final ZoneInfo ZONE_INFO = ZoneInfo.of(ZONE_NAME, DNS_NAME, DESCRIPTION); private static final ProjectInfo PROJECT_INFO = ProjectInfo.builder().build(); - private static final ChangeRequest CHANGE_REQUEST_PARTIAL = ChangeRequest.builder() + private static final ChangeRequestInfo CHANGE_REQUEST_PARTIAL = ChangeRequest.builder() .add(DNS_RECORD1) .build(); - private static final ChangeRequest CHANGE_REQUEST_COMPLETE = ChangeRequest.builder() + private static final ChangeRequestInfo CHANGE_REQUEST_COMPLETE = ChangeRequest.builder() .add(DNS_RECORD1) .startTimeMillis(123L) .status(ChangeRequest.Status.PENDING) diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/SerializationTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/SerializationTest.java index c06cd096bf1e..4e492eff3526 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/SerializationTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/SerializationTest.java @@ -63,7 +63,10 @@ public class SerializationTest extends BaseSerializationTest { private static final Zone FULL_ZONE = new Zone(DNS, new ZoneInfo.BuilderImpl(FULL_ZONE_INFO)); private static final Zone PARTIAL_ZONE = new Zone(DNS, new ZoneInfo.BuilderImpl(PARTIAL_ZONE_INFO)); - private static final ChangeRequest CHANGE_REQUEST_PARTIAL = ChangeRequest.builder().build(); + private static final ChangeRequestInfo CHANGE_REQUEST_INFO_PARTIAL = + ChangeRequest.builder().build(); + private static final ChangeRequest CHANGE_REQUEST_PARTIAL = new ChangeRequest(DNS, "name", + new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_INFO_PARTIAL)); private static final RecordSet RECORD_SET_PARTIAL = RecordSet.builder("www.www.com", RecordSet.Type.AAAA).build(); private static final RecordSet RECORD_SET_COMPLETE = @@ -71,13 +74,15 @@ public class SerializationTest extends BaseSerializationTest { .ttl(12, TimeUnit.HOURS) .addRecord("record") .build(); - private static final ChangeRequest CHANGE_REQUEST_COMPLETE = ChangeRequest.builder() + private static final ChangeRequestInfo CHANGE_REQUEST_INFO_COMPLETE = ChangeRequest.builder() .add(RECORD_SET_COMPLETE) .delete(RECORD_SET_PARTIAL) .status(ChangeRequest.Status.PENDING) .id("some id") .startTimeMillis(132L) .build(); + private static final ChangeRequest CHANGE_REQUEST_COMPLETE = new ChangeRequest(DNS, "name", + new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_INFO_COMPLETE)); @Override protected Serializable[] serializableObjects() { @@ -91,8 +96,9 @@ protected Serializable[] serializableObjects() { return new Serializable[]{FULL_ZONE_INFO, PARTIAL_ZONE_INFO, ZONE_LIST_OPTION, RECORD_SET_LIST_OPTION, CHANGE_REQUEST_LIST_OPTION, ZONE_OPTION, CHANGE_REQUEST_OPTION, PROJECT_OPTION, PARTIAL_PROJECT_INFO, FULL_PROJECT_INFO, OPTIONS, FULL_ZONE, PARTIAL_ZONE, - OPTIONS, CHANGE_REQUEST_PARTIAL, RECORD_SET_PARTIAL, RECORD_SET_COMPLETE, - CHANGE_REQUEST_COMPLETE, options, otherOptions}; + OPTIONS, CHANGE_REQUEST_INFO_PARTIAL, CHANGE_REQUEST_PARTIAL, RECORD_SET_PARTIAL, + RECORD_SET_COMPLETE, CHANGE_REQUEST_INFO_COMPLETE, CHANGE_REQUEST_COMPLETE, options, + otherOptions}; } @Override 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 bd59f8c140e9..3ed395bf6881 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 @@ -60,25 +60,29 @@ public class ZoneTest { 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 static final ChangeRequestInfo CHANGE_REQUEST = + ChangeRequest.builder().id("someid").build(); + private static final ChangeRequestInfo CHANGE_REQUEST_NO_ID = + ChangeRequest.builder().build(); private static final DnsException EXCEPTION = createStrictMock(DnsException.class); private static final DnsOptions OPTIONS = createStrictMock(DnsOptions.class); private Dns dns; private Zone zone; private Zone zoneNoId; + private ChangeRequest changeRequestAfter; @Before public void setUp() throws Exception { dns = createStrictMock(Dns.class); expect(dns.options()).andReturn(OPTIONS); expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS); replay(dns); zone = new Zone(dns, new ZoneInfo.BuilderImpl(ZONE_INFO)); zoneNoId = new Zone(dns, new ZoneInfo.BuilderImpl(NO_ID_INFO)); + changeRequestAfter = new ChangeRequest(dns, ZONE_NAME, new ChangeRequestInfo.BuilderImpl( + CHANGE_REQUEST.toBuilder().startTimeMillis(123465L).build())); reset(dns); } @@ -208,24 +212,24 @@ public void reloadByNameAndNotFound() { @Test public void applyChangeByNameAndFound() { expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST)) - .andReturn(CHANGE_REQUEST_AFTER); + .andReturn(changeRequestAfter); expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST)) - .andReturn(CHANGE_REQUEST_AFTER); + .andReturn(changeRequestAfter); // again for options expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(CHANGE_REQUEST_AFTER); + .andReturn(changeRequestAfter); expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(CHANGE_REQUEST_AFTER); + .andReturn(changeRequestAfter); replay(dns); ChangeRequest result = zoneNoId.applyChangeRequest(CHANGE_REQUEST); - assertEquals(CHANGE_REQUEST_AFTER, result); + assertEquals(changeRequestAfter, result); result = zone.applyChangeRequest(CHANGE_REQUEST); - assertEquals(CHANGE_REQUEST_AFTER, result); + assertEquals(changeRequestAfter, result); // check options result = zoneNoId.applyChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS); - assertEquals(CHANGE_REQUEST_AFTER, result); + assertEquals(changeRequestAfter, result); result = zone.applyChangeRequest(CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS); - assertEquals(CHANGE_REQUEST_AFTER, result); + assertEquals(changeRequestAfter, result); } @Test @@ -298,24 +302,24 @@ public void applyNullChangeRequest() { @Test public void getChangeAndZoneFoundByName() { expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())) - .andReturn(CHANGE_REQUEST_AFTER); + .andReturn(changeRequestAfter); expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())) - .andReturn(CHANGE_REQUEST_AFTER); + .andReturn(changeRequestAfter); // again for options expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(CHANGE_REQUEST_AFTER); + .andReturn(changeRequestAfter); expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(CHANGE_REQUEST_AFTER); + .andReturn(changeRequestAfter); replay(dns); ChangeRequest result = zoneNoId.getChangeRequest(CHANGE_REQUEST.id()); - assertEquals(CHANGE_REQUEST_AFTER, result); + assertEquals(changeRequestAfter, result); result = zone.getChangeRequest(CHANGE_REQUEST.id()); - assertEquals(CHANGE_REQUEST_AFTER, result); + assertEquals(changeRequestAfter, result); // check options result = zoneNoId.getChangeRequest(CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS); - assertEquals(CHANGE_REQUEST_AFTER, result); + assertEquals(changeRequestAfter, result); result = zone.getChangeRequest(CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS); - assertEquals(CHANGE_REQUEST_AFTER, result); + assertEquals(changeRequestAfter, result); } @Test diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java index 8f7626a5ae0a..dd8e21043181 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java @@ -26,6 +26,7 @@ import com.google.common.collect.ImmutableList; import com.google.gcloud.Page; import com.google.gcloud.dns.ChangeRequest; +import com.google.gcloud.dns.ChangeRequestInfo; import com.google.gcloud.dns.Dns; import com.google.gcloud.dns.DnsException; import com.google.gcloud.dns.DnsOptions; @@ -76,11 +77,11 @@ public class ITDnsTest { .records(ImmutableList.of("ed:ed:12:aa:36:3:3:105")) .ttl(25, TimeUnit.SECONDS) .build(); - private static final ChangeRequest CHANGE_ADD_ZONE1 = ChangeRequest.builder() + private static final ChangeRequestInfo CHANGE_ADD_ZONE1 = ChangeRequest.builder() .add(A_RECORD_ZONE1) .add(AAAA_RECORD_ZONE1) .build(); - private static final ChangeRequest CHANGE_DELETE_ZONE1 = ChangeRequest.builder() + private static final ChangeRequestInfo CHANGE_DELETE_ZONE1 = ChangeRequest.builder() .delete(A_RECORD_ZONE1) .delete(AAAA_RECORD_ZONE1) .build(); @@ -593,7 +594,7 @@ public void testInvalidChangeRequest() { .records(ImmutableList.of("0.255.1.5")) .build(); try { - ChangeRequest validChange = ChangeRequest.builder().add(validA).build(); + ChangeRequestInfo validChange = ChangeRequest.builder().add(validA).build(); zone.applyChangeRequest(validChange); try { zone.applyChangeRequest(validChange); @@ -605,7 +606,7 @@ public void testInvalidChangeRequest() { } // delete with field mismatch RecordSet mismatch = validA.toBuilder().ttl(20, TimeUnit.SECONDS).build(); - ChangeRequest deletion = ChangeRequest.builder().delete(mismatch).build(); + ChangeRequestInfo deletion = ChangeRequest.builder().delete(mismatch).build(); try { zone.applyChangeRequest(deletion); fail("Deleted a record set without a complete match."); @@ -629,7 +630,7 @@ public void testInvalidChangeRequest() { } } deletion = deletion.toBuilder().deletions(deletions).build(); - ChangeRequest addition = ChangeRequest.builder().additions(additions).build(); + ChangeRequestInfo addition = ChangeRequest.builder().additions(additions).build(); try { zone.applyChangeRequest(deletion); fail("Deleted SOA."); @@ -647,7 +648,7 @@ public void testInvalidChangeRequest() { assertEquals(400, ex.code()); } } finally { - ChangeRequest deletion = ChangeRequest.builder().delete(validA).build(); + ChangeRequestInfo deletion = ChangeRequest.builder().delete(validA).build(); ChangeRequest request = zone.applyChangeRequest(deletion); waitForChangeToComplete(zone.name(), request.id()); zone.delete(); From de8bf4bad79ffe422688fd69d12fa388bfb8b44d Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Fri, 25 Mar 2016 09:49:23 -0700 Subject: [PATCH 31/60] Addressed comments. This includes: - Moved Status enum to ChangeRequestInfo. - Switching from ChangeRequest.builer() to ChangeRequestInfo.builder() - Fixing README accordingly - Used times() for mocking instead of repeated expect() - Snippets and documentation work with ChangeRequestInfo. Fixes #788. - Equals uses getClass instead if instanceof. - Removed unnecessary imports. --- README.md | 6 +- gcloud-java-dns/README.md | 26 ++++-- .../com/google/gcloud/dns/ChangeRequest.java | 81 ++++++++--------- .../google/gcloud/dns/ChangeRequestInfo.java | 35 +++++--- .../java/com/google/gcloud/dns/DnsImpl.java | 5 +- .../main/java/com/google/gcloud/dns/Zone.java | 2 +- .../com/google/gcloud/dns/package-info.java | 2 +- .../google/gcloud/dns/ChangeRequestTest.java | 43 ++++----- .../com/google/gcloud/dns/DnsImplTest.java | 32 ++++--- .../google/gcloud/dns/SerializationTest.java | 2 +- .../java/com/google/gcloud/dns/ZoneTest.java | 90 ++++++------------- .../gcloud/examples/dns/DnsExample.java | 11 +-- .../snippets/CreateOrUpdateRecordSets.java | 6 +- .../examples/dns/snippets/DeleteZone.java | 8 +- .../ManipulateZonesAndRecordSets.java | 9 +- 15 files changed, 171 insertions(+), 187 deletions(-) diff --git a/README.md b/README.md index 52229f6d5d34..6061d9dd4c8f 100644 --- a/README.md +++ b/README.md @@ -252,7 +252,7 @@ Zone zone = dns.create(zoneInfo); The second snippet shows how to create records inside a zone. The complete code can be found on [CreateOrUpdateRecordSets.java](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateOrUpdateRecordSets.java). ```java -import com.google.gcloud.dns.ChangeRequest; +import com.google.gcloud.dns.ChangeRequestInfo; import com.google.gcloud.dns.Dns; import com.google.gcloud.dns.DnsOptions; import com.google.gcloud.dns.RecordSet; @@ -269,7 +269,7 @@ RecordSet toCreate = RecordSet.builder("www.someexampledomain.com.", RecordSet.T .ttl(24, TimeUnit.HOURS) .addRecord(ip) .build(); -ChangeRequest.Builder changeBuilder = ChangeRequest.builder().add(toCreate); +ChangeRequestInfo.Builder changeBuilder = ChangeRequestInfo.builder().add(toCreate); // Verify that the record does not exist yet. // If it does exist, we will overwrite it with our prepared record. @@ -282,7 +282,7 @@ while (recordSetIterator.hasNext()) { } } -ChangeRequest changeRequest = changeBuilder.build(); +ChangeRequestInfo changeRequest = changeBuilder.build(); zone.applyChangeRequest(changeRequest); ``` diff --git a/gcloud-java-dns/README.md b/gcloud-java-dns/README.md index a2c3238d1f8f..d2e4c85b3b76 100644 --- a/gcloud-java-dns/README.md +++ b/gcloud-java-dns/README.md @@ -159,7 +159,7 @@ our zone that creates a record set of type A and points URL www.someexampledomai IP address 12.13.14.15. Start by adding ```java -import com.google.gcloud.dns.ChangeRequest; +import com.google.gcloud.dns.ChangeRequestInfo; import com.google.gcloud.dns.RecordSet; import java.util.concurrent.TimeUnit; @@ -176,7 +176,7 @@ RecordSet toCreate = RecordSet.builder("www." + zone.dnsName(), RecordSet.Type.A .build(); // Make a change -ChangeRequest changeRequest = ChangeRequest.builder().add(toCreate).build(); +ChangeRequestInfo changeRequest = ChangeRequestInfo.builder().add(toCreate).build(); // Build and apply the change request to our zone changeRequest = zone.applyChangeRequest(changeRequest); @@ -198,7 +198,7 @@ and in the code ```java // Make a change -ChangeRequest.Builder changeBuilder = ChangeRequest.builder().add(toCreate); +ChangeRequestInfo.Builder changeBuilder = ChangeRequestInfo.builder().add(toCreate); // Verify the type A record does not exist yet. // If it does exist, we will overwrite it with our prepared record. @@ -211,7 +211,7 @@ while (recordSetIterator.hasNext()) { } // Build and apply the change request to our zone -ChangeRequest changeRequest = changeBuilder.build(); +ChangeRequestInfo changeRequest = changeBuilder.build(); zone.applyChangeRequest(changeRequest); ``` You can find more information about changes in the [Cloud DNS documentation] (https://cloud.google.com/dns/what-is-cloud-dns#cloud_dns_api_concepts). @@ -220,7 +220,7 @@ When the change request is applied, it is registered with the Cloud DNS service can wait for its completion as follows: ```java -while (ChangeRequest.Status.PENDING.equals(changeRequest.status())) { +while (ChangeRequestInfo.Status.PENDING.equals(changeRequest.status())) { try { Thread.sleep(500L); } catch (InterruptedException e) { @@ -262,9 +262,17 @@ while (recordSetIterator.hasNext()) { } ``` -You can also list the history of change requests that were applied to a zone: +You can also list the history of change requests that were applied to a zone. +First add: ```java +import java.util.ChangeRequest; +``` + +and then: + +```java + // List the change requests applied to a particular zone Iterator changeIterator = zone.listChangeRequests().iterateAll(); System.out.println(String.format("The history of changes in %s:", zone.name())); @@ -280,7 +288,7 @@ First, you need to empty the zone by deleting all its records except for the def ```java // Make a change for deleting the record sets -changeBuilder = ChangeRequest.builder(); +changeBuilder = ChangeRequestInfo.builder(); while (recordIterator.hasNext()) { RecordSet current = recordIterator.next(); // SOA and NS records cannot be deleted @@ -290,14 +298,14 @@ while (recordIterator.hasNext()) { } // Build and apply the change request to our zone if it contains records to delete -ChangeRequest changeRequest = changeBuilder.build(); +ChangeRequestInfo changeRequest = changeBuilder.build(); if (!changeRequest.deletions().isEmpty()) { changeRequest = dns.applyChangeRequest(zoneName, changeRequest); // Wait for change to finish, but save data traffic by transferring only ID and status Dns.ChangeRequestOption option = Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS); - while (ChangeRequest.Status.PENDING.equals(changeRequest.status())) { + while (ChangeRequestInfo.Status.PENDING.equals(changeRequest.status())) { System.out.println("Waiting for change to complete. Going to sleep for 500ms..."); try { Thread.sleep(500); diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java index ad1c65f7cb0f..4b6369976ca6 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java @@ -16,6 +16,8 @@ package com.google.gcloud.dns; +import static com.google.common.base.Preconditions.checkNotNull; + import com.google.api.services.dns.model.Change; import com.google.common.base.Function; @@ -25,41 +27,30 @@ import java.util.Objects; /** - * A class representing an atomic update to a collection of {@link RecordSet}s within a {@code - * Zone}. + * An immutable class representing an atomic update to a collection of {@link RecordSet}s within a + * {@code Zone}. * * @see Google Cloud DNS documentation */ public class ChangeRequest extends ChangeRequestInfo { - private static final long serialVersionUID = -9027378042756366333L; + private static final long serialVersionUID = 5335667200595081449L; private final DnsOptions options; - private final String zoneName; + private final String zone; private transient Dns dns; - /** - * This enumerates the possible states of a {@code ChangeRequest}. - * - * @see Google Cloud DNS - * documentation - */ - public enum Status { - PENDING, - DONE - } - /** * A builder for {@code ChangeRequest}s. */ public static class Builder extends ChangeRequestInfo.Builder { private final Dns dns; - private final String zoneName; + private final String zone; private final ChangeRequestInfo.BuilderImpl infoBuilder; private Builder(ChangeRequest cr) { this.dns = cr.dns; - this.zoneName = cr.zoneName; + this.zone = cr.zone; this.infoBuilder = new ChangeRequestInfo.BuilderImpl(cr); } @@ -131,45 +122,36 @@ Builder status(Status status) { @Override public ChangeRequest build() { - return new ChangeRequest(dns, zoneName, infoBuilder); + return new ChangeRequest(dns, zone, infoBuilder); } } - ChangeRequest(Dns dns, String zoneName, ChangeRequest.BuilderImpl infoBuilder) { + ChangeRequest(Dns dns, String zone, ChangeRequest.BuilderImpl infoBuilder) { super(infoBuilder); - this.zoneName = zoneName; - this.dns = dns; + this.zone = checkNotNull(zone); + this.dns = checkNotNull(dns); this.options = dns.options(); } - static Function fromPbFunction(final Dns dns, final String zoneName) { - return new Function() { - @Override - public ChangeRequest apply(com.google.api.services.dns.model.Change pb) { - return ChangeRequest.fromPb(dns, zoneName, pb); - } - }; - } - /** * Returns the name of the {@link Zone} associated with this change request. */ - public String zoneName() { - return this.zoneName; + public String zone() { + return this.zone; } /** - * Returns the {@link Dns} service object associated with this change request. + * Returns the change request's {@code Dns} object used to issue requests. */ public Dns dns() { return dns; } /** - * Applies this change request to a zone that it is associated with. + * Applies this change request to the associated zone. */ public ChangeRequest applyTo(Dns.ChangeRequestOption... options) { - return dns.applyChangeRequest(zoneName, this, options); + return dns.applyChangeRequest(zone, this, options); } @Override @@ -179,24 +161,37 @@ public Builder toBuilder() { @Override public boolean equals(Object obj) { - return obj instanceof ChangeRequest && Objects.equals(toPb(), ((ChangeRequest) obj).toPb()) - && Objects.equals(options, ((ChangeRequest) obj).options) - && Objects.equals(zoneName, ((ChangeRequest) obj).zoneName); + if (obj == null || !obj.getClass().equals(ChangeRequest.class)) { + return false; + } else { + ChangeRequest other = (ChangeRequest) obj; + return Objects.equals(options, other.options) + && Objects.equals(zone, other.zone) + && Objects.equals(toPb(), other.toPb()); + } } @Override public int hashCode() { - return Objects.hash(super.hashCode(), options, zoneName); + return Objects.hash(super.hashCode(), options, zone); } - private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { - in.defaultReadObject(); + private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException { + input.defaultReadObject(); this.dns = options.service(); } - static ChangeRequest fromPb(Dns dns, String zoneName, - com.google.api.services.dns.model.Change pb) { + static ChangeRequest fromPb(Dns dns, String zoneName, Change pb) { ChangeRequestInfo info = ChangeRequestInfo.fromPb(pb); return new ChangeRequest(dns, zoneName, new ChangeRequestInfo.BuilderImpl(info)); } + + static Function fromPbFunction(final Dns dns, final String zoneName) { + return new Function() { + @Override + public ChangeRequest apply(com.google.api.services.dns.model.Change pb) { + return ChangeRequest.fromPb(dns, zoneName, pb); + } + }; + } } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequestInfo.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequestInfo.java index 25b915521b13..b63b4f4a0788 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequestInfo.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequestInfo.java @@ -52,7 +52,18 @@ public ChangeRequestInfo apply(Change pb) { private final List deletions; private final String id; private final Long startTimeMillis; - private final ChangeRequest.Status status; + private final ChangeRequestInfo.Status status; + + /** + * This enumerates the possible states of a change request. + * + * @see Google Cloud DNS + * documentation + */ + public enum Status { + PENDING, + DONE + } /** * A builder for {@code ChangeRequestInfo}. @@ -110,7 +121,7 @@ public abstract static class Builder { /** * Associates a server-assigned id to this {@code ChangeRequestInfo}. */ - abstract Builder id(String id); + abstract Builder id(String id); /** * Sets the time when this change request was started by a server. @@ -134,7 +145,7 @@ static class BuilderImpl extends Builder { private List deletions; private String id; private Long startTimeMillis; - private ChangeRequest.Status status; + private ChangeRequestInfo.Status status; BuilderImpl() { this.additions = new LinkedList<>(); @@ -215,7 +226,7 @@ Builder startTimeMillis(long startTimeMillis) { } @Override - Builder status(ChangeRequest.Status status) { + Builder status(ChangeRequestInfo.Status status) { this.status = checkNotNull(status); return this; } @@ -274,15 +285,15 @@ public Long startTimeMillis() { } /** - * Returns the status of this {@code ChangeRequest}. + * Returns the status of this {@code ChangeRequest}. If the change request has not been applied + * yet, the status is {@code PENDING}. */ - public ChangeRequest.Status status() { + public ChangeRequestInfo.Status status() { return status; } - com.google.api.services.dns.model.Change toPb() { - com.google.api.services.dns.model.Change pb = - new com.google.api.services.dns.model.Change(); + Change toPb() { + Change pb = new Change(); // set id if (id() != null) { pb.setId(id()); @@ -302,7 +313,7 @@ com.google.api.services.dns.model.Change toPb() { return pb; } - static ChangeRequestInfo fromPb(com.google.api.services.dns.model.Change pb) { + static ChangeRequestInfo fromPb(Change pb) { Builder builder = builder(); if (pb.getId() != null) { builder.id(pb.getId()); @@ -325,8 +336,8 @@ static ChangeRequestInfo fromPb(com.google.api.services.dns.model.Change pb) { @Override public boolean equals(Object other) { - return (other instanceof ChangeRequestInfo) - && toPb().equals(((ChangeRequestInfo) other).toPb()); + return other != null && other.getClass().equals(ChangeRequestInfo.class) + && other instanceof ChangeRequestInfo && toPb().equals(((ChangeRequestInfo) other).toPb()); } @Override diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java index 4e2bd1b207d5..51ab0bd92720 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java @@ -19,7 +19,6 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.gcloud.RetryHelper.RetryHelperException; import static com.google.gcloud.RetryHelper.runWithRetries; -import static com.google.gcloud.dns.ChangeRequest.fromPb; import com.google.api.services.dns.model.Change; import com.google.api.services.dns.model.ManagedZone; @@ -170,7 +169,7 @@ public DnsRpc.ListResult call() { Iterable changes = result.results() == null ? ImmutableList.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) { @@ -285,7 +284,7 @@ public com.google.api.services.dns.model.Change call() { return dnsRpc.applyChangeRequest(zoneName, changeRequest.toPb(), optionsMap); } }, options().retryParams(), EXCEPTION_HANDLER); - return answer == null ? null : fromPb(this, zoneName, answer); // should never be null + return answer == null ? null : ChangeRequest.fromPb(this, zoneName, answer); // not null } catch (RetryHelper.RetryHelperException ex) { throw DnsException.translateAndThrow(ex); } 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 88b9e7273e9c..9930bfdbad67 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 @@ -146,7 +146,7 @@ public Page listRecordSets(Dns.RecordSetListOption... options) { } /** - * Submits {@link ChangeRequest} to the service for it to applied to this zone. The method + * Submits {@link ChangeRequestInfo} to the service for it to applied to this zone. The method * searches for zone by name. * * @param options optional restriction on what fields of {@link ChangeRequest} should be returned diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/package-info.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/package-info.java index 69bee930df62..36f41852400c 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/package-info.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/package-info.java @@ -46,7 +46,7 @@ * .ttl(24, TimeUnit.HOURS) * .addRecord(ip) * .build(); - * ChangeRequest changeRequest = ChangeRequest.builder().add(toCreate).build(); + * ChangeRequestInfo changeRequest = ChangeRequestInfo.builder().add(toCreate).build(); * zone.applyChangeRequest(changeRequest); * } * diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java index 8d6cc799cad8..bfd1d0f512f4 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java @@ -23,6 +23,7 @@ import static org.easymock.EasyMock.verify; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import com.google.common.collect.ImmutableList; @@ -47,8 +48,7 @@ public class ChangeRequestTest { @Before public void setUp() throws Exception { dns = createStrictMock(Dns.class); - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS).times(2); replay(dns); changeRequest = new ChangeRequest(dns, ZONE_NAME, new ChangeRequestInfo.BuilderImpl( CHANGE_REQUEST_INFO.toBuilder() @@ -68,28 +68,28 @@ public void tearDown() throws Exception { @Test public void testConstructor() { + expect(dns.options()).andReturn(OPTIONS); replay(dns); - assertEquals(CHANGE_REQUEST_INFO.toPb(), changeRequestPartial.toPb()); + assertEquals(new ChangeRequest(dns, ZONE_NAME, + new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_INFO)), changeRequestPartial); assertNotNull(changeRequest.dns()); - assertEquals(ZONE_NAME, changeRequest.zoneName()); - assertNotNull(changeRequestPartial.dns()); - assertEquals(ZONE_NAME, changeRequestPartial.zoneName()); + assertEquals(ZONE_NAME, changeRequest.zone()); + assertSame(dns, changeRequestPartial.dns()); + assertEquals(ZONE_NAME, changeRequestPartial.zone()); } @Test public void testFromPb() { - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS).times(2); replay(dns); - assertEquals(ChangeRequest.fromPb(dns, ZONE_NAME, changeRequest.toPb()), changeRequest); - assertEquals(ChangeRequest.fromPb(dns, ZONE_NAME, changeRequestPartial.toPb()), - changeRequestPartial); + assertEquals(changeRequest, ChangeRequest.fromPb(dns, ZONE_NAME, changeRequest.toPb())); + assertEquals(changeRequestPartial, + ChangeRequest.fromPb(dns, ZONE_NAME, changeRequestPartial.toPb())); } @Test public void testEqualsAndToBuilder() { - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS).times(2); replay(dns); ChangeRequest compare = changeRequest.toBuilder().build(); assertEquals(changeRequest, compare); @@ -102,15 +102,7 @@ public void testEqualsAndToBuilder() { @Test public void testBuilder() { // one for each build() call because it invokes a constructor - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS).times(9); replay(dns); String id = changeRequest.id() + "aaa"; assertEquals(id, changeRequest.toBuilder().id(id).build().id()); @@ -131,7 +123,7 @@ public void testBuilder() { modified = changeRequest.toBuilder().delete(cname).build(); assertTrue(modified.deletions().contains(cname)); modified = changeRequest.toBuilder().startTimeMillis(0L).build(); - assertEquals(new Long(0), modified.startTimeMillis()); + assertEquals(Long.valueOf(0), modified.startTimeMillis()); } @Test @@ -141,7 +133,8 @@ public void testApplyTo() { Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME))) .andReturn(changeRequest); replay(dns); - changeRequest.applyTo(); - changeRequest.applyTo(Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); + assertSame(changeRequest, changeRequest.applyTo()); + assertSame(changeRequest, + changeRequest.applyTo(Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME))); } } diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java index 73548e9cbb91..94ed4a3da3f7 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java @@ -53,10 +53,10 @@ public class DnsImplTest { private static final String PAGE_TOKEN = "some token"; private static final ZoneInfo ZONE_INFO = ZoneInfo.of(ZONE_NAME, DNS_NAME, DESCRIPTION); private static final ProjectInfo PROJECT_INFO = ProjectInfo.builder().build(); - private static final ChangeRequestInfo CHANGE_REQUEST_PARTIAL = ChangeRequest.builder() + private static final ChangeRequestInfo CHANGE_REQUEST_PARTIAL = ChangeRequestInfo.builder() .add(DNS_RECORD1) .build(); - private static final ChangeRequestInfo CHANGE_REQUEST_COMPLETE = ChangeRequest.builder() + private static final ChangeRequestInfo CHANGE_REQUEST_COMPLETE = ChangeRequestInfo.builder() .add(DNS_RECORD1) .startTimeMillis(123L) .status(ChangeRequest.Status.PENDING) @@ -221,7 +221,8 @@ public void testGetChangeRequest() { dns = options.service(); // creates DnsImpl ChangeRequest changeRequest = dns.getChangeRequest(ZONE_INFO.name(), CHANGE_REQUEST_COMPLETE.id()); - assertEquals(CHANGE_REQUEST_COMPLETE, changeRequest); + assertEquals(new ChangeRequest(dns, ZONE_INFO.name(), + new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_COMPLETE)), changeRequest); } @Test @@ -235,7 +236,8 @@ public void testGetChangeRequestWithOptions() { ChangeRequest changeRequest = dns.getChangeRequest(ZONE_INFO.name(), CHANGE_REQUEST_COMPLETE.id(), CHANGE_GET_FIELDS); String selector = (String) capturedOptions.getValue().get(CHANGE_GET_FIELDS.rpcOption()); - assertEquals(CHANGE_REQUEST_COMPLETE, changeRequest); + assertEquals(new ChangeRequest(dns, ZONE_INFO.name(), + new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_COMPLETE)), changeRequest); assertTrue(selector.contains(Dns.ChangeRequestField.STATUS.selector())); assertTrue(selector.contains(Dns.ChangeRequestField.ID.selector())); } @@ -248,7 +250,8 @@ public void testApplyChangeRequest() { dns = options.service(); // creates DnsImpl ChangeRequest changeRequest = dns.applyChangeRequest(ZONE_INFO.name(), CHANGE_REQUEST_PARTIAL); - assertEquals(CHANGE_REQUEST_COMPLETE, changeRequest); + assertEquals(new ChangeRequest(dns, ZONE_INFO.name(), + new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_COMPLETE)), changeRequest); } @Test @@ -262,7 +265,8 @@ public void testApplyChangeRequestWithOptions() { ChangeRequest changeRequest = dns.applyChangeRequest(ZONE_INFO.name(), CHANGE_REQUEST_PARTIAL, CHANGE_GET_FIELDS); String selector = (String) capturedOptions.getValue().get(CHANGE_GET_FIELDS.rpcOption()); - assertEquals(CHANGE_REQUEST_COMPLETE, changeRequest); + assertEquals(new ChangeRequest(dns, ZONE_INFO.name(), + new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_COMPLETE)), changeRequest); assertTrue(selector.contains(Dns.ChangeRequestField.STATUS.selector())); assertTrue(selector.contains(Dns.ChangeRequestField.ID.selector())); } @@ -275,8 +279,12 @@ public void testListChangeRequests() { EasyMock.replay(dnsRpcMock); dns = options.service(); // creates DnsImpl Page changeRequestPage = dns.listChangeRequests(ZONE_INFO.name()); - assertTrue(Lists.newArrayList(changeRequestPage.values()).contains(CHANGE_REQUEST_COMPLETE)); - assertTrue(Lists.newArrayList(changeRequestPage.values()).contains(CHANGE_REQUEST_PARTIAL)); + assertTrue(Lists.newArrayList(changeRequestPage.values()).contains( + new ChangeRequest(dns, ZONE_INFO.name(), + new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_COMPLETE)))); + assertTrue(Lists.newArrayList(changeRequestPage.values()).contains( + new ChangeRequest(dns, ZONE_INFO.name(), + new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_PARTIAL)))); assertEquals(2, Lists.newArrayList(changeRequestPage.values()).size()); } @@ -288,8 +296,12 @@ public void testListChangeRequestsWithOptions() { EasyMock.replay(dnsRpcMock); dns = options.service(); // creates DnsImpl Page changeRequestPage = dns.listChangeRequests(ZONE_NAME, CHANGE_LIST_OPTIONS); - assertTrue(Lists.newArrayList(changeRequestPage.values()).contains(CHANGE_REQUEST_COMPLETE)); - assertTrue(Lists.newArrayList(changeRequestPage.values()).contains(CHANGE_REQUEST_PARTIAL)); + assertTrue(Lists.newArrayList(changeRequestPage.values()).contains( + new ChangeRequest(dns, ZONE_INFO.name(), + new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_COMPLETE)))); + assertTrue(Lists.newArrayList(changeRequestPage.values()).contains( + new ChangeRequest(dns, ZONE_INFO.name(), + new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_PARTIAL)))); assertEquals(2, Lists.newArrayList(changeRequestPage.values()).size()); Integer size = (Integer) capturedOptions.getValue().get(CHANGE_LIST_OPTIONS[0].rpcOption()); assertEquals(MAX_SIZE, size); diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/SerializationTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/SerializationTest.java index 4e492eff3526..ad25b31068dd 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/SerializationTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/SerializationTest.java @@ -74,7 +74,7 @@ public class SerializationTest extends BaseSerializationTest { .ttl(12, TimeUnit.HOURS) .addRecord("record") .build(); - private static final ChangeRequestInfo CHANGE_REQUEST_INFO_COMPLETE = ChangeRequest.builder() + private static final ChangeRequestInfo CHANGE_REQUEST_INFO_COMPLETE = ChangeRequestInfo.builder() .add(RECORD_SET_COMPLETE) .delete(RECORD_SET_PARTIAL) .status(ChangeRequest.Status.PENDING) 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 3ed395bf6881..ba4493abfca8 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 @@ -61,9 +61,9 @@ public class ZoneTest { private static final Dns.ChangeRequestListOption CHANGE_REQUEST_LIST_OPTIONS = Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.START_TIME); private static final ChangeRequestInfo CHANGE_REQUEST = - ChangeRequest.builder().id("someid").build(); + ChangeRequestInfo.builder().id("someid").build(); private static final ChangeRequestInfo CHANGE_REQUEST_NO_ID = - ChangeRequest.builder().build(); + ChangeRequestInfo.builder().build(); private static final DnsException EXCEPTION = createStrictMock(DnsException.class); private static final DnsOptions OPTIONS = createStrictMock(DnsOptions.class); @@ -75,9 +75,7 @@ public class ZoneTest { @Before public void setUp() throws Exception { dns = createStrictMock(Dns.class); - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS).times(3); replay(dns); zone = new Zone(dns, new ZoneInfo.BuilderImpl(ZONE_INFO)); zoneNoId = new Zone(dns, new ZoneInfo.BuilderImpl(NO_ID_INFO)); @@ -101,8 +99,7 @@ public void testConstructor() { @Test public void deleteByNameAndFound() { - expect(dns.delete(ZONE_NAME)).andReturn(true); - expect(dns.delete(ZONE_NAME)).andReturn(true); + expect(dns.delete(ZONE_NAME)).andReturn(true).times(2); replay(dns); boolean result = zone.delete(); assertTrue(result); @@ -112,8 +109,7 @@ public void deleteByNameAndFound() { @Test public void deleteByNameAndNotFound() { - expect(dns.delete(ZONE_NAME)).andReturn(false); - expect(dns.delete(ZONE_NAME)).andReturn(false); + expect(dns.delete(ZONE_NAME)).andReturn(false).times(2); replay(dns); boolean result = zoneNoId.delete(); assertFalse(result); @@ -126,11 +122,9 @@ public void listDnsRecordsByNameAndFound() { @SuppressWarnings("unchecked") Page pageMock = createStrictMock(Page.class); replay(pageMock); - expect(dns.listRecordSets(ZONE_NAME)).andReturn(pageMock); - expect(dns.listRecordSets(ZONE_NAME)).andReturn(pageMock); + expect(dns.listRecordSets(ZONE_NAME)).andReturn(pageMock).times(2); // again for options - expect(dns.listRecordSets(ZONE_NAME, DNS_RECORD_OPTIONS)).andReturn(pageMock); - expect(dns.listRecordSets(ZONE_NAME, DNS_RECORD_OPTIONS)).andReturn(pageMock); + expect(dns.listRecordSets(ZONE_NAME, DNS_RECORD_OPTIONS)).andReturn(pageMock).times(2); replay(dns); Page result = zone.listRecordSets(); assertSame(pageMock, result); @@ -143,11 +137,9 @@ public void listDnsRecordsByNameAndFound() { @Test public void listDnsRecordsByNameAndNotFound() { - expect(dns.listRecordSets(ZONE_NAME)).andThrow(EXCEPTION); - expect(dns.listRecordSets(ZONE_NAME)).andThrow(EXCEPTION); + expect(dns.listRecordSets(ZONE_NAME)).andThrow(EXCEPTION).times(2); // again for options - expect(dns.listRecordSets(ZONE_NAME, DNS_RECORD_OPTIONS)).andThrow(EXCEPTION); - expect(dns.listRecordSets(ZONE_NAME, DNS_RECORD_OPTIONS)).andThrow(EXCEPTION); + expect(dns.listRecordSets(ZONE_NAME, DNS_RECORD_OPTIONS)).andThrow(EXCEPTION).times(2); replay(dns); try { zoneNoId.listRecordSets(); @@ -177,8 +169,7 @@ public void listDnsRecordsByNameAndNotFound() { @Test public void reloadByNameAndFound() { - expect(dns.getZone(ZONE_NAME)).andReturn(zone); - expect(dns.getZone(ZONE_NAME)).andReturn(zone); + expect(dns.getZone(ZONE_NAME)).andReturn(zone).times(2); // again for options expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(zoneNoId); expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(zone); @@ -195,11 +186,9 @@ public void reloadByNameAndFound() { @Test public void reloadByNameAndNotFound() { - expect(dns.getZone(ZONE_NAME)).andReturn(null); - expect(dns.getZone(ZONE_NAME)).andReturn(null); + expect(dns.getZone(ZONE_NAME)).andReturn(null).times(2); // again for options - expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(null); - expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(null); + expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(null).times(2); replay(dns); Zone result = zoneNoId.reload(); assertNull(result); @@ -235,13 +224,10 @@ public void applyChangeByNameAndFound() { @Test public void applyChangeByNameAndNotFound() { // ID is not set - expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST)).andThrow(EXCEPTION); - expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST)).andThrow(EXCEPTION); + expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST)).andThrow(EXCEPTION).times(2); // again for options expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS)) - .andThrow(EXCEPTION); - expect(dns.applyChangeRequest(ZONE_NAME, CHANGE_REQUEST, CHANGE_REQUEST_FIELD_OPTIONS)) - .andThrow(EXCEPTION); + .andThrow(EXCEPTION).times(2); replay(dns); try { zoneNoId.applyChangeRequest(CHANGE_REQUEST); @@ -302,14 +288,10 @@ public void applyNullChangeRequest() { @Test public void getChangeAndZoneFoundByName() { expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())) - .andReturn(changeRequestAfter); - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())) - .andReturn(changeRequestAfter); + .andReturn(changeRequestAfter).times(2); // again for options expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(changeRequestAfter); - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(changeRequestAfter); + .andReturn(changeRequestAfter).times(2); replay(dns); ChangeRequest result = zoneNoId.getChangeRequest(CHANGE_REQUEST.id()); assertEquals(changeRequestAfter, result); @@ -324,13 +306,10 @@ public void getChangeAndZoneFoundByName() { @Test public void getChangeAndZoneNotFoundByName() { - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())).andThrow(EXCEPTION); - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())).andThrow(EXCEPTION); + expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())).andThrow(EXCEPTION).times(2); // again for options expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)) - .andThrow(EXCEPTION); - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)) - .andThrow(EXCEPTION); + .andThrow(EXCEPTION).times(2); replay(dns); try { zoneNoId.getChangeRequest(CHANGE_REQUEST.id()); @@ -361,13 +340,10 @@ public void getChangeAndZoneNotFoundByName() { @Test public void getChangedWhichDoesNotExistZoneFound() { - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())).andReturn(null); - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())).andReturn(null); + expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())).andReturn(null).times(2); // 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); + .andReturn(null).times(2); replay(dns); assertNull(zoneNoId.getChangeRequest(CHANGE_REQUEST.id())); assertNull(zone.getChangeRequest(CHANGE_REQUEST.id())); @@ -438,13 +414,10 @@ public void listChangeRequestsAndZoneFound() { @SuppressWarnings("unchecked") Page pageMock = createStrictMock(Page.class); replay(pageMock); - expect(dns.listChangeRequests(ZONE_NAME)).andReturn(pageMock); - expect(dns.listChangeRequests(ZONE_NAME)).andReturn(pageMock); + expect(dns.listChangeRequests(ZONE_NAME)).andReturn(pageMock).times(2); // again for options expect(dns.listChangeRequests(ZONE_NAME, CHANGE_REQUEST_LIST_OPTIONS)) - .andReturn(pageMock); - expect(dns.listChangeRequests(ZONE_NAME, CHANGE_REQUEST_LIST_OPTIONS)) - .andReturn(pageMock); + .andReturn(pageMock).times(2); replay(dns); Page result = zoneNoId.listChangeRequests(); assertSame(pageMock, result); @@ -457,11 +430,10 @@ public void listChangeRequestsAndZoneFound() { @Test public void listChangeRequestsAndZoneNotFound() { - expect(dns.listChangeRequests(ZONE_NAME)).andThrow(EXCEPTION); - expect(dns.listChangeRequests(ZONE_NAME)).andThrow(EXCEPTION); + expect(dns.listChangeRequests(ZONE_NAME)).andThrow(EXCEPTION).times(2); // again for options - expect(dns.listChangeRequests(ZONE_NAME, CHANGE_REQUEST_LIST_OPTIONS)).andThrow(EXCEPTION); - expect(dns.listChangeRequests(ZONE_NAME, CHANGE_REQUEST_LIST_OPTIONS)).andThrow(EXCEPTION); + expect(dns.listChangeRequests(ZONE_NAME, CHANGE_REQUEST_LIST_OPTIONS)).andThrow(EXCEPTION) + .times(2); replay(dns); try { zoneNoId.listChangeRequests(); @@ -498,8 +470,7 @@ public void testFromPb() { @Test public void testEqualsAndToBuilder() { - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS).times(2); replay(dns); assertEquals(zone, zone.toBuilder().build()); assertEquals(zone.hashCode(), zone.toBuilder().build().hashCode()); @@ -508,14 +479,7 @@ public void testEqualsAndToBuilder() { @Test public void testBuilder() { // one for each build() call because it invokes a constructor - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); - expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS).times(8); replay(dns); assertNotEquals(zone, zone.toBuilder() .id((new BigInteger(zone.id())).add(BigInteger.ONE).toString()) diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java index 40ce61b07281..a9e5c5d25377 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java @@ -19,6 +19,7 @@ import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import com.google.gcloud.dns.ChangeRequest; +import com.google.gcloud.dns.ChangeRequestInfo; import com.google.gcloud.dns.Dns; import com.google.gcloud.dns.DnsOptions; import com.google.gcloud.dns.ProjectInfo; @@ -207,7 +208,7 @@ public void run(Dns dns, String... args) { .records(ImmutableList.of(ip)) .ttl(ttl, TimeUnit.SECONDS) .build(); - ChangeRequest changeRequest = ChangeRequest.builder() + ChangeRequestInfo changeRequest = ChangeRequest.builder() .delete(recordSet) .build(); changeRequest = dns.applyChangeRequest(zoneName, changeRequest); @@ -254,7 +255,7 @@ public void run(Dns dns, String... args) { .records(ImmutableList.of(ip)) .ttl(ttl, TimeUnit.SECONDS) .build(); - ChangeRequest changeRequest = ChangeRequest.builder().add(recordSet).build(); + ChangeRequestInfo changeRequest = ChangeRequest.builder().add(recordSet).build(); changeRequest = dns.applyChangeRequest(zoneName, changeRequest); System.out.printf("The request for adding A record %s for zone %s was successfully " + "submitted and assigned ID %s.%n", recordName, zoneName, changeRequest.id()); @@ -444,9 +445,9 @@ private static void printZone(Zone zone) { System.out.printf("Name servers: %s%n", Joiner.on(", ").join(zone.nameServers())); } - private static ChangeRequest waitForChangeToFinish(Dns dns, String zoneName, - ChangeRequest request) { - ChangeRequest current = request; + private static ChangeRequestInfo waitForChangeToFinish(Dns dns, String zoneName, + ChangeRequestInfo request) { + ChangeRequestInfo current = request; while (current.status().equals(ChangeRequest.Status.PENDING)) { System.out.print("."); try { diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateOrUpdateRecordSets.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateOrUpdateRecordSets.java index 74647daf666e..e3ddbb10fc0f 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateOrUpdateRecordSets.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateOrUpdateRecordSets.java @@ -22,7 +22,7 @@ package com.google.gcloud.examples.dns.snippets; -import com.google.gcloud.dns.ChangeRequest; +import com.google.gcloud.dns.ChangeRequestInfo; import com.google.gcloud.dns.Dns; import com.google.gcloud.dns.DnsOptions; import com.google.gcloud.dns.RecordSet; @@ -55,7 +55,7 @@ public static void main(String... args) { .build(); // Make a change - ChangeRequest.Builder changeBuilder = ChangeRequest.builder().add(toCreate); + ChangeRequestInfo.Builder changeBuilder = ChangeRequestInfo.builder().add(toCreate); // Verify a www.. type A record does not exist yet. // If it does exist, we will overwrite it with our prepared record. @@ -68,7 +68,7 @@ public static void main(String... args) { } // Build and apply the change request to our zone - ChangeRequest changeRequest = changeBuilder.build(); + ChangeRequestInfo changeRequest = changeBuilder.build(); zone.applyChangeRequest(changeRequest); } } diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java index 27377345b62f..63f26eeebb2a 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java @@ -22,7 +22,7 @@ package com.google.gcloud.examples.dns.snippets; -import com.google.gcloud.dns.ChangeRequest; +import com.google.gcloud.dns.ChangeRequestInfo; import com.google.gcloud.dns.Dns; import com.google.gcloud.dns.DnsOptions; import com.google.gcloud.dns.RecordSet; @@ -47,7 +47,7 @@ public static void main(String... args) { Iterator recordIterator = dns.listRecordSets(zoneName).iterateAll(); // Make a change for deleting the records - ChangeRequest.Builder changeBuilder = ChangeRequest.builder(); + ChangeRequestInfo.Builder changeBuilder = ChangeRequestInfo.builder(); while (recordIterator.hasNext()) { RecordSet current = recordIterator.next(); // SOA and NS records cannot be deleted @@ -57,14 +57,14 @@ public static void main(String... args) { } // Build and apply the change request to our zone if it contains records to delete - ChangeRequest changeRequest = changeBuilder.build(); + ChangeRequestInfo changeRequest = changeBuilder.build(); if (!changeRequest.deletions().isEmpty()) { changeRequest = dns.applyChangeRequest(zoneName, changeRequest); // Wait for change to finish, but save data traffic by transferring only ID and status Dns.ChangeRequestOption option = Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS); - while (ChangeRequest.Status.PENDING.equals(changeRequest.status())) { + while (ChangeRequestInfo.Status.PENDING.equals(changeRequest.status())) { System.out.println("Waiting for change to complete. Going to sleep for 500ms..."); try { Thread.sleep(500); diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java index 6d9d09d704a6..9c9a9e77289c 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java @@ -23,6 +23,7 @@ package com.google.gcloud.examples.dns.snippets; import com.google.gcloud.dns.ChangeRequest; +import com.google.gcloud.dns.ChangeRequestInfo; import com.google.gcloud.dns.Dns; import com.google.gcloud.dns.DnsOptions; import com.google.gcloud.dns.RecordSet; @@ -66,7 +67,7 @@ public static void main(String... args) { .build(); // Make a change - ChangeRequest.Builder changeBuilder = ChangeRequest.builder().add(toCreate); + ChangeRequestInfo.Builder changeBuilder = ChangeRequestInfo.builder().add(toCreate); // Verify the type A record does not exist yet. // If it does exist, we will overwrite it with our prepared record. @@ -79,10 +80,10 @@ public static void main(String... args) { } // Build and apply the change request to our zone - ChangeRequest changeRequest = changeBuilder.build(); + ChangeRequestInfo changeRequest = changeBuilder.build(); zone.applyChangeRequest(changeRequest); - while (ChangeRequest.Status.PENDING.equals(changeRequest.status())) { + while (ChangeRequestInfo.Status.PENDING.equals(changeRequest.status())) { try { Thread.sleep(500L); } catch (InterruptedException e) { @@ -115,7 +116,7 @@ public static void main(String... args) { } // Make a change for deleting the record sets - changeBuilder = ChangeRequest.builder(); + changeBuilder = ChangeRequestInfo.builder(); while (recordSetIterator.hasNext()) { RecordSet current = recordSetIterator.next(); // SOA and NS records cannot be deleted From f2f93bcfa3c07bb0bb415441e1cd3544ec21e084 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 28 Mar 2016 17:36:19 -0700 Subject: [PATCH 32/60] Release v0.1.6 --- gcloud-java-bigquery/pom.xml | 2 +- gcloud-java-contrib/pom.xml | 2 +- gcloud-java-core/pom.xml | 2 +- gcloud-java-datastore/pom.xml | 2 +- gcloud-java-dns/pom.xml | 2 +- gcloud-java-examples/pom.xml | 2 +- gcloud-java-resourcemanager/pom.xml | 2 +- gcloud-java-storage/pom.xml | 2 +- gcloud-java/pom.xml | 2 +- pom.xml | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/gcloud-java-bigquery/pom.xml b/gcloud-java-bigquery/pom.xml index 5c79f150c722..cd7883bccaab 100644 --- a/gcloud-java-bigquery/pom.xml +++ b/gcloud-java-bigquery/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.6-SNAPSHOT + 0.1.6 gcloud-java-bigquery diff --git a/gcloud-java-contrib/pom.xml b/gcloud-java-contrib/pom.xml index bd4a6458dc38..33cdb552411c 100644 --- a/gcloud-java-contrib/pom.xml +++ b/gcloud-java-contrib/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.6-SNAPSHOT + 0.1.6 gcloud-java-contrib diff --git a/gcloud-java-core/pom.xml b/gcloud-java-core/pom.xml index 6d0ed675b423..51d6baf213a9 100644 --- a/gcloud-java-core/pom.xml +++ b/gcloud-java-core/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.6-SNAPSHOT + 0.1.6 gcloud-java-core diff --git a/gcloud-java-datastore/pom.xml b/gcloud-java-datastore/pom.xml index f3b46e22b3c8..e97a2b943582 100644 --- a/gcloud-java-datastore/pom.xml +++ b/gcloud-java-datastore/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.6-SNAPSHOT + 0.1.6 gcloud-java-datastore diff --git a/gcloud-java-dns/pom.xml b/gcloud-java-dns/pom.xml index b55200b8fc7d..68c46c909f2a 100644 --- a/gcloud-java-dns/pom.xml +++ b/gcloud-java-dns/pom.xml @@ -13,7 +13,7 @@ com.google.gcloud gcloud-java-pom - 0.1.6-SNAPSHOT + 0.1.6 gcloud-java-dns diff --git a/gcloud-java-examples/pom.xml b/gcloud-java-examples/pom.xml index 111308658c2e..7b23f7ebe48d 100644 --- a/gcloud-java-examples/pom.xml +++ b/gcloud-java-examples/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.6-SNAPSHOT + 0.1.6 gcloud-java-examples diff --git a/gcloud-java-resourcemanager/pom.xml b/gcloud-java-resourcemanager/pom.xml index c0c48af48f1e..d428d4a937fc 100644 --- a/gcloud-java-resourcemanager/pom.xml +++ b/gcloud-java-resourcemanager/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.6-SNAPSHOT + 0.1.6 gcloud-java-resourcemanager diff --git a/gcloud-java-storage/pom.xml b/gcloud-java-storage/pom.xml index 16427d50de3a..59b4f9a55245 100644 --- a/gcloud-java-storage/pom.xml +++ b/gcloud-java-storage/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.6-SNAPSHOT + 0.1.6 gcloud-java-storage diff --git a/gcloud-java/pom.xml b/gcloud-java/pom.xml index 654b34f92056..2e6249a20b1a 100644 --- a/gcloud-java/pom.xml +++ b/gcloud-java/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.6-SNAPSHOT + 0.1.6 diff --git a/pom.xml b/pom.xml index d73956f506ee..02061658a8be 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.google.gcloud gcloud-java-pom pom - 0.1.6-SNAPSHOT + 0.1.6 GCloud Java https://github.com/GoogleCloudPlatform/gcloud-java From 700491864f4fb5c1edb975677741b9592523559b Mon Sep 17 00:00:00 2001 From: travis-ci Date: Tue, 29 Mar 2016 01:10:21 +0000 Subject: [PATCH 33/60] Updating version in README files. [ci skip] --- README.md | 6 +++--- gcloud-java-bigquery/README.md | 6 +++--- gcloud-java-contrib/README.md | 6 +++--- gcloud-java-core/README.md | 6 +++--- gcloud-java-datastore/README.md | 6 +++--- gcloud-java-dns/README.md | 6 +++--- gcloud-java-examples/README.md | 6 +++--- gcloud-java-resourcemanager/README.md | 6 +++--- gcloud-java-storage/README.md | 6 +++--- gcloud-java/README.md | 6 +++--- 10 files changed, 30 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 6061d9dd4c8f..d5604dddbbff 100644 --- a/README.md +++ b/README.md @@ -30,16 +30,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java - 0.1.5 + 0.1.6 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java:0.1.5' +compile 'com.google.gcloud:gcloud-java:0.1.6' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java" % "0.1.5" +libraryDependencies += "com.google.gcloud" % "gcloud-java" % "0.1.6" ``` Example Applications diff --git a/gcloud-java-bigquery/README.md b/gcloud-java-bigquery/README.md index 81b5db71bcac..f81cccd5ad44 100644 --- a/gcloud-java-bigquery/README.md +++ b/gcloud-java-bigquery/README.md @@ -22,16 +22,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java-bigquery - 0.1.5 + 0.1.6 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java-bigquery:0.1.5' +compile 'com.google.gcloud:gcloud-java-bigquery:0.1.6' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java-bigquery" % "0.1.5" +libraryDependencies += "com.google.gcloud" % "gcloud-java-bigquery" % "0.1.6" ``` Example Application diff --git a/gcloud-java-contrib/README.md b/gcloud-java-contrib/README.md index 426417d54e87..b41467934690 100644 --- a/gcloud-java-contrib/README.md +++ b/gcloud-java-contrib/README.md @@ -16,16 +16,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java-contrib - 0.1.5 + 0.1.6 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java-contrib:0.1.5' +compile 'com.google.gcloud:gcloud-java-contrib:0.1.6' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java-contrib" % "0.1.5" +libraryDependencies += "com.google.gcloud" % "gcloud-java-contrib" % "0.1.6" ``` Java Versions diff --git a/gcloud-java-core/README.md b/gcloud-java-core/README.md index fc5f481f8ec3..1ee96b950471 100644 --- a/gcloud-java-core/README.md +++ b/gcloud-java-core/README.md @@ -19,16 +19,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java-core - 0.1.5 + 0.1.6 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java-core:0.1.5' +compile 'com.google.gcloud:gcloud-java-core:0.1.6' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java-core" % "0.1.5" +libraryDependencies += "com.google.gcloud" % "gcloud-java-core" % "0.1.6" ``` Troubleshooting diff --git a/gcloud-java-datastore/README.md b/gcloud-java-datastore/README.md index 0d89a0a07e3e..1025de79f63d 100644 --- a/gcloud-java-datastore/README.md +++ b/gcloud-java-datastore/README.md @@ -22,16 +22,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java-datastore - 0.1.5 + 0.1.6 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java-datastore:0.1.5' +compile 'com.google.gcloud:gcloud-java-datastore:0.1.6' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java-datastore" % "0.1.5" +libraryDependencies += "com.google.gcloud" % "gcloud-java-datastore" % "0.1.6" ``` Example Application diff --git a/gcloud-java-dns/README.md b/gcloud-java-dns/README.md index d2e4c85b3b76..a01282f16c5d 100644 --- a/gcloud-java-dns/README.md +++ b/gcloud-java-dns/README.md @@ -22,16 +22,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java-dns - 0.1.5 + 0.1.6 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java-dns:0.1.5' +compile 'com.google.gcloud:gcloud-java-dns:0.1.6' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java-dns" % "0.1.5" +libraryDependencies += "com.google.gcloud" % "gcloud-java-dns" % "0.1.6" ``` Example Application diff --git a/gcloud-java-examples/README.md b/gcloud-java-examples/README.md index 8084cee562f0..45658e37323e 100644 --- a/gcloud-java-examples/README.md +++ b/gcloud-java-examples/README.md @@ -19,16 +19,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java-examples - 0.1.5 + 0.1.6 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java-examples:0.1.5' +compile 'com.google.gcloud:gcloud-java-examples:0.1.6' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java-examples" % "0.1.5" +libraryDependencies += "com.google.gcloud" % "gcloud-java-examples" % "0.1.6" ``` To run examples from your command line: diff --git a/gcloud-java-resourcemanager/README.md b/gcloud-java-resourcemanager/README.md index a2539df7adab..9d142fc558ea 100644 --- a/gcloud-java-resourcemanager/README.md +++ b/gcloud-java-resourcemanager/README.md @@ -22,16 +22,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java-resourcemanager - 0.1.5 + 0.1.6 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java-resourcemanager:0.1.5' +compile 'com.google.gcloud:gcloud-java-resourcemanager:0.1.6' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java-resourcemanager" % "0.1.5" +libraryDependencies += "com.google.gcloud" % "gcloud-java-resourcemanager" % "0.1.6" ``` Example Application diff --git a/gcloud-java-storage/README.md b/gcloud-java-storage/README.md index 0ee05b31c10c..962b90c82b34 100644 --- a/gcloud-java-storage/README.md +++ b/gcloud-java-storage/README.md @@ -22,16 +22,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java-storage - 0.1.5 + 0.1.6 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java-storage:0.1.5' +compile 'com.google.gcloud:gcloud-java-storage:0.1.6' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java-storage" % "0.1.5" +libraryDependencies += "com.google.gcloud" % "gcloud-java-storage" % "0.1.6" ``` Example Application diff --git a/gcloud-java/README.md b/gcloud-java/README.md index e296d0c0c565..c898da5d5f72 100644 --- a/gcloud-java/README.md +++ b/gcloud-java/README.md @@ -27,16 +27,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java - 0.1.5 + 0.1.6 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java:0.1.5' +compile 'com.google.gcloud:gcloud-java:0.1.6' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java" % "0.1.5" +libraryDependencies += "com.google.gcloud" % "gcloud-java" % "0.1.6" ``` Troubleshooting From d2dc5e73dbc5484d0014074c27265e294e3b8bbf Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 28 Mar 2016 18:33:20 -0700 Subject: [PATCH 34/60] Update version to 0.1.7-SNAPSHOT --- gcloud-java-bigquery/pom.xml | 2 +- gcloud-java-contrib/pom.xml | 2 +- gcloud-java-core/pom.xml | 2 +- gcloud-java-datastore/pom.xml | 2 +- gcloud-java-dns/pom.xml | 2 +- gcloud-java-examples/pom.xml | 2 +- gcloud-java-resourcemanager/pom.xml | 2 +- gcloud-java-storage/pom.xml | 2 +- gcloud-java/pom.xml | 2 +- pom.xml | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/gcloud-java-bigquery/pom.xml b/gcloud-java-bigquery/pom.xml index cd7883bccaab..952e75899de7 100644 --- a/gcloud-java-bigquery/pom.xml +++ b/gcloud-java-bigquery/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.6 + 0.1.7-SNAPSHOT gcloud-java-bigquery diff --git a/gcloud-java-contrib/pom.xml b/gcloud-java-contrib/pom.xml index 33cdb552411c..c1a0eaad9008 100644 --- a/gcloud-java-contrib/pom.xml +++ b/gcloud-java-contrib/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.6 + 0.1.7-SNAPSHOT gcloud-java-contrib diff --git a/gcloud-java-core/pom.xml b/gcloud-java-core/pom.xml index 51d6baf213a9..13e170e061ee 100644 --- a/gcloud-java-core/pom.xml +++ b/gcloud-java-core/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.6 + 0.1.7-SNAPSHOT gcloud-java-core diff --git a/gcloud-java-datastore/pom.xml b/gcloud-java-datastore/pom.xml index e97a2b943582..7110d8de3947 100644 --- a/gcloud-java-datastore/pom.xml +++ b/gcloud-java-datastore/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.6 + 0.1.7-SNAPSHOT gcloud-java-datastore diff --git a/gcloud-java-dns/pom.xml b/gcloud-java-dns/pom.xml index 68c46c909f2a..66dc1e54752e 100644 --- a/gcloud-java-dns/pom.xml +++ b/gcloud-java-dns/pom.xml @@ -13,7 +13,7 @@ com.google.gcloud gcloud-java-pom - 0.1.6 + 0.1.7-SNAPSHOT gcloud-java-dns diff --git a/gcloud-java-examples/pom.xml b/gcloud-java-examples/pom.xml index 7b23f7ebe48d..15bb536916f4 100644 --- a/gcloud-java-examples/pom.xml +++ b/gcloud-java-examples/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.6 + 0.1.7-SNAPSHOT gcloud-java-examples diff --git a/gcloud-java-resourcemanager/pom.xml b/gcloud-java-resourcemanager/pom.xml index d428d4a937fc..f8a2697c23b5 100644 --- a/gcloud-java-resourcemanager/pom.xml +++ b/gcloud-java-resourcemanager/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.6 + 0.1.7-SNAPSHOT gcloud-java-resourcemanager diff --git a/gcloud-java-storage/pom.xml b/gcloud-java-storage/pom.xml index 59b4f9a55245..37f4d00991a3 100644 --- a/gcloud-java-storage/pom.xml +++ b/gcloud-java-storage/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.6 + 0.1.7-SNAPSHOT gcloud-java-storage diff --git a/gcloud-java/pom.xml b/gcloud-java/pom.xml index 2e6249a20b1a..2a1720fcd300 100644 --- a/gcloud-java/pom.xml +++ b/gcloud-java/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.6 + 0.1.7-SNAPSHOT diff --git a/pom.xml b/pom.xml index 02061658a8be..7b1c7d31a069 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.google.gcloud gcloud-java-pom pom - 0.1.6 + 0.1.7-SNAPSHOT GCloud Java https://github.com/GoogleCloudPlatform/gcloud-java From 4ee0e3201df3da62f8d7a4a01a8453ccaacebddd Mon Sep 17 00:00:00 2001 From: Arie Ozarov Date: Mon, 28 Mar 2016 19:28:25 -0700 Subject: [PATCH 35/60] update pom.xml to include DNS in javadoc groups --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 7b1c7d31a069..4c53e54560c2 100644 --- a/pom.xml +++ b/pom.xml @@ -404,7 +404,7 @@ Test helpers packages - com.google.gcloud.bigquery.testing:com.google.gcloud.datastore.testing:com.google.gcloud.resourcemanager.testing:com.google.gcloud.storage.testing + com.google.gcloud.bigquery.testing:com.google.gcloud.datastore.testing:com.google.gcloud.dns.testing:com.google.gcloud.resourcemanager.testing:com.google.gcloud.storage.testing Example packages @@ -412,7 +412,7 @@ SPI packages - com.google.gcloud.spi:com.google.gcloud.bigquery.spi:com.google.gcloud.datastore.spi:com.google.gcloud.resourcemanager.spi:com.google.gcloud.storage.spi + com.google.gcloud.spi:com.google.gcloud.bigquery.spi:com.google.gcloud.datastore.spi:com.google.gcloud.dns.spi:com.google.gcloud.resourcemanager.spi:com.google.gcloud.storage.spi From 7849e6ea7dcbb5c4d38fe7c6278e67ed308a6177 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Tue, 29 Mar 2016 08:10:33 -0700 Subject: [PATCH 36/60] Add pkg info for dns --- .../gcloud/dns/testing/package-info.java | 36 +++++++++++++++++++ .../resourcemanager/testing/package-info.java | 5 +-- 2 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/package-info.java diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/package-info.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/package-info.java new file mode 100644 index 000000000000..a0a0c593c2b7 --- /dev/null +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/package-info.java @@ -0,0 +1,36 @@ +/* + * 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. + */ + +/** + * A testing helper for Google Cloud DNS. + * + *

    A simple usage example: + * Before the test: + *

     {@code
    + * // Minimum delay before processing change requests (in ms). Setting the delay to 0 makes change
    + * // request processing synchronous.
    + * long delay = 0;
    + * LocalDnsHelper dnsHelper = LocalDnsHelper.create(delay);
    + * Dns dns = dnsHelper.options().service();
    + * dnsHelper.start();
    + * }
    + * + *

    After the test: + *

     {@code
    + * dnsHelper.stop();
    + * }
    + */ +package com.google.gcloud.dns.testing; diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/testing/package-info.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/testing/package-info.java index 7e5519f7d085..b0165c1ddd9d 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/testing/package-info.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/testing/package-info.java @@ -22,11 +22,12 @@ *
     {@code
      * LocalResourceManagerHelper resourceManagerHelper = LocalResourceManagerHelper.create();
      * ResourceManager resourceManager = resourceManagerHelper.options().service();
    - * } 
    + * resourceManagerHelper.start(); + * } * *

    After the test: *

     {@code
      * resourceManagerHelper.stop();
    - * } 
    + * } */ package com.google.gcloud.resourcemanager.testing; From 712b255f40bfd67128edd4f2b287c7a7193156fa Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Tue, 29 Mar 2016 17:57:35 +0200 Subject: [PATCH 37/60] Add common interface for field selectors --- .../com/google/gcloud/bigquery/BigQuery.java | 66 +++++-------- .../com/google/gcloud/bigquery/Option.java | 24 ++++- .../java/com/google/gcloud/FieldSelector.java | 30 ++++++ .../com/google/gcloud/dns/AbstractOption.java | 21 ++++ .../main/java/com/google/gcloud/dns/Dns.java | 97 +++++++------------ .../google/gcloud/resourcemanager/Option.java | 21 ++++ .../resourcemanager/ResourceManager.java | 27 +++--- .../com/google/gcloud/storage/Option.java | 21 ++++ .../com/google/gcloud/storage/Storage.java | 50 ++++------ 9 files changed, 205 insertions(+), 152 deletions(-) create mode 100644 gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java index 14e324a43370..a0724b2026b2 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java @@ -19,16 +19,14 @@ import static com.google.common.base.Preconditions.checkArgument; import com.google.common.base.Function; -import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; -import com.google.common.collect.Sets; +import com.google.gcloud.FieldSelector; import com.google.gcloud.Page; import com.google.gcloud.Service; import com.google.gcloud.bigquery.spi.BigQueryRpc; import java.util.List; -import java.util.Set; /** * An interface for Google Cloud BigQuery. @@ -43,7 +41,7 @@ public interface BigQuery extends Service { * @see Dataset * Resource */ - enum DatasetField { + enum DatasetField implements FieldSelector { ACCESS("access"), CREATION_TIME("creationTime"), DATASET_REFERENCE("datasetReference"), @@ -56,24 +54,19 @@ enum DatasetField { LOCATION("location"), SELF_LINK("selfLink"); + static final List REQUIRED_FIELDS = + ImmutableList.of(DATASET_REFERENCE); + private final String selector; DatasetField(String selector) { this.selector = selector; } + @Override public String selector() { return selector; } - - static String selector(DatasetField... fields) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1); - fieldStrings.add(DATASET_REFERENCE.selector()); - for (DatasetField field : fields) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } } /** @@ -82,7 +75,7 @@ static String selector(DatasetField... fields) { * @see Table * Resource */ - enum TableField { + enum TableField implements FieldSelector { CREATION_TIME("creationTime"), DESCRIPTION("description"), ETAG("etag"), @@ -101,25 +94,19 @@ enum TableField { TYPE("type"), VIEW("view"); + static final List REQUIRED_FIELDS = + ImmutableList.of(TABLE_REFERENCE, TYPE); + private final String selector; TableField(String selector) { this.selector = selector; } + @Override public String selector() { return selector; } - - static String selector(TableField... fields) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 2); - fieldStrings.add(TABLE_REFERENCE.selector()); - fieldStrings.add(TYPE.selector()); - for (TableField field : fields) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } } /** @@ -128,7 +115,7 @@ static String selector(TableField... fields) { * @see Job Resource * */ - enum JobField { + enum JobField implements FieldSelector { CONFIGURATION("configuration"), ETAG("etag"), ID("id"), @@ -138,25 +125,19 @@ enum JobField { STATUS("status"), USER_EMAIL("user_email"); + static final List REQUIRED_FIELDS = + ImmutableList.of(JOB_REFERENCE, CONFIGURATION); + private final String selector; JobField(String selector) { this.selector = selector; } + @Override public String selector() { return selector; } - - static String selector(JobField... fields) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 2); - fieldStrings.add(JOB_REFERENCE.selector()); - fieldStrings.add(CONFIGURATION.selector()); - for (JobField field : fields) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } } /** @@ -210,7 +191,8 @@ private DatasetOption(BigQueryRpc.Option option, Object value) { * returned, even if not specified. */ public static DatasetOption fields(DatasetField... fields) { - return new DatasetOption(BigQueryRpc.Option.FIELDS, DatasetField.selector(fields)); + return new DatasetOption(BigQueryRpc.Option.FIELDS, + selector(DatasetField.REQUIRED_FIELDS, fields)); } } @@ -279,7 +261,8 @@ private TableOption(BigQueryRpc.Option option, Object value) { * of {@link Table#definition()}) are always returned, even if not specified. */ public static TableOption fields(TableField... fields) { - return new TableOption(BigQueryRpc.Option.FIELDS, TableField.selector(fields)); + return new TableOption(BigQueryRpc.Option.FIELDS, + selector(TableField.REQUIRED_FIELDS, fields)); } } @@ -376,9 +359,9 @@ public static JobListOption pageToken(String pageToken) { * listing jobs. */ public static JobListOption fields(JobField... fields) { - String selector = JobField.selector(fields); - StringBuilder builder = new StringBuilder(); - builder.append("etag,jobs(").append(selector).append(",state,errorResult),nextPageToken"); + StringBuilder builder = + selector(new StringBuilder().append("etag,jobs("), JobField.REQUIRED_FIELDS, fields) + .append(",state,errorResult),nextPageToken"); return new JobListOption(BigQueryRpc.Option.FIELDS, builder.toString()); } } @@ -402,7 +385,8 @@ private JobOption(BigQueryRpc.Option option, Object value) { * returned, even if not specified. */ public static JobOption fields(JobField... fields) { - return new JobOption(BigQueryRpc.Option.FIELDS, JobField.selector(fields)); + return new JobOption(BigQueryRpc.Option.FIELDS, + Option.selector(JobField.REQUIRED_FIELDS, fields)); } } diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java index 3fdc27ecab99..9b123fc1c89c 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java @@ -18,11 +18,16 @@ import static com.google.common.base.Preconditions.checkNotNull; +import com.google.common.base.Joiner; import com.google.common.base.MoreObjects; +import com.google.common.collect.Sets; +import com.google.gcloud.FieldSelector; import com.google.gcloud.bigquery.spi.BigQueryRpc; import java.io.Serializable; +import java.util.List; import java.util.Objects; +import java.util.Set; /** * Base class for BigQuery operation option. @@ -53,8 +58,7 @@ public boolean equals(Object obj) { return false; } Option other = (Option) obj; - return Objects.equals(rpcOption, other.rpcOption) - && Objects.equals(value, other.value); + return Objects.equals(rpcOption, other.rpcOption) && Objects.equals(value, other.value); } @Override @@ -69,4 +73,20 @@ public String toString() { .add("value", value) .toString(); } + + static String selector(List required, FieldSelector... others) { + Set fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length); + for (FieldSelector field : required) { + fieldStrings.add(field.selector()); + } + for (FieldSelector field : others) { + fieldStrings.add(field.selector()); + } + return Joiner.on(',').join(fieldStrings); + } + + static StringBuilder selector(StringBuilder partialSelector, List required, + FieldSelector... others) { + return partialSelector.append(selector(required, others)); + } } diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java b/gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java new file mode 100644 index 000000000000..358c4d4798f6 --- /dev/null +++ b/gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java @@ -0,0 +1,30 @@ +/* + * 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; + +/** + * Interface for Google Cloud resource's fields. Implementations of this interface can be used to + * select only desired fields when getting or listing Google Cloud resources. + */ +public interface FieldSelector { + + /** + * Returns a string selector. This selector is passed to a Google Cloud service (possibly with + * other field selectors) to specify which resource fields should be returned by an API call. + */ + String selector(); +} diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/AbstractOption.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/AbstractOption.java index e12f7412e687..43dfe69df213 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/AbstractOption.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/AbstractOption.java @@ -18,11 +18,16 @@ import static com.google.common.base.Preconditions.checkNotNull; +import com.google.common.base.Joiner; import com.google.common.base.MoreObjects; +import com.google.common.collect.Sets; +import com.google.gcloud.FieldSelector; import com.google.gcloud.dns.spi.DnsRpc; import java.io.Serializable; +import java.util.List; import java.util.Objects; +import java.util.Set; /** * A base class for options. @@ -67,4 +72,20 @@ public String toString() { .add("rpcOption", rpcOption) .toString(); } + + static String selector(List required, FieldSelector... others) { + Set fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length); + for (FieldSelector field : required) { + fieldStrings.add(field.selector()); + } + for (FieldSelector field : others) { + fieldStrings.add(field.selector()); + } + return Joiner.on(',').join(fieldStrings); + } + + static StringBuilder selector(StringBuilder partialSelector, List required, + FieldSelector... others) { + return partialSelector.append(selector(required, others)); + } } 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 f2b42f30a9f6..ee0611279d27 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 @@ -16,14 +16,14 @@ package com.google.gcloud.dns; -import com.google.common.base.Joiner; -import com.google.common.collect.Sets; +import com.google.common.collect.ImmutableList; +import com.google.gcloud.FieldSelector; import com.google.gcloud.Page; import com.google.gcloud.Service; import com.google.gcloud.dns.spi.DnsRpc; import java.io.Serializable; -import java.util.Set; +import java.util.List; /** * An interface for the Google Cloud DNS service. @@ -39,29 +39,23 @@ public interface Dns extends Service { * {@link Dns#getProject(ProjectOption...)}. Project ID is always returned, even if not * specified. */ - enum ProjectField { + enum ProjectField implements FieldSelector { PROJECT_ID("id"), PROJECT_NUMBER("number"), QUOTA("quota"); + static final List REQUIRED_FIELDS = ImmutableList.of(PROJECT_ID); + private final String selector; ProjectField(String selector) { this.selector = selector; } - String selector() { + @Override + public String selector() { return selector; } - - static String selector(ProjectField... fields) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1); - fieldStrings.add(PROJECT_ID.selector()); - for (ProjectField field : fields) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } } /** @@ -71,7 +65,7 @@ static String selector(ProjectField... fields) { * {@link Dns#getZone(String, ZoneOption...)}. The name is always returned, even if not * specified. */ - enum ZoneField { + enum ZoneField implements FieldSelector { CREATION_TIME("creationTime"), DESCRIPTION("description"), DNS_NAME("dnsName"), @@ -80,24 +74,18 @@ enum ZoneField { NAME_SERVER_SET("nameServerSet"), NAME_SERVERS("nameServers"); + static final List REQUIRED_FIELDS = ImmutableList.of(NAME); + private final String selector; ZoneField(String selector) { this.selector = selector; } - String selector() { + @Override + public String selector() { return selector; } - - static String selector(ZoneField... fields) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1); - fieldStrings.add(NAME.selector()); - for (ZoneField field : fields) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } } /** @@ -107,31 +95,24 @@ static String selector(ZoneField... fields) { * {@link Dns#listRecordSets(String, RecordSetListOption...)}. The name and type are always * returned even if not selected. */ - enum RecordSetField { + enum RecordSetField implements FieldSelector { DNS_RECORDS("rrdatas"), NAME("name"), TTL("ttl"), TYPE("type"); + static final List REQUIRED_FIELDS = ImmutableList.of(NAME, TYPE); + private final String selector; RecordSetField(String selector) { this.selector = selector; } - String selector() { + @Override + public String selector() { return selector; } - - static String selector(RecordSetField... fields) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1); - fieldStrings.add(NAME.selector()); - fieldStrings.add(TYPE.selector()); - for (RecordSetField field : fields) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } } /** @@ -141,31 +122,25 @@ static String selector(RecordSetField... fields) { * {@link Dns#applyChangeRequest(String, ChangeRequestInfo, ChangeRequestOption...)} The ID is always * returned even if not selected. */ - enum ChangeRequestField { + enum ChangeRequestField implements FieldSelector { ID("id"), START_TIME("startTime"), STATUS("status"), ADDITIONS("additions"), DELETIONS("deletions"); + static final List REQUIRED_FIELDS = ImmutableList.of(ID); + private final String selector; ChangeRequestField(String selector) { this.selector = selector; } - String selector() { + @Override + public String selector() { return selector; } - - static String selector(ChangeRequestField... fields) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1); - fieldStrings.add(ID.selector()); - for (ChangeRequestField field : fields) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } } /** @@ -199,9 +174,8 @@ class RecordSetListOption extends AbstractOption implements Serializable { * of fields that can be used. */ public static RecordSetListOption fields(RecordSetField... fields) { - StringBuilder builder = new StringBuilder(); - builder.append("nextPageToken,rrsets(").append(RecordSetField.selector(fields)) - .append(')'); + StringBuilder builder = selector(new StringBuilder().append("nextPageToken,rrsets("), + RecordSetField.REQUIRED_FIELDS, fields).append(')'); return new RecordSetListOption(DnsRpc.Option.FIELDS, builder.toString()); } @@ -260,7 +234,7 @@ class ZoneOption extends AbstractOption implements Serializable { * specified. {@link ZoneField} provides a list of fields that can be used. */ public static ZoneOption fields(ZoneField... fields) { - return new ZoneOption(DnsRpc.Option.FIELDS, ZoneField.selector(fields)); + return new ZoneOption(DnsRpc.Option.FIELDS, selector(ZoneField.REQUIRED_FIELDS, fields)); } } @@ -283,8 +257,8 @@ class ZoneListOption extends AbstractOption implements Serializable { * specified. {@link ZoneField} provides a list of fields that can be used. */ public static ZoneListOption fields(ZoneField... fields) { - StringBuilder builder = new StringBuilder(); - builder.append("nextPageToken,managedZones(").append(ZoneField.selector(fields)).append(')'); + StringBuilder builder = selector(new StringBuilder().append("nextPageToken,managedZones("), + ZoneField.REQUIRED_FIELDS, fields).append(')'); return new ZoneListOption(DnsRpc.Option.FIELDS, builder.toString()); } @@ -302,7 +276,6 @@ public static ZoneListOption pageToken(String pageToken) { * Restricts the list to only zone with this fully qualified domain name. */ public static ZoneListOption dnsName(String dnsName) { - StringBuilder builder = new StringBuilder(); return new ZoneListOption(DnsRpc.Option.DNS_NAME, dnsName); } @@ -337,7 +310,8 @@ class ProjectOption extends AbstractOption implements Serializable { * can be used. */ public static ProjectOption fields(ProjectField... fields) { - return new ProjectOption(DnsRpc.Option.FIELDS, ProjectField.selector(fields)); + return new ProjectOption(DnsRpc.Option.FIELDS, + selector(ProjectField.REQUIRED_FIELDS, fields)); } } @@ -362,10 +336,8 @@ class ChangeRequestOption extends AbstractOption implements Serializable { * a list of fields that can be used. */ public static ChangeRequestOption fields(ChangeRequestField... fields) { - return new ChangeRequestOption( - DnsRpc.Option.FIELDS, - ChangeRequestField.selector(fields) - ); + return new ChangeRequestOption(DnsRpc.Option.FIELDS, + selector(ChangeRequestField.REQUIRED_FIELDS, fields)); } } @@ -390,9 +362,8 @@ class ChangeRequestListOption extends AbstractOption implements Serializable { * a list of fields that can be used. */ public static ChangeRequestListOption fields(ChangeRequestField... fields) { - StringBuilder builder = new StringBuilder(); - builder.append("nextPageToken,changes(").append(ChangeRequestField.selector(fields)) - .append(')'); + StringBuilder builder = selector(new StringBuilder().append("nextPageToken,changes("), + ChangeRequestField.REQUIRED_FIELDS, fields).append(')'); return new ChangeRequestListOption(DnsRpc.Option.FIELDS, builder.toString()); } diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Option.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Option.java index 72d62d7fc224..3af167db71a5 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Option.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Option.java @@ -18,11 +18,16 @@ import static com.google.common.base.Preconditions.checkNotNull; +import com.google.common.base.Joiner; import com.google.common.base.MoreObjects; +import com.google.common.collect.Sets; +import com.google.gcloud.FieldSelector; import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc; import java.io.Serializable; +import java.util.List; import java.util.Objects; +import java.util.Set; /** * Base class for Resource Manager operation options. @@ -69,4 +74,20 @@ public String toString() { .add("value", value) .toString(); } + + static String selector(List required, FieldSelector... others) { + Set fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length); + for (FieldSelector field : required) { + fieldStrings.add(field.selector()); + } + for (FieldSelector field : others) { + fieldStrings.add(field.selector()); + } + return Joiner.on(',').join(fieldStrings); + } + + static StringBuilder selector(StringBuilder partialSelector, List required, + FieldSelector... others) { + return partialSelector.append(selector(required, others)); + } } diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java index 70eeb9c8eb50..595bfab556d6 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java @@ -16,15 +16,14 @@ package com.google.gcloud.resourcemanager; -import com.google.common.base.Joiner; -import com.google.common.collect.Sets; +import com.google.common.collect.ImmutableList; +import com.google.gcloud.FieldSelector; import com.google.gcloud.IamPolicy; import com.google.gcloud.Page; import com.google.gcloud.Service; import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc; import java.util.List; -import java.util.Set; /** * An interface for Google Cloud Resource Manager. @@ -42,7 +41,7 @@ public interface ResourceManager extends Service { * {@link ResourceManager#get} or {@link ResourceManager#list}. Project ID is always returned, * even if not specified. */ - enum ProjectField { + enum ProjectField implements FieldSelector { PROJECT_ID("projectId"), NAME("name"), LABELS("labels"), @@ -50,24 +49,18 @@ enum ProjectField { STATE("lifecycleState"), CREATE_TIME("createTime"); + static final List REQUIRED_FIELDS = ImmutableList.of(PROJECT_ID); + private final String selector; ProjectField(String selector) { this.selector = selector; } + @Override public String selector() { return selector; } - - static String selector(ProjectField... fields) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1); - fieldStrings.add(PROJECT_ID.selector()); - for (ProjectField field : fields) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } } /** @@ -90,7 +83,8 @@ private ProjectGetOption(ResourceManagerRpc.Option option, Object value) { * that can be used. */ public static ProjectGetOption fields(ProjectField... fields) { - return new ProjectGetOption(ResourceManagerRpc.Option.FIELDS, ProjectField.selector(fields)); + return new ProjectGetOption(ResourceManagerRpc.Option.FIELDS, + selector(ProjectField.REQUIRED_FIELDS, fields)); } } @@ -163,8 +157,9 @@ public static ProjectListOption pageSize(int pageSize) { * that can be used. */ public static ProjectListOption fields(ProjectField... fields) { - StringBuilder builder = new StringBuilder(); - builder.append("projects(").append(ProjectField.selector(fields)).append("),nextPageToken"); + StringBuilder builder = + selector(new StringBuilder().append("projects("), ProjectField.REQUIRED_FIELDS, fields) + .append("),nextPageToken"); return new ProjectListOption(ResourceManagerRpc.Option.FIELDS, builder.toString()); } } diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Option.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Option.java index 65c55da7efc8..1ea0c0561eaa 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Option.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Option.java @@ -18,11 +18,16 @@ import static com.google.common.base.Preconditions.checkNotNull; +import com.google.common.base.Joiner; import com.google.common.base.MoreObjects; +import com.google.common.collect.Sets; +import com.google.gcloud.FieldSelector; import com.google.gcloud.storage.spi.StorageRpc; import java.io.Serializable; +import java.util.List; import java.util.Objects; +import java.util.Set; /** * Base class for Storage operation option. @@ -69,4 +74,20 @@ public String toString() { .add("value", value) .toString(); } + + static String selector(List required, FieldSelector... others) { + Set fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length); + for (FieldSelector field : required) { + fieldStrings.add(field.selector()); + } + for (FieldSelector field : others) { + fieldStrings.add(field.selector()); + } + return Joiner.on(',').join(fieldStrings); + } + + static StringBuilder selector(StringBuilder partialSelector, List required, + FieldSelector... others) { + return partialSelector.append(selector(required, others)); + } } diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java index 78f421e94e52..eb51e9d0f6c1 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java @@ -19,13 +19,12 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; -import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; -import com.google.common.collect.Sets; import com.google.gcloud.AuthCredentials; import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials; +import com.google.gcloud.FieldSelector; import com.google.gcloud.Page; import com.google.gcloud.ReadChannel; import com.google.gcloud.Service; @@ -38,7 +37,6 @@ import java.net.URL; import java.util.Arrays; import java.util.Collections; -import java.util.HashSet; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; @@ -74,7 +72,7 @@ String entry() { } } - enum BucketField { + enum BucketField implements FieldSelector { ID("id"), SELF_LINK("selfLink"), NAME("name"), @@ -90,27 +88,21 @@ enum BucketField { STORAGE_CLASS("storageClass"), ETAG("etag"); + static final List REQUIRED_FIELDS = ImmutableList.of(NAME); + private final String selector; BucketField(String selector) { this.selector = selector; } + @Override public String selector() { return selector; } - - static String selector(BucketField... fields) { - HashSet fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1); - fieldStrings.add(NAME.selector()); - for (BucketField field : fields) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } } - enum BlobField { + enum BlobField implements FieldSelector { ACL("acl"), BUCKET("bucket"), CACHE_CONTROL("cacheControl"), @@ -136,25 +128,19 @@ enum BlobField { TIME_DELETED("timeDeleted"), UPDATED("updated"); + static final List REQUIRED_FIELDS = + ImmutableList.of(BUCKET, NAME); + private final String selector; BlobField(String selector) { this.selector = selector; } + @Override public String selector() { return selector; } - - static String selector(BlobField... fields) { - HashSet fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 2); - fieldStrings.add(BUCKET.selector()); - fieldStrings.add(NAME.selector()); - for (BlobField field : fields) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } } /** @@ -269,7 +255,8 @@ public static BucketGetOption metagenerationNotMatch(long metageneration) { * specified. */ public static BucketGetOption fields(BucketField... fields) { - return new BucketGetOption(StorageRpc.Option.FIELDS, BucketField.selector(fields)); + return new BucketGetOption(StorageRpc.Option.FIELDS, + selector(BucketField.REQUIRED_FIELDS, fields)); } } @@ -609,7 +596,8 @@ public static BlobGetOption metagenerationNotMatch(long metageneration) { * specified. */ public static BlobGetOption fields(BlobField... fields) { - return new BlobGetOption(StorageRpc.Option.FIELDS, BlobField.selector(fields)); + return new BlobGetOption(StorageRpc.Option.FIELDS, + selector(BlobField.REQUIRED_FIELDS, fields)); } } @@ -653,8 +641,9 @@ public static BucketListOption prefix(String prefix) { * specified. */ public static BucketListOption fields(BucketField... fields) { - StringBuilder builder = new StringBuilder(); - builder.append("items(").append(BucketField.selector(fields)).append("),nextPageToken"); + StringBuilder builder = + selector(new StringBuilder().append("items("), BucketField.REQUIRED_FIELDS, fields) + .append("),nextPageToken"); return new BucketListOption(StorageRpc.Option.FIELDS, builder.toString()); } } @@ -722,8 +711,9 @@ public static BlobListOption versions(boolean versions) { * specified. */ public static BlobListOption fields(BlobField... fields) { - StringBuilder builder = new StringBuilder(); - builder.append("items(").append(BlobField.selector(fields)).append("),nextPageToken"); + StringBuilder builder = + selector(new StringBuilder().append("items("), BlobField.REQUIRED_FIELDS, fields) + .append("),nextPageToken"); return new BlobListOption(StorageRpc.Option.FIELDS, builder.toString()); } } From 69476eb1a3f0e952df5a750f691f36cd600c2d60 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Tue, 29 Mar 2016 08:19:12 -0700 Subject: [PATCH 38/60] Add comment about releasing new modules --- RELEASING.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/RELEASING.md b/RELEASING.md index 5e2d6202062e..81a7bcd06129 100644 --- a/RELEASING.md +++ b/RELEASING.md @@ -7,8 +7,8 @@ Most of the release process is handled by the `after_success.sh` script, trigger 1. Run `utilities/update_pom_version.sh` from the repository's base directory. This script takes an optional argument denoting the new version. By default, if the current version is X.Y.Z-SNAPSHOT, the script will update the version in all the pom.xml files to X.Y.Z. If desired, another version can be supplied via command line argument instead. -2. Create a PR to update the pom.xml version. -The PR should look something like [#225](https://github.com/GoogleCloudPlatform/gcloud-java/pull/225). After this PR is merged into GoogleCloudPlatform/gcloud-java, Travis CI will push a new website to GoogleCloudPlatform/gh-pages, push a new artifact to the Maven Central Repository, and update versions in the README files. +2. Create a PR to update the pom.xml version. If releasing a new client library, this PR should also update javadoc grouping in the base directory's [pom.xml](./pom.xml). +PRs that don't release new modules should look something like [#225](https://github.com/GoogleCloudPlatform/gcloud-java/pull/225). PRs that do release a new module should also add the appropriate packages to the javadoc groups "SPI" and "Test helpers", as shown in [#802](https://github.com/GoogleCloudPlatform/gcloud-java/pull/802) for `gcloud-java-dns`. After this PR is merged into GoogleCloudPlatform/gcloud-java, Travis CI will push a new website to GoogleCloudPlatform/gh-pages, push a new artifact to the Maven Central Repository, and update versions in the README files. 3. Before moving on, verify that the artifacts have successfully been pushed to the Maven Central Repository. Open Travis CI, click the ["Build History" tab](https://travis-ci.org/GoogleCloudPlatform/gcloud-java/builds), and open the second build's logs for Step 2's PR. Be sure that you are not opening the "Pull Request" build logs. When the build finishes, scroll to the end of the log and verify that the artifacts were successfully staged and deployed. You can also search for `gcloud-java` on the [Sonatype website](https://oss.sonatype.org/#nexus-search;quick~gcloud-java) and check the latest version number. If the deployment didn't succeed because of a flaky test, rerun the build. From 4d7cb29c0b90a3684b291c1385e79a6024aff2a8 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Tue, 29 Mar 2016 19:11:04 -0700 Subject: [PATCH 39/60] Check if record added --- .../src/test/java/com/google/gcloud/dns/it/ITDnsTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java index dd8e21043181..38273cff49d6 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java @@ -593,9 +593,11 @@ public void testInvalidChangeRequest() { RecordSet.builder("subdomain." + zone.dnsName(), RecordSet.Type.A) .records(ImmutableList.of("0.255.1.5")) .build(); + boolean recordAdded = false; try { ChangeRequestInfo validChange = ChangeRequest.builder().add(validA).build(); zone.applyChangeRequest(validChange); + recordAdded = true; try { zone.applyChangeRequest(validChange); fail("Created a record set which already exists."); @@ -648,6 +650,7 @@ public void testInvalidChangeRequest() { assertEquals(400, ex.code()); } } finally { + assertTrue(recordAdded); ChangeRequestInfo deletion = ChangeRequest.builder().delete(validA).build(); ChangeRequest request = zone.applyChangeRequest(deletion); waitForChangeToComplete(zone.name(), request.id()); From 1710a4cc3907a53cec4d1335f8a6b56b3a92b392 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Wed, 30 Mar 2016 14:57:00 +0200 Subject: [PATCH 40/60] Refactor field selection and helpers - Add FieldSelector.SelectorHelper with static methods to create selectors - Add SelectorHelperTest - Rename dns.AbstractOption to Option, make other Option classes abstract - Add OptionTest class to resource manager and refactor other OptionTest classes --- .../com/google/gcloud/bigquery/BigQuery.java | 14 ++-- .../com/google/gcloud/bigquery/Option.java | 23 +----- .../gcloud/bigquery/BigQueryImplTest.java | 8 ++- .../google/gcloud/bigquery/OptionTest.java | 41 +++++++++-- .../java/com/google/gcloud/FieldSelector.java | 68 +++++++++++++++++- .../com/google/gcloud/SelectorHelperTest.java | 71 +++++++++++++++++++ .../main/java/com/google/gcloud/dns/Dns.java | 35 +++++---- .../java/com/google/gcloud/dns/DnsImpl.java | 4 +- .../dns/{AbstractOption.java => Option.java} | 29 ++------ ...bstractOptionTest.java => OptionTest.java} | 31 ++++---- .../google/gcloud/resourcemanager/Option.java | 23 +----- .../resourcemanager/ResourceManager.java | 9 ++- .../gcloud/resourcemanager/OptionTest.java | 66 +++++++++++++++++ .../com/google/gcloud/storage/Option.java | 23 +----- .../com/google/gcloud/storage/Storage.java | 17 ++--- .../com/google/gcloud/storage/OptionTest.java | 41 +++++++++-- 16 files changed, 337 insertions(+), 166 deletions(-) create mode 100644 gcloud-java-core/src/test/java/com/google/gcloud/SelectorHelperTest.java rename gcloud-java-dns/src/main/java/com/google/gcloud/dns/{AbstractOption.java => Option.java} (62%) rename gcloud-java-dns/src/test/java/com/google/gcloud/dns/{AbstractOptionTest.java => OptionTest.java} (66%) create mode 100644 gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/OptionTest.java diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java index a0724b2026b2..c23ee2844762 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java @@ -22,6 +22,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import com.google.gcloud.FieldSelector; +import com.google.gcloud.FieldSelector.SelectorHelper; import com.google.gcloud.Page; import com.google.gcloud.Service; import com.google.gcloud.bigquery.spi.BigQueryRpc; @@ -192,7 +193,7 @@ private DatasetOption(BigQueryRpc.Option option, Object value) { */ public static DatasetOption fields(DatasetField... fields) { return new DatasetOption(BigQueryRpc.Option.FIELDS, - selector(DatasetField.REQUIRED_FIELDS, fields)); + SelectorHelper.selector(DatasetField.REQUIRED_FIELDS, fields)); } } @@ -262,7 +263,7 @@ private TableOption(BigQueryRpc.Option option, Object value) { */ public static TableOption fields(TableField... fields) { return new TableOption(BigQueryRpc.Option.FIELDS, - selector(TableField.REQUIRED_FIELDS, fields)); + SelectorHelper.selector(TableField.REQUIRED_FIELDS, fields)); } } @@ -359,10 +360,9 @@ public static JobListOption pageToken(String pageToken) { * listing jobs. */ public static JobListOption fields(JobField... fields) { - StringBuilder builder = - selector(new StringBuilder().append("etag,jobs("), JobField.REQUIRED_FIELDS, fields) - .append(",state,errorResult),nextPageToken"); - return new JobListOption(BigQueryRpc.Option.FIELDS, builder.toString()); + return new JobListOption(BigQueryRpc.Option.FIELDS, + SelectorHelper.selector("jobs", JobField.REQUIRED_FIELDS, fields, "state", + "errorResult")); } } @@ -386,7 +386,7 @@ private JobOption(BigQueryRpc.Option option, Object value) { */ public static JobOption fields(JobField... fields) { return new JobOption(BigQueryRpc.Option.FIELDS, - Option.selector(JobField.REQUIRED_FIELDS, fields)); + SelectorHelper.selector(JobField.REQUIRED_FIELDS, fields)); } } diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java index 9b123fc1c89c..e7ac0d0a8cc4 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java @@ -18,21 +18,16 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.common.base.Joiner; import com.google.common.base.MoreObjects; -import com.google.common.collect.Sets; -import com.google.gcloud.FieldSelector; import com.google.gcloud.bigquery.spi.BigQueryRpc; import java.io.Serializable; -import java.util.List; import java.util.Objects; -import java.util.Set; /** * Base class for BigQuery operation option. */ -class Option implements Serializable { +abstract class Option implements Serializable { private static final long serialVersionUID = -6647817677804099207L; @@ -73,20 +68,4 @@ public String toString() { .add("value", value) .toString(); } - - static String selector(List required, FieldSelector... others) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length); - for (FieldSelector field : required) { - fieldStrings.add(field.selector()); - } - for (FieldSelector field : others) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } - - static StringBuilder selector(StringBuilder partialSelector, List required, - FieldSelector... others) { - return partialSelector.append(selector(required, others)); - } } diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java index a6f512800024..c7d7cf846ef2 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java @@ -886,12 +886,14 @@ public com.google.api.services.bigquery.model.Job apply(Job job) { assertEquals(cursor, page.nextPageCursor()); assertArrayEquals(jobList.toArray(), Iterables.toArray(page.values(), Job.class)); String selector = (String) capturedOptions.getValue().get(JOB_OPTION_FIELDS.rpcOption()); - assertTrue(selector.contains("etag,jobs(")); + assertTrue(selector.contains("nextPageToken,jobs(")); assertTrue(selector.contains("configuration")); assertTrue(selector.contains("jobReference")); assertTrue(selector.contains("statistics")); - assertTrue(selector.contains("state,errorResult),nextPageToken")); - assertEquals(80, selector.length()); + assertTrue(selector.contains("state")); + assertTrue(selector.contains("errorResult")); + assertTrue(selector.contains(")")); + assertEquals(75, selector.length()); } @Test diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/OptionTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/OptionTest.java index 2c89ececedb8..42f19830fb6c 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/OptionTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/OptionTest.java @@ -17,22 +17,49 @@ package com.google.gcloud.bigquery; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNull; import com.google.gcloud.bigquery.spi.BigQueryRpc; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; public class OptionTest { + private static final BigQueryRpc.Option RPC_OPTION = BigQueryRpc.Option.PAGE_TOKEN; + private static final BigQueryRpc.Option ANOTHER_RPC_OPTION = BigQueryRpc.Option.FIELDS; + private static final String VALUE = "some value"; + private static final String OTHER_VALUE = "another value"; + private static final Option OPTION = new Option(RPC_OPTION, VALUE) {}; + private static final Option OPTION_EQUALS = new Option(RPC_OPTION, VALUE) {}; + private static final Option OPTION_NOT_EQUALS1 = new Option(RPC_OPTION, OTHER_VALUE) {}; + private static final Option OPTION_NOT_EQUALS2 = new Option(ANOTHER_RPC_OPTION, VALUE) {}; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testEquals() { + assertEquals(OPTION, OPTION_EQUALS); + assertNotEquals(OPTION, OPTION_NOT_EQUALS1); + assertNotEquals(OPTION, OPTION_NOT_EQUALS2); + } + @Test - public void testOption() { - Option option = new Option(BigQueryRpc.Option.PAGE_TOKEN, "token"); - assertEquals(BigQueryRpc.Option.PAGE_TOKEN, option.rpcOption()); - assertEquals("token", option.value()); + public void testHashCode() { + assertEquals(OPTION.hashCode(), OPTION_EQUALS.hashCode()); } - @Test(expected = NullPointerException.class) - public void testNullRpcOption() { - new Option(null, "token"); + @Test + public void testConstructor() { + assertEquals(RPC_OPTION, OPTION.rpcOption()); + assertEquals(VALUE, OPTION.value()); + Option option = new Option(RPC_OPTION, null) {}; + assertEquals(RPC_OPTION, option.rpcOption()); + assertNull(option.value()); + thrown.expect(NullPointerException.class); + new Option(null, VALUE) {}; } } diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java b/gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java index 358c4d4798f6..343f67e3381b 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java @@ -16,9 +16,18 @@ package com.google.gcloud; +import com.google.common.base.Function; +import com.google.common.base.Joiner; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; + +import java.util.Arrays; +import java.util.List; +import java.util.Set; + /** * Interface for Google Cloud resource's fields. Implementations of this interface can be used to - * select only desired fields when getting or listing Google Cloud resources. + * select only desired fields from a returned Google Cloud resource. */ public interface FieldSelector { @@ -27,4 +36,61 @@ public interface FieldSelector { * other field selectors) to specify which resource fields should be returned by an API call. */ String selector(); + + /** + * A helper class used to build composite selectors given a number of fields. This class is not + * supposed to be used directly by users. + */ + class SelectorHelper { + + private SelectorHelper() {} + + private static final Function FIELD_TO_STRING_FUNCTION = + new Function() { + @Override + public String apply(FieldSelector fieldSelector) { + return fieldSelector.selector(); + } + }; + + private static String selector(List required, FieldSelector[] others, + String... extraResourceFields) { + Set fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length); + fieldStrings.addAll(Lists.transform(required, FIELD_TO_STRING_FUNCTION)); + fieldStrings.addAll(Lists.transform(Arrays.asList(others), FIELD_TO_STRING_FUNCTION)); + fieldStrings.addAll(Arrays.asList(extraResourceFields)); + return Joiner.on(',').join(fieldStrings); + } + + /** + * Returns a composite selector given a number of fields. The string selector returned by this + * method can be used for field selection in API calls that return a single resource. This + * method is not supposed to be used directly by users. + */ + public static String selector(List required, FieldSelector... others) { + return selector(required, others, new String[]{}); + } + + /** + * Returns a composite selector given a number of fields and a container name. The string + * selector returned by this method can be used for field selection in API calls that return a + * list of resources. This method is not supposed to be used directly by users. + */ + public static String selector(String containerName, List required, + FieldSelector... others) { + return "nextPageToken," + containerName + '(' + selector(required, others) + ')'; + } + + /** + * Returns a composite selector given a number of fields and a container name. This methods also + * takes an {@code extraResourceFields} parameter to specify some extra fields as strings. The + * string selector returned by this method can be used for field selection in API calls that + * return a list of resources. This method is not supposed to be used directly by users. + */ + public static String selector(String containerName, List required, + FieldSelector[] others, String... extraResourceFields) { + return "nextPageToken," + containerName + '(' + + selector(required, others, extraResourceFields) + ')'; + } + } } diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/SelectorHelperTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/SelectorHelperTest.java new file mode 100644 index 000000000000..2279205539cc --- /dev/null +++ b/gcloud-java-core/src/test/java/com/google/gcloud/SelectorHelperTest.java @@ -0,0 +1,71 @@ +/* + * 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; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import com.google.common.collect.ImmutableList; +import com.google.gcloud.FieldSelector.SelectorHelper; + +import org.junit.Test; + +import java.util.List; + +public class SelectorHelperTest { + + private static final FieldSelector FIELD1 = new FieldSelector() { + @Override + public String selector() { + return "field1"; + } + }; + private static final FieldSelector FIELD2 = new FieldSelector() { + @Override + public String selector() { + return "field2"; + } + }; + private static final FieldSelector FIELD3 = new FieldSelector() { + @Override + public String selector() { + return "field3"; + } + }; + private static final List REQUIRED_FIELDS = ImmutableList.of(FIELD1, FIELD2); + private static final String CONTAINER = "container"; + + @Test + public void testSelector() { + String selector = SelectorHelper.selector(REQUIRED_FIELDS, FIELD3); + assertTrue(selector.contains("field1")); + assertTrue(selector.contains("field2")); + assertTrue(selector.contains("field3")); + assertEquals(20, selector.length()); + } + + @Test + public void testListSelector() { + String selector = SelectorHelper.selector(CONTAINER, REQUIRED_FIELDS, FIELD3); + assertTrue(selector.startsWith("nextPageToken,container(")); + assertTrue(selector.contains("field1")); + assertTrue(selector.contains("field2")); + assertTrue(selector.contains("field3")); + assertTrue(selector.endsWith(")")); + assertEquals(45, selector.length()); + } +} 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 ee0611279d27..c4b08e31d533 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,6 +18,7 @@ import com.google.common.collect.ImmutableList; import com.google.gcloud.FieldSelector; +import com.google.gcloud.FieldSelector.SelectorHelper; import com.google.gcloud.Page; import com.google.gcloud.Service; import com.google.gcloud.dns.spi.DnsRpc; @@ -157,7 +158,7 @@ public String selector() { /** * Class for specifying record set listing options. */ - class RecordSetListOption extends AbstractOption implements Serializable { + class RecordSetListOption extends Option implements Serializable { private static final long serialVersionUID = 1009627025381096098L; @@ -174,9 +175,8 @@ class RecordSetListOption extends AbstractOption implements Serializable { * of fields that can be used. */ public static RecordSetListOption fields(RecordSetField... fields) { - StringBuilder builder = selector(new StringBuilder().append("nextPageToken,rrsets("), - RecordSetField.REQUIRED_FIELDS, fields).append(')'); - return new RecordSetListOption(DnsRpc.Option.FIELDS, builder.toString()); + return new RecordSetListOption(DnsRpc.Option.FIELDS, + SelectorHelper.selector("rrsets", RecordSetField.REQUIRED_FIELDS, fields)); } /** @@ -218,7 +218,7 @@ public static RecordSetListOption type(RecordSet.Type type) { /** * Class for specifying zone field options. */ - class ZoneOption extends AbstractOption implements Serializable { + class ZoneOption extends Option implements Serializable { private static final long serialVersionUID = -8065564464895945037L; @@ -234,14 +234,15 @@ class ZoneOption extends AbstractOption implements Serializable { * specified. {@link ZoneField} provides a list of fields that can be used. */ public static ZoneOption fields(ZoneField... fields) { - return new ZoneOption(DnsRpc.Option.FIELDS, selector(ZoneField.REQUIRED_FIELDS, fields)); + return new ZoneOption(DnsRpc.Option.FIELDS, + SelectorHelper.selector(ZoneField.REQUIRED_FIELDS, fields)); } } /** * Class for specifying zone listing options. */ - class ZoneListOption extends AbstractOption implements Serializable { + class ZoneListOption extends Option implements Serializable { private static final long serialVersionUID = -2830645032124504717L; @@ -257,9 +258,8 @@ class ZoneListOption extends AbstractOption implements Serializable { * specified. {@link ZoneField} provides a list of fields that can be used. */ public static ZoneListOption fields(ZoneField... fields) { - StringBuilder builder = selector(new StringBuilder().append("nextPageToken,managedZones("), - ZoneField.REQUIRED_FIELDS, fields).append(')'); - return new ZoneListOption(DnsRpc.Option.FIELDS, builder.toString()); + return new ZoneListOption(DnsRpc.Option.FIELDS, + SelectorHelper.selector("managedZones", ZoneField.REQUIRED_FIELDS, fields)); } /** @@ -293,7 +293,7 @@ public static ZoneListOption pageSize(int pageSize) { /** * Class for specifying project options. */ - class ProjectOption extends AbstractOption implements Serializable { + class ProjectOption extends Option implements Serializable { private static final long serialVersionUID = 6817937338218847748L; @@ -311,14 +311,14 @@ class ProjectOption extends AbstractOption implements Serializable { */ public static ProjectOption fields(ProjectField... fields) { return new ProjectOption(DnsRpc.Option.FIELDS, - selector(ProjectField.REQUIRED_FIELDS, fields)); + SelectorHelper.selector(ProjectField.REQUIRED_FIELDS, fields)); } } /** * Class for specifying change request field options. */ - class ChangeRequestOption extends AbstractOption implements Serializable { + class ChangeRequestOption extends Option implements Serializable { private static final long serialVersionUID = 1067273695061077782L; @@ -337,14 +337,14 @@ class ChangeRequestOption extends AbstractOption implements Serializable { */ public static ChangeRequestOption fields(ChangeRequestField... fields) { return new ChangeRequestOption(DnsRpc.Option.FIELDS, - selector(ChangeRequestField.REQUIRED_FIELDS, fields)); + SelectorHelper.selector(ChangeRequestField.REQUIRED_FIELDS, fields)); } } /** * Class for specifying change request listing options. */ - class ChangeRequestListOption extends AbstractOption implements Serializable { + class ChangeRequestListOption extends Option implements Serializable { private static final long serialVersionUID = -900209143895376089L; @@ -362,9 +362,8 @@ class ChangeRequestListOption extends AbstractOption implements Serializable { * a list of fields that can be used. */ public static ChangeRequestListOption fields(ChangeRequestField... fields) { - StringBuilder builder = selector(new StringBuilder().append("nextPageToken,changes("), - ChangeRequestField.REQUIRED_FIELDS, fields).append(')'); - return new ChangeRequestListOption(DnsRpc.Option.FIELDS, builder.toString()); + return new ChangeRequestListOption(DnsRpc.Option.FIELDS, + SelectorHelper.selector("changes", ChangeRequestField.REQUIRED_FIELDS, fields)); } /** diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java index 51ab0bd92720..9f4fa2a9d9d1 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java @@ -309,9 +309,9 @@ public com.google.api.services.dns.model.Change call() { } } - private Map optionMap(AbstractOption... options) { + private Map optionMap(Option... options) { Map temp = Maps.newEnumMap(DnsRpc.Option.class); - for (AbstractOption option : options) { + for (Option option : options) { Object prev = temp.put(option.rpcOption(), option.value()); checkArgument(prev == null, "Duplicate option %s", option); } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/AbstractOption.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Option.java similarity index 62% rename from gcloud-java-dns/src/main/java/com/google/gcloud/dns/AbstractOption.java rename to gcloud-java-dns/src/main/java/com/google/gcloud/dns/Option.java index 43dfe69df213..fee99898fb24 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/AbstractOption.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Option.java @@ -18,27 +18,22 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.common.base.Joiner; import com.google.common.base.MoreObjects; -import com.google.common.collect.Sets; -import com.google.gcloud.FieldSelector; import com.google.gcloud.dns.spi.DnsRpc; import java.io.Serializable; -import java.util.List; import java.util.Objects; -import java.util.Set; /** * A base class for options. */ -abstract class AbstractOption implements Serializable { +abstract class Option implements Serializable { private static final long serialVersionUID = -5912727967831484228L; private final Object value; private final DnsRpc.Option rpcOption; - AbstractOption(DnsRpc.Option rpcOption, Object value) { + Option(DnsRpc.Option rpcOption, Object value) { this.rpcOption = checkNotNull(rpcOption); this.value = value; } @@ -53,10 +48,10 @@ DnsRpc.Option rpcOption() { @Override public boolean equals(Object obj) { - if (!(obj instanceof AbstractOption)) { + if (!(obj instanceof Option)) { return false; } - AbstractOption other = (AbstractOption) obj; + Option other = (Option) obj; return Objects.equals(value, other.value) && Objects.equals(rpcOption, other.rpcOption); } @@ -72,20 +67,4 @@ public String toString() { .add("rpcOption", rpcOption) .toString(); } - - static String selector(List required, FieldSelector... others) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length); - for (FieldSelector field : required) { - fieldStrings.add(field.selector()); - } - for (FieldSelector field : others) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } - - static StringBuilder selector(StringBuilder partialSelector, List required, - FieldSelector... others) { - return partialSelector.append(selector(required, others)); - } } 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/OptionTest.java similarity index 66% rename from gcloud-java-dns/src/test/java/com/google/gcloud/dns/AbstractOptionTest.java rename to gcloud-java-dns/src/test/java/com/google/gcloud/dns/OptionTest.java index d88ea85c5846..e9906354f963 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/OptionTest.java @@ -18,24 +18,27 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertNull; import com.google.gcloud.dns.spi.DnsRpc; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; -public class AbstractOptionTest { +public class OptionTest { private static final DnsRpc.Option RPC_OPTION = DnsRpc.Option.DNS_TYPE; private static final DnsRpc.Option ANOTHER_RPC_OPTION = DnsRpc.Option.DNS_NAME; private static final String VALUE = "some value"; private static final String OTHER_VALUE = "another value"; - private static final AbstractOption OPTION = new AbstractOption(RPC_OPTION, VALUE) {}; - private static final AbstractOption OPTION_EQUALS = new AbstractOption(RPC_OPTION, VALUE) {}; - private static final AbstractOption OPTION_NOT_EQUALS1 = - new AbstractOption(RPC_OPTION, OTHER_VALUE) {}; - private static final AbstractOption OPTION_NOT_EQUALS2 = - new AbstractOption(ANOTHER_RPC_OPTION, VALUE) {}; + private static final Option OPTION = new Option(RPC_OPTION, VALUE) {}; + private static final Option OPTION_EQUALS = new Option(RPC_OPTION, VALUE) {}; + private static final Option OPTION_NOT_EQUALS1 = new Option(RPC_OPTION, OTHER_VALUE) {}; + private static final Option OPTION_NOT_EQUALS2 = new Option(ANOTHER_RPC_OPTION, VALUE) {}; + + @Rule + public ExpectedException thrown = ExpectedException.none(); @Test public void testEquals() { @@ -53,12 +56,10 @@ public void testHashCode() { public void testConstructor() { assertEquals(RPC_OPTION, OPTION.rpcOption()); assertEquals(VALUE, OPTION.value()); - try { - new AbstractOption(null, VALUE) {}; - fail("Cannot build with empty option."); - } catch (NullPointerException e) { - // expected - } - new AbstractOption(RPC_OPTION, null) {}; // null value is ok + Option option = new Option(RPC_OPTION, null) {}; + assertEquals(RPC_OPTION, option.rpcOption()); + assertNull(option.value()); + thrown.expect(NullPointerException.class); + new Option(null, VALUE) {}; } } diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Option.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Option.java index 3af167db71a5..3df68468f69f 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Option.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Option.java @@ -18,21 +18,16 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.common.base.Joiner; import com.google.common.base.MoreObjects; -import com.google.common.collect.Sets; -import com.google.gcloud.FieldSelector; import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc; import java.io.Serializable; -import java.util.List; import java.util.Objects; -import java.util.Set; /** * Base class for Resource Manager operation options. */ -class Option implements Serializable { +abstract class Option implements Serializable { private static final long serialVersionUID = 2655177550880762967L; @@ -74,20 +69,4 @@ public String toString() { .add("value", value) .toString(); } - - static String selector(List required, FieldSelector... others) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length); - for (FieldSelector field : required) { - fieldStrings.add(field.selector()); - } - for (FieldSelector field : others) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } - - static StringBuilder selector(StringBuilder partialSelector, List required, - FieldSelector... others) { - return partialSelector.append(selector(required, others)); - } } diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java index 595bfab556d6..e000ca69c359 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java @@ -18,6 +18,7 @@ import com.google.common.collect.ImmutableList; import com.google.gcloud.FieldSelector; +import com.google.gcloud.FieldSelector.SelectorHelper; import com.google.gcloud.IamPolicy; import com.google.gcloud.Page; import com.google.gcloud.Service; @@ -84,7 +85,7 @@ private ProjectGetOption(ResourceManagerRpc.Option option, Object value) { */ public static ProjectGetOption fields(ProjectField... fields) { return new ProjectGetOption(ResourceManagerRpc.Option.FIELDS, - selector(ProjectField.REQUIRED_FIELDS, fields)); + SelectorHelper.selector(ProjectField.REQUIRED_FIELDS, fields)); } } @@ -157,10 +158,8 @@ public static ProjectListOption pageSize(int pageSize) { * that can be used. */ public static ProjectListOption fields(ProjectField... fields) { - StringBuilder builder = - selector(new StringBuilder().append("projects("), ProjectField.REQUIRED_FIELDS, fields) - .append("),nextPageToken"); - return new ProjectListOption(ResourceManagerRpc.Option.FIELDS, builder.toString()); + return new ProjectListOption(ResourceManagerRpc.Option.FIELDS, + SelectorHelper.selector("projects", ProjectField.REQUIRED_FIELDS, fields)); } } diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/OptionTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/OptionTest.java new file mode 100644 index 000000000000..729c7a4b8911 --- /dev/null +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/OptionTest.java @@ -0,0 +1,66 @@ +/* + * 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.resourcemanager; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNull; + +import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class OptionTest { + + private static final ResourceManagerRpc.Option RPC_OPTION = ResourceManagerRpc.Option.FILTER; + private static final ResourceManagerRpc.Option ANOTHER_RPC_OPTION = + ResourceManagerRpc.Option.FIELDS; + private static final String VALUE = "some value"; + private static final String OTHER_VALUE = "another value"; + private static final Option OPTION = new Option(RPC_OPTION, VALUE) {}; + private static final Option OPTION_EQUALS = new Option(RPC_OPTION, VALUE) {}; + private static final Option OPTION_NOT_EQUALS1 = new Option(RPC_OPTION, OTHER_VALUE) {}; + private static final Option OPTION_NOT_EQUALS2 = new Option(ANOTHER_RPC_OPTION, VALUE) {}; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testEquals() { + assertEquals(OPTION, OPTION_EQUALS); + assertNotEquals(OPTION, OPTION_NOT_EQUALS1); + assertNotEquals(OPTION, OPTION_NOT_EQUALS2); + } + + @Test + public void testHashCode() { + assertEquals(OPTION.hashCode(), OPTION_EQUALS.hashCode()); + } + + @Test + public void testConstructor() { + assertEquals(RPC_OPTION, OPTION.rpcOption()); + assertEquals(VALUE, OPTION.value()); + Option option = new Option(RPC_OPTION, null) {}; + assertEquals(RPC_OPTION, option.rpcOption()); + assertNull(option.value()); + thrown.expect(NullPointerException.class); + new Option(null, VALUE) {}; + } +} diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Option.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Option.java index 1ea0c0561eaa..774023eff78b 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Option.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Option.java @@ -18,21 +18,16 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.common.base.Joiner; import com.google.common.base.MoreObjects; -import com.google.common.collect.Sets; -import com.google.gcloud.FieldSelector; import com.google.gcloud.storage.spi.StorageRpc; import java.io.Serializable; -import java.util.List; import java.util.Objects; -import java.util.Set; /** * Base class for Storage operation option. */ -class Option implements Serializable { +abstract class Option implements Serializable { private static final long serialVersionUID = -73199088766477208L; @@ -74,20 +69,4 @@ public String toString() { .add("value", value) .toString(); } - - static String selector(List required, FieldSelector... others) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length); - for (FieldSelector field : required) { - fieldStrings.add(field.selector()); - } - for (FieldSelector field : others) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } - - static StringBuilder selector(StringBuilder partialSelector, List required, - FieldSelector... others) { - return partialSelector.append(selector(required, others)); - } } diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java index eb51e9d0f6c1..87d82aad686b 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java @@ -25,6 +25,7 @@ import com.google.gcloud.AuthCredentials; import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials; import com.google.gcloud.FieldSelector; +import com.google.gcloud.FieldSelector.SelectorHelper; import com.google.gcloud.Page; import com.google.gcloud.ReadChannel; import com.google.gcloud.Service; @@ -256,7 +257,7 @@ public static BucketGetOption metagenerationNotMatch(long metageneration) { */ public static BucketGetOption fields(BucketField... fields) { return new BucketGetOption(StorageRpc.Option.FIELDS, - selector(BucketField.REQUIRED_FIELDS, fields)); + SelectorHelper.selector(BucketField.REQUIRED_FIELDS, fields)); } } @@ -597,7 +598,7 @@ public static BlobGetOption metagenerationNotMatch(long metageneration) { */ public static BlobGetOption fields(BlobField... fields) { return new BlobGetOption(StorageRpc.Option.FIELDS, - selector(BlobField.REQUIRED_FIELDS, fields)); + SelectorHelper.selector(BlobField.REQUIRED_FIELDS, fields)); } } @@ -641,10 +642,8 @@ public static BucketListOption prefix(String prefix) { * specified. */ public static BucketListOption fields(BucketField... fields) { - StringBuilder builder = - selector(new StringBuilder().append("items("), BucketField.REQUIRED_FIELDS, fields) - .append("),nextPageToken"); - return new BucketListOption(StorageRpc.Option.FIELDS, builder.toString()); + return new BucketListOption(StorageRpc.Option.FIELDS, + SelectorHelper.selector("items", BucketField.REQUIRED_FIELDS, fields)); } } @@ -711,10 +710,8 @@ public static BlobListOption versions(boolean versions) { * specified. */ public static BlobListOption fields(BlobField... fields) { - StringBuilder builder = - selector(new StringBuilder().append("items("), BlobField.REQUIRED_FIELDS, fields) - .append("),nextPageToken"); - return new BlobListOption(StorageRpc.Option.FIELDS, builder.toString()); + return new BlobListOption(StorageRpc.Option.FIELDS, + SelectorHelper.selector("items", BlobField.REQUIRED_FIELDS, fields)); } } diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/OptionTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/OptionTest.java index 5924174ab138..08a8e79b2c3b 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/OptionTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/OptionTest.java @@ -17,22 +17,49 @@ package com.google.gcloud.storage; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNull; import com.google.gcloud.storage.spi.StorageRpc; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; public class OptionTest { + private static final StorageRpc.Option RPC_OPTION = StorageRpc.Option.DELIMITER; + private static final StorageRpc.Option ANOTHER_RPC_OPTION = StorageRpc.Option.FIELDS; + private static final String VALUE = "some value"; + private static final String OTHER_VALUE = "another value"; + private static final Option OPTION = new Option(RPC_OPTION, VALUE) {}; + private static final Option OPTION_EQUALS = new Option(RPC_OPTION, VALUE) {}; + private static final Option OPTION_NOT_EQUALS1 = new Option(RPC_OPTION, OTHER_VALUE) {}; + private static final Option OPTION_NOT_EQUALS2 = new Option(ANOTHER_RPC_OPTION, VALUE) {}; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testEquals() { + assertEquals(OPTION, OPTION_EQUALS); + assertNotEquals(OPTION, OPTION_NOT_EQUALS1); + assertNotEquals(OPTION, OPTION_NOT_EQUALS2); + } + @Test - public void testOption() { - Option option = new Option(StorageRpc.Option.DELIMITER, "/"); - assertEquals(StorageRpc.Option.DELIMITER, option.rpcOption()); - assertEquals("/", option.value()); + public void testHashCode() { + assertEquals(OPTION.hashCode(), OPTION_EQUALS.hashCode()); } - @Test(expected = NullPointerException.class) - public void testIndexOutOfBoundsException() { - new Option(null, "/"); + @Test + public void testConstructor() { + assertEquals(RPC_OPTION, OPTION.rpcOption()); + assertEquals(VALUE, OPTION.value()); + Option option = new Option(RPC_OPTION, null) {}; + assertEquals(RPC_OPTION, option.rpcOption()); + assertNull(option.value()); + thrown.expect(NullPointerException.class); + new Option(null, VALUE) {}; } } From 80268f7f4de379ff03f0b4dabea99d167f5084b2 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Wed, 30 Mar 2016 09:10:35 -0700 Subject: [PATCH 41/60] allow cleanup if record not added --- .../test/java/com/google/gcloud/dns/it/ITDnsTest.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java index 38273cff49d6..c9070f2eddde 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java @@ -650,10 +650,11 @@ public void testInvalidChangeRequest() { assertEquals(400, ex.code()); } } finally { - assertTrue(recordAdded); - ChangeRequestInfo deletion = ChangeRequest.builder().delete(validA).build(); - ChangeRequest request = zone.applyChangeRequest(deletion); - waitForChangeToComplete(zone.name(), request.id()); + if (recordAdded) { + ChangeRequestInfo deletion = ChangeRequest.builder().delete(validA).build(); + ChangeRequest request = zone.applyChangeRequest(deletion); + waitForChangeToComplete(zone.name(), request.id()); + } zone.delete(); } } From acacd1db03ae25c638b68629098cd678d516d65e Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Wed, 30 Mar 2016 19:11:07 +0200 Subject: [PATCH 42/60] Update codacy configuration - Disable "Avoid excessively long variable names like" test - Disable "JUnit tests should include assert() or fail()" test - Avoid applying underscore naming rule to test methods --- codacy-conf.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codacy-conf.json b/codacy-conf.json index e8c819684c9c..47998a3870d7 100644 --- a/codacy-conf.json +++ b/codacy-conf.json @@ -1 +1 @@ -{"patterns":[{"patternId":"Custom_Javascript_Scopes","enabled":true},{"patternId":"Custom_Javascript_EvalWith","enabled":true},{"patternId":"Custom_Javascript_TryCatch","enabled":true},{"patternId":"Custom_Scala_NonFatal","enabled":true},{"patternId":"bitwise","enabled":true},{"patternId":"maxparams","enabled":true},{"patternId":"CSSLint_universal_selector","enabled":true},{"patternId":"CSSLint_unqualified_attributes","enabled":true},{"patternId":"CSSLint_zero_units","enabled":true},{"patternId":"CSSLint_overqualified_elements","enabled":true},{"patternId":"CSSLint_shorthand","enabled":true},{"patternId":"CSSLint_duplicate_background_images","enabled":true},{"patternId":"CSSLint_box_model","enabled":true},{"patternId":"CSSLint_compatible_vendor_prefixes","enabled":true},{"patternId":"CSSLint_display_property_grouping","enabled":true},{"patternId":"CSSLint_duplicate_properties","enabled":true},{"patternId":"CSSLint_empty_rules","enabled":true},{"patternId":"CSSLint_errors","enabled":true},{"patternId":"CSSLint_gradients","enabled":true},{"patternId":"CSSLint_important","enabled":true},{"patternId":"CSSLint_known_properties","enabled":true},{"patternId":"CSSLint_text_indent","enabled":true},{"patternId":"CSSLint_unique_headings","enabled":true},{"patternId":"PyLint_E0100","enabled":true},{"patternId":"PyLint_E0101","enabled":true},{"patternId":"PyLint_E0102","enabled":true},{"patternId":"PyLint_E0103","enabled":true},{"patternId":"PyLint_E0104","enabled":true},{"patternId":"PyLint_E0105","enabled":true},{"patternId":"PyLint_E0106","enabled":true},{"patternId":"PyLint_E0107","enabled":true},{"patternId":"PyLint_E0108","enabled":true},{"patternId":"PyLint_E0202","enabled":true},{"patternId":"PyLint_E0203","enabled":true},{"patternId":"PyLint_E0211","enabled":true},{"patternId":"PyLint_E0601","enabled":true},{"patternId":"PyLint_E0603","enabled":true},{"patternId":"PyLint_E0604","enabled":true},{"patternId":"PyLint_E0701","enabled":true},{"patternId":"PyLint_E0702","enabled":true},{"patternId":"PyLint_E0710","enabled":true},{"patternId":"PyLint_E0711","enabled":true},{"patternId":"PyLint_E0712","enabled":true},{"patternId":"PyLint_E1003","enabled":true},{"patternId":"PyLint_E1102","enabled":true},{"patternId":"PyLint_E1111","enabled":true},{"patternId":"PyLint_E1120","enabled":true},{"patternId":"PyLint_E1121","enabled":true},{"patternId":"PyLint_E1123","enabled":true},{"patternId":"PyLint_E1124","enabled":true},{"patternId":"PyLint_E1200","enabled":true},{"patternId":"PyLint_E1201","enabled":true},{"patternId":"PyLint_E1205","enabled":true},{"patternId":"PyLint_E1206","enabled":true},{"patternId":"PyLint_E1300","enabled":true},{"patternId":"PyLint_E1301","enabled":true},{"patternId":"PyLint_E1302","enabled":true},{"patternId":"PyLint_E1303","enabled":true},{"patternId":"PyLint_E1304","enabled":true},{"patternId":"PyLint_E1305","enabled":true},{"patternId":"PyLint_E1306","enabled":true},{"patternId":"rulesets-codesize.xml-CyclomaticComplexity","enabled":true},{"patternId":"rulesets-codesize.xml-NPathComplexity","enabled":true},{"patternId":"rulesets-codesize.xml-ExcessiveMethodLength","enabled":true},{"patternId":"rulesets-codesize.xml-ExcessiveClassLength","enabled":true},{"patternId":"rulesets-codesize.xml-ExcessiveParameterList","enabled":true},{"patternId":"rulesets-codesize.xml-ExcessivePublicCount","enabled":true},{"patternId":"rulesets-codesize.xml-TooManyFields","enabled":true},{"patternId":"rulesets-codesize.xml-TooManyMethods","enabled":true},{"patternId":"rulesets-codesize.xml-ExcessiveClassComplexity","enabled":true},{"patternId":"rulesets-controversial.xml-Superglobals","enabled":true},{"patternId":"rulesets-design.xml-ExitExpression","enabled":true},{"patternId":"rulesets-design.xml-EvalExpression","enabled":true},{"patternId":"rulesets-design.xml-GotoStatement","enabled":true},{"patternId":"rulesets-design.xml-NumberOfChildren","enabled":true},{"patternId":"rulesets-design.xml-DepthOfInheritance","enabled":true},{"patternId":"rulesets-unusedcode.xml-UnusedPrivateField","enabled":true},{"patternId":"rulesets-unusedcode.xml-UnusedLocalVariable","enabled":true},{"patternId":"rulesets-unusedcode.xml-UnusedPrivateMethod","enabled":true},{"patternId":"rulesets-unusedcode.xml-UnusedFormalParameter","enabled":true},{"patternId":"PyLint_C0303","enabled":true},{"patternId":"PyLint_C1001","enabled":true},{"patternId":"rulesets-naming.xml-ShortVariable","enabled":true},{"patternId":"rulesets-naming.xml-LongVariable","enabled":true},{"patternId":"rulesets-naming.xml-ShortMethodName","enabled":true},{"patternId":"rulesets-naming.xml-ConstantNamingConventions","enabled":true},{"patternId":"rulesets-naming.xml-BooleanGetMethodName","enabled":true},{"patternId":"PyLint_W0101","enabled":true},{"patternId":"PyLint_W0102","enabled":true},{"patternId":"PyLint_W0104","enabled":true},{"patternId":"PyLint_W0105","enabled":true},{"patternId":"Custom_Scala_GetCalls","enabled":true},{"patternId":"ScalaStyle_EqualsHashCodeChecker","enabled":true},{"patternId":"ScalaStyle_ParameterNumberChecker","enabled":true},{"patternId":"ScalaStyle_ReturnChecker","enabled":true},{"patternId":"ScalaStyle_NullChecker","enabled":true},{"patternId":"ScalaStyle_NoCloneChecker","enabled":true},{"patternId":"ScalaStyle_NoFinalizeChecker","enabled":true},{"patternId":"ScalaStyle_CovariantEqualsChecker","enabled":true},{"patternId":"ScalaStyle_StructuralTypeChecker","enabled":true},{"patternId":"ScalaStyle_MethodLengthChecker","enabled":true},{"patternId":"ScalaStyle_NumberOfMethodsInTypeChecker","enabled":true},{"patternId":"ScalaStyle_WhileChecker","enabled":true},{"patternId":"ScalaStyle_VarFieldChecker","enabled":true},{"patternId":"ScalaStyle_VarLocalChecker","enabled":true},{"patternId":"ScalaStyle_RedundantIfChecker","enabled":true},{"patternId":"ScalaStyle_DeprecatedJavaChecker","enabled":true},{"patternId":"ScalaStyle_EmptyClassChecker","enabled":true},{"patternId":"ScalaStyle_NotImplementedErrorUsage","enabled":true},{"patternId":"Custom_Scala_GroupImports","enabled":true},{"patternId":"Custom_Scala_ReservedKeywords","enabled":true},{"patternId":"Custom_Scala_ElseIf","enabled":true},{"patternId":"Custom_Scala_CallByNameAsLastArguments","enabled":true},{"patternId":"Custom_Scala_WildcardImportOnMany","enabled":true},{"patternId":"Custom_Scala_UtilTryForTryCatch","enabled":true},{"patternId":"Custom_Scala_ProhibitObjectName","enabled":true},{"patternId":"Custom_Scala_ImportsAtBeginningOfPackage","enabled":true},{"patternId":"Custom_Scala_NameResultsAndParameters","enabled":true},{"patternId":"Custom_Scala_IncompletePatternMatching","enabled":true},{"patternId":"Custom_Scala_UsefulTypeAlias","enabled":true},{"patternId":"Custom_Scala_JavaThreads","enabled":true},{"patternId":"Custom_Scala_DirectPromiseCreation","enabled":true},{"patternId":"Custom_Scala_StructuralTypes","enabled":true},{"patternId":"Custom_Scala_CollectionLastHead","enabled":true},{"patternId":"PyLint_W0106","enabled":true},{"patternId":"PyLint_W0107","enabled":true},{"patternId":"PyLint_W0108","enabled":true},{"patternId":"PyLint_W0109","enabled":true},{"patternId":"PyLint_W0110","enabled":true},{"patternId":"PyLint_W0120","enabled":true},{"patternId":"PyLint_W0122","enabled":true},{"patternId":"PyLint_W0150","enabled":true},{"patternId":"PyLint_W0199","enabled":true},{"patternId":"rulesets-cleancode.xml-ElseExpression","enabled":true},{"patternId":"rulesets-cleancode.xml-StaticAccess","enabled":true},{"patternId":"ScalaStyle_NonASCIICharacterChecker","enabled":true},{"patternId":"ScalaStyle_FieldNamesChecker","enabled":true},{"patternId":"Custom_Scala_WithNameCalls","enabled":true},{"patternId":"strictexception_AvoidRethrowingException","enabled":true},{"patternId":"strings_AppendCharacterWithChar","enabled":true},{"patternId":"braces_IfElseStmtsMustUseBraces","enabled":true},{"patternId":"basic_AvoidDecimalLiteralsInBigDecimalConstructor","enabled":true},{"patternId":"basic_CheckSkipResult","enabled":true},{"patternId":"javabeans_MissingSerialVersionUID","enabled":true},{"patternId":"migrating_ShortInstantiation","enabled":true},{"patternId":"design_AvoidInstanceofChecksInCatchClause","enabled":true},{"patternId":"naming_LongVariable","enabled":true},{"patternId":"migrating_ReplaceEnumerationWithIterator","enabled":true},{"patternId":"j2ee_DoNotCallSystemExit","enabled":true},{"patternId":"unusedcode_UnusedLocalVariable","enabled":true},{"patternId":"strings_InefficientStringBuffering","enabled":true},{"patternId":"basic_DontUseFloatTypeForLoopIndices","enabled":true},{"patternId":"basic_AvoidBranchingStatementAsLastInLoop","enabled":true},{"patternId":"migrating_JUnit4TestShouldUseTestAnnotation","enabled":true},{"patternId":"optimizations_AddEmptyString","enabled":true},{"patternId":"logging-jakarta-commons_ProperLogger","enabled":true},{"patternId":"optimizations_RedundantFieldInitializer","enabled":true},{"patternId":"logging-java_AvoidPrintStackTrace","enabled":true},{"patternId":"empty_EmptyFinallyBlock","enabled":true},{"patternId":"design_CompareObjectsWithEquals","enabled":true},{"patternId":"basic_ClassCastExceptionWithToArray","enabled":true},{"patternId":"strictexception_DoNotExtendJavaLangError","enabled":true},{"patternId":"junit_UnnecessaryBooleanAssertion","enabled":true},{"patternId":"design_SimplifyBooleanExpressions","enabled":true},{"patternId":"basic_ForLoopShouldBeWhileLoop","enabled":true},{"patternId":"basic_BigIntegerInstantiation","enabled":true},{"patternId":"optimizations_UseArrayListInsteadOfVector","enabled":true},{"patternId":"optimizations_UnnecessaryWrapperObjectCreation","enabled":true},{"patternId":"strings_StringBufferInstantiationWithChar","enabled":true},{"patternId":"basic_JumbledIncrementer","enabled":true},{"patternId":"design_SwitchStmtsShouldHaveDefault","enabled":true},{"patternId":"strictexception_AvoidThrowingRawExceptionTypes","enabled":true},{"patternId":"migrating_LongInstantiation","enabled":true},{"patternId":"design_SimplifyBooleanReturns","enabled":true},{"patternId":"empty_EmptyInitializer","enabled":true},{"patternId":"design_FieldDeclarationsShouldBeAtStartOfClass","enabled":true},{"patternId":"unnecessary_UnnecessaryConversionTemporary","enabled":true},{"patternId":"design_AvoidProtectedFieldInFinalClass","enabled":true},{"patternId":"junit_UseAssertTrueInsteadOfAssertEquals","enabled":true},{"patternId":"naming_PackageCase","enabled":true},{"patternId":"migrating_JUnitUseExpected","enabled":true},{"patternId":"controversial_UnnecessaryConstructor","enabled":true},{"patternId":"naming_MethodNamingConventions","enabled":true},{"patternId":"design_DefaultLabelNotLastInSwitchStmt","enabled":true},{"patternId":"basic_UnconditionalIfStatement","enabled":true},{"patternId":"design_SingularField","enabled":true},{"patternId":"design_AssignmentToNonFinalStatic","enabled":true},{"patternId":"braces_WhileLoopsMustUseBraces","enabled":true},{"patternId":"logging-java_SystemPrintln","enabled":true},{"patternId":"strings_UseStringBufferLength","enabled":true},{"patternId":"controversial_AvoidUsingNativeCode","enabled":true},{"patternId":"strictexception_AvoidLosingExceptionInformation","enabled":true},{"patternId":"imports_ImportFromSamePackage","enabled":true},{"patternId":"finalizers_AvoidCallingFinalize","enabled":true},{"patternId":"finalizers_FinalizeOverloaded","enabled":true},{"patternId":"naming_ClassNamingConventions","enabled":true},{"patternId":"logging-java_LoggerIsNotStaticFinal","enabled":true},{"patternId":"finalizers_FinalizeOnlyCallsSuperFinalize","enabled":true},{"patternId":"unnecessary_UselessOverridingMethod","enabled":true},{"patternId":"naming_SuspiciousConstantFieldName","enabled":true},{"patternId":"design_OptimizableToArrayCall","enabled":true},{"patternId":"imports_UnnecessaryFullyQualifiedName","enabled":true},{"patternId":"migrating_ReplaceHashtableWithMap","enabled":true},{"patternId":"unusedcode_UnusedPrivateField","enabled":true},{"patternId":"strings_UnnecessaryCaseChange","enabled":true},{"patternId":"migrating_IntegerInstantiation","enabled":true},{"patternId":"design_NonStaticInitializer","enabled":true},{"patternId":"design_MissingBreakInSwitch","enabled":true},{"patternId":"design_AvoidReassigningParameters","enabled":true},{"patternId":"basic_AvoidThreadGroup","enabled":true},{"patternId":"empty_EmptyCatchBlock","parameters":{"allowCommentedBlocks":"true"},"enabled":true},{"patternId":"codesize_ExcessiveParameterList","parameters":{"minimum":"8","violationSuppressRegex":"\"\"","violationSuppressXPath":"\"\""},"enabled":true},{"patternId":"naming_SuspiciousHashcodeMethodName","enabled":true},{"patternId":"migrating_JUnit4TestShouldUseBeforeAnnotation","enabled":true},{"patternId":"design_UncommentedEmptyMethodBody","enabled":true},{"patternId":"basic_BrokenNullCheck","enabled":true},{"patternId":"strings_ConsecutiveLiteralAppends","enabled":true},{"patternId":"strings_StringInstantiation","enabled":true},{"patternId":"design_EqualsNull","enabled":true},{"patternId":"basic_OverrideBothEqualsAndHashcode","enabled":true},{"patternId":"design_InstantiationToGetClass","enabled":true},{"patternId":"basic_BooleanInstantiation","enabled":true},{"patternId":"strings_AvoidStringBufferField","enabled":true},{"patternId":"basic_ReturnFromFinallyBlock","enabled":true},{"patternId":"empty_EmptyTryBlock","enabled":true},{"patternId":"naming_SuspiciousEqualsMethodName","enabled":true},{"patternId":"basic_ExtendsObject","enabled":true},{"patternId":"strings_UselessStringValueOf","enabled":true},{"patternId":"design_UnsynchronizedStaticDateFormatter","enabled":true},{"patternId":"design_UseCollectionIsEmpty","enabled":true},{"patternId":"controversial_AvoidFinalLocalVariable","enabled":true},{"patternId":"strictexception_AvoidThrowingNullPointerException","enabled":true},{"patternId":"design_AvoidProtectedMethodInFinalClassNotExtending","enabled":true},{"patternId":"optimizations_PrematureDeclaration","enabled":true},{"patternId":"empty_EmptySwitchStatements","enabled":true},{"patternId":"basic_MisplacedNullCheck","enabled":true},{"patternId":"optimizations_UseStringBufferForStringAppends","enabled":true},{"patternId":"strings_StringToString","enabled":true},{"patternId":"naming_MethodWithSameNameAsEnclosingClass","enabled":true},{"patternId":"migrating_ReplaceVectorWithList","enabled":true},{"patternId":"imports_UnusedImports","enabled":true},{"patternId":"unnecessary_UnnecessaryFinalModifier","enabled":true},{"patternId":"basic_AvoidMultipleUnaryOperators","enabled":true},{"patternId":"junit_SimplifyBooleanAssertion","enabled":true},{"patternId":"unnecessary_UselessParentheses","enabled":true},{"patternId":"design_IdempotentOperations","enabled":true},{"patternId":"braces_IfStmtsMustUseBraces","enabled":true},{"patternId":"strings_UseIndexOfChar","enabled":true},{"patternId":"naming_NoPackage","enabled":true},{"patternId":"finalizers_FinalizeDoesNotCallSuperFinalize","enabled":true},{"patternId":"design_UseVarargs","enabled":true},{"patternId":"unusedcode_UnusedFormalParameter","enabled":true},{"patternId":"design_ReturnEmptyArrayRatherThanNull","enabled":true},{"patternId":"junit_UseAssertNullInsteadOfAssertTrue","enabled":true},{"patternId":"design_UseUtilityClass","enabled":true},{"patternId":"design_AvoidDeeplyNestedIfStmts","enabled":true},{"patternId":"empty_EmptyStatementNotInLoop","enabled":true},{"patternId":"junit_UseAssertSameInsteadOfAssertTrue","enabled":true},{"patternId":"braces_ForLoopsMustUseBraces","enabled":true},{"patternId":"controversial_DoNotCallGarbageCollectionExplicitly","enabled":true},{"patternId":"naming_GenericsNaming","enabled":true},{"patternId":"strings_UseEqualsToCompareStrings","enabled":true},{"patternId":"optimizations_AvoidArrayLoops","enabled":true},{"patternId":"empty_EmptyStaticInitializer","enabled":true},{"patternId":"design_UncommentedEmptyConstructor","enabled":true},{"patternId":"empty_EmptyStatementBlock","enabled":true},{"patternId":"basic_CollapsibleIfStatements","enabled":true},{"patternId":"design_FinalFieldCouldBeStatic","enabled":true},{"patternId":"logging-java_MoreThanOneLogger","enabled":true},{"patternId":"codesize_ExcessiveClassLength","enabled":true},{"patternId":"design_ImmutableField","enabled":true},{"patternId":"controversial_OneDeclarationPerLine","enabled":true},{"patternId":"empty_EmptyWhileStmt","enabled":true},{"patternId":"unnecessary_UnnecessaryReturn","enabled":true},{"patternId":"strings_InefficientEmptyStringCheck","enabled":true},{"patternId":"design_UseNotifyAllInsteadOfNotify","enabled":true},{"patternId":"strictexception_DoNotThrowExceptionInFinally","enabled":true},{"patternId":"junit_UseAssertEqualsInsteadOfAssertTrue","enabled":true},{"patternId":"typeresolution_CloneMethodMustImplementCloneable","enabled":true},{"patternId":"codesize_NPathComplexity","enabled":true},{"patternId":"imports_DontImportJavaLang","enabled":true},{"patternId":"empty_EmptySynchronizedBlock","enabled":true},{"patternId":"migrating_JUnit4TestShouldUseAfterAnnotation","enabled":true},{"patternId":"design_AvoidConstantsInterface","enabled":true},{"patternId":"unnecessary_UselessOperationOnImmutable","enabled":true},{"patternId":"design_PositionLiteralsFirstInComparisons","enabled":true},{"patternId":"migrating_ByteInstantiation","enabled":true},{"patternId":"junit_JUnitSpelling","enabled":true},{"patternId":"junit_JUnitTestsShouldIncludeAssert","enabled":true},{"patternId":"finalizers_EmptyFinalizer","enabled":true},{"patternId":"design_NonCaseLabelInSwitchStatement","enabled":true},{"patternId":"android_DoNotHardCodeSDCard","enabled":true},{"patternId":"design_LogicInversion","enabled":true},{"patternId":"unusedcode_UnusedPrivateMethod","enabled":true},{"patternId":"naming_AvoidDollarSigns","enabled":true},{"patternId":"finalizers_FinalizeShouldBeProtected","enabled":true},{"patternId":"clone_ProperCloneImplementation","enabled":true},{"patternId":"basic_CheckResultSet","enabled":true},{"patternId":"controversial_AvoidPrefixingMethodParameters","enabled":true},{"patternId":"migrating_JUnit4SuitesShouldUseSuiteAnnotation","enabled":true},{"patternId":"empty_EmptyIfStmt","enabled":true},{"patternId":"basic_DontCallThreadRun","enabled":true},{"patternId":"junit_JUnitStaticSuite","enabled":true},{"patternId":"optimizations_UseArraysAsList","enabled":true},{"patternId":"design_MissingStaticMethodInNonInstantiatableClass","enabled":true},{"patternId":"unusedcode_UnusedModifier","enabled":true},{"patternId":"Style_MethodName","enabled":true},{"patternId":"Metrics_CyclomaticComplexity","enabled":true},{"patternId":"Lint_DuplicateMethods","enabled":true},{"patternId":"Style_Lambda","enabled":true},{"patternId":"Lint_UselessSetterCall","enabled":true},{"patternId":"Style_VariableName","enabled":true},{"patternId":"Lint_AmbiguousOperator","enabled":true},{"patternId":"Style_LeadingCommentSpace","enabled":true},{"patternId":"Style_CaseEquality","enabled":true},{"patternId":"Lint_StringConversionInInterpolation","enabled":true},{"patternId":"Performance_ReverseEach","enabled":true},{"patternId":"Lint_LiteralInCondition","enabled":true},{"patternId":"Performance_Sample","enabled":true},{"patternId":"Style_NonNilCheck","enabled":true},{"patternId":"Lint_RescueException","enabled":true},{"patternId":"Lint_UselessElseWithoutRescue","enabled":true},{"patternId":"Style_ConstantName","enabled":true},{"patternId":"Lint_LiteralInInterpolation","enabled":true},{"patternId":"Lint_NestedMethodDefinition","enabled":true},{"patternId":"Style_DoubleNegation","enabled":true},{"patternId":"Lint_SpaceBeforeFirstArg","enabled":true},{"patternId":"Lint_Debugger","enabled":true},{"patternId":"Style_ClassVars","enabled":true},{"patternId":"Lint_EmptyEnsure","enabled":true},{"patternId":"Style_MultilineBlockLayout","enabled":true},{"patternId":"Lint_UnusedBlockArgument","enabled":true},{"patternId":"Lint_UselessAccessModifier","enabled":true},{"patternId":"Performance_Size","enabled":true},{"patternId":"Lint_EachWithObjectArgument","enabled":true},{"patternId":"Style_Alias","enabled":true},{"patternId":"Lint_Loop","enabled":true},{"patternId":"Style_NegatedWhile","enabled":true},{"patternId":"Style_ColonMethodCall","enabled":true},{"patternId":"Lint_AmbiguousRegexpLiteral","enabled":true},{"patternId":"Lint_UnusedMethodArgument","enabled":true},{"patternId":"Style_MultilineIfThen","enabled":true},{"patternId":"Lint_EnsureReturn","enabled":true},{"patternId":"Style_NegatedIf","enabled":true},{"patternId":"Lint_Eval","enabled":true},{"patternId":"Style_NilComparison","enabled":true},{"patternId":"Style_ArrayJoin","enabled":true},{"patternId":"Lint_ConditionPosition","enabled":true},{"patternId":"Lint_UnreachableCode","enabled":true},{"patternId":"Performance_Count","enabled":true},{"patternId":"Lint_EmptyInterpolation","enabled":true},{"patternId":"Style_LambdaCall","enabled":true},{"patternId":"Lint_HandleExceptions","enabled":true},{"patternId":"Lint_ShadowingOuterLocalVariable","enabled":true},{"patternId":"Lint_EndAlignment","enabled":true},{"patternId":"Style_MultilineTernaryOperator","enabled":true},{"patternId":"Style_AutoResourceCleanup","enabled":true},{"patternId":"Lint_ElseLayout","enabled":true},{"patternId":"Style_NestedTernaryOperator","enabled":true},{"patternId":"Style_OneLineConditional","enabled":true},{"patternId":"Style_EmptyElse","enabled":true},{"patternId":"Lint_UselessComparison","enabled":true},{"patternId":"Metrics_PerceivedComplexity","enabled":true},{"patternId":"Style_InfiniteLoop","enabled":true},{"patternId":"Rails_Date","enabled":true},{"patternId":"Style_EvenOdd","enabled":true},{"patternId":"Style_IndentationConsistency","enabled":true},{"patternId":"Style_ModuleFunction","enabled":true},{"patternId":"Lint_UselessAssignment","enabled":true},{"patternId":"Style_EachWithObject","enabled":true},{"patternId":"Performance_Detect","enabled":true},{"patternId":"duplicate_key","enabled":true},{"patternId":"no_interpolation_in_single_quotes","enabled":true},{"patternId":"no_backticks","enabled":true},{"patternId":"no_unnecessary_fat_arrows","enabled":true},{"patternId":"indentation","enabled":true},{"patternId":"ensure_comprehensions","enabled":true},{"patternId":"no_stand_alone_at","enabled":true},{"patternId":"cyclomatic_complexity","enabled":true},{"patternId":"Deserialize","enabled":true},{"patternId":"SymbolDoS","enabled":true},{"patternId":"SkipBeforeFilter","enabled":true},{"patternId":"SanitizeMethods","enabled":true},{"patternId":"SelectTag","enabled":true},{"patternId":"XMLDoS","enabled":true},{"patternId":"SimpleFormat","enabled":true},{"patternId":"Evaluation","enabled":true},{"patternId":"BasicAuth","enabled":true},{"patternId":"JRubyXML","enabled":true},{"patternId":"RenderInline","enabled":true},{"patternId":"YAMLParsing","enabled":true},{"patternId":"Redirect","enabled":true},{"patternId":"UnsafeReflection","enabled":true},{"patternId":"SSLVerify","enabled":true},{"patternId":"HeaderDoS","enabled":true},{"patternId":"TranslateBug","enabled":true},{"patternId":"Execute","enabled":true},{"patternId":"JSONParsing","enabled":true},{"patternId":"LinkTo","enabled":true},{"patternId":"FileDisclosure","enabled":true},{"patternId":"SafeBufferManipulation","enabled":true},{"patternId":"ModelAttributes","enabled":true},{"patternId":"ResponseSplitting","enabled":true},{"patternId":"DigestDoS","enabled":true},{"patternId":"Send","enabled":true},{"patternId":"MailTo","enabled":true},{"patternId":"SymbolDoSCVE","enabled":true},{"patternId":"StripTags","enabled":true},{"patternId":"MassAssignment","enabled":true},{"patternId":"RegexDoS","enabled":true},{"patternId":"SelectVulnerability","enabled":true},{"patternId":"FileAccess","enabled":true},{"patternId":"ContentTag","enabled":true},{"patternId":"SessionSettings","enabled":true},{"patternId":"FilterSkipping","enabled":true},{"patternId":"CreateWith","enabled":true},{"patternId":"JSONEncoding","enabled":true},{"patternId":"SQLCVEs","enabled":true},{"patternId":"ForgerySetting","enabled":true},{"patternId":"QuoteTableName","enabled":true},{"patternId":"I18nXSS","enabled":true},{"patternId":"WithoutProtection","enabled":true},{"patternId":"CrossSiteScripting","enabled":true},{"patternId":"SingleQuotes","enabled":true},{"patternId":"NestedAttributes","enabled":true},{"patternId":"DetailedExceptions","enabled":true},{"patternId":"LinkToHref","enabled":true},{"patternId":"RenderDoS","enabled":true},{"patternId":"ModelSerialize","enabled":true},{"patternId":"SQL","enabled":true},{"patternId":"Render","enabled":true},{"patternId":"UnscopedFind","enabled":true},{"patternId":"ValidationRegex","enabled":true},{"patternId":"EscapeFunction","enabled":true},{"patternId":"Custom_Scala_FieldNamesChecker","enabled":true},{"patternId":"Custom_Scala_ObjDeserialization","enabled":true},{"patternId":"Custom_Scala_RSAPadding","enabled":true},{"patternId":"ESLint_no-extra-boolean-cast","enabled":true},{"patternId":"ESLint_no-iterator","enabled":true},{"patternId":"ESLint_no-invalid-regexp","enabled":true},{"patternId":"ESLint_no-obj-calls","enabled":true},{"patternId":"ESLint_no-sparse-arrays","enabled":true},{"patternId":"ESLint_no-unreachable","enabled":true},{"patternId":"ESLint_no-dupe-keys","enabled":true},{"patternId":"ESLint_no-multi-str","enabled":true},{"patternId":"ESLint_no-extend-native","enabled":true},{"patternId":"ESLint_guard-for-in","enabled":true},{"patternId":"ESLint_no-func-assign","enabled":true},{"patternId":"ESLint_no-extra-semi","enabled":true},{"patternId":"ESLint_camelcase","enabled":true},{"patternId":"ESLint_no-mixed-spaces-and-tabs","enabled":true},{"patternId":"ESLint_no-undef","enabled":true},{"patternId":"ESLint_semi","enabled":true},{"patternId":"ESLint_no-empty-character-class","enabled":true},{"patternId":"ESLint_complexity","enabled":true},{"patternId":"ESLint_no-dupe-class-members","enabled":true},{"patternId":"ESLint_no-debugger","enabled":true},{"patternId":"ESLint_block-scoped-var","enabled":true},{"patternId":"ESLint_no-loop-func","enabled":true},{"patternId":"ESLint_no-use-before-define","enabled":true},{"patternId":"ESLint_no-console","enabled":true},{"patternId":"ESLint_require-yield","enabled":true},{"patternId":"ESLint_no-redeclare","enabled":true},{"patternId":"ESLint_no-undefined","enabled":true},{"patternId":"ESLint_use-isnan","enabled":true},{"patternId":"ESLint_no-control-regex","enabled":true},{"patternId":"ESLint_no-const-assign","enabled":true},{"patternId":"ESLint_no-new","enabled":true},{"patternId":"ESLint_new-cap","enabled":true},{"patternId":"ESLint_no-irregular-whitespace","enabled":true},{"patternId":"ESLint_object-shorthand","enabled":true},{"patternId":"ESLint_no-ex-assign","enabled":true},{"patternId":"ESLint_wrap-iife","enabled":true},{"patternId":"ESLint_arrow-parens","enabled":true},{"patternId":"ESLint_no-constant-condition","enabled":true},{"patternId":"ESLint_no-octal","enabled":true},{"patternId":"ESLint_no-dupe-args","enabled":true},{"patternId":"ESLint_quotes","enabled":true},{"patternId":"ESLint_no-fallthrough","enabled":true},{"patternId":"ESLint_no-delete-var","enabled":true},{"patternId":"ESLint_no-caller","enabled":true},{"patternId":"ESLint_no-cond-assign","enabled":true},{"patternId":"ESLint_no-this-before-super","enabled":true},{"patternId":"ESLint_no-negated-in-lhs","enabled":true},{"patternId":"ESLint_no-inner-declarations","enabled":true},{"patternId":"ESLint_eqeqeq","enabled":true},{"patternId":"ESLint_curly","enabled":true},{"patternId":"ESLint_arrow-spacing","enabled":true},{"patternId":"ESLint_no-empty","enabled":true},{"patternId":"ESLint_no-unused-vars","enabled":true},{"patternId":"ESLint_generator-star-spacing","enabled":true},{"patternId":"ESLint_no-duplicate-case","enabled":true},{"patternId":"ESLint_valid-typeof","enabled":true},{"patternId":"ESLint_no-regex-spaces","enabled":true},{"patternId":"ESLint_no-class-assign","enabled":true},{"patternId":"PyLint_W0221","enabled":true},{"patternId":"PyLint_E0117","enabled":true},{"patternId":"PyLint_E0001","enabled":true},{"patternId":"PyLint_E0241","enabled":true},{"patternId":"PyLint_W0404","enabled":true},{"patternId":"PyLint_E0704","enabled":true},{"patternId":"PyLint_E0703","enabled":true},{"patternId":"PyLint_E0302","enabled":true},{"patternId":"PyLint_W1301","enabled":true},{"patternId":"PyLint_R0201","enabled":true},{"patternId":"PyLint_E0113","enabled":true},{"patternId":"PyLint_W0410","enabled":true},{"patternId":"PyLint_C0123","enabled":true},{"patternId":"PyLint_E0115","enabled":true},{"patternId":"PyLint_E0114","enabled":true},{"patternId":"PyLint_E1126","enabled":true},{"patternId":"PyLint_W0702","enabled":true},{"patternId":"PyLint_W1303","enabled":true},{"patternId":"PyLint_W0622","enabled":true},{"patternId":"PyLint_W0222","enabled":true},{"patternId":"PyLint_W0233","enabled":true},{"patternId":"PyLint_W1305","enabled":true},{"patternId":"PyLint_E1127","enabled":true},{"patternId":"PyLint_E0112","enabled":true},{"patternId":"PyLint_W0611","enabled":true},{"patternId":"PyLint_W0601","enabled":true},{"patternId":"PyLint_W1300","enabled":true},{"patternId":"PyLint_W0124","enabled":true},{"patternId":"PyLint_R0203","enabled":true},{"patternId":"PyLint_E0236","enabled":true},{"patternId":"PyLint_W0612","enabled":true},{"patternId":"PyLint_W0604","enabled":true},{"patternId":"PyLint_W0705","enabled":true},{"patternId":"PyLint_E0238","enabled":true},{"patternId":"PyLint_W0602","enabled":true},{"patternId":"PyLint_R0102","enabled":true},{"patternId":"PyLint_R0202","enabled":true},{"patternId":"PyLint_E0240","enabled":true},{"patternId":"PyLint_W0623","enabled":true},{"patternId":"PyLint_W0711","enabled":true},{"patternId":"PyLint_E0116","enabled":true},{"patternId":"PyLint_E0239","enabled":true},{"patternId":"PyLint_E1132","enabled":true},{"patternId":"PyLint_W1307","enabled":true},{"patternId":"PyLint_C0200","enabled":true},{"patternId":"PyLint_E0301","enabled":true},{"patternId":"PyLint_W1306","enabled":true},{"patternId":"PyLint_W1302","enabled":true},{"patternId":"PyLint_E0110","enabled":true},{"patternId":"PyLint_E1125","enabled":true}]} \ No newline at end of file +{"patterns":[{"patternId":"Custom_Javascript_Scopes","enabled":true},{"patternId":"Custom_Javascript_EvalWith","enabled":true},{"patternId":"Custom_Javascript_TryCatch","enabled":true},{"patternId":"Custom_Scala_NonFatal","enabled":true},{"patternId":"bitwise","enabled":true},{"patternId":"maxparams","enabled":true},{"patternId":"CSSLint_universal_selector","enabled":true},{"patternId":"CSSLint_unqualified_attributes","enabled":true},{"patternId":"CSSLint_zero_units","enabled":true},{"patternId":"CSSLint_overqualified_elements","enabled":true},{"patternId":"CSSLint_shorthand","enabled":true},{"patternId":"CSSLint_duplicate_background_images","enabled":true},{"patternId":"CSSLint_box_model","enabled":true},{"patternId":"CSSLint_compatible_vendor_prefixes","enabled":true},{"patternId":"CSSLint_display_property_grouping","enabled":true},{"patternId":"CSSLint_duplicate_properties","enabled":true},{"patternId":"CSSLint_empty_rules","enabled":true},{"patternId":"CSSLint_errors","enabled":true},{"patternId":"CSSLint_gradients","enabled":true},{"patternId":"CSSLint_important","enabled":true},{"patternId":"CSSLint_known_properties","enabled":true},{"patternId":"CSSLint_text_indent","enabled":true},{"patternId":"CSSLint_unique_headings","enabled":true},{"patternId":"PyLint_E0100","enabled":true},{"patternId":"PyLint_E0101","enabled":true},{"patternId":"PyLint_E0102","enabled":true},{"patternId":"PyLint_E0103","enabled":true},{"patternId":"PyLint_E0104","enabled":true},{"patternId":"PyLint_E0105","enabled":true},{"patternId":"PyLint_E0106","enabled":true},{"patternId":"PyLint_E0107","enabled":true},{"patternId":"PyLint_E0108","enabled":true},{"patternId":"PyLint_E0202","enabled":true},{"patternId":"PyLint_E0203","enabled":true},{"patternId":"PyLint_E0211","enabled":true},{"patternId":"PyLint_E0601","enabled":true},{"patternId":"PyLint_E0603","enabled":true},{"patternId":"PyLint_E0604","enabled":true},{"patternId":"PyLint_E0701","enabled":true},{"patternId":"PyLint_E0702","enabled":true},{"patternId":"PyLint_E0710","enabled":true},{"patternId":"PyLint_E0711","enabled":true},{"patternId":"PyLint_E0712","enabled":true},{"patternId":"PyLint_E1003","enabled":true},{"patternId":"PyLint_E1102","enabled":true},{"patternId":"PyLint_E1111","enabled":true},{"patternId":"PyLint_E1120","enabled":true},{"patternId":"PyLint_E1121","enabled":true},{"patternId":"PyLint_E1123","enabled":true},{"patternId":"PyLint_E1124","enabled":true},{"patternId":"PyLint_E1200","enabled":true},{"patternId":"PyLint_E1201","enabled":true},{"patternId":"PyLint_E1205","enabled":true},{"patternId":"PyLint_E1206","enabled":true},{"patternId":"PyLint_E1300","enabled":true},{"patternId":"PyLint_E1301","enabled":true},{"patternId":"PyLint_E1302","enabled":true},{"patternId":"PyLint_E1303","enabled":true},{"patternId":"PyLint_E1304","enabled":true},{"patternId":"PyLint_E1305","enabled":true},{"patternId":"PyLint_E1306","enabled":true},{"patternId":"rulesets-codesize.xml-CyclomaticComplexity","enabled":true},{"patternId":"rulesets-codesize.xml-NPathComplexity","enabled":true},{"patternId":"rulesets-codesize.xml-ExcessiveMethodLength","enabled":true},{"patternId":"rulesets-codesize.xml-ExcessiveClassLength","enabled":true},{"patternId":"rulesets-codesize.xml-ExcessiveParameterList","enabled":true},{"patternId":"rulesets-codesize.xml-ExcessivePublicCount","enabled":true},{"patternId":"rulesets-codesize.xml-TooManyFields","enabled":true},{"patternId":"rulesets-codesize.xml-TooManyMethods","enabled":true},{"patternId":"rulesets-codesize.xml-ExcessiveClassComplexity","enabled":true},{"patternId":"rulesets-controversial.xml-Superglobals","enabled":true},{"patternId":"rulesets-design.xml-ExitExpression","enabled":true},{"patternId":"rulesets-design.xml-EvalExpression","enabled":true},{"patternId":"rulesets-design.xml-GotoStatement","enabled":true},{"patternId":"rulesets-design.xml-NumberOfChildren","enabled":true},{"patternId":"rulesets-design.xml-DepthOfInheritance","enabled":true},{"patternId":"rulesets-unusedcode.xml-UnusedPrivateField","enabled":true},{"patternId":"rulesets-unusedcode.xml-UnusedLocalVariable","enabled":true},{"patternId":"rulesets-unusedcode.xml-UnusedPrivateMethod","enabled":true},{"patternId":"rulesets-unusedcode.xml-UnusedFormalParameter","enabled":true},{"patternId":"PyLint_C0303","enabled":true},{"patternId":"PyLint_C1001","enabled":true},{"patternId":"rulesets-naming.xml-ShortVariable","enabled":true},{"patternId":"rulesets-naming.xml-LongVariable","enabled":true},{"patternId":"rulesets-naming.xml-ShortMethodName","enabled":true},{"patternId":"rulesets-naming.xml-ConstantNamingConventions","enabled":true},{"patternId":"rulesets-naming.xml-BooleanGetMethodName","enabled":true},{"patternId":"PyLint_W0101","enabled":true},{"patternId":"PyLint_W0102","enabled":true},{"patternId":"PyLint_W0104","enabled":true},{"patternId":"PyLint_W0105","enabled":true},{"patternId":"Custom_Scala_GetCalls","enabled":true},{"patternId":"ScalaStyle_EqualsHashCodeChecker","enabled":true},{"patternId":"ScalaStyle_ParameterNumberChecker","enabled":true},{"patternId":"ScalaStyle_ReturnChecker","enabled":true},{"patternId":"ScalaStyle_NullChecker","enabled":true},{"patternId":"ScalaStyle_NoCloneChecker","enabled":true},{"patternId":"ScalaStyle_NoFinalizeChecker","enabled":true},{"patternId":"ScalaStyle_CovariantEqualsChecker","enabled":true},{"patternId":"ScalaStyle_StructuralTypeChecker","enabled":true},{"patternId":"ScalaStyle_MethodLengthChecker","enabled":true},{"patternId":"ScalaStyle_NumberOfMethodsInTypeChecker","enabled":true},{"patternId":"ScalaStyle_WhileChecker","enabled":true},{"patternId":"ScalaStyle_VarFieldChecker","enabled":true},{"patternId":"ScalaStyle_VarLocalChecker","enabled":true},{"patternId":"ScalaStyle_RedundantIfChecker","enabled":true},{"patternId":"ScalaStyle_DeprecatedJavaChecker","enabled":true},{"patternId":"ScalaStyle_EmptyClassChecker","enabled":true},{"patternId":"ScalaStyle_NotImplementedErrorUsage","enabled":true},{"patternId":"Custom_Scala_GroupImports","enabled":true},{"patternId":"Custom_Scala_ReservedKeywords","enabled":true},{"patternId":"Custom_Scala_ElseIf","enabled":true},{"patternId":"Custom_Scala_CallByNameAsLastArguments","enabled":true},{"patternId":"Custom_Scala_WildcardImportOnMany","enabled":true},{"patternId":"Custom_Scala_UtilTryForTryCatch","enabled":true},{"patternId":"Custom_Scala_ProhibitObjectName","enabled":true},{"patternId":"Custom_Scala_ImportsAtBeginningOfPackage","enabled":true},{"patternId":"Custom_Scala_NameResultsAndParameters","enabled":true},{"patternId":"Custom_Scala_IncompletePatternMatching","enabled":true},{"patternId":"Custom_Scala_UsefulTypeAlias","enabled":true},{"patternId":"Custom_Scala_JavaThreads","enabled":true},{"patternId":"Custom_Scala_DirectPromiseCreation","enabled":true},{"patternId":"Custom_Scala_StructuralTypes","enabled":true},{"patternId":"Custom_Scala_CollectionLastHead","enabled":true},{"patternId":"PyLint_W0106","enabled":true},{"patternId":"PyLint_W0107","enabled":true},{"patternId":"PyLint_W0108","enabled":true},{"patternId":"PyLint_W0109","enabled":true},{"patternId":"PyLint_W0110","enabled":true},{"patternId":"PyLint_W0120","enabled":true},{"patternId":"PyLint_W0122","enabled":true},{"patternId":"PyLint_W0150","enabled":true},{"patternId":"PyLint_W0199","enabled":true},{"patternId":"rulesets-cleancode.xml-ElseExpression","enabled":true},{"patternId":"rulesets-cleancode.xml-StaticAccess","enabled":true},{"patternId":"ScalaStyle_NonASCIICharacterChecker","enabled":true},{"patternId":"ScalaStyle_FieldNamesChecker","enabled":true},{"patternId":"Custom_Scala_WithNameCalls","enabled":true},{"patternId":"strictexception_AvoidRethrowingException","enabled":true},{"patternId":"strings_AppendCharacterWithChar","enabled":true},{"patternId":"braces_IfElseStmtsMustUseBraces","enabled":true},{"patternId":"basic_AvoidDecimalLiteralsInBigDecimalConstructor","enabled":true},{"patternId":"basic_CheckSkipResult","enabled":true},{"patternId":"javabeans_MissingSerialVersionUID","enabled":true},{"patternId":"migrating_ShortInstantiation","enabled":true},{"patternId":"design_AvoidInstanceofChecksInCatchClause","enabled":true},{"patternId":"migrating_ReplaceEnumerationWithIterator","enabled":true},{"patternId":"j2ee_DoNotCallSystemExit","enabled":true},{"patternId":"unusedcode_UnusedLocalVariable","enabled":true},{"patternId":"strings_InefficientStringBuffering","enabled":true},{"patternId":"basic_DontUseFloatTypeForLoopIndices","enabled":true},{"patternId":"basic_AvoidBranchingStatementAsLastInLoop","enabled":true},{"patternId":"migrating_JUnit4TestShouldUseTestAnnotation","enabled":true},{"patternId":"optimizations_AddEmptyString","enabled":true},{"patternId":"logging-jakarta-commons_ProperLogger","enabled":true},{"patternId":"optimizations_RedundantFieldInitializer","enabled":true},{"patternId":"logging-java_AvoidPrintStackTrace","enabled":true},{"patternId":"empty_EmptyFinallyBlock","enabled":true},{"patternId":"design_CompareObjectsWithEquals","enabled":true},{"patternId":"basic_ClassCastExceptionWithToArray","enabled":true},{"patternId":"strictexception_DoNotExtendJavaLangError","enabled":true},{"patternId":"junit_UnnecessaryBooleanAssertion","enabled":true},{"patternId":"design_SimplifyBooleanExpressions","enabled":true},{"patternId":"basic_ForLoopShouldBeWhileLoop","enabled":true},{"patternId":"basic_BigIntegerInstantiation","enabled":true},{"patternId":"optimizations_UseArrayListInsteadOfVector","enabled":true},{"patternId":"optimizations_UnnecessaryWrapperObjectCreation","enabled":true},{"patternId":"strings_StringBufferInstantiationWithChar","enabled":true},{"patternId":"basic_JumbledIncrementer","enabled":true},{"patternId":"design_SwitchStmtsShouldHaveDefault","enabled":true},{"patternId":"strictexception_AvoidThrowingRawExceptionTypes","enabled":true},{"patternId":"migrating_LongInstantiation","enabled":true},{"patternId":"design_SimplifyBooleanReturns","enabled":true},{"patternId":"empty_EmptyInitializer","enabled":true},{"patternId":"design_FieldDeclarationsShouldBeAtStartOfClass","enabled":true},{"patternId":"unnecessary_UnnecessaryConversionTemporary","enabled":true},{"patternId":"design_AvoidProtectedFieldInFinalClass","enabled":true},{"patternId":"junit_UseAssertTrueInsteadOfAssertEquals","enabled":true},{"patternId":"naming_PackageCase","enabled":true},{"patternId":"migrating_JUnitUseExpected","enabled":true},{"patternId":"controversial_UnnecessaryConstructor","enabled":true},{"patternId":"naming_MethodNamingConventions","parameters":{"violationSuppressRegex":"\"\"","violationSuppressXPath":"\"//MethodDeclarator[contains(@Image, 'test')]\""},"enabled":true},{"patternId":"design_DefaultLabelNotLastInSwitchStmt","enabled":true},{"patternId":"basic_UnconditionalIfStatement","enabled":true},{"patternId":"design_SingularField","enabled":true},{"patternId":"design_AssignmentToNonFinalStatic","enabled":true},{"patternId":"braces_WhileLoopsMustUseBraces","enabled":true},{"patternId":"logging-java_SystemPrintln","enabled":true},{"patternId":"strings_UseStringBufferLength","enabled":true},{"patternId":"controversial_AvoidUsingNativeCode","enabled":true},{"patternId":"strictexception_AvoidLosingExceptionInformation","enabled":true},{"patternId":"imports_ImportFromSamePackage","enabled":true},{"patternId":"finalizers_AvoidCallingFinalize","enabled":true},{"patternId":"finalizers_FinalizeOverloaded","enabled":true},{"patternId":"naming_ClassNamingConventions","enabled":true},{"patternId":"logging-java_LoggerIsNotStaticFinal","enabled":true},{"patternId":"finalizers_FinalizeOnlyCallsSuperFinalize","enabled":true},{"patternId":"unnecessary_UselessOverridingMethod","enabled":true},{"patternId":"naming_SuspiciousConstantFieldName","enabled":true},{"patternId":"design_OptimizableToArrayCall","enabled":true},{"patternId":"imports_UnnecessaryFullyQualifiedName","enabled":true},{"patternId":"migrating_ReplaceHashtableWithMap","enabled":true},{"patternId":"unusedcode_UnusedPrivateField","enabled":true},{"patternId":"strings_UnnecessaryCaseChange","enabled":true},{"patternId":"migrating_IntegerInstantiation","enabled":true},{"patternId":"design_NonStaticInitializer","enabled":true},{"patternId":"design_MissingBreakInSwitch","enabled":true},{"patternId":"design_AvoidReassigningParameters","enabled":true},{"patternId":"basic_AvoidThreadGroup","enabled":true},{"patternId":"empty_EmptyCatchBlock","parameters":{"allowCommentedBlocks":"true"},"enabled":true},{"patternId":"codesize_ExcessiveParameterList","parameters":{"minimum":"8","violationSuppressRegex":"\"\"","violationSuppressXPath":"\"\""},"enabled":true},{"patternId":"naming_SuspiciousHashcodeMethodName","enabled":true},{"patternId":"migrating_JUnit4TestShouldUseBeforeAnnotation","enabled":true},{"patternId":"design_UncommentedEmptyMethodBody","enabled":true},{"patternId":"basic_BrokenNullCheck","enabled":true},{"patternId":"strings_ConsecutiveLiteralAppends","enabled":true},{"patternId":"strings_StringInstantiation","enabled":true},{"patternId":"design_EqualsNull","enabled":true},{"patternId":"basic_OverrideBothEqualsAndHashcode","enabled":true},{"patternId":"design_InstantiationToGetClass","enabled":true},{"patternId":"basic_BooleanInstantiation","enabled":true},{"patternId":"strings_AvoidStringBufferField","enabled":true},{"patternId":"basic_ReturnFromFinallyBlock","enabled":true},{"patternId":"empty_EmptyTryBlock","enabled":true},{"patternId":"naming_SuspiciousEqualsMethodName","enabled":true},{"patternId":"basic_ExtendsObject","enabled":true},{"patternId":"strings_UselessStringValueOf","enabled":true},{"patternId":"design_UnsynchronizedStaticDateFormatter","enabled":true},{"patternId":"design_UseCollectionIsEmpty","enabled":true},{"patternId":"controversial_AvoidFinalLocalVariable","enabled":true},{"patternId":"strictexception_AvoidThrowingNullPointerException","enabled":true},{"patternId":"design_AvoidProtectedMethodInFinalClassNotExtending","enabled":true},{"patternId":"optimizations_PrematureDeclaration","enabled":true},{"patternId":"empty_EmptySwitchStatements","enabled":true},{"patternId":"basic_MisplacedNullCheck","enabled":true},{"patternId":"optimizations_UseStringBufferForStringAppends","enabled":true},{"patternId":"strings_StringToString","enabled":true},{"patternId":"naming_MethodWithSameNameAsEnclosingClass","enabled":true},{"patternId":"migrating_ReplaceVectorWithList","enabled":true},{"patternId":"imports_UnusedImports","enabled":true},{"patternId":"unnecessary_UnnecessaryFinalModifier","enabled":true},{"patternId":"basic_AvoidMultipleUnaryOperators","enabled":true},{"patternId":"junit_SimplifyBooleanAssertion","enabled":true},{"patternId":"unnecessary_UselessParentheses","enabled":true},{"patternId":"design_IdempotentOperations","enabled":true},{"patternId":"braces_IfStmtsMustUseBraces","enabled":true},{"patternId":"strings_UseIndexOfChar","enabled":true},{"patternId":"naming_NoPackage","enabled":true},{"patternId":"finalizers_FinalizeDoesNotCallSuperFinalize","enabled":true},{"patternId":"design_UseVarargs","enabled":true},{"patternId":"unusedcode_UnusedFormalParameter","enabled":true},{"patternId":"design_ReturnEmptyArrayRatherThanNull","enabled":true},{"patternId":"junit_UseAssertNullInsteadOfAssertTrue","enabled":true},{"patternId":"design_UseUtilityClass","enabled":true},{"patternId":"design_AvoidDeeplyNestedIfStmts","enabled":true},{"patternId":"empty_EmptyStatementNotInLoop","enabled":true},{"patternId":"junit_UseAssertSameInsteadOfAssertTrue","enabled":true},{"patternId":"braces_ForLoopsMustUseBraces","enabled":true},{"patternId":"controversial_DoNotCallGarbageCollectionExplicitly","enabled":true},{"patternId":"naming_GenericsNaming","enabled":true},{"patternId":"strings_UseEqualsToCompareStrings","enabled":true},{"patternId":"optimizations_AvoidArrayLoops","enabled":true},{"patternId":"empty_EmptyStaticInitializer","enabled":true},{"patternId":"design_UncommentedEmptyConstructor","enabled":true},{"patternId":"empty_EmptyStatementBlock","enabled":true},{"patternId":"basic_CollapsibleIfStatements","enabled":true},{"patternId":"design_FinalFieldCouldBeStatic","enabled":true},{"patternId":"logging-java_MoreThanOneLogger","enabled":true},{"patternId":"codesize_ExcessiveClassLength","enabled":true},{"patternId":"design_ImmutableField","enabled":true},{"patternId":"controversial_OneDeclarationPerLine","enabled":true},{"patternId":"empty_EmptyWhileStmt","enabled":true},{"patternId":"unnecessary_UnnecessaryReturn","enabled":true},{"patternId":"strings_InefficientEmptyStringCheck","enabled":true},{"patternId":"design_UseNotifyAllInsteadOfNotify","enabled":true},{"patternId":"strictexception_DoNotThrowExceptionInFinally","enabled":true},{"patternId":"junit_UseAssertEqualsInsteadOfAssertTrue","enabled":true},{"patternId":"typeresolution_CloneMethodMustImplementCloneable","enabled":true},{"patternId":"codesize_NPathComplexity","enabled":true},{"patternId":"imports_DontImportJavaLang","enabled":true},{"patternId":"empty_EmptySynchronizedBlock","enabled":true},{"patternId":"migrating_JUnit4TestShouldUseAfterAnnotation","enabled":true},{"patternId":"design_AvoidConstantsInterface","enabled":true},{"patternId":"unnecessary_UselessOperationOnImmutable","enabled":true},{"patternId":"design_PositionLiteralsFirstInComparisons","enabled":true},{"patternId":"migrating_ByteInstantiation","enabled":true},{"patternId":"junit_JUnitSpelling","enabled":true},{"patternId":"finalizers_EmptyFinalizer","enabled":true},{"patternId":"design_NonCaseLabelInSwitchStatement","enabled":true},{"patternId":"android_DoNotHardCodeSDCard","enabled":true},{"patternId":"design_LogicInversion","enabled":true},{"patternId":"unusedcode_UnusedPrivateMethod","enabled":true},{"patternId":"naming_AvoidDollarSigns","enabled":true},{"patternId":"finalizers_FinalizeShouldBeProtected","enabled":true},{"patternId":"clone_ProperCloneImplementation","enabled":true},{"patternId":"basic_CheckResultSet","enabled":true},{"patternId":"controversial_AvoidPrefixingMethodParameters","enabled":true},{"patternId":"migrating_JUnit4SuitesShouldUseSuiteAnnotation","enabled":true},{"patternId":"empty_EmptyIfStmt","enabled":true},{"patternId":"basic_DontCallThreadRun","enabled":true},{"patternId":"junit_JUnitStaticSuite","enabled":true},{"patternId":"optimizations_UseArraysAsList","enabled":true},{"patternId":"design_MissingStaticMethodInNonInstantiatableClass","enabled":true},{"patternId":"unusedcode_UnusedModifier","enabled":true},{"patternId":"Style_MethodName","enabled":true},{"patternId":"Metrics_CyclomaticComplexity","enabled":true},{"patternId":"Lint_DuplicateMethods","enabled":true},{"patternId":"Style_Lambda","enabled":true},{"patternId":"Lint_UselessSetterCall","enabled":true},{"patternId":"Style_VariableName","enabled":true},{"patternId":"Lint_AmbiguousOperator","enabled":true},{"patternId":"Style_LeadingCommentSpace","enabled":true},{"patternId":"Style_CaseEquality","enabled":true},{"patternId":"Lint_StringConversionInInterpolation","enabled":true},{"patternId":"Performance_ReverseEach","enabled":true},{"patternId":"Lint_LiteralInCondition","enabled":true},{"patternId":"Performance_Sample","enabled":true},{"patternId":"Style_NonNilCheck","enabled":true},{"patternId":"Lint_RescueException","enabled":true},{"patternId":"Lint_UselessElseWithoutRescue","enabled":true},{"patternId":"Style_ConstantName","enabled":true},{"patternId":"Lint_LiteralInInterpolation","enabled":true},{"patternId":"Lint_NestedMethodDefinition","enabled":true},{"patternId":"Style_DoubleNegation","enabled":true},{"patternId":"Lint_SpaceBeforeFirstArg","enabled":true},{"patternId":"Lint_Debugger","enabled":true},{"patternId":"Style_ClassVars","enabled":true},{"patternId":"Lint_EmptyEnsure","enabled":true},{"patternId":"Style_MultilineBlockLayout","enabled":true},{"patternId":"Lint_UnusedBlockArgument","enabled":true},{"patternId":"Lint_UselessAccessModifier","enabled":true},{"patternId":"Performance_Size","enabled":true},{"patternId":"Lint_EachWithObjectArgument","enabled":true},{"patternId":"Style_Alias","enabled":true},{"patternId":"Lint_Loop","enabled":true},{"patternId":"Style_NegatedWhile","enabled":true},{"patternId":"Style_ColonMethodCall","enabled":true},{"patternId":"Lint_AmbiguousRegexpLiteral","enabled":true},{"patternId":"Lint_UnusedMethodArgument","enabled":true},{"patternId":"Style_MultilineIfThen","enabled":true},{"patternId":"Lint_EnsureReturn","enabled":true},{"patternId":"Style_NegatedIf","enabled":true},{"patternId":"Lint_Eval","enabled":true},{"patternId":"Style_NilComparison","enabled":true},{"patternId":"Style_ArrayJoin","enabled":true},{"patternId":"Lint_ConditionPosition","enabled":true},{"patternId":"Lint_UnreachableCode","enabled":true},{"patternId":"Performance_Count","enabled":true},{"patternId":"Lint_EmptyInterpolation","enabled":true},{"patternId":"Style_LambdaCall","enabled":true},{"patternId":"Lint_HandleExceptions","enabled":true},{"patternId":"Lint_ShadowingOuterLocalVariable","enabled":true},{"patternId":"Lint_EndAlignment","enabled":true},{"patternId":"Style_MultilineTernaryOperator","enabled":true},{"patternId":"Style_AutoResourceCleanup","enabled":true},{"patternId":"Lint_ElseLayout","enabled":true},{"patternId":"Style_NestedTernaryOperator","enabled":true},{"patternId":"Style_OneLineConditional","enabled":true},{"patternId":"Style_EmptyElse","enabled":true},{"patternId":"Lint_UselessComparison","enabled":true},{"patternId":"Metrics_PerceivedComplexity","enabled":true},{"patternId":"Style_InfiniteLoop","enabled":true},{"patternId":"Rails_Date","enabled":true},{"patternId":"Style_EvenOdd","enabled":true},{"patternId":"Style_IndentationConsistency","enabled":true},{"patternId":"Style_ModuleFunction","enabled":true},{"patternId":"Lint_UselessAssignment","enabled":true},{"patternId":"Style_EachWithObject","enabled":true},{"patternId":"Performance_Detect","enabled":true},{"patternId":"duplicate_key","enabled":true},{"patternId":"no_interpolation_in_single_quotes","enabled":true},{"patternId":"no_backticks","enabled":true},{"patternId":"no_unnecessary_fat_arrows","enabled":true},{"patternId":"indentation","enabled":true},{"patternId":"ensure_comprehensions","enabled":true},{"patternId":"no_stand_alone_at","enabled":true},{"patternId":"cyclomatic_complexity","enabled":true},{"patternId":"Deserialize","enabled":true},{"patternId":"SymbolDoS","enabled":true},{"patternId":"SkipBeforeFilter","enabled":true},{"patternId":"SanitizeMethods","enabled":true},{"patternId":"SelectTag","enabled":true},{"patternId":"XMLDoS","enabled":true},{"patternId":"SimpleFormat","enabled":true},{"patternId":"Evaluation","enabled":true},{"patternId":"BasicAuth","enabled":true},{"patternId":"JRubyXML","enabled":true},{"patternId":"RenderInline","enabled":true},{"patternId":"YAMLParsing","enabled":true},{"patternId":"Redirect","enabled":true},{"patternId":"UnsafeReflection","enabled":true},{"patternId":"SSLVerify","enabled":true},{"patternId":"HeaderDoS","enabled":true},{"patternId":"TranslateBug","enabled":true},{"patternId":"Execute","enabled":true},{"patternId":"JSONParsing","enabled":true},{"patternId":"LinkTo","enabled":true},{"patternId":"FileDisclosure","enabled":true},{"patternId":"SafeBufferManipulation","enabled":true},{"patternId":"ModelAttributes","enabled":true},{"patternId":"ResponseSplitting","enabled":true},{"patternId":"DigestDoS","enabled":true},{"patternId":"Send","enabled":true},{"patternId":"MailTo","enabled":true},{"patternId":"SymbolDoSCVE","enabled":true},{"patternId":"StripTags","enabled":true},{"patternId":"MassAssignment","enabled":true},{"patternId":"RegexDoS","enabled":true},{"patternId":"SelectVulnerability","enabled":true},{"patternId":"FileAccess","enabled":true},{"patternId":"ContentTag","enabled":true},{"patternId":"SessionSettings","enabled":true},{"patternId":"FilterSkipping","enabled":true},{"patternId":"CreateWith","enabled":true},{"patternId":"JSONEncoding","enabled":true},{"patternId":"SQLCVEs","enabled":true},{"patternId":"ForgerySetting","enabled":true},{"patternId":"QuoteTableName","enabled":true},{"patternId":"I18nXSS","enabled":true},{"patternId":"WithoutProtection","enabled":true},{"patternId":"CrossSiteScripting","enabled":true},{"patternId":"SingleQuotes","enabled":true},{"patternId":"NestedAttributes","enabled":true},{"patternId":"DetailedExceptions","enabled":true},{"patternId":"LinkToHref","enabled":true},{"patternId":"RenderDoS","enabled":true},{"patternId":"ModelSerialize","enabled":true},{"patternId":"SQL","enabled":true},{"patternId":"Render","enabled":true},{"patternId":"UnscopedFind","enabled":true},{"patternId":"ValidationRegex","enabled":true},{"patternId":"EscapeFunction","enabled":true},{"patternId":"Custom_Scala_FieldNamesChecker","enabled":true},{"patternId":"Custom_Scala_ObjDeserialization","enabled":true},{"patternId":"Custom_Scala_RSAPadding","enabled":true},{"patternId":"ESLint_no-extra-boolean-cast","enabled":true},{"patternId":"ESLint_no-iterator","enabled":true},{"patternId":"ESLint_no-invalid-regexp","enabled":true},{"patternId":"ESLint_no-obj-calls","enabled":true},{"patternId":"ESLint_no-sparse-arrays","enabled":true},{"patternId":"ESLint_no-unreachable","enabled":true},{"patternId":"ESLint_no-dupe-keys","enabled":true},{"patternId":"ESLint_no-multi-str","enabled":true},{"patternId":"ESLint_no-extend-native","enabled":true},{"patternId":"ESLint_guard-for-in","enabled":true},{"patternId":"ESLint_no-func-assign","enabled":true},{"patternId":"ESLint_no-extra-semi","enabled":true},{"patternId":"ESLint_camelcase","enabled":true},{"patternId":"ESLint_no-mixed-spaces-and-tabs","enabled":true},{"patternId":"ESLint_no-undef","enabled":true},{"patternId":"ESLint_semi","enabled":true},{"patternId":"ESLint_no-empty-character-class","enabled":true},{"patternId":"ESLint_complexity","enabled":true},{"patternId":"ESLint_no-dupe-class-members","enabled":true},{"patternId":"ESLint_no-debugger","enabled":true},{"patternId":"ESLint_block-scoped-var","enabled":true},{"patternId":"ESLint_no-loop-func","enabled":true},{"patternId":"ESLint_no-use-before-define","enabled":true},{"patternId":"ESLint_no-console","enabled":true},{"patternId":"ESLint_require-yield","enabled":true},{"patternId":"ESLint_no-redeclare","enabled":true},{"patternId":"ESLint_no-undefined","enabled":true},{"patternId":"ESLint_use-isnan","enabled":true},{"patternId":"ESLint_no-control-regex","enabled":true},{"patternId":"ESLint_no-const-assign","enabled":true},{"patternId":"ESLint_no-new","enabled":true},{"patternId":"ESLint_new-cap","enabled":true},{"patternId":"ESLint_no-irregular-whitespace","enabled":true},{"patternId":"ESLint_object-shorthand","enabled":true},{"patternId":"ESLint_no-ex-assign","enabled":true},{"patternId":"ESLint_wrap-iife","enabled":true},{"patternId":"ESLint_arrow-parens","enabled":true},{"patternId":"ESLint_no-constant-condition","enabled":true},{"patternId":"ESLint_no-octal","enabled":true},{"patternId":"ESLint_no-dupe-args","enabled":true},{"patternId":"ESLint_quotes","enabled":true},{"patternId":"ESLint_no-fallthrough","enabled":true},{"patternId":"ESLint_no-delete-var","enabled":true},{"patternId":"ESLint_no-caller","enabled":true},{"patternId":"ESLint_no-cond-assign","enabled":true},{"patternId":"ESLint_no-this-before-super","enabled":true},{"patternId":"ESLint_no-negated-in-lhs","enabled":true},{"patternId":"ESLint_no-inner-declarations","enabled":true},{"patternId":"ESLint_eqeqeq","enabled":true},{"patternId":"ESLint_curly","enabled":true},{"patternId":"ESLint_arrow-spacing","enabled":true},{"patternId":"ESLint_no-empty","enabled":true},{"patternId":"ESLint_no-unused-vars","enabled":true},{"patternId":"ESLint_generator-star-spacing","enabled":true},{"patternId":"ESLint_no-duplicate-case","enabled":true},{"patternId":"ESLint_valid-typeof","enabled":true},{"patternId":"ESLint_no-regex-spaces","enabled":true},{"patternId":"ESLint_no-class-assign","enabled":true},{"patternId":"PyLint_W0221","enabled":true},{"patternId":"PyLint_E0117","enabled":true},{"patternId":"PyLint_E0001","enabled":true},{"patternId":"PyLint_E0241","enabled":true},{"patternId":"PyLint_W0404","enabled":true},{"patternId":"PyLint_E0704","enabled":true},{"patternId":"PyLint_E0703","enabled":true},{"patternId":"PyLint_E0302","enabled":true},{"patternId":"PyLint_W1301","enabled":true},{"patternId":"PyLint_R0201","enabled":true},{"patternId":"PyLint_E0113","enabled":true},{"patternId":"PyLint_W0410","enabled":true},{"patternId":"PyLint_C0123","enabled":true},{"patternId":"PyLint_E0115","enabled":true},{"patternId":"PyLint_E0114","enabled":true},{"patternId":"PyLint_E1126","enabled":true},{"patternId":"PyLint_W0702","enabled":true},{"patternId":"PyLint_W1303","enabled":true},{"patternId":"PyLint_W0622","enabled":true},{"patternId":"PyLint_W0222","enabled":true},{"patternId":"PyLint_W0233","enabled":true},{"patternId":"PyLint_W1305","enabled":true},{"patternId":"PyLint_E1127","enabled":true},{"patternId":"PyLint_E0112","enabled":true},{"patternId":"PyLint_W0611","enabled":true},{"patternId":"PyLint_W0601","enabled":true},{"patternId":"PyLint_W1300","enabled":true},{"patternId":"PyLint_W0124","enabled":true},{"patternId":"PyLint_R0203","enabled":true},{"patternId":"PyLint_E0236","enabled":true},{"patternId":"PyLint_W0612","enabled":true},{"patternId":"PyLint_W0604","enabled":true},{"patternId":"PyLint_W0705","enabled":true},{"patternId":"PyLint_E0238","enabled":true},{"patternId":"PyLint_W0602","enabled":true},{"patternId":"PyLint_R0102","enabled":true},{"patternId":"PyLint_R0202","enabled":true},{"patternId":"PyLint_E0240","enabled":true},{"patternId":"PyLint_W0623","enabled":true},{"patternId":"PyLint_W0711","enabled":true},{"patternId":"PyLint_E0116","enabled":true},{"patternId":"PyLint_E0239","enabled":true},{"patternId":"PyLint_E1132","enabled":true},{"patternId":"PyLint_W1307","enabled":true},{"patternId":"PyLint_C0200","enabled":true},{"patternId":"PyLint_E0301","enabled":true},{"patternId":"PyLint_W1306","enabled":true},{"patternId":"PyLint_W1302","enabled":true},{"patternId":"PyLint_E0110","enabled":true},{"patternId":"PyLint_E1125","enabled":true}]} \ No newline at end of file From 637860c6b7548c5aa8c118b2580a83bc837ef270 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Tue, 29 Mar 2016 17:25:05 -0700 Subject: [PATCH 43/60] Update dependencies --- gcloud-java-datastore/pom.xml | 14 ++------------ .../gcloud/datastore/testing/LocalGcdHelper.java | 4 ++-- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/gcloud-java-datastore/pom.xml b/gcloud-java-datastore/pom.xml index e25144eec8a4..c640ab5a6d86 100644 --- a/gcloud-java-datastore/pom.xml +++ b/gcloud-java-datastore/pom.xml @@ -12,16 +12,6 @@ gcloud-java-pom 0.1.7-SNAPSHOT - - - sonatype-snapshots - sonatype-snapshots - https://oss.sonatype.org/content/repositories/snapshots/ - - true - - - gcloud-java-datastore @@ -34,12 +24,12 @@ com.google.cloud.datastore datastore-v1beta3-protos - 0.0.1-SNAPSHOT + 1.0.0-beta com.google.cloud.datastore datastore-v1beta3-proto-client - 0.0.1-SNAPSHOT + 1.0.0-beta ${project.groupId} diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/testing/LocalGcdHelper.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/testing/LocalGcdHelper.java index d5ce0efe0498..6f1a76b0a482 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/testing/LocalGcdHelper.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/testing/LocalGcdHelper.java @@ -78,10 +78,10 @@ public class LocalGcdHelper { public static final String DEFAULT_PROJECT_ID = "projectid1"; public static final int DEFAULT_PORT = 8080; private static final String GCD_VERSION = "v1beta3"; - private static final String GCD_BUILD = "0.0.1"; + private static final String GCD_BUILD = "1.0.0"; private static final String GCD_BASENAME = "gcd-" + GCD_VERSION + "-" + GCD_BUILD; private static final String GCD_FILENAME = GCD_BASENAME + ".zip"; - private static final String MD5_CHECKSUM = "496b16f32473d0de0c7a974bd0ee1461"; + private static final String MD5_CHECKSUM = "72156cc993835c57f72789519b85249b"; private static final URL GCD_URL; private static final String GCLOUD = "gcloud"; private static final Path INSTALLED_GCD_PATH; From 8db4b9b6f7b0c56c9b93c5a614a1c945b10336b6 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Wed, 30 Mar 2016 22:42:29 +0200 Subject: [PATCH 44/60] Minor refactoring for SelectorHelper - Rename selector method for lists to listSelector - Update selectors to accept List parameters - Add missing unit test for listSelector - Remove redundant extends Serializable from dns' option classes --- .../com/google/gcloud/bigquery/BigQuery.java | 23 ++++++----- .../java/com/google/gcloud/FieldSelector.java | 12 +++--- ...Test.java => FieldSelectorHelperTest.java} | 21 ++++++++-- .../main/java/com/google/gcloud/dns/Dns.java | 38 +++++++++---------- .../resourcemanager/ResourceManager.java | 8 ++-- .../com/google/gcloud/storage/Storage.java | 17 ++++----- 6 files changed, 65 insertions(+), 54 deletions(-) rename gcloud-java-core/src/test/java/com/google/gcloud/{SelectorHelperTest.java => FieldSelectorHelperTest.java} (72%) diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java index c23ee2844762..2d864383e81a 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java @@ -22,7 +22,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import com.google.gcloud.FieldSelector; -import com.google.gcloud.FieldSelector.SelectorHelper; +import com.google.gcloud.FieldSelector.Helper; import com.google.gcloud.Page; import com.google.gcloud.Service; import com.google.gcloud.bigquery.spi.BigQueryRpc; @@ -55,8 +55,8 @@ enum DatasetField implements FieldSelector { LOCATION("location"), SELF_LINK("selfLink"); - static final List REQUIRED_FIELDS = - ImmutableList.of(DATASET_REFERENCE); + static final List REQUIRED_FIELDS = + ImmutableList.of(DATASET_REFERENCE); private final String selector; @@ -95,8 +95,8 @@ enum TableField implements FieldSelector { TYPE("type"), VIEW("view"); - static final List REQUIRED_FIELDS = - ImmutableList.of(TABLE_REFERENCE, TYPE); + static final List REQUIRED_FIELDS = + ImmutableList.of(TABLE_REFERENCE, TYPE); private final String selector; @@ -126,8 +126,8 @@ enum JobField implements FieldSelector { STATUS("status"), USER_EMAIL("user_email"); - static final List REQUIRED_FIELDS = - ImmutableList.of(JOB_REFERENCE, CONFIGURATION); + static final List REQUIRED_FIELDS = + ImmutableList.of(JOB_REFERENCE, CONFIGURATION); private final String selector; @@ -193,7 +193,7 @@ private DatasetOption(BigQueryRpc.Option option, Object value) { */ public static DatasetOption fields(DatasetField... fields) { return new DatasetOption(BigQueryRpc.Option.FIELDS, - SelectorHelper.selector(DatasetField.REQUIRED_FIELDS, fields)); + Helper.selector(DatasetField.REQUIRED_FIELDS, fields)); } } @@ -263,7 +263,7 @@ private TableOption(BigQueryRpc.Option option, Object value) { */ public static TableOption fields(TableField... fields) { return new TableOption(BigQueryRpc.Option.FIELDS, - SelectorHelper.selector(TableField.REQUIRED_FIELDS, fields)); + Helper.selector(TableField.REQUIRED_FIELDS, fields)); } } @@ -361,8 +361,7 @@ public static JobListOption pageToken(String pageToken) { */ public static JobListOption fields(JobField... fields) { return new JobListOption(BigQueryRpc.Option.FIELDS, - SelectorHelper.selector("jobs", JobField.REQUIRED_FIELDS, fields, "state", - "errorResult")); + Helper.listSelector("jobs", JobField.REQUIRED_FIELDS, fields, "state", "errorResult")); } } @@ -386,7 +385,7 @@ private JobOption(BigQueryRpc.Option option, Object value) { */ public static JobOption fields(JobField... fields) { return new JobOption(BigQueryRpc.Option.FIELDS, - SelectorHelper.selector(JobField.REQUIRED_FIELDS, fields)); + Helper.selector(JobField.REQUIRED_FIELDS, fields)); } } diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java b/gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java index 343f67e3381b..be6ab73d00bf 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java @@ -41,9 +41,9 @@ public interface FieldSelector { * A helper class used to build composite selectors given a number of fields. This class is not * supposed to be used directly by users. */ - class SelectorHelper { + class Helper { - private SelectorHelper() {} + private Helper() {} private static final Function FIELD_TO_STRING_FUNCTION = new Function() { @@ -53,7 +53,7 @@ public String apply(FieldSelector fieldSelector) { } }; - private static String selector(List required, FieldSelector[] others, + private static String selector(List required, FieldSelector[] others, String... extraResourceFields) { Set fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length); fieldStrings.addAll(Lists.transform(required, FIELD_TO_STRING_FUNCTION)); @@ -67,7 +67,7 @@ private static String selector(List required, FieldSelector[] oth * method can be used for field selection in API calls that return a single resource. This * method is not supposed to be used directly by users. */ - public static String selector(List required, FieldSelector... others) { + public static String selector(List required, FieldSelector... others) { return selector(required, others, new String[]{}); } @@ -76,7 +76,7 @@ public static String selector(List required, FieldSelector... oth * selector returned by this method can be used for field selection in API calls that return a * list of resources. This method is not supposed to be used directly by users. */ - public static String selector(String containerName, List required, + public static String listSelector(String containerName, List required, FieldSelector... others) { return "nextPageToken," + containerName + '(' + selector(required, others) + ')'; } @@ -87,7 +87,7 @@ public static String selector(String containerName, List required * string selector returned by this method can be used for field selection in API calls that * return a list of resources. This method is not supposed to be used directly by users. */ - public static String selector(String containerName, List required, + public static String listSelector(String containerName, List required, FieldSelector[] others, String... extraResourceFields) { return "nextPageToken," + containerName + '(' + selector(required, others, extraResourceFields) + ')'; diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/SelectorHelperTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/FieldSelectorHelperTest.java similarity index 72% rename from gcloud-java-core/src/test/java/com/google/gcloud/SelectorHelperTest.java rename to gcloud-java-core/src/test/java/com/google/gcloud/FieldSelectorHelperTest.java index 2279205539cc..9871c942180e 100644 --- a/gcloud-java-core/src/test/java/com/google/gcloud/SelectorHelperTest.java +++ b/gcloud-java-core/src/test/java/com/google/gcloud/FieldSelectorHelperTest.java @@ -20,13 +20,13 @@ import static org.junit.Assert.assertTrue; import com.google.common.collect.ImmutableList; -import com.google.gcloud.FieldSelector.SelectorHelper; +import com.google.gcloud.FieldSelector.Helper; import org.junit.Test; import java.util.List; -public class SelectorHelperTest { +public class FieldSelectorHelperTest { private static final FieldSelector FIELD1 = new FieldSelector() { @Override @@ -51,7 +51,7 @@ public String selector() { @Test public void testSelector() { - String selector = SelectorHelper.selector(REQUIRED_FIELDS, FIELD3); + String selector = Helper.selector(REQUIRED_FIELDS, FIELD3); assertTrue(selector.contains("field1")); assertTrue(selector.contains("field2")); assertTrue(selector.contains("field3")); @@ -60,7 +60,7 @@ public void testSelector() { @Test public void testListSelector() { - String selector = SelectorHelper.selector(CONTAINER, REQUIRED_FIELDS, FIELD3); + String selector = Helper.listSelector(CONTAINER, REQUIRED_FIELDS, FIELD3); assertTrue(selector.startsWith("nextPageToken,container(")); assertTrue(selector.contains("field1")); assertTrue(selector.contains("field2")); @@ -68,4 +68,17 @@ public void testListSelector() { assertTrue(selector.endsWith(")")); assertEquals(45, selector.length()); } + + @Test + public void testListSelectorWithExtraFields() { + String selector = Helper.listSelector(CONTAINER, REQUIRED_FIELDS, + new FieldSelector[]{FIELD3}, "field4"); + assertTrue(selector.startsWith("nextPageToken,container(")); + assertTrue(selector.contains("field1")); + assertTrue(selector.contains("field2")); + assertTrue(selector.contains("field3")); + assertTrue(selector.contains("field4")); + assertTrue(selector.endsWith(")")); + assertEquals(52, selector.length()); + } } 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 c4b08e31d533..d95d11a97c19 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,7 +18,7 @@ import com.google.common.collect.ImmutableList; import com.google.gcloud.FieldSelector; -import com.google.gcloud.FieldSelector.SelectorHelper; +import com.google.gcloud.FieldSelector.Helper; import com.google.gcloud.Page; import com.google.gcloud.Service; import com.google.gcloud.dns.spi.DnsRpc; @@ -45,7 +45,7 @@ enum ProjectField implements FieldSelector { PROJECT_NUMBER("number"), QUOTA("quota"); - static final List REQUIRED_FIELDS = ImmutableList.of(PROJECT_ID); + static final List REQUIRED_FIELDS = ImmutableList.of(PROJECT_ID); private final String selector; @@ -75,7 +75,7 @@ enum ZoneField implements FieldSelector { NAME_SERVER_SET("nameServerSet"), NAME_SERVERS("nameServers"); - static final List REQUIRED_FIELDS = ImmutableList.of(NAME); + static final List REQUIRED_FIELDS = ImmutableList.of(NAME); private final String selector; @@ -102,7 +102,7 @@ enum RecordSetField implements FieldSelector { TTL("ttl"), TYPE("type"); - static final List REQUIRED_FIELDS = ImmutableList.of(NAME, TYPE); + static final List REQUIRED_FIELDS = ImmutableList.of(NAME, TYPE); private final String selector; @@ -120,8 +120,8 @@ public String selector() { * 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(String, ChangeRequestInfo, ChangeRequestOption...)} The ID is always - * returned even if not selected. + * {@link Dns#applyChangeRequest(String, ChangeRequestInfo, ChangeRequestOption...)} The ID is + * always returned even if not selected. */ enum ChangeRequestField implements FieldSelector { ID("id"), @@ -130,7 +130,7 @@ enum ChangeRequestField implements FieldSelector { ADDITIONS("additions"), DELETIONS("deletions"); - static final List REQUIRED_FIELDS = ImmutableList.of(ID); + static final List REQUIRED_FIELDS = ImmutableList.of(ID); private final String selector; @@ -158,7 +158,7 @@ public String selector() { /** * Class for specifying record set listing options. */ - class RecordSetListOption extends Option implements Serializable { + class RecordSetListOption extends Option { private static final long serialVersionUID = 1009627025381096098L; @@ -176,7 +176,7 @@ class RecordSetListOption extends Option implements Serializable { */ public static RecordSetListOption fields(RecordSetField... fields) { return new RecordSetListOption(DnsRpc.Option.FIELDS, - SelectorHelper.selector("rrsets", RecordSetField.REQUIRED_FIELDS, fields)); + Helper.listSelector("rrsets", RecordSetField.REQUIRED_FIELDS, fields)); } /** @@ -218,7 +218,7 @@ public static RecordSetListOption type(RecordSet.Type type) { /** * Class for specifying zone field options. */ - class ZoneOption extends Option implements Serializable { + class ZoneOption extends Option { private static final long serialVersionUID = -8065564464895945037L; @@ -235,14 +235,14 @@ class ZoneOption extends Option implements Serializable { */ public static ZoneOption fields(ZoneField... fields) { return new ZoneOption(DnsRpc.Option.FIELDS, - SelectorHelper.selector(ZoneField.REQUIRED_FIELDS, fields)); + Helper.selector(ZoneField.REQUIRED_FIELDS, fields)); } } /** * Class for specifying zone listing options. */ - class ZoneListOption extends Option implements Serializable { + class ZoneListOption extends Option { private static final long serialVersionUID = -2830645032124504717L; @@ -259,7 +259,7 @@ class ZoneListOption extends Option implements Serializable { */ public static ZoneListOption fields(ZoneField... fields) { return new ZoneListOption(DnsRpc.Option.FIELDS, - SelectorHelper.selector("managedZones", ZoneField.REQUIRED_FIELDS, fields)); + Helper.listSelector("managedZones", ZoneField.REQUIRED_FIELDS, fields)); } /** @@ -293,7 +293,7 @@ public static ZoneListOption pageSize(int pageSize) { /** * Class for specifying project options. */ - class ProjectOption extends Option implements Serializable { + class ProjectOption extends Option { private static final long serialVersionUID = 6817937338218847748L; @@ -311,14 +311,14 @@ class ProjectOption extends Option implements Serializable { */ public static ProjectOption fields(ProjectField... fields) { return new ProjectOption(DnsRpc.Option.FIELDS, - SelectorHelper.selector(ProjectField.REQUIRED_FIELDS, fields)); + Helper.selector(ProjectField.REQUIRED_FIELDS, fields)); } } /** * Class for specifying change request field options. */ - class ChangeRequestOption extends Option implements Serializable { + class ChangeRequestOption extends Option { private static final long serialVersionUID = 1067273695061077782L; @@ -337,14 +337,14 @@ class ChangeRequestOption extends Option implements Serializable { */ public static ChangeRequestOption fields(ChangeRequestField... fields) { return new ChangeRequestOption(DnsRpc.Option.FIELDS, - SelectorHelper.selector(ChangeRequestField.REQUIRED_FIELDS, fields)); + Helper.selector(ChangeRequestField.REQUIRED_FIELDS, fields)); } } /** * Class for specifying change request listing options. */ - class ChangeRequestListOption extends Option implements Serializable { + class ChangeRequestListOption extends Option { private static final long serialVersionUID = -900209143895376089L; @@ -363,7 +363,7 @@ class ChangeRequestListOption extends Option implements Serializable { */ public static ChangeRequestListOption fields(ChangeRequestField... fields) { return new ChangeRequestListOption(DnsRpc.Option.FIELDS, - SelectorHelper.selector("changes", ChangeRequestField.REQUIRED_FIELDS, fields)); + Helper.listSelector("changes", ChangeRequestField.REQUIRED_FIELDS, fields)); } /** diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java index e000ca69c359..92494a5152fe 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java @@ -18,7 +18,7 @@ import com.google.common.collect.ImmutableList; import com.google.gcloud.FieldSelector; -import com.google.gcloud.FieldSelector.SelectorHelper; +import com.google.gcloud.FieldSelector.Helper; import com.google.gcloud.IamPolicy; import com.google.gcloud.Page; import com.google.gcloud.Service; @@ -50,7 +50,7 @@ enum ProjectField implements FieldSelector { STATE("lifecycleState"), CREATE_TIME("createTime"); - static final List REQUIRED_FIELDS = ImmutableList.of(PROJECT_ID); + static final List REQUIRED_FIELDS = ImmutableList.of(PROJECT_ID); private final String selector; @@ -85,7 +85,7 @@ private ProjectGetOption(ResourceManagerRpc.Option option, Object value) { */ public static ProjectGetOption fields(ProjectField... fields) { return new ProjectGetOption(ResourceManagerRpc.Option.FIELDS, - SelectorHelper.selector(ProjectField.REQUIRED_FIELDS, fields)); + Helper.selector(ProjectField.REQUIRED_FIELDS, fields)); } } @@ -159,7 +159,7 @@ public static ProjectListOption pageSize(int pageSize) { */ public static ProjectListOption fields(ProjectField... fields) { return new ProjectListOption(ResourceManagerRpc.Option.FIELDS, - SelectorHelper.selector("projects", ProjectField.REQUIRED_FIELDS, fields)); + Helper.listSelector("projects", ProjectField.REQUIRED_FIELDS, fields)); } } diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java index 87d82aad686b..72d89348f5fa 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java @@ -25,7 +25,7 @@ import com.google.gcloud.AuthCredentials; import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials; import com.google.gcloud.FieldSelector; -import com.google.gcloud.FieldSelector.SelectorHelper; +import com.google.gcloud.FieldSelector.Helper; import com.google.gcloud.Page; import com.google.gcloud.ReadChannel; import com.google.gcloud.Service; @@ -89,7 +89,7 @@ enum BucketField implements FieldSelector { STORAGE_CLASS("storageClass"), ETAG("etag"); - static final List REQUIRED_FIELDS = ImmutableList.of(NAME); + static final List REQUIRED_FIELDS = ImmutableList.of(NAME); private final String selector; @@ -129,8 +129,7 @@ enum BlobField implements FieldSelector { TIME_DELETED("timeDeleted"), UPDATED("updated"); - static final List REQUIRED_FIELDS = - ImmutableList.of(BUCKET, NAME); + static final List REQUIRED_FIELDS = ImmutableList.of(BUCKET, NAME); private final String selector; @@ -257,7 +256,7 @@ public static BucketGetOption metagenerationNotMatch(long metageneration) { */ public static BucketGetOption fields(BucketField... fields) { return new BucketGetOption(StorageRpc.Option.FIELDS, - SelectorHelper.selector(BucketField.REQUIRED_FIELDS, fields)); + Helper.selector(BucketField.REQUIRED_FIELDS, fields)); } } @@ -598,7 +597,7 @@ public static BlobGetOption metagenerationNotMatch(long metageneration) { */ public static BlobGetOption fields(BlobField... fields) { return new BlobGetOption(StorageRpc.Option.FIELDS, - SelectorHelper.selector(BlobField.REQUIRED_FIELDS, fields)); + Helper.selector(BlobField.REQUIRED_FIELDS, fields)); } } @@ -643,7 +642,7 @@ public static BucketListOption prefix(String prefix) { */ public static BucketListOption fields(BucketField... fields) { return new BucketListOption(StorageRpc.Option.FIELDS, - SelectorHelper.selector("items", BucketField.REQUIRED_FIELDS, fields)); + Helper.listSelector("items", BucketField.REQUIRED_FIELDS, fields)); } } @@ -711,7 +710,7 @@ public static BlobListOption versions(boolean versions) { */ public static BlobListOption fields(BlobField... fields) { return new BlobListOption(StorageRpc.Option.FIELDS, - SelectorHelper.selector("items", BlobField.REQUIRED_FIELDS, fields)); + Helper.listSelector("items", BlobField.REQUIRED_FIELDS, fields)); } } @@ -1529,7 +1528,7 @@ public static Builder builder() { * are merged with metadata in the provided {@code BlobInfo} objects. To replace metadata instead * you first have to unset them. Unsetting metadata can be done by setting the provided * {@code BlobInfo} objects metadata to {@code null}. See - * {@link #update(com.google.gcloud.storage.BlobInfo)} for a code example. + * {@link #update(BlobInfo)} for a code example. * * @param blobInfos blobs to update * @return an immutable list of {@code Blob} objects. If a blob does not exist or access to it From 379a0a89e116413827339169361879480fec703b Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Fri, 1 Apr 2016 00:28:12 +0200 Subject: [PATCH 45/60] Get projectId from gcloud active config --- .../java/com/google/gcloud/ServiceOptions.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java index d45069434a26..1c5289bbcda9 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java @@ -27,6 +27,7 @@ import com.google.api.client.http.javanet.NetHttpTransport; import com.google.auth.http.HttpCredentialsAdapter; import com.google.common.collect.Iterables; +import com.google.common.io.Files; import com.google.gcloud.spi.ServiceRpcFactory; import java.io.BufferedReader; @@ -42,6 +43,7 @@ import java.lang.reflect.Method; import java.net.HttpURLConnection; import java.net.URL; +import java.nio.charset.Charset; import java.util.Enumeration; import java.util.Locale; import java.util.Objects; @@ -381,6 +383,18 @@ protected String defaultProject() { return projectId != null ? projectId : googleCloudProjectId(); } + private static String activeGoogleCloudConfig(File configDir) { + String activeGoogleCloudConfig = null; + try { + activeGoogleCloudConfig = + Files.readFirstLine(new File(configDir, "active_config"), Charset.defaultCharset()); + } catch (IOException ex) { + // ignore + } + // if reading active_config failed or the file is empty we try default + return firstNonNull(activeGoogleCloudConfig, "default"); + } + protected static String googleCloudProjectId() { File configDir; if (System.getenv().containsKey("CLOUDSDK_CONFIG")) { @@ -390,9 +404,10 @@ protected static String googleCloudProjectId() { } else { configDir = new File(System.getProperty("user.home"), ".config/gcloud"); } + String activeConfig = activeGoogleCloudConfig(configDir); FileReader fileReader = null; try { - fileReader = new FileReader(new File(configDir, "configurations/config_default")); + fileReader = new FileReader(new File(configDir, "configurations/config_" + activeConfig)); } catch (FileNotFoundException newConfigFileNotFoundEx) { try { fileReader = new FileReader(new File(configDir, "properties")); From 3324808be960ddd0b6dbbe696a7e373fe67cbfa8 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Fri, 1 Apr 2016 16:07:20 +0200 Subject: [PATCH 46/60] Rename id to generatedId for BigQuery resources --- .../com/google/gcloud/bigquery/BigQuery.java | 19 ++++++------ .../com/google/gcloud/bigquery/Dataset.java | 4 +-- .../google/gcloud/bigquery/DatasetInfo.java | 29 +++++++++---------- .../java/com/google/gcloud/bigquery/Job.java | 4 +-- .../com/google/gcloud/bigquery/JobInfo.java | 28 +++++++++--------- .../com/google/gcloud/bigquery/Table.java | 4 +-- .../com/google/gcloud/bigquery/TableInfo.java | 28 +++++++++--------- .../gcloud/bigquery/DatasetInfoTest.java | 10 +++---- .../google/gcloud/bigquery/DatasetTest.java | 8 ++--- .../google/gcloud/bigquery/JobInfoTest.java | 20 ++++++------- .../com/google/gcloud/bigquery/JobTest.java | 10 +++---- .../gcloud/bigquery/SerializationTest.java | 10 +++---- .../google/gcloud/bigquery/TableInfoTest.java | 16 +++++----- .../com/google/gcloud/bigquery/TableTest.java | 8 ++--- .../gcloud/bigquery/it/ITBigQueryTest.java | 10 +++---- 15 files changed, 104 insertions(+), 104 deletions(-) diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java index 2d864383e81a..d40cc8145feb 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java @@ -471,9 +471,10 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) { Dataset getDataset(DatasetId datasetId, DatasetOption... options); /** - * Lists the project's datasets. This method returns partial information on each dataset - * ({@link Dataset#datasetId()}, {@link Dataset#friendlyName()} and {@link Dataset#id()}). To get - * complete information use either {@link #getDataset(String, DatasetOption...)} or + * Lists the project's datasets. This method returns partial information on each dataset: + * ({@link Dataset#datasetId()}, {@link Dataset#friendlyName()} and + * {@link Dataset#generatedId()}). To get complete information use either + * {@link #getDataset(String, DatasetOption...)} or * {@link #getDataset(DatasetId, DatasetOption...)}. * * @throws BigQueryException upon failure @@ -541,9 +542,9 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) { Table getTable(TableId tableId, TableOption... options); /** - * Lists the tables in the dataset. This method returns partial information on each table - * ({@link Table#tableId()}, {@link Table#friendlyName()}, {@link Table#id()} and type, which - * is part of {@link Table#definition()}). To get complete information use either + * Lists the tables in the dataset. This method returns partial information on each table: + * ({@link Table#tableId()}, {@link Table#friendlyName()}, {@link Table#generatedId()} and type, + * which is part of {@link Table#definition()}). To get complete information use either * {@link #getTable(TableId, TableOption...)} or * {@link #getTable(String, String, TableOption...)}. * @@ -552,9 +553,9 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) { Page listTables(String datasetId, TableListOption... options); /** - * Lists the tables in the dataset. This method returns partial information on each table - * ({@link Table#tableId()}, {@link Table#friendlyName()}, {@link Table#id()} and type, which - * is part of {@link Table#definition()}). To get complete information use either + * Lists the tables in the dataset. This method returns partial information on each table: + * ({@link Table#tableId()}, {@link Table#friendlyName()}, {@link Table#generatedId()} and type, + * which is part of {@link Table#definition()}). To get complete information use either * {@link #getTable(TableId, TableOption...)} or * {@link #getTable(String, String, TableOption...)}. * diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Dataset.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Dataset.java index e17d3e82c4ef..215a0c662c3b 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Dataset.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Dataset.java @@ -103,8 +103,8 @@ public Builder friendlyName(String friendlyName) { } @Override - Builder id(String id) { - infoBuilder.id(id); + Builder generatedId(String generatedId) { + infoBuilder.generatedId(generatedId); return this; } diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/DatasetInfo.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/DatasetInfo.java index aa767b97631b..9f1d25f05925 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/DatasetInfo.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/DatasetInfo.java @@ -55,8 +55,7 @@ public Dataset apply(DatasetInfo datasetInfo) { return datasetInfo.toPb(); } }; - - private static final long serialVersionUID = -6615133444520365839L; + private static final long serialVersionUID = 8469473744160758489L; private final DatasetId datasetId; private final List acl; @@ -65,7 +64,7 @@ public Dataset apply(DatasetInfo datasetInfo) { private final String description; private final String etag; private final String friendlyName; - private final String id; + private final String generatedId; private final Long lastModified; private final String location; private final String selfLink; @@ -114,7 +113,7 @@ public abstract static class Builder { */ public abstract Builder friendlyName(String friendlyName); - abstract Builder id(String id); + abstract Builder generatedId(String generatedId); abstract Builder lastModified(Long lastModified); @@ -144,7 +143,7 @@ static final class BuilderImpl extends Builder { private String description; private String etag; private String friendlyName; - private String id; + private String generatedId; private Long lastModified; private String location; private String selfLink; @@ -159,7 +158,7 @@ static final class BuilderImpl extends Builder { this.description = datasetInfo.description; this.etag = datasetInfo.etag; this.friendlyName = datasetInfo.friendlyName; - this.id = datasetInfo.id; + this.generatedId = datasetInfo.generatedId; this.lastModified = datasetInfo.lastModified; this.location = datasetInfo.location; this.selfLink = datasetInfo.selfLink; @@ -182,7 +181,7 @@ public Acl apply(Dataset.Access accessPb) { this.description = datasetPb.getDescription(); this.etag = datasetPb.getEtag(); this.friendlyName = datasetPb.getFriendlyName(); - this.id = datasetPb.getId(); + this.generatedId = datasetPb.getId(); this.lastModified = datasetPb.getLastModifiedTime(); this.location = datasetPb.getLocation(); this.selfLink = datasetPb.getSelfLink(); @@ -232,8 +231,8 @@ public Builder friendlyName(String friendlyName) { } @Override - Builder id(String id) { - this.id = id; + Builder generatedId(String generatedId) { + this.generatedId = generatedId; return this; } @@ -269,7 +268,7 @@ public DatasetInfo build() { description = builder.description; etag = builder.etag; friendlyName = builder.friendlyName; - id = builder.id; + generatedId = builder.generatedId; lastModified = builder.lastModified; location = builder.location; selfLink = builder.selfLink; @@ -333,10 +332,10 @@ public String friendlyName() { } /** - * Returns an opaque id for the dataset. + * Returns the service-generated id for the dataset. */ - public String id() { - return id; + public String generatedId() { + return generatedId; } /** @@ -381,7 +380,7 @@ public String toString() { .add("description", description) .add("etag", etag) .add("friendlyName", friendlyName) - .add("id", id) + .add("generatedId", generatedId) .add("lastModified", lastModified) .add("location", location) .add("selfLink", selfLink) @@ -431,7 +430,7 @@ Dataset toPb() { datasetPb.setDescription(description); datasetPb.setEtag(etag); datasetPb.setFriendlyName(friendlyName); - datasetPb.setId(id); + datasetPb.setId(generatedId); datasetPb.setLastModifiedTime(lastModified); datasetPb.setLocation(location); datasetPb.setSelfLink(selfLink); diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Job.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Job.java index 1e63344a600d..586395a0f879 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Job.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Job.java @@ -63,8 +63,8 @@ Builder etag(String etag) { } @Override - Builder id(String id) { - infoBuilder.id(id); + Builder generatedId(String generatedId) { + infoBuilder.generatedId(generatedId); return this; } diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/JobInfo.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/JobInfo.java index 1adf7fabafc1..b15ba38f8912 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/JobInfo.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/JobInfo.java @@ -42,10 +42,10 @@ public JobInfo apply(Job pb) { } }; - private static final long serialVersionUID = -3272941007234620265L; + private static final long serialVersionUID = 2740548743267670124L; private final String etag; - private final String id; + private final String generatedId; private final JobId jobId; private final String selfLink; private final JobStatus status; @@ -95,7 +95,7 @@ public abstract static class Builder { abstract Builder etag(String etag); - abstract Builder id(String id); + abstract Builder generatedId(String generatedId); /** * Sets the job identity. @@ -128,7 +128,7 @@ public abstract static class Builder { static final class BuilderImpl extends Builder { private String etag; - private String id; + private String generatedId; private JobId jobId; private String selfLink; private JobStatus status; @@ -140,7 +140,7 @@ static final class BuilderImpl extends Builder { BuilderImpl(JobInfo jobInfo) { this.etag = jobInfo.etag; - this.id = jobInfo.id; + this.generatedId = jobInfo.generatedId; this.jobId = jobInfo.jobId; this.selfLink = jobInfo.selfLink; this.status = jobInfo.status; @@ -151,7 +151,7 @@ static final class BuilderImpl extends Builder { BuilderImpl(Job jobPb) { this.etag = jobPb.getEtag(); - this.id = jobPb.getId(); + this.generatedId = jobPb.getId(); if (jobPb.getJobReference() != null) { this.jobId = JobId.fromPb(jobPb.getJobReference()); } @@ -173,8 +173,8 @@ Builder etag(String etag) { } @Override - Builder id(String id) { - this.id = id; + Builder generatedId(String generatedId) { + this.generatedId = generatedId; return this; } @@ -223,7 +223,7 @@ public JobInfo build() { JobInfo(BuilderImpl builder) { this.jobId = builder.jobId; this.etag = builder.etag; - this.id = builder.id; + this.generatedId = builder.generatedId; this.selfLink = builder.selfLink; this.status = builder.status; this.statistics = builder.statistics; @@ -239,10 +239,10 @@ public String etag() { } /** - * Returns an opaque id for the job. + * Returns the service-generated id for the job. */ - public String id() { - return id; + public String generatedId() { + return generatedId; } /** @@ -306,7 +306,7 @@ public String toString() { .add("statistics", statistics) .add("userEmail", userEmail) .add("etag", etag) - .add("id", id) + .add("generatedId", generatedId) .add("selfLink", selfLink) .add("configuration", configuration) .toString(); @@ -331,7 +331,7 @@ JobInfo setProjectId(String projectId) { Job toPb() { Job jobPb = new Job(); jobPb.setEtag(etag); - jobPb.setId(id); + jobPb.setId(generatedId); jobPb.setSelfLink(selfLink); jobPb.setUserEmail(userEmail); if (jobId != null) { diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Table.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Table.java index 3f902d2ff242..b007771645df 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Table.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Table.java @@ -92,8 +92,8 @@ public Builder friendlyName(String friendlyName) { } @Override - Builder id(String id) { - infoBuilder.id(id); + Builder generatedId(String generatedId) { + infoBuilder.generatedId(generatedId); return this; } diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableInfo.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableInfo.java index de331350e978..938907e245e7 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableInfo.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableInfo.java @@ -52,10 +52,10 @@ public Table apply(TableInfo tableInfo) { } }; - private static final long serialVersionUID = -7679032506430816205L; + private static final long serialVersionUID = 609769795097719407L; private final String etag; - private final String id; + private final String generatedId; private final String selfLink; private final TableId tableId; private final String friendlyName; @@ -90,7 +90,7 @@ public abstract static class Builder { */ public abstract Builder friendlyName(String friendlyName); - abstract Builder id(String id); + abstract Builder generatedId(String generatedId); abstract Builder lastModifiedTime(Long lastModifiedTime); @@ -117,7 +117,7 @@ public abstract static class Builder { static class BuilderImpl extends Builder { private String etag; - private String id; + private String generatedId; private String selfLink; private TableId tableId; private String friendlyName; @@ -131,7 +131,7 @@ static class BuilderImpl extends Builder { BuilderImpl(TableInfo tableInfo) { this.etag = tableInfo.etag; - this.id = tableInfo.id; + this.generatedId = tableInfo.generatedId; this.selfLink = tableInfo.selfLink; this.tableId = tableInfo.tableId; this.friendlyName = tableInfo.friendlyName; @@ -152,7 +152,7 @@ static class BuilderImpl extends Builder { this.friendlyName = tablePb.getFriendlyName(); this.creationTime = tablePb.getCreationTime(); this.etag = tablePb.getEtag(); - this.id = tablePb.getId(); + this.generatedId = tablePb.getId(); this.selfLink = tablePb.getSelfLink(); this.definition = TableDefinition.fromPb(tablePb); } @@ -188,8 +188,8 @@ public Builder friendlyName(String friendlyName) { } @Override - Builder id(String id) { - this.id = id; + Builder generatedId(String generatedId) { + this.generatedId = generatedId; return this; } @@ -226,7 +226,7 @@ public TableInfo build() { TableInfo(BuilderImpl builder) { this.tableId = checkNotNull(builder.tableId); this.etag = builder.etag; - this.id = builder.id; + this.generatedId = builder.generatedId; this.selfLink = builder.selfLink; this.friendlyName = builder.friendlyName; this.description = builder.description; @@ -244,10 +244,10 @@ public String etag() { } /** - * Returns an opaque id for the table. + * Returns the service-generated id for the table. */ - public String id() { - return id; + public String generatedId() { + return generatedId; } /** @@ -321,7 +321,7 @@ public String toString() { return MoreObjects.toStringHelper(this) .add("tableId", tableId) .add("etag", etag) - .add("id", id) + .add("generatedId", generatedId) .add("selfLink", selfLink) .add("friendlyName", friendlyName) .add("description", description) @@ -379,7 +379,7 @@ Table toPb() { tablePb.setEtag(etag); tablePb.setExpirationTime(expirationTime); tablePb.setFriendlyName(friendlyName); - tablePb.setId(id); + tablePb.setId(generatedId); tablePb.setSelfLink(selfLink); return tablePb; } diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DatasetInfoTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DatasetInfoTest.java index 20875c0fc853..474a31d44a20 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DatasetInfoTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DatasetInfoTest.java @@ -38,7 +38,7 @@ public class DatasetInfoTest { private static final String DESCRIPTION = "description"; private static final String ETAG = "0xFF00"; private static final String FRIENDLY_NAME = "friendlyDataset"; - private static final String ID = "P/D:1"; + private static final String GENERATED_ID = "P/D:1"; private static final Long LAST_MODIFIED = CREATION_TIME + 50; private static final String LOCATION = ""; private static final String SELF_LINK = "http://bigquery/p/d"; @@ -51,7 +51,7 @@ public class DatasetInfoTest { .description(DESCRIPTION) .etag(ETAG) .friendlyName(FRIENDLY_NAME) - .id(ID) + .generatedId(GENERATED_ID) .lastModified(LAST_MODIFIED) .location(LOCATION) .selfLink(SELF_LINK) @@ -90,7 +90,7 @@ public void testBuilder() { assertEquals(DESCRIPTION, DATASET_INFO.description()); assertEquals(ETAG, DATASET_INFO.etag()); assertEquals(FRIENDLY_NAME, DATASET_INFO.friendlyName()); - assertEquals(ID, DATASET_INFO.id()); + assertEquals(GENERATED_ID, DATASET_INFO.generatedId()); assertEquals(LAST_MODIFIED, DATASET_INFO.lastModified()); assertEquals(LOCATION, DATASET_INFO.location()); assertEquals(SELF_LINK, DATASET_INFO.selfLink()); @@ -101,7 +101,7 @@ public void testBuilder() { assertEquals(DESCRIPTION, DATASET_INFO_COMPLETE.description()); assertEquals(ETAG, DATASET_INFO_COMPLETE.etag()); assertEquals(FRIENDLY_NAME, DATASET_INFO_COMPLETE.friendlyName()); - assertEquals(ID, DATASET_INFO_COMPLETE.id()); + assertEquals(GENERATED_ID, DATASET_INFO_COMPLETE.generatedId()); assertEquals(LAST_MODIFIED, DATASET_INFO_COMPLETE.lastModified()); assertEquals(LOCATION, DATASET_INFO_COMPLETE.location()); assertEquals(SELF_LINK, DATASET_INFO_COMPLETE.selfLink()); @@ -125,7 +125,7 @@ private void compareDatasets(DatasetInfo expected, DatasetInfo value) { assertEquals(expected.description(), value.description()); assertEquals(expected.etag(), value.etag()); assertEquals(expected.friendlyName(), value.friendlyName()); - assertEquals(expected.id(), value.id()); + assertEquals(expected.generatedId(), value.generatedId()); assertEquals(expected.location(), value.location()); assertEquals(expected.selfLink(), value.selfLink()); assertEquals(expected.acl(), value.acl()); diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DatasetTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DatasetTest.java index dd03b7899ebc..43c550c59d11 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DatasetTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DatasetTest.java @@ -51,7 +51,7 @@ public class DatasetTest { private static final String DESCRIPTION = "description"; private static final String ETAG = "0xFF00"; private static final String FRIENDLY_NAME = "friendlyDataset"; - private static final String ID = "P/D:1"; + private static final String GENERATED_ID = "P/D:1"; private static final Long LAST_MODIFIED = CREATION_TIME + 50; private static final String LOCATION = ""; private static final String SELF_LINK = "http://bigquery/p/d"; @@ -102,7 +102,7 @@ public void testBuilder() { .description(DESCRIPTION) .etag(ETAG) .friendlyName(FRIENDLY_NAME) - .id(ID) + .generatedId(GENERATED_ID) .lastModified(LAST_MODIFIED) .location(LOCATION) .selfLink(SELF_LINK) @@ -114,7 +114,7 @@ public void testBuilder() { assertEquals(DESCRIPTION, builtDataset.description()); assertEquals(ETAG, builtDataset.etag()); assertEquals(FRIENDLY_NAME, builtDataset.friendlyName()); - assertEquals(ID, builtDataset.id()); + assertEquals(GENERATED_ID, builtDataset.generatedId()); assertEquals(LAST_MODIFIED, builtDataset.lastModified()); assertEquals(LOCATION, builtDataset.location()); assertEquals(SELF_LINK, builtDataset.selfLink()); @@ -362,7 +362,7 @@ private void compareDatasetInfo(DatasetInfo expected, DatasetInfo value) { assertEquals(expected.description(), value.description()); assertEquals(expected.etag(), value.etag()); assertEquals(expected.friendlyName(), value.friendlyName()); - assertEquals(expected.id(), value.id()); + assertEquals(expected.generatedId(), value.generatedId()); assertEquals(expected.location(), value.location()); assertEquals(expected.selfLink(), value.selfLink()); assertEquals(expected.acl(), value.acl()); diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobInfoTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobInfoTest.java index 260088470aff..9c90fbe7b05f 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobInfoTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobInfoTest.java @@ -37,7 +37,7 @@ public class JobInfoTest { private static final String ETAG = "etag"; - private static final String ID = "id"; + private static final String GENERATED_ID = "id"; private static final String SELF_LINK = "selfLink"; private static final String EMAIL = "email"; private static final JobId JOB_ID = JobId.of("job"); @@ -163,7 +163,7 @@ public class JobInfoTest { .statistics(COPY_JOB_STATISTICS) .jobId(JOB_ID) .etag(ETAG) - .id(ID) + .generatedId(GENERATED_ID) .selfLink(SELF_LINK) .userEmail(EMAIL) .status(JOB_STATUS) @@ -173,7 +173,7 @@ public class JobInfoTest { .statistics(EXTRACT_JOB_STATISTICS) .jobId(JOB_ID) .etag(ETAG) - .id(ID) + .generatedId(GENERATED_ID) .selfLink(SELF_LINK) .userEmail(EMAIL) .status(JOB_STATUS) @@ -183,7 +183,7 @@ public class JobInfoTest { .statistics(LOAD_JOB_STATISTICS) .jobId(JOB_ID) .etag(ETAG) - .id(ID) + .generatedId(GENERATED_ID) .selfLink(SELF_LINK) .userEmail(EMAIL) .status(JOB_STATUS) @@ -193,7 +193,7 @@ public class JobInfoTest { .statistics(QUERY_JOB_STATISTICS) .jobId(JOB_ID) .etag(ETAG) - .id(ID) + .generatedId(GENERATED_ID) .selfLink(SELF_LINK) .userEmail(EMAIL) .status(JOB_STATUS) @@ -266,7 +266,7 @@ public void testToBuilderIncomplete() { @Test public void testBuilder() { assertEquals(ETAG, COPY_JOB.etag()); - assertEquals(ID, COPY_JOB.id()); + assertEquals(GENERATED_ID, COPY_JOB.generatedId()); assertEquals(SELF_LINK, COPY_JOB.selfLink()); assertEquals(EMAIL, COPY_JOB.userEmail()); assertEquals(JOB_ID, COPY_JOB.jobId()); @@ -275,7 +275,7 @@ public void testBuilder() { assertEquals(COPY_JOB_STATISTICS, COPY_JOB.statistics()); assertEquals(ETAG, EXTRACT_JOB.etag()); - assertEquals(ID, EXTRACT_JOB.id()); + assertEquals(GENERATED_ID, EXTRACT_JOB.generatedId()); assertEquals(SELF_LINK, EXTRACT_JOB.selfLink()); assertEquals(EMAIL, EXTRACT_JOB.userEmail()); assertEquals(JOB_ID, EXTRACT_JOB.jobId()); @@ -284,7 +284,7 @@ public void testBuilder() { assertEquals(EXTRACT_JOB_STATISTICS, EXTRACT_JOB.statistics()); assertEquals(ETAG, LOAD_JOB.etag()); - assertEquals(ID, LOAD_JOB.id()); + assertEquals(GENERATED_ID, LOAD_JOB.generatedId()); assertEquals(SELF_LINK, LOAD_JOB.selfLink()); assertEquals(EMAIL, LOAD_JOB.userEmail()); assertEquals(JOB_ID, LOAD_JOB.jobId()); @@ -293,7 +293,7 @@ public void testBuilder() { assertEquals(LOAD_JOB_STATISTICS, LOAD_JOB.statistics()); assertEquals(ETAG, QUERY_JOB.etag()); - assertEquals(ID, QUERY_JOB.id()); + assertEquals(GENERATED_ID, QUERY_JOB.generatedId()); assertEquals(SELF_LINK, QUERY_JOB.selfLink()); assertEquals(EMAIL, QUERY_JOB.userEmail()); assertEquals(JOB_ID, QUERY_JOB.jobId()); @@ -359,7 +359,7 @@ private void compareJobInfo(JobInfo expected, JobInfo value) { assertEquals(expected.hashCode(), value.hashCode()); assertEquals(expected.toString(), value.toString()); assertEquals(expected.etag(), value.etag()); - assertEquals(expected.id(), value.id()); + assertEquals(expected.generatedId(), value.generatedId()); assertEquals(expected.jobId(), value.jobId()); assertEquals(expected.selfLink(), value.selfLink()); assertEquals(expected.status(), value.status()); diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobTest.java index db51706fff5a..e066bbed2fb4 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobTest.java @@ -36,7 +36,7 @@ public class JobTest { private static final TableId TABLE_ID1 = TableId.of("dataset", "table1"); private static final TableId TABLE_ID2 = TableId.of("dataset", "table2"); private static final String ETAG = "etag"; - private static final String ID = "id"; + private static final String GENERATED_ID = "id"; private static final String SELF_LINK = "selfLink"; private static final String EMAIL = "email"; private static final JobStatus JOB_STATUS = new JobStatus(JobStatus.State.DONE); @@ -52,7 +52,7 @@ public class JobTest { .statistics(COPY_JOB_STATISTICS) .jobId(JOB_ID) .etag(ETAG) - .id(ID) + .generatedId(GENERATED_ID) .selfLink(SELF_LINK) .userEmail(EMAIL) .status(JOB_STATUS) @@ -89,13 +89,13 @@ public void testBuilder() { .statistics(COPY_JOB_STATISTICS) .jobId(JOB_ID) .etag(ETAG) - .id(ID) + .generatedId(GENERATED_ID) .selfLink(SELF_LINK) .userEmail(EMAIL) .status(JOB_STATUS) .build(); assertEquals(ETAG, builtJob.etag()); - assertEquals(ID, builtJob.id()); + assertEquals(GENERATED_ID, builtJob.generatedId()); assertEquals(SELF_LINK, builtJob.selfLink()); assertEquals(EMAIL, builtJob.userEmail()); assertEquals(JOB_ID, builtJob.jobId()); @@ -247,7 +247,7 @@ private void compareJobInfo(JobInfo expected, JobInfo value) { assertEquals(expected.hashCode(), value.hashCode()); assertEquals(expected.toString(), value.toString()); assertEquals(expected.etag(), value.etag()); - assertEquals(expected.id(), value.id()); + assertEquals(expected.generatedId(), value.generatedId()); assertEquals(expected.jobId(), value.jobId()); assertEquals(expected.selfLink(), value.selfLink()); assertEquals(expected.status(), value.status()); diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java index 111df074ffa2..61e763f9a539 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java @@ -44,7 +44,7 @@ public class SerializationTest extends BaseSerializationTest { private static final String DESCRIPTION = "Description"; private static final String ETAG = "0xFF00"; private static final String FRIENDLY_NAME = "friendlyDataset"; - private static final String ID = "P/D:1"; + private static final String GENERATED_ID = "P/D:1"; private static final Long LAST_MODIFIED = CREATION_TIME + 50; private static final String LOCATION = ""; private static final String SELF_LINK = "http://bigquery/p/d"; @@ -56,7 +56,7 @@ public class SerializationTest extends BaseSerializationTest { .description(DESCRIPTION) .etag(ETAG) .friendlyName(FRIENDLY_NAME) - .id(ID) + .generatedId(GENERATED_ID) .lastModified(LAST_MODIFIED) .location(LOCATION) .selfLink(SELF_LINK) @@ -106,21 +106,21 @@ public class SerializationTest extends BaseSerializationTest { .creationTime(CREATION_TIME) .description(DESCRIPTION) .etag(ETAG) - .id(ID) + .generatedId(GENERATED_ID) .build(); private static final TableDefinition VIEW_DEFINITION = ViewDefinition.of("QUERY"); private static final TableInfo VIEW_INFO = TableInfo.builder(TABLE_ID, VIEW_DEFINITION) .creationTime(CREATION_TIME) .description(DESCRIPTION) .etag(ETAG) - .id(ID) + .generatedId(GENERATED_ID) .build(); private static final TableInfo EXTERNAL_TABLE_INFO = TableInfo.builder(TABLE_ID, EXTERNAL_TABLE_DEFINITION) .creationTime(CREATION_TIME) .description(DESCRIPTION) .etag(ETAG) - .id(ID) + .generatedId(GENERATED_ID) .build(); private static final JobStatistics JOB_STATISTICS = JobStatistics.builder() .creationTime(1L) diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableInfoTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableInfoTest.java index 18b8be10d71e..84d224f220cb 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableInfoTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableInfoTest.java @@ -27,7 +27,7 @@ public class TableInfoTest { private static final String ETAG = "etag"; - private static final String ID = "project:dataset:table"; + private static final String GENERATED_ID = "project:dataset:table"; private static final String SELF_LINK = "selfLink"; private static final TableId TABLE_ID = TableId.of("dataset", "table"); private static final String FRIENDLY_NAME = "friendlyName"; @@ -89,7 +89,7 @@ public class TableInfoTest { .etag(ETAG) .expirationTime(EXPIRATION_TIME) .friendlyName(FRIENDLY_NAME) - .id(ID) + .generatedId(GENERATED_ID) .lastModifiedTime(LAST_MODIFIED_TIME) .selfLink(SELF_LINK) .build(); @@ -99,7 +99,7 @@ public class TableInfoTest { .etag(ETAG) .expirationTime(EXPIRATION_TIME) .friendlyName(FRIENDLY_NAME) - .id(ID) + .generatedId(GENERATED_ID) .lastModifiedTime(LAST_MODIFIED_TIME) .selfLink(SELF_LINK) .build(); @@ -110,7 +110,7 @@ public class TableInfoTest { .etag(ETAG) .expirationTime(EXPIRATION_TIME) .friendlyName(FRIENDLY_NAME) - .id(ID) + .generatedId(GENERATED_ID) .lastModifiedTime(LAST_MODIFIED_TIME) .selfLink(SELF_LINK) .build(); @@ -148,7 +148,7 @@ public void testBuilder() { assertEquals(ETAG, TABLE_INFO.etag()); assertEquals(EXPIRATION_TIME, TABLE_INFO.expirationTime()); assertEquals(FRIENDLY_NAME, TABLE_INFO.friendlyName()); - assertEquals(ID, TABLE_INFO.id()); + assertEquals(GENERATED_ID, TABLE_INFO.generatedId()); assertEquals(LAST_MODIFIED_TIME, TABLE_INFO.lastModifiedTime()); assertEquals(TABLE_DEFINITION, TABLE_INFO.definition()); assertEquals(SELF_LINK, TABLE_INFO.selfLink()); @@ -159,7 +159,7 @@ public void testBuilder() { assertEquals(ETAG, VIEW_INFO.etag()); assertEquals(EXPIRATION_TIME, VIEW_INFO.expirationTime()); assertEquals(FRIENDLY_NAME, VIEW_INFO.friendlyName()); - assertEquals(ID, VIEW_INFO.id()); + assertEquals(GENERATED_ID, VIEW_INFO.generatedId()); assertEquals(LAST_MODIFIED_TIME, VIEW_INFO.lastModifiedTime()); assertEquals(VIEW_TYPE, VIEW_INFO.definition()); assertEquals(SELF_LINK, VIEW_INFO.selfLink()); @@ -169,7 +169,7 @@ public void testBuilder() { assertEquals(ETAG, EXTERNAL_TABLE_INFO.etag()); assertEquals(EXPIRATION_TIME, EXTERNAL_TABLE_INFO.expirationTime()); assertEquals(FRIENDLY_NAME, EXTERNAL_TABLE_INFO.friendlyName()); - assertEquals(ID, EXTERNAL_TABLE_INFO.id()); + assertEquals(GENERATED_ID, EXTERNAL_TABLE_INFO.generatedId()); assertEquals(LAST_MODIFIED_TIME, EXTERNAL_TABLE_INFO.lastModifiedTime()); assertEquals(EXTERNAL_TABLE_DEFINITION, EXTERNAL_TABLE_INFO.definition()); assertEquals(SELF_LINK, EXTERNAL_TABLE_INFO.selfLink()); @@ -198,7 +198,7 @@ private void compareTableInfo(TableInfo expected, TableInfo value) { assertEquals(expected.etag(), value.etag()); assertEquals(expected.expirationTime(), value.expirationTime()); assertEquals(expected.friendlyName(), value.friendlyName()); - assertEquals(expected.id(), value.id()); + assertEquals(expected.generatedId(), value.generatedId()); assertEquals(expected.lastModifiedTime(), value.lastModifiedTime()); assertEquals(expected.selfLink(), value.selfLink()); assertEquals(expected.definition(), value.definition()); diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableTest.java index c7828ebeadf4..cab71d4705d5 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableTest.java @@ -44,7 +44,7 @@ public class TableTest { private static final String ETAG = "etag"; - private static final String ID = "project:dataset:table1"; + private static final String GENERATED_ID = "project:dataset:table1"; private static final String SELF_LINK = "selfLink"; private static final String FRIENDLY_NAME = "friendlyName"; private static final String DESCRIPTION = "description"; @@ -115,7 +115,7 @@ public void testBuilder() { .etag(ETAG) .expirationTime(EXPIRATION_TIME) .friendlyName(FRIENDLY_NAME) - .id(ID) + .generatedId(GENERATED_ID) .lastModifiedTime(LAST_MODIFIED_TIME) .selfLink(SELF_LINK) .build(); @@ -125,7 +125,7 @@ public void testBuilder() { assertEquals(ETAG, builtTable.etag()); assertEquals(EXPIRATION_TIME, builtTable.expirationTime()); assertEquals(FRIENDLY_NAME, builtTable.friendlyName()); - assertEquals(ID, builtTable.id()); + assertEquals(GENERATED_ID, builtTable.generatedId()); assertEquals(LAST_MODIFIED_TIME, builtTable.lastModifiedTime()); assertEquals(TABLE_DEFINITION, builtTable.definition()); assertEquals(SELF_LINK, builtTable.selfLink()); @@ -398,7 +398,7 @@ private void compareTableInfo(TableInfo expected, TableInfo value) { assertEquals(expected.etag(), value.etag()); assertEquals(expected.expirationTime(), value.expirationTime()); assertEquals(expected.friendlyName(), value.friendlyName()); - assertEquals(expected.id(), value.id()); + assertEquals(expected.generatedId(), value.generatedId()); assertEquals(expected.lastModifiedTime(), value.lastModifiedTime()); assertEquals(expected.selfLink(), value.selfLink()); assertEquals(expected.definition(), value.definition()); diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/it/ITBigQueryTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/it/ITBigQueryTest.java index 50780b4fc9a9..e712eeeb23fd 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/it/ITBigQueryTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/it/ITBigQueryTest.java @@ -214,7 +214,7 @@ public void testGetDataset() { assertEquals(DESCRIPTION, dataset.description()); assertNotNull(dataset.acl()); assertNotNull(dataset.etag()); - assertNotNull(dataset.id()); + assertNotNull(dataset.generatedId()); assertNotNull(dataset.lastModified()); assertNotNull(dataset.selfLink()); } @@ -231,7 +231,7 @@ public void testGetDatasetWithSelectedFields() { assertNull(dataset.acl()); assertNull(dataset.etag()); assertNull(dataset.friendlyName()); - assertNull(dataset.id()); + assertNull(dataset.generatedId()); assertNull(dataset.lastModified()); assertNull(dataset.location()); assertNull(dataset.selfLink()); @@ -270,7 +270,7 @@ public void testUpdateDatasetWithSelectedFields() { assertNull(updatedDataset.acl()); assertNull(updatedDataset.etag()); assertNull(updatedDataset.friendlyName()); - assertNull(updatedDataset.id()); + assertNull(updatedDataset.generatedId()); assertNull(updatedDataset.lastModified()); assertNull(updatedDataset.location()); assertNull(updatedDataset.selfLink()); @@ -697,7 +697,7 @@ public void testListJobs() { assertNotNull(job.statistics()); assertNotNull(job.status()); assertNotNull(job.userEmail()); - assertNotNull(job.id()); + assertNotNull(job.generatedId()); } } @@ -709,7 +709,7 @@ public void testListJobsWithSelectedFields() { assertNotNull(job.status()); assertNotNull(job.userEmail()); assertNull(job.statistics()); - assertNull(job.id()); + assertNull(job.generatedId()); } } From d4faad32326b9f9043bfc9f405fc00ef4e77690e Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Fri, 1 Apr 2016 16:14:08 +0200 Subject: [PATCH 47/60] Rename id to generatedId for Storage resources --- .../java/com/google/gcloud/storage/Blob.java | 4 +-- .../com/google/gcloud/storage/BlobInfo.java | 26 +++++++++---------- .../com/google/gcloud/storage/Bucket.java | 4 +-- .../com/google/gcloud/storage/BucketInfo.java | 26 +++++++++---------- .../google/gcloud/storage/BlobInfoTest.java | 12 ++++----- .../com/google/gcloud/storage/BlobTest.java | 10 +++---- .../google/gcloud/storage/BucketInfoTest.java | 14 +++++----- .../com/google/gcloud/storage/BucketTest.java | 8 +++--- .../gcloud/storage/it/ITStorageTest.java | 4 +-- 9 files changed, 54 insertions(+), 54 deletions(-) diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java index b6f668dada82..9bd9902fee56 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java @@ -178,8 +178,8 @@ public Builder blobId(BlobId blobId) { } @Override - Builder id(String id) { - infoBuilder.id(id); + Builder generatedId(String generatedId) { + infoBuilder.generatedId(generatedId); return this; } diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobInfo.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobInfo.java index cf509c8f0961..42a2e282b002 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobInfo.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobInfo.java @@ -57,9 +57,9 @@ public StorageObject apply(BlobInfo blobInfo) { } }; - private static final long serialVersionUID = 2228487739943277159L; + private static final long serialVersionUID = -5625857076205028976L; private final BlobId blobId; - private final String id; + private final String generatedId; private final String selfLink; private final String cacheControl; private final List acl; @@ -101,7 +101,7 @@ public abstract static class Builder { */ public abstract Builder blobId(BlobId blobId); - abstract Builder id(String id); + abstract Builder generatedId(String generatedId); /** * Sets the blob's data content type. @@ -199,7 +199,7 @@ public abstract static class Builder { static final class BuilderImpl extends Builder { private BlobId blobId; - private String id; + private String generatedId; private String contentType; private String contentEncoding; private String contentDisposition; @@ -226,7 +226,7 @@ static final class BuilderImpl extends Builder { BuilderImpl(BlobInfo blobInfo) { blobId = blobInfo.blobId; - id = blobInfo.id; + generatedId = blobInfo.generatedId; cacheControl = blobInfo.cacheControl; contentEncoding = blobInfo.contentEncoding; contentType = blobInfo.contentType; @@ -255,8 +255,8 @@ public Builder blobId(BlobId blobId) { } @Override - Builder id(String id) { - this.id = id; + Builder generatedId(String generatedId) { + this.generatedId = generatedId; return this; } @@ -384,7 +384,7 @@ public BlobInfo build() { BlobInfo(BuilderImpl builder) { blobId = builder.blobId; - id = builder.id; + generatedId = builder.generatedId; cacheControl = builder.cacheControl; contentEncoding = builder.contentEncoding; contentType = builder.contentType; @@ -421,10 +421,10 @@ public String bucket() { } /** - * Returns the blob's id. + * Returns the service-generated for the blob. */ - public String id() { - return id; + public String generatedId() { + return generatedId; } /** @@ -684,7 +684,7 @@ public ObjectAccessControl apply(Acl acl) { storageObject.setComponentCount(componentCount); storageObject.setContentLanguage(contentLanguage); storageObject.setEtag(etag); - storageObject.setId(id); + storageObject.setId(generatedId); storageObject.setSelfLink(selfLink); return storageObject; } @@ -757,7 +757,7 @@ static BlobInfo fromPb(StorageObject storageObject) { builder.etag(storageObject.getEtag()); } if (storageObject.getId() != null) { - builder.id(storageObject.getId()); + builder.generatedId(storageObject.getId()); } if (storageObject.getSelfLink() != null) { builder.selfLink(storageObject.getSelfLink()); diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Bucket.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Bucket.java index e44bd60d785c..cb2058a9e7ab 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Bucket.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Bucket.java @@ -427,8 +427,8 @@ public Builder name(String name) { } @Override - Builder id(String id) { - infoBuilder.id(id); + Builder generatedId(String generatedId) { + infoBuilder.generatedId(generatedId); return this; } diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BucketInfo.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BucketInfo.java index a1de1a07e03e..a893e45c5c86 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BucketInfo.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BucketInfo.java @@ -64,8 +64,8 @@ public com.google.api.services.storage.model.Bucket apply(BucketInfo bucketInfo) return bucketInfo.toPb(); } }; - private static final long serialVersionUID = -3946094202176916586L; - private final String id; + private static final long serialVersionUID = -4712013629621638459L; + private final String generatedId; private final String name; private final Acl.Entity owner; private final String selfLink; @@ -326,7 +326,7 @@ public abstract static class Builder { */ public abstract Builder name(String name); - abstract Builder id(String id); + abstract Builder generatedId(String generatedId); abstract Builder owner(Acl.Entity owner); @@ -411,7 +411,7 @@ public abstract static class Builder { static final class BuilderImpl extends Builder { - private String id; + private String generatedId; private String name; private Acl.Entity owner; private String selfLink; @@ -433,7 +433,7 @@ static final class BuilderImpl extends Builder { } BuilderImpl(BucketInfo bucketInfo) { - id = bucketInfo.id; + generatedId = bucketInfo.generatedId; name = bucketInfo.name; etag = bucketInfo.etag; createTime = bucketInfo.createTime; @@ -458,8 +458,8 @@ public Builder name(String name) { } @Override - Builder id(String id) { - this.id = id; + Builder generatedId(String generatedId) { + this.generatedId = generatedId; return this; } @@ -555,7 +555,7 @@ public BucketInfo build() { } BucketInfo(BuilderImpl builder) { - id = builder.id; + generatedId = builder.generatedId; name = builder.name; etag = builder.etag; createTime = builder.createTime; @@ -574,10 +574,10 @@ public BucketInfo build() { } /** - * Returns the bucket's id. + * Returns the service-generated id for the bucket. */ - public String id() { - return id; + public String generatedId() { + return generatedId; } /** @@ -733,7 +733,7 @@ public String toString() { com.google.api.services.storage.model.Bucket toPb() { com.google.api.services.storage.model.Bucket bucketPb = new com.google.api.services.storage.model.Bucket(); - bucketPb.setId(id); + bucketPb.setId(generatedId); bucketPb.setName(name); bucketPb.setEtag(etag); if (createTime != null) { @@ -810,7 +810,7 @@ public static Builder builder(String name) { static BucketInfo fromPb(com.google.api.services.storage.model.Bucket bucketPb) { Builder builder = new BuilderImpl(bucketPb.getName()); if (bucketPb.getId() != null) { - builder.id(bucketPb.getId()); + builder.generatedId(bucketPb.getId()); } if (bucketPb.getEtag() != null) { builder.etag(bucketPb.getEtag()); diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobInfoTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobInfoTest.java index 029181c6c07b..db9dddaa864e 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobInfoTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobInfoTest.java @@ -51,7 +51,7 @@ public class BlobInfoTest { private static final Long DELETE_TIME = System.currentTimeMillis(); private static final String ETAG = "0xFF00"; private static final Long GENERATION = 1L; - private static final String ID = "B/N:1"; + private static final String GENERATED_ID = "B/N:1"; private static final String MD5 = "0xFF00"; private static final String MEDIA_LINK = "http://media/b/n"; private static final Map METADATA = ImmutableMap.of("n1", "v1", "n2", "v2"); @@ -71,7 +71,7 @@ public class BlobInfoTest { .crc32c(CRC32) .deleteTime(DELETE_TIME) .etag(ETAG) - .id(ID) + .generatedId(GENERATED_ID) .md5(MD5) .mediaLink(MEDIA_LINK) .metadata(METADATA) @@ -118,7 +118,7 @@ public void testBuilder() { assertEquals(DELETE_TIME, BLOB_INFO.deleteTime()); assertEquals(ETAG, BLOB_INFO.etag()); assertEquals(GENERATION, BLOB_INFO.generation()); - assertEquals(ID, BLOB_INFO.id()); + assertEquals(GENERATED_ID, BLOB_INFO.generatedId()); assertEquals(MD5, BLOB_INFO.md5()); assertEquals(MEDIA_LINK, BLOB_INFO.mediaLink()); assertEquals(METADATA, BLOB_INFO.metadata()); @@ -141,7 +141,7 @@ public void testBuilder() { assertNull(DIRECTORY_INFO.deleteTime()); assertNull(DIRECTORY_INFO.etag()); assertNull(DIRECTORY_INFO.generation()); - assertNull(DIRECTORY_INFO.id()); + assertNull(DIRECTORY_INFO.generatedId()); assertNull(DIRECTORY_INFO.md5()); assertNull(DIRECTORY_INFO.mediaLink()); assertNull(DIRECTORY_INFO.metadata()); @@ -168,7 +168,7 @@ private void compareBlobs(BlobInfo expected, BlobInfo value) { assertEquals(expected.deleteTime(), value.deleteTime()); assertEquals(expected.etag(), value.etag()); assertEquals(expected.generation(), value.generation()); - assertEquals(expected.id(), value.id()); + assertEquals(expected.generatedId(), value.generatedId()); assertEquals(expected.md5(), value.md5()); assertEquals(expected.mediaLink(), value.mediaLink()); assertEquals(expected.metadata(), value.metadata()); @@ -203,7 +203,7 @@ public void testToPbAndFromPb() { assertNull(blobInfo.deleteTime()); assertNull(blobInfo.etag()); assertNull(blobInfo.generation()); - assertNull(blobInfo.id()); + assertNull(blobInfo.generatedId()); assertNull(blobInfo.md5()); assertNull(blobInfo.mediaLink()); assertNull(blobInfo.metadata()); diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobTest.java index d6c97ca9ca03..c8c5fb5d763c 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobTest.java @@ -64,7 +64,7 @@ public class BlobTest { private static final Long DELETE_TIME = System.currentTimeMillis(); private static final String ETAG = "0xFF00"; private static final Long GENERATION = 1L; - private static final String ID = "B/N:1"; + private static final String GENERATED_ID = "B/N:1"; private static final String MD5 = "0xFF00"; private static final String MEDIA_LINK = "http://media/b/n"; private static final Map METADATA = ImmutableMap.of("n1", "v1", "n2", "v2"); @@ -84,7 +84,7 @@ public class BlobTest { .crc32c(CRC32) .deleteTime(DELETE_TIME) .etag(ETAG) - .id(ID) + .generatedId(GENERATED_ID) .md5(MD5) .mediaLink(MEDIA_LINK) .metadata(METADATA) @@ -338,7 +338,7 @@ public void testBuilder() { .crc32c(CRC32) .deleteTime(DELETE_TIME) .etag(ETAG) - .id(ID) + .generatedId(GENERATED_ID) .md5(MD5) .mediaLink(MEDIA_LINK) .metadata(METADATA) @@ -360,7 +360,7 @@ public void testBuilder() { assertEquals(CRC32, blob.crc32c()); assertEquals(DELETE_TIME, blob.deleteTime()); assertEquals(ETAG, blob.etag()); - assertEquals(ID, blob.id()); + assertEquals(GENERATED_ID, blob.generatedId()); assertEquals(MD5, blob.md5()); assertEquals(MEDIA_LINK, blob.mediaLink()); assertEquals(METADATA, blob.metadata()); @@ -387,7 +387,7 @@ public void testBuilder() { assertNull(blob.crc32c()); assertNull(blob.deleteTime()); assertNull(blob.etag()); - assertNull(blob.id()); + assertNull(blob.generatedId()); assertNull(blob.md5()); assertNull(blob.mediaLink()); assertNull(blob.metadata()); diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BucketInfoTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BucketInfoTest.java index bd6bcdbbcff2..6f9fadfdf7cd 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BucketInfoTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BucketInfoTest.java @@ -44,7 +44,7 @@ public class BucketInfoTest { Acl.of(User.ofAllAuthenticatedUsers(), Role.READER), Acl.of(new Project(VIEWERS, "p1"), Role.WRITER)); private static final String ETAG = "0xFF00"; - private static final String ID = "B/N:1"; + private static final String GENERATED_ID = "B/N:1"; private static final Long META_GENERATION = 10L; private static final User OWNER = new User("user@gmail.com"); private static final String SELF_LINK = "http://storage/b/n"; @@ -62,7 +62,7 @@ public class BucketInfoTest { private static final BucketInfo BUCKET_INFO = BucketInfo.builder("b") .acl(ACL) .etag(ETAG) - .id(ID) + .generatedId(GENERATED_ID) .metageneration(META_GENERATION) .owner(OWNER) .selfLink(SELF_LINK) @@ -80,10 +80,10 @@ public class BucketInfoTest { @Test public void testToBuilder() { compareBuckets(BUCKET_INFO, BUCKET_INFO.toBuilder().build()); - BucketInfo bucketInfo = BUCKET_INFO.toBuilder().name("B").id("id").build(); + BucketInfo bucketInfo = BUCKET_INFO.toBuilder().name("B").generatedId("id").build(); assertEquals("B", bucketInfo.name()); - assertEquals("id", bucketInfo.id()); - bucketInfo = bucketInfo.toBuilder().name("b").id(ID).build(); + assertEquals("id", bucketInfo.generatedId()); + bucketInfo = bucketInfo.toBuilder().name("b").generatedId(GENERATED_ID).build(); compareBuckets(BUCKET_INFO, bucketInfo); } @@ -104,7 +104,7 @@ public void testBuilder() { assertEquals("b", BUCKET_INFO.name()); assertEquals(ACL, BUCKET_INFO.acl()); assertEquals(ETAG, BUCKET_INFO.etag()); - assertEquals(ID, BUCKET_INFO.id()); + assertEquals(GENERATED_ID, BUCKET_INFO.generatedId()); assertEquals(META_GENERATION, BUCKET_INFO.metageneration()); assertEquals(OWNER, BUCKET_INFO.owner()); assertEquals(SELF_LINK, BUCKET_INFO.selfLink()); @@ -131,7 +131,7 @@ private void compareBuckets(BucketInfo expected, BucketInfo value) { assertEquals(expected.name(), value.name()); assertEquals(expected.acl(), value.acl()); assertEquals(expected.etag(), value.etag()); - assertEquals(expected.id(), value.id()); + assertEquals(expected.generatedId(), value.generatedId()); assertEquals(expected.metageneration(), value.metageneration()); assertEquals(expected.owner(), value.owner()); assertEquals(expected.selfLink(), value.selfLink()); diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BucketTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BucketTest.java index 53056c39c0dc..30b7416531f3 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BucketTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BucketTest.java @@ -59,7 +59,7 @@ public class BucketTest { private static final List ACL = ImmutableList.of( Acl.of(User.ofAllAuthenticatedUsers(), READER), Acl.of(new Project(VIEWERS, "p1"), WRITER)); private static final String ETAG = "0xFF00"; - private static final String ID = "B/N:1"; + private static final String GENERATED_ID = "B/N:1"; private static final Long META_GENERATION = 10L; private static final User OWNER = new User("user@gmail.com"); private static final String SELF_LINK = "http://storage/b/n"; @@ -77,7 +77,7 @@ public class BucketTest { private static final BucketInfo FULL_BUCKET_INFO = BucketInfo.builder("b") .acl(ACL) .etag(ETAG) - .id(ID) + .generatedId(GENERATED_ID) .metageneration(META_GENERATION) .owner(OWNER) .selfLink(SELF_LINK) @@ -495,7 +495,7 @@ public void testBuilder() { new Bucket.Builder(new Bucket(storage, new BucketInfo.BuilderImpl(BUCKET_INFO))); Bucket bucket = builder.acl(ACL) .etag(ETAG) - .id(ID) + .generatedId(GENERATED_ID) .metageneration(META_GENERATION) .owner(OWNER) .selfLink(SELF_LINK) @@ -512,7 +512,7 @@ public void testBuilder() { assertEquals("b", bucket.name()); assertEquals(ACL, bucket.acl()); assertEquals(ETAG, bucket.etag()); - assertEquals(ID, bucket.id()); + assertEquals(GENERATED_ID, bucket.generatedId()); assertEquals(META_GENERATION, bucket.metageneration()); assertEquals(OWNER, bucket.owner()); assertEquals(SELF_LINK, bucket.selfLink()); diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java index 13d768442c34..542cf966d269 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java @@ -122,7 +122,7 @@ public void testGetBucketSelectedFields() { Bucket remoteBucket = storage.get(BUCKET, Storage.BucketGetOption.fields(BucketField.ID)); assertEquals(BUCKET, remoteBucket.name()); assertNull(remoteBucket.createTime()); - assertNotNull(remoteBucket.id()); + assertNotNull(remoteBucket.generatedId()); } @Test @@ -256,7 +256,7 @@ public void testGetBlobAllSelectedFields() { assertEquals(blob.bucket(), remoteBlob.bucket()); assertEquals(blob.name(), remoteBlob.name()); assertEquals(ImmutableMap.of("k", "v"), remoteBlob.metadata()); - assertNotNull(remoteBlob.id()); + assertNotNull(remoteBlob.generatedId()); assertNotNull(remoteBlob.selfLink()); assertTrue(remoteBlob.delete()); } From 8df138b0aba5dbfa0a5b8dc07a1dc6541b4d2bac Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Fri, 1 Apr 2016 19:29:28 +0200 Subject: [PATCH 48/60] Rename id to generatedId for Dns resources --- gcloud-java-dns/README.md | 6 +- .../com/google/gcloud/dns/ChangeRequest.java | 4 +- .../google/gcloud/dns/ChangeRequestInfo.java | 40 +++--- .../main/java/com/google/gcloud/dns/Zone.java | 4 +- .../java/com/google/gcloud/dns/ZoneInfo.java | 36 +++--- .../gcloud/dns/ChangeRequestInfoTest.java | 12 +- .../google/gcloud/dns/ChangeRequestTest.java | 6 +- .../com/google/gcloud/dns/DnsImplTest.java | 13 +- .../google/gcloud/dns/SerializationTest.java | 4 +- .../com/google/gcloud/dns/ZoneInfoTest.java | 18 +-- .../java/com/google/gcloud/dns/ZoneTest.java | 60 ++++----- .../com/google/gcloud/dns/it/ITDnsTest.java | 118 +++++++++--------- .../gcloud/examples/dns/DnsExample.java | 12 +- .../examples/dns/snippets/CreateZone.java | 2 +- .../examples/dns/snippets/DeleteZone.java | 2 +- .../ManipulateZonesAndRecordSets.java | 6 +- 16 files changed, 175 insertions(+), 168 deletions(-) diff --git a/gcloud-java-dns/README.md b/gcloud-java-dns/README.md index a01282f16c5d..703bcb190807 100644 --- a/gcloud-java-dns/README.md +++ b/gcloud-java-dns/README.md @@ -125,7 +125,7 @@ ZoneInfo zoneInfo = ZoneInfo.of(zoneName, domainName, description); // Create zone in Google Cloud DNS Zone zone = dns.create(zoneInfo); -System.out.printf("Zone was created and assigned ID %s.%n", zone.id()); +System.out.printf("Zone was created and assigned ID %s.%n", zone.generatedId()); ``` You now have an empty zone hosted in Google Cloud DNS which is ready to be populated with @@ -226,7 +226,7 @@ while (ChangeRequestInfo.Status.PENDING.equals(changeRequest.status())) { } catch (InterruptedException e) { System.err.println("The thread was interrupted while waiting..."); } - changeRequest = dns.getChangeRequest(zone.name(), changeRequest.id()); + changeRequest = dns.getChangeRequest(zone.name(), changeRequest.generatedId()); } System.out.println("The change request has been applied."); ``` @@ -315,7 +315,7 @@ if (!changeRequest.deletions().isEmpty()) { } // Update the change, but fetch only change ID and status - changeRequest = dns.getChangeRequest(zoneName, changeRequest.id(), option); + changeRequest = dns.getChangeRequest(zoneName, changeRequest.generatedId(), option); } } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java index 4b6369976ca6..60b053faab6d 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java @@ -103,8 +103,8 @@ public Builder removeDeletion(RecordSet recordSet) { } @Override - Builder id(String id) { - infoBuilder.id(id); + Builder generatedId(String generatedId) { + infoBuilder.generatedId(generatedId); return this; } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequestInfo.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequestInfo.java index b63b4f4a0788..4a92512996dd 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequestInfo.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequestInfo.java @@ -47,10 +47,10 @@ public ChangeRequestInfo apply(Change pb) { return ChangeRequestInfo.fromPb(pb); } }; - private static final long serialVersionUID = -9027378042756366333L; + private static final long serialVersionUID = -6029143477639439169L; private final List additions; private final List deletions; - private final String id; + private final String generatedId; private final Long startTimeMillis; private final ChangeRequestInfo.Status status; @@ -119,9 +119,9 @@ public abstract static class Builder { public abstract Builder removeDeletion(RecordSet recordSet); /** - * Associates a server-assigned id to this {@code ChangeRequestInfo}. + * Associates a service-generated id to this {@code ChangeRequestInfo}. */ - abstract Builder id(String id); + abstract Builder generatedId(String generatedId); /** * Sets the time when this change request was started by a server. @@ -143,7 +143,7 @@ public abstract static class Builder { static class BuilderImpl extends Builder { private List additions; private List deletions; - private String id; + private String generatedId; private Long startTimeMillis; private ChangeRequestInfo.Status status; @@ -155,7 +155,7 @@ static class BuilderImpl extends Builder { BuilderImpl(ChangeRequestInfo info) { this.additions = Lists.newLinkedList(info.additions()); this.deletions = Lists.newLinkedList(info.deletions()); - this.id = info.id(); + this.generatedId = info.generatedId; this.startTimeMillis = info.startTimeMillis; this.status = info.status; } @@ -214,8 +214,8 @@ public ChangeRequestInfo build() { } @Override - Builder id(String id) { - this.id = checkNotNull(id); + Builder generatedId(String generatedId) { + this.generatedId = checkNotNull(generatedId); return this; } @@ -235,7 +235,7 @@ Builder status(ChangeRequestInfo.Status status) { ChangeRequestInfo(BuilderImpl builder) { this.additions = ImmutableList.copyOf(builder.additions); this.deletions = ImmutableList.copyOf(builder.deletions); - this.id = builder.id; + this.generatedId = builder.generatedId; this.startTimeMillis = builder.startTimeMillis; this.status = builder.status; } @@ -271,22 +271,22 @@ public List deletions() { } /** - * Returns the id assigned to this {@code ChangeRequest} by the server. + * Returns the service-generated id for this change request. */ - public String id() { - return id; + public String generatedId() { + return generatedId; } /** - * Returns the time when this {@code ChangeRequest} was started by the server. + * Returns the time when this change request was started by the server. */ public Long startTimeMillis() { return startTimeMillis; } /** - * Returns the status of this {@code ChangeRequest}. If the change request has not been applied - * yet, the status is {@code PENDING}. + * Returns the status of this change request. If the change request has not been applied yet, the + * status is {@code PENDING}. */ public ChangeRequestInfo.Status status() { return status; @@ -295,8 +295,8 @@ public ChangeRequestInfo.Status status() { Change toPb() { Change pb = new Change(); // set id - if (id() != null) { - pb.setId(id()); + if (generatedId() != null) { + pb.setId(generatedId()); } // set timestamp if (startTimeMillis() != null) { @@ -316,7 +316,7 @@ Change toPb() { static ChangeRequestInfo fromPb(Change pb) { Builder builder = builder(); if (pb.getId() != null) { - builder.id(pb.getId()); + builder.generatedId(pb.getId()); } if (pb.getStartTime() != null) { builder.startTimeMillis(DateTime.parse(pb.getStartTime()).getMillis()); @@ -342,7 +342,7 @@ public boolean equals(Object other) { @Override public int hashCode() { - return Objects.hash(additions, deletions, id, startTimeMillis, status); + return Objects.hash(additions, deletions, generatedId, startTimeMillis, status); } @Override @@ -350,7 +350,7 @@ public String toString() { return MoreObjects.toStringHelper(this) .add("additions", additions) .add("deletions", deletions) - .add("id", id) + .add("generatedId", generatedId) .add("startTimeMillis", startTimeMillis) .add("status", status) .toString(); 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 9930bfdbad67..6e22bfc6785a 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 @@ -61,8 +61,8 @@ public Builder name(String name) { } @Override - Builder id(String id) { - infoBuilder.id(id); + Builder generatedId(String generatedId) { + infoBuilder.generatedId(generatedId); return this; } 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 38a88b67777e..a0ee24bc5948 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 @@ -38,9 +38,9 @@ */ public class ZoneInfo implements Serializable { - private static final long serialVersionUID = 201601191647L; + private static final long serialVersionUID = -5313169712036079818L; private final String name; - private final String id; + private final String generatedId; private final Long creationTimeMillis; private final String dnsName; private final String description; @@ -57,9 +57,9 @@ public abstract static class Builder { public abstract Builder name(String name); /** - * Sets an id for the zone which is assigned to the zone by the server. + * Sets service-generated id for the zone. */ - abstract Builder id(String id); + abstract Builder generatedId(String generatedId); /** * Sets the time when this zone was created. @@ -98,7 +98,7 @@ public abstract static class Builder { static class BuilderImpl extends Builder { private String name; - private String id; + private String generatedId; private Long creationTimeMillis; private String dnsName; private String description; @@ -114,7 +114,7 @@ private BuilderImpl(String name) { */ BuilderImpl(ZoneInfo info) { this.name = info.name; - this.id = info.id; + this.generatedId = info.generatedId; this.creationTimeMillis = info.creationTimeMillis; this.dnsName = info.dnsName; this.description = info.description; @@ -131,8 +131,8 @@ public Builder name(String name) { } @Override - Builder id(String id) { - this.id = id; + Builder generatedId(String generatedId) { + this.generatedId = generatedId; return this; } @@ -175,7 +175,7 @@ public ZoneInfo build() { ZoneInfo(BuilderImpl builder) { this.name = builder.name; - this.id = builder.id; + this.generatedId = builder.generatedId; this.creationTimeMillis = builder.creationTimeMillis; this.dnsName = builder.dnsName; this.description = builder.description; @@ -199,10 +199,10 @@ public String name() { } /** - * Returns the read-only zone id assigned by the server. + * Returns the service-generated id for this zone. */ - public String id() { - return id; + public String generatedId() { + return generatedId; } /** @@ -253,8 +253,8 @@ com.google.api.services.dns.model.ManagedZone toPb() { new com.google.api.services.dns.model.ManagedZone(); pb.setDescription(this.description()); pb.setDnsName(this.dnsName()); - if (this.id() != null) { - pb.setId(new BigInteger(this.id())); + if (this.generatedId() != null) { + pb.setId(new BigInteger(this.generatedId())); } pb.setName(this.name()); pb.setNameServers(this.nameServers); // do use real attribute value which may be null @@ -276,7 +276,7 @@ static ZoneInfo fromPb(com.google.api.services.dns.model.ManagedZone pb) { builder.dnsName(pb.getDnsName()); } if (pb.getId() != null) { - builder.id(pb.getId().toString()); + builder.generatedId(pb.getId().toString()); } if (pb.getNameServers() != null) { builder.nameServers(pb.getNameServers()); @@ -298,15 +298,15 @@ public boolean equals(Object obj) { @Override public int hashCode() { - return Objects.hash(name, id, creationTimeMillis, dnsName, - description, nameServerSet, nameServers); + return Objects.hash(name, generatedId, creationTimeMillis, dnsName, description, nameServerSet, + nameServers); } @Override public String toString() { return MoreObjects.toStringHelper(this) .add("name", name()) - .add("id", id()) + .add("generatedId", generatedId()) .add("description", description()) .add("dnsName", dnsName()) .add("nameServerSet", nameServerSet()) diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestInfoTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestInfoTest.java index 55f2af0824ec..f3ae7a586391 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestInfoTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestInfoTest.java @@ -31,7 +31,7 @@ public class ChangeRequestInfoTest { - private static final String ID = "cr-id-1"; + private static final String GENERATED_ID = "cr-id-1"; private static final Long START_TIME_MILLIS = 12334567890L; private static final ChangeRequest.Status STATUS = ChangeRequest.Status.PENDING; private static final String NAME1 = "dns1"; @@ -51,7 +51,7 @@ public class ChangeRequestInfoTest { .delete(RECORD3) .startTimeMillis(START_TIME_MILLIS) .status(STATUS) - .id(ID) + .generatedId(GENERATED_ID) .build(); @Test @@ -65,7 +65,7 @@ public void testEmptyBuilder() { @Test public void testBuilder() { - assertEquals(ID, CHANGE.id()); + assertEquals(GENERATED_ID, CHANGE.generatedId()); assertEquals(STATUS, CHANGE.status()); assertEquals(START_TIME_MILLIS, CHANGE.startTimeMillis()); assertEquals(ADDITIONS, CHANGE.additions()); @@ -85,7 +85,7 @@ public void testEqualsAndNotEquals() { assertEquals(CHANGE, clone); clone = ChangeRequest.fromPb(CHANGE.toPb()); assertEquals(CHANGE, clone); - clone = CHANGE.toBuilder().id("some-other-id").build(); + clone = CHANGE.toBuilder().generatedId("some-other-id").build(); assertNotEquals(CHANGE, clone); clone = CHANGE.toBuilder().startTimeMillis(CHANGE.startTimeMillis() + 1).build(); assertNotEquals(CHANGE, clone); @@ -112,7 +112,7 @@ public void testToAndFromPb() { assertEquals(CHANGE, ChangeRequest.fromPb(CHANGE.toPb())); ChangeRequestInfo partial = ChangeRequest.builder().build(); assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); - partial = ChangeRequest.builder().id(ID).build(); + partial = ChangeRequest.builder().generatedId(GENERATED_ID).build(); assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); partial = ChangeRequest.builder().add(RECORD1).build(); assertEquals(partial, ChangeRequest.fromPb(partial.toPb())); @@ -133,7 +133,7 @@ public void testToBuilder() { assertEquals(CHANGE, CHANGE.toBuilder().build()); ChangeRequestInfo partial = ChangeRequest.builder().build(); assertEquals(partial, partial.toBuilder().build()); - partial = ChangeRequest.builder().id(ID).build(); + partial = ChangeRequest.builder().generatedId(GENERATED_ID).build(); assertEquals(partial, partial.toBuilder().build()); partial = ChangeRequest.builder().add(RECORD1).build(); assertEquals(partial, partial.toBuilder().build()); diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java index bfd1d0f512f4..4d4b2c01823c 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java @@ -53,7 +53,7 @@ public void setUp() throws Exception { changeRequest = new ChangeRequest(dns, ZONE_NAME, new ChangeRequestInfo.BuilderImpl( CHANGE_REQUEST_INFO.toBuilder() .startTimeMillis(132L) - .id("12") + .generatedId("12") .status(ChangeRequest.Status.DONE) .build())); changeRequestPartial = new ChangeRequest(dns, ZONE_NAME, @@ -104,8 +104,8 @@ public void testBuilder() { // one for each build() call because it invokes a constructor expect(dns.options()).andReturn(OPTIONS).times(9); replay(dns); - String id = changeRequest.id() + "aaa"; - assertEquals(id, changeRequest.toBuilder().id(id).build().id()); + String id = changeRequest.generatedId() + "aaa"; + assertEquals(id, changeRequest.toBuilder().generatedId(id).build().generatedId()); ChangeRequest modified = changeRequest.toBuilder().status(ChangeRequest.Status.PENDING).build(); assertEquals(ChangeRequest.Status.PENDING, modified.status()); diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java index 94ed4a3da3f7..d9d657ee03e7 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java @@ -60,7 +60,7 @@ public class DnsImplTest { .add(DNS_RECORD1) .startTimeMillis(123L) .status(ChangeRequest.Status.PENDING) - .id(CHANGE_ID) + .generatedId(CHANGE_ID) .build(); // Result lists @@ -215,12 +215,13 @@ public void testProjectGetWithOptions() { @Test public void testGetChangeRequest() { - EasyMock.expect(dnsRpcMock.getChangeRequest(ZONE_INFO.name(), CHANGE_REQUEST_COMPLETE.id(), - EMPTY_RPC_OPTIONS)).andReturn(CHANGE_REQUEST_COMPLETE.toPb()); + EasyMock.expect(dnsRpcMock.getChangeRequest(ZONE_INFO.name(), + CHANGE_REQUEST_COMPLETE.generatedId(), EMPTY_RPC_OPTIONS)) + .andReturn(CHANGE_REQUEST_COMPLETE.toPb()); EasyMock.replay(dnsRpcMock); dns = options.service(); // creates DnsImpl ChangeRequest changeRequest = dns.getChangeRequest(ZONE_INFO.name(), - CHANGE_REQUEST_COMPLETE.id()); + CHANGE_REQUEST_COMPLETE.generatedId()); assertEquals(new ChangeRequest(dns, ZONE_INFO.name(), new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_COMPLETE)), changeRequest); } @@ -229,12 +230,12 @@ public void testGetChangeRequest() { public void testGetChangeRequestWithOptions() { Capture> capturedOptions = Capture.newInstance(); EasyMock.expect(dnsRpcMock.getChangeRequest(EasyMock.eq(ZONE_INFO.name()), - EasyMock.eq(CHANGE_REQUEST_COMPLETE.id()), EasyMock.capture(capturedOptions))) + EasyMock.eq(CHANGE_REQUEST_COMPLETE.generatedId()), EasyMock.capture(capturedOptions))) .andReturn(CHANGE_REQUEST_COMPLETE.toPb()); EasyMock.replay(dnsRpcMock); dns = options.service(); // creates DnsImpl ChangeRequest changeRequest = dns.getChangeRequest(ZONE_INFO.name(), - CHANGE_REQUEST_COMPLETE.id(), CHANGE_GET_FIELDS); + CHANGE_REQUEST_COMPLETE.generatedId(), CHANGE_GET_FIELDS); String selector = (String) capturedOptions.getValue().get(CHANGE_GET_FIELDS.rpcOption()); assertEquals(new ChangeRequest(dns, ZONE_INFO.name(), new ChangeRequestInfo.BuilderImpl(CHANGE_REQUEST_COMPLETE)), changeRequest); diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/SerializationTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/SerializationTest.java index ad25b31068dd..7742d4c59a48 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/SerializationTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/SerializationTest.java @@ -31,7 +31,7 @@ public class SerializationTest extends BaseSerializationTest { private static final ZoneInfo FULL_ZONE_INFO = Zone.of("some zone name", "www.example.com", "some descriptions").toBuilder() .creationTimeMillis(132L) - .id("123333") + .generatedId("123333") .nameServers(ImmutableList.of("server 1", "server 2")) .nameServerSet("specificationstring") .build(); @@ -78,7 +78,7 @@ public class SerializationTest extends BaseSerializationTest { .add(RECORD_SET_COMPLETE) .delete(RECORD_SET_PARTIAL) .status(ChangeRequest.Status.PENDING) - .id("some id") + .generatedId("some id") .startTimeMillis(132L) .build(); private static final ChangeRequest CHANGE_REQUEST_COMPLETE = new ChangeRequest(DNS, "name", 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 923672bb85a7..d0a64a61b816 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 @@ -32,7 +32,7 @@ public class ZoneInfoTest { private static final String NAME = "mz-example.com"; - private static final String ID = "123456"; + private static final String GENERATED_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"; @@ -43,7 +43,7 @@ public class ZoneInfoTest { private static final List NAME_SERVERS = ImmutableList.of(NS1, NS2, NS3); private static final ZoneInfo INFO = ZoneInfo.of(NAME, DNS_NAME, DESCRIPTION).toBuilder() .creationTimeMillis(CREATION_TIME_MILLIS) - .id(ID) + .generatedId(GENERATED_ID) .nameServerSet(NAME_SERVER_SET) .nameServers(NAME_SERVERS) .build(); @@ -53,7 +53,7 @@ public void testOf() { ZoneInfo partial = ZoneInfo.of(NAME, DNS_NAME, DESCRIPTION); assertTrue(partial.nameServers().isEmpty()); assertEquals(NAME, partial.name()); - assertNull(partial.id()); + assertNull(partial.generatedId()); assertNull(partial.creationTimeMillis()); assertNull(partial.nameServerSet()); assertEquals(DESCRIPTION, partial.description()); @@ -67,7 +67,7 @@ public void testBuilder() { assertEquals(NS2, INFO.nameServers().get(1)); assertEquals(NS3, INFO.nameServers().get(2)); assertEquals(NAME, INFO.name()); - assertEquals(ID, INFO.id()); + assertEquals(GENERATED_ID, INFO.generatedId()); assertEquals(CREATION_TIME_MILLIS, INFO.creationTimeMillis()); assertEquals(NAME_SERVER_SET, INFO.nameServerSet()); assertEquals(DESCRIPTION, INFO.description()); @@ -91,7 +91,7 @@ public void testEqualsAndNotEquals() { assertNotEquals(INFO, clone); clone = INFO.toBuilder().dnsName(differentName).build(); assertNotEquals(INFO, clone); - clone = INFO.toBuilder().id(INFO.id() + "1111").build(); + clone = INFO.toBuilder().generatedId(INFO.generatedId() + "1111").build(); assertNotEquals(INFO, clone); clone = INFO.toBuilder().nameServerSet(INFO.nameServerSet() + "salt").build(); assertNotEquals(INFO, clone); @@ -109,7 +109,9 @@ public void testToBuilder() { assertEquals(INFO, INFO.toBuilder().build()); ZoneInfo partial = ZoneInfo.of(NAME, DNS_NAME, DESCRIPTION); assertEquals(partial, partial.toBuilder().build()); - partial = ZoneInfo.of(NAME, DNS_NAME, DESCRIPTION).toBuilder().id(ID).build(); + partial = ZoneInfo.of(NAME, DNS_NAME, DESCRIPTION).toBuilder() + .generatedId(GENERATED_ID) + .build(); assertEquals(partial, partial.toBuilder().build()); partial = ZoneInfo.of(NAME, DNS_NAME, DESCRIPTION).toBuilder() .creationTimeMillis(CREATION_TIME_MILLIS).build(); @@ -128,7 +130,9 @@ public void testToAndFromPb() { assertEquals(INFO, ZoneInfo.fromPb(INFO.toPb())); ZoneInfo partial = ZoneInfo.of(NAME, DNS_NAME, DESCRIPTION); assertEquals(partial, ZoneInfo.fromPb(partial.toPb())); - partial = ZoneInfo.of(NAME, DNS_NAME, DESCRIPTION).toBuilder().id(ID).build(); + partial = ZoneInfo.of(NAME, DNS_NAME, DESCRIPTION).toBuilder() + .generatedId(GENERATED_ID) + .build(); assertEquals(partial, ZoneInfo.fromPb(partial.toPb())); partial = ZoneInfo.of(NAME, DNS_NAME, DESCRIPTION).toBuilder() .creationTimeMillis(CREATION_TIME_MILLIS).build(); 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 ba4493abfca8..f14addfc44b6 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 @@ -45,7 +45,7 @@ public class ZoneTest { private static final String ZONE_ID = "123"; private static final ZoneInfo ZONE_INFO = Zone.of(ZONE_NAME, "example.com", "description") .toBuilder() - .id(ZONE_ID) + .generatedId(ZONE_ID) .creationTimeMillis(123478946464L) .build(); private static final ZoneInfo NO_ID_INFO = @@ -61,7 +61,7 @@ public class ZoneTest { private static final Dns.ChangeRequestListOption CHANGE_REQUEST_LIST_OPTIONS = Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.START_TIME); private static final ChangeRequestInfo CHANGE_REQUEST = - ChangeRequestInfo.builder().id("someid").build(); + ChangeRequestInfo.builder().generatedId("someid").build(); private static final ChangeRequestInfo CHANGE_REQUEST_NO_ID = ChangeRequestInfo.builder().build(); private static final DnsException EXCEPTION = createStrictMock(DnsException.class); @@ -287,51 +287,52 @@ public void applyNullChangeRequest() { @Test public void getChangeAndZoneFoundByName() { - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())) + expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.generatedId())) .andReturn(changeRequestAfter).times(2); // again for options - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(changeRequestAfter).times(2); + expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.generatedId(), + CHANGE_REQUEST_FIELD_OPTIONS)).andReturn(changeRequestAfter).times(2); replay(dns); - ChangeRequest result = zoneNoId.getChangeRequest(CHANGE_REQUEST.id()); + ChangeRequest result = zoneNoId.getChangeRequest(CHANGE_REQUEST.generatedId()); assertEquals(changeRequestAfter, result); - result = zone.getChangeRequest(CHANGE_REQUEST.id()); + result = zone.getChangeRequest(CHANGE_REQUEST.generatedId()); assertEquals(changeRequestAfter, result); // check options - result = zoneNoId.getChangeRequest(CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS); + result = zoneNoId.getChangeRequest(CHANGE_REQUEST.generatedId(), CHANGE_REQUEST_FIELD_OPTIONS); assertEquals(changeRequestAfter, result); - result = zone.getChangeRequest(CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS); + result = zone.getChangeRequest(CHANGE_REQUEST.generatedId(), CHANGE_REQUEST_FIELD_OPTIONS); assertEquals(changeRequestAfter, result); } @Test public void getChangeAndZoneNotFoundByName() { - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())).andThrow(EXCEPTION).times(2); + expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.generatedId())).andThrow(EXCEPTION) + .times(2); // again for options - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)) - .andThrow(EXCEPTION).times(2); + expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.generatedId(), + CHANGE_REQUEST_FIELD_OPTIONS)).andThrow(EXCEPTION).times(2); replay(dns); try { - zoneNoId.getChangeRequest(CHANGE_REQUEST.id()); + zoneNoId.getChangeRequest(CHANGE_REQUEST.generatedId()); fail("Parent container not found, should throw an exception."); } catch (DnsException e) { // expected } try { - zone.getChangeRequest(CHANGE_REQUEST.id()); + zone.getChangeRequest(CHANGE_REQUEST.generatedId()); fail("Parent container not found, should throw an exception."); } catch (DnsException e) { // expected } // check options try { - zoneNoId.getChangeRequest(CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS); + zoneNoId.getChangeRequest(CHANGE_REQUEST.generatedId(), 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); + zone.getChangeRequest(CHANGE_REQUEST.generatedId(), CHANGE_REQUEST_FIELD_OPTIONS); fail("Parent container not found, should throw an exception."); } catch (DnsException e) { // expected @@ -340,15 +341,16 @@ public void getChangeAndZoneNotFoundByName() { @Test public void getChangedWhichDoesNotExistZoneFound() { - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id())).andReturn(null).times(2); + expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.generatedId())).andReturn(null).times(2); // again for options - expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.id(), CHANGE_REQUEST_FIELD_OPTIONS)) - .andReturn(null).times(2); + expect(dns.getChangeRequest(ZONE_NAME, CHANGE_REQUEST.generatedId(), + CHANGE_REQUEST_FIELD_OPTIONS)).andReturn(null).times(2); replay(dns); - 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)); + assertNull(zoneNoId.getChangeRequest(CHANGE_REQUEST.generatedId())); + assertNull(zone.getChangeRequest(CHANGE_REQUEST.generatedId())); + assertNull( + zoneNoId.getChangeRequest(CHANGE_REQUEST.generatedId(),CHANGE_REQUEST_FIELD_OPTIONS)); + assertNull(zone.getChangeRequest(CHANGE_REQUEST.generatedId(), CHANGE_REQUEST_FIELD_OPTIONS)); } @Test @@ -384,25 +386,25 @@ public void getNullChangeRequest() { public void getChangeRequestWithNoId() { replay(dns); // no calls expected try { - zone.getChangeRequest(CHANGE_REQUEST_NO_ID.id()); + zone.getChangeRequest(CHANGE_REQUEST_NO_ID.generatedId()); fail("Cannot get ChangeRequest by null id."); } catch (NullPointerException e) { // expected } try { - zone.getChangeRequest(CHANGE_REQUEST_NO_ID.id(), CHANGE_REQUEST_FIELD_OPTIONS); + zone.getChangeRequest(CHANGE_REQUEST_NO_ID.generatedId(), CHANGE_REQUEST_FIELD_OPTIONS); fail("Cannot get ChangeRequest by null id."); } catch (NullPointerException e) { // expected } try { - zoneNoId.getChangeRequest(CHANGE_REQUEST_NO_ID.id()); + zoneNoId.getChangeRequest(CHANGE_REQUEST_NO_ID.generatedId()); fail("Cannot get ChangeRequest by null id."); } catch (NullPointerException e) { // expected } try { - zoneNoId.getChangeRequest(CHANGE_REQUEST_NO_ID.id(), CHANGE_REQUEST_FIELD_OPTIONS); + zoneNoId.getChangeRequest(CHANGE_REQUEST_NO_ID.generatedId(), CHANGE_REQUEST_FIELD_OPTIONS); fail("Cannot get ChangeRequest by null id."); } catch (NullPointerException e) { // expected @@ -482,7 +484,7 @@ public void testBuilder() { expect(dns.options()).andReturn(OPTIONS).times(8); replay(dns); assertNotEquals(zone, zone.toBuilder() - .id((new BigInteger(zone.id())).add(BigInteger.ONE).toString()) + .generatedId(new BigInteger(zone.generatedId()).add(BigInteger.ONE).toString()) .build()); assertNotEquals(zone, zone.toBuilder().dnsName(zone.name() + "aaaa").build()); assertNotEquals(zone, zone.toBuilder().nameServerSet(zone.nameServerSet() + "aaaa").build()); @@ -491,7 +493,7 @@ public void testBuilder() { assertNotEquals(zone, zone.toBuilder().creationTimeMillis(zone.creationTimeMillis() + 1) .build()); Zone.Builder builder = zone.toBuilder(); - builder.id(ZONE_ID) + builder.generatedId(ZONE_ID) .dnsName("example.com") .creationTimeMillis(123478946464L) .build(); diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java index c9070f2eddde..aa6c78461b8b 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java @@ -97,7 +97,7 @@ private static void clear() { when the list has been retrieved and executing the subsequent delete operation. */ Iterator iterator = zone.listChangeRequests().iterateAll(); while (iterator.hasNext()) { - waitForChangeToComplete(zoneName, iterator.next().id()); + waitForChangeToComplete(zoneName, iterator.next().generatedId()); } Iterator recordSetIterator = zone.listRecordSets().iterateAll(); List toDelete = new LinkedList<>(); @@ -111,7 +111,7 @@ private static void clear() { if (!toDelete.isEmpty()) { ChangeRequest deletion = zone.applyChangeRequest(ChangeRequest.builder().deletions(toDelete).build()); - waitForChangeToComplete(zone.name(), deletion.id()); + waitForChangeToComplete(zone.name(), deletion.generatedId()); } zone.delete(); } @@ -142,7 +142,7 @@ public static void after() { private static void assertEqChangesIgnoreStatus(ChangeRequest expected, ChangeRequest actual) { assertEquals(expected.additions(), actual.additions()); assertEquals(expected.deletions(), actual.deletions()); - assertEquals(expected.id(), actual.id()); + assertEquals(expected.generatedId(), actual.generatedId()); assertEquals(expected.startTimeMillis(), actual.startTimeMillis()); } @@ -174,7 +174,7 @@ public void testCreateValidZone() { assertNotNull(created.creationTimeMillis()); assertNotNull(created.nameServers()); assertNull(created.nameServerSet()); - assertNotNull(created.id()); + assertNotNull(created.generatedId()); Zone retrieved = DNS.getZone(ZONE1.name()); assertEquals(created, retrieved); created = DNS.create(ZONE_EMPTY_DESCRIPTION); @@ -184,7 +184,7 @@ public void testCreateValidZone() { assertNotNull(created.creationTimeMillis()); assertNotNull(created.nameServers()); assertNull(created.nameServerSet()); - assertNotNull(created.id()); + assertNotNull(created.generatedId()); retrieved = DNS.getZone(ZONE_EMPTY_DESCRIPTION.name()); assertEquals(created, retrieved); } finally { @@ -226,7 +226,7 @@ public void testCreateZoneWithOptions() { assertNull(created.dnsName()); assertTrue(created.nameServers().isEmpty()); // never returns null assertNull(created.nameServerSet()); - assertNull(created.id()); + assertNull(created.generatedId()); created.delete(); created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.DESCRIPTION)); assertEquals(ZONE1.name(), created.name()); // always returned @@ -235,7 +235,7 @@ public void testCreateZoneWithOptions() { assertNull(created.dnsName()); assertTrue(created.nameServers().isEmpty()); // never returns null assertNull(created.nameServerSet()); - assertNull(created.id()); + assertNull(created.generatedId()); created.delete(); created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.DNS_NAME)); assertEquals(ZONE1.name(), created.name()); // always returned @@ -244,7 +244,7 @@ public void testCreateZoneWithOptions() { assertNull(created.description()); assertTrue(created.nameServers().isEmpty()); // never returns null assertNull(created.nameServerSet()); - assertNull(created.id()); + assertNull(created.generatedId()); created.delete(); created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); assertEquals(ZONE1.name(), created.name()); // always returned @@ -253,7 +253,7 @@ public void testCreateZoneWithOptions() { assertNull(created.description()); assertTrue(created.nameServers().isEmpty()); // never returns null assertNull(created.nameServerSet()); - assertNull(created.id()); + assertNull(created.generatedId()); created.delete(); created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVER_SET)); assertEquals(ZONE1.name(), created.name()); // always returned @@ -262,7 +262,7 @@ public void testCreateZoneWithOptions() { assertNull(created.description()); assertTrue(created.nameServers().isEmpty()); // never returns null assertNull(created.nameServerSet()); // we did not set it - assertNull(created.id()); + assertNull(created.generatedId()); created.delete(); created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVERS)); assertEquals(ZONE1.name(), created.name()); // always returned @@ -271,7 +271,7 @@ public void testCreateZoneWithOptions() { assertNull(created.description()); assertFalse(created.nameServers().isEmpty()); assertNull(created.nameServerSet()); - assertNull(created.id()); + assertNull(created.generatedId()); created.delete(); created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.ZONE_ID)); assertEquals(ZONE1.name(), created.name()); // always returned @@ -280,7 +280,7 @@ public void testCreateZoneWithOptions() { assertNull(created.description()); assertNotNull(created.nameServers()); assertTrue(created.nameServers().isEmpty()); // never returns null - assertNotNull(created.id()); + assertNotNull(created.generatedId()); created.delete(); // combination of multiple things created = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.ZONE_ID, @@ -291,7 +291,7 @@ public void testCreateZoneWithOptions() { assertEquals(ZONE1.description(), created.description()); assertFalse(created.nameServers().isEmpty()); assertNull(created.nameServerSet()); // we did not set it - assertNotNull(created.id()); + assertNotNull(created.generatedId()); } finally { DNS.delete(ZONE1.name()); } @@ -308,7 +308,7 @@ public void testGetZone() { assertNull(created.dnsName()); assertTrue(created.nameServers().isEmpty()); // never returns null assertNull(created.nameServerSet()); - assertNull(created.id()); + assertNull(created.generatedId()); created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.DESCRIPTION)); assertEquals(ZONE1.name(), created.name()); // always returned assertNull(created.creationTimeMillis()); @@ -316,7 +316,7 @@ public void testGetZone() { assertNull(created.dnsName()); assertTrue(created.nameServers().isEmpty()); // never returns null assertNull(created.nameServerSet()); - assertNull(created.id()); + assertNull(created.generatedId()); created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.DNS_NAME)); assertEquals(ZONE1.name(), created.name()); // always returned assertNull(created.creationTimeMillis()); @@ -324,7 +324,7 @@ public void testGetZone() { assertNull(created.description()); assertTrue(created.nameServers().isEmpty()); // never returns null assertNull(created.nameServerSet()); - assertNull(created.id()); + assertNull(created.generatedId()); created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.NAME)); assertEquals(ZONE1.name(), created.name()); // always returned assertNull(created.creationTimeMillis()); @@ -332,7 +332,7 @@ public void testGetZone() { assertNull(created.description()); assertTrue(created.nameServers().isEmpty()); // never returns null assertNull(created.nameServerSet()); - assertNull(created.id()); + assertNull(created.generatedId()); created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVER_SET)); assertEquals(ZONE1.name(), created.name()); // always returned assertNull(created.creationTimeMillis()); @@ -340,7 +340,7 @@ public void testGetZone() { assertNull(created.description()); assertTrue(created.nameServers().isEmpty()); // never returns null assertNull(created.nameServerSet()); // we did not set it - assertNull(created.id()); + assertNull(created.generatedId()); created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.NAME_SERVERS)); assertEquals(ZONE1.name(), created.name()); // always returned assertNull(created.creationTimeMillis()); @@ -348,7 +348,7 @@ public void testGetZone() { assertNull(created.description()); assertFalse(created.nameServers().isEmpty()); assertNull(created.nameServerSet()); - assertNull(created.id()); + assertNull(created.generatedId()); created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.ZONE_ID)); assertEquals(ZONE1.name(), created.name()); // always returned assertNull(created.creationTimeMillis()); @@ -356,7 +356,7 @@ public void testGetZone() { assertNull(created.description()); assertNotNull(created.nameServers()); assertTrue(created.nameServers().isEmpty()); // never returns null - assertNotNull(created.id()); + assertNotNull(created.generatedId()); // combination of multiple things created = DNS.getZone(ZONE1.name(), Dns.ZoneOption.fields(Dns.ZoneField.ZONE_ID, Dns.ZoneField.NAME_SERVERS, Dns.ZoneField.NAME_SERVER_SET, Dns.ZoneField.DESCRIPTION)); @@ -366,7 +366,7 @@ public void testGetZone() { assertEquals(ZONE1.description(), created.description()); assertFalse(created.nameServers().isEmpty()); assertNull(created.nameServerSet()); // we did not set it - assertNotNull(created.id()); + assertNotNull(created.generatedId()); } finally { DNS.delete(ZONE1.name()); } @@ -428,7 +428,7 @@ public void testListZones() { assertNull(zone.description()); assertNull(zone.nameServerSet()); assertTrue(zone.nameServers().isEmpty()); - assertNotNull(zone.id()); + assertNotNull(zone.generatedId()); assertFalse(zoneIterator.hasNext()); zoneIterator = DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), Dns.ZoneListOption.fields(Dns.ZoneField.CREATION_TIME)).iterateAll(); @@ -439,7 +439,7 @@ public void testListZones() { assertNull(zone.description()); assertNull(zone.nameServerSet()); assertTrue(zone.nameServers().isEmpty()); - assertNull(zone.id()); + assertNull(zone.generatedId()); assertFalse(zoneIterator.hasNext()); zoneIterator = DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), Dns.ZoneListOption.fields(Dns.ZoneField.DNS_NAME)).iterateAll(); @@ -450,7 +450,7 @@ public void testListZones() { assertNull(zone.description()); assertNull(zone.nameServerSet()); assertTrue(zone.nameServers().isEmpty()); - assertNull(zone.id()); + assertNull(zone.generatedId()); assertFalse(zoneIterator.hasNext()); zoneIterator = DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), Dns.ZoneListOption.fields(Dns.ZoneField.DESCRIPTION)).iterateAll(); @@ -461,7 +461,7 @@ public void testListZones() { assertNotNull(zone.description()); assertNull(zone.nameServerSet()); assertTrue(zone.nameServers().isEmpty()); - assertNull(zone.id()); + assertNull(zone.generatedId()); assertFalse(zoneIterator.hasNext()); zoneIterator = DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), Dns.ZoneListOption.fields(Dns.ZoneField.NAME_SERVERS)).iterateAll(); @@ -472,7 +472,7 @@ public void testListZones() { assertNull(zone.description()); assertNull(zone.nameServerSet()); assertTrue(!zone.nameServers().isEmpty()); - assertNull(zone.id()); + assertNull(zone.generatedId()); assertFalse(zoneIterator.hasNext()); zoneIterator = DNS.listZones(Dns.ZoneListOption.dnsName(ZONE1.dnsName()), Dns.ZoneListOption.fields(Dns.ZoneField.NAME_SERVER_SET)).iterateAll(); @@ -483,7 +483,7 @@ public void testListZones() { assertNull(zone.description()); assertNull(zone.nameServerSet()); // we cannot set it using gcloud java assertTrue(zone.nameServers().isEmpty()); - assertNull(zone.id()); + assertNull(zone.generatedId()); assertFalse(zoneIterator.hasNext()); // several combined zones = filter(DNS.listZones(Dns.ZoneListOption.fields(Dns.ZoneField.ZONE_ID, @@ -497,7 +497,7 @@ public void testListZones() { assertNotNull(current.description()); assertNull(current.nameServerSet()); assertTrue(zone.nameServers().isEmpty()); - assertNotNull(current.id()); + assertNotNull(current.generatedId()); } } finally { DNS.delete(ZONE1.name()); @@ -525,7 +525,7 @@ public void testCreateChange() { assertEquals(CHANGE_ADD_ZONE1.additions(), created.additions()); assertNotNull(created.startTimeMillis()); assertTrue(created.deletions().isEmpty()); - assertEquals("1", created.id()); + assertEquals("1", created.generatedId()); assertTrue(ImmutableList.of(ChangeRequest.Status.PENDING, ChangeRequest.Status.DONE) .contains(created.status())); assertEqChangesIgnoreStatus(created, DNS.getChangeRequest(ZONE1.name(), "1")); @@ -538,7 +538,7 @@ public void testCreateChange() { assertTrue(created.additions().isEmpty()); assertNull(created.startTimeMillis()); assertTrue(created.deletions().isEmpty()); - assertEquals("3", created.id()); + assertEquals("3", created.generatedId()); assertNull(created.status()); waitForChangeToComplete(ZONE1.name(), "3"); DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); @@ -548,7 +548,7 @@ public void testCreateChange() { assertTrue(created.additions().isEmpty()); assertNull(created.startTimeMillis()); assertTrue(created.deletions().isEmpty()); - assertEquals("5", created.id()); + assertEquals("5", created.generatedId()); assertNotNull(created.status()); waitForChangeToComplete(ZONE1.name(), "5"); DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); @@ -558,7 +558,7 @@ public void testCreateChange() { assertTrue(created.additions().isEmpty()); assertNotNull(created.startTimeMillis()); assertTrue(created.deletions().isEmpty()); - assertEquals("7", created.id()); + assertEquals("7", created.generatedId()); assertNull(created.status()); waitForChangeToComplete(ZONE1.name(), "7"); DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); @@ -568,7 +568,7 @@ public void testCreateChange() { assertEquals(CHANGE_ADD_ZONE1.additions(), created.additions()); assertNull(created.startTimeMillis()); assertTrue(created.deletions().isEmpty()); - assertEquals("9", created.id()); + assertEquals("9", created.generatedId()); assertNull(created.status()); // finishes with delete otherwise we cannot delete the zone waitForChangeToComplete(ZONE1.name(), "9"); @@ -578,7 +578,7 @@ public void testCreateChange() { assertEquals(CHANGE_DELETE_ZONE1.deletions(), created.deletions()); assertNull(created.startTimeMillis()); assertTrue(created.additions().isEmpty()); - assertEquals("10", created.id()); + assertEquals("10", created.generatedId()); assertNull(created.status()); waitForChangeToComplete(ZONE1.name(), "10"); } finally { @@ -653,7 +653,7 @@ public void testInvalidChangeRequest() { if (recordAdded) { ChangeRequestInfo deletion = ChangeRequest.builder().delete(validA).build(); ChangeRequest request = zone.applyChangeRequest(deletion); - waitForChangeToComplete(zone.name(), request.id()); + waitForChangeToComplete(zone.name(), request.generatedId()); } zone.delete(); } @@ -678,13 +678,13 @@ public void testListChanges() { assertEquals(1, changes.size()); // default change creating SOA and NS // zone has changes ChangeRequest change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); - waitForChangeToComplete(ZONE1.name(), change.id()); + waitForChangeToComplete(ZONE1.name(), change.generatedId()); change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - waitForChangeToComplete(ZONE1.name(), change.id()); + waitForChangeToComplete(ZONE1.name(), change.generatedId()); change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); - waitForChangeToComplete(ZONE1.name(), change.id()); + waitForChangeToComplete(ZONE1.name(), change.generatedId()); change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - waitForChangeToComplete(ZONE1.name(), change.id()); + waitForChangeToComplete(ZONE1.name(), change.generatedId()); changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name()).iterateAll()); assertEquals(5, changes.size()); // error in options @@ -724,7 +724,7 @@ public void testListChanges() { change = changes.get(1); assertEquals(CHANGE_ADD_ZONE1.additions(), change.additions()); assertTrue(change.deletions().isEmpty()); - assertEquals("1", change.id()); + assertEquals("1", change.generatedId()); assertNull(change.startTimeMillis()); assertNull(change.status()); changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name(), @@ -733,7 +733,7 @@ public void testListChanges() { change = changes.get(2); assertTrue(change.additions().isEmpty()); assertNotNull(change.deletions()); - assertEquals("2", change.id()); + assertEquals("2", change.generatedId()); assertNull(change.startTimeMillis()); assertNull(change.status()); changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name(), @@ -742,7 +742,7 @@ public void testListChanges() { change = changes.get(1); assertTrue(change.additions().isEmpty()); assertTrue(change.deletions().isEmpty()); - assertEquals("1", change.id()); + assertEquals("1", change.generatedId()); assertNull(change.startTimeMillis()); assertNull(change.status()); changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name(), @@ -751,7 +751,7 @@ public void testListChanges() { change = changes.get(1); assertTrue(change.additions().isEmpty()); assertTrue(change.deletions().isEmpty()); - assertEquals("1", change.id()); + assertEquals("1", change.generatedId()); assertNotNull(change.startTimeMillis()); assertNull(change.status()); changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name(), @@ -760,7 +760,7 @@ public void testListChanges() { change = changes.get(1); assertTrue(change.additions().isEmpty()); assertTrue(change.deletions().isEmpty()); - assertEquals("1", change.id()); + assertEquals("1", change.generatedId()); assertNull(change.startTimeMillis()); assertEquals(ChangeRequest.Status.DONE, change.status()); } finally { @@ -773,45 +773,45 @@ public void testGetChange() { try { Zone zone = DNS.create(ZONE1, Dns.ZoneOption.fields(Dns.ZoneField.NAME)); ChangeRequest created = zone.applyChangeRequest(CHANGE_ADD_ZONE1); - ChangeRequest retrieved = DNS.getChangeRequest(zone.name(), created.id()); + ChangeRequest retrieved = DNS.getChangeRequest(zone.name(), created.generatedId()); assertEqChangesIgnoreStatus(created, retrieved); - waitForChangeToComplete(zone.name(), created.id()); + waitForChangeToComplete(zone.name(), created.generatedId()); zone.applyChangeRequest(CHANGE_DELETE_ZONE1); // with options created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID)); - retrieved = DNS.getChangeRequest(zone.name(), created.id(), + retrieved = DNS.getChangeRequest(zone.name(), created.generatedId(), Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID)); assertEqChangesIgnoreStatus(created, retrieved); - waitForChangeToComplete(zone.name(), created.id()); + waitForChangeToComplete(zone.name(), created.generatedId()); zone.applyChangeRequest(CHANGE_DELETE_ZONE1); created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); - retrieved = DNS.getChangeRequest(zone.name(), created.id(), + retrieved = DNS.getChangeRequest(zone.name(), created.generatedId(), Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); assertEqChangesIgnoreStatus(created, retrieved); - waitForChangeToComplete(zone.name(), created.id()); + waitForChangeToComplete(zone.name(), created.generatedId()); zone.applyChangeRequest(CHANGE_DELETE_ZONE1); created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); - retrieved = DNS.getChangeRequest(zone.name(), created.id(), + retrieved = DNS.getChangeRequest(zone.name(), created.generatedId(), Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); assertEqChangesIgnoreStatus(created, retrieved); - waitForChangeToComplete(zone.name(), created.id()); + waitForChangeToComplete(zone.name(), created.generatedId()); zone.applyChangeRequest(CHANGE_DELETE_ZONE1); created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS)); - retrieved = DNS.getChangeRequest(zone.name(), created.id(), + retrieved = DNS.getChangeRequest(zone.name(), created.generatedId(), Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS)); assertEqChangesIgnoreStatus(created, retrieved); - waitForChangeToComplete(zone.name(), created.id()); + waitForChangeToComplete(zone.name(), created.generatedId()); // finishes with delete otherwise we cannot delete the zone created = zone.applyChangeRequest(CHANGE_DELETE_ZONE1, Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS)); - retrieved = DNS.getChangeRequest(zone.name(), created.id(), + retrieved = DNS.getChangeRequest(zone.name(), created.generatedId(), Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS)); assertEqChangesIgnoreStatus(created, retrieved); - waitForChangeToComplete(zone.name(), created.id()); + waitForChangeToComplete(zone.name(), created.generatedId()); } finally { clear(); } @@ -904,7 +904,7 @@ public void testListDnsRecords() { assertEquals(1, ImmutableList.copyOf(recordSetPage.values().iterator()).size()); // test name filter ChangeRequest change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1); - waitForChangeToComplete(ZONE1.name(), change.id()); + waitForChangeToComplete(ZONE1.name(), change.generatedId()); recordSetIterator = DNS.listRecordSets(ZONE1.name(), Dns.RecordSetListOption.dnsName(A_RECORD_ZONE1.name())).iterateAll(); counter = 0; @@ -916,7 +916,7 @@ public void testListDnsRecords() { } assertEquals(2, counter); // test type filter - waitForChangeToComplete(ZONE1.name(), change.id()); + waitForChangeToComplete(ZONE1.name(), change.generatedId()); recordSetIterator = DNS.listRecordSets(ZONE1.name(), Dns.RecordSetListOption.dnsName(A_RECORD_ZONE1.name()), Dns.RecordSetListOption.type(A_RECORD_ZONE1.type())) @@ -956,7 +956,7 @@ public void testListDnsRecords() { assertEquals(400, ex.code()); assertFalse(ex.retryable()); } - waitForChangeToComplete(ZONE1.name(), change.id()); + waitForChangeToComplete(ZONE1.name(), change.generatedId()); } finally { clear(); } diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java index a9e5c5d25377..e5f0cf01a6c9 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java @@ -90,7 +90,7 @@ public void run(Dns dns, String... args) { ZoneInfo zoneInfo = ZoneInfo.of(zoneName, dnsName, description); Zone zone = dns.create(zoneInfo); System.out.printf("Successfully created zone with name %s which was assigned ID %s.%n", - zone.name(), zone.id()); + zone.name(), zone.generatedId()); } @Override @@ -213,7 +213,7 @@ public void run(Dns dns, String... args) { .build(); changeRequest = dns.applyChangeRequest(zoneName, changeRequest); System.out.printf("The request for deleting A record %s for zone %s was successfully " - + "submitted and assigned ID %s.%n", recordName, zoneName, changeRequest.id()); + + "submitted and assigned ID %s.%n", recordName, zoneName, changeRequest.generatedId()); System.out.print("Waiting for deletion to happen..."); waitForChangeToFinish(dns, zoneName, changeRequest); System.out.printf("%nThe deletion has been completed.%n"); @@ -258,7 +258,7 @@ public void run(Dns dns, String... args) { ChangeRequestInfo changeRequest = ChangeRequest.builder().add(recordSet).build(); changeRequest = dns.applyChangeRequest(zoneName, changeRequest); System.out.printf("The request for adding A record %s for zone %s was successfully " - + "submitted and assigned ID %s.%n", recordName, zoneName, changeRequest.id()); + + "submitted and assigned ID %s.%n", recordName, zoneName, changeRequest.generatedId()); System.out.print("Waiting for addition to happen..."); waitForChangeToFinish(dns, zoneName, changeRequest); System.out.printf("The addition has been completed.%n"); @@ -334,7 +334,7 @@ public void run(Dns dns, String... args) { System.out.printf("Change requests for zone %s:%n", zoneName); while (iterator.hasNext()) { ChangeRequest change = iterator.next(); - System.out.printf("%nID: %s%n", change.id()); + 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("Deletions: %s%n", Joiner.on(", ").join(change.deletions())); @@ -439,7 +439,7 @@ public boolean check(String... args) { private static void printZone(Zone zone) { System.out.printf("%nName: %s%n", zone.name()); - System.out.printf("ID: %s%n", zone.id()); + 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()))); System.out.printf("Name servers: %s%n", Joiner.on(", ").join(zone.nameServers())); @@ -455,7 +455,7 @@ private static ChangeRequestInfo waitForChangeToFinish(Dns dns, String zoneName, } catch (InterruptedException e) { System.err.println("Thread was interrupted while waiting."); } - current = dns.getChangeRequest(zoneName, current.id()); + current = dns.getChangeRequest(zoneName, current.generatedId()); } return current; } diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateZone.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateZone.java index 2c2ba211bd86..dcf8019319c3 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateZone.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateZone.java @@ -46,6 +46,6 @@ public static void main(String... args) { // Create zone in Google Cloud DNS Zone zone = dns.create(zoneInfo); - System.out.printf("Zone was created and assigned ID %s.%n", zone.id()); + System.out.printf("Zone was created and assigned ID %s.%n", zone.generatedId()); } } diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java index 63f26eeebb2a..aaf663920173 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java @@ -73,7 +73,7 @@ public static void main(String... args) { + "processed."); } // Update the change, but fetch only change ID and status - changeRequest = dns.getChangeRequest(zoneName, changeRequest.id(), option); + changeRequest = dns.getChangeRequest(zoneName, changeRequest.generatedId(), option); } } diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java index 9c9a9e77289c..50f3a1a5c951 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java @@ -51,7 +51,7 @@ public static void main(String... args) { // Create zone in Google Cloud DNS Zone zone = dns.create(zoneInfo); - System.out.printf("Zone was created and assigned ID %s.%n", zone.id()); + System.out.printf("Zone was created and assigned ID %s.%n", zone.generatedId()); // Print assigned name servers List nameServers = zone.nameServers(); @@ -89,7 +89,7 @@ public static void main(String... args) { } catch (InterruptedException e) { System.err.println("The thread was interrupted while waiting..."); } - changeRequest = dns.getChangeRequest(zone.name(), changeRequest.id()); + changeRequest = dns.getChangeRequest(zone.name(), changeRequest.generatedId()); } System.out.println("The change request has been applied."); @@ -143,7 +143,7 @@ public static void main(String... args) { } // Update the change, but fetch only change ID and status - changeRequest = dns.getChangeRequest(zoneName, changeRequest.id(), option); + changeRequest = dns.getChangeRequest(zoneName, changeRequest.generatedId(), option); } } From 883c60eff3f38413ca4187722233c599378e6de2 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Fri, 1 Apr 2016 13:43:05 -0700 Subject: [PATCH 49/60] rename QueryStage id to generatedId --- .../google/gcloud/bigquery/QueryStage.java | 26 +++++++++---------- .../gcloud/bigquery/JobStatisticsTest.java | 2 +- .../gcloud/bigquery/QueryStageTest.java | 6 ++--- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryStage.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryStage.java index 8c9f91fd39f3..9404fbf5d9ad 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryStage.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryStage.java @@ -137,7 +137,7 @@ static QueryStep fromPb(com.google.api.services.bigquery.model.ExplainQueryStep private final double computeRatioAvg; private final double computeRatioMax; - private final long id; + private final long generatedId; private final String name; private final double readRatioAvg; private final double readRatioMax; @@ -153,7 +153,7 @@ static final class Builder { private double computeRatioAvg; private double computeRatioMax; - private long id; + private long generatedId; private String name; private double readRatioAvg; private double readRatioMax; @@ -177,8 +177,8 @@ Builder computeRatioMax(double computeRatioMax) { return this; } - Builder id(long id) { - this.id = id; + Builder generatedId(long generatedId) { + this.generatedId = generatedId; return this; } @@ -240,7 +240,7 @@ QueryStage build() { QueryStage(Builder builder) { computeRatioAvg = builder.computeRatioAvg; computeRatioMax = builder.computeRatioMax; - id = builder.id; + generatedId = builder.generatedId; name = builder.name; readRatioAvg = builder.readRatioAvg; readRatioMax = builder.readRatioMax; @@ -270,10 +270,10 @@ public double computeRatioMax() { } /** - * Returns a unique ID for the stage within its plan. + * Returns a unique, server-generated ID for the stage within its plan. */ - public long id() { - return id; + public long generatedId() { + return generatedId; } /** @@ -357,7 +357,7 @@ public String toString() { return MoreObjects.toStringHelper(this) .add("computeRatioAvg", computeRatioAvg) .add("computeRatioMax", computeRatioMax) - .add("id", id) + .add("generatedId", generatedId) .add("name", name) .add("readRatioAvg", readRatioAvg) .add("readRatioMax", readRatioMax) @@ -373,7 +373,7 @@ public String toString() { @Override public int hashCode() { - return Objects.hash(computeRatioAvg, computeRatioMax, id, name, readRatioAvg, readRatioMax, + return Objects.hash(computeRatioAvg, computeRatioMax, generatedId, name, readRatioAvg, readRatioMax, recordsRead, recordsWritten, steps, waitRatioAvg, waitRatioMax, writeRatioAvg); } @@ -383,7 +383,7 @@ public boolean equals(Object obj) { return false; } QueryStage other = (QueryStage) obj; - return id == other.id + return generatedId == other.generatedId && computeRatioAvg == other.computeRatioAvg && computeRatioMax == other.computeRatioMax && readRatioAvg == other.readRatioAvg @@ -406,7 +406,7 @@ ExplainQueryStage toPb() { ExplainQueryStage stagePb = new ExplainQueryStage() .setComputeRatioAvg(computeRatioAvg) .setComputeRatioMax(computeRatioMax) - .setId(id) + .setId(generatedId) .setName(name) .setReadRatioAvg(readRatioAvg) .setReadRatioMax(readRatioMax) @@ -426,7 +426,7 @@ static QueryStage fromPb(com.google.api.services.bigquery.model.ExplainQueryStag Builder builder = new QueryStage.Builder(); builder.computeRatioAvg(stagePb.getComputeRatioAvg()); builder.computeRatioMax(stagePb.getComputeRatioMax()); - builder.id(stagePb.getId()); + builder.generatedId(stagePb.getId()); builder.name(stagePb.getName()); builder.readRatioAvg(stagePb.getReadRatioAvg()); builder.readRatioMax(stagePb.getReadRatioMax()); diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobStatisticsTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobStatisticsTest.java index 1ec67d034754..4a3940ba4543 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobStatisticsTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobStatisticsTest.java @@ -71,7 +71,7 @@ public class JobStatisticsTest { private static final QueryStage QUERY_STAGE = QueryStage.builder() .computeRatioAvg(1.1) .computeRatioMax(2.2) - .id(42L) + .generatedId(42L) .name("stage") .readRatioAvg(3.3) .readRatioMax(4.4) diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/QueryStageTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/QueryStageTest.java index 99a7c8096454..ac60967a8bee 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/QueryStageTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/QueryStageTest.java @@ -48,7 +48,7 @@ public class QueryStageTest { private static final QueryStage QUERY_STAGE = QueryStage.builder() .computeRatioAvg(COMPUTE_RATIO_AVG) .computeRatioMax(COMPUTE_RATIO_MAX) - .id(ID) + .generatedId(ID) .name(NAME) .readRatioAvg(READ_RATIO_AVG) .readRatioMax(READ_RATIO_MAX) @@ -73,7 +73,7 @@ public void testQueryStepConstructor() { public void testBuilder() { assertEquals(COMPUTE_RATIO_AVG, QUERY_STAGE.computeRatioAvg(), 0); assertEquals(COMPUTE_RATIO_MAX, QUERY_STAGE.computeRatioMax(), 0); - assertEquals(ID, QUERY_STAGE.id()); + assertEquals(ID, QUERY_STAGE.generatedId()); assertEquals(NAME, QUERY_STAGE.name()); assertEquals(READ_RATIO_AVG, QUERY_STAGE.readRatioAvg(), 0); assertEquals(READ_RATIO_MAX, QUERY_STAGE.readRatioMax(), 0); @@ -108,7 +108,7 @@ private void compareQueryStage(QueryStage expected, QueryStage value) { assertEquals(expected, value); assertEquals(expected.computeRatioAvg(), value.computeRatioAvg(), 0); assertEquals(expected.computeRatioMax(), value.computeRatioMax(), 0); - assertEquals(expected.id(), value.id()); + assertEquals(expected.generatedId(), value.generatedId()); assertEquals(expected.name(), value.name()); assertEquals(expected.readRatioAvg(), value.readRatioAvg(), 0); assertEquals(expected.readRatioMax(), value.readRatioMax(), 0); From 43b2ef990fd27944823722aaf6caee902f9d429c Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Fri, 1 Apr 2016 13:46:59 -0700 Subject: [PATCH 50/60] Release 0.1.7 --- gcloud-java-bigquery/pom.xml | 2 +- gcloud-java-contrib/pom.xml | 2 +- gcloud-java-core/pom.xml | 2 +- gcloud-java-datastore/pom.xml | 2 +- gcloud-java-dns/pom.xml | 2 +- gcloud-java-examples/pom.xml | 2 +- gcloud-java-resourcemanager/pom.xml | 2 +- gcloud-java-storage/pom.xml | 2 +- gcloud-java/pom.xml | 2 +- pom.xml | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/gcloud-java-bigquery/pom.xml b/gcloud-java-bigquery/pom.xml index 952e75899de7..867318ee6c3f 100644 --- a/gcloud-java-bigquery/pom.xml +++ b/gcloud-java-bigquery/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.7-SNAPSHOT + 0.1.7 gcloud-java-bigquery diff --git a/gcloud-java-contrib/pom.xml b/gcloud-java-contrib/pom.xml index c1a0eaad9008..aef99d78306e 100644 --- a/gcloud-java-contrib/pom.xml +++ b/gcloud-java-contrib/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.7-SNAPSHOT + 0.1.7 gcloud-java-contrib diff --git a/gcloud-java-core/pom.xml b/gcloud-java-core/pom.xml index 13e170e061ee..7d6b9c888788 100644 --- a/gcloud-java-core/pom.xml +++ b/gcloud-java-core/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.7-SNAPSHOT + 0.1.7 gcloud-java-core diff --git a/gcloud-java-datastore/pom.xml b/gcloud-java-datastore/pom.xml index 7110d8de3947..2f23e1fbb919 100644 --- a/gcloud-java-datastore/pom.xml +++ b/gcloud-java-datastore/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.7-SNAPSHOT + 0.1.7 gcloud-java-datastore diff --git a/gcloud-java-dns/pom.xml b/gcloud-java-dns/pom.xml index 66dc1e54752e..c36a0ffe39a6 100644 --- a/gcloud-java-dns/pom.xml +++ b/gcloud-java-dns/pom.xml @@ -13,7 +13,7 @@ com.google.gcloud gcloud-java-pom - 0.1.7-SNAPSHOT + 0.1.7 gcloud-java-dns diff --git a/gcloud-java-examples/pom.xml b/gcloud-java-examples/pom.xml index 15bb536916f4..948ee5c53005 100644 --- a/gcloud-java-examples/pom.xml +++ b/gcloud-java-examples/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.7-SNAPSHOT + 0.1.7 gcloud-java-examples diff --git a/gcloud-java-resourcemanager/pom.xml b/gcloud-java-resourcemanager/pom.xml index f8a2697c23b5..a50067519613 100644 --- a/gcloud-java-resourcemanager/pom.xml +++ b/gcloud-java-resourcemanager/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.7-SNAPSHOT + 0.1.7 gcloud-java-resourcemanager diff --git a/gcloud-java-storage/pom.xml b/gcloud-java-storage/pom.xml index 37f4d00991a3..80ad0e3ca4e7 100644 --- a/gcloud-java-storage/pom.xml +++ b/gcloud-java-storage/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.7-SNAPSHOT + 0.1.7 gcloud-java-storage diff --git a/gcloud-java/pom.xml b/gcloud-java/pom.xml index 2a1720fcd300..01aab1ff7121 100644 --- a/gcloud-java/pom.xml +++ b/gcloud-java/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.7-SNAPSHOT + 0.1.7 diff --git a/pom.xml b/pom.xml index 4c53e54560c2..67e71716cc0f 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.google.gcloud gcloud-java-pom pom - 0.1.7-SNAPSHOT + 0.1.7 GCloud Java https://github.com/GoogleCloudPlatform/gcloud-java From 9a8d15605d156f02030ee3e9950a452a19664a9c Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Fri, 1 Apr 2016 14:30:58 -0700 Subject: [PATCH 51/60] Remove flaky tests --- .../java/com/google/gcloud/bigquery/it/ITBigQueryTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/it/ITBigQueryTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/it/ITBigQueryTest.java index e712eeeb23fd..cc7f06a251db 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/it/ITBigQueryTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/it/ITBigQueryTest.java @@ -71,6 +71,7 @@ import org.junit.AfterClass; import org.junit.BeforeClass; +import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.rules.Timeout; @@ -623,6 +624,7 @@ public void testInsertAllWithErrors() { assertTrue(bigquery.delete(TableId.of(DATASET, tableName))); } + @Ignore("Flaky test; see issue #836") @Test public void testListAllTableData() { Page> rows = bigquery.listTableData(TABLE_ID); @@ -906,6 +908,7 @@ public void testCancelNonExistingJob() { assertFalse(bigquery.cancel("test_cancel_non_existing_job")); } + @Ignore("Flaky test; see #836") @Test public void testInsertFromFile() throws InterruptedException { String destinationTableName = "test_insert_from_file_table"; From df86a444de86aefbd5b9cbb198976badc8438052 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Fri, 1 Apr 2016 15:37:22 -0700 Subject: [PATCH 52/60] Fix endpoint --- .../java/com/google/gcloud/datastore/DatastoreOptions.java | 2 +- .../com/google/gcloud/datastore/spi/DefaultDatastoreRpc.java | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java index 81e4a36a3584..bc0efd837755 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java @@ -130,7 +130,7 @@ protected String defaultHost() { String host = System.getProperty( com.google.datastore.v1beta3.client.DatastoreHelper.LOCAL_HOST_ENV_VAR, System.getenv(com.google.datastore.v1beta3.client.DatastoreHelper.LOCAL_HOST_ENV_VAR)); - return host != null ? host : super.defaultHost(); + return host != null ? host : com.google.datastore.v1beta3.client.DatastoreFactory.DEFAULT_HOST; } @Override diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/spi/DefaultDatastoreRpc.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/spi/DefaultDatastoreRpc.java index 421ba95161f2..1fb0fa3a26c6 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/spi/DefaultDatastoreRpc.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/spi/DefaultDatastoreRpc.java @@ -54,8 +54,9 @@ public DefaultDatastoreRpc(DatastoreOptions options) { if (fullURL.charAt(fullURL.length() - 1) != '/') { fullURL = fullURL + '/'; } - fullURL = fullURL + "datastore/" - + com.google.datastore.v1beta3.client.DatastoreFactory.VERSION + "/projects/" + fullURL = fullURL + + com.google.datastore.v1beta3.client.DatastoreFactory.VERSION + + "/projects/" + options.projectId(); clientBuilder = clientBuilder.projectId(null).projectEndpoint(fullURL); } From 6f54be2ea7bfe9b7f715e5bc834aab00dcc1f30d Mon Sep 17 00:00:00 2001 From: travis-ci Date: Fri, 1 Apr 2016 23:24:02 +0000 Subject: [PATCH 53/60] Updating version in README files. [ci skip] --- README.md | 6 +++--- gcloud-java-bigquery/README.md | 6 +++--- gcloud-java-contrib/README.md | 6 +++--- gcloud-java-core/README.md | 6 +++--- gcloud-java-datastore/README.md | 6 +++--- gcloud-java-dns/README.md | 6 +++--- gcloud-java-examples/README.md | 6 +++--- gcloud-java-resourcemanager/README.md | 6 +++--- gcloud-java-storage/README.md | 6 +++--- gcloud-java/README.md | 6 +++--- 10 files changed, 30 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index d5604dddbbff..6708ba3d358d 100644 --- a/README.md +++ b/README.md @@ -30,16 +30,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java - 0.1.6 + 0.1.7 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java:0.1.6' +compile 'com.google.gcloud:gcloud-java:0.1.7' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java" % "0.1.6" +libraryDependencies += "com.google.gcloud" % "gcloud-java" % "0.1.7" ``` Example Applications diff --git a/gcloud-java-bigquery/README.md b/gcloud-java-bigquery/README.md index f81cccd5ad44..d300d3969c40 100644 --- a/gcloud-java-bigquery/README.md +++ b/gcloud-java-bigquery/README.md @@ -22,16 +22,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java-bigquery - 0.1.6 + 0.1.7 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java-bigquery:0.1.6' +compile 'com.google.gcloud:gcloud-java-bigquery:0.1.7' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java-bigquery" % "0.1.6" +libraryDependencies += "com.google.gcloud" % "gcloud-java-bigquery" % "0.1.7" ``` Example Application diff --git a/gcloud-java-contrib/README.md b/gcloud-java-contrib/README.md index b41467934690..f0ead8c79dea 100644 --- a/gcloud-java-contrib/README.md +++ b/gcloud-java-contrib/README.md @@ -16,16 +16,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java-contrib - 0.1.6 + 0.1.7 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java-contrib:0.1.6' +compile 'com.google.gcloud:gcloud-java-contrib:0.1.7' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java-contrib" % "0.1.6" +libraryDependencies += "com.google.gcloud" % "gcloud-java-contrib" % "0.1.7" ``` Java Versions diff --git a/gcloud-java-core/README.md b/gcloud-java-core/README.md index 1ee96b950471..067505179bc9 100644 --- a/gcloud-java-core/README.md +++ b/gcloud-java-core/README.md @@ -19,16 +19,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java-core - 0.1.6 + 0.1.7 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java-core:0.1.6' +compile 'com.google.gcloud:gcloud-java-core:0.1.7' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java-core" % "0.1.6" +libraryDependencies += "com.google.gcloud" % "gcloud-java-core" % "0.1.7" ``` Troubleshooting diff --git a/gcloud-java-datastore/README.md b/gcloud-java-datastore/README.md index 1025de79f63d..69cf039472a3 100644 --- a/gcloud-java-datastore/README.md +++ b/gcloud-java-datastore/README.md @@ -22,16 +22,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java-datastore - 0.1.6 + 0.1.7 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java-datastore:0.1.6' +compile 'com.google.gcloud:gcloud-java-datastore:0.1.7' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java-datastore" % "0.1.6" +libraryDependencies += "com.google.gcloud" % "gcloud-java-datastore" % "0.1.7" ``` Example Application diff --git a/gcloud-java-dns/README.md b/gcloud-java-dns/README.md index 703bcb190807..ae5e52e9291f 100644 --- a/gcloud-java-dns/README.md +++ b/gcloud-java-dns/README.md @@ -22,16 +22,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java-dns - 0.1.6 + 0.1.7 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java-dns:0.1.6' +compile 'com.google.gcloud:gcloud-java-dns:0.1.7' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java-dns" % "0.1.6" +libraryDependencies += "com.google.gcloud" % "gcloud-java-dns" % "0.1.7" ``` Example Application diff --git a/gcloud-java-examples/README.md b/gcloud-java-examples/README.md index 45658e37323e..12f25bbe480c 100644 --- a/gcloud-java-examples/README.md +++ b/gcloud-java-examples/README.md @@ -19,16 +19,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java-examples - 0.1.6 + 0.1.7 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java-examples:0.1.6' +compile 'com.google.gcloud:gcloud-java-examples:0.1.7' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java-examples" % "0.1.6" +libraryDependencies += "com.google.gcloud" % "gcloud-java-examples" % "0.1.7" ``` To run examples from your command line: diff --git a/gcloud-java-resourcemanager/README.md b/gcloud-java-resourcemanager/README.md index 9d142fc558ea..667e9222a402 100644 --- a/gcloud-java-resourcemanager/README.md +++ b/gcloud-java-resourcemanager/README.md @@ -22,16 +22,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java-resourcemanager - 0.1.6 + 0.1.7 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java-resourcemanager:0.1.6' +compile 'com.google.gcloud:gcloud-java-resourcemanager:0.1.7' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java-resourcemanager" % "0.1.6" +libraryDependencies += "com.google.gcloud" % "gcloud-java-resourcemanager" % "0.1.7" ``` Example Application diff --git a/gcloud-java-storage/README.md b/gcloud-java-storage/README.md index 962b90c82b34..1bd12d7846bb 100644 --- a/gcloud-java-storage/README.md +++ b/gcloud-java-storage/README.md @@ -22,16 +22,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java-storage - 0.1.6 + 0.1.7 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java-storage:0.1.6' +compile 'com.google.gcloud:gcloud-java-storage:0.1.7' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java-storage" % "0.1.6" +libraryDependencies += "com.google.gcloud" % "gcloud-java-storage" % "0.1.7" ``` Example Application diff --git a/gcloud-java/README.md b/gcloud-java/README.md index c898da5d5f72..36e82fb23a74 100644 --- a/gcloud-java/README.md +++ b/gcloud-java/README.md @@ -27,16 +27,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java - 0.1.6 + 0.1.7 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java:0.1.6' +compile 'com.google.gcloud:gcloud-java:0.1.7' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java" % "0.1.6" +libraryDependencies += "com.google.gcloud" % "gcloud-java" % "0.1.7" ``` Troubleshooting From 85d0188e1444252b62b507b67c63b8a130201cba Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Fri, 1 Apr 2016 17:05:26 -0700 Subject: [PATCH 54/60] Update to version 0.1.8-SNAPSHOT --- gcloud-java-bigquery/pom.xml | 2 +- gcloud-java-contrib/pom.xml | 2 +- gcloud-java-core/pom.xml | 2 +- gcloud-java-datastore/pom.xml | 2 +- gcloud-java-dns/pom.xml | 2 +- gcloud-java-examples/pom.xml | 2 +- gcloud-java-resourcemanager/pom.xml | 2 +- gcloud-java-storage/pom.xml | 2 +- gcloud-java/pom.xml | 2 +- pom.xml | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/gcloud-java-bigquery/pom.xml b/gcloud-java-bigquery/pom.xml index 867318ee6c3f..39b0d6fc4e37 100644 --- a/gcloud-java-bigquery/pom.xml +++ b/gcloud-java-bigquery/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.7 + 0.1.8-SNAPSHOT gcloud-java-bigquery diff --git a/gcloud-java-contrib/pom.xml b/gcloud-java-contrib/pom.xml index aef99d78306e..1ef855a1ae5d 100644 --- a/gcloud-java-contrib/pom.xml +++ b/gcloud-java-contrib/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.7 + 0.1.8-SNAPSHOT gcloud-java-contrib diff --git a/gcloud-java-core/pom.xml b/gcloud-java-core/pom.xml index 7d6b9c888788..df116b0b1be4 100644 --- a/gcloud-java-core/pom.xml +++ b/gcloud-java-core/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.7 + 0.1.8-SNAPSHOT gcloud-java-core diff --git a/gcloud-java-datastore/pom.xml b/gcloud-java-datastore/pom.xml index bbdc122a3772..22498ab505f4 100644 --- a/gcloud-java-datastore/pom.xml +++ b/gcloud-java-datastore/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.7 + 0.1.8-SNAPSHOT gcloud-java-datastore diff --git a/gcloud-java-dns/pom.xml b/gcloud-java-dns/pom.xml index c36a0ffe39a6..e7a02452d92f 100644 --- a/gcloud-java-dns/pom.xml +++ b/gcloud-java-dns/pom.xml @@ -13,7 +13,7 @@ com.google.gcloud gcloud-java-pom - 0.1.7 + 0.1.8-SNAPSHOT gcloud-java-dns diff --git a/gcloud-java-examples/pom.xml b/gcloud-java-examples/pom.xml index 948ee5c53005..f5d4cb290118 100644 --- a/gcloud-java-examples/pom.xml +++ b/gcloud-java-examples/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.7 + 0.1.8-SNAPSHOT gcloud-java-examples diff --git a/gcloud-java-resourcemanager/pom.xml b/gcloud-java-resourcemanager/pom.xml index a50067519613..d970bdf883da 100644 --- a/gcloud-java-resourcemanager/pom.xml +++ b/gcloud-java-resourcemanager/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.7 + 0.1.8-SNAPSHOT gcloud-java-resourcemanager diff --git a/gcloud-java-storage/pom.xml b/gcloud-java-storage/pom.xml index 80ad0e3ca4e7..2316706850d3 100644 --- a/gcloud-java-storage/pom.xml +++ b/gcloud-java-storage/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.7 + 0.1.8-SNAPSHOT gcloud-java-storage diff --git a/gcloud-java/pom.xml b/gcloud-java/pom.xml index 01aab1ff7121..9b765db419d7 100644 --- a/gcloud-java/pom.xml +++ b/gcloud-java/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.7 + 0.1.8-SNAPSHOT diff --git a/pom.xml b/pom.xml index 67e71716cc0f..42f9bd73cdfb 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.google.gcloud gcloud-java-pom pom - 0.1.7 + 0.1.8-SNAPSHOT GCloud Java https://github.com/GoogleCloudPlatform/gcloud-java From 59cdf37fed705df06669611a79487faf8d1af8f7 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Mon, 4 Apr 2016 17:41:05 +0200 Subject: [PATCH 55/60] Update ruby logo --- src/site/resources/img/icon-lang-ruby.svg | 38 +++++++++-------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/src/site/resources/img/icon-lang-ruby.svg b/src/site/resources/img/icon-lang-ruby.svg index acfaab8d6ea5..5f4e5a25d893 100644 --- a/src/site/resources/img/icon-lang-ruby.svg +++ b/src/site/resources/img/icon-lang-ruby.svg @@ -1,23 +1,15 @@ - - - - - - - - - - - - - - - - - - - - + + + + + + From d17e3253afaafaa9441df140d95fbca86a4051ef Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Mon, 4 Apr 2016 18:29:44 +0200 Subject: [PATCH 56/60] Extract projectId from GOOGLE_APPLICATION_CREDENTIALS if set * Extract projectId from GOOGLE_APPLICATION_CREDENTIALS if set * First look for AE's project id, then GOOGLE_APPLICATION_CREDENTIALS' --- README.md | 5 ++-- .../com/google/gcloud/ServiceOptions.java | 26 +++++++++++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6708ba3d358d..6bd1e7211f2c 100644 --- a/README.md +++ b/README.md @@ -84,8 +84,9 @@ Most `gcloud-java` libraries require a project ID. There are multiple ways to s 1. Project ID supplied when building the service options 2. Project ID specified by the environment variable `GCLOUD_PROJECT` 3. App Engine project ID -4. Google Cloud SDK project ID -5. Compute Engine project ID +4. Project ID specified in the JSON credentials file pointed by the `GOOGLE_APPLICATION_CREDENTIALS` environment variable +5. Google Cloud SDK project ID +6. Compute Engine project ID Authentication -------------- diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java index 1c5289bbcda9..d53cfcdafe24 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java @@ -30,8 +30,13 @@ import com.google.common.io.Files; import com.google.gcloud.spi.ServiceRpcFactory; +import org.json.JSONException; +import org.json.JSONObject; +import org.json.JSONTokener; + import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; @@ -378,7 +383,10 @@ protected String defaultHost() { protected String defaultProject() { String projectId = System.getProperty(PROJECT_ENV_NAME, System.getenv(PROJECT_ENV_NAME)); if (projectId == null) { - projectId = getAppEngineProjectId(); + projectId = appEngineProjectId(); + } + if (projectId == null) { + projectId = serviceAccountProjectId(); } return projectId != null ? projectId : googleCloudProjectId(); } @@ -461,7 +469,7 @@ private static boolean isWindows() { return System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows"); } - protected static String getAppEngineProjectId() { + protected static String appEngineProjectId() { try { Class factoryClass = Class.forName("com.google.appengine.api.appidentity.AppIdentityServiceFactory"); @@ -479,6 +487,20 @@ protected static String getAppEngineProjectId() { } } + protected static String serviceAccountProjectId() { + String project = null; + String credentialsPath = System.getenv("GOOGLE_APPLICATION_CREDENTIALS"); + if(credentialsPath != null) { + try (InputStream credentialsStream = new FileInputStream(credentialsPath)) { + JSONObject json = new JSONObject(new JSONTokener(credentialsStream)); + project = json.getString("project_id"); + } catch (IOException | JSONException ex) { + // ignore + } + } + return project; + } + @SuppressWarnings("unchecked") public ServiceT service() { if (service == null) { From 560d0b6b5df5f1d71731ddd2eb121750c1fa39b3 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Mon, 4 Apr 2016 16:23:42 -0400 Subject: [PATCH 57/60] Modified methods in ChangeRequest. * Modified methods in ChangeRequest. Added reload() and isDone() to change request. Also changed signature of applyTo(). --- gcloud-java-dns/README.md | 16 +++---- .../com/google/gcloud/dns/ChangeRequest.java | 38 ++++++++++++++-- .../main/java/com/google/gcloud/dns/Dns.java | 2 +- .../google/gcloud/dns/ChangeRequestTest.java | 41 ++++++++++++++++-- .../com/google/gcloud/dns/it/ITDnsTest.java | 43 ++++++++++--------- .../examples/dns/snippets/DeleteZone.java | 11 ++--- .../ManipulateZonesAndRecordSets.java | 11 ++--- 7 files changed, 107 insertions(+), 55 deletions(-) diff --git a/gcloud-java-dns/README.md b/gcloud-java-dns/README.md index ae5e52e9291f..0bb9a47635a2 100644 --- a/gcloud-java-dns/README.md +++ b/gcloud-java-dns/README.md @@ -212,7 +212,7 @@ while (recordSetIterator.hasNext()) { // Build and apply the change request to our zone ChangeRequestInfo changeRequest = changeBuilder.build(); -zone.applyChangeRequest(changeRequest); +ChangeRequest pendingRequest = zone.applyChangeRequest(changeRequest); ``` You can find more information about changes in the [Cloud DNS documentation] (https://cloud.google.com/dns/what-is-cloud-dns#cloud_dns_api_concepts). @@ -220,13 +220,12 @@ When the change request is applied, it is registered with the Cloud DNS service can wait for its completion as follows: ```java -while (ChangeRequestInfo.Status.PENDING.equals(changeRequest.status())) { +while (!pendingRequest.isDone()) { try { Thread.sleep(500L); } catch (InterruptedException e) { System.err.println("The thread was interrupted while waiting..."); } - changeRequest = dns.getChangeRequest(zone.name(), changeRequest.generatedId()); } System.out.println("The change request has been applied."); ``` @@ -300,12 +299,10 @@ while (recordIterator.hasNext()) { // Build and apply the change request to our zone if it contains records to delete ChangeRequestInfo changeRequest = changeBuilder.build(); if (!changeRequest.deletions().isEmpty()) { - changeRequest = dns.applyChangeRequest(zoneName, changeRequest); + ChangeRequest pendingRequest = dns.applyChangeRequest(zoneName, changeRequest); - // Wait for change to finish, but save data traffic by transferring only ID and status - Dns.ChangeRequestOption option = - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS); - while (ChangeRequestInfo.Status.PENDING.equals(changeRequest.status())) { + // Wait for the change request to complete + while (!pendingRequest.isDone()) { System.out.println("Waiting for change to complete. Going to sleep for 500ms..."); try { Thread.sleep(500); @@ -313,9 +310,6 @@ if (!changeRequest.deletions().isEmpty()) { System.err.println("The thread was interrupted while waiting for change request to be " + "processed."); } - - // Update the change, but fetch only change ID and status - changeRequest = dns.getChangeRequest(zoneName, changeRequest.generatedId(), option); } } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java index 60b053faab6d..66e841c20a15 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java @@ -148,10 +148,42 @@ public Dns dns() { } /** - * Applies this change request to the associated zone. + * Applies this change request to the zone identified by {@code zoneName}. + * + * @throws DnsException upon failure or if zone is not found */ - public ChangeRequest applyTo(Dns.ChangeRequestOption... options) { - return dns.applyChangeRequest(zone, this, options); + public ChangeRequest applyTo(String zoneName, Dns.ChangeRequestOption... options) { + return dns.applyChangeRequest(zoneName, this, options); + } + + /** + * Retrieves the up-to-date information about the change request from Google Cloud DNS. Parameter + * {@code options} can be used to restrict the fields to be included in the updated object the + * same way as in {@link Dns#getChangeRequest(String, String, Dns.ChangeRequestOption...)}. If + * {@code options} are provided, any field other than generatedId which is not included in the + * {@code options} will be {@code null} regardless of whether they are initialized or not in + * {@code this} instance. + * + * @return an object with the updated information or {@code null} if it does not exist + * @throws DnsException upon failure of the API call or if the associated zone was not found + */ + public ChangeRequest reload(Dns.ChangeRequestOption... options) { + return dns.getChangeRequest(zone, generatedId(), options); + } + + /** + * Returns {@code true} if the change request has been completed. If the status is not {@link + * Status#DONE} already, the method makes an API call to Google Cloud DNS to update the change + * request first. + * + * @throws DnsException upon failure of the API call or if the associated zone was not found + */ + public boolean isDone() { + if (status() == Status.DONE) { + return true; + } + ChangeRequest updated = reload(Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); + return updated == null || updated.status() == Status.DONE; } @Override 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 d95d11a97c19..a78eaef7c4e7 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 @@ -475,7 +475,7 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) { * servers. The fields to be returned can be selected by {@link ChangeRequestOption}s. * * @return the new {@link ChangeRequest} - * @throws DnsException upon failure if zone is not found + * @throws DnsException upon failure or if zone is not found * @see Cloud DNS Changes: create */ ChangeRequest applyChangeRequest(String zoneName, ChangeRequestInfo changeRequest, diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java index 4d4b2c01823c..2c3be337f033 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java @@ -43,12 +43,13 @@ public class ChangeRequestTest { private Dns dns; private ChangeRequest changeRequest; + private ChangeRequest changeRequestPending; private ChangeRequest changeRequestPartial; @Before public void setUp() throws Exception { dns = createStrictMock(Dns.class); - expect(dns.options()).andReturn(OPTIONS).times(2); + expect(dns.options()).andReturn(OPTIONS).times(3); replay(dns); changeRequest = new ChangeRequest(dns, ZONE_NAME, new ChangeRequestInfo.BuilderImpl( CHANGE_REQUEST_INFO.toBuilder() @@ -56,6 +57,12 @@ public void setUp() throws Exception { .generatedId("12") .status(ChangeRequest.Status.DONE) .build())); + changeRequestPending = new ChangeRequest(dns, ZONE_NAME, new ChangeRequestInfo.BuilderImpl( + CHANGE_REQUEST_INFO.toBuilder() + .startTimeMillis(132L) + .generatedId("12") + .status(ChangeRequest.Status.PENDING) + .build())); changeRequestPartial = new ChangeRequest(dns, ZONE_NAME, new ChangeRequest.BuilderImpl(CHANGE_REQUEST_INFO)); reset(dns); @@ -133,8 +140,34 @@ public void testApplyTo() { Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME))) .andReturn(changeRequest); replay(dns); - assertSame(changeRequest, changeRequest.applyTo()); - assertSame(changeRequest, - changeRequest.applyTo(Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME))); + assertSame(changeRequest, changeRequest.applyTo(ZONE_NAME)); + assertSame(changeRequest, changeRequest.applyTo(ZONE_NAME, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME))); + } + + @Test + public void testReload() { + expect(dns.getChangeRequest(ZONE_NAME, changeRequest.generatedId())).andReturn(changeRequest); + expect(dns.getChangeRequest(ZONE_NAME, changeRequest.generatedId(), + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME))) + .andReturn(changeRequest); + replay(dns); + assertSame(changeRequest, changeRequest.reload()); + assertSame(changeRequest, changeRequest.reload( + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME))); + } + + @Test + public void testIsDone() { + replay(dns); + assertTrue(changeRequest.isDone()); + verify(dns); + reset(dns); + expect(dns.getChangeRequest(ZONE_NAME, changeRequest.generatedId(), + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS))) + .andReturn(changeRequest); + replay(dns); + assertTrue(changeRequestPending.isDone()); + verify(dns); } } diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java index aa6c78461b8b..f25bd537cd72 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java @@ -147,12 +147,13 @@ private static void assertEqChangesIgnoreStatus(ChangeRequest expected, ChangeRe } private static void waitForChangeToComplete(String zoneName, String changeId) { - while (true) { - ChangeRequest changeRequest = DNS.getChangeRequest(zoneName, changeId, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); - if (ChangeRequest.Status.DONE.equals(changeRequest.status())) { - return; - } + ChangeRequest changeRequest = DNS.getChangeRequest(zoneName, changeId, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); + waitForChangeToComplete(changeRequest); + } + + private static void waitForChangeToComplete(ChangeRequest changeRequest) { + while (!changeRequest.isDone()) { try { Thread.sleep(500); } catch (InterruptedException e) { @@ -529,9 +530,9 @@ public void testCreateChange() { assertTrue(ImmutableList.of(ChangeRequest.Status.PENDING, ChangeRequest.Status.DONE) .contains(created.status())); assertEqChangesIgnoreStatus(created, DNS.getChangeRequest(ZONE1.name(), "1")); - waitForChangeToComplete(ZONE1.name(), "1"); - DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - waitForChangeToComplete(ZONE1.name(), "2"); + waitForChangeToComplete(created); + created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + waitForChangeToComplete(created); // with options created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID)); @@ -540,9 +541,9 @@ public void testCreateChange() { assertTrue(created.deletions().isEmpty()); assertEquals("3", created.generatedId()); assertNull(created.status()); - waitForChangeToComplete(ZONE1.name(), "3"); - DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - waitForChangeToComplete(ZONE1.name(), "4"); + waitForChangeToComplete(created); + created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + waitForChangeToComplete(created); created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); assertTrue(created.additions().isEmpty()); @@ -550,9 +551,9 @@ public void testCreateChange() { assertTrue(created.deletions().isEmpty()); assertEquals("5", created.generatedId()); assertNotNull(created.status()); - waitForChangeToComplete(ZONE1.name(), "5"); - DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - waitForChangeToComplete(ZONE1.name(), "6"); + waitForChangeToComplete(created); + created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + waitForChangeToComplete(created); created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); assertTrue(created.additions().isEmpty()); @@ -560,9 +561,9 @@ public void testCreateChange() { assertTrue(created.deletions().isEmpty()); assertEquals("7", created.generatedId()); assertNull(created.status()); - waitForChangeToComplete(ZONE1.name(), "7"); - DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - waitForChangeToComplete(ZONE1.name(), "8"); + waitForChangeToComplete(created); + created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + waitForChangeToComplete(created); created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS)); assertEquals(CHANGE_ADD_ZONE1.additions(), created.additions()); @@ -571,16 +572,16 @@ public void testCreateChange() { assertEquals("9", created.generatedId()); assertNull(created.status()); // finishes with delete otherwise we cannot delete the zone - waitForChangeToComplete(ZONE1.name(), "9"); + waitForChangeToComplete(created); created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1, Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS)); - waitForChangeToComplete(ZONE1.name(), "10"); + waitForChangeToComplete(created); assertEquals(CHANGE_DELETE_ZONE1.deletions(), created.deletions()); assertNull(created.startTimeMillis()); assertTrue(created.additions().isEmpty()); assertEquals("10", created.generatedId()); assertNull(created.status()); - waitForChangeToComplete(ZONE1.name(), "10"); + waitForChangeToComplete(created); } finally { clear(); } diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java index aaf663920173..c791ea8c0992 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java @@ -22,6 +22,7 @@ package com.google.gcloud.examples.dns.snippets; +import com.google.gcloud.dns.ChangeRequest; import com.google.gcloud.dns.ChangeRequestInfo; import com.google.gcloud.dns.Dns; import com.google.gcloud.dns.DnsOptions; @@ -59,12 +60,10 @@ public static void main(String... args) { // Build and apply the change request to our zone if it contains records to delete ChangeRequestInfo changeRequest = changeBuilder.build(); if (!changeRequest.deletions().isEmpty()) { - changeRequest = dns.applyChangeRequest(zoneName, changeRequest); + ChangeRequest pendingRequest = dns.applyChangeRequest(zoneName, changeRequest); - // Wait for change to finish, but save data traffic by transferring only ID and status - Dns.ChangeRequestOption option = - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS); - while (ChangeRequestInfo.Status.PENDING.equals(changeRequest.status())) { + // Wait for the change request to complete + while (!pendingRequest.isDone()) { System.out.println("Waiting for change to complete. Going to sleep for 500ms..."); try { Thread.sleep(500); @@ -72,8 +71,6 @@ public static void main(String... args) { System.err.println("The thread was interrupted while waiting for change request to be " + "processed."); } - // Update the change, but fetch only change ID and status - changeRequest = dns.getChangeRequest(zoneName, changeRequest.generatedId(), option); } } diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java index 50f3a1a5c951..c96c93191c10 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java @@ -128,12 +128,10 @@ public static void main(String... args) { // Build and apply the change request to our zone if it contains records to delete changeRequest = changeBuilder.build(); if (!changeRequest.deletions().isEmpty()) { - changeRequest = dns.applyChangeRequest(zoneName, changeRequest); + ChangeRequest pendingRequest = dns.applyChangeRequest(zoneName, changeRequest); - // Wait for change to finish, but save data traffic by transferring only ID and status - Dns.ChangeRequestOption option = - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS); - while (ChangeRequest.Status.PENDING.equals(changeRequest.status())) { + // Wait for the change request to complete + while (!pendingRequest.isDone()) { System.out.println("Waiting for change to complete. Going to sleep for 500ms..."); try { Thread.sleep(500); @@ -141,9 +139,6 @@ public static void main(String... args) { System.err.println("The thread was interrupted while waiting for change request to be " + "processed."); } - - // Update the change, but fetch only change ID and status - changeRequest = dns.getChangeRequest(zoneName, changeRequest.generatedId(), option); } } From 25ccf0868ed807f7159de8ba0e401620d332cc19 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 4 Apr 2016 20:28:33 -0700 Subject: [PATCH 58/60] Fix Storage API activation instructions --- gcloud-java-examples/README.md | 2 +- gcloud-java-storage/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gcloud-java-examples/README.md b/gcloud-java-examples/README.md index 12f25bbe480c..fc6b7c28f636 100644 --- a/gcloud-java-examples/README.md +++ b/gcloud-java-examples/README.md @@ -98,7 +98,7 @@ To run examples from your command line: * Here's an example run of `StorageExample`. - Before running the example, go to the [Google Developers Console][developers-console] to ensure that Google Cloud Storage API is enabled and that you have a bucket. Also ensure that you have a test file (`test.txt` is chosen here) to upload to Cloud Storage stored locally on your machine. + Before running the example, go to the [Google Developers Console][developers-console] to ensure that "Google Cloud Storage" and "Google Cloud Storage JSON API" are enabled and that you have a bucket. Also ensure that you have a test file (`test.txt` is chosen here) to upload to Cloud Storage stored locally on your machine. ``` mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.storage.StorageExample" -Dexec.args="upload /path/to/test.txt " mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.storage.StorageExample" -Dexec.args="list " diff --git a/gcloud-java-storage/README.md b/gcloud-java-storage/README.md index 1bd12d7846bb..5a8897356db8 100644 --- a/gcloud-java-storage/README.md +++ b/gcloud-java-storage/README.md @@ -61,7 +61,7 @@ with the Cloud Storage using this Client Library. Getting Started --------------- #### Prerequisites -For this tutorial, you will need a [Google Developers Console](https://console.developers.google.com/) project with the Storage JSON API enabled. You will need to [enable billing](https://support.google.com/cloud/answer/6158867?hl=en) to use Google Cloud Storage. [Follow these instructions](https://cloud.google.com/docs/authentication#preparation) to get your project set up. You will also need to set up the local development environment by [installing the Google Cloud SDK](https://cloud.google.com/sdk/) and running the following commands in command line: `gcloud auth login` and `gcloud config set project [YOUR PROJECT ID]`. +For this tutorial, you will need a [Google Developers Console](https://console.developers.google.com/) project with "Google Cloud Storage" and "Google Cloud Storage JSON API" enabled via the console's API Manager. You will need to [enable billing](https://support.google.com/cloud/answer/6158867?hl=en) to use Google Cloud Storage. [Follow these instructions](https://cloud.google.com/docs/authentication#preparation) to get your project set up. You will also need to set up the local development environment by [installing the Google Cloud SDK](https://cloud.google.com/sdk/) and running the following commands in command line: `gcloud auth login` and `gcloud config set project [YOUR PROJECT ID]`. #### Installation and setup You'll need to obtain the `gcloud-java-storage` library. See the [Quickstart](#quickstart) section to add `gcloud-java-storage` as a dependency in your code. From 3e7069a1e4af6f7c7efbc7a71e5ccbb148387863 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Tue, 5 Apr 2016 11:29:50 -0400 Subject: [PATCH 59/60] Addressed codacy issues in DNS. * Addressed codacy issues in DNS. This includes - 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. * Fixed some other minor issues: - added joda formatter to example - style in ZoneTest - visibility in LocalDnsHelper - added private constructor to OptionParsers - adjusted pom.xml * Unified imports of model objects. --- gcloud-java-dns/pom.xml | 1 - .../com/google/gcloud/dns/ChangeRequest.java | 2 +- .../main/java/com/google/gcloud/dns/Dns.java | 1 - .../java/com/google/gcloud/dns/DnsImpl.java | 61 +++++++++---------- .../com/google/gcloud/dns/ProjectInfo.java | 10 +-- .../java/com/google/gcloud/dns/RecordSet.java | 7 +-- .../main/java/com/google/gcloud/dns/Zone.java | 7 ++- .../java/com/google/gcloud/dns/ZoneInfo.java | 9 +-- .../gcloud/dns/testing/LocalDnsHelper.java | 8 +-- .../gcloud/dns/testing/OptionParsers.java | 3 + .../gcloud/dns/ChangeRequestInfoTest.java | 3 +- .../com/google/gcloud/dns/DnsImplTest.java | 4 +- .../com/google/gcloud/dns/RecordSetTest.java | 25 ++++---- .../com/google/gcloud/dns/ZoneInfoTest.java | 5 +- .../java/com/google/gcloud/dns/ZoneTest.java | 2 +- .../com/google/gcloud/dns/it/ITDnsTest.java | 8 +-- .../dns/testing/LocalDnsHelperTest.java | 7 --- .../gcloud/examples/dns/DnsExample.java | 13 ++-- 18 files changed, 85 insertions(+), 91 deletions(-) diff --git a/gcloud-java-dns/pom.xml b/gcloud-java-dns/pom.xml index e7a02452d92f..a4928ccf0397 100644 --- a/gcloud-java-dns/pom.xml +++ b/gcloud-java-dns/pom.xml @@ -3,7 +3,6 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.google.gcloud gcloud-java-dns jar GCloud Java DNS diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java index 66e841c20a15..b3a2a2170d8b 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java @@ -221,7 +221,7 @@ static ChangeRequest fromPb(Dns dns, String zoneName, Change pb) { static Function fromPbFunction(final Dns dns, final String zoneName) { return new Function() { @Override - public ChangeRequest apply(com.google.api.services.dns.model.Change pb) { + public ChangeRequest apply(Change pb) { return ChangeRequest.fromPb(dns, zoneName, pb); } }; 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 a78eaef7c4e7..2216733ca779 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.Service; import com.google.gcloud.dns.spi.DnsRpc; -import java.io.Serializable; import java.util.List; /** diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java index 9f4fa2a9d9d1..3218daa543b2 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java @@ -17,11 +17,11 @@ 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; import com.google.api.services.dns.model.ManagedZone; +import com.google.api.services.dns.model.Project; import com.google.api.services.dns.model.ResourceRecordSet; import com.google.common.base.Function; import com.google.common.collect.ImmutableList; @@ -121,8 +121,7 @@ private static Page listZones(final DnsOptions serviceOptions, // this differs from the other list operations since zone is functional and requires dns service Function pbToZoneFunction = new Function() { @Override - public Zone apply( - com.google.api.services.dns.model.ManagedZone zonePb) { + public Zone apply(ManagedZone zonePb) { return Zone.fromPb(serviceOptions.service(), zonePb); } }; @@ -142,7 +141,7 @@ public DnsRpc.ListResult call() { ? ImmutableList.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); } } @@ -169,10 +168,10 @@ public DnsRpc.ListResult call() { Iterable changes = result.results() == null ? ImmutableList.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); } } @@ -201,7 +200,7 @@ public DnsRpc.ListResult 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); } } @@ -210,10 +209,10 @@ public DnsRpc.ListResult call() { public Zone create(final ZoneInfo zoneInfo, Dns.ZoneOption... options) { final Map optionsMap = optionMap(options); try { - com.google.api.services.dns.model.ManagedZone answer = runWithRetries( - new Callable() { + ManagedZone answer = runWithRetries( + new Callable() { @Override - public com.google.api.services.dns.model.ManagedZone call() { + public ManagedZone call() { return dnsRpc.create(zoneInfo.toPb(), optionsMap); } }, options().retryParams(), EXCEPTION_HANDLER); @@ -227,10 +226,10 @@ public com.google.api.services.dns.model.ManagedZone call() { public Zone getZone(final String zoneName, Dns.ZoneOption... options) { final Map optionsMap = optionMap(options); try { - com.google.api.services.dns.model.ManagedZone answer = runWithRetries( - new Callable() { + ManagedZone answer = runWithRetries( + new Callable() { @Override - public com.google.api.services.dns.model.ManagedZone call() { + public ManagedZone call() { return dnsRpc.getZone(zoneName, optionsMap); } }, options().retryParams(), EXCEPTION_HANDLER); @@ -258,10 +257,10 @@ public Boolean call() { public ProjectInfo getProject(Dns.ProjectOption... fields) { final Map optionsMap = optionMap(fields); try { - com.google.api.services.dns.model.Project answer = runWithRetries( - new Callable() { + Project answer = runWithRetries( + new Callable() { @Override - public com.google.api.services.dns.model.Project call() { + public Project call() { return dnsRpc.getProject(optionsMap); } }, options().retryParams(), EXCEPTION_HANDLER); @@ -276,14 +275,13 @@ public ChangeRequest applyChangeRequest(final String zoneName, final ChangeRequestInfo changeRequest, ChangeRequestOption... options) { final Map optionsMap = optionMap(options); try { - com.google.api.services.dns.model.Change answer = - runWithRetries( - new Callable() { - @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() { + @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); @@ -295,14 +293,13 @@ public ChangeRequest getChangeRequest(final String zoneName, final String change Dns.ChangeRequestOption... options) { final Map optionsMap = optionMap(options); try { - com.google.api.services.dns.model.Change answer = - runWithRetries( - new Callable() { - @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() { + @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); diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ProjectInfo.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ProjectInfo.java index 319f06ad2444..52edb25ec902 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ProjectInfo.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ProjectInfo.java @@ -18,6 +18,7 @@ import static com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull; +import com.google.api.services.dns.model.Project; import com.google.common.base.MoreObjects; import java.io.Serializable; @@ -143,14 +144,13 @@ com.google.api.services.dns.model.Quota toPb() { } static Quota fromPb(com.google.api.services.dns.model.Quota pb) { - Quota quota = new Quota(pb.getManagedZones(), + return new Quota(pb.getManagedZones(), pb.getResourceRecordsPerRrset(), pb.getRrsetAdditionsPerChange(), pb.getRrsetDeletionsPerChange(), pb.getRrsetsPerManagedZone(), pb.getTotalRrdataSizePerChange() ); - return quota; } @Override @@ -243,8 +243,8 @@ String id() { return id; } - com.google.api.services.dns.model.Project toPb() { - com.google.api.services.dns.model.Project pb = new com.google.api.services.dns.model.Project(); + Project toPb() { + Project pb = new Project(); pb.setId(id); pb.setNumber(number); if (this.quota != null) { @@ -253,7 +253,7 @@ com.google.api.services.dns.model.Project toPb() { return pb; } - static ProjectInfo fromPb(com.google.api.services.dns.model.Project pb) { + static ProjectInfo fromPb(Project pb) { Builder builder = builder(); if (pb.getId() != null) { builder.id(pb.getId()); diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/RecordSet.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/RecordSet.java index dc6d956406c3..90beedba1d89 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/RecordSet.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/RecordSet.java @@ -285,9 +285,8 @@ 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()); @@ -295,7 +294,7 @@ com.google.api.services.dns.model.ResourceRecordSet toPb() { 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()); 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 6e22bfc6785a..c4250bd206eb 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 @@ -18,6 +18,7 @@ import static com.google.common.base.Preconditions.checkNotNull; +import com.google.api.services.dns.model.ManagedZone; import com.google.gcloud.Page; import java.io.IOException; @@ -205,12 +206,12 @@ 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(); } - static Zone fromPb(Dns dns, com.google.api.services.dns.model.ManagedZone zone) { + static Zone fromPb(Dns dns, ManagedZone zone) { ZoneInfo info = ZoneInfo.fromPb(zone); return new Zone(dns, new ZoneInfo.BuilderImpl(info)); } 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 a0ee24bc5948..03c23a6ce3d3 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 @@ -18,6 +18,7 @@ import static com.google.common.base.Preconditions.checkNotNull; +import com.google.api.services.dns.model.ManagedZone; import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; @@ -248,9 +249,9 @@ public Builder toBuilder() { return new BuilderImpl(this); } - com.google.api.services.dns.model.ManagedZone toPb() { - com.google.api.services.dns.model.ManagedZone pb = - new com.google.api.services.dns.model.ManagedZone(); + ManagedZone toPb() { + ManagedZone pb = + new ManagedZone(); pb.setDescription(this.description()); pb.setDnsName(this.dnsName()); if (this.generatedId() != null) { @@ -267,7 +268,7 @@ com.google.api.services.dns.model.ManagedZone toPb() { return pb; } - static ZoneInfo fromPb(com.google.api.services.dns.model.ManagedZone pb) { + static ZoneInfo fromPb(ManagedZone pb) { Builder builder = new BuilderImpl(pb.getName()); if (pb.getDescription() != null) { builder.description(pb.getDescription()); diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/LocalDnsHelper.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/LocalDnsHelper.java index 0ae2c37b9b4d..54edcddb5ade 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/LocalDnsHelper.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/LocalDnsHelper.java @@ -122,7 +122,7 @@ public class LocalDnsHelper { } } - private long delayChange; + private final long delayChange; private final HttpServer server; private final int port; @@ -140,8 +140,8 @@ private enum CallRegex { PROJECT_GET("GET", CONTEXT + "/[^/]+"), RECORD_LIST("GET", CONTEXT + "/[^/]+/managedZones/[^/]+/rrsets"); - private String method; - private String pathRegex; + private final String method; + private final String pathRegex; CallRegex(String method, String pathRegex) { this.pathRegex = pathRegex; @@ -382,7 +382,7 @@ ConcurrentSkipListMap projects() { } /** - * Creates new {@link LocalDnsHelper} instance that listens to requests on the local machine. This + * Creates new {@code LocalDnsHelper} instance that listens to requests on the local machine. This * instance processes changes in separate thread. The parameter determines how long a thread * should wait before processing a change. If it is set to 0, the threading is turned off and the * mock will behave synchronously. diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/OptionParsers.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/OptionParsers.java index 578a0b52db3d..bd773a931ef2 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/OptionParsers.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/OptionParsers.java @@ -29,6 +29,9 @@ */ class OptionParsers { + private OptionParsers() { + } + static Map parseListZonesOptions(String query) { Map options = new HashMap<>(); if (query != null) { diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestInfoTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestInfoTest.java index f3ae7a586391..136a6e0d56ec 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestInfoTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestInfoTest.java @@ -23,6 +23,7 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import com.google.api.services.dns.model.Change; import com.google.common.collect.ImmutableList; import org.junit.Test; @@ -209,7 +210,7 @@ public void testRemoveDeletion() { @Test public void testDateParsing() { String startTime = "2016-01-26T18:33:43.512Z"; // obtained from service - com.google.api.services.dns.model.Change change = CHANGE.toPb().setStartTime(startTime); + Change change = CHANGE.toPb().setStartTime(startTime); ChangeRequestInfo converted = ChangeRequest.fromPb(change); assertNotNull(converted.startTimeMillis()); assertEquals(change, converted.toPb()); diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java index d9d657ee03e7..c359871b998c 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java @@ -21,6 +21,7 @@ import com.google.api.services.dns.model.Change; import com.google.api.services.dns.model.ManagedZone; +import com.google.api.services.dns.model.ResourceRecordSet; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; @@ -69,8 +70,7 @@ public class DnsImplTest { CHANGE_REQUEST_PARTIAL.toPb())); private static final DnsRpc.ListResult LIST_RESULT_OF_PB_ZONES = DnsRpc.ListResult.of("cursor", ImmutableList.of(ZONE_INFO.toPb())); - private static final DnsRpc.ListResult - LIST_OF_PB_DNS_RECORDS = + private static final DnsRpc.ListResult LIST_OF_PB_DNS_RECORDS = DnsRpc.ListResult.of("cursor", ImmutableList.of(DNS_RECORD1.toPb(), DNS_RECORD2.toPb())); // Field options diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/RecordSetTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/RecordSetTest.java index 369e078a48c7..fc888bf8a697 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/RecordSetTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/RecordSetTest.java @@ -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; @@ -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()); @@ -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 @@ -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()); } 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 d0a64a61b816..b537735c3c4e 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 @@ -21,6 +21,7 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import com.google.api.services.dns.model.ManagedZone; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; @@ -155,10 +156,10 @@ public void testEmptyNameServers() { @Test public void testDateParsing() { - com.google.api.services.dns.model.ManagedZone pb = INFO.toPb(); + ManagedZone pb = INFO.toPb(); pb.setCreationTime("2016-01-19T18:00:12.854Z"); // a real value obtained from Google Cloud DNS ZoneInfo mz = ZoneInfo.fromPb(pb); // parses the string timestamp to millis - com.google.api.services.dns.model.ManagedZone pbClone = mz.toPb(); // converts it back to string + ManagedZone pbClone = mz.toPb(); // converts it back to string assertEquals(pb, pbClone); assertEquals(pb.getCreationTime(), pbClone.getCreationTime()); } 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 f14addfc44b6..5407d4f248b7 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 @@ -349,7 +349,7 @@ public void getChangedWhichDoesNotExistZoneFound() { assertNull(zoneNoId.getChangeRequest(CHANGE_REQUEST.generatedId())); assertNull(zone.getChangeRequest(CHANGE_REQUEST.generatedId())); assertNull( - zoneNoId.getChangeRequest(CHANGE_REQUEST.generatedId(),CHANGE_REQUEST_FIELD_OPTIONS)); + zoneNoId.getChangeRequest(CHANGE_REQUEST.generatedId(), CHANGE_REQUEST_FIELD_OPTIONS)); assertNull(zone.getChangeRequest(CHANGE_REQUEST.generatedId(), CHANGE_REQUEST_FIELD_OPTIONS)); } diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java index f25bd537cd72..e6513451e7f6 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java @@ -87,6 +87,9 @@ public class ITDnsTest { .build(); private static final List 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) { @@ -162,9 +165,6 @@ private static void waitForChangeToComplete(ChangeRequest changeRequest) { } } - @Rule - public Timeout globalTimeout = Timeout.seconds(300); - @Test public void testCreateValidZone() { try { @@ -472,7 +472,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()), diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/testing/LocalDnsHelperTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/testing/LocalDnsHelperTest.java index 44516f47c657..315a47c5bb7c 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/testing/LocalDnsHelperTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/testing/LocalDnsHelperTest.java @@ -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); diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java index e5f0cf01a6c9..d0cda485e9f5 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java @@ -27,10 +27,10 @@ import com.google.gcloud.dns.Zone; import com.google.gcloud.dns.ZoneInfo; -import java.text.DateFormat; -import java.text.SimpleDateFormat; +import org.joda.time.format.DateTimeFormat; +import org.joda.time.format.DateTimeFormatter; + import java.util.Arrays; -import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Map; @@ -67,7 +67,8 @@ public class DnsExample { private static final Map ACTIONS = new HashMap<>(); - private static final DateFormat FORMATTER = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss"); + private static final DateTimeFormatter FORMATTER = + DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss"); private interface DnsAction { void run(Dns dns, String... args); @@ -336,7 +337,7 @@ public void run(Dns dns, String... args) { 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.print(change.startTimeMillis())); System.out.printf("Deletions: %s%n", Joiner.on(", ").join(change.deletions())); System.out.printf("Additions: %s%n", Joiner.on(", ").join(change.additions())); } @@ -441,7 +442,7 @@ 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()))); + System.out.printf("Created: %s%n", FORMATTER.print(zone.creationTimeMillis())); System.out.printf("Name servers: %s%n", Joiner.on(", ").join(zone.nameServers())); } From 33a81a0433d45e1ca2e203a1be9d93b27b28fc74 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Tue, 5 Apr 2016 11:21:13 +0200 Subject: [PATCH 60/60] Job.isDone() returns true if the job does not exist --- .../main/java/com/google/gcloud/bigquery/Job.java | 15 ++++++--------- .../java/com/google/gcloud/bigquery/JobTest.java | 2 +- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Job.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Job.java index 586395a0f879..7f8237473b15 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Job.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Job.java @@ -128,22 +128,19 @@ public boolean exists() { /** * Checks if this job has completed its execution, either failing or succeeding. If the job does - * not exist this method returns {@code false}. To correctly wait for job's completion check that - * the job exists first, using {@link #exists()}: + * not exist this method returns {@code true}. You can wait for job completion with: *
     {@code
    -   * if (job.exists()) {
    -   *   while(!job.isDone()) {
    -   *     Thread.sleep(1000L);
    -   *   }
    +   * while(!job.isDone()) {
    +   *   Thread.sleep(1000L);
        * }}
    * - * @return {@code true} if this job is in {@link JobStatus.State#DONE} state, {@code false} if the - * state is not {@link JobStatus.State#DONE} or the job does not exist + * @return {@code true} if this job is in {@link JobStatus.State#DONE} state or if it does not + * exist, {@code false} if the state is not {@link JobStatus.State#DONE} * @throws BigQueryException upon failure */ public boolean isDone() { Job job = bigquery.getJob(jobId(), BigQuery.JobOption.fields(BigQuery.JobField.STATUS)); - return job != null && job.status().state() == JobStatus.State.DONE; + return job == null || job.status().state() == JobStatus.State.DONE; } /** diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobTest.java index e066bbed2fb4..61c9c521196a 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobTest.java @@ -172,7 +172,7 @@ public void testIsDone_NotExists() throws Exception { expect(bigquery.getJob(JOB_INFO.jobId(), expectedOptions)).andReturn(null); replay(bigquery); initializeJob(); - assertFalse(job.isDone()); + assertTrue(job.isDone()); } @Test