A simple usage example: *
{@code - * DatastoreServiceOptions options = - * DatastoreServiceOptions.builder().projectId(PROJECT_ID).build(); - * DatastoreService datastore = DatastoreServiceFactory.instance().get(options); + * DatastoreOptions options = DatastoreOptions.builder().projectId(PROJECT_ID).build(); + * Datastore datastore = DatastoreFactory.instance().get(options); * KeyFactory keyFactory = datastore.newKeyFactory().kind(kind); * Key key = keyFactory.newKey(keyName); * Entity entity = datastore.get(key); diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DatastoreRpcFactory.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DatastoreRpcFactory.java index 952114788f01..1815dda30f5d 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DatastoreRpcFactory.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DatastoreRpcFactory.java @@ -16,13 +16,13 @@ package com.google.gcloud.spi; -import com.google.gcloud.datastore.DatastoreServiceOptions; +import com.google.gcloud.datastore.DatastoreOptions; /** * An interface for Datastore RPC factory. * Implementation will be loaded via {@link java.util.ServiceLoader}. */ public interface DatastoreRpcFactory extends - ServiceRpcFactory{ + ServiceRpcFactory { } 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 3d909d368bc0..2f245260b325 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 @@ -33,7 +33,7 @@ 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.DatastoreServiceOptions; +import com.google.gcloud.datastore.DatastoreOptions; import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException.Reason; import org.json.JSONException; @@ -61,7 +61,7 @@ public class DefaultDatastoreRpc implements DatastoreRpc { HTTP_STATUS_TO_REASON = ImmutableMap.copyOf(httpCodes); } - public DefaultDatastoreRpc(DatastoreServiceOptions options) { + public DefaultDatastoreRpc(DatastoreOptions options) { client = DatastoreFactory.get().create( new Builder() .dataset(options.projectId()) 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 71576036fe3b..c3f1bfbd5a71 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 @@ -1 +1 @@ -/* * 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.easymock.EasyMock.*; import static org.junit.Assert.assertEquals; import com.google.api.services.datastore.DatastoreV1; import com.google.common.collect.ImmutableList; import org.easymock.EasyMock; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.util.List; public class BaseDatastoreBatchWriterTest { private static final Key KEY1 = Key.builder("dataset1", "kind1", "name1").build(); private static final Key KEY2 = Key.builder(KEY1, 1).build(); private static final Key KEY3 = Key.builder(KEY1, 2).build(); private static final IncompleteKey INCOMPLETE_KEY = IncompleteKey.builder(KEY1).build(); private static final Entity ENTITY1 = Entity.builder(KEY1).build(); private static final Entity ENTITY2 = Entity.builder(KEY2).set("bak", true).build(); private static final Entity ENTITY3 = Entity.builder(KEY3).set("bak", true).build(); private static final FullEntity INCOMPLETE_ENTITY_1 = Entity.builder(INCOMPLETE_KEY).build(); private static final FullEntity INCOMPLETE_ENTITY_2 = Entity.builder(INCOMPLETE_KEY).set("name", "dan").build(); private DatastoreBatchWriter batchWriter; private class DatastoreBatchWriter extends BaseDatastoreBatchWriter { private final DatastoreService datastore; protected DatastoreBatchWriter() { super("test"); datastore = EasyMock.createMock(DatastoreService.class); IncompleteKey[] expected = {INCOMPLETE_KEY, INCOMPLETE_KEY}; List result = ImmutableList.of(KEY2, KEY3); expect(datastore.allocateId(expected)).andReturn(result).times(0, 1); replay(datastore); } @Override protected DatastoreService datastore() { return datastore; } void finish() { verify(datastore); } } @Before public void setUp() { batchWriter = new DatastoreBatchWriter(); } @After public void tearDown() { batchWriter.finish(); } @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 entities = batchWriter .add(ENTITY1, INCOMPLETE_ENTITY_1, INCOMPLETE_ENTITY_2, entity2); assertEquals(pb, batchWriter.toMutationPb().build()); 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(); batchWriter.delete(KEY1); batchWriter.add(ENTITY1); assertEquals(pb, batchWriter.toMutationPb().build()); } @Test(expected = DatastoreServiceException.class) public void testAddDuplicate() throws Exception { batchWriter.add(ENTITY1); batchWriter.add(ENTITY1); } @Test(expected = DatastoreServiceException.class) public void testAddAfterPut() throws Exception { batchWriter.put(ENTITY1); batchWriter.add(ENTITY1); } @Test(expected = DatastoreServiceException.class) public void testAddAfterUpdate() throws Exception { batchWriter.update(ENTITY1); batchWriter.add(ENTITY1); } @Test(expected = DatastoreServiceException.class) public void testAddWhenNotActive() throws Exception { batchWriter.deactivate(); batchWriter.add(ENTITY1); } @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(); batchWriter.addWithDeferredIdAllocation(ENTITY1, INCOMPLETE_ENTITY_1); batchWriter.addWithDeferredIdAllocation(INCOMPLETE_ENTITY_2); assertEquals(pb, batchWriter.toMutationPb().build()); } @Test(expected = DatastoreServiceException.class) public void testAddWithDeferredAllocationWhenNotActive() throws Exception { batchWriter.deactivate(); batchWriter.addWithDeferredIdAllocation(INCOMPLETE_ENTITY_1); } @Test public void testUpdate() throws Exception { DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() .addUpdate(ENTITY1.toPb()) .addUpdate(ENTITY2.toPb()) .addUpdate(ENTITY3.toPb()) .build(); batchWriter.update(ENTITY1, ENTITY2); batchWriter.update(ENTITY3); assertEquals(pb, batchWriter.toMutationPb().build()); } @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(); batchWriter.update(ENTITY1); batchWriter.update(entity); assertEquals(pb, batchWriter.toMutationPb().build()); } @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(); batchWriter.add(ENTITY1); batchWriter.update(entity); assertEquals(pb, batchWriter.toMutationPb().build()); } @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(); batchWriter.put(ENTITY1); batchWriter.update(entity); assertEquals(pb, batchWriter.toMutationPb().build()); } @Test(expected = DatastoreServiceException.class) public void testUpdateAfterDelete() throws Exception { batchWriter.delete(KEY1); batchWriter.update(ENTITY1, ENTITY2); } @Test(expected = DatastoreServiceException.class) public void testUpdateWhenNotActive() throws Exception { batchWriter.deactivate(); batchWriter.update(ENTITY1); } @Test public void testPut() throws Exception { DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() .addUpsert(ENTITY1.toPb()) .addUpsert(ENTITY2.toPb()) .addUpsert(ENTITY3.toPb()) .build(); batchWriter.put(ENTITY1, ENTITY2); batchWriter.put(ENTITY3); assertEquals(pb, batchWriter.toMutationPb().build()); } @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(); batchWriter.put(ENTITY1); batchWriter.put(entity); assertEquals(pb, batchWriter.toMutationPb().build()); } @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(); batchWriter.add(ENTITY1); batchWriter.put(entity); assertEquals(pb, batchWriter.toMutationPb().build()); } @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(); batchWriter.update(ENTITY1); batchWriter.put(entity); assertEquals(pb, batchWriter.toMutationPb().build()); } @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(); batchWriter.delete(KEY1); batchWriter.put(entity); assertEquals(pb, batchWriter.toMutationPb().build()); } @Test(expected = DatastoreServiceException.class) public void testPutWhenNotActive() throws Exception { batchWriter.deactivate(); batchWriter.put(ENTITY1); } @Test public void testDelete() throws Exception { DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() .addDelete(KEY1.toPb()) .addDelete(KEY2.toPb()) .addDelete(KEY3.toPb()) .build(); batchWriter.delete(KEY1, KEY2); batchWriter.delete(KEY3); assertEquals(pb, batchWriter.toMutationPb().build()); } @Test public void testDeleteAfterAdd() throws Exception { DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() .addInsertAutoId(INCOMPLETE_ENTITY_1.toPb()) .addDelete(KEY1.toPb()) .build(); batchWriter.add(ENTITY1); batchWriter.addWithDeferredIdAllocation(INCOMPLETE_ENTITY_1); batchWriter.delete(KEY1); assertEquals(pb, batchWriter.toMutationPb().build()); } @Test public void testDeleteAfterUpdate() throws Exception { DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() .addDelete(KEY1.toPb()) .build(); batchWriter.update(ENTITY1); batchWriter.delete(KEY1); assertEquals(pb, batchWriter.toMutationPb().build()); } @Test public void testDeleteAfterPut() throws Exception { DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() .addDelete(KEY1.toPb()) .build(); batchWriter.put(ENTITY1); batchWriter.delete(KEY1); assertEquals(pb, batchWriter.toMutationPb().build()); } @Test(expected = DatastoreServiceException.class) public void testDeleteWhenNotActive() throws Exception { batchWriter.deactivate(); batchWriter.delete(KEY1); } } \ No newline at end of file +/* * 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.easymock.EasyMock.*; import static org.junit.Assert.assertEquals; import com.google.api.services.datastore.DatastoreV1; import com.google.common.collect.ImmutableList; import org.easymock.EasyMock; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.util.List; public class BaseDatastoreBatchWriterTest { private static final Key KEY1 = Key.builder("dataset1", "kind1", "name1").build(); private static final Key KEY2 = Key.builder(KEY1, 1).build(); private static final Key KEY3 = Key.builder(KEY1, 2).build(); private static final IncompleteKey INCOMPLETE_KEY = IncompleteKey.builder(KEY1).build(); private static final Entity ENTITY1 = Entity.builder(KEY1).build(); private static final Entity ENTITY2 = Entity.builder(KEY2).set("bak", true).build(); private static final Entity ENTITY3 = Entity.builder(KEY3).set("bak", true).build(); private static final FullEntity INCOMPLETE_ENTITY_1 = Entity.builder(INCOMPLETE_KEY).build(); private static final FullEntity INCOMPLETE_ENTITY_2 = Entity.builder(INCOMPLETE_KEY).set("name", "dan").build(); private DatastoreBatchWriter batchWriter; private class DatastoreBatchWriter extends BaseDatastoreBatchWriter { private final Datastore datastore; protected DatastoreBatchWriter() { super("test"); datastore = EasyMock.createMock(Datastore.class); IncompleteKey[] expected = {INCOMPLETE_KEY, INCOMPLETE_KEY}; List result = ImmutableList.of(KEY2, KEY3); expect(datastore.allocateId(expected)).andReturn(result).times(0, 1); replay(datastore); } @Override protected Datastore datastore() { return datastore; } void finish() { verify(datastore); } } @Before public void setUp() { batchWriter = new DatastoreBatchWriter(); } @After public void tearDown() { batchWriter.finish(); } @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 entities = batchWriter .add(ENTITY1, INCOMPLETE_ENTITY_1, INCOMPLETE_ENTITY_2, entity2); assertEquals(pb, batchWriter.toMutationPb().build()); 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(); batchWriter.delete(KEY1); batchWriter.add(ENTITY1); assertEquals(pb, batchWriter.toMutationPb().build()); } @Test(expected = DatastoreException.class) public void testAddDuplicate() throws Exception { batchWriter.add(ENTITY1); batchWriter.add(ENTITY1); } @Test(expected = DatastoreException.class) public void testAddAfterPut() throws Exception { batchWriter.put(ENTITY1); batchWriter.add(ENTITY1); } @Test(expected = DatastoreException.class) public void testAddAfterUpdate() throws Exception { batchWriter.update(ENTITY1); batchWriter.add(ENTITY1); } @Test(expected = DatastoreException.class) public void testAddWhenNotActive() throws Exception { batchWriter.deactivate(); batchWriter.add(ENTITY1); } @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(); batchWriter.addWithDeferredIdAllocation(ENTITY1, INCOMPLETE_ENTITY_1); batchWriter.addWithDeferredIdAllocation(INCOMPLETE_ENTITY_2); assertEquals(pb, batchWriter.toMutationPb().build()); } @Test(expected = DatastoreException.class) public void testAddWithDeferredAllocationWhenNotActive() throws Exception { batchWriter.deactivate(); batchWriter.addWithDeferredIdAllocation(INCOMPLETE_ENTITY_1); } @Test public void testUpdate() throws Exception { DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() .addUpdate(ENTITY1.toPb()) .addUpdate(ENTITY2.toPb()) .addUpdate(ENTITY3.toPb()) .build(); batchWriter.update(ENTITY1, ENTITY2); batchWriter.update(ENTITY3); assertEquals(pb, batchWriter.toMutationPb().build()); } @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(); batchWriter.update(ENTITY1); batchWriter.update(entity); assertEquals(pb, batchWriter.toMutationPb().build()); } @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(); batchWriter.add(ENTITY1); batchWriter.update(entity); assertEquals(pb, batchWriter.toMutationPb().build()); } @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(); batchWriter.put(ENTITY1); batchWriter.update(entity); assertEquals(pb, batchWriter.toMutationPb().build()); } @Test(expected = DatastoreException.class) public void testUpdateAfterDelete() throws Exception { batchWriter.delete(KEY1); batchWriter.update(ENTITY1, ENTITY2); } @Test(expected = DatastoreException.class) public void testUpdateWhenNotActive() throws Exception { batchWriter.deactivate(); batchWriter.update(ENTITY1); } @Test public void testPut() throws Exception { DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() .addUpsert(ENTITY1.toPb()) .addUpsert(ENTITY2.toPb()) .addUpsert(ENTITY3.toPb()) .build(); batchWriter.put(ENTITY1, ENTITY2); batchWriter.put(ENTITY3); assertEquals(pb, batchWriter.toMutationPb().build()); } @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(); batchWriter.put(ENTITY1); batchWriter.put(entity); assertEquals(pb, batchWriter.toMutationPb().build()); } @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(); batchWriter.add(ENTITY1); batchWriter.put(entity); assertEquals(pb, batchWriter.toMutationPb().build()); } @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(); batchWriter.update(ENTITY1); batchWriter.put(entity); assertEquals(pb, batchWriter.toMutationPb().build()); } @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(); batchWriter.delete(KEY1); batchWriter.put(entity); assertEquals(pb, batchWriter.toMutationPb().build()); } @Test(expected = DatastoreException.class) public void testPutWhenNotActive() throws Exception { batchWriter.deactivate(); batchWriter.put(ENTITY1); } @Test public void testDelete() throws Exception { DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() .addDelete(KEY1.toPb()) .addDelete(KEY2.toPb()) .addDelete(KEY3.toPb()) .build(); batchWriter.delete(KEY1, KEY2); batchWriter.delete(KEY3); assertEquals(pb, batchWriter.toMutationPb().build()); } @Test public void testDeleteAfterAdd() throws Exception { DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() .addInsertAutoId(INCOMPLETE_ENTITY_1.toPb()) .addDelete(KEY1.toPb()) .build(); batchWriter.add(ENTITY1); batchWriter.addWithDeferredIdAllocation(INCOMPLETE_ENTITY_1); batchWriter.delete(KEY1); assertEquals(pb, batchWriter.toMutationPb().build()); } @Test public void testDeleteAfterUpdate() throws Exception { DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() .addDelete(KEY1.toPb()) .build(); batchWriter.update(ENTITY1); batchWriter.delete(KEY1); assertEquals(pb, batchWriter.toMutationPb().build()); } @Test public void testDeleteAfterPut() throws Exception { DatastoreV1.Mutation pb = DatastoreV1.Mutation.newBuilder() .addDelete(KEY1.toPb()) .build(); batchWriter.put(ENTITY1); batchWriter.delete(KEY1); assertEquals(pb, batchWriter.toMutationPb().build()); } @Test(expected = DatastoreException.class) public void testDeleteWhenNotActive() throws Exception { batchWriter.deactivate(); batchWriter.delete(KEY1); } } \ No newline at end of file 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 567e795a66e7..daa0c502d4b5 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 @@ -1 +1 @@ -/* * 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 com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import org.junit.Before; import org.junit.Test; import java.util.Calendar; import java.util.Collections; import java.util.List; import java.util.Set; 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 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(); private static final FullEntity PARTIAL_ENTITY = Entity.builder(INCOMPLETE_KEY).build(); private Builder builder; private class Builder extends BaseEntity.Builder { @Override public BaseEntity build() { return new BaseEntity(this) { @Override protected Builder emptyBuilder() { return new BaseEntityTest.Builder(); } }; } } @Before 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("partialEntity", PARTIAL_ENTITY).set("stringValue", StringValue.of("bla")); builder.set("list1", NullValue.of(), StringValue.of("foo")); builder.set("list2", ImmutableList.of(LongValue.of(10), DoubleValue.of(2))); builder.set("list3", Collections.singletonList(BooleanValue.of(true))); } @Test public void testContains() throws Exception { BaseEntity entity = builder.build(); assertTrue(entity.contains("list1")); assertFalse(entity.contains("bla")); entity = builder.clear().build(); assertFalse(entity.contains("list1")); } @Test public void testGetValue() throws Exception { BaseEntity entity = builder.build(); assertEquals(BlobValue.of(BLOB), entity.getValue("blob")); } @Test(expected = DatastoreServiceException.class) public void testGetValueNotFound() throws Exception { BaseEntity entity = builder.clear().build(); entity.getValue("blob"); } @Test public void testIsNull() throws Exception { BaseEntity entity = builder.build(); assertTrue(entity.isNull("null")); assertFalse(entity.isNull("blob")); entity = builder.setNull("blob").build(); assertTrue(entity.isNull("blob")); } @Test(expected = DatastoreServiceException.class) public void testIsNullNotFound() throws Exception { BaseEntity entity = builder.clear().build(); entity.isNull("null"); } @Test public void testGetString() throws Exception { BaseEntity entity = builder.build(); assertEquals("hello world", entity.getString("string")); assertEquals("bla", entity.getString("stringValue")); entity = builder.set("string", "foo").build(); assertEquals("foo", entity.getString("string")); } @Test public void testGetLong() throws Exception { BaseEntity entity = builder.build(); assertEquals(125, entity.getLong("long")); entity = builder.set("long", LongValue.of(10)).build(); assertEquals(10, entity.getLong("long")); } @Test public void testGetDouble() throws Exception { BaseEntity entity = builder.build(); assertEquals(1.25, entity.getDouble("double"), 0); entity = builder.set("double", DoubleValue.of(10)).build(); assertEquals(10, entity.getDouble("double"), 0); } @Test public void testGetBoolean() throws Exception { BaseEntity entity = builder.build(); assertTrue(entity.getBoolean("boolean")); entity = builder.set("boolean", BooleanValue.of(false)).build(); assertFalse(entity.getBoolean("boolean")); } @Test public void testGetDateTime() throws Exception { BaseEntity entity = builder.build(); assertEquals(DATE_TIME, entity.getDateTime("dateTime")); Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1); DateTime dateTime = DateTime.copyFrom(cal); entity = builder.set("dateTime", DateTimeValue.of(dateTime)).build(); assertEquals(dateTime, entity.getDateTime("dateTime")); } @Test public void testGetKey() throws Exception { BaseEntity entity = builder.build(); assertEquals(KEY, entity.getKey("key")); Key key = Key.builder(KEY).name("BLA").build(); entity = builder.set("key", key).build(); assertEquals(key, entity.getKey("key")); } @Test public void testGetEntity() throws Exception { BaseEntity entity = builder.build(); assertEquals(ENTITY, entity.getEntity("entity")); assertEquals(PARTIAL_ENTITY, entity.getEntity("partialEntity")); entity = builder.set("entity", EntityValue.of(PARTIAL_ENTITY)).build(); assertEquals(PARTIAL_ENTITY, entity.getEntity("entity")); } @Test public void testGetList() throws Exception { BaseEntity entity = builder.build(); List extends Value>> list = entity.getList("list1"); assertEquals(2, list.size()); assertEquals(NullValue.of(), list.get(0)); assertEquals("foo", list.get(1).get()); list = entity.getList("list2"); assertEquals(2, list.size()); assertEquals(Long.valueOf(10), list.get(0).get()); assertEquals(Double.valueOf(2), list.get(1).get()); list = entity.getList("list3"); assertEquals(1, list.size()); assertEquals(Boolean.TRUE, list.get(0).get()); entity = builder.set("list1", ListValue.of(list)).build(); assertEquals(list, entity.getList("list1")); } @Test public void testGetBlob() throws Exception { BaseEntity entity = builder.build(); assertEquals(BLOB, entity.getBlob("blob")); Blob blob = Blob.copyFrom(new byte[] {}); entity = builder.set("blob", BlobValue.of(blob)).build(); assertEquals(blob, entity.getBlob("blob")); } @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") .build(); BaseEntity entity = builder.build(); assertEquals(names, entity.names()); } } \ No newline at end of file +/* * 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 com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import org.junit.Before; import org.junit.Test; import java.util.Calendar; import java.util.Collections; import java.util.List; import java.util.Set; 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 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(); private static final FullEntity PARTIAL_ENTITY = Entity.builder(INCOMPLETE_KEY).build(); private Builder builder; private class Builder extends BaseEntity.Builder { @Override public BaseEntity build() { return new BaseEntity(this) { @Override protected Builder emptyBuilder() { return new BaseEntityTest.Builder(); } }; } } @Before 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("partialEntity", PARTIAL_ENTITY).set("stringValue", StringValue.of("bla")); builder.set("list1", NullValue.of(), StringValue.of("foo")); builder.set("list2", ImmutableList.of(LongValue.of(10), DoubleValue.of(2))); builder.set("list3", Collections.singletonList(BooleanValue.of(true))); } @Test public void testContains() throws Exception { BaseEntity entity = builder.build(); assertTrue(entity.contains("list1")); assertFalse(entity.contains("bla")); entity = builder.clear().build(); assertFalse(entity.contains("list1")); } @Test public void testGetValue() throws Exception { BaseEntity entity = builder.build(); assertEquals(BlobValue.of(BLOB), entity.getValue("blob")); } @Test(expected = DatastoreException.class) public void testGetValueNotFound() throws Exception { BaseEntity entity = builder.clear().build(); entity.getValue("blob"); } @Test public void testIsNull() throws Exception { BaseEntity entity = builder.build(); assertTrue(entity.isNull("null")); assertFalse(entity.isNull("blob")); entity = builder.setNull("blob").build(); assertTrue(entity.isNull("blob")); } @Test(expected = DatastoreException.class) public void testIsNullNotFound() throws Exception { BaseEntity entity = builder.clear().build(); entity.isNull("null"); } @Test public void testGetString() throws Exception { BaseEntity entity = builder.build(); assertEquals("hello world", entity.getString("string")); assertEquals("bla", entity.getString("stringValue")); entity = builder.set("string", "foo").build(); assertEquals("foo", entity.getString("string")); } @Test public void testGetLong() throws Exception { BaseEntity entity = builder.build(); assertEquals(125, entity.getLong("long")); entity = builder.set("long", LongValue.of(10)).build(); assertEquals(10, entity.getLong("long")); } @Test public void testGetDouble() throws Exception { BaseEntity entity = builder.build(); assertEquals(1.25, entity.getDouble("double"), 0); entity = builder.set("double", DoubleValue.of(10)).build(); assertEquals(10, entity.getDouble("double"), 0); } @Test public void testGetBoolean() throws Exception { BaseEntity entity = builder.build(); assertTrue(entity.getBoolean("boolean")); entity = builder.set("boolean", BooleanValue.of(false)).build(); assertFalse(entity.getBoolean("boolean")); } @Test public void testGetDateTime() throws Exception { BaseEntity entity = builder.build(); assertEquals(DATE_TIME, entity.getDateTime("dateTime")); Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1); DateTime dateTime = DateTime.copyFrom(cal); entity = builder.set("dateTime", DateTimeValue.of(dateTime)).build(); assertEquals(dateTime, entity.getDateTime("dateTime")); } @Test public void testGetKey() throws Exception { BaseEntity entity = builder.build(); assertEquals(KEY, entity.getKey("key")); Key key = Key.builder(KEY).name("BLA").build(); entity = builder.set("key", key).build(); assertEquals(key, entity.getKey("key")); } @Test public void testGetEntity() throws Exception { BaseEntity entity = builder.build(); assertEquals(ENTITY, entity.getEntity("entity")); assertEquals(PARTIAL_ENTITY, entity.getEntity("partialEntity")); entity = builder.set("entity", EntityValue.of(PARTIAL_ENTITY)).build(); assertEquals(PARTIAL_ENTITY, entity.getEntity("entity")); } @Test public void testGetList() throws Exception { BaseEntity entity = builder.build(); List extends Value>> list = entity.getList("list1"); assertEquals(2, list.size()); assertEquals(NullValue.of(), list.get(0)); assertEquals("foo", list.get(1).get()); list = entity.getList("list2"); assertEquals(2, list.size()); assertEquals(Long.valueOf(10), list.get(0).get()); assertEquals(Double.valueOf(2), list.get(1).get()); list = entity.getList("list3"); assertEquals(1, list.size()); assertEquals(Boolean.TRUE, list.get(0).get()); entity = builder.set("list1", ListValue.of(list)).build(); assertEquals(list, entity.getList("list1")); } @Test public void testGetBlob() throws Exception { BaseEntity entity = builder.build(); assertEquals(BLOB, entity.getBlob("blob")); Blob blob = Blob.copyFrom(new byte[] {}); entity = builder.set("blob", BlobValue.of(blob)).build(); assertEquals(blob, entity.getBlob("blob")); } @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") .build(); BaseEntity entity = builder.build(); assertEquals(names, entity.names()); } } \ No newline at end of file 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 new file mode 100644 index 000000000000..1dd0f255ceca --- /dev/null +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java @@ -0,0 +1 @@ +/* * 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.fail; import com.google.gcloud.datastore.DatastoreException.Code; import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException; import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException.Reason; import org.junit.Test; public class DatastoreExceptionTest { @Test public void testCode() throws Exception { for (Reason reason : Reason.values()) { Code code = Code.valueOf(reason.name()); assertEquals(reason.retryable(), code.retryable()); assertEquals(reason.description(), code.description()); assertEquals(reason.httpStatus(), code.httpStatus()); } DatastoreException exception = new DatastoreException(Code.ABORTED, "bla"); assertEquals(Code.ABORTED, exception.code()); } @Test public void testTranslateAndThrow() throws Exception { for (Reason reason : Reason.values()) { try { DatastoreException.translateAndThrow(new DatastoreRpcException(reason)); fail("Exception expected"); } catch (DatastoreException ex) { assertEquals(reason.name(), ex.code().name()); } } } @Test public void testThrowInvalidRequest() throws Exception { try { DatastoreException.throwInvalidRequest("message %s %d", "a", 1); fail("Exception expected"); } catch (DatastoreException ex) { assertEquals(Code.FAILED_PRECONDITION, ex.code()); assertEquals("message a 1", ex.getMessage()); } } } \ No newline at end of file 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 8a31ece583ff..55c8d0cf3ce6 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 @@ -28,7 +28,7 @@ import static org.junit.Assert.fail; import com.google.common.collect.Iterators; -import com.google.gcloud.datastore.DatastoreService.TransactionCallable; +import com.google.gcloud.datastore.Datastore.TransactionCallable; import org.easymock.EasyMock; import org.junit.Test; @@ -40,7 +40,7 @@ public class DatastoreHelperTest { @Test public void testNewKeyFactory() { - DatastoreServiceOptions options = createMock(DatastoreServiceOptions.class); + DatastoreOptions options = createMock(DatastoreOptions.class); expect(options.projectId()).andReturn("ds1").once(); expect(options.namespace()).andReturn("ns1").once(); replay(options); @@ -55,73 +55,73 @@ public void testNewKeyFactory() { @Test public void testAllocateId() throws Exception { - DatastoreService datastoreService = createStrictMock(DatastoreService.class); + Datastore datastore = createStrictMock(Datastore.class); IncompleteKey pKey1 = IncompleteKey.builder("ds", "k").build(); Key key1 = Key.builder(pKey1, 1).build(); - expect(datastoreService.allocateId(new IncompleteKey[] {pKey1})) + expect(datastore.allocateId(new IncompleteKey[] {pKey1})) .andReturn(Collections.singletonList(key1)); - replay(datastoreService); - assertEquals(key1, DatastoreHelper.allocateId(datastoreService, pKey1)); - verify(datastoreService); + replay(datastore); + assertEquals(key1, DatastoreHelper.allocateId(datastore, pKey1)); + verify(datastore); } @Test public void testGet() throws Exception { - DatastoreService datastoreService = createStrictMock(DatastoreService.class); + 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(datastoreService.get(new Key[]{key1})) + expect(datastore.get(new Key[]{key1})) .andReturn(Collections.singletonList(entity1).iterator()); - expect(datastoreService.get(new Key[]{key2})) + expect(datastore.get(new Key[]{key2})) .andReturn(Collections. emptyIterator()); - replay(datastoreService); - assertEquals(entity1, DatastoreHelper.get(datastoreService, key1)); - assertNull(DatastoreHelper.get(datastoreService, key2)); - verify(datastoreService); + replay(datastore); + assertEquals(entity1, DatastoreHelper.get(datastore, key1)); + assertNull(DatastoreHelper.get(datastore, key2)); + verify(datastore); } @Test public void testAdd() throws Exception { - DatastoreService datastoreService = createStrictMock(DatastoreService.class); + 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(datastoreService.add(new Entity[]{entity})) + expect(datastore.add(new Entity[]{entity})) .andReturn(Collections.singletonList(entity)); - replay(datastoreService); - assertEquals(entity, DatastoreHelper.add(datastoreService, entity)); - verify(datastoreService); + replay(datastore); + assertEquals(entity, DatastoreHelper.add(datastore, entity)); + verify(datastore); } @Test public void testFetch() throws Exception { - DatastoreService datastoreService = createStrictMock(DatastoreService.class); + 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(datastoreService.get(key1, key2)).andReturn(Iterators.forArray(entity1, entity2)).once(); - replay(datastoreService); - List values = DatastoreHelper.fetch(datastoreService, key1, key2); + expect(datastore.get(key1, key2)).andReturn(Iterators.forArray(entity1, entity2)).once(); + replay(datastore); + List values = DatastoreHelper.fetch(datastore, key1, key2); assertEquals(2, values.size()); assertEquals(entity1, values.get(0)); assertEquals(entity2, values.get(1)); - verify(datastoreService); + verify(datastore); } @Test public void testRunInTransaction() throws Exception { - final DatastoreService datastoreService = createStrictMock(DatastoreService.class); + final Datastore datastore = createStrictMock(Datastore.class); final Transaction transaction = createStrictMock(Transaction.class); - expect(datastoreService.newTransaction()).andReturn(transaction).once(); + expect(datastore.newTransaction()).andReturn(transaction).once(); expect(transaction.active()).andReturn(true).once(); expect(transaction.commit()).andReturn(null).once(); expect(transaction.active()).andReturn(false).once(); - replay(datastoreService, transaction); - String value = DatastoreHelper.runInTransaction(datastoreService, + replay(datastore, transaction); + String value = DatastoreHelper.runInTransaction(datastore, new TransactionCallable () { @Override public String run(DatastoreReaderWriter readerWriter) { @@ -130,22 +130,22 @@ public String run(DatastoreReaderWriter readerWriter) { return "done"; } }); - verify(datastoreService, transaction); + verify(datastore, transaction); assertEquals("done", value); } @Test public void testRunInTransactionWithException() throws Exception { - final DatastoreService datastoreService = createStrictMock(DatastoreService.class); + final Datastore datastore = createStrictMock(Datastore.class); final Transaction transaction = createStrictMock(Transaction.class); - expect(datastoreService.newTransaction()).andReturn(transaction).once(); + expect(datastore.newTransaction()).andReturn(transaction).once(); expect(transaction.active()).andReturn(true).once(); transaction.rollback(); EasyMock.expectLastCall().once(); expect(transaction.active()).andReturn(false).once(); - replay(datastoreService, transaction); + replay(datastore, transaction); try { - DatastoreHelper.runInTransaction(datastoreService, new TransactionCallable () { + DatastoreHelper.runInTransaction(datastore, new TransactionCallable () { @Override public Void run(DatastoreReaderWriter readerWriter) throws Exception { assertTrue(transaction.active()); @@ -153,10 +153,10 @@ public Void run(DatastoreReaderWriter readerWriter) throws Exception { throw new Exception("Bla"); } }); - fail("DatastoreServiceException was expected"); - } catch (DatastoreServiceException ex) { + fail("DatastoreException was expected"); + } catch (DatastoreException ex) { assertEquals("Bla", ex.getCause().getMessage()); } - verify(datastoreService, transaction); + verify(datastore, transaction); } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreServiceOptionsTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java similarity index 90% rename from gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreServiceOptionsTest.java rename to gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java index 59468be58129..e7dc71c50ff6 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreServiceOptionsTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java @@ -31,23 +31,23 @@ import java.io.IOException; -public class DatastoreServiceOptionsTest { +public class DatastoreOptionsTest { private static final String PROJECT_ID = "project_id"; private DatastoreRpcFactory datastoreRpcFactory; private DatastoreRpc datastoreRpc; - private DatastoreServiceOptions.Builder options; + private DatastoreOptions.Builder options; @Before public void setUp() throws IOException, InterruptedException { datastoreRpcFactory = EasyMock.createMock(DatastoreRpcFactory.class); datastoreRpc = EasyMock.createMock(DatastoreRpc.class); - options = DatastoreServiceOptions.builder() + options = DatastoreOptions.builder() .normalizeDataset(false) .serviceRpcFactory(datastoreRpcFactory) .projectId(PROJECT_ID) .host("http://localhost:" + LocalGcdHelper.PORT); - EasyMock.expect(datastoreRpcFactory.create(EasyMock.anyObject(DatastoreServiceOptions.class))) + EasyMock.expect(datastoreRpcFactory.create(EasyMock.anyObject(DatastoreOptions.class))) .andReturn(datastoreRpc) .anyTimes(); EasyMock.replay(datastoreRpcFactory, datastoreRpc); @@ -83,8 +83,8 @@ public void testDatastore() throws Exception { @Test public void testToBuilder() throws Exception { - DatastoreServiceOptions original = options.namespace("ns1").force(true).build(); - DatastoreServiceOptions copy = original.toBuilder().build(); + DatastoreOptions original = options.namespace("ns1").force(true).build(); + DatastoreOptions copy = original.toBuilder().build(); assertEquals(original.projectId(), copy.projectId()); assertEquals(original.namespace(), copy.namespace()); assertEquals(original.host(), copy.host()); diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreServiceExceptionTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreServiceExceptionTest.java deleted file mode 100644 index 1a06de833cf2..000000000000 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreServiceExceptionTest.java +++ /dev/null @@ -1 +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 static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import com.google.gcloud.datastore.DatastoreServiceException.Code; import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException; import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException.Reason; import org.junit.Test; public class DatastoreServiceExceptionTest { @Test public void testCode() throws Exception { for (Reason reason : Reason.values()) { Code code = Code.valueOf(reason.name()); assertEquals(reason.retryable(), code.retryable()); assertEquals(reason.description(), code.description()); assertEquals(reason.httpStatus(), code.httpStatus()); } DatastoreServiceException exception = new DatastoreServiceException(Code.ABORTED, "bla"); assertEquals(Code.ABORTED, exception.code()); } @Test public void testTranslateAndThrow() throws Exception { for (Reason reason : Reason.values()) { try { DatastoreServiceException.translateAndThrow(new DatastoreRpcException(reason)); fail("Exception expected"); } catch (DatastoreServiceException ex) { assertEquals(reason.name(), ex.code().name()); } } } @Test public void testThrowInvalidRequest() throws Exception { try { DatastoreServiceException.throwInvalidRequest("message %s %d", "a", 1); fail("Exception expected"); } catch (DatastoreServiceException ex) { assertEquals(Code.FAILED_PRECONDITION, ex.code()); assertEquals("message a 1", ex.getMessage()); } } } \ No newline at end of file diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreServiceTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java similarity index 95% rename from gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreServiceTest.java rename to gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index 535fcb5f13e1..156f9684f8ba 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreServiceTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -51,7 +51,7 @@ import java.util.List; @RunWith(JUnit4.class) -public class DatastoreServiceTest { +public class DatastoreTest { private static final String PROJECT_ID = LocalGcdHelper.DEFAULT_PROJECT_ID; private static final String KIND1 = "kind1"; @@ -97,8 +97,8 @@ public class DatastoreServiceTest { 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 DatastoreServiceOptions options; - private DatastoreService datastore; + private DatastoreOptions options; + private Datastore datastore; private static LocalGcdHelper gcdHelper; @@ -111,11 +111,11 @@ public static void beforeClass() throws IOException, InterruptedException { @Before public void setUp() throws IOException, InterruptedException { - options = DatastoreServiceOptions.builder() + options = DatastoreOptions.builder() .projectId(PROJECT_ID) .host("http://localhost:" + LocalGcdHelper.PORT) .build(); - datastore = DatastoreServiceFactory.instance().get(options); + datastore = DatastoreFactory.instance().get(options); StructuredQuery query = Query.keyQueryBuilder().build(); QueryResults result = datastore.run(query); datastore.delete(Iterators.toArray(result, Key.class)); @@ -155,14 +155,14 @@ public void testNewTransactionCommit() { try { transaction.commit(); fail("Expecting a failure"); - } catch (DatastoreServiceException ex) { + } catch (DatastoreException ex) { // expected to fail } try { transaction.rollback(); fail("Expecting a failure"); - } catch (DatastoreServiceException ex) { + } catch (DatastoreException ex) { // expected to fail } @@ -185,8 +185,8 @@ public void testTransactionWithRead() { try { transaction.commit(); fail("Expecting a failure"); - } catch (DatastoreServiceException expected) { - assertEquals(DatastoreServiceException.Code.ABORTED, expected.code()); + } catch (DatastoreException expected) { + assertEquals(DatastoreException.Code.ABORTED, expected.code()); } } @@ -213,8 +213,8 @@ public void testTransactionWithQuery() { try { transaction.commit(); fail("Expecting a failure"); - } catch (DatastoreServiceException expected) { - assertEquals(DatastoreServiceException.Code.ABORTED, expected.code()); + } catch (DatastoreException expected) { + assertEquals(DatastoreException.Code.ABORTED, expected.code()); } } @@ -232,7 +232,7 @@ public void testNewTransactionRollback() { try { transaction.commit(); fail("Expecting a failure"); - } catch (DatastoreServiceException ex) { + } catch (DatastoreException ex) { // expected to fail } @@ -249,28 +249,28 @@ private void verifyNotUsable(DatastoreWriter writer) { try { writer.add(ENTITY3); fail("Expecting a failure"); - } catch (DatastoreServiceException ex) { + } catch (DatastoreException ex) { // expected to fail } try { writer.put(ENTITY3); fail("Expecting a failure"); - } catch (DatastoreServiceException ex) { + } catch (DatastoreException ex) { // expected to fail } try { writer.update(ENTITY3); fail("Expecting a failure"); - } catch (DatastoreServiceException ex) { + } catch (DatastoreException ex) { // expected to fail } try { writer.delete(ENTITY3.key()); fail("Expecting a failure"); - } catch (DatastoreServiceException ex) { + } catch (DatastoreException ex) { // expected to fail } } @@ -315,7 +315,7 @@ public void testNewBatch() { try { batch.submit(); fail("Expecting a failure"); - } catch (DatastoreServiceException ex) { + } catch (DatastoreException ex) { // expected to fail } verifyNotUsable(batch); @@ -535,7 +535,7 @@ public void testGetArray() { try { entity3.getString("str"); fail("Expecting a failure"); - } catch (DatastoreServiceException expected) { + } catch (DatastoreException expected) { // expected - no such property } assertFalse(result.hasNext()); @@ -552,7 +552,7 @@ public void testAddEntity() { try { datastore.add(ENTITY1); fail("Expecting a failure"); - } catch (DatastoreServiceException expected) { + } catch (DatastoreException expected) { // expected; } @@ -578,7 +578,7 @@ public void testUpdate() { try { datastore.update(ENTITY3); fail("Expecting a failure"); - } catch (DatastoreServiceException expected) { + } catch (DatastoreException expected) { // expected; } datastore.add(ENTITY3); @@ -642,17 +642,17 @@ public void testRetires() throws Exception { .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(DatastoreServiceOptions.class))) + EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) .andReturn(rpcMock); EasyMock.expect(rpcMock.lookup(requestPb)) .andThrow(new DatastoreRpc.DatastoreRpcException(Reason.UNAVAILABLE)) .andReturn(responsePb); EasyMock.replay(rpcFactoryMock, rpcMock); - DatastoreServiceOptions options = this.options.toBuilder() + DatastoreOptions options = this.options.toBuilder() .retryParams(RetryParams.getDefaultInstance()) .serviceRpcFactory(rpcFactoryMock) .build(); - DatastoreService datastore = DatastoreServiceFactory.instance().get(options); + Datastore datastore = DatastoreFactory.instance().get(options); Entity entity = datastore.get(KEY1); assertEquals(ENTITY1, entity); EasyMock.verify(rpcFactoryMock, rpcMock); 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 04fdbec54727..36e3571d49ac 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 @@ -45,7 +45,7 @@ public void testOf() throws Exception { assertFalse(value.hasMeaning()); } - @Test(expected = DatastoreServiceException.class) + @Test(expected = DatastoreException.class) public void testIndexedCannotBeSpecified() { ListValue.builder().indexed(false); } 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 1e827971c7fe..9574f1e246d2 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 @@ -132,12 +132,12 @@ public class SerializationTest { @Test public void testServiceOptions() throws Exception { - DatastoreServiceOptions options = DatastoreServiceOptions.builder() + DatastoreOptions options = DatastoreOptions.builder() .authCredentials(AuthCredentials.createForAppEngine()) .normalizeDataset(false) .projectId("ds1") .build(); - DatastoreServiceOptions serializedCopy = serializeAndDeserialize(options); + DatastoreOptions serializedCopy = serializeAndDeserialize(options); assertEquals(options, serializedCopy); options = options.toBuilder() diff --git a/gcloud-java-examples/pom.xml b/gcloud-java-examples/pom.xml index 66d9fc9c93e3..1c0357d63635 100644 --- a/gcloud-java-examples/pom.xml +++ b/gcloud-java-examples/pom.xml @@ -21,4 +21,15 @@ ${project.version}