Skip to content

Commit

Permalink
test: add testing for OSSClient initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ekawinataa committed Dec 6, 2024
1 parent ca2dc7f commit 3dec049
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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);
Expand Down

0 comments on commit 3dec049

Please sign in to comment.