Skip to content

Commit

Permalink
Aliyun: Switch iceberg-aliyun's tests to JUnit5 (apache#9122)
Browse files Browse the repository at this point in the history
  • Loading branch information
lisirrx authored Dec 5, 2023
1 parent cbd33a6 commit b4c050b
Show file tree
Hide file tree
Showing 14 changed files with 214 additions and 199 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,44 @@
import java.util.Map;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.junit.Assert;
import org.junit.Test;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

public class TestAliyunClientFactories {

@Test
public void testLoadDefault() {
Assert.assertEquals(
"Default client should be singleton",
AliyunClientFactories.defaultFactory(),
AliyunClientFactories.defaultFactory());
Assertions.assertThat(AliyunClientFactories.defaultFactory())
.as("Default client should be singleton")
.isEqualTo(AliyunClientFactories.defaultFactory());

AliyunClientFactory defaultFactory = AliyunClientFactories.from(Maps.newHashMap());
Assert.assertTrue(
"Should load default when factory impl not configured",
defaultFactory instanceof AliyunClientFactories.DefaultAliyunClientFactory);
Assert.assertNull(
"Should have no Aliyun properties set", defaultFactory.aliyunProperties().accessKeyId());
Assertions.assertThat(defaultFactory)
.as("Should load default when factory impl not configured")
.isInstanceOf(AliyunClientFactories.DefaultAliyunClientFactory.class);

Assertions.assertThat(defaultFactory.aliyunProperties().accessKeyId())
.as("Should have no Aliyun properties set")
.isNull();

AliyunClientFactory defaultFactoryWithConfig =
AliyunClientFactories.from(ImmutableMap.of(AliyunProperties.CLIENT_ACCESS_KEY_ID, "key"));
Assert.assertTrue(
"Should load default when factory impl not configured",
defaultFactoryWithConfig instanceof AliyunClientFactories.DefaultAliyunClientFactory);
Assert.assertEquals(
"Should have access key set",
"key",
defaultFactoryWithConfig.aliyunProperties().accessKeyId());
Assertions.assertThat(defaultFactoryWithConfig)
.as("Should load default when factory impl not configured")
.isInstanceOf(AliyunClientFactories.DefaultAliyunClientFactory.class);

Assertions.assertThat(defaultFactoryWithConfig.aliyunProperties().accessKeyId())
.as("Should have access key set")
.isEqualTo("key");
}

@Test
public void testLoadCustom() {
Map<String, String> properties = Maps.newHashMap();
properties.put(AliyunProperties.CLIENT_FACTORY, CustomFactory.class.getName());
Assert.assertTrue(
"Should load custom class",
AliyunClientFactories.from(properties) instanceof CustomFactory);
Assertions.assertThat(AliyunClientFactories.from(properties))
.as("Should load custom class")
.isInstanceOf(CustomFactory.class);
}

public static class CustomFactory implements AliyunClientFactory {
Expand Down
27 changes: 14 additions & 13 deletions aliyun/src/test/java/org/apache/iceberg/aliyun/TestUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
*/
package org.apache.iceberg.aliyun;

import org.apache.iceberg.aliyun.oss.AliyunOSSTestRule;
import org.apache.iceberg.aliyun.oss.AliyunOSSExtension;
import org.apache.iceberg.aliyun.oss.OSSURI;
import org.apache.iceberg.aliyun.oss.mock.AliyunOSSMockRule;
import org.apache.iceberg.aliyun.oss.mock.AliyunOSSMockExtension;
import org.apache.iceberg.common.DynConstructors;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.relocated.com.google.common.base.Strings;
Expand All @@ -41,33 +41,34 @@ public class TestUtility {

private TestUtility() {}

public static AliyunOSSTestRule initialize() {
AliyunOSSTestRule testRule;
public static AliyunOSSExtension initialize() {
AliyunOSSExtension extension;

String implClass = System.getenv(ALIYUN_TEST_OSS_RULE_CLASS);
if (!Strings.isNullOrEmpty(implClass)) {
LOG.info("The initializing AliyunOSSTestRule implementation is: {}", implClass);
LOG.info("The initializing AliyunOSSExtension implementation is: {}", implClass);
try {
DynConstructors.Ctor<AliyunOSSTestRule> ctor =
DynConstructors.builder(AliyunOSSTestRule.class).impl(implClass).buildChecked();
testRule = ctor.newInstance();
DynConstructors.Ctor<AliyunOSSExtension> ctor =
DynConstructors.builder(AliyunOSSExtension.class).impl(implClass).buildChecked();
extension = ctor.newInstance();
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException(
String.format(
"Cannot initialize AliyunOSSTestRule, missing no-arg constructor: %s", implClass),
"Cannot initialize AliyunOSSExtension, missing no-arg constructor: %s", implClass),
e);
} catch (ClassCastException e) {
throw new IllegalArgumentException(
String.format(
"Cannot initialize AliyunOSSTestRule, %s does not implement it.", implClass),
"Cannot initialize AliyunOSSExtension, %s does not implement it.", implClass),
e);
}
} else {
LOG.info("Initializing AliyunOSSTestRule implementation with default AliyunOSSMockRule");
testRule = AliyunOSSMockRule.builder().silent().build();
LOG.info(
"Initializing AliyunOSSExtension implementation with default AliyunOSSMockExtension");
extension = AliyunOSSMockExtension.builder().silent().build();
}

return testRule;
return extension;
}

public static String accessKeyId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@

import com.aliyun.oss.OSS;
import java.util.UUID;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

/**
* API for test Aliyun Object Storage Service (OSS) which is either local mock http server or remote
* aliyun oss server
*
* <p>This API includes start,stop OSS service, create OSS client, setup bucket and teardown bucket.
*/
public interface AliyunOSSTestRule extends TestRule {
public interface AliyunOSSExtension extends BeforeAllCallback, AfterAllCallback {
UUID RANDOM_UUID = java.util.UUID.randomUUID();

/** Returns a specific bucket name for testing purpose. */
Expand All @@ -39,18 +39,13 @@ default String testBucketName() {
}

@Override
default Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
start();
try {
base.evaluate();
} finally {
stop();
}
}
};
default void afterAll(ExtensionContext context) throws Exception {
stop();
}

@Override
default void beforeAll(ExtensionContext context) throws Exception {
start();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,26 @@
import com.aliyun.oss.OSS;
import org.apache.iceberg.aliyun.TestUtility;
import org.apache.iceberg.util.SerializableSupplier;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.RegisterExtension;

public abstract class AliyunOSSTestBase {
@ClassRule public static final AliyunOSSTestRule OSS_TEST_RULE = TestUtility.initialize();
@RegisterExtension
private static final AliyunOSSExtension OSS_TEST_EXTENSION = TestUtility.initialize();

private final SerializableSupplier<OSS> ossClient = OSS_TEST_RULE::createOSSClient;
private final String bucketName = OSS_TEST_RULE.testBucketName();
private final String keyPrefix = OSS_TEST_RULE.keyPrefix();
private final SerializableSupplier<OSS> ossClient = OSS_TEST_EXTENSION::createOSSClient;
private final String bucketName = OSS_TEST_EXTENSION.testBucketName();
private final String keyPrefix = OSS_TEST_EXTENSION.keyPrefix();

@Before
@BeforeEach
public void before() {
OSS_TEST_RULE.setUpBucket(bucketName);
OSS_TEST_EXTENSION.setUpBucket(bucketName);
}

@After
@AfterEach
public void after() {
OSS_TEST_RULE.tearDownBucket(bucketName);
OSS_TEST_EXTENSION.tearDownBucket(bucketName);
}

protected String location(String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.apache.iceberg.aliyun.TestUtility;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;

public class OSSIntegrationTestRule implements AliyunOSSTestRule {
public class OSSIntegrationExtension implements AliyunOSSExtension {
// Aliyun access key pair.
private String accessKeyId;
private String accessKeySecret;
Expand Down Expand Up @@ -106,7 +106,7 @@ public void tearDownBucket(String bucket) {

private OSS ossClient() {
if (lazyClient == null) {
synchronized (OSSIntegrationTestRule.class) {
synchronized (OSSIntegrationExtension.class) {
if (lazyClient == null) {
lazyClient = createOSSClient();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
import org.apache.iceberg.relocated.com.google.common.io.ByteStreams;
import org.apache.iceberg.util.SerializableSupplier;
import org.apache.iceberg.util.SerializationUtil;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class TestOSSFileIO extends AliyunOSSTestBase {
private static final String OSS_IMPL_CLASS = OSSFileIO.class.getName();
Expand All @@ -51,12 +51,12 @@ public class TestOSSFileIO extends AliyunOSSTestBase {

private FileIO fileIO;

@Before
@BeforeEach
public void beforeFile() {
fileIO = new OSSFileIO(ossClient());
}

@After
@AfterEach
public void afterFile() {
if (fileIO != null) {
fileIO.close();
Expand All @@ -73,28 +73,33 @@ public void testOutputFile() throws IOException {
writeOSSData(out, data);

OSSURI uri = new OSSURI(location);
Assert.assertTrue(
"OSS file should exist", ossClient().get().doesObjectExist(uri.bucket(), uri.key()));
Assert.assertEquals("Should have expected location", location, out.location());
Assert.assertEquals("Should have expected length", dataSize, ossDataLength(uri));
Assert.assertArrayEquals("Should have expected content", data, ossDataContent(uri, dataSize));
Assertions.assertThat(ossClient().get().doesObjectExist(uri.bucket(), uri.key()))
.as("OSS file should exist")
.isTrue();
Assertions.assertThat(out.location()).as("Should have expected location").isEqualTo(location);
Assertions.assertThat(ossDataLength(uri)).as("Should have expected length").isEqualTo(dataSize);
Assertions.assertThat(ossDataContent(uri, dataSize))
.as("Should have expected content")
.isEqualTo(data);
}

@Test
public void testInputFile() throws IOException {
String location = randomLocation();
InputFile in = fileIO().newInputFile(location);
Assert.assertFalse("OSS file should not exist", in.exists());
Assertions.assertThat(in.exists()).as("OSS file should not exist").isFalse();

int dataSize = 1024 * 10;
byte[] data = randomData(dataSize);
OutputFile out = fileIO().newOutputFile(location);
writeOSSData(out, data);

Assert.assertTrue("OSS file should exist", in.exists());
Assert.assertEquals("Should have expected location", location, in.location());
Assert.assertEquals("Should have expected length", dataSize, in.getLength());
Assert.assertArrayEquals("Should have expected content", data, inFileContent(in, dataSize));
Assertions.assertThat(in.exists()).as("OSS file should exist").isTrue();
Assertions.assertThat(in.location()).as("Should have expected location").isEqualTo(location);
Assertions.assertThat(in.getLength()).as("Should have expected length").isEqualTo(dataSize);
Assertions.assertThat(inFileContent(in, dataSize))
.as("Should have expected content")
.isEqualTo(data);
}

@Test
Expand All @@ -106,20 +111,24 @@ public void testDeleteFile() throws IOException {
writeOSSData(out, data);

InputFile in = fileIO().newInputFile(location);
Assert.assertTrue("OSS file should exist", in.exists());
Assertions.assertThat(in.exists()).as("OSS file should exist").isTrue();

fileIO().deleteFile(in);
Assert.assertFalse("OSS file should not exist", fileIO().newInputFile(location).exists());
Assertions.assertThat(fileIO().newInputFile(location).exists())
.as("OSS file should not exist")
.isFalse();
}

@Test
public void testLoadFileIO() {
FileIO file = CatalogUtil.loadFileIO(OSS_IMPL_CLASS, ImmutableMap.of(), conf);
Assert.assertTrue("Should be OSSFileIO", file instanceof OSSFileIO);
Assertions.assertThat(file).as("Should be OSSFileIO").isInstanceOf(OSSFileIO.class);

byte[] data = SerializationUtil.serializeToBytes(file);
FileIO expectedFileIO = SerializationUtil.deserializeFromBytes(data);
Assert.assertTrue(
"The deserialized FileIO should be OSSFileIO", expectedFileIO instanceof OSSFileIO);
Assertions.assertThat(expectedFileIO)
.as("The deserialized FileIO should be OSSFileIO")
.isInstanceOf(OSSFileIO.class);
}

@Test
Expand All @@ -134,19 +143,21 @@ public void serializeClient() throws URISyntaxException {
SerializableSupplier<OSS> post = SerializationUtil.deserializeFromBytes(data);

OSS client = post.get();
Assert.assertTrue("Should be instance of oss client", client instanceof OSSClient);
Assertions.assertThat(client)
.as("Should be instance of oss client")
.isInstanceOf(OSSClient.class);

OSSClient oss = (OSSClient) client;
Assert.assertEquals(
"Should have expected endpoint", new URI("http://" + endpoint), oss.getEndpoint());
Assert.assertEquals(
"Should have expected access key",
accessKeyId,
oss.getCredentialsProvider().getCredentials().getAccessKeyId());
Assert.assertEquals(
"Should have expected secret key",
accessSecret,
oss.getCredentialsProvider().getCredentials().getSecretAccessKey());
Assertions.assertThat(oss.getEndpoint())
.as("Should have expected endpoint")
.isEqualTo(new URI("http://" + endpoint));

Assertions.assertThat(oss.getCredentialsProvider().getCredentials().getAccessKeyId())
.as("Should have expected access key")
.isEqualTo(accessKeyId);
Assertions.assertThat(oss.getCredentialsProvider().getCredentials().getSecretAccessKey())
.as("Should have expected secret key")
.isEqualTo(accessSecret);
}

private FileIO fileIO() {
Expand Down
Loading

0 comments on commit b4c050b

Please sign in to comment.