Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
SUBMARINE-1349. Change the enum class for singleton and add unit-test…
Browse files Browse the repository at this point in the history
… to cover it.
  • Loading branch information
hhcs9527 committed Feb 1, 2023
1 parent 53edebb commit 86cc8cf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import java.util.List;
import java.util.Stack;
import javax.ws.rs.core.Response;
import java.util.Map;
import java.util.HashMap;

import io.minio.CopyObjectArgs;
import io.minio.CopySource;
Expand All @@ -48,26 +46,15 @@
* S3(Minio) default client
*/
public enum Client {
DEFAULT(SubmarineConfiguration.getInstance().getString(SubmarineConfVars.ConfVars.SUBMARINE_S3_ENDPOINT)),
CUSTOMER("http://localhost:9000");
INSTANCE(SubmarineConfiguration.getInstance().getString(SubmarineConfVars.ConfVars.SUBMARINE_S3_ENDPOINT));

/* submarine config */
private final SubmarineConfiguration conf = SubmarineConfiguration.getInstance();

/* minio client */
private final MinioClient minioClient;

public static Map<String, Client> clientFactory = new HashMap<String, Client>();
private final String endpoint;

static {
for (Client clientSingleton : Client.values()) {
clientFactory.put(clientSingleton.endpoint, clientSingleton);
}
}

Client(String endpoint) {
this.endpoint = endpoint;
this.minioClient = MinioClient.builder()
.endpoint(endpoint)
.credentials(
Expand All @@ -76,28 +63,8 @@ public enum Client {
).build();
}

Client() {
this.endpoint = conf.getString(SubmarineConfVars.ConfVars.SUBMARINE_S3_ENDPOINT);
this.minioClient = MinioClient.builder()
.endpoint(conf.getString(SubmarineConfVars.ConfVars.SUBMARINE_S3_ENDPOINT))
.credentials(
conf.getString(SubmarineConfVars.ConfVars.SUBMARINE_S3_ACCESS_KEY_ID),
conf.getString(SubmarineConfVars.ConfVars.SUBMARINE_S3_SECRET_ACCESS_KEY)
).build();
}

public static Client getInstance() {
return clientFactory.get(SubmarineConfiguration.getInstance()
.getString(SubmarineConfVars.ConfVars.SUBMARINE_S3_ENDPOINT));
}

public static Client getInstance(String endpoint) {
try {
return clientFactory.get(endpoint);
} catch (Exception e) {
throw new SubmarineRuntimeException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
e.getMessage());
}
return INSTANCE;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@


public class ClientTest {
private final Client client = Client.getInstance("http://localhost:9000");
private final Client client = Client.getInstance();
private final String testExperimentId = "experiment-sample";

@After
Expand Down Expand Up @@ -79,4 +79,10 @@ public void testCopyObject() {
response = client.downloadArtifact(copyPath);
Assert.assertArrayEquals(content, response);
}

@Test
public void testSingleton() {
Client testClient = Client.getInstance();
Assert.assertEquals(testClient, client);
}
}

0 comments on commit 86cc8cf

Please sign in to comment.