From 3dec049db9319cd73e3a5148b389d425b59cfd9d Mon Sep 17 00:00:00 2001 From: Eka Winata Date: Fri, 6 Dec 2024 12:02:05 +0700 Subject: [PATCH] test: add testing for OSSClient initialization --- .../blobstorage/oss/ObjectStorageService.java | 2 +- .../oss/ObjectStorageServiceTest.java | 68 ++++++++++++++++++- 2 files changed, 67 insertions(+), 3 deletions(-) rename src/test/java/com/gotocompany/firehose/sink/common/{ => blobstorage}/oss/ObjectStorageServiceTest.java (67%) diff --git a/src/main/java/com/gotocompany/firehose/sink/common/blobstorage/oss/ObjectStorageService.java b/src/main/java/com/gotocompany/firehose/sink/common/blobstorage/oss/ObjectStorageService.java index 97340edb..a981a78d 100644 --- a/src/main/java/com/gotocompany/firehose/sink/common/blobstorage/oss/ObjectStorageService.java +++ b/src/main/java/com/gotocompany/firehose/sink/common/blobstorage/oss/ObjectStorageService.java @@ -36,7 +36,7 @@ public ObjectStorageService(ObjectStorageServiceConfig objectStorageServiceConfi checkBucket(); } - private static OSS initializeOss(ObjectStorageServiceConfig objectStorageServiceConfig) { + protected static OSS initializeOss(ObjectStorageServiceConfig objectStorageServiceConfig) { ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); clientBuilderConfiguration.setSocketTimeout(objectStorageServiceConfig.getOssSocketTimeoutMs()); diff --git a/src/test/java/com/gotocompany/firehose/sink/common/oss/ObjectStorageServiceTest.java b/src/test/java/com/gotocompany/firehose/sink/common/blobstorage/oss/ObjectStorageServiceTest.java similarity index 67% rename from src/test/java/com/gotocompany/firehose/sink/common/oss/ObjectStorageServiceTest.java rename to src/test/java/com/gotocompany/firehose/sink/common/blobstorage/oss/ObjectStorageServiceTest.java index 81345a31..4835bbc0 100644 --- a/src/test/java/com/gotocompany/firehose/sink/common/oss/ObjectStorageServiceTest.java +++ b/src/test/java/com/gotocompany/firehose/sink/common/blobstorage/oss/ObjectStorageServiceTest.java @@ -1,15 +1,19 @@ -package com.gotocompany.firehose.sink.common.oss; +package com.gotocompany.firehose.sink.common.blobstorage.oss; import com.aliyun.oss.ClientException; import com.aliyun.oss.OSS; +import com.aliyun.oss.OSSClient; import com.aliyun.oss.OSSException; +import com.aliyun.oss.common.auth.Credentials; +import com.aliyun.oss.common.auth.DefaultCredentialProvider; +import com.aliyun.oss.common.comm.ServiceClient; +import com.aliyun.oss.internal.OSSOperation; import com.aliyun.oss.model.Bucket; import com.aliyun.oss.model.BucketList; import com.aliyun.oss.model.ListBucketsRequest; import com.aliyun.oss.model.PutObjectRequest; import com.gotocompany.firehose.config.ObjectStorageServiceConfig; import com.gotocompany.firehose.sink.common.blobstorage.BlobStorageException; -import com.gotocompany.firehose.sink.common.blobstorage.oss.ObjectStorageService; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.mockito.Mockito; @@ -18,6 +22,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collections; @@ -26,6 +31,65 @@ public class ObjectStorageServiceTest { + @Test + public void shouldInitializeOssGivenObjectStorageServiceConfig() throws NoSuchFieldException, IllegalAccessException { + ObjectStorageServiceConfig objectStorageServiceConfig = Mockito.mock(ObjectStorageServiceConfig.class); + when(objectStorageServiceConfig.getOssEndpoint()).thenReturn("http://localhost:9000"); + when(objectStorageServiceConfig.getOssRegion()).thenReturn("ap-southeast-5"); + when(objectStorageServiceConfig.getOssAccessId()).thenReturn("accessId"); + when(objectStorageServiceConfig.getOssAccessKey()).thenReturn("accessKey"); + + OSSClient oss = (OSSClient) ObjectStorageService.initializeOss(objectStorageServiceConfig); + Field credentialProviderField = oss.getClass().getDeclaredField("credsProvider"); + credentialProviderField.setAccessible(true); + Field credentialsField = DefaultCredentialProvider.class.getDeclaredField("creds"); + credentialsField.setAccessible(true); + Field endpointField = oss.getClass().getDeclaredField("endpoint"); + endpointField.setAccessible(true); + Field ossBucketOperationField = oss.getClass().getDeclaredField("bucketOperation"); + ossBucketOperationField.setAccessible(true); + Field region = OSSOperation.class.getDeclaredField("region"); + region.setAccessible(true); + + Credentials credentials = (Credentials) credentialsField.get(credentialProviderField.get(oss)); + assertEquals("http://localhost:9000", endpointField.get(oss).toString()); + assertEquals("ap-southeast-5", region.get(ossBucketOperationField.get(oss))); + assertEquals("accessId", credentials.getAccessKeyId()); + assertEquals("accessKey", credentials.getSecretAccessKey()); + } + + @Test + public void shouldInitializeOssGivenObjectStorageServiceConfigWithRetryConfig() throws NoSuchFieldException, IllegalAccessException { + ObjectStorageServiceConfig objectStorageServiceConfig = Mockito.mock(ObjectStorageServiceConfig.class); + when(objectStorageServiceConfig.getOssEndpoint()).thenReturn("http://localhost:9000"); + when(objectStorageServiceConfig.getOssRegion()).thenReturn("ap-southeast-5"); + when(objectStorageServiceConfig.getOssAccessId()).thenReturn("accessId"); + when(objectStorageServiceConfig.getOssAccessKey()).thenReturn("accessKey"); + when(objectStorageServiceConfig.isRetryEnabled()).thenReturn(true); + when(objectStorageServiceConfig.getOssMaxRetryAttempts()).thenReturn(3); + + OSSClient oss = (OSSClient) ObjectStorageService.initializeOss(objectStorageServiceConfig); + Field serviceClient = oss.getClass().getDeclaredField("serviceClient"); + serviceClient.setAccessible(true); + Field credentialProviderField = oss.getClass().getDeclaredField("credsProvider"); + credentialProviderField.setAccessible(true); + Field credentialsField = DefaultCredentialProvider.class.getDeclaredField("creds"); + credentialsField.setAccessible(true); + Field endpointField = oss.getClass().getDeclaredField("endpoint"); + endpointField.setAccessible(true); + Field ossBucketOperationField = oss.getClass().getDeclaredField("bucketOperation"); + ossBucketOperationField.setAccessible(true); + Field region = OSSOperation.class.getDeclaredField("region"); + region.setAccessible(true); + + Credentials credentials = (Credentials) credentialsField.get(credentialProviderField.get(oss)); + assertEquals("http://localhost:9000", endpointField.get(oss).toString()); + assertEquals("ap-southeast-5", region.get(ossBucketOperationField.get(oss))); + assertEquals("accessId", credentials.getAccessKeyId()); + assertEquals("accessKey", credentials.getSecretAccessKey()); + assertEquals(3, ((ServiceClient) serviceClient.get(oss)).getClientConfiguration().getMaxErrorRetry()); + } + @Test public void shouldStoreObjectGivenFilePath() throws BlobStorageException { ObjectStorageServiceConfig objectStorageServiceConfig = Mockito.mock(ObjectStorageServiceConfig.class);