From 3d835178aa4da68b1d2d3e7cdc0ab871764a0114 Mon Sep 17 00:00:00 2001 From: Jeremy Boynes Date: Wed, 3 Jun 2015 09:55:52 -0700 Subject: [PATCH] Replace CR characters with newlines --- .../java/com/google/gcloud/BaseService.java | 32 +- .../main/java/com/google/gcloud/Service.java | 22 +- .../google/gcloud/spi/ServiceRpcFactory.java | 31 +- .../com/google/gcloud/spi/DatastoreRpc.java | 118 ++++++- .../BaseDatastoreBatchWriterTest.java | 321 +++++++++++++++++- .../gcloud/datastore/BaseEntityTest.java | 207 ++++++++++- .../com/google/gcloud/datastore/BlobTest.java | 112 +++++- .../google/gcloud/datastore/CursorTest.java | 53 ++- .../datastore/DatastoreExceptionTest.java | 66 +++- .../google/gcloud/datastore/DateTimeTest.java | 78 ++++- .../com/google/gcloud/spi/StorageRpc.java | 172 +++++++++- .../gcloud/storage/StorageException.java | 49 ++- 12 files changed, 1249 insertions(+), 12 deletions(-) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/BaseService.java b/gcloud-java-core/src/main/java/com/google/gcloud/BaseService.java index 4d96ac5b86e6..02bfd76771b2 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/BaseService.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/BaseService.java @@ -1 +1,31 @@ -/* * 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; public abstract class BaseService implements Service { private final O options; protected BaseService(O options) { this.options = options; } @Override public O options() { return options; } } \ 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; + +public abstract class BaseService implements Service { + + private final O options; + + protected BaseService(O options) { + this.options = options; + } + + @Override + public O options() { + return options; + } +} diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/Service.java b/gcloud-java-core/src/main/java/com/google/gcloud/Service.java index 02d810186bfc..8a43b9cf2244 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/Service.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/Service.java @@ -1 +1,21 @@ -/* * 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; public interface Service { O options(); } \ 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; + +public interface Service { + O options(); +} diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/spi/ServiceRpcFactory.java b/gcloud-java-core/src/main/java/com/google/gcloud/spi/ServiceRpcFactory.java index e8e67615305d..9bab3f81ce9c 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/spi/ServiceRpcFactory.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/spi/ServiceRpcFactory.java @@ -1 +1,30 @@ -/* * 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.spi; import com.google.gcloud.ServiceOptions; import java.io.Serializable; /** * A base interface for all service RPC factories. * Loading of a factory implementation is done via {@link java.util.ServiceLoader}. */ public interface ServiceRpcFactory extends Serializable { S create(O options); } \ 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.spi; + +import com.google.gcloud.ServiceOptions; + +import java.io.Serializable; + +/** + * A base interface for all service RPC factories. + * Loading of a factory implementation is done via {@link java.util.ServiceLoader}. + */ +public interface ServiceRpcFactory extends Serializable { + + S create(O options); +} 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 be194c7b3848..f4b7cd39587b 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 @@ -1 +1,117 @@ -/* * 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.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. */ public interface DatastoreRpc { public class DatastoreRpcException extends Exception { /** * The reason for the exception. * * @see Google Cloud Datastore error codes */ public enum Reason { ABORTED(true, "Request aborted", 409), DEADLINE_EXCEEDED(true, "Deadline exceeded", 403), FAILED_PRECONDITION(false, "Invalid request", 412), INTERNAL(false, "Server returned an error", 500), INVALID_ARGUMENT(false, "Request parameter has an invalid value", 400), PERMISSION_DENIED(false, "Unauthorized request", 403), RESOURCE_EXHAUSTED(false, "Quota exceeded", 402), UNAVAILABLE(true, "Could not reach service", 503); private final boolean retryable; private final String description; private final int httpStatus; private Reason(boolean retryable, String description, int httpStatus) { this.retryable = retryable; this.description = description; this.httpStatus = httpStatus; } public boolean retryable() { return retryable; } public String description() { return description; } public int httpStatus() { return httpStatus; } } private final String reason; private final int httpStatus; private final boolean retryable; public DatastoreRpcException(Reason reason) { this(reason.name(), reason.httpStatus, reason.retryable, reason.description); } public DatastoreRpcException(String reason, int httpStatus, boolean retryable, String message) { super(message); this.reason = reason; this.httpStatus = httpStatus; this.retryable = retryable; } public String reason() { return reason; } public int httpStatus() { return httpStatus; } public boolean retryable() { return retryable; } } AllocateIdsResponse allocateIds(AllocateIdsRequest request) throws DatastoreRpcException; BeginTransactionResponse beginTransaction(BeginTransactionRequest request) throws DatastoreRpcException; CommitResponse commit(CommitRequest request) throws DatastoreRpcException; LookupResponse lookup(LookupRequest request) throws DatastoreRpcException; RollbackResponse rollback(RollbackRequest request) throws DatastoreRpcException; RunQueryResponse runQuery(RunQueryRequest request) throws DatastoreRpcException; } \ 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.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. + */ +public interface DatastoreRpc { + + public class DatastoreRpcException extends Exception { + + /** + * The reason for the exception. + * + * @see Google Cloud Datastore error codes + */ + public enum Reason { + + ABORTED(true, "Request aborted", 409), + DEADLINE_EXCEEDED(true, "Deadline exceeded", 403), + FAILED_PRECONDITION(false, "Invalid request", 412), + INTERNAL(false, "Server returned an error", 500), + INVALID_ARGUMENT(false, "Request parameter has an invalid value", 400), + PERMISSION_DENIED(false, "Unauthorized request", 403), + RESOURCE_EXHAUSTED(false, "Quota exceeded", 402), + UNAVAILABLE(true, "Could not reach service", 503); + + private final boolean retryable; + private final String description; + private final int httpStatus; + + private Reason(boolean retryable, String description, int httpStatus) { + this.retryable = retryable; + this.description = description; + this.httpStatus = httpStatus; + } + + public boolean retryable() { + return retryable; + } + + public String description() { + return description; + } + + public int httpStatus() { + return httpStatus; + } + } + + private final String reason; + private final int httpStatus; + private final boolean retryable; + + public DatastoreRpcException(Reason reason) { + this(reason.name(), reason.httpStatus, reason.retryable, reason.description); + } + + public DatastoreRpcException(String reason, int httpStatus, boolean retryable, String message) { + super(message); + this.reason = reason; + this.httpStatus = httpStatus; + this.retryable = retryable; + } + + public String reason() { + return reason; + } + + public int httpStatus() { + return httpStatus; + } + + public boolean retryable() { + return retryable; + } + } + + AllocateIdsResponse allocateIds(AllocateIdsRequest request) throws DatastoreRpcException; + + BeginTransactionResponse beginTransaction(BeginTransactionRequest request) + throws DatastoreRpcException; + + CommitResponse commit(CommitRequest request) throws DatastoreRpcException; + + LookupResponse lookup(LookupRequest request) throws DatastoreRpcException; + + RollbackResponse rollback(RollbackRequest request) throws DatastoreRpcException; + + RunQueryResponse runQuery(RunQueryRequest request) throws DatastoreRpcException; +} 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 c3f1bfbd5a71..9fe207a04056 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,320 @@ -/* * 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 +/* + * 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); + } +} + 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 daa0c502d4b5..02ccd996dc83 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,206 @@ -/* * 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> 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> 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()); + } +} diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BlobTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BlobTest.java index e78023101e48..9cd0b4ce332a 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BlobTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BlobTest.java @@ -1 +1,111 @@ -/* * 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.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import org.junit.Before; import org.junit.Test; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.nio.ByteBuffer; import java.util.Random; public class BlobTest { private byte[] bytes1 = new byte[10]; private byte[] bytes2 = new byte[11]; private Blob blob1; private Blob blob2; @Before public void setUp() { Random rnd = new Random(); rnd.nextBytes(bytes1); rnd.nextBytes(bytes2); blob1 = Blob.copyFrom(bytes1); blob2 = Blob.copyFrom(bytes2); } @Test public void testEquals() throws Exception { assertEquals(blob1, blob1); assertEquals(blob1, Blob.copyFrom(bytes1)); assertNotEquals(blob1, blob2); } @Test public void testLength() throws Exception { assertEquals(bytes1.length, blob1.length()); assertEquals(bytes2.length, blob2.length()); } @Test public void testToByteArray() throws Exception { assertArrayEquals(bytes1, blob1.toByteArray()); assertArrayEquals(bytes2, blob2.toByteArray()); } @Test public void testAsReadOnlyByteBuffer() throws Exception { ByteBuffer buffer = blob1.asReadOnlyByteBuffer(); byte[] bytes = new byte[bytes1.length]; buffer.get(bytes); assertFalse(buffer.hasRemaining()); assertArrayEquals(bytes1, bytes); } @Test public void testAsInputStream() throws Exception { byte[] bytes = new byte[bytes1.length]; InputStream in = blob1.asInputStream(); assertEquals(bytes1.length, in.read(bytes)); assertEquals(-1, in.read()); assertArrayEquals(bytes1, bytes); } @Test public void testCopyTo() throws Exception { byte[] bytes = new byte[bytes1.length]; blob1.copyTo(bytes); assertArrayEquals(bytes1, bytes); ByteBuffer buffer = ByteBuffer.allocate(bytes1.length); blob1.copyTo(buffer); buffer.flip(); bytes = new byte[bytes1.length]; buffer.get(bytes); assertFalse(buffer.hasRemaining()); assertArrayEquals(bytes1, bytes); } @Test public void testCopyFrom() throws Exception { Blob blob = Blob.copyFrom(ByteBuffer.wrap(bytes1)); assertEquals(blob1, blob); assertArrayEquals(bytes1, blob.toByteArray()); blob = Blob.copyFrom(new ByteArrayInputStream(bytes2)); assertEquals(blob2, blob); assertArrayEquals(bytes2, blob.toByteArray()); } } \ 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.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; + +import org.junit.Before; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.util.Random; + +public class BlobTest { + + private byte[] bytes1 = new byte[10]; + private byte[] bytes2 = new byte[11]; + private Blob blob1; + private Blob blob2; + + + @Before + public void setUp() { + Random rnd = new Random(); + rnd.nextBytes(bytes1); + rnd.nextBytes(bytes2); + blob1 = Blob.copyFrom(bytes1); + blob2 = Blob.copyFrom(bytes2); + } + + @Test + public void testEquals() throws Exception { + assertEquals(blob1, blob1); + assertEquals(blob1, Blob.copyFrom(bytes1)); + assertNotEquals(blob1, blob2); + } + + @Test + public void testLength() throws Exception { + assertEquals(bytes1.length, blob1.length()); + assertEquals(bytes2.length, blob2.length()); + } + + @Test + public void testToByteArray() throws Exception { + assertArrayEquals(bytes1, blob1.toByteArray()); + assertArrayEquals(bytes2, blob2.toByteArray()); + } + + @Test + public void testAsReadOnlyByteBuffer() throws Exception { + ByteBuffer buffer = blob1.asReadOnlyByteBuffer(); + byte[] bytes = new byte[bytes1.length]; + buffer.get(bytes); + assertFalse(buffer.hasRemaining()); + assertArrayEquals(bytes1, bytes); + } + + @Test + public void testAsInputStream() throws Exception { + byte[] bytes = new byte[bytes1.length]; + InputStream in = blob1.asInputStream(); + assertEquals(bytes1.length, in.read(bytes)); + assertEquals(-1, in.read()); + assertArrayEquals(bytes1, bytes); + } + + @Test + public void testCopyTo() throws Exception { + byte[] bytes = new byte[bytes1.length]; + blob1.copyTo(bytes); + assertArrayEquals(bytes1, bytes); + + ByteBuffer buffer = ByteBuffer.allocate(bytes1.length); + blob1.copyTo(buffer); + buffer.flip(); + bytes = new byte[bytes1.length]; + buffer.get(bytes); + assertFalse(buffer.hasRemaining()); + assertArrayEquals(bytes1, bytes); + } + + @Test + public void testCopyFrom() throws Exception { + Blob blob = Blob.copyFrom(ByteBuffer.wrap(bytes1)); + assertEquals(blob1, blob); + assertArrayEquals(bytes1, blob.toByteArray()); + + blob = Blob.copyFrom(new ByteArrayInputStream(bytes2)); + assertEquals(blob2, blob); + assertArrayEquals(bytes2, blob.toByteArray()); + } +} diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/CursorTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/CursorTest.java index 6806fd698331..e4a9b6cbc742 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/CursorTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/CursorTest.java @@ -1 +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.assertNotEquals; import com.google.protobuf.ByteString; import org.junit.Before; import org.junit.Test; public class CursorTest { private byte[] bytes1 = {1, 2, 3, '%', '<', '+'}; private byte[] bytes2 = {10, 20, 30}; private Cursor cursor1; private Cursor cursor2; @Before public void setUp() throws Exception { cursor1 = new Cursor(ByteString.copyFrom(bytes1)); cursor2 = new Cursor(ByteString.copyFrom(bytes2)); } @Test public void testToFromUrlSafe() throws Exception { String urlSafe = cursor1.toUrlSafe(); assertEquals(cursor1, Cursor.fromUrlSafe(urlSafe)); } @Test public void testCopyFrom() throws Exception { Cursor cursor = Cursor.copyFrom(bytes2); assertEquals(cursor2, cursor); assertNotEquals(cursor1, cursor); } } \ 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.assertNotEquals; + +import com.google.protobuf.ByteString; + +import org.junit.Before; +import org.junit.Test; + +public class CursorTest { + + private byte[] bytes1 = {1, 2, 3, '%', '<', '+'}; + private byte[] bytes2 = {10, 20, 30}; + private Cursor cursor1; + private Cursor cursor2; + + @Before + public void setUp() throws Exception { + cursor1 = new Cursor(ByteString.copyFrom(bytes1)); + cursor2 = new Cursor(ByteString.copyFrom(bytes2)); + } + + @Test + public void testToFromUrlSafe() throws Exception { + String urlSafe = cursor1.toUrlSafe(); + assertEquals(cursor1, Cursor.fromUrlSafe(urlSafe)); + } + + @Test + public void testCopyFrom() throws Exception { + Cursor cursor = Cursor.copyFrom(bytes2); + assertEquals(cursor2, cursor); + assertNotEquals(cursor1, cursor); + } +} 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 1dd0f255ceca..a64a3531c19d 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 @@ -1 +1,65 @@ -/* * 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 +/* + * 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()); + } + } +} diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DateTimeTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DateTimeTest.java index a7131e04e89c..23f3951a5dc3 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DateTimeTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DateTimeTest.java @@ -1 +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 static org.junit.Assert.assertTrue; import org.junit.Test; import java.util.Calendar; public class DateTimeTest { @Test public void testTimestampMicroseconds() throws Exception { Calendar cal = Calendar.getInstance(); DateTime date = DateTime.copyFrom(cal); assertEquals(cal.getTimeInMillis() * 1000, date.timestampMicroseconds()); } @Test public void testTimestampMillis() throws Exception { Calendar cal = Calendar.getInstance(); DateTime date = DateTime.copyFrom(cal); assertEquals(cal.getTimeInMillis(), date.timestampMillis()); } @Test public void testToDate() throws Exception { Calendar cal = Calendar.getInstance(); DateTime date = DateTime.copyFrom(cal); assertEquals(cal.getTime(), date.toDate()); } @Test public void testToCalendar() throws Exception { Calendar cal = Calendar.getInstance(); DateTime date = DateTime.copyFrom(cal); assertEquals(cal, date.toCalendar()); } @Test public void testNow() throws Exception { Calendar cal1 = Calendar.getInstance(); DateTime now = DateTime.now(); Calendar cal2 = Calendar.getInstance(); assertTrue(now.timestampMillis() >= cal1.getTimeInMillis()); assertTrue(now.timestampMillis() <= cal2.getTimeInMillis()); } @Test public void testCopyFrom() throws Exception { Calendar cal = Calendar.getInstance(); DateTime date1 = DateTime.copyFrom(cal); DateTime date2 = DateTime.copyFrom(cal.getTime()); cal.add(Calendar.DATE, 1); DateTime date3 = DateTime.copyFrom(cal.getTime()); assertEquals(date1, date2); assertNotEquals(date1, date3); } } \ 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.assertNotEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import java.util.Calendar; + +public class DateTimeTest { + + @Test + public void testTimestampMicroseconds() throws Exception { + Calendar cal = Calendar.getInstance(); + DateTime date = DateTime.copyFrom(cal); + assertEquals(cal.getTimeInMillis() * 1000, date.timestampMicroseconds()); + } + + @Test + public void testTimestampMillis() throws Exception { + Calendar cal = Calendar.getInstance(); + DateTime date = DateTime.copyFrom(cal); + assertEquals(cal.getTimeInMillis(), date.timestampMillis()); + } + + @Test + public void testToDate() throws Exception { + Calendar cal = Calendar.getInstance(); + DateTime date = DateTime.copyFrom(cal); + assertEquals(cal.getTime(), date.toDate()); + } + + @Test + public void testToCalendar() throws Exception { + Calendar cal = Calendar.getInstance(); + DateTime date = DateTime.copyFrom(cal); + assertEquals(cal, date.toCalendar()); + } + + @Test + public void testNow() throws Exception { + Calendar cal1 = Calendar.getInstance(); + DateTime now = DateTime.now(); + Calendar cal2 = Calendar.getInstance(); + assertTrue(now.timestampMillis() >= cal1.getTimeInMillis()); + assertTrue(now.timestampMillis() <= cal2.getTimeInMillis()); + } + + @Test + public void testCopyFrom() throws Exception { + Calendar cal = Calendar.getInstance(); + DateTime date1 = DateTime.copyFrom(cal); + DateTime date2 = DateTime.copyFrom(cal.getTime()); + cal.add(Calendar.DATE, 1); + DateTime date3 = DateTime.copyFrom(cal.getTime()); + assertEquals(date1, date2); + assertNotEquals(date1, date3); + } +} + diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpc.java b/gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpc.java index bd5d9b782975..7448fec9d32f 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpc.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpc.java @@ -1 +1,171 @@ -/* * 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.spi; import com.google.api.services.storage.model.Bucket; import com.google.api.services.storage.model.StorageObject; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.gcloud.storage.StorageException; import java.util.List; import java.util.Map; public interface StorageRpc { // These options are part of the Google Cloud storage header options enum Option { PREDEFINED_ACL("predefinedAcl"), PREDEFINED_DEFAULT_OBJECT_ACL("predefinedDefaultObjectAcl"), IF_METAGENERATION_MATCH("ifMetagenerationMatch"), IF_METAGENERATION_NOT_MATCH("ifMetagenerationNotMatch"), IF_GENERATION_NOT_MATCH("ifGenerationMatch"), IF_GENERATION_MATCH("ifGenerationNotMatch"), IF_SOURCE_METAGENERATION_MATCH("ifSourceMetagenerationMatch"), IF_SOURCE_METAGENERATION_NOT_MATCH("ifSourceMetagenerationNotMatch"), IF_SOURCE_GENERATION_MATCH("ifSourceGenerationMatch"), IF_SOURCE_GENERATION_NOT_MATCH("ifSourceGenerationNotMatch"), PREFIX("prefix"), MAX_RESULTS("maxResults"), PAGE_TOKEN("pageToken"), DELIMITER("delimiter"), VERSIONS("versions"); private final String value; Option(String value) { this.value = value; } public String value() { return value; } @SuppressWarnings("unchecked") T get(Map options) { return (T) options.get(this); } String getString(Map options) { return get(options); } Long getLong(Map options) { return get(options); } Boolean getBoolean(Map options) { return get(options); } } class Tuple { private final X x; private final Y y; private Tuple(X x, Y y) { this.x = x; this.y = y; } public static Tuple of(X x, Y y) { return new Tuple<>(x, y); } public X x() { return x; } public Y y() { return y; } } class BatchRequest { public final List>> toDelete; public final List>> toUpdate; public final List>> toGet; public BatchRequest(Iterable>> toDelete, Iterable>> toUpdate, Iterable>> toGet) { this.toDelete = ImmutableList.copyOf(toDelete); this.toUpdate = ImmutableList.copyOf(toUpdate); this.toGet = ImmutableList.copyOf(toGet); } } class BatchResponse { public final Map> deletes; public final Map> updates; public final Map> gets; public BatchResponse(Map> deletes, Map> updates, Map> gets) { this.deletes = ImmutableMap.copyOf(deletes); this.updates = ImmutableMap.copyOf(updates); this.gets = ImmutableMap.copyOf(gets); } } Bucket create(Bucket bucket, Map options) throws StorageException; StorageObject create(StorageObject object, byte[] content, Map options) throws StorageException; Tuple> list(Map options) throws StorageException; Tuple> list(String bucket, Map options) throws StorageException; Bucket get(Bucket bucket, Map options) throws StorageException; StorageObject get(StorageObject object, Map options) throws StorageException; Bucket patch(Bucket bucket, Map options) throws StorageException; StorageObject patch(StorageObject storageObject, Map options) throws StorageException; boolean delete(Bucket bucket, Map options) throws StorageException; boolean delete(StorageObject object, Map options) throws StorageException; BatchResponse batch(BatchRequest request) throws StorageException; StorageObject compose(Iterable sources, StorageObject target, Map targetOptions) throws StorageException; StorageObject copy(StorageObject source, Map sourceOptions, StorageObject target, Map targetOptions) throws StorageException; byte[] load(StorageObject storageObject, Map options) throws StorageException; byte[] read(StorageObject from, Map options, long position, int bytes) throws StorageException; String open(StorageObject object, Map options) throws StorageException; void write(String uploadId, byte[] toWrite, int toWriteOffset, StorageObject dest, long destOffset, int length, boolean last) throws StorageException; } \ 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.spi; + +import com.google.api.services.storage.model.Bucket; +import com.google.api.services.storage.model.StorageObject; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.gcloud.storage.StorageException; + +import java.util.List; +import java.util.Map; + +public interface StorageRpc { + + // These options are part of the Google Cloud storage header options + enum Option { + PREDEFINED_ACL("predefinedAcl"), + PREDEFINED_DEFAULT_OBJECT_ACL("predefinedDefaultObjectAcl"), + IF_METAGENERATION_MATCH("ifMetagenerationMatch"), + IF_METAGENERATION_NOT_MATCH("ifMetagenerationNotMatch"), + IF_GENERATION_NOT_MATCH("ifGenerationMatch"), + IF_GENERATION_MATCH("ifGenerationNotMatch"), + IF_SOURCE_METAGENERATION_MATCH("ifSourceMetagenerationMatch"), + IF_SOURCE_METAGENERATION_NOT_MATCH("ifSourceMetagenerationNotMatch"), + IF_SOURCE_GENERATION_MATCH("ifSourceGenerationMatch"), + IF_SOURCE_GENERATION_NOT_MATCH("ifSourceGenerationNotMatch"), + PREFIX("prefix"), + MAX_RESULTS("maxResults"), + PAGE_TOKEN("pageToken"), + DELIMITER("delimiter"), + VERSIONS("versions"); + + private final String value; + + Option(String value) { + this.value = value; + } + + public String value() { + return value; + } + + @SuppressWarnings("unchecked") + T get(Map options) { + return (T) options.get(this); + } + + String getString(Map options) { + return get(options); + } + + Long getLong(Map options) { + return get(options); + } + + Boolean getBoolean(Map options) { + return get(options); + } + } + + class Tuple { + + private final X x; + private final Y y; + + private Tuple(X x, Y y) { + this.x = x; + this.y = y; + } + + public static Tuple of(X x, Y y) { + return new Tuple<>(x, y); + } + + public X x() { + return x; + } + + public Y y() { + return y; + } + } + + class BatchRequest { + + public final List>> toDelete; + public final List>> toUpdate; + public final List>> toGet; + + public BatchRequest(Iterable>> toDelete, + Iterable>> toUpdate, + Iterable>> toGet) { + this.toDelete = ImmutableList.copyOf(toDelete); + this.toUpdate = ImmutableList.copyOf(toUpdate); + this.toGet = ImmutableList.copyOf(toGet); + } + } + + class BatchResponse { + + public final Map> deletes; + public final Map> updates; + public final Map> gets; + + public BatchResponse(Map> deletes, + Map> updates, + Map> gets) { + this.deletes = ImmutableMap.copyOf(deletes); + this.updates = ImmutableMap.copyOf(updates); + this.gets = ImmutableMap.copyOf(gets); + } + } + + Bucket create(Bucket bucket, Map options) throws StorageException; + + StorageObject create(StorageObject object, byte[] content, Map options) + throws StorageException; + + Tuple> list(Map options) throws StorageException; + + Tuple> list(String bucket, Map options) + throws StorageException; + + Bucket get(Bucket bucket, Map options) throws StorageException; + + StorageObject get(StorageObject object, Map options) + throws StorageException; + + Bucket patch(Bucket bucket, Map options) throws StorageException; + + StorageObject patch(StorageObject storageObject, Map options) + throws StorageException; + + boolean delete(Bucket bucket, Map options) throws StorageException; + + boolean delete(StorageObject object, Map options) throws StorageException; + + BatchResponse batch(BatchRequest request) throws StorageException; + + StorageObject compose(Iterable sources, StorageObject target, + Map targetOptions) throws StorageException; + + StorageObject copy(StorageObject source, Map sourceOptions, + StorageObject target, Map targetOptions) throws StorageException; + + byte[] load(StorageObject storageObject, Map options) + throws StorageException; + + byte[] read(StorageObject from, Map options, long position, int bytes) + throws StorageException; + + String open(StorageObject object, Map options) throws StorageException; + + void write(String uploadId, byte[] toWrite, int toWriteOffset, StorageObject dest, + long destOffset, int length, boolean last) throws StorageException; +} diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageException.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageException.java index 9bf073bef6b4..45734d631aec 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageException.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageException.java @@ -1 +1,48 @@ -/* * 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.storage; /** * Storage service exception. * * @see Google Cloud * Storage error codes */ public class StorageException extends RuntimeException { private static final long serialVersionUID = -3748432005065428084L; private final int code; private final boolean retryable; public StorageException(int code, String message, boolean retryable) { super(message); this.code = code; this.retryable = retryable; } /** * Returns the code associated with this exception. */ public int code() { return code; } public boolean retryable() { return retryable; } } \ 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.storage; + +/** + * Storage service exception. + * + * @see Google Cloud + * Storage error codes + */ +public class StorageException extends RuntimeException { + + private static final long serialVersionUID = -3748432005065428084L; + + private final int code; + private final boolean retryable; + + public StorageException(int code, String message, boolean retryable) { + super(message); + this.code = code; + this.retryable = retryable; + } + + /** + * Returns the code associated with this exception. + */ + public int code() { + return code; + } + + public boolean retryable() { + return retryable; + } +}