Skip to content

Commit 08aacfb

Browse files
committed
MSEARCH-1245: Fix bucket creation
1 parent 98b9849 commit 08aacfb

4 files changed

Lines changed: 97 additions & 0 deletions

File tree

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### Stories
44
* [MSEARCH-1245](https://issues.folio.org/browse/MSEARCH-1245) Provide http client configuration for MinioS3Client/AwsS3Client
5+
* [MSEARCH-1245](https://issues.folio.org/browse/MSEARCH-1245) Fix bucket creation
56

67
## v3.0.0 - Released
78
This release contains improvement client configuration and new features

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<aws.sdk.version>2.42.34</aws.sdk.version>
2929
<junit.version>6.0.3</junit.version>
3030
<testcontainers.version>2.0.4</testcontainers.version>
31+
<mockito.version>5.18.0</mockito.version>
3132
<commons-io.version>2.21.0</commons-io.version>
3233
</properties>
3334

@@ -86,6 +87,12 @@
8687
<version>${junit.version}</version>
8788
<scope>test</scope>
8889
</dependency>
90+
<dependency>
91+
<groupId>org.mockito</groupId>
92+
<artifactId>mockito-junit-jupiter</artifactId>
93+
<version>${mockito.version}</version>
94+
<scope>test</scope>
95+
</dependency>
8996
<dependency>
9097
<groupId>org.testcontainers</groupId>
9198
<artifactId>testcontainers</artifactId>

src/main/java/org/folio/s3/client/MinioS3Client.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public void createBucketIfNotExists() {
135135
.get();
136136
if (Boolean.TRUE.equals(exists)) {
137137
log.debug("Bucket already exists.");
138+
return;
138139
}
139140
client.makeBucket(MakeBucketArgs.builder()
140141
.bucket(bucket)
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)