From dd5802555eb0351a5afa2f2f197cb93ca6d3b66e Mon Sep 17 00:00:00 2001 From: Dmitry <58846611+dmitry-fa@users.noreply.github.com> Date: Mon, 8 Jun 2020 20:52:48 +0300 Subject: [PATCH] feat: stub implementation of StorageRpc for the sake of testing (#351) * feat: stub implementation of StorageRpc which could be used outside of the storage module for testing purposes * feat: stub implementation of StorageRpc which could be used outside of the storage module for testing purposes * feat: stub implementation of StorageRpc which could be used outside of the storage module for testing purposes --- .../storage/testing/StorageRpcTestBase.java | 292 +++++++++ .../testing/StorageRpcTestBaseTest.java | 601 ++++++++++++++++++ 2 files changed, 893 insertions(+) create mode 100644 google-cloud-storage/src/main/java/com/google/cloud/storage/testing/StorageRpcTestBase.java create mode 100644 google-cloud-storage/src/test/java/com/google/cloud/storage/testing/StorageRpcTestBaseTest.java diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/testing/StorageRpcTestBase.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/testing/StorageRpcTestBase.java new file mode 100644 index 000000000..6bd2d487e --- /dev/null +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/testing/StorageRpcTestBase.java @@ -0,0 +1,292 @@ +/* + * Copyright 2020 Google LLC + * + * 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.cloud.storage.testing; + +import com.google.api.services.storage.model.Bucket; +import com.google.api.services.storage.model.BucketAccessControl; +import com.google.api.services.storage.model.HmacKey; +import com.google.api.services.storage.model.HmacKeyMetadata; +import com.google.api.services.storage.model.Notification; +import com.google.api.services.storage.model.ObjectAccessControl; +import com.google.api.services.storage.model.Policy; +import com.google.api.services.storage.model.ServiceAccount; +import com.google.api.services.storage.model.StorageObject; +import com.google.api.services.storage.model.TestIamPermissionsResponse; +import com.google.cloud.Tuple; +import com.google.cloud.storage.spi.v1.RpcBatch; +import com.google.cloud.storage.spi.v1.StorageRpc; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.List; +import java.util.Map; + +/** + * A stub implementation of {@link StorageRpc} which can be used outside of the Storage module for + * testing purposes. All the methods throw an {@code UnsupportedOperationException}. + */ +public class StorageRpcTestBase implements StorageRpc { + + @Override + public Bucket create(Bucket bucket, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public StorageObject create(StorageObject object, InputStream content, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public Tuple> list(Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public Tuple> list(String bucket, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public Bucket get(Bucket bucket, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public StorageObject get(StorageObject object, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public Bucket patch(Bucket bucket, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public StorageObject patch(StorageObject storageObject, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public boolean delete(Bucket bucket, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public boolean delete(StorageObject object, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public RpcBatch createBatch() { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public StorageObject compose( + Iterable sources, StorageObject target, Map targetOptions) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public byte[] load(StorageObject storageObject, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public Tuple read( + StorageObject from, Map options, long position, int bytes) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public long read( + StorageObject from, Map options, long position, OutputStream outputStream) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public String open(StorageObject object, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public String open(String signedURL) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public void write( + String uploadId, + byte[] toWrite, + int toWriteOffset, + long destOffset, + int length, + boolean last) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public RewriteResponse openRewrite(RewriteRequest rewriteRequest) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public RewriteResponse continueRewrite(RewriteResponse previousResponse) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public BucketAccessControl getAcl(String bucket, String entity, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public boolean deleteAcl(String bucket, String entity, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public BucketAccessControl createAcl(BucketAccessControl acl, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public BucketAccessControl patchAcl(BucketAccessControl acl, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public List listAcls(String bucket, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public ObjectAccessControl getDefaultAcl(String bucket, String entity) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public boolean deleteDefaultAcl(String bucket, String entity) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public ObjectAccessControl createDefaultAcl(ObjectAccessControl acl) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public ObjectAccessControl patchDefaultAcl(ObjectAccessControl acl) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public List listDefaultAcls(String bucket) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public ObjectAccessControl getAcl(String bucket, String object, Long generation, String entity) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public boolean deleteAcl(String bucket, String object, Long generation, String entity) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public ObjectAccessControl createAcl(ObjectAccessControl acl) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public ObjectAccessControl patchAcl(ObjectAccessControl acl) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public List listAcls(String bucket, String object, Long generation) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public HmacKey createHmacKey(String serviceAccountEmail, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public Tuple> listHmacKeys(Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public HmacKeyMetadata updateHmacKey(HmacKeyMetadata hmacKeyMetadata, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public HmacKeyMetadata getHmacKey(String accessId, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public void deleteHmacKey(HmacKeyMetadata hmacKeyMetadata, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public Policy getIamPolicy(String bucket, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public Policy setIamPolicy(String bucket, Policy policy, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public TestIamPermissionsResponse testIamPermissions( + String bucket, List permissions, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public boolean deleteNotification(String bucket, String notification) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public List listNotifications(String bucket) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public Notification createNotification(String bucket, Notification notification) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public Bucket lockRetentionPolicy(Bucket bucket, Map options) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public ServiceAccount getServiceAccount(String projectId) { + throw new UnsupportedOperationException("Not implemented yet"); + } +} diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/testing/StorageRpcTestBaseTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/testing/StorageRpcTestBaseTest.java new file mode 100644 index 000000000..b798ad572 --- /dev/null +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/testing/StorageRpcTestBaseTest.java @@ -0,0 +1,601 @@ +/* + * Copyright 2020 Google LLC + * + * 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.cloud.storage.testing; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import com.google.api.services.storage.model.Bucket; +import com.google.api.services.storage.model.BucketAccessControl; +import com.google.api.services.storage.model.HmacKey; +import com.google.api.services.storage.model.HmacKeyMetadata; +import com.google.api.services.storage.model.Notification; +import com.google.api.services.storage.model.ObjectAccessControl; +import com.google.api.services.storage.model.Policy; +import com.google.api.services.storage.model.ServiceAccount; +import com.google.api.services.storage.model.StorageObject; +import com.google.api.services.storage.model.TestIamPermissionsResponse; +import com.google.cloud.Tuple; +import com.google.cloud.storage.spi.v1.RpcBatch; +import com.google.cloud.storage.spi.v1.StorageRpc; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.Callable; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class StorageRpcTestBaseTest { + + private Callable rpc; + + private static final StorageRpc STORAGE_RPC = new StorageRpcTestBase(); + private static final Map OPTIONS = new HashMap<>(); + private static final Bucket BUCKET = new Bucket().setName("fake-bucket"); + private static final byte[] BYTES = {0, 1, 2, 3, 4, 5, 6, 7}; + private static final StorageObject OBJECT = + new StorageObject().setName("object name").setBucket("bucket name"); + + @Before + public void setUp() { + rpc = null; + } + + @After + public void tearDown() throws Exception { + assertNotNull(rpc); + try { + rpc.call(); + fail("UnsupportedOperationException expected"); + } catch (UnsupportedOperationException e) { + // expected + } + } + + @Test + public void testCreateBucket() { + rpc = + new Callable() { + @Override + public Bucket call() { + return STORAGE_RPC.create(BUCKET, OPTIONS); + } + }; + } + + @Test + public void testCreateObject() { + rpc = + new Callable() { + @Override + public StorageObject call() { + return STORAGE_RPC.create(OBJECT, new ByteArrayInputStream(BYTES), OPTIONS); + } + }; + } + + @Test + public void testList() { + rpc = + new Callable>>() { + @Override + public Tuple> call() { + return STORAGE_RPC.list(OPTIONS); + } + }; + } + + @Test + public void testListBucket() { + rpc = + new Callable>>() { + @Override + public Tuple> call() { + return STORAGE_RPC.list(BUCKET.getName(), OPTIONS); + } + }; + } + + @Test + public void testGetBucket() { + rpc = + new Callable() { + @Override + public Bucket call() { + return STORAGE_RPC.get(BUCKET, OPTIONS); + } + }; + } + + @Test + public void testGetObject() { + rpc = + new Callable() { + @Override + public StorageObject call() { + return STORAGE_RPC.get(OBJECT, OPTIONS); + } + }; + } + + @Test + public void testPatchBucket() { + rpc = + new Callable() { + @Override + public Bucket call() { + return STORAGE_RPC.patch(BUCKET, OPTIONS); + } + }; + } + + @Test + public void testPatchObject() { + rpc = + new Callable() { + @Override + public StorageObject call() { + return STORAGE_RPC.patch(OBJECT, OPTIONS); + } + }; + } + + @Test + public void testDeleteBucket() { + rpc = + new Callable() { + @Override + public Boolean call() { + return STORAGE_RPC.delete(BUCKET, OPTIONS); + } + }; + } + + @Test + public void testDeleteObject() { + rpc = + new Callable() { + @Override + public Boolean call() { + return STORAGE_RPC.delete(OBJECT, OPTIONS); + } + }; + } + + @Test + public void testCreateBatch() { + rpc = + new Callable() { + @Override + public RpcBatch call() { + return STORAGE_RPC.createBatch(); + } + }; + } + + @Test + public void testCompose() { + rpc = + new Callable() { + @Override + public StorageObject call() { + return STORAGE_RPC.compose(null, OBJECT, OPTIONS); + } + }; + } + + @Test + public void testLoad() { + rpc = + new Callable() { + @Override + public byte[] call() { + return STORAGE_RPC.load(OBJECT, OPTIONS); + } + }; + } + + @Test + public void testReadBytes() { + rpc = + new Callable>() { + @Override + public Tuple call() { + return STORAGE_RPC.read(OBJECT, OPTIONS, 0, 0); + } + }; + } + + @Test + public void testReadOutputStream() { + rpc = + new Callable() { + @Override + public Long call() { + return STORAGE_RPC.read(OBJECT, OPTIONS, 0, new ByteArrayOutputStream(100)); + } + }; + } + + @Test + public void testOpenObject() { + rpc = + new Callable() { + @Override + public String call() { + return STORAGE_RPC.open(OBJECT, OPTIONS); + } + }; + } + + @Test + public void testOpenSignedURL() { + rpc = + new Callable() { + @Override + public String call() { + return STORAGE_RPC.open("signedURL"); + } + }; + } + + @Test + public void testWrite() { + rpc = + new Callable() { + @Override + public Void call() { + STORAGE_RPC.write("uploadId", new byte[10], 1, 2L, 3, false); + return null; + } + }; + } + + @Test + public void testOpenRewrite() { + rpc = + new Callable() { + @Override + public StorageRpc.RewriteResponse call() { + return STORAGE_RPC.openRewrite(null); + } + }; + } + + @Test + public void testContinueRewrite() { + rpc = + new Callable() { + @Override + public StorageRpc.RewriteResponse call() { + return STORAGE_RPC.continueRewrite(null); + } + }; + } + + @Test + public void testGetAclBucket() { + rpc = + new Callable() { + @Override + public BucketAccessControl call() { + return STORAGE_RPC.getAcl("bucket", "entity", OPTIONS); + } + }; + } + + @Test + public void testGetAclObject() { + rpc = + new Callable() { + @Override + public ObjectAccessControl call() { + return STORAGE_RPC.getAcl("bucket", "object", 1L, "entity"); + } + }; + } + + @Test + public void testDeleteAclBucket() { + rpc = + new Callable() { + @Override + public Boolean call() { + return STORAGE_RPC.deleteAcl("bucketName", "entity", OPTIONS); + } + }; + } + + @Test + public void testDeleteAclObject() { + rpc = + new Callable() { + @Override + public Boolean call() { + return STORAGE_RPC.deleteAcl("bucketName", "object", 0L, "entity"); + } + }; + } + + @Test + public void testCreateAclBucket() { + rpc = + new Callable() { + @Override + public BucketAccessControl call() { + return STORAGE_RPC.createAcl(null, OPTIONS); + } + }; + } + + @Test + public void testCreateAclObject() { + rpc = + new Callable() { + @Override + public ObjectAccessControl call() { + return STORAGE_RPC.createAcl(null); + } + }; + } + + @Test + public void testPatchAclBucket() { + rpc = + new Callable() { + @Override + public ObjectAccessControl call() { + return STORAGE_RPC.createAcl(null); + } + }; + } + + @Test + public void testPatchAclObject() { + rpc = + new Callable() { + @Override + public ObjectAccessControl call() { + return STORAGE_RPC.patchAcl(null); + } + }; + } + + @Test + public void testListAclsBucket() { + rpc = + new Callable>() { + @Override + public List call() { + return STORAGE_RPC.listAcls("BUCKET_NAME", OPTIONS); + } + }; + } + + @Test + public void testListAclsObject() { + rpc = + new Callable>() { + @Override + public List call() { + return STORAGE_RPC.listAcls("BUCKET_NAME", "OBJECT_NAME", 100L); + } + }; + } + + @Test + public void testCreateHmacKey() { + rpc = + new Callable() { + @Override + public HmacKey call() { + return STORAGE_RPC.createHmacKey("account", OPTIONS); + } + }; + } + + @Test + public void testListHmacKeys() { + rpc = + new Callable>>() { + @Override + public Tuple> call() { + return STORAGE_RPC.listHmacKeys(OPTIONS); + } + }; + } + + @Test + public void testUpdateHmacKey() { + rpc = + new Callable() { + @Override + public HmacKeyMetadata call() { + return STORAGE_RPC.updateHmacKey(null, OPTIONS); + } + }; + } + + @Test + public void testGetHmacKey() { + rpc = + new Callable() { + @Override + public HmacKeyMetadata call() { + return STORAGE_RPC.getHmacKey("account", OPTIONS); + } + }; + } + + @Test + public void testDeleteHmacKey() { + rpc = + new Callable() { + @Override + public Void call() { + STORAGE_RPC.deleteHmacKey(null, OPTIONS); + return null; + } + }; + } + + @Test + public void testGetDefaultAcl() { + rpc = + new Callable() { + @Override + public ObjectAccessControl call() { + return STORAGE_RPC.getDefaultAcl("bucket", "entity"); + } + }; + } + + @Test + public void testDeleteDefaultAcl() { + rpc = + new Callable() { + @Override + public Boolean call() { + return STORAGE_RPC.deleteDefaultAcl("bucket", "entity"); + } + }; + } + + @Test + public void testCreateDefaultAcl() { + rpc = + new Callable() { + @Override + public ObjectAccessControl call() { + return STORAGE_RPC.createDefaultAcl(null); + } + }; + } + + @Test + public void testPatchDefaultAcl() { + rpc = + new Callable() { + @Override + public ObjectAccessControl call() { + return STORAGE_RPC.patchDefaultAcl(null); + } + }; + } + + @Test + public void testListDefaultAcls() { + rpc = + new Callable>() { + @Override + public List call() { + return STORAGE_RPC.listDefaultAcls("bucket"); + } + }; + } + + @Test + public void testGetIamPolicy() { + rpc = + new Callable() { + @Override + public Policy call() { + return STORAGE_RPC.getIamPolicy("bucket", OPTIONS); + } + }; + } + + @Test + public void testSetIamPolicy() { + rpc = + new Callable() { + @Override + public Policy call() { + return STORAGE_RPC.setIamPolicy("bucket", null, OPTIONS); + } + }; + } + + @Test + public void testTestIamPermissions() { + rpc = + new Callable() { + @Override + public TestIamPermissionsResponse call() { + return STORAGE_RPC.testIamPermissions("bucket", null, OPTIONS); + } + }; + } + + @Test + public void testDeleteNotification() { + rpc = + new Callable() { + @Override + public Boolean call() { + return STORAGE_RPC.deleteNotification("bucket", "entity"); + } + }; + } + + @Test + public void testListNotifications() { + rpc = + new Callable>() { + @Override + public List call() { + return STORAGE_RPC.listNotifications("bucket"); + } + }; + } + + @Test + public void testCreateNotification() { + rpc = + new Callable() { + @Override + public Notification call() { + return STORAGE_RPC.createNotification("bucket", null); + } + }; + } + + @Test + public void testLockRetentionPolicy() { + rpc = + new Callable() { + @Override + public Bucket call() { + return STORAGE_RPC.lockRetentionPolicy(BUCKET, OPTIONS); + } + }; + } + + @Test + public void testGetServiceAccount() { + rpc = + new Callable() { + @Override + public ServiceAccount call() { + return STORAGE_RPC.getServiceAccount("project"); + } + }; + } +}