|
| 1 | +package org.folio.s3.client; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | +import static org.mockito.ArgumentMatchers.any; |
| 6 | +import static org.mockito.Mockito.never; |
| 7 | +import static org.mockito.Mockito.verify; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import java.util.concurrent.CompletableFuture; |
| 11 | + |
| 12 | +import org.folio.s3.client.impl.ExtendedMinioAsyncClient; |
| 13 | +import org.folio.s3.exception.S3ClientException; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 16 | +import org.mockito.Mock; |
| 17 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 18 | + |
| 19 | +import io.minio.BucketExistsArgs; |
| 20 | +import io.minio.MakeBucketArgs; |
| 21 | + |
| 22 | +@ExtendWith(MockitoExtension.class) |
| 23 | +class MinioS3ClientCreateBucketTest { |
| 24 | + |
| 25 | + private static final String BUCKET = "test-bucket"; |
| 26 | + private static final String REGION = "us-east-1"; |
| 27 | + |
| 28 | + @Mock |
| 29 | + private ExtendedMinioAsyncClient minioClient; |
| 30 | + |
| 31 | + private MinioS3Client buildClient(String bucket) { |
| 32 | + var props = S3ClientProperties.builder() |
| 33 | + .endpoint("http://localhost:9000") |
| 34 | + .region(REGION) |
| 35 | + .bucket(bucket) |
| 36 | + .accessKey("ak") |
| 37 | + .secretKey("sk") |
| 38 | + .build(); |
| 39 | + return new MinioS3Client(props, minioClient); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void createBucketIfNotExists_whenBucketNameIsBlank_doesNothing() throws Exception { |
| 44 | + var client = buildClient(" "); |
| 45 | + |
| 46 | + assertDoesNotThrow(client::createBucketIfNotExists); |
| 47 | + |
| 48 | + verify(minioClient, never()).bucketExists(any(BucketExistsArgs.class)); |
| 49 | + verify(minioClient, never()).makeBucket(any(MakeBucketArgs.class)); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void createBucketIfNotExists_whenBucketAlreadyExists_doesNotCallMakeBucket() throws Exception { |
| 54 | + when(minioClient.bucketExists(any(BucketExistsArgs.class))) |
| 55 | + .thenReturn(CompletableFuture.completedFuture(true)); |
| 56 | + |
| 57 | + var client = buildClient(BUCKET); |
| 58 | + assertDoesNotThrow(client::createBucketIfNotExists); |
| 59 | + |
| 60 | + verify(minioClient).bucketExists(any(BucketExistsArgs.class)); |
| 61 | + verify(minioClient, never()).makeBucket(any(MakeBucketArgs.class)); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void createBucketIfNotExists_whenBucketDoesNotExist_createsBucket() throws Exception { |
| 66 | + when(minioClient.bucketExists(any(BucketExistsArgs.class))) |
| 67 | + .thenReturn(CompletableFuture.completedFuture(false)); |
| 68 | + when(minioClient.makeBucket(any(MakeBucketArgs.class))) |
| 69 | + .thenReturn(CompletableFuture.completedFuture(null)); |
| 70 | + |
| 71 | + var client = buildClient(BUCKET); |
| 72 | + assertDoesNotThrow(client::createBucketIfNotExists); |
| 73 | + |
| 74 | + verify(minioClient).bucketExists(any(BucketExistsArgs.class)); |
| 75 | + verify(minioClient).makeBucket(any(MakeBucketArgs.class)); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void createBucketIfNotExists_whenBucketExistsCheckThrows_throwsS3ClientException() throws Exception { |
| 80 | + when(minioClient.bucketExists(any(BucketExistsArgs.class))) |
| 81 | + .thenReturn(CompletableFuture.failedFuture(new RuntimeException("connection refused"))); |
| 82 | + |
| 83 | + var client = buildClient(BUCKET); |
| 84 | + |
| 85 | + assertThrows(S3ClientException.class, client::createBucketIfNotExists); |
| 86 | + verify(minioClient, never()).makeBucket(any(MakeBucketArgs.class)); |
| 87 | + } |
| 88 | +} |
0 commit comments