Skip to content

Commit

Permalink
Migrate azure-storage-common to JUnit 5 and share common code (#38170)
Browse files Browse the repository at this point in the history
Migrate azure-storage-common to JUnit 5 and share common code
  • Loading branch information
alzimmermsft authored Jan 3, 2024
1 parent 4954b7c commit 428211c
Show file tree
Hide file tree
Showing 151 changed files with 3,198 additions and 4,380 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -38,7 +37,7 @@ final class BlobBatchOperationInfo {
*/
BlobBatchOperationInfo() {
this.contentId = new AtomicInteger();
this.batchBoundary = "batch_" + UUID.randomUUID();
this.batchBoundary = "batch_" + CoreUtils.randomUuid();
this.contentType = "multipart/mixed; boundary=" + batchBoundary;
this.batchOperations = new ConcurrentLinkedQueue<>();
this.batchOperationResponseMap = new ConcurrentHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
import com.azure.storage.common.sas.AccountSasService;
import com.azure.storage.common.sas.AccountSasSignatureValues;
import com.azure.storage.common.sas.SasProtocol;
import com.azure.storage.common.test.shared.extensions.RequiredServiceVersion;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIf;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand Down Expand Up @@ -115,7 +115,7 @@ public void setTierAllSucceed() {
assertEquals(200, response2.getStatusCode());
}

@DisabledIf("olderThan20191212ServiceVersion")
@RequiredServiceVersion(clazz = BlobServiceVersion.class, min = "2019-12-12")
@ParameterizedTest
@MethodSource("setTierRehydratePrioritySupplier")
public void setTierRehydratePriority(RehydratePriority rehydratePriority) {
Expand All @@ -140,7 +140,7 @@ private static Stream<RehydratePriority> setTierRehydratePrioritySupplier() {
return Stream.of(RehydratePriority.STANDARD, RehydratePriority.HIGH);
}

@DisabledIf("olderThan20191212ServiceVersion")
@RequiredServiceVersion(clazz = BlobServiceVersion.class, min = "2019-12-12")
@ParameterizedTest
@MethodSource("setTierAcSupplier")
public void setTierAc(String leaseId, String tags) {
Expand Down Expand Up @@ -600,7 +600,7 @@ public void submitBatchWithAccountSasCredentialsError() {
assertEquals(2, getIterableSize(ex.getBatchExceptions()));
}

@DisabledIf("olderThan20200612ServiceVersion")
@RequiredServiceVersion(clazz = BlobServiceVersion.class, min = "2020-06-12")
// Container scoped batch
@Test
public void setTierAllSucceedContainerScoped() {
Expand All @@ -621,7 +621,7 @@ public void setTierAllSucceedContainerScoped() {
assertEquals(200, response2.getStatusCode());
}

@DisabledIf("olderThan20200612ServiceVersion")
@RequiredServiceVersion(clazz = BlobServiceVersion.class, min = "2020-06-12")
@Test
public void deleteBlobAllSucceedContainerScoped() {
String containerName = generateContainerName();
Expand All @@ -641,7 +641,7 @@ public void deleteBlobAllSucceedContainerScoped() {
assertEquals(202, response2.getStatusCode());
}

@DisabledIf("olderThan20200612ServiceVersion")
@RequiredServiceVersion(clazz = BlobServiceVersion.class, min = "2020-06-12")
@Test
public void bulkDeleteBlobsContainerScoped() {
String containerName = generateContainerName();
Expand All @@ -660,7 +660,7 @@ public void bulkDeleteBlobsContainerScoped() {
}
}

@DisabledIf("olderThan20200612ServiceVersion")
@RequiredServiceVersion(clazz = BlobServiceVersion.class, min = "2020-06-12")
@Test
public void bulkSetAccessTierContainerScoped() {
String containerName = generateContainerName();
Expand Down Expand Up @@ -718,7 +718,7 @@ public void deleteBlobContainerScopedErrorWrongContainer() {
assertThrows(BlobStorageException.class, () -> batchClient.submitBatch(batch));
}

@DisabledIf("olderThan20200612ServiceVersion")
@RequiredServiceVersion(clazz = BlobServiceVersion.class, min = "2020-06-12")
@Test
public void submitBatchWithContainerSasCredentials() {
String containerName = generateContainerName();
Expand Down Expand Up @@ -762,7 +762,7 @@ public void submitBatchWithContainerSasCredentials() {
assertEquals(202, response2.getStatusCode());
}

@DisabledIf("olderThan20200612ServiceVersion")
@RequiredServiceVersion(clazz = BlobServiceVersion.class, min = "2020-06-12")
@Test
public void submitBatchWithContainerSasCredentialsError() {
String containerName = generateContainerName();
Expand Down Expand Up @@ -799,21 +799,4 @@ public void submitBatchWithContainerSasCredentialsError() {
() -> batchClient.submitBatch(batch));
assertEquals(2, getIterableSize(ex.getBatchExceptions()));
}

private static boolean olderThan20191212ServiceVersion() {
return olderThan(BlobServiceVersion.V2019_12_12);
}

private static boolean olderThan20200612ServiceVersion() {
return olderThan(BlobServiceVersion.V2020_06_12);
}

private static boolean olderThan(BlobServiceVersion targetVersion) {
String targetServiceVersionFromEnvironment = ENVIRONMENT.getServiceVersion();
BlobServiceVersion version = (targetServiceVersionFromEnvironment != null)
? Enum.valueOf(BlobServiceVersion.class, targetServiceVersionFromEnvironment)
: BlobServiceVersion.getLatest();

return version.ordinal() < targetVersion.ordinal();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@
package com.azure.storage.blob.batch;

import com.azure.core.client.traits.HttpTrait;
import com.azure.core.http.HttpClient;
import com.azure.core.http.netty.NettyAsyncHttpClientBuilder;
import com.azure.core.http.okhttp.OkHttpAsyncHttpClientBuilder;
import com.azure.core.http.policy.HttpPipelinePolicy;
import com.azure.core.test.TestMode;
import com.azure.core.test.TestProxyTestBase;
import com.azure.core.test.models.BodilessMatcher;
import com.azure.core.test.models.CustomMatcher;
import com.azure.core.test.models.TestProxySanitizer;
import com.azure.core.test.models.TestProxySanitizerType;
import com.azure.core.util.ServiceVersion;
import com.azure.core.util.CoreUtils;
import com.azure.identity.EnvironmentCredentialBuilder;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobContainerClientBuilder;
Expand All @@ -26,33 +23,21 @@
import com.azure.storage.blob.specialized.BlobLeaseClient;
import com.azure.storage.blob.specialized.BlobLeaseClientBuilder;
import com.azure.storage.common.StorageSharedKeyCredential;
import com.azure.storage.common.test.shared.ServiceVersionValidationPolicy;
import com.azure.storage.common.test.shared.StorageCommonTestUtils;
import com.azure.storage.common.test.shared.TestAccount;
import com.azure.storage.common.test.shared.TestDataFactory;
import com.azure.storage.common.test.shared.TestEnvironment;
import okhttp3.ConnectionPool;

import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.zip.CRC32;

public class BlobBatchTestBase extends TestProxyTestBase {
protected static final TestEnvironment ENVIRONMENT = TestEnvironment.getInstance();
protected static final TestDataFactory DATA = TestDataFactory.getInstance();
private static final HttpClient NETTY_HTTP_CLIENT = new NettyAsyncHttpClientBuilder().build();
private static final HttpClient OK_HTTP_CLIENT = new OkHttpAsyncHttpClientBuilder()
.connectionPool(new ConnectionPool(50, 5, TimeUnit.MINUTES))
.build();

protected static final String RECEIVED_LEASE_ID = "received";
protected static final String GARBAGE_LEASE_ID = UUID.randomUUID().toString();
protected static final String GARBAGE_LEASE_ID = CoreUtils.randomUuid().toString();

protected String prefix;

Expand All @@ -66,7 +51,7 @@ public class BlobBatchTestBase extends TestProxyTestBase {
@Override
public void beforeTest() {
super.beforeTest();
prefix = getCrc32(testContextManager.getTestPlaybackRecordingName());
prefix = StorageCommonTestUtils.getCrc32(testContextManager.getTestPlaybackRecordingName());

if (getTestMode() != TestMode.LIVE) {
interceptorManager.addSanitizers(Collections.singletonList(
Expand Down Expand Up @@ -200,67 +185,16 @@ protected String setupBlobLeaseCondition(BlobClientBase bc, String leaseID) {
return responseLeaseId;
}

protected static BlobLeaseClient createLeaseClient(BlobClientBase blobClient) {
return createLeaseClient(blobClient, null);
}

protected static BlobLeaseClient createLeaseClient(BlobClientBase blobClient, String leaseId) {
return new BlobLeaseClientBuilder()
.blobClient(blobClient)
.leaseId(leaseId)
.buildClient();
}

private static String getCrc32(String input) {
CRC32 crc32 = new CRC32();
crc32.update(input.getBytes(StandardCharsets.UTF_8));
return String.format(Locale.US, "%08X", crc32.getValue()).toLowerCase();
}

@SuppressWarnings("unchecked")
protected <T extends HttpTrait<T>, E extends Enum<E>> T instrument(T builder) {
// Groovy style reflection. All our builders follow this pattern.
builder.httpClient(getHttpClient());

if (interceptorManager.isRecordMode()) {
builder.addPolicy(interceptorManager.getRecordPolicy());
}

if (ENVIRONMENT.getServiceVersion() != null) {
try {
Method serviceVersionMethod = Arrays.stream(builder.getClass().getDeclaredMethods())
.filter(method -> "serviceVersion".equals(method.getName())
&& method.getParameterCount() == 1
&& ServiceVersion.class.isAssignableFrom(method.getParameterTypes()[0]))
.findFirst()
.orElseThrow(() -> new RuntimeException("Unable to find serviceVersion method for builder: "
+ builder.getClass()));
Class<E> serviceVersionClass = (Class<E>) serviceVersionMethod.getParameterTypes()[0];
ServiceVersion serviceVersion = (ServiceVersion) Enum.valueOf(serviceVersionClass,
ENVIRONMENT.getServiceVersion());
serviceVersionMethod.invoke(builder, serviceVersion);
builder.addPolicy(new ServiceVersionValidationPolicy(serviceVersion.getVersion()));
} catch (ReflectiveOperationException ex) {
throw new RuntimeException(ex);
}
}

return builder;
}

protected HttpClient getHttpClient() {
if (ENVIRONMENT.getTestMode() != TestMode.PLAYBACK) {
switch (ENVIRONMENT.getHttpClientType()) {
case NETTY:
return NETTY_HTTP_CLIENT;
case OK_HTTP:
return OK_HTTP_CLIENT;
default:
throw new IllegalArgumentException("Unknown http client type: " + ENVIRONMENT.getHttpClientType());
}
} else {
return interceptorManager.getPlaybackClient();
}
return StorageCommonTestUtils.instrument(builder, BlobServiceClientBuilder.getDefaultHttpLogOptions(),
interceptorManager);
}

protected String getPrimaryConnectionString() {
Expand All @@ -269,7 +203,7 @@ protected String getPrimaryConnectionString() {

protected static int getIterableSize(Iterable<?> iterable) {
if (iterable instanceof Collection<?>) {
return ((List<?>) iterable).size();
return ((Collection<?>) iterable).size();
} else {
int size = 0;
for (Object ignored : iterable) {
Expand Down
Loading

0 comments on commit 428211c

Please sign in to comment.