xingbowu commented on a change in pull request #3067:
URL: https://github.com/apache/iceberg/pull/3067#discussion_r707339576



##########
File path: 
aliyun/src/test/java/org/apache/iceberg/aliyun/oss/mock/TestLocalAliyunOSS.java
##########
@@ -0,0 +1,184 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.iceberg.aliyun.oss.mock;
+
+import com.aliyun.oss.OSS;
+import com.aliyun.oss.OSSErrorCode;
+import com.aliyun.oss.OSSException;
+import com.aliyun.oss.model.GetObjectRequest;
+import com.aliyun.oss.model.PutObjectResult;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Objects;
+import java.util.Random;
+import java.util.UUID;
+import org.apache.commons.io.IOUtils;
+import org.apache.iceberg.aliyun.oss.AliyunOSSTestRule;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+
+public class TestLocalAliyunOSS {
+
+  @ClassRule
+  public static final AliyunOSSTestRule OSS_TEST_RULE = 
AliyunOSSMockRule.builder().silent().build();
+
+  private final OSS oss = OSS_TEST_RULE.createOSSClient();
+  private final String bucketName = OSS_TEST_RULE.testBucketName();
+  private final Random random = new Random(1);
+
+  private static void assertThrows(Runnable runnable, String 
expectedErrorCode) {
+    try {
+      runnable.run();
+      Assert.fail("No exception was thrown, expected errorCode: " + 
expectedErrorCode);
+    } catch (OSSException e) {
+      Assert.assertEquals(expectedErrorCode, e.getErrorCode());
+    }
+  }
+
+  @Before
+  public void before() {
+    OSS_TEST_RULE.setUpBucket(bucketName);
+  }
+
+  @After
+  public void after() {
+    OSS_TEST_RULE.tearDownBucket(bucketName);
+  }
+
+  @Test
+  public void testBuckets() {
+    Assert.assertTrue(doesBucketExist(bucketName));
+    assertThrows(() -> oss.createBucket(bucketName), 
OSSErrorCode.BUCKET_ALREADY_EXISTS);
+
+    oss.deleteBucket(bucketName);
+    Assert.assertFalse(doesBucketExist(bucketName));
+
+    oss.createBucket(bucketName);
+    Assert.assertTrue(doesBucketExist(bucketName));
+  }
+
+  @Test
+  public void testDeleteBucket() {
+    String bucketNotExist = String.format("bucket-not-existing-%s", 
UUID.randomUUID());
+    assertThrows(() -> oss.deleteBucket(bucketNotExist), 
OSSErrorCode.NO_SUCH_BUCKET);
+
+    byte[] bytes = new byte[2000];
+    random.nextBytes(bytes);
+
+    oss.putObject(bucketName, "object1", wrap(bytes));
+
+    oss.putObject(bucketName, "object2", wrap(bytes));
+
+    assertThrows(() -> oss.deleteBucket(bucketName), 
OSSErrorCode.BUCKET_NOT_EMPTY);
+
+    oss.deleteObject(bucketName, "object1");
+    assertThrows(() -> oss.deleteBucket(bucketName), 
OSSErrorCode.BUCKET_NOT_EMPTY);
+
+    oss.deleteObject(bucketName, "object2");
+    oss.deleteBucket(bucketName);
+    Assert.assertFalse(doesBucketExist(bucketName));
+
+    oss.createBucket(bucketName);
+  }
+
+  @Test
+  public void testPutObject() throws IOException {
+    byte[] bytes = new byte[4 * 1024];
+    random.nextBytes(bytes);
+
+    String bucketNotExist = String.format("bucket-not-existing-%s", 
UUID.randomUUID());
+    assertThrows(() -> oss.putObject(bucketNotExist, "object", wrap(bytes)), 
OSSErrorCode.NO_SUCH_BUCKET);
+
+    PutObjectResult result = oss.putObject(bucketName, "object", wrap(bytes));
+    Assert.assertEquals(AliyunOSSMockLocalStore.md5sum(wrap(bytes)), 
result.getETag());
+  }
+
+  @Test
+  public void testDoesObjectExist() {
+    Assert.assertFalse(oss.doesObjectExist(bucketName, "key"));
+
+    Assert.assertFalse(oss.doesObjectExist(bucketName, "key"));
+
+    byte[] bytes = new byte[4 * 1024];
+    random.nextBytes(bytes);
+    oss.putObject(bucketName, "key", wrap(bytes));
+
+    Assert.assertTrue(oss.doesObjectExist(bucketName, "key"));
+    oss.deleteObject(bucketName, "key");
+  }
+
+  @Test
+  public void testGetObject() throws IOException {
+    String bucketNotExist = String.format("bucket-not-existing-%s", 
UUID.randomUUID());
+    assertThrows(() -> oss.getObject(bucketNotExist, "key"), 
OSSErrorCode.NO_SUCH_BUCKET);
+
+    assertThrows(() -> oss.getObject(bucketName, "key"), 
OSSErrorCode.NO_SUCH_KEY);
+
+    byte[] bytes = new byte[2000];
+    random.nextBytes(bytes);
+
+    oss.putObject(bucketName, "key", new ByteArrayInputStream(bytes));
+
+    byte[] actual = new byte[2000];
+    IOUtils.readFully(oss.getObject(bucketName, "key").getObjectContent(), 
actual);
+
+    Assert.assertArrayEquals(bytes, actual);
+    oss.deleteObject(bucketName, "key");
+  }
+
+  @Test
+  public void testGetObjectWithRange() throws IOException {
+
+    byte[] bytes = new byte[2000];
+    random.nextBytes(bytes);
+    oss.putObject(bucketName, "key", new ByteArrayInputStream(bytes));
+
+    byte[] actual = new byte[2000];
+    int start = 0;
+    int end = 1999;
+    GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, 
"key");
+    getObjectRequest.setRange(start, end);
+    IOUtils.readFully(oss.getObject(getObjectRequest).getObjectContent(), 
actual);
+
+    Assert.assertArrayEquals(bytes, actual);
+    oss.deleteObject(bucketName, "key");
+  }
+
+  private InputStream wrap(byte[] data) {
+    return new ByteArrayInputStream(data);
+  }
+
+  private boolean doesBucketExist(String bucket) {
+    try {
+      oss.createBucket(bucket);

Review comment:
       it is the simplest way to simulate this api behavior.  we can mock 
further version if necessary later per upper layer need. may keep it as it is 
here.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to