alexr17 commented on code in PR #13126:
URL: https://github.com/apache/hudi/pull/13126#discussion_r2038830805


##########
hudi-aws/src/test/java/org/apache/hudi/aws/transaction/lock/TestS3StorageLockClient.java:
##########
@@ -0,0 +1,380 @@
+/*
+ * 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.hudi.aws.transaction.lock;
+
+import org.apache.hudi.client.transaction.lock.models.LockGetResult;
+import org.apache.hudi.client.transaction.lock.models.LockUpdateResult;
+import org.apache.hudi.client.transaction.lock.models.StorageLockData;
+import org.apache.hudi.client.transaction.lock.models.StorageLockFile;
+import org.apache.hudi.common.util.collection.Pair;
+import org.apache.hudi.exception.HoodieIOException;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.slf4j.Logger;
+import software.amazon.awssdk.awscore.exception.AwsServiceException;
+import software.amazon.awssdk.core.ResponseInputStream;
+import software.amazon.awssdk.core.sync.RequestBody;
+import software.amazon.awssdk.services.s3.S3Client;
+import software.amazon.awssdk.services.s3.model.GetObjectRequest;
+import software.amazon.awssdk.services.s3.model.GetObjectResponse;
+import software.amazon.awssdk.services.s3.model.PutObjectRequest;
+import software.amazon.awssdk.services.s3.model.PutObjectResponse;
+import software.amazon.awssdk.services.s3.model.S3Exception;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import static 
org.apache.hudi.client.transaction.lock.models.LockUpdateResult.UNKNOWN_ERROR;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.contains;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class TestS3StorageLockClient {
+
+  private static final String OWNER_ID = "ownerId";
+  private static final String BUCKET_NAME = "bucketName";
+  private static final String LOCK_FILE_PATH = "lockFilePath";
+
+  @Mock
+  private S3Client mockS3Client;
+
+  @Mock
+  private Logger mockLogger;
+
+  private S3StorageLockClient lockService;
+
+  @BeforeEach
+  void setUp() {
+    lockService = new S3StorageLockClient(
+            OWNER_ID,
+            BUCKET_NAME,
+            LOCK_FILE_PATH,
+            mockS3Client,
+            mockLogger
+    );
+  }
+
+  private void mockS3ObjectWithLockData(StorageLockData lockData, String eTag) 
{
+    byte[] bytes = StorageLockFile.toByteArray(lockData);
+    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
+
+    ResponseInputStream<GetObjectResponse> mockResponseStream = new 
ResponseInputStream<>(
+            GetObjectResponse.builder().eTag(eTag).build(),
+            new MockAbortableInputStream(bais)
+    );
+
+    
when(mockS3Client.getObject(any(GetObjectRequest.class))).thenReturn(mockResponseStream);
+  }
+
+  private static class MockAbortableInputStream extends 
java.io.FilterInputStream {
+    protected MockAbortableInputStream(ByteArrayInputStream in) {
+      super(in);
+    }
+  }
+
+  @Test
+  void testTryCreateOrUpdateLockFile_noPreviousLock_success() {
+    StorageLockData lockData = new StorageLockData(false, 
System.currentTimeMillis(), "myTxOwner");
+    PutObjectResponse putResp = 
PutObjectResponse.builder().eTag("new-etag-123").build();
+    when(mockS3Client.putObject(any(PutObjectRequest.class), 
any(RequestBody.class))).thenReturn(putResp);
+
+    Pair<LockUpdateResult, StorageLockFile> result = 
lockService.tryCreateOrUpdateLockFile(lockData, null);
+
+    assertEquals(LockUpdateResult.SUCCESS, result.getLeft());
+    assertNotNull(result.getRight());
+    assertEquals("new-etag-123", result.getRight().getVersionId());
+    verify(mockS3Client, times(1)).putObject(any(PutObjectRequest.class), 
any(RequestBody.class));
+    verifyNoMoreInteractions(mockLogger);
+  }
+
+  @Test
+  void testTryCreateOrUpdateLockFile_withPreviousLock_success() {
+    StorageLockData lockData = new StorageLockData(false, 1000L, "myTxOwner");
+    StorageLockFile prevLockFile = new StorageLockFile(lockData, 
"old-etag-999");
+    PutObjectResponse putResp = 
PutObjectResponse.builder().eTag("new-etag-456").build();
+    when(mockS3Client.putObject(
+            eq(PutObjectRequest.builder().bucket(BUCKET_NAME)
+                    .key(LOCK_FILE_PATH)
+                    .ifMatch("old-etag-999")
+                    .build()),
+            any(RequestBody.class)
+    )).thenReturn(putResp);
+
+    Pair<LockUpdateResult, StorageLockFile> result =
+            lockService.tryCreateOrUpdateLockFile(lockData, prevLockFile);
+
+    assertEquals(LockUpdateResult.SUCCESS, result.getLeft());
+    assertNotNull(result.getRight());
+    assertEquals("new-etag-456", result.getRight().getVersionId());
+    verify(mockS3Client, times(1)).putObject(any(PutObjectRequest.class), 
any(RequestBody.class));
+  }
+
+  @Test
+  void testTryCreateOrUpdateLockFile_preconditionFailed() {
+    StorageLockData lockData = new StorageLockData(false, 2000L, "txOwner");
+    StorageLockFile prevLockFile = new StorageLockFile(lockData, "some-etag");
+
+    AwsServiceException ex412 = S3Exception.builder().statusCode(412).build();
+    when(mockS3Client.putObject(any(PutObjectRequest.class), 
any(RequestBody.class))).thenThrow(ex412);

Review Comment:
   Yes



-- 
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]

Reply via email to