From 99c23759ba7bcd6b0461b75679a74ae47ee26431 Mon Sep 17 00:00:00 2001 From: Mitchell Gale Date: Thu, 10 Aug 2023 12:23:14 -0700 Subject: [PATCH] [Spotless] Applying Google Code Format for datasource files #6 (#1939) * Spotless apply for datasources. Signed-off-by: Mitchell Gale * ignore checkstyle for datasources Signed-off-by: Mitchell Gale --------- Signed-off-by: Mitchell Gale Signed-off-by: Mitchell Gale Signed-off-by: Mitchell Gale --- build.gradle | 3 +- datasources/build.gradle | 3 + .../datasources/auth/AuthenticationType.java | 4 +- .../DataSourceUserAuthorizationHelper.java | 5 +- ...DataSourceUserAuthorizationHelperImpl.java | 23 +- .../sql/datasources/encryptor/Encryptor.java | 1 - .../datasources/encryptor/EncryptorImpl.java | 42 +-- .../DataSourceNotFoundException.java | 5 +- .../datasources/exceptions/ErrorMessage.java | 18 +- .../CreateDataSourceActionRequest.java | 16 +- .../CreateDataSourceActionResponse.java | 6 +- .../DeleteDataSourceActionRequest.java | 12 +- .../DeleteDataSourceActionResponse.java | 4 +- .../transport/GetDataSourceActionRequest.java | 13 +- .../GetDataSourceActionResponse.java | 4 +- .../UpdateDataSourceActionRequest.java | 12 +- .../UpdateDataSourceActionResponse.java | 6 +- .../rest/RestDataSourceQueryAction.java | 206 ++++++++------ .../service/DataSourceLoaderCache.java | 5 +- .../service/DataSourceLoaderCacheImpl.java | 25 +- .../service/DataSourceMetadataStorage.java | 19 +- .../service/DataSourceServiceImpl.java | 41 ++- .../OpenSearchDataSourceMetadataStorage.java | 142 +++++----- .../TransportCreateDataSourceAction.java | 32 ++- .../TransportDeleteDataSourceAction.java | 32 ++- .../TransportGetDataSourceAction.java | 61 ++-- .../TransportUpdateDataSourceAction.java | 28 +- .../utils/XContentParserUtils.java | 16 +- .../auth/AuthenticationTypeTest.java | 1 - ...SourceUserAuthorizationHelperImplTest.java | 60 ++-- .../encryptor/EncryptorImplTest.java | 66 +++-- .../DataSourceLoaderCacheImplTest.java | 11 +- .../service/DataSourceServiceImplTest.java | 235 +++++++-------- ...enSearchDataSourceMetadataStorageTest.java | 268 ++++++++---------- .../TransportCreateDataSourceActionTest.java | 38 ++- .../TransportDeleteDataSourceActionTest.java | 34 +-- .../TransportGetDataSourceActionTest.java | 49 ++-- .../TransportUpdateDataSourceActionTest.java | 37 ++- .../sql/datasources/utils/SchedulerTest.java | 17 +- .../utils/XContentParserUtilsTest.java | 32 ++- 40 files changed, 813 insertions(+), 819 deletions(-) diff --git a/build.gradle b/build.gradle index 0c58fccfeb..f623b5da4e 100644 --- a/build.gradle +++ b/build.gradle @@ -84,7 +84,8 @@ repositories { spotless { java { target fileTree('.') { - include 'core/**/*.java' + include 'datasources/**/*.java', + 'core/**/*.java' exclude '**/build/**', '**/build-*/**' } importOrder() diff --git a/datasources/build.gradle b/datasources/build.gradle index ef52db2305..830fadbc35 100644 --- a/datasources/build.gradle +++ b/datasources/build.gradle @@ -31,6 +31,9 @@ dependencies { testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2' } +checkstyleTest.ignoreFailures = true +checkstyleMain.ignoreFailures = true + test { useJUnitPlatform() testLogging { diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/auth/AuthenticationType.java b/datasources/src/main/java/org/opensearch/sql/datasources/auth/AuthenticationType.java index 715e72c0c3..b6581608bf 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/auth/AuthenticationType.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/auth/AuthenticationType.java @@ -12,8 +12,8 @@ import java.util.Map; public enum AuthenticationType { - - BASICAUTH("basicauth"), AWSSIGV4AUTH("awssigv4"); + BASICAUTH("basicauth"), + AWSSIGV4AUTH("awssigv4"); private String name; diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelper.java b/datasources/src/main/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelper.java index adcfb0bdfd..75d0ec8539 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelper.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelper.java @@ -8,9 +8,8 @@ import org.opensearch.sql.datasource.model.DataSourceMetadata; /** - * Interface for datasource authorization helper. - * The implementation of this class helps in determining - * if authorization is required and the roles associated with the user. + * Interface for datasource authorization helper. The implementation of this class helps in + * determining if authorization is required and the roles associated with the user. */ public interface DataSourceUserAuthorizationHelper { diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelperImpl.java b/datasources/src/main/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelperImpl.java index cd55991d00..67d747f0bf 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelperImpl.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelperImpl.java @@ -19,36 +19,39 @@ public class DataSourceUserAuthorizationHelperImpl implements DataSourceUserAuth private final Client client; private Boolean isAuthorizationRequired() { - String userString = client.threadPool() - .getThreadContext().getTransient( - ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT); + String userString = + client + .threadPool() + .getThreadContext() + .getTransient(ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT); return userString != null; } private List getUserRoles() { - String userString = client.threadPool() - .getThreadContext().getTransient( - ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT); + String userString = + client + .threadPool() + .getThreadContext() + .getTransient(ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT); User user = User.parse(userString); return user.getRoles(); } - @Override public void authorizeDataSource(DataSourceMetadata dataSourceMetadata) { if (isAuthorizationRequired() && !dataSourceMetadata.getName().equals(DEFAULT_DATASOURCE_NAME)) { boolean isAuthorized = false; for (String role : getUserRoles()) { - if (dataSourceMetadata.getAllowedRoles().contains(role) - || role.equals("all_access")) { + if (dataSourceMetadata.getAllowedRoles().contains(role) || role.equals("all_access")) { isAuthorized = true; break; } } if (!isAuthorized) { throw new SecurityException( - String.format("User is not authorized to access datasource %s. " + String.format( + "User is not authorized to access datasource %s. " + "User should be mapped to any of the roles in %s for access.", dataSourceMetadata.getName(), dataSourceMetadata.getAllowedRoles().toString())); } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/encryptor/Encryptor.java b/datasources/src/main/java/org/opensearch/sql/datasources/encryptor/Encryptor.java index 578b66d0ba..4572b45f53 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/encryptor/Encryptor.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/encryptor/Encryptor.java @@ -24,5 +24,4 @@ public interface Encryptor { * @return String plainText. */ String decrypt(String encryptedText); - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/encryptor/EncryptorImpl.java b/datasources/src/main/java/org/opensearch/sql/datasources/encryptor/EncryptorImpl.java index 98f693eca1..c6abe78394 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/encryptor/EncryptorImpl.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/encryptor/EncryptorImpl.java @@ -25,32 +25,40 @@ public class EncryptorImpl implements Encryptor { @Override public String encrypt(String plainText) { validate(masterKey); - final AwsCrypto crypto = AwsCrypto.builder() - .withCommitmentPolicy(CommitmentPolicy.RequireEncryptRequireDecrypt) - .build(); + final AwsCrypto crypto = + AwsCrypto.builder() + .withCommitmentPolicy(CommitmentPolicy.RequireEncryptRequireDecrypt) + .build(); - JceMasterKey jceMasterKey - = JceMasterKey.getInstance(new SecretKeySpec(masterKey.getBytes(), "AES"), "Custom", - "opensearch.config.master.key", "AES/GCM/NoPadding"); + JceMasterKey jceMasterKey = + JceMasterKey.getInstance( + new SecretKeySpec(masterKey.getBytes(), "AES"), + "Custom", + "opensearch.config.master.key", + "AES/GCM/NoPadding"); - final CryptoResult encryptResult = crypto.encryptData(jceMasterKey, - plainText.getBytes(StandardCharsets.UTF_8)); + final CryptoResult encryptResult = + crypto.encryptData(jceMasterKey, plainText.getBytes(StandardCharsets.UTF_8)); return Base64.getEncoder().encodeToString(encryptResult.getResult()); } @Override public String decrypt(String encryptedText) { validate(masterKey); - final AwsCrypto crypto = AwsCrypto.builder() - .withCommitmentPolicy(CommitmentPolicy.RequireEncryptRequireDecrypt) - .build(); + final AwsCrypto crypto = + AwsCrypto.builder() + .withCommitmentPolicy(CommitmentPolicy.RequireEncryptRequireDecrypt) + .build(); - JceMasterKey jceMasterKey - = JceMasterKey.getInstance(new SecretKeySpec(masterKey.getBytes(), "AES"), "Custom", - "opensearch.config.master.key", "AES/GCM/NoPadding"); + JceMasterKey jceMasterKey = + JceMasterKey.getInstance( + new SecretKeySpec(masterKey.getBytes(), "AES"), + "Custom", + "opensearch.config.master.key", + "AES/GCM/NoPadding"); - final CryptoResult decryptedResult - = crypto.decryptData(jceMasterKey, Base64.getDecoder().decode(encryptedText)); + final CryptoResult decryptedResult = + crypto.decryptData(jceMasterKey, Base64.getDecoder().decode(encryptedText)); return new String(decryptedResult.getResult()); } @@ -65,6 +73,4 @@ private void validate(String masterKey) { + "admin/datasources.rst#master-key-config-for-encrypting-credential-information"); } } - - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/exceptions/DataSourceNotFoundException.java b/datasources/src/main/java/org/opensearch/sql/datasources/exceptions/DataSourceNotFoundException.java index 484b0b92b2..40b601000c 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/exceptions/DataSourceNotFoundException.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/exceptions/DataSourceNotFoundException.java @@ -7,12 +7,9 @@ package org.opensearch.sql.datasources.exceptions; -/** - * DataSourceNotFoundException. - */ +/** DataSourceNotFoundException. */ public class DataSourceNotFoundException extends RuntimeException { public DataSourceNotFoundException(String message) { super(message); } - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/exceptions/ErrorMessage.java b/datasources/src/main/java/org/opensearch/sql/datasources/exceptions/ErrorMessage.java index 265b3ddf31..386eb780cd 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/exceptions/ErrorMessage.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/exceptions/ErrorMessage.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.datasources.exceptions; import com.google.gson.Gson; @@ -12,27 +11,20 @@ import lombok.Getter; import org.opensearch.core.rest.RestStatus; -/** - * Error Message. - */ +/** Error Message. */ public class ErrorMessage { protected Throwable exception; private final int status; - @Getter - private final String type; + @Getter private final String type; - @Getter - private final String reason; + @Getter private final String reason; - @Getter - private final String details; + @Getter private final String details; - /** - * Error Message Constructor. - */ + /** Error Message Constructor. */ public ErrorMessage(Throwable exception, int status) { this.exception = exception; this.status = status; diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/CreateDataSourceActionRequest.java b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/CreateDataSourceActionRequest.java index 0cbb2355ca..b01d5b40dd 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/CreateDataSourceActionRequest.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/CreateDataSourceActionRequest.java @@ -7,7 +7,6 @@ package org.opensearch.sql.datasources.model.transport; - import static org.opensearch.sql.analysis.DataSourceSchemaIdentifierNameResolver.DEFAULT_DATASOURCE_NAME; import java.io.IOException; @@ -17,15 +16,11 @@ import org.opensearch.core.common.io.stream.StreamInput; import org.opensearch.sql.datasource.model.DataSourceMetadata; -public class CreateDataSourceActionRequest - extends ActionRequest { +public class CreateDataSourceActionRequest extends ActionRequest { - @Getter - private DataSourceMetadata dataSourceMetadata; + @Getter private DataSourceMetadata dataSourceMetadata; - /** - * Constructor of CreateDataSourceActionRequest from StreamInput. - */ + /** Constructor of CreateDataSourceActionRequest from StreamInput. */ public CreateDataSourceActionRequest(StreamInput in) throws IOException { super(in); } @@ -38,9 +33,8 @@ public CreateDataSourceActionRequest(DataSourceMetadata dataSourceMetadata) { public ActionRequestValidationException validate() { if (this.dataSourceMetadata.getName().equals(DEFAULT_DATASOURCE_NAME)) { ActionRequestValidationException exception = new ActionRequestValidationException(); - exception - .addValidationError( - "Not allowed to create datasource with name : " + DEFAULT_DATASOURCE_NAME); + exception.addValidationError( + "Not allowed to create datasource with name : " + DEFAULT_DATASOURCE_NAME); return exception; } else { return null; diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/CreateDataSourceActionResponse.java b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/CreateDataSourceActionResponse.java index 377a249a44..a3cf6001ab 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/CreateDataSourceActionResponse.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/CreateDataSourceActionResponse.java @@ -15,11 +15,9 @@ import org.opensearch.core.common.io.stream.StreamOutput; @RequiredArgsConstructor -public class CreateDataSourceActionResponse - extends ActionResponse { +public class CreateDataSourceActionResponse extends ActionResponse { - @Getter - private final String result; + @Getter private final String result; public CreateDataSourceActionResponse(StreamInput in) throws IOException { super(in); diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DeleteDataSourceActionRequest.java b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DeleteDataSourceActionRequest.java index 1eb2d17bff..d6e3bcb3f9 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DeleteDataSourceActionRequest.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DeleteDataSourceActionRequest.java @@ -18,8 +18,7 @@ public class DeleteDataSourceActionRequest extends ActionRequest { - @Getter - private String dataSourceName; + @Getter private String dataSourceName; /** Constructor of DeleteDataSourceActionRequest from StreamInput. */ public DeleteDataSourceActionRequest(StreamInput in) throws IOException { @@ -34,18 +33,15 @@ public DeleteDataSourceActionRequest(String dataSourceName) { public ActionRequestValidationException validate() { if (StringUtils.isEmpty(this.dataSourceName)) { ActionRequestValidationException exception = new ActionRequestValidationException(); - exception - .addValidationError("Datasource Name cannot be empty or null"); + exception.addValidationError("Datasource Name cannot be empty or null"); return exception; } else if (this.dataSourceName.equals(DEFAULT_DATASOURCE_NAME)) { ActionRequestValidationException exception = new ActionRequestValidationException(); - exception - .addValidationError( - "Not allowed to delete datasource with name : " + DEFAULT_DATASOURCE_NAME); + exception.addValidationError( + "Not allowed to delete datasource with name : " + DEFAULT_DATASOURCE_NAME); return exception; } else { return null; } } - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DeleteDataSourceActionResponse.java b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DeleteDataSourceActionResponse.java index 4bb6e290c5..0f0c2e0fc7 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DeleteDataSourceActionResponse.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DeleteDataSourceActionResponse.java @@ -17,8 +17,7 @@ @RequiredArgsConstructor public class DeleteDataSourceActionResponse extends ActionResponse { - @Getter - private final String result; + @Getter private final String result; public DeleteDataSourceActionResponse(StreamInput in) throws IOException { super(in); @@ -29,5 +28,4 @@ public DeleteDataSourceActionResponse(StreamInput in) throws IOException { public void writeTo(StreamOutput streamOutput) throws IOException { streamOutput.writeString(result); } - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/GetDataSourceActionRequest.java b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/GetDataSourceActionRequest.java index 23f4898543..2d9a4de35a 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/GetDataSourceActionRequest.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/GetDataSourceActionRequest.java @@ -19,12 +19,9 @@ @NoArgsConstructor public class GetDataSourceActionRequest extends ActionRequest { - @Getter - private String dataSourceName; + @Getter private String dataSourceName; - /** - * Constructor of GetDataSourceActionRequest from StreamInput. - */ + /** Constructor of GetDataSourceActionRequest from StreamInput. */ public GetDataSourceActionRequest(StreamInput in) throws IOException { super(in); } @@ -37,13 +34,11 @@ public GetDataSourceActionRequest(String dataSourceName) { public ActionRequestValidationException validate() { if (this.dataSourceName != null && this.dataSourceName.equals(DEFAULT_DATASOURCE_NAME)) { ActionRequestValidationException exception = new ActionRequestValidationException(); - exception - .addValidationError( - "Not allowed to fetch datasource with name : " + DEFAULT_DATASOURCE_NAME); + exception.addValidationError( + "Not allowed to fetch datasource with name : " + DEFAULT_DATASOURCE_NAME); return exception; } else { return null; } } - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/GetDataSourceActionResponse.java b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/GetDataSourceActionResponse.java index 964e5989f8..2712f515c0 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/GetDataSourceActionResponse.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/GetDataSourceActionResponse.java @@ -17,8 +17,7 @@ @RequiredArgsConstructor public class GetDataSourceActionResponse extends ActionResponse { - @Getter - private final String result; + @Getter private final String result; public GetDataSourceActionResponse(StreamInput in) throws IOException { super(in); @@ -29,5 +28,4 @@ public GetDataSourceActionResponse(StreamInput in) throws IOException { public void writeTo(StreamOutput streamOutput) throws IOException { streamOutput.writeString(result); } - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/UpdateDataSourceActionRequest.java b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/UpdateDataSourceActionRequest.java index 11bc2d1e20..b502f348e2 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/UpdateDataSourceActionRequest.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/UpdateDataSourceActionRequest.java @@ -7,7 +7,6 @@ package org.opensearch.sql.datasources.model.transport; - import static org.opensearch.sql.analysis.DataSourceSchemaIdentifierNameResolver.DEFAULT_DATASOURCE_NAME; import java.io.IOException; @@ -17,11 +16,9 @@ import org.opensearch.core.common.io.stream.StreamInput; import org.opensearch.sql.datasource.model.DataSourceMetadata; -public class UpdateDataSourceActionRequest - extends ActionRequest { +public class UpdateDataSourceActionRequest extends ActionRequest { - @Getter - private DataSourceMetadata dataSourceMetadata; + @Getter private DataSourceMetadata dataSourceMetadata; /** Constructor of UpdateDataSourceActionRequest from StreamInput. */ public UpdateDataSourceActionRequest(StreamInput in) throws IOException { @@ -36,9 +33,8 @@ public UpdateDataSourceActionRequest(DataSourceMetadata dataSourceMetadata) { public ActionRequestValidationException validate() { if (this.dataSourceMetadata.getName().equals(DEFAULT_DATASOURCE_NAME)) { ActionRequestValidationException exception = new ActionRequestValidationException(); - exception - .addValidationError( - "Not allowed to update datasource with name : " + DEFAULT_DATASOURCE_NAME); + exception.addValidationError( + "Not allowed to update datasource with name : " + DEFAULT_DATASOURCE_NAME); return exception; } else { return null; diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/UpdateDataSourceActionResponse.java b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/UpdateDataSourceActionResponse.java index 5f5f6f496a..c5c2eb58af 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/UpdateDataSourceActionResponse.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/UpdateDataSourceActionResponse.java @@ -15,11 +15,9 @@ import org.opensearch.core.common.io.stream.StreamOutput; @RequiredArgsConstructor -public class UpdateDataSourceActionResponse - extends ActionResponse { +public class UpdateDataSourceActionResponse extends ActionResponse { - @Getter - private final String result; + @Getter private final String result; public UpdateDataSourceActionResponse(StreamInput in) throws IOException { super(in); diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/rest/RestDataSourceQueryAction.java b/datasources/src/main/java/org/opensearch/sql/datasources/rest/RestDataSourceQueryAction.java index a56512f838..e93245ed7b 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/rest/RestDataSourceQueryAction.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/rest/RestDataSourceQueryAction.java @@ -47,7 +47,6 @@ import org.opensearch.sql.datasources.utils.Scheduler; import org.opensearch.sql.datasources.utils.XContentParserUtils; - public class RestDataSourceQueryAction extends BaseRestHandler { public static final String DATASOURCE_ACTIONS = "datasource_actions"; @@ -83,8 +82,9 @@ public List routes() { * Response body: * Ref [org.opensearch.sql.plugin.transport.datasource.model.GetDataSourceActionResponse] */ - new Route(GET, String.format(Locale.ROOT, "%s/{%s}", - BASE_DATASOURCE_ACTION_URL, "dataSourceName")), + new Route( + GET, + String.format(Locale.ROOT, "%s/{%s}", BASE_DATASOURCE_ACTION_URL, "dataSourceName")), new Route(GET, BASE_DATASOURCE_ACTION_URL), /* @@ -107,9 +107,9 @@ public List routes() { * Response body: Ref * [org.opensearch.sql.plugin.transport.datasource.model.DeleteDataSourceActionResponse] */ - new Route(DELETE, String.format(Locale.ROOT, "%s/{%s}", - BASE_DATASOURCE_ACTION_URL, "dataSourceName")) - ); + new Route( + DELETE, + String.format(Locale.ROOT, "%s/{%s}", BASE_DATASOURCE_ACTION_URL, "dataSourceName"))); } @Override @@ -125,101 +125,125 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient case DELETE: return executeDeleteRequest(restRequest, nodeClient); default: - return restChannel - -> restChannel.sendResponse(new BytesRestResponse(RestStatus.METHOD_NOT_ALLOWED, - String.valueOf(restRequest.method()))); + return restChannel -> + restChannel.sendResponse( + new BytesRestResponse( + RestStatus.METHOD_NOT_ALLOWED, String.valueOf(restRequest.method()))); } } - private RestChannelConsumer executePostRequest(RestRequest restRequest, - NodeClient nodeClient) throws IOException { - - DataSourceMetadata dataSourceMetadata - = XContentParserUtils.toDataSourceMetadata(restRequest.contentParser()); - return restChannel -> Scheduler.schedule(nodeClient, - () -> nodeClient.execute(TransportCreateDataSourceAction.ACTION_TYPE, - new CreateDataSourceActionRequest(dataSourceMetadata), - new ActionListener<>() { - @Override - public void onResponse( - CreateDataSourceActionResponse createDataSourceActionResponse) { - restChannel.sendResponse( - new BytesRestResponse(RestStatus.CREATED, "application/json; charset=UTF-8", - createDataSourceActionResponse.getResult())); - } - - @Override - public void onFailure(Exception e) { - handleException(e, restChannel); - } - })); + private RestChannelConsumer executePostRequest(RestRequest restRequest, NodeClient nodeClient) + throws IOException { + + DataSourceMetadata dataSourceMetadata = + XContentParserUtils.toDataSourceMetadata(restRequest.contentParser()); + return restChannel -> + Scheduler.schedule( + nodeClient, + () -> + nodeClient.execute( + TransportCreateDataSourceAction.ACTION_TYPE, + new CreateDataSourceActionRequest(dataSourceMetadata), + new ActionListener<>() { + @Override + public void onResponse( + CreateDataSourceActionResponse createDataSourceActionResponse) { + restChannel.sendResponse( + new BytesRestResponse( + RestStatus.CREATED, + "application/json; charset=UTF-8", + createDataSourceActionResponse.getResult())); + } + + @Override + public void onFailure(Exception e) { + handleException(e, restChannel); + } + })); } - private RestChannelConsumer executeGetRequest(RestRequest restRequest, - NodeClient nodeClient) { + private RestChannelConsumer executeGetRequest(RestRequest restRequest, NodeClient nodeClient) { String dataSourceName = restRequest.param("dataSourceName"); - return restChannel -> Scheduler.schedule(nodeClient, - () -> nodeClient.execute(TransportGetDataSourceAction.ACTION_TYPE, - new GetDataSourceActionRequest(dataSourceName), - new ActionListener<>() { - @Override - public void onResponse(GetDataSourceActionResponse getDataSourceActionResponse) { - restChannel.sendResponse( - new BytesRestResponse(RestStatus.OK, "application/json; charset=UTF-8", - getDataSourceActionResponse.getResult())); - } - - @Override - public void onFailure(Exception e) { - handleException(e, restChannel); - } - })); + return restChannel -> + Scheduler.schedule( + nodeClient, + () -> + nodeClient.execute( + TransportGetDataSourceAction.ACTION_TYPE, + new GetDataSourceActionRequest(dataSourceName), + new ActionListener<>() { + @Override + public void onResponse( + GetDataSourceActionResponse getDataSourceActionResponse) { + restChannel.sendResponse( + new BytesRestResponse( + RestStatus.OK, + "application/json; charset=UTF-8", + getDataSourceActionResponse.getResult())); + } + + @Override + public void onFailure(Exception e) { + handleException(e, restChannel); + } + })); } - private RestChannelConsumer executeUpdateRequest(RestRequest restRequest, - NodeClient nodeClient) throws IOException { - DataSourceMetadata dataSourceMetadata - = XContentParserUtils.toDataSourceMetadata(restRequest.contentParser()); - return restChannel -> Scheduler.schedule(nodeClient, - () -> nodeClient.execute(TransportUpdateDataSourceAction.ACTION_TYPE, - new UpdateDataSourceActionRequest(dataSourceMetadata), - new ActionListener<>() { - @Override - public void onResponse( - UpdateDataSourceActionResponse updateDataSourceActionResponse) { - restChannel.sendResponse( - new BytesRestResponse(RestStatus.OK, "application/json; charset=UTF-8", - updateDataSourceActionResponse.getResult())); - } - - @Override - public void onFailure(Exception e) { - handleException(e, restChannel); - } - })); + private RestChannelConsumer executeUpdateRequest(RestRequest restRequest, NodeClient nodeClient) + throws IOException { + DataSourceMetadata dataSourceMetadata = + XContentParserUtils.toDataSourceMetadata(restRequest.contentParser()); + return restChannel -> + Scheduler.schedule( + nodeClient, + () -> + nodeClient.execute( + TransportUpdateDataSourceAction.ACTION_TYPE, + new UpdateDataSourceActionRequest(dataSourceMetadata), + new ActionListener<>() { + @Override + public void onResponse( + UpdateDataSourceActionResponse updateDataSourceActionResponse) { + restChannel.sendResponse( + new BytesRestResponse( + RestStatus.OK, + "application/json; charset=UTF-8", + updateDataSourceActionResponse.getResult())); + } + + @Override + public void onFailure(Exception e) { + handleException(e, restChannel); + } + })); } - private RestChannelConsumer executeDeleteRequest(RestRequest restRequest, - NodeClient nodeClient) { + private RestChannelConsumer executeDeleteRequest(RestRequest restRequest, NodeClient nodeClient) { String dataSourceName = restRequest.param("dataSourceName"); - return restChannel -> Scheduler.schedule(nodeClient, - () -> nodeClient.execute(TransportDeleteDataSourceAction.ACTION_TYPE, - new DeleteDataSourceActionRequest(dataSourceName), - new ActionListener<>() { - @Override - public void onResponse( - DeleteDataSourceActionResponse deleteDataSourceActionResponse) { - restChannel.sendResponse( - new BytesRestResponse(RestStatus.NO_CONTENT, "application/json; charset=UTF-8", - deleteDataSourceActionResponse.getResult())); - } - - @Override - public void onFailure(Exception e) { - handleException(e, restChannel); - } - })); + return restChannel -> + Scheduler.schedule( + nodeClient, + () -> + nodeClient.execute( + TransportDeleteDataSourceAction.ACTION_TYPE, + new DeleteDataSourceActionRequest(dataSourceName), + new ActionListener<>() { + @Override + public void onResponse( + DeleteDataSourceActionResponse deleteDataSourceActionResponse) { + restChannel.sendResponse( + new BytesRestResponse( + RestStatus.NO_CONTENT, + "application/json; charset=UTF-8", + deleteDataSourceActionResponse.getResult())); + } + + @Override + public void onFailure(Exception e) { + handleException(e, restChannel); + } + })); } private void handleException(Exception e, RestChannel restChannel) { @@ -240,8 +264,7 @@ private void handleException(Exception e, RestChannel restChannel) { private void reportError(final RestChannel channel, final Exception e, final RestStatus status) { channel.sendResponse( - new BytesRestResponse( - status, new ErrorMessage(e, status.getStatus()).toString())); + new BytesRestResponse(status, new ErrorMessage(e, status.getStatus()).toString())); } private static boolean isClientError(Exception e) { @@ -250,5 +273,4 @@ private static boolean isClientError(Exception e) { || e instanceof IllegalArgumentException || e instanceof IllegalStateException; } - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceLoaderCache.java b/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceLoaderCache.java index 3fe2954c12..dbcc321b3f 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceLoaderCache.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceLoaderCache.java @@ -4,8 +4,8 @@ import org.opensearch.sql.datasource.model.DataSourceMetadata; /** - * Interface for DataSourceLoaderCache which provides methods for - * fetch, loading and invalidating DataSource cache. + * Interface for DataSourceLoaderCache which provides methods for fetch, loading and invalidating + * DataSource cache. */ public interface DataSourceLoaderCache { @@ -16,5 +16,4 @@ public interface DataSourceLoaderCache { * @return {@link DataSource} */ DataSource getOrLoadDataSource(DataSourceMetadata dataSourceMetadata); - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceLoaderCacheImpl.java b/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceLoaderCacheImpl.java index ba9520fc0c..44454dbd38 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceLoaderCacheImpl.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceLoaderCacheImpl.java @@ -12,10 +12,9 @@ import org.opensearch.sql.storage.DataSourceFactory; /** - * Default implementation of DataSourceLoaderCache. This implementation - * utilizes Google Guava Cache {@link Cache} for caching DataSource objects - * against {@link DataSourceMetadata}. Expires the cache objects every 24 hrs after - * the last access. + * Default implementation of DataSourceLoaderCache. This implementation utilizes Google Guava Cache + * {@link Cache} for caching DataSource objects against {@link DataSourceMetadata}. Expires the + * cache objects every 24 hrs after the last access. */ public class DataSourceLoaderCacheImpl implements DataSourceLoaderCache { private final Map dataSourceFactoryMap; @@ -27,24 +26,24 @@ public class DataSourceLoaderCacheImpl implements DataSourceLoaderCache { * @param dataSourceFactorySet set of {@link DataSourceFactory}. */ public DataSourceLoaderCacheImpl(Set dataSourceFactorySet) { - this.dataSourceFactoryMap = dataSourceFactorySet.stream() - .collect(Collectors.toMap(DataSourceFactory::getDataSourceType, f -> f)); - this.dataSourceCache = CacheBuilder.newBuilder() - .maximumSize(1000) - .expireAfterAccess(24, TimeUnit.HOURS) - .build(); + this.dataSourceFactoryMap = + dataSourceFactorySet.stream() + .collect(Collectors.toMap(DataSourceFactory::getDataSourceType, f -> f)); + this.dataSourceCache = + CacheBuilder.newBuilder().maximumSize(1000).expireAfterAccess(24, TimeUnit.HOURS).build(); } @Override public DataSource getOrLoadDataSource(DataSourceMetadata dataSourceMetadata) { DataSource dataSource = this.dataSourceCache.getIfPresent(dataSourceMetadata); if (dataSource == null) { - dataSource = this.dataSourceFactoryMap.get(dataSourceMetadata.getConnector()) - .createDataSource(dataSourceMetadata); + dataSource = + this.dataSourceFactoryMap + .get(dataSourceMetadata.getConnector()) + .createDataSource(dataSourceMetadata); this.dataSourceCache.put(dataSourceMetadata, dataSource); return dataSource; } return dataSource; } - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceMetadataStorage.java b/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceMetadataStorage.java index e6483900c6..4d59c68fa0 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceMetadataStorage.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceMetadataStorage.java @@ -13,29 +13,26 @@ import org.opensearch.sql.datasource.model.DataSourceMetadata; /** - * Interface for DataSourceMetadata Storage - * which will be only used by DataSourceService for Storage. + * Interface for DataSourceMetadata Storage which will be only used by DataSourceService for + * Storage. */ public interface DataSourceMetadataStorage { /** - * Returns all dataSource Metadata objects. The returned objects won't contain - * any of the credential info. + * Returns all dataSource Metadata objects. The returned objects won't contain any of the + * credential info. * * @return list of {@link DataSourceMetadata}. */ List getDataSourceMetadata(); - /** - * Gets {@link DataSourceMetadata} corresponding to the - * datasourceName from underlying storage. + * Gets {@link DataSourceMetadata} corresponding to the datasourceName from underlying storage. * * @param datasourceName name of the {@link DataSource}. */ Optional getDataSourceMetadata(String datasourceName); - /** * Stores {@link DataSourceMetadata} in underlying storage. * @@ -43,7 +40,6 @@ public interface DataSourceMetadataStorage { */ void createDataSourceMetadata(DataSourceMetadata dataSourceMetadata); - /** * Updates {@link DataSourceMetadata} in underlying storage. * @@ -51,13 +47,10 @@ public interface DataSourceMetadataStorage { */ void updateDataSourceMetadata(DataSourceMetadata dataSourceMetadata); - /** - * Deletes {@link DataSourceMetadata} corresponding to the - * datasourceName from underlying storage. + * Deletes {@link DataSourceMetadata} corresponding to the datasourceName from underlying storage. * * @param datasourceName name of the {@link DataSource}. */ void deleteDataSourceMetadata(String datasourceName); - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceServiceImpl.java b/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceServiceImpl.java index 86afa90c2b..2ac480bbf2 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceServiceImpl.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceServiceImpl.java @@ -41,13 +41,11 @@ public class DataSourceServiceImpl implements DataSourceService { private final DataSourceUserAuthorizationHelper dataSourceUserAuthorizationHelper; - /** - * Construct from the set of {@link DataSourceFactory} at bootstrap time. - */ - public DataSourceServiceImpl(Set dataSourceFactories, - DataSourceMetadataStorage dataSourceMetadataStorage, - DataSourceUserAuthorizationHelper - dataSourceUserAuthorizationHelper) { + /** Construct from the set of {@link DataSourceFactory} at bootstrap time. */ + public DataSourceServiceImpl( + Set dataSourceFactories, + DataSourceMetadataStorage dataSourceMetadataStorage, + DataSourceUserAuthorizationHelper dataSourceUserAuthorizationHelper) { this.dataSourceMetadataStorage = dataSourceMetadataStorage; this.dataSourceUserAuthorizationHelper = dataSourceUserAuthorizationHelper; this.dataSourceLoaderCache = new DataSourceLoaderCacheImpl(dataSourceFactories); @@ -55,8 +53,8 @@ public DataSourceServiceImpl(Set dataSourceFactories, @Override public Set getDataSourceMetadata(boolean isDefaultDataSourceRequired) { - List dataSourceMetadataList - = this.dataSourceMetadataStorage.getDataSourceMetadata(); + List dataSourceMetadataList = + this.dataSourceMetadataStorage.getDataSourceMetadata(); Set dataSourceMetadataSet = new HashSet<>(dataSourceMetadataList); if (isDefaultDataSourceRequired) { dataSourceMetadataSet.add(DataSourceMetadata.defaultOpenSearchDataSourceMetadata()); @@ -67,28 +65,26 @@ public Set getDataSourceMetadata(boolean isDefaultDataSource @Override public DataSourceMetadata getDataSourceMetadata(String datasourceName) { - Optional dataSourceMetadataOptional - = getDataSourceMetadataFromName(datasourceName); + Optional dataSourceMetadataOptional = + getDataSourceMetadataFromName(datasourceName); if (dataSourceMetadataOptional.isEmpty()) { - throw new IllegalArgumentException("DataSource with name: " + datasourceName - + " doesn't exist."); + throw new IllegalArgumentException( + "DataSource with name: " + datasourceName + " doesn't exist."); } removeAuthInfo(dataSourceMetadataOptional.get()); return dataSourceMetadataOptional.get(); } - @Override public DataSource getDataSource(String dataSourceName) { - Optional - dataSourceMetadataOptional = getDataSourceMetadataFromName(dataSourceName); + Optional dataSourceMetadataOptional = + getDataSourceMetadataFromName(dataSourceName); if (dataSourceMetadataOptional.isEmpty()) { throw new DataSourceNotFoundException( String.format("DataSource with name %s doesn't exist.", dataSourceName)); } else { DataSourceMetadata dataSourceMetadata = dataSourceMetadataOptional.get(); - this.dataSourceUserAuthorizationHelper - .authorizeDataSource(dataSourceMetadata); + this.dataSourceUserAuthorizationHelper.authorizeDataSource(dataSourceMetadata); return dataSourceLoaderCache.getOrLoadDataSource(dataSourceMetadata); } } @@ -130,7 +126,6 @@ public Boolean dataSourceExists(String dataSourceName) { || this.dataSourceMetadataStorage.getDataSourceMetadata(dataSourceName).isPresent(); } - /** * This can be moved to a different validator class when we introduce more connectors. * @@ -159,7 +154,6 @@ private Optional getDataSourceMetadataFromName(String dataSo } } - // It is advised to avoid sending any kind credential // info in api response from security point of view. private void removeAuthInfo(Set dataSourceMetadataSet) { @@ -167,11 +161,8 @@ private void removeAuthInfo(Set dataSourceMetadataSet) { } private void removeAuthInfo(DataSourceMetadata dataSourceMetadata) { - HashMap safeProperties - = new HashMap<>(dataSourceMetadata.getProperties()); - safeProperties - .entrySet() - .removeIf(entry -> entry.getKey().contains("auth")); + HashMap safeProperties = new HashMap<>(dataSourceMetadata.getProperties()); + safeProperties.entrySet().removeIf(entry -> entry.getKey().contains("auth")); dataSourceMetadata.setProperties(safeProperties); } } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/storage/OpenSearchDataSourceMetadataStorage.java b/datasources/src/main/java/org/opensearch/sql/datasources/storage/OpenSearchDataSourceMetadataStorage.java index d756f2e029..fef684bf1d 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/storage/OpenSearchDataSourceMetadataStorage.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/storage/OpenSearchDataSourceMetadataStorage.java @@ -55,8 +55,8 @@ public class OpenSearchDataSourceMetadataStorage implements DataSourceMetadataSt private static final String DATASOURCE_INDEX_MAPPING_FILE_NAME = "datasources-index-mapping.yml"; private static final Integer DATASOURCE_QUERY_RESULT_SIZE = 10000; - private static final String DATASOURCE_INDEX_SETTINGS_FILE_NAME - = "datasources-index-settings.yml"; + private static final String DATASOURCE_INDEX_SETTINGS_FILE_NAME = + "datasources-index-settings.yml"; private static final Logger LOG = LogManager.getLogger(); private final Client client; private final ClusterService clusterService; @@ -64,15 +64,15 @@ public class OpenSearchDataSourceMetadataStorage implements DataSourceMetadataSt private final Encryptor encryptor; /** - * This class implements DataSourceMetadataStorage interface - * using OpenSearch as underlying storage. + * This class implements DataSourceMetadataStorage interface using OpenSearch as underlying + * storage. * - * @param client opensearch NodeClient. + * @param client opensearch NodeClient. * @param clusterService ClusterService. - * @param encryptor Encryptor. + * @param encryptor Encryptor. */ - public OpenSearchDataSourceMetadataStorage(Client client, ClusterService clusterService, - Encryptor encryptor) { + public OpenSearchDataSourceMetadataStorage( + Client client, ClusterService clusterService, Encryptor encryptor) { this.client = client; this.clusterService = clusterService; this.encryptor = encryptor; @@ -93,8 +93,7 @@ public Optional getDataSourceMetadata(String datasourceName) createDataSourcesIndex(); return Optional.empty(); } - return searchInDataSourcesIndex(QueryBuilders.termQuery("name", datasourceName)) - .stream() + return searchInDataSourcesIndex(QueryBuilders.termQuery("name", datasourceName)).stream() .findFirst() .map(x -> this.encryptDecryptAuthenticationData(x, false)); } @@ -111,14 +110,14 @@ public void createDataSourceMetadata(DataSourceMetadata dataSourceMetadata) { indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); ActionFuture indexResponseActionFuture; IndexResponse indexResponse; - try (ThreadContext.StoredContext storedContext = client.threadPool().getThreadContext() - .stashContext()) { + try (ThreadContext.StoredContext storedContext = + client.threadPool().getThreadContext().stashContext()) { indexRequest.source(XContentParserUtils.convertToXContent(dataSourceMetadata)); indexResponseActionFuture = client.index(indexRequest); indexResponse = indexResponseActionFuture.actionGet(); } catch (VersionConflictEngineException exception) { - throw new IllegalArgumentException("A datasource already exists with name: " - + dataSourceMetadata.getName()); + throw new IllegalArgumentException( + "A datasource already exists with name: " + dataSourceMetadata.getName()); } catch (Exception e) { throw new RuntimeException(e); } @@ -126,27 +125,27 @@ public void createDataSourceMetadata(DataSourceMetadata dataSourceMetadata) { if (indexResponse.getResult().equals(DocWriteResponse.Result.CREATED)) { LOG.debug("DatasourceMetadata : {} successfully created", dataSourceMetadata.getName()); } else { - throw new RuntimeException("Saving dataSource metadata information failed with result : " - + indexResponse.getResult().getLowercase()); + throw new RuntimeException( + "Saving dataSource metadata information failed with result : " + + indexResponse.getResult().getLowercase()); } } @Override public void updateDataSourceMetadata(DataSourceMetadata dataSourceMetadata) { encryptDecryptAuthenticationData(dataSourceMetadata, true); - UpdateRequest updateRequest - = new UpdateRequest(DATASOURCE_INDEX_NAME, dataSourceMetadata.getName()); + UpdateRequest updateRequest = + new UpdateRequest(DATASOURCE_INDEX_NAME, dataSourceMetadata.getName()); UpdateResponse updateResponse; - try (ThreadContext.StoredContext storedContext = client.threadPool().getThreadContext() - .stashContext()) { + try (ThreadContext.StoredContext storedContext = + client.threadPool().getThreadContext().stashContext()) { updateRequest.doc(XContentParserUtils.convertToXContent(dataSourceMetadata)); updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - ActionFuture updateResponseActionFuture - = client.update(updateRequest); + ActionFuture updateResponseActionFuture = client.update(updateRequest); updateResponse = updateResponseActionFuture.actionGet(); } catch (DocumentMissingException exception) { - throw new DataSourceNotFoundException("Datasource with name: " - + dataSourceMetadata.getName() + " doesn't exist"); + throw new DataSourceNotFoundException( + "Datasource with name: " + dataSourceMetadata.getName() + " doesn't exist"); } catch (Exception e) { throw new RuntimeException(e); } @@ -154,8 +153,9 @@ public void updateDataSourceMetadata(DataSourceMetadata dataSourceMetadata) { if (updateResponse.getResult().equals(DocWriteResponse.Result.UPDATED)) { LOG.debug("DatasourceMetadata : {} successfully updated", dataSourceMetadata.getName()); } else { - throw new RuntimeException("Saving dataSource metadata information failed with result : " - + updateResponse.getResult().getLowercase()); + throw new RuntimeException( + "Saving dataSource metadata information failed with result : " + + updateResponse.getResult().getLowercase()); } } @@ -164,48 +164,54 @@ public void deleteDataSourceMetadata(String datasourceName) { DeleteRequest deleteRequest = new DeleteRequest(DATASOURCE_INDEX_NAME); deleteRequest.id(datasourceName); ActionFuture deleteResponseActionFuture; - try (ThreadContext.StoredContext storedContext = client.threadPool().getThreadContext() - .stashContext()) { + try (ThreadContext.StoredContext storedContext = + client.threadPool().getThreadContext().stashContext()) { deleteResponseActionFuture = client.delete(deleteRequest); } DeleteResponse deleteResponse = deleteResponseActionFuture.actionGet(); if (deleteResponse.getResult().equals(DocWriteResponse.Result.DELETED)) { LOG.debug("DatasourceMetadata : {} successfully deleted", datasourceName); } else if (deleteResponse.getResult().equals(DocWriteResponse.Result.NOT_FOUND)) { - throw new DataSourceNotFoundException("Datasource with name: " - + datasourceName + " doesn't exist"); + throw new DataSourceNotFoundException( + "Datasource with name: " + datasourceName + " doesn't exist"); } else { - throw new RuntimeException("Deleting dataSource metadata information failed with result : " - + deleteResponse.getResult().getLowercase()); + throw new RuntimeException( + "Deleting dataSource metadata information failed with result : " + + deleteResponse.getResult().getLowercase()); } } private void createDataSourcesIndex() { try { - InputStream mappingFileStream = OpenSearchDataSourceMetadataStorage.class.getClassLoader() - .getResourceAsStream(DATASOURCE_INDEX_MAPPING_FILE_NAME); - InputStream settingsFileStream = OpenSearchDataSourceMetadataStorage.class.getClassLoader() - .getResourceAsStream(DATASOURCE_INDEX_SETTINGS_FILE_NAME); + InputStream mappingFileStream = + OpenSearchDataSourceMetadataStorage.class + .getClassLoader() + .getResourceAsStream(DATASOURCE_INDEX_MAPPING_FILE_NAME); + InputStream settingsFileStream = + OpenSearchDataSourceMetadataStorage.class + .getClassLoader() + .getResourceAsStream(DATASOURCE_INDEX_SETTINGS_FILE_NAME); CreateIndexRequest createIndexRequest = new CreateIndexRequest(DATASOURCE_INDEX_NAME); - createIndexRequest.mapping(IOUtils.toString(mappingFileStream, StandardCharsets.UTF_8), - XContentType.YAML) - .settings(IOUtils.toString(settingsFileStream, StandardCharsets.UTF_8), - XContentType.YAML); + createIndexRequest + .mapping(IOUtils.toString(mappingFileStream, StandardCharsets.UTF_8), XContentType.YAML) + .settings( + IOUtils.toString(settingsFileStream, StandardCharsets.UTF_8), XContentType.YAML); ActionFuture createIndexResponseActionFuture; - try (ThreadContext.StoredContext ignored = client.threadPool().getThreadContext() - .stashContext()) { + try (ThreadContext.StoredContext ignored = + client.threadPool().getThreadContext().stashContext()) { createIndexResponseActionFuture = client.admin().indices().create(createIndexRequest); } CreateIndexResponse createIndexResponse = createIndexResponseActionFuture.actionGet(); if (createIndexResponse.isAcknowledged()) { LOG.info("Index: {} creation Acknowledged", DATASOURCE_INDEX_NAME); } else { - throw new RuntimeException( - "Index creation is not acknowledged."); + throw new RuntimeException("Index creation is not acknowledged."); } } catch (Throwable e) { throw new RuntimeException( - "Internal server error while creating" + DATASOURCE_INDEX_NAME + " index:: " + "Internal server error while creating" + + DATASOURCE_INDEX_NAME + + " index:: " + e.getMessage()); } } @@ -217,17 +223,19 @@ private List searchInDataSourcesIndex(QueryBuilder query) { searchSourceBuilder.query(query); searchSourceBuilder.size(DATASOURCE_QUERY_RESULT_SIZE); searchRequest.source(searchSourceBuilder); - // strongly consistent reads is requred. more info https://github.com/opensearch-project/sql/issues/1801. + // strongly consistent reads is requred. more info + // https://github.com/opensearch-project/sql/issues/1801. searchRequest.preference("_primary"); ActionFuture searchResponseActionFuture; - try (ThreadContext.StoredContext ignored = client.threadPool().getThreadContext() - .stashContext()) { + try (ThreadContext.StoredContext ignored = + client.threadPool().getThreadContext().stashContext()) { searchResponseActionFuture = client.search(searchRequest); } SearchResponse searchResponse = searchResponseActionFuture.actionGet(); if (searchResponse.status().getStatus() != 200) { - throw new RuntimeException("Fetching dataSource metadata information failed with status : " - + searchResponse.status()); + throw new RuntimeException( + "Fetching dataSource metadata information failed with status : " + + searchResponse.status()); } else { List list = new ArrayList<>(); for (SearchHit searchHit : searchResponse.getHits().getHits()) { @@ -245,14 +253,15 @@ private List searchInDataSourcesIndex(QueryBuilder query) { } @SuppressWarnings("missingswitchdefault") - private DataSourceMetadata encryptDecryptAuthenticationData(DataSourceMetadata dataSourceMetadata, - Boolean isEncryption) { + private DataSourceMetadata encryptDecryptAuthenticationData( + DataSourceMetadata dataSourceMetadata, Boolean isEncryption) { Map propertiesMap = dataSourceMetadata.getProperties(); - Optional authTypeOptional - = propertiesMap.keySet().stream().filter(s -> s.endsWith("auth.type")) - .findFirst() - .map(propertiesMap::get) - .map(AuthenticationType::get); + Optional authTypeOptional = + propertiesMap.keySet().stream() + .filter(s -> s.endsWith("auth.type")) + .findFirst() + .map(propertiesMap::get) + .map(AuthenticationType::get); if (authTypeOptional.isPresent()) { switch (authTypeOptional.get()) { case BASICAUTH: @@ -266,8 +275,8 @@ private DataSourceMetadata encryptDecryptAuthenticationData(DataSourceMetadata d return dataSourceMetadata; } - private void handleBasicAuthPropertiesEncryptionDecryption(Map propertiesMap, - Boolean isEncryption) { + private void handleBasicAuthPropertiesEncryptionDecryption( + Map propertiesMap, Boolean isEncryption) { ArrayList list = new ArrayList<>(); propertiesMap.keySet().stream() .filter(s -> s.endsWith("auth.username")) @@ -280,21 +289,19 @@ private void handleBasicAuthPropertiesEncryptionDecryption(Map p encryptOrDecrypt(propertiesMap, isEncryption, list); } - private void encryptOrDecrypt(Map propertiesMap, Boolean isEncryption, - List keyIdentifiers) { + private void encryptOrDecrypt( + Map propertiesMap, Boolean isEncryption, List keyIdentifiers) { for (String key : keyIdentifiers) { if (isEncryption) { - propertiesMap.put(key, - this.encryptor.encrypt(propertiesMap.get(key))); + propertiesMap.put(key, this.encryptor.encrypt(propertiesMap.get(key))); } else { - propertiesMap.put(key, - this.encryptor.decrypt(propertiesMap.get(key))); + propertiesMap.put(key, this.encryptor.decrypt(propertiesMap.get(key))); } } } - private void handleSigV4PropertiesEncryptionDecryption(Map propertiesMap, - Boolean isEncryption) { + private void handleSigV4PropertiesEncryptionDecryption( + Map propertiesMap, Boolean isEncryption) { ArrayList list = new ArrayList<>(); propertiesMap.keySet().stream() .filter(s -> s.endsWith("auth.access_key")) @@ -306,5 +313,4 @@ private void handleSigV4PropertiesEncryptionDecryption(Map prope .ifPresent(list::add); encryptOrDecrypt(propertiesMap, isEncryption, list); } - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportCreateDataSourceAction.java b/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportCreateDataSourceAction.java index ce1c1bb157..e49ff7d796 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportCreateDataSourceAction.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportCreateDataSourceAction.java @@ -23,38 +23,44 @@ public class TransportCreateDataSourceAction extends HandledTransportAction { public static final String NAME = "cluster:admin/opensearch/ql/datasources/create"; - public static final ActionType - ACTION_TYPE = new ActionType<>(NAME, CreateDataSourceActionResponse::new); + public static final ActionType ACTION_TYPE = + new ActionType<>(NAME, CreateDataSourceActionResponse::new); private DataSourceService dataSourceService; /** * TransportCreateDataSourceAction action for creating datasource. * - * @param transportService transportService. - * @param actionFilters actionFilters. + * @param transportService transportService. + * @param actionFilters actionFilters. * @param dataSourceService dataSourceService. */ @Inject - public TransportCreateDataSourceAction(TransportService transportService, - ActionFilters actionFilters, - DataSourceServiceImpl dataSourceService) { - super(TransportCreateDataSourceAction.NAME, transportService, actionFilters, + public TransportCreateDataSourceAction( + TransportService transportService, + ActionFilters actionFilters, + DataSourceServiceImpl dataSourceService) { + super( + TransportCreateDataSourceAction.NAME, + transportService, + actionFilters, CreateDataSourceActionRequest::new); this.dataSourceService = dataSourceService; } @Override - protected void doExecute(Task task, CreateDataSourceActionRequest request, - ActionListener actionListener) { + protected void doExecute( + Task task, + CreateDataSourceActionRequest request, + ActionListener actionListener) { try { DataSourceMetadata dataSourceMetadata = request.getDataSourceMetadata(); dataSourceService.createDataSource(dataSourceMetadata); - actionListener.onResponse(new CreateDataSourceActionResponse("Created DataSource with name " - + dataSourceMetadata.getName())); + actionListener.onResponse( + new CreateDataSourceActionResponse( + "Created DataSource with name " + dataSourceMetadata.getName())); } catch (Exception e) { actionListener.onFailure(e); } } - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportDeleteDataSourceAction.java b/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportDeleteDataSourceAction.java index fe2df1ee51..7b814b516a 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportDeleteDataSourceAction.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportDeleteDataSourceAction.java @@ -23,37 +23,43 @@ public class TransportDeleteDataSourceAction extends HandledTransportAction { public static final String NAME = "cluster:admin/opensearch/ql/datasources/delete"; - public static final ActionType - ACTION_TYPE = new ActionType<>(NAME, DeleteDataSourceActionResponse::new); + public static final ActionType ACTION_TYPE = + new ActionType<>(NAME, DeleteDataSourceActionResponse::new); private DataSourceService dataSourceService; /** * TransportDeleteDataSourceAction action for deleting datasource. * - * @param transportService transportService. - * @param actionFilters actionFilters. + * @param transportService transportService. + * @param actionFilters actionFilters. * @param dataSourceService dataSourceService. */ @Inject - public TransportDeleteDataSourceAction(TransportService transportService, - ActionFilters actionFilters, - DataSourceServiceImpl dataSourceService) { - super(TransportDeleteDataSourceAction.NAME, transportService, actionFilters, + public TransportDeleteDataSourceAction( + TransportService transportService, + ActionFilters actionFilters, + DataSourceServiceImpl dataSourceService) { + super( + TransportDeleteDataSourceAction.NAME, + transportService, + actionFilters, DeleteDataSourceActionRequest::new); this.dataSourceService = dataSourceService; } @Override - protected void doExecute(Task task, DeleteDataSourceActionRequest request, - ActionListener actionListener) { + protected void doExecute( + Task task, + DeleteDataSourceActionRequest request, + ActionListener actionListener) { try { dataSourceService.deleteDataSource(request.getDataSourceName()); - actionListener.onResponse(new DeleteDataSourceActionResponse("Deleted DataSource with name " - + request.getDataSourceName())); + actionListener.onResponse( + new DeleteDataSourceActionResponse( + "Deleted DataSource with name " + request.getDataSourceName())); } catch (Exception e) { actionListener.onFailure(e); } } - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportGetDataSourceAction.java b/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportGetDataSourceAction.java index 7a36114755..b8147d47ca 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportGetDataSourceAction.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportGetDataSourceAction.java @@ -26,30 +26,36 @@ public class TransportGetDataSourceAction extends HandledTransportAction { public static final String NAME = "cluster:admin/opensearch/ql/datasources/read"; - public static final ActionType - ACTION_TYPE = new ActionType<>(NAME, GetDataSourceActionResponse::new); + public static final ActionType ACTION_TYPE = + new ActionType<>(NAME, GetDataSourceActionResponse::new); private DataSourceService dataSourceService; /** * TransportGetDataSourceAction action for getting datasource. * - * @param transportService transportService. - * @param actionFilters actionFilters. + * @param transportService transportService. + * @param actionFilters actionFilters. * @param dataSourceService dataSourceService. */ @Inject - public TransportGetDataSourceAction(TransportService transportService, - ActionFilters actionFilters, - DataSourceServiceImpl dataSourceService) { - super(TransportGetDataSourceAction.NAME, transportService, actionFilters, + public TransportGetDataSourceAction( + TransportService transportService, + ActionFilters actionFilters, + DataSourceServiceImpl dataSourceService) { + super( + TransportGetDataSourceAction.NAME, + transportService, + actionFilters, GetDataSourceActionRequest::new); this.dataSourceService = dataSourceService; } @Override - protected void doExecute(Task task, GetDataSourceActionRequest request, - ActionListener actionListener) { + protected void doExecute( + Task task, + GetDataSourceActionRequest request, + ActionListener actionListener) { try { String responseContent; if (request.getDataSourceName() == null) { @@ -66,30 +72,27 @@ protected void doExecute(Task task, GetDataSourceActionRequest request, private String handleGetAllDataSourcesRequest() { String responseContent; - Set dataSourceMetadataSet = - dataSourceService.getDataSourceMetadata(false); - responseContent = new JsonResponseFormatter>( - JsonResponseFormatter.Style.PRETTY) { - @Override - protected Object buildJsonObject(Set response) { - return response; - } - }.format(dataSourceMetadataSet); + Set dataSourceMetadataSet = dataSourceService.getDataSourceMetadata(false); + responseContent = + new JsonResponseFormatter>(JsonResponseFormatter.Style.PRETTY) { + @Override + protected Object buildJsonObject(Set response) { + return response; + } + }.format(dataSourceMetadataSet); return responseContent; } private String handleSingleDataSourceRequest(String datasourceName) { String responseContent; - DataSourceMetadata dataSourceMetadata - = dataSourceService - .getDataSourceMetadata(datasourceName); - responseContent = new JsonResponseFormatter( - JsonResponseFormatter.Style.PRETTY) { - @Override - protected Object buildJsonObject(DataSourceMetadata response) { - return response; - } - }.format(dataSourceMetadata); + DataSourceMetadata dataSourceMetadata = dataSourceService.getDataSourceMetadata(datasourceName); + responseContent = + new JsonResponseFormatter(JsonResponseFormatter.Style.PRETTY) { + @Override + protected Object buildJsonObject(DataSourceMetadata response) { + return response; + } + }.format(dataSourceMetadata); return responseContent; } } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportUpdateDataSourceAction.java b/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportUpdateDataSourceAction.java index 13f80733e6..6964d574dd 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportUpdateDataSourceAction.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportUpdateDataSourceAction.java @@ -23,8 +23,8 @@ public class TransportUpdateDataSourceAction extends HandledTransportAction { public static final String NAME = "cluster:admin/opensearch/ql/datasources/update"; - public static final ActionType - ACTION_TYPE = new ActionType<>(NAME, UpdateDataSourceActionResponse::new); + public static final ActionType ACTION_TYPE = + new ActionType<>(NAME, UpdateDataSourceActionResponse::new); private DataSourceService dataSourceService; @@ -36,24 +36,30 @@ public class TransportUpdateDataSourceAction * @param dataSourceService dataSourceService. */ @Inject - public TransportUpdateDataSourceAction(TransportService transportService, - ActionFilters actionFilters, - DataSourceServiceImpl dataSourceService) { - super(TransportUpdateDataSourceAction.NAME, transportService, actionFilters, + public TransportUpdateDataSourceAction( + TransportService transportService, + ActionFilters actionFilters, + DataSourceServiceImpl dataSourceService) { + super( + TransportUpdateDataSourceAction.NAME, + transportService, + actionFilters, UpdateDataSourceActionRequest::new); this.dataSourceService = dataSourceService; } @Override - protected void doExecute(Task task, UpdateDataSourceActionRequest request, - ActionListener actionListener) { + protected void doExecute( + Task task, + UpdateDataSourceActionRequest request, + ActionListener actionListener) { try { dataSourceService.updateDataSource(request.getDataSourceMetadata()); - actionListener.onResponse(new UpdateDataSourceActionResponse("Updated DataSource with name " - + request.getDataSourceMetadata().getName())); + actionListener.onResponse( + new UpdateDataSourceActionResponse( + "Updated DataSource with name " + request.getDataSourceMetadata().getName())); } catch (Exception e) { actionListener.onFailure(e); } } - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/utils/XContentParserUtils.java b/datasources/src/main/java/org/opensearch/sql/datasources/utils/XContentParserUtils.java index 38a500afae..1ad79addac 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/utils/XContentParserUtils.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/utils/XContentParserUtils.java @@ -22,9 +22,7 @@ import org.opensearch.sql.datasource.model.DataSourceMetadata; import org.opensearch.sql.datasource.model.DataSourceType; -/** - * Utitlity class to serialize and deserialize objects in XContent. - */ +/** Utitlity class to serialize and deserialize objects in XContent. */ @UtilityClass public class XContentParserUtils { public static final String NAME_FIELD = "name"; @@ -87,9 +85,13 @@ public static DataSourceMetadata toDataSourceMetadata(XContentParser parser) thr * @throws IOException IOException. */ public static DataSourceMetadata toDataSourceMetadata(String json) throws IOException { - try (XContentParser parser = XContentType.JSON.xContent() - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - json)) { + try (XContentParser parser = + XContentType.JSON + .xContent() + .createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + json)) { return toDataSourceMetadata(parser); } } @@ -116,6 +118,4 @@ public static XContentBuilder convertToXContent(DataSourceMetadata metadata) thr builder.endObject(); return builder; } - - } diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/auth/AuthenticationTypeTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/auth/AuthenticationTypeTest.java index 23bb4688e1..4bc4800093 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/auth/AuthenticationTypeTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/auth/AuthenticationTypeTest.java @@ -5,7 +5,6 @@ package org.opensearch.sql.datasources.auth; - import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelperImplTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelperImplTest.java index 552bd0edf9..6ee3c12edd 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelperImplTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelperImplTest.java @@ -27,65 +27,76 @@ public class DataSourceUserAuthorizationHelperImplTest { @Mock(answer = Answers.RETURNS_DEEP_STUBS) private Client client; - @InjectMocks - private DataSourceUserAuthorizationHelperImpl dataSourceUserAuthorizationHelper; - + @InjectMocks private DataSourceUserAuthorizationHelperImpl dataSourceUserAuthorizationHelper; @Test public void testAuthorizeDataSourceWithAllowedRoles() { String userString = "myuser|bckrole1,bckrol2|prometheus_access|myTenant"; - Mockito.when(client.threadPool().getThreadContext() - .getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT)) + Mockito.when( + client + .threadPool() + .getThreadContext() + .getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT)) .thenReturn(userString); DataSourceMetadata dataSourceMetadata = dataSourceMetadata(); - this.dataSourceUserAuthorizationHelper - .authorizeDataSource(dataSourceMetadata); + this.dataSourceUserAuthorizationHelper.authorizeDataSource(dataSourceMetadata); } @Test public void testAuthorizeDataSourceWithAdminRole() { String userString = "myuser|bckrole1,bckrol2|all_access|myTenant"; - Mockito.when(client.threadPool().getThreadContext() - .getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT)) + Mockito.when( + client + .threadPool() + .getThreadContext() + .getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT)) .thenReturn(userString); DataSourceMetadata dataSourceMetadata = dataSourceMetadata(); - this.dataSourceUserAuthorizationHelper - .authorizeDataSource(dataSourceMetadata); + this.dataSourceUserAuthorizationHelper.authorizeDataSource(dataSourceMetadata); } @Test public void testAuthorizeDataSourceWithNullUserString() { - Mockito.when(client.threadPool().getThreadContext() - .getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT)) + Mockito.when( + client + .threadPool() + .getThreadContext() + .getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT)) .thenReturn(null); DataSourceMetadata dataSourceMetadata = dataSourceMetadata(); - this.dataSourceUserAuthorizationHelper - .authorizeDataSource(dataSourceMetadata); + this.dataSourceUserAuthorizationHelper.authorizeDataSource(dataSourceMetadata); } @Test public void testAuthorizeDataSourceWithDefaultDataSource() { String userString = "myuser|bckrole1,bckrol2|role1|myTenant"; - Mockito.when(client.threadPool().getThreadContext() - .getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT)) + Mockito.when( + client + .threadPool() + .getThreadContext() + .getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT)) .thenReturn(userString); DataSourceMetadata dataSourceMetadata = DataSourceMetadata.defaultOpenSearchDataSourceMetadata(); - this.dataSourceUserAuthorizationHelper - .authorizeDataSource(dataSourceMetadata); + this.dataSourceUserAuthorizationHelper.authorizeDataSource(dataSourceMetadata); } @Test public void testAuthorizeDataSourceWithException() { String userString = "myuser|bckrole1,bckrol2|role1|myTenant"; - Mockito.when(client.threadPool().getThreadContext() - .getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT)) + Mockito.when( + client + .threadPool() + .getThreadContext() + .getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT)) .thenReturn(userString); DataSourceMetadata dataSourceMetadata = dataSourceMetadata(); - SecurityException securityException - = Assert.assertThrows(SecurityException.class, + SecurityException securityException = + Assert.assertThrows( + SecurityException.class, () -> this.dataSourceUserAuthorizationHelper.authorizeDataSource(dataSourceMetadata)); - Assert.assertEquals("User is not authorized to access datasource test. " + Assert.assertEquals( + "User is not authorized to access datasource test. " + "User should be mapped to any of the roles in [prometheus_access] for access.", securityException.getMessage()); } @@ -98,5 +109,4 @@ private DataSourceMetadata dataSourceMetadata() { dataSourceMetadata.setProperties(new HashMap<>()); return dataSourceMetadata; } - } diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/encryptor/EncryptorImplTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/encryptor/EncryptorImplTest.java index d62a5a957a..26432b139b 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/encryptor/EncryptorImplTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/encryptor/EncryptorImplTest.java @@ -17,7 +17,6 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; - @ExtendWith(MockitoExtension.class) public class EncryptorImplTest { @@ -38,9 +37,11 @@ public void testMasterKeySize() { String input = "This is a test input"; String masterKey8 = "12345678"; Encryptor encryptor8 = new EncryptorImpl(masterKey8); - assertThrows(AwsCryptoException.class, () -> { - encryptor8.encrypt(input); - }); + assertThrows( + AwsCryptoException.class, + () -> { + encryptor8.encrypt(input); + }); String masterKey16 = "1234567812345678"; Encryptor encryptor16 = new EncryptorImpl(masterKey16); @@ -54,9 +55,11 @@ public void testMasterKeySize() { String masterKey17 = "12345678123456781"; Encryptor encryptor17 = new EncryptorImpl(masterKey17); - assertThrows(AwsCryptoException.class, () -> { - encryptor17.encrypt(input); - }); + assertThrows( + AwsCryptoException.class, + () -> { + encryptor17.encrypt(input); + }); } @Test @@ -64,9 +67,11 @@ public void testInvalidBase64String() { String encrypted = "invalidBase64String"; Encryptor encryptor = new EncryptorImpl("randomMasterKey"); - assertThrows(BadCiphertextException.class, () -> { - encryptor.decrypt(encrypted); - }); + assertThrows( + BadCiphertextException.class, + () -> { + encryptor.decrypt(encrypted); + }); } @Test @@ -80,19 +85,21 @@ public void testDecryptWithDifferentKey() { String encrypted = encryptor1.encrypt(input); - assertThrows(Exception.class, () -> { - encryptor2.decrypt(encrypted); - }); + assertThrows( + Exception.class, + () -> { + encryptor2.decrypt(encrypted); + }); } @Test public void testEncryptionAndDecryptionWithNullMasterKey() { String input = "This is a test input"; Encryptor encryptor = new EncryptorImpl(null); - IllegalStateException illegalStateException - = Assertions.assertThrows(IllegalStateException.class, - () -> encryptor.encrypt(input)); - Assertions.assertEquals("Master key is a required config for using create and" + IllegalStateException illegalStateException = + Assertions.assertThrows(IllegalStateException.class, () -> encryptor.encrypt(input)); + Assertions.assertEquals( + "Master key is a required config for using create and" + " update datasource APIs." + "Please set plugins.query.datasources.encryption.masterkey config " + "in opensearch.yml in all the cluster nodes. " @@ -100,10 +107,10 @@ public void testEncryptionAndDecryptionWithNullMasterKey() { + "https://github.com/opensearch-project/sql/blob/main/docs/user/ppl/" + "admin/datasources.rst#master-key-config-for-encrypting-credential-information", illegalStateException.getMessage()); - illegalStateException - = Assertions.assertThrows(IllegalStateException.class, - () -> encryptor.decrypt(input)); - Assertions.assertEquals("Master key is a required config for using create and" + illegalStateException = + Assertions.assertThrows(IllegalStateException.class, () -> encryptor.decrypt(input)); + Assertions.assertEquals( + "Master key is a required config for using create and" + " update datasource APIs." + "Please set plugins.query.datasources.encryption.masterkey config " + "in opensearch.yml in all the cluster nodes. " @@ -118,10 +125,10 @@ public void testEncryptionAndDecryptionWithEmptyMasterKey() { String masterKey = ""; String input = "This is a test input"; Encryptor encryptor = new EncryptorImpl(masterKey); - IllegalStateException illegalStateException - = Assertions.assertThrows(IllegalStateException.class, - () -> encryptor.encrypt(input)); - Assertions.assertEquals("Master key is a required config for using create and" + IllegalStateException illegalStateException = + Assertions.assertThrows(IllegalStateException.class, () -> encryptor.encrypt(input)); + Assertions.assertEquals( + "Master key is a required config for using create and" + " update datasource APIs." + "Please set plugins.query.datasources.encryption.masterkey config " + "in opensearch.yml in all the cluster nodes. " @@ -129,10 +136,10 @@ public void testEncryptionAndDecryptionWithEmptyMasterKey() { + "https://github.com/opensearch-project/sql/blob/main/docs/user/ppl/" + "admin/datasources.rst#master-key-config-for-encrypting-credential-information", illegalStateException.getMessage()); - illegalStateException - = Assertions.assertThrows(IllegalStateException.class, - () -> encryptor.decrypt(input)); - Assertions.assertEquals("Master key is a required config for using create and" + illegalStateException = + Assertions.assertThrows(IllegalStateException.class, () -> encryptor.decrypt(input)); + Assertions.assertEquals( + "Master key is a required config for using create and" + " update datasource APIs." + "Please set plugins.query.datasources.encryption.masterkey config " + "in opensearch.yml in all the cluster nodes. " @@ -141,5 +148,4 @@ public void testEncryptionAndDecryptionWithEmptyMasterKey() { + "admin/datasources.rst#master-key-config-for-encrypting-credential-information", illegalStateException.getMessage()); } - } diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/service/DataSourceLoaderCacheImplTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/service/DataSourceLoaderCacheImplTest.java index bf656857b0..b2ea221eb7 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/service/DataSourceLoaderCacheImplTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/service/DataSourceLoaderCacheImplTest.java @@ -25,11 +25,9 @@ @ExtendWith(MockitoExtension.class) class DataSourceLoaderCacheImplTest { - @Mock - private DataSourceFactory dataSourceFactory; + @Mock private DataSourceFactory dataSourceFactory; - @Mock - private StorageEngine storageEngine; + @Mock private StorageEngine storageEngine; @BeforeEach public void setup() { @@ -55,8 +53,8 @@ void testGetOrLoadDataSource() { dataSourceMetadata.setProperties(ImmutableMap.of()); DataSource dataSource = dataSourceLoaderCache.getOrLoadDataSource(dataSourceMetadata); verify(dataSourceFactory, times(1)).createDataSource(dataSourceMetadata); - Assertions.assertEquals(dataSource, - dataSourceLoaderCache.getOrLoadDataSource(dataSourceMetadata)); + Assertions.assertEquals( + dataSource, dataSourceLoaderCache.getOrLoadDataSource(dataSourceMetadata)); verifyNoMoreInteractions(dataSourceFactory); } @@ -81,5 +79,4 @@ private DataSourceMetadata getMetadata() { dataSourceMetadata.setProperties(ImmutableMap.of()); return dataSourceMetadata; } - } diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/service/DataSourceServiceImplTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/service/DataSourceServiceImplTest.java index e1312ec582..56d3586c6e 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/service/DataSourceServiceImplTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/service/DataSourceServiceImplTest.java @@ -46,15 +46,11 @@ @ExtendWith(MockitoExtension.class) class DataSourceServiceImplTest { - @Mock - private DataSourceFactory dataSourceFactory; - @Mock - private StorageEngine storageEngine; - @Mock - private DataSourceMetadataStorage dataSourceMetadataStorage; + @Mock private DataSourceFactory dataSourceFactory; + @Mock private StorageEngine storageEngine; + @Mock private DataSourceMetadataStorage dataSourceMetadataStorage; - @Mock - private DataSourceUserAuthorizationHelper dataSourceUserAuthorizationHelper; + @Mock private DataSourceUserAuthorizationHelper dataSourceUserAuthorizationHelper; private DataSourceService dataSourceService; @@ -75,7 +71,8 @@ public void setup() { { add(dataSourceFactory); } - }, dataSourceMetadataStorage, + }, + dataSourceMetadataStorage, dataSourceUserAuthorizationHelper); } @@ -91,22 +88,18 @@ void testGetDataSourceForDefaultOpenSearchDataSource() { @Test void testGetDataSourceForNonExistingDataSource() { - when(dataSourceMetadataStorage.getDataSourceMetadata("test")) - .thenReturn(Optional.empty()); + when(dataSourceMetadataStorage.getDataSourceMetadata("test")).thenReturn(Optional.empty()); DataSourceNotFoundException exception = assertThrows( - DataSourceNotFoundException.class, - () -> - dataSourceService.getDataSource("test")); + DataSourceNotFoundException.class, () -> dataSourceService.getDataSource("test")); assertEquals("DataSource with name test doesn't exist.", exception.getMessage()); - verify(dataSourceMetadataStorage, times(1)) - .getDataSourceMetadata("test"); + verify(dataSourceMetadataStorage, times(1)).getDataSourceMetadata("test"); } @Test void testGetDataSourceSuccessCase() { - DataSourceMetadata dataSourceMetadata = metadata("test", DataSourceType.OPENSEARCH, - Collections.emptyList(), ImmutableMap.of()); + DataSourceMetadata dataSourceMetadata = + metadata("test", DataSourceType.OPENSEARCH, Collections.emptyList(), ImmutableMap.of()); doNothing().when(dataSourceUserAuthorizationHelper).authorizeDataSource(dataSourceMetadata); when(dataSourceMetadataStorage.getDataSourceMetadata("test")) .thenReturn(Optional.of(dataSourceMetadata)); @@ -114,26 +107,31 @@ void testGetDataSourceSuccessCase() { assertEquals("test", dataSource.getName()); assertEquals(DataSourceType.OPENSEARCH, dataSource.getConnectorType()); verify(dataSourceMetadataStorage, times(1)).getDataSourceMetadata("test"); - verify(dataSourceFactory, times(1)) - .createDataSource(dataSourceMetadata); + verify(dataSourceFactory, times(1)).createDataSource(dataSourceMetadata); } @Test void testGetDataSourceWithAuthorizationFailure() { - DataSourceMetadata dataSourceMetadata = metadata("test", DataSourceType.OPENSEARCH, - Collections.singletonList("prometheus_access"), ImmutableMap.of()); - doThrow(new SecurityException("User is not authorized to access datasource test. " - + "User should be mapped to any of the roles in [prometheus_access] for access.")) + DataSourceMetadata dataSourceMetadata = + metadata( + "test", + DataSourceType.OPENSEARCH, + Collections.singletonList("prometheus_access"), + ImmutableMap.of()); + doThrow( + new SecurityException( + "User is not authorized to access datasource test. User should be mapped to any of" + + " the roles in [prometheus_access] for access.")) .when(dataSourceUserAuthorizationHelper) .authorizeDataSource(dataSourceMetadata); when(dataSourceMetadataStorage.getDataSourceMetadata("test")) .thenReturn(Optional.of(dataSourceMetadata)); - - SecurityException securityException - = Assertions.assertThrows(SecurityException.class, - () -> dataSourceService.getDataSource("test")); - Assertions.assertEquals("User is not authorized to access datasource test. " + SecurityException securityException = + Assertions.assertThrows( + SecurityException.class, () -> dataSourceService.getDataSource("test")); + Assertions.assertEquals( + "User is not authorized to access datasource test. " + "User should be mapped to any of the roles in [prometheus_access] for access.", securityException.getMessage()); @@ -141,21 +139,23 @@ void testGetDataSourceWithAuthorizationFailure() { verify(dataSourceFactory, times(0)).createDataSource(dataSourceMetadata); } - @Test void testCreateDataSourceSuccessCase() { - DataSourceMetadata dataSourceMetadata = metadata("testDS", DataSourceType.OPENSEARCH, - Collections.emptyList(), ImmutableMap.of()); + DataSourceMetadata dataSourceMetadata = + metadata("testDS", DataSourceType.OPENSEARCH, Collections.emptyList(), ImmutableMap.of()); dataSourceService.createDataSource(dataSourceMetadata); - verify(dataSourceMetadataStorage, times(1)) - .createDataSourceMetadata(dataSourceMetadata); - verify(dataSourceFactory, times(1)) - .createDataSource(dataSourceMetadata); + verify(dataSourceMetadataStorage, times(1)).createDataSourceMetadata(dataSourceMetadata); + verify(dataSourceFactory, times(1)).createDataSource(dataSourceMetadata); when(dataSourceMetadataStorage.getDataSourceMetadata("testDS")) - .thenReturn(Optional.ofNullable(metadata("testDS", DataSourceType.OPENSEARCH, - Collections.emptyList(), ImmutableMap.of()))); + .thenReturn( + Optional.ofNullable( + metadata( + "testDS", + DataSourceType.OPENSEARCH, + Collections.emptyList(), + ImmutableMap.of()))); DataSource dataSource = dataSourceService.getDataSource("testDS"); assertEquals("testDS", dataSource.getName()); assertEquals(storageEngine, dataSource.getStorageEngine()); @@ -164,14 +164,15 @@ void testCreateDataSourceSuccessCase() { @Test void testCreateDataSourceWithDisallowedDatasourceName() { - DataSourceMetadata dataSourceMetadata = metadata("testDS$$$", DataSourceType.OPENSEARCH, - Collections.emptyList(), ImmutableMap.of()); + DataSourceMetadata dataSourceMetadata = + metadata( + "testDS$$$", DataSourceType.OPENSEARCH, Collections.emptyList(), ImmutableMap.of()); IllegalArgumentException exception = assertThrows( IllegalArgumentException.class, - () -> - dataSourceService.createDataSource(dataSourceMetadata)); - assertEquals("DataSource Name: testDS$$$ contains illegal characters." + () -> dataSourceService.createDataSource(dataSourceMetadata)); + assertEquals( + "DataSource Name: testDS$$$ contains illegal characters." + " Allowed characters: a-zA-Z0-9_-*@.", exception.getMessage()); verify(dataSourceFactory, times(1)).getDataSourceType(); @@ -181,14 +182,14 @@ void testCreateDataSourceWithDisallowedDatasourceName() { @Test void testCreateDataSourceWithEmptyDatasourceName() { - DataSourceMetadata dataSourceMetadata = metadata("", DataSourceType.OPENSEARCH, - Collections.emptyList(), ImmutableMap.of()); + DataSourceMetadata dataSourceMetadata = + metadata("", DataSourceType.OPENSEARCH, Collections.emptyList(), ImmutableMap.of()); IllegalArgumentException exception = assertThrows( IllegalArgumentException.class, - () -> - dataSourceService.createDataSource(dataSourceMetadata)); - assertEquals("Missing Name Field from a DataSource. Name is a required parameter.", + () -> dataSourceService.createDataSource(dataSourceMetadata)); + assertEquals( + "Missing Name Field from a DataSource. Name is a required parameter.", exception.getMessage()); verify(dataSourceFactory, times(1)).getDataSourceType(); verify(dataSourceFactory, times(0)).createDataSource(dataSourceMetadata); @@ -197,14 +198,14 @@ void testCreateDataSourceWithEmptyDatasourceName() { @Test void testCreateDataSourceWithNullParameters() { - DataSourceMetadata dataSourceMetadata = metadata("testDS", DataSourceType.OPENSEARCH, - Collections.emptyList(), null); + DataSourceMetadata dataSourceMetadata = + metadata("testDS", DataSourceType.OPENSEARCH, Collections.emptyList(), null); IllegalArgumentException exception = assertThrows( IllegalArgumentException.class, - () -> - dataSourceService.createDataSource(dataSourceMetadata)); - assertEquals("Missing properties field in datasource configuration. " + () -> dataSourceService.createDataSource(dataSourceMetadata)); + assertEquals( + "Missing properties field in datasource configuration. " + "Properties are required parameters.", exception.getMessage()); verify(dataSourceFactory, times(1)).getDataSourceType(); @@ -219,88 +220,99 @@ void testGetDataSourceMetadataSet() { properties.put("prometheus.auth.type", "basicauth"); properties.put("prometheus.auth.username", "username"); properties.put("prometheus.auth.password", "password"); - when(dataSourceMetadataStorage.getDataSourceMetadata()).thenReturn(new ArrayList<>() { - { - add(metadata("testDS", DataSourceType.PROMETHEUS, Collections.emptyList(), - properties)); - } - }); - Set dataSourceMetadataSet - = dataSourceService.getDataSourceMetadata(false); + when(dataSourceMetadataStorage.getDataSourceMetadata()) + .thenReturn( + new ArrayList<>() { + { + add( + metadata( + "testDS", DataSourceType.PROMETHEUS, Collections.emptyList(), properties)); + } + }); + Set dataSourceMetadataSet = dataSourceService.getDataSourceMetadata(false); assertEquals(1, dataSourceMetadataSet.size()); DataSourceMetadata dataSourceMetadata = dataSourceMetadataSet.iterator().next(); assertTrue(dataSourceMetadata.getProperties().containsKey("prometheus.uri")); assertFalse(dataSourceMetadata.getProperties().containsKey("prometheus.auth.type")); assertFalse(dataSourceMetadata.getProperties().containsKey("prometheus.auth.username")); assertFalse(dataSourceMetadata.getProperties().containsKey("prometheus.auth.password")); - assertFalse(dataSourceMetadataSet - .contains(DataSourceMetadata.defaultOpenSearchDataSourceMetadata())); + assertFalse( + dataSourceMetadataSet.contains(DataSourceMetadata.defaultOpenSearchDataSourceMetadata())); verify(dataSourceMetadataStorage, times(1)).getDataSourceMetadata(); } @Test void testGetDataSourceMetadataSetWithDefaultDatasource() { - when(dataSourceMetadataStorage.getDataSourceMetadata()).thenReturn(new ArrayList<>() { - { - add(metadata("testDS", DataSourceType.PROMETHEUS, Collections.emptyList(), - ImmutableMap.of())); - } - }); - Set dataSourceMetadataSet - = dataSourceService.getDataSourceMetadata(true); + when(dataSourceMetadataStorage.getDataSourceMetadata()) + .thenReturn( + new ArrayList<>() { + { + add( + metadata( + "testDS", + DataSourceType.PROMETHEUS, + Collections.emptyList(), + ImmutableMap.of())); + } + }); + Set dataSourceMetadataSet = dataSourceService.getDataSourceMetadata(true); assertEquals(2, dataSourceMetadataSet.size()); - assertTrue(dataSourceMetadataSet - .contains(DataSourceMetadata.defaultOpenSearchDataSourceMetadata())); + assertTrue( + dataSourceMetadataSet.contains(DataSourceMetadata.defaultOpenSearchDataSourceMetadata())); verify(dataSourceMetadataStorage, times(1)).getDataSourceMetadata(); } @Test void testUpdateDataSourceSuccessCase() { - DataSourceMetadata dataSourceMetadata = metadata("testDS", DataSourceType.OPENSEARCH, - Collections.emptyList(), ImmutableMap.of()); + DataSourceMetadata dataSourceMetadata = + metadata("testDS", DataSourceType.OPENSEARCH, Collections.emptyList(), ImmutableMap.of()); dataSourceService.updateDataSource(dataSourceMetadata); - verify(dataSourceMetadataStorage, times(1)) - .updateDataSourceMetadata(dataSourceMetadata); - verify(dataSourceFactory, times(1)) - .createDataSource(dataSourceMetadata); + verify(dataSourceMetadataStorage, times(1)).updateDataSourceMetadata(dataSourceMetadata); + verify(dataSourceFactory, times(1)).createDataSource(dataSourceMetadata); } @Test void testUpdateDefaultDataSource() { - DataSourceMetadata dataSourceMetadata = metadata(DEFAULT_DATASOURCE_NAME, - DataSourceType.OPENSEARCH, Collections.emptyList(), ImmutableMap.of()); - UnsupportedOperationException unsupportedOperationException - = assertThrows(UnsupportedOperationException.class, + DataSourceMetadata dataSourceMetadata = + metadata( + DEFAULT_DATASOURCE_NAME, + DataSourceType.OPENSEARCH, + Collections.emptyList(), + ImmutableMap.of()); + UnsupportedOperationException unsupportedOperationException = + assertThrows( + UnsupportedOperationException.class, () -> dataSourceService.updateDataSource(dataSourceMetadata)); - assertEquals("Not allowed to update default datasource :" + DEFAULT_DATASOURCE_NAME, + assertEquals( + "Not allowed to update default datasource :" + DEFAULT_DATASOURCE_NAME, unsupportedOperationException.getMessage()); } @Test void testDeleteDatasource() { dataSourceService.deleteDataSource("testDS"); - verify(dataSourceMetadataStorage, times(1)) - .deleteDataSourceMetadata("testDS"); + verify(dataSourceMetadataStorage, times(1)).deleteDataSourceMetadata("testDS"); } @Test void testDeleteDefaultDatasource() { - UnsupportedOperationException unsupportedOperationException - = assertThrows(UnsupportedOperationException.class, - () -> dataSourceService.deleteDataSource(DEFAULT_DATASOURCE_NAME)); - assertEquals("Not allowed to delete default datasource :" + DEFAULT_DATASOURCE_NAME, + UnsupportedOperationException unsupportedOperationException = + assertThrows( + UnsupportedOperationException.class, + () -> dataSourceService.deleteDataSource(DEFAULT_DATASOURCE_NAME)); + assertEquals( + "Not allowed to delete default datasource :" + DEFAULT_DATASOURCE_NAME, unsupportedOperationException.getMessage()); } @Test void testDataSourceExists() { - when(dataSourceMetadataStorage.getDataSourceMetadata("test")) - .thenReturn(Optional.empty()); + when(dataSourceMetadataStorage.getDataSourceMetadata("test")).thenReturn(Optional.empty()); Assertions.assertFalse(dataSourceService.dataSourceExists("test")); when(dataSourceMetadataStorage.getDataSourceMetadata("test")) - .thenReturn(Optional.of(metadata("test", DataSourceType.PROMETHEUS, - List.of(), ImmutableMap.of()))); + .thenReturn( + Optional.of(metadata("test", DataSourceType.PROMETHEUS, List.of(), ImmutableMap.of()))); Assertions.assertTrue(dataSourceService.dataSourceExists("test")); } @@ -310,9 +322,8 @@ void testDataSourceExistsForDefaultDataSource() { verifyNoInteractions(dataSourceMetadataStorage); } - DataSourceMetadata metadata(String name, DataSourceType type, - List allowedRoles, - Map properties) { + DataSourceMetadata metadata( + String name, DataSourceType type, List allowedRoles, Map properties) { DataSourceMetadata dataSourceMetadata = new DataSourceMetadata(); dataSourceMetadata.setName(name); dataSourceMetadata.setConnector(type); @@ -329,13 +340,15 @@ void testRemovalOfAuthorizationInfo() { properties.put("prometheus.auth.username", "username"); properties.put("prometheus.auth.password", "password"); DataSourceMetadata dataSourceMetadata = - new DataSourceMetadata("testDS", DataSourceType.PROMETHEUS, - Collections.singletonList("prometheus_access"), properties); + new DataSourceMetadata( + "testDS", + DataSourceType.PROMETHEUS, + Collections.singletonList("prometheus_access"), + properties); when(dataSourceMetadataStorage.getDataSourceMetadata("testDS")) .thenReturn(Optional.of(dataSourceMetadata)); - DataSourceMetadata dataSourceMetadata1 - = dataSourceService.getDataSourceMetadata("testDS"); + DataSourceMetadata dataSourceMetadata1 = dataSourceService.getDataSourceMetadata("testDS"); assertEquals("testDS", dataSourceMetadata1.getName()); assertEquals(DataSourceType.PROMETHEUS, dataSourceMetadata1.getConnector()); assertFalse(dataSourceMetadata1.getProperties().containsKey("prometheus.auth.type")); @@ -345,10 +358,11 @@ void testRemovalOfAuthorizationInfo() { @Test void testGetDataSourceMetadataForNonExistingDataSource() { - when(dataSourceMetadataStorage.getDataSourceMetadata("testDS")) - .thenReturn(Optional.empty()); - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, - () -> dataSourceService.getDataSourceMetadata("testDS")); + when(dataSourceMetadataStorage.getDataSourceMetadata("testDS")).thenReturn(Optional.empty()); + IllegalArgumentException exception = + assertThrows( + IllegalArgumentException.class, + () -> dataSourceService.getDataSourceMetadata("testDS")); assertEquals("DataSource with name: testDS doesn't exist.", exception.getMessage()); } @@ -360,16 +374,15 @@ void testGetDataSourceMetadataForSpecificDataSourceName() { properties.put("prometheus.auth.username", "username"); properties.put("prometheus.auth.password", "password"); when(dataSourceMetadataStorage.getDataSourceMetadata("testDS")) - .thenReturn(Optional.ofNullable( - metadata("testDS", DataSourceType.PROMETHEUS, Collections.emptyList(), - properties))); - DataSourceMetadata dataSourceMetadata - = this.dataSourceService.getDataSourceMetadata("testDS"); + .thenReturn( + Optional.ofNullable( + metadata( + "testDS", DataSourceType.PROMETHEUS, Collections.emptyList(), properties))); + DataSourceMetadata dataSourceMetadata = this.dataSourceService.getDataSourceMetadata("testDS"); assertTrue(dataSourceMetadata.getProperties().containsKey("prometheus.uri")); assertFalse(dataSourceMetadata.getProperties().containsKey("prometheus.auth.type")); assertFalse(dataSourceMetadata.getProperties().containsKey("prometheus.auth.username")); assertFalse(dataSourceMetadata.getProperties().containsKey("prometheus.auth.password")); verify(dataSourceMetadataStorage, times(1)).getDataSourceMetadata("testDS"); } - } diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/storage/OpenSearchDataSourceMetadataStorageTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/storage/OpenSearchDataSourceMetadataStorageTest.java index b1554ed585..6e412e8333 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/storage/OpenSearchDataSourceMetadataStorageTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/storage/OpenSearchDataSourceMetadataStorageTest.java @@ -52,33 +52,25 @@ public class OpenSearchDataSourceMetadataStorageTest { @Mock(answer = Answers.RETURNS_DEEP_STUBS) private Client client; + @Mock(answer = Answers.RETURNS_DEEP_STUBS) private ClusterService clusterService; - @Mock - private Encryptor encryptor; + + @Mock private Encryptor encryptor; + @Mock(answer = Answers.RETURNS_DEEP_STUBS) private SearchResponse searchResponse; - @Mock - private ActionFuture searchResponseActionFuture; - @Mock - private ActionFuture createIndexResponseActionFuture; - @Mock - private ActionFuture indexResponseActionFuture; - @Mock - private IndexResponse indexResponse; - @Mock - private ActionFuture updateResponseActionFuture; - @Mock - private UpdateResponse updateResponse; - @Mock - private ActionFuture deleteResponseActionFuture; - @Mock - private DeleteResponse deleteResponse; - @Mock - private SearchHit searchHit; - @InjectMocks - private OpenSearchDataSourceMetadataStorage openSearchDataSourceMetadataStorage; + @Mock private ActionFuture searchResponseActionFuture; + @Mock private ActionFuture createIndexResponseActionFuture; + @Mock private ActionFuture indexResponseActionFuture; + @Mock private IndexResponse indexResponse; + @Mock private ActionFuture updateResponseActionFuture; + @Mock private UpdateResponse updateResponse; + @Mock private ActionFuture deleteResponseActionFuture; + @Mock private DeleteResponse deleteResponse; + @Mock private SearchHit searchHit; + @InjectMocks private OpenSearchDataSourceMetadataStorage openSearchDataSourceMetadataStorage; @SneakyThrows @Test @@ -91,28 +83,24 @@ public void testGetDataSourceMetadata() { Mockito.when(searchResponse.getHits()) .thenReturn( new SearchHits( - new SearchHit[] {searchHit}, - new TotalHits(21, TotalHits.Relation.EQUAL_TO), - 1.0F)); - Mockito.when(searchHit.getSourceAsString()) - .thenReturn(getBasicDataSourceMetadataString()); + new SearchHit[] {searchHit}, new TotalHits(21, TotalHits.Relation.EQUAL_TO), 1.0F)); + Mockito.when(searchHit.getSourceAsString()).thenReturn(getBasicDataSourceMetadataString()); Mockito.when(encryptor.decrypt("password")).thenReturn("password"); Mockito.when(encryptor.decrypt("username")).thenReturn("username"); - Optional dataSourceMetadataOptional - = openSearchDataSourceMetadataStorage.getDataSourceMetadata(TEST_DATASOURCE_INDEX_NAME); - + Optional dataSourceMetadataOptional = + openSearchDataSourceMetadataStorage.getDataSourceMetadata(TEST_DATASOURCE_INDEX_NAME); Assertions.assertFalse(dataSourceMetadataOptional.isEmpty()); DataSourceMetadata dataSourceMetadata = dataSourceMetadataOptional.get(); Assertions.assertEquals(TEST_DATASOURCE_INDEX_NAME, dataSourceMetadata.getName()); Assertions.assertEquals(DataSourceType.PROMETHEUS, dataSourceMetadata.getConnector()); - Assertions.assertEquals("password", - dataSourceMetadata.getProperties().get("prometheus.auth.password")); - Assertions.assertEquals("username", - dataSourceMetadata.getProperties().get("prometheus.auth.username")); - Assertions.assertEquals("basicauth", - dataSourceMetadata.getProperties().get("prometheus.auth.type")); + Assertions.assertEquals( + "password", dataSourceMetadata.getProperties().get("prometheus.auth.password")); + Assertions.assertEquals( + "username", dataSourceMetadata.getProperties().get("prometheus.auth.username")); + Assertions.assertEquals( + "basicauth", dataSourceMetadata.getProperties().get("prometheus.auth.type")); } @SneakyThrows @@ -124,9 +112,12 @@ public void testGetDataSourceMetadataWith404SearchResponse() { Mockito.when(searchResponseActionFuture.actionGet()).thenReturn(searchResponse); Mockito.when(searchResponse.status()).thenReturn(RestStatus.NOT_FOUND); - RuntimeException runtimeException = Assertions.assertThrows(RuntimeException.class, - () -> openSearchDataSourceMetadataStorage.getDataSourceMetadata( - TEST_DATASOURCE_INDEX_NAME)); + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, + () -> + openSearchDataSourceMetadataStorage.getDataSourceMetadata( + TEST_DATASOURCE_INDEX_NAME)); Assertions.assertEquals( "Fetching dataSource metadata information failed with status : NOT_FOUND", runtimeException.getMessage()); @@ -143,15 +134,13 @@ public void testGetDataSourceMetadataWithParsingFailed() { Mockito.when(searchResponse.getHits()) .thenReturn( new SearchHits( - new SearchHit[] {searchHit}, - new TotalHits(21, TotalHits.Relation.EQUAL_TO), - 1.0F)); - Mockito.when(searchHit.getSourceAsString()) - .thenReturn("..testDs"); + new SearchHit[] {searchHit}, new TotalHits(21, TotalHits.Relation.EQUAL_TO), 1.0F)); + Mockito.when(searchHit.getSourceAsString()).thenReturn("..testDs"); - Assertions.assertThrows(RuntimeException.class, - () -> openSearchDataSourceMetadataStorage.getDataSourceMetadata( - TEST_DATASOURCE_INDEX_NAME)); + Assertions.assertThrows( + RuntimeException.class, + () -> + openSearchDataSourceMetadataStorage.getDataSourceMetadata(TEST_DATASOURCE_INDEX_NAME)); } @SneakyThrows @@ -165,28 +154,24 @@ public void testGetDataSourceMetadataWithAWSSigV4() { Mockito.when(searchResponse.getHits()) .thenReturn( new SearchHits( - new SearchHit[] {searchHit}, - new TotalHits(21, TotalHits.Relation.EQUAL_TO), - 1.0F)); - Mockito.when(searchHit.getSourceAsString()) - .thenReturn(getAWSSigv4DataSourceMetadataString()); + new SearchHit[] {searchHit}, new TotalHits(21, TotalHits.Relation.EQUAL_TO), 1.0F)); + Mockito.when(searchHit.getSourceAsString()).thenReturn(getAWSSigv4DataSourceMetadataString()); Mockito.when(encryptor.decrypt("secret_key")).thenReturn("secret_key"); Mockito.when(encryptor.decrypt("access_key")).thenReturn("access_key"); - Optional dataSourceMetadataOptional - = openSearchDataSourceMetadataStorage.getDataSourceMetadata(TEST_DATASOURCE_INDEX_NAME); - + Optional dataSourceMetadataOptional = + openSearchDataSourceMetadataStorage.getDataSourceMetadata(TEST_DATASOURCE_INDEX_NAME); Assertions.assertFalse(dataSourceMetadataOptional.isEmpty()); DataSourceMetadata dataSourceMetadata = dataSourceMetadataOptional.get(); Assertions.assertEquals(TEST_DATASOURCE_INDEX_NAME, dataSourceMetadata.getName()); Assertions.assertEquals(DataSourceType.PROMETHEUS, dataSourceMetadata.getConnector()); - Assertions.assertEquals("secret_key", - dataSourceMetadata.getProperties().get("prometheus.auth.secret_key")); - Assertions.assertEquals("access_key", - dataSourceMetadata.getProperties().get("prometheus.auth.access_key")); - Assertions.assertEquals("awssigv4", - dataSourceMetadata.getProperties().get("prometheus.auth.type")); + Assertions.assertEquals( + "secret_key", dataSourceMetadata.getProperties().get("prometheus.auth.secret_key")); + Assertions.assertEquals( + "access_key", dataSourceMetadata.getProperties().get("prometheus.auth.access_key")); + Assertions.assertEquals( + "awssigv4", dataSourceMetadata.getProperties().get("prometheus.auth.type")); } @SneakyThrows @@ -200,31 +185,27 @@ public void testGetDataSourceMetadataWithBasicAuth() { Mockito.when(searchResponse.getHits()) .thenReturn( new SearchHits( - new SearchHit[] {searchHit}, - new TotalHits(21, TotalHits.Relation.EQUAL_TO), - 1.0F)); + new SearchHit[] {searchHit}, new TotalHits(21, TotalHits.Relation.EQUAL_TO), 1.0F)); Mockito.when(searchHit.getSourceAsString()) .thenReturn(getDataSourceMetadataStringWithBasicAuthentication()); Mockito.when(encryptor.decrypt("username")).thenReturn("username"); Mockito.when(encryptor.decrypt("password")).thenReturn("password"); - Optional dataSourceMetadataOptional - = openSearchDataSourceMetadataStorage.getDataSourceMetadata(TEST_DATASOURCE_INDEX_NAME); - + Optional dataSourceMetadataOptional = + openSearchDataSourceMetadataStorage.getDataSourceMetadata(TEST_DATASOURCE_INDEX_NAME); Assertions.assertFalse(dataSourceMetadataOptional.isEmpty()); DataSourceMetadata dataSourceMetadata = dataSourceMetadataOptional.get(); Assertions.assertEquals(TEST_DATASOURCE_INDEX_NAME, dataSourceMetadata.getName()); Assertions.assertEquals(DataSourceType.PROMETHEUS, dataSourceMetadata.getConnector()); - Assertions.assertEquals("username", - dataSourceMetadata.getProperties().get("prometheus.auth.username")); - Assertions.assertEquals("password", - dataSourceMetadata.getProperties().get("prometheus.auth.password")); - Assertions.assertEquals("basicauth", - dataSourceMetadata.getProperties().get("prometheus.auth.type")); + Assertions.assertEquals( + "username", dataSourceMetadata.getProperties().get("prometheus.auth.username")); + Assertions.assertEquals( + "password", dataSourceMetadata.getProperties().get("prometheus.auth.password")); + Assertions.assertEquals( + "basicauth", dataSourceMetadata.getProperties().get("prometheus.auth.type")); } - @SneakyThrows @Test public void testGetDataSourceMetadataList() { @@ -236,15 +217,12 @@ public void testGetDataSourceMetadataList() { Mockito.when(searchResponse.getHits()) .thenReturn( new SearchHits( - new SearchHit[] {searchHit}, - new TotalHits(21, TotalHits.Relation.EQUAL_TO), - 1.0F)); + new SearchHit[] {searchHit}, new TotalHits(21, TotalHits.Relation.EQUAL_TO), 1.0F)); Mockito.when(searchHit.getSourceAsString()) .thenReturn(getDataSourceMetadataStringWithNoAuthentication()); - List dataSourceMetadataList - = openSearchDataSourceMetadataStorage.getDataSourceMetadata(); - + List dataSourceMetadataList = + openSearchDataSourceMetadataStorage.getDataSourceMetadata(); Assertions.assertEquals(1, dataSourceMetadataList.size()); DataSourceMetadata dataSourceMetadata = dataSourceMetadataList.get(0); @@ -252,7 +230,6 @@ public void testGetDataSourceMetadataList() { Assertions.assertEquals(DataSourceType.PROMETHEUS, dataSourceMetadata.getConnector()); } - @SneakyThrows @Test public void testGetDataSourceMetadataListWithNoIndex() { @@ -264,8 +241,8 @@ public void testGetDataSourceMetadataListWithNoIndex() { .thenReturn(new CreateIndexResponse(true, true, DATASOURCE_INDEX_NAME)); Mockito.when(client.index(ArgumentMatchers.any())).thenReturn(indexResponseActionFuture); - List dataSourceMetadataList - = openSearchDataSourceMetadataStorage.getDataSourceMetadata(); + List dataSourceMetadataList = + openSearchDataSourceMetadataStorage.getDataSourceMetadata(); Assertions.assertEquals(0, dataSourceMetadataList.size()); } @@ -281,8 +258,8 @@ public void testGetDataSourceMetadataWithNoIndex() { .thenReturn(new CreateIndexResponse(true, true, DATASOURCE_INDEX_NAME)); Mockito.when(client.index(ArgumentMatchers.any())).thenReturn(indexResponseActionFuture); - Optional dataSourceMetadataOptional - = openSearchDataSourceMetadataStorage.getDataSourceMetadata(TEST_DATASOURCE_INDEX_NAME); + Optional dataSourceMetadataOptional = + openSearchDataSourceMetadataStorage.getDataSourceMetadata(TEST_DATASOURCE_INDEX_NAME); Assertions.assertFalse(dataSourceMetadataOptional.isPresent()); } @@ -310,8 +287,6 @@ public void testCreateDataSourceMetadata() { Mockito.verify(client.admin().indices(), Mockito.times(1)).create(ArgumentMatchers.any()); Mockito.verify(client, Mockito.times(1)).index(ArgumentMatchers.any()); Mockito.verify(client.threadPool().getThreadContext(), Mockito.times(2)).stashContext(); - - } @Test @@ -334,7 +309,6 @@ public void testCreateDataSourceMetadataWithOutCreatingIndex() { Mockito.verify(client.threadPool().getThreadContext(), Mockito.times(1)).stashContext(); } - @Test public void testCreateDataSourceMetadataFailedWithNotFoundResponse() { @@ -351,10 +325,14 @@ public void testCreateDataSourceMetadataFailedWithNotFoundResponse() { Mockito.when(indexResponse.getResult()).thenReturn(DocWriteResponse.Result.NOT_FOUND); DataSourceMetadata dataSourceMetadata = getDataSourceMetadata(); - RuntimeException runtimeException = Assertions.assertThrows(RuntimeException.class, - () -> this.openSearchDataSourceMetadataStorage.createDataSourceMetadata( - dataSourceMetadata)); - Assertions.assertEquals("Saving dataSource metadata information failed with result : not_found", + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, + () -> + this.openSearchDataSourceMetadataStorage.createDataSourceMetadata( + dataSourceMetadata)); + Assertions.assertEquals( + "Saving dataSource metadata information failed with result : not_found", runtimeException.getMessage()); Mockito.verify(encryptor, Mockito.times(1)).encrypt("secret_key"); @@ -362,8 +340,6 @@ public void testCreateDataSourceMetadataFailedWithNotFoundResponse() { Mockito.verify(client.admin().indices(), Mockito.times(1)).create(ArgumentMatchers.any()); Mockito.verify(client, Mockito.times(1)).index(ArgumentMatchers.any()); Mockito.verify(client.threadPool().getThreadContext(), Mockito.times(2)).stashContext(); - - } @Test @@ -381,20 +357,19 @@ public void testCreateDataSourceMetadataWithVersionConflict() { .thenThrow(VersionConflictEngineException.class); DataSourceMetadata dataSourceMetadata = getDataSourceMetadata(); IllegalArgumentException illegalArgumentException = - Assertions.assertThrows(IllegalArgumentException.class, - () -> this.openSearchDataSourceMetadataStorage.createDataSourceMetadata( - dataSourceMetadata)); - Assertions.assertEquals("A datasource already exists with name: testDS", - illegalArgumentException.getMessage()); - + Assertions.assertThrows( + IllegalArgumentException.class, + () -> + this.openSearchDataSourceMetadataStorage.createDataSourceMetadata( + dataSourceMetadata)); + Assertions.assertEquals( + "A datasource already exists with name: testDS", illegalArgumentException.getMessage()); Mockito.verify(encryptor, Mockito.times(1)).encrypt("secret_key"); Mockito.verify(encryptor, Mockito.times(1)).encrypt("access_key"); Mockito.verify(client.admin().indices(), Mockito.times(1)).create(ArgumentMatchers.any()); Mockito.verify(client, Mockito.times(1)).index(ArgumentMatchers.any()); Mockito.verify(client.threadPool().getThreadContext(), Mockito.times(2)).stashContext(); - - } @Test @@ -412,19 +387,20 @@ public void testCreateDataSourceMetadataWithException() { .thenThrow(new RuntimeException("error while indexing")); DataSourceMetadata dataSourceMetadata = getDataSourceMetadata(); - RuntimeException runtimeException = Assertions.assertThrows(RuntimeException.class, - () -> this.openSearchDataSourceMetadataStorage.createDataSourceMetadata( - dataSourceMetadata)); - Assertions.assertEquals("java.lang.RuntimeException: error while indexing", - runtimeException.getMessage()); + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, + () -> + this.openSearchDataSourceMetadataStorage.createDataSourceMetadata( + dataSourceMetadata)); + Assertions.assertEquals( + "java.lang.RuntimeException: error while indexing", runtimeException.getMessage()); Mockito.verify(encryptor, Mockito.times(1)).encrypt("secret_key"); Mockito.verify(encryptor, Mockito.times(1)).encrypt("access_key"); Mockito.verify(client.admin().indices(), Mockito.times(1)).create(ArgumentMatchers.any()); Mockito.verify(client, Mockito.times(1)).index(ArgumentMatchers.any()); Mockito.verify(client.threadPool().getThreadContext(), Mockito.times(2)).stashContext(); - - } @Test @@ -440,9 +416,12 @@ public void testCreateDataSourceMetadataWithIndexCreationFailed() { .thenReturn(new CreateIndexResponse(false, false, DATASOURCE_INDEX_NAME)); DataSourceMetadata dataSourceMetadata = getDataSourceMetadata(); - RuntimeException runtimeException = Assertions.assertThrows(RuntimeException.class, - () -> this.openSearchDataSourceMetadataStorage.createDataSourceMetadata( - dataSourceMetadata)); + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, + () -> + this.openSearchDataSourceMetadataStorage.createDataSourceMetadata( + dataSourceMetadata)); Assertions.assertEquals( "Internal server error while creating.ql-datasources index:: " + "Index creation is not acknowledged.", @@ -470,7 +449,6 @@ public void testUpdateDataSourceMetadata() { Mockito.verify(client.admin().indices(), Mockito.times(0)).create(ArgumentMatchers.any()); Mockito.verify(client, Mockito.times(1)).update(ArgumentMatchers.any()); Mockito.verify(client.threadPool().getThreadContext(), Mockito.times(1)).stashContext(); - } @Test @@ -482,10 +460,14 @@ public void testUpdateDataSourceMetadataWithNotFoundResult() { Mockito.when(updateResponse.getResult()).thenReturn(DocWriteResponse.Result.NOT_FOUND); DataSourceMetadata dataSourceMetadata = getDataSourceMetadata(); - RuntimeException runtimeException = Assertions.assertThrows(RuntimeException.class, - () -> this.openSearchDataSourceMetadataStorage.updateDataSourceMetadata( - dataSourceMetadata)); - Assertions.assertEquals("Saving dataSource metadata information failed with result : not_found", + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, + () -> + this.openSearchDataSourceMetadataStorage.updateDataSourceMetadata( + dataSourceMetadata)); + Assertions.assertEquals( + "Saving dataSource metadata information failed with result : not_found", runtimeException.getMessage()); Mockito.verify(encryptor, Mockito.times(1)).encrypt("secret_key"); @@ -493,32 +475,31 @@ public void testUpdateDataSourceMetadataWithNotFoundResult() { Mockito.verify(client.admin().indices(), Mockito.times(0)).create(ArgumentMatchers.any()); Mockito.verify(client, Mockito.times(1)).update(ArgumentMatchers.any()); Mockito.verify(client.threadPool().getThreadContext(), Mockito.times(1)).stashContext(); - } @Test public void testUpdateDataSourceMetadataWithDocumentMissingException() { Mockito.when(encryptor.encrypt("secret_key")).thenReturn("secret_key"); Mockito.when(encryptor.encrypt("access_key")).thenReturn("access_key"); - Mockito.when(client.update(ArgumentMatchers.any())).thenThrow(new DocumentMissingException( - ShardId.fromString("[2][2]"), "testDS")); + Mockito.when(client.update(ArgumentMatchers.any())) + .thenThrow(new DocumentMissingException(ShardId.fromString("[2][2]"), "testDS")); DataSourceMetadata dataSourceMetadata = getDataSourceMetadata(); dataSourceMetadata.setName("testDS"); - DataSourceNotFoundException dataSourceNotFoundException = - Assertions.assertThrows(DataSourceNotFoundException.class, - () -> this.openSearchDataSourceMetadataStorage.updateDataSourceMetadata( - dataSourceMetadata)); - Assertions.assertEquals("Datasource with name: testDS doesn't exist", - dataSourceNotFoundException.getMessage()); + Assertions.assertThrows( + DataSourceNotFoundException.class, + () -> + this.openSearchDataSourceMetadataStorage.updateDataSourceMetadata( + dataSourceMetadata)); + Assertions.assertEquals( + "Datasource with name: testDS doesn't exist", dataSourceNotFoundException.getMessage()); Mockito.verify(encryptor, Mockito.times(1)).encrypt("secret_key"); Mockito.verify(encryptor, Mockito.times(1)).encrypt("access_key"); Mockito.verify(client.admin().indices(), Mockito.times(0)).create(ArgumentMatchers.any()); Mockito.verify(client, Mockito.times(1)).update(ArgumentMatchers.any()); Mockito.verify(client.threadPool().getThreadContext(), Mockito.times(1)).stashContext(); - } @Test @@ -530,19 +511,20 @@ public void testUpdateDataSourceMetadataWithRuntimeException() { DataSourceMetadata dataSourceMetadata = getDataSourceMetadata(); dataSourceMetadata.setName("testDS"); - - RuntimeException runtimeException = Assertions.assertThrows(RuntimeException.class, - () -> this.openSearchDataSourceMetadataStorage.updateDataSourceMetadata( - dataSourceMetadata)); - Assertions.assertEquals("java.lang.RuntimeException: error message", - runtimeException.getMessage()); + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, + () -> + this.openSearchDataSourceMetadataStorage.updateDataSourceMetadata( + dataSourceMetadata)); + Assertions.assertEquals( + "java.lang.RuntimeException: error message", runtimeException.getMessage()); Mockito.verify(encryptor, Mockito.times(1)).encrypt("secret_key"); Mockito.verify(encryptor, Mockito.times(1)).encrypt("access_key"); Mockito.verify(client.admin().indices(), Mockito.times(0)).create(ArgumentMatchers.any()); Mockito.verify(client, Mockito.times(1)).update(ArgumentMatchers.any()); Mockito.verify(client.threadPool().getThreadContext(), Mockito.times(1)).stashContext(); - } @Test @@ -566,11 +548,11 @@ public void testDeleteDataSourceMetadataWhichisAlreadyDeleted() { Mockito.when(deleteResponse.getResult()).thenReturn(DocWriteResponse.Result.NOT_FOUND); DataSourceNotFoundException dataSourceNotFoundException = - Assertions.assertThrows(DataSourceNotFoundException.class, + Assertions.assertThrows( + DataSourceNotFoundException.class, () -> this.openSearchDataSourceMetadataStorage.deleteDataSourceMetadata("testDS")); - Assertions.assertEquals("Datasource with name: testDS doesn't exist", - dataSourceNotFoundException.getMessage()); - + Assertions.assertEquals( + "Datasource with name: testDS doesn't exist", dataSourceNotFoundException.getMessage()); Mockito.verifyNoInteractions(encryptor); Mockito.verify(client.admin().indices(), Mockito.times(0)).create(ArgumentMatchers.any()); @@ -584,9 +566,12 @@ public void testDeleteDataSourceMetadataWithUnexpectedResult() { Mockito.when(deleteResponseActionFuture.actionGet()).thenReturn(deleteResponse); Mockito.when(deleteResponse.getResult()).thenReturn(DocWriteResponse.Result.NOOP); - RuntimeException runtimeException = Assertions.assertThrows(RuntimeException.class, - () -> this.openSearchDataSourceMetadataStorage.deleteDataSourceMetadata("testDS")); - Assertions.assertEquals("Deleting dataSource metadata information failed with result : noop", + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, + () -> this.openSearchDataSourceMetadataStorage.deleteDataSourceMetadata("testDS")); + Assertions.assertEquals( + "Deleting dataSource metadata information failed with result : noop", runtimeException.getMessage()); Mockito.verifyNoInteractions(encryptor); @@ -666,5 +651,4 @@ private DataSourceMetadata getDataSourceMetadata() { dataSourceMetadata.setProperties(properties); return dataSourceMetadata; } - } diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportCreateDataSourceActionTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportCreateDataSourceActionTest.java index 2ece0eb5cd..a2482602ff 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportCreateDataSourceActionTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportCreateDataSourceActionTest.java @@ -27,27 +27,23 @@ @ExtendWith(MockitoExtension.class) public class TransportCreateDataSourceActionTest { - @Mock - private TransportService transportService; - @Mock - private TransportCreateDataSourceAction action; - @Mock - private DataSourceServiceImpl dataSourceService; - @Mock - private Task task; - @Mock - private ActionListener actionListener; + @Mock private TransportService transportService; + @Mock private TransportCreateDataSourceAction action; + @Mock private DataSourceServiceImpl dataSourceService; + @Mock private Task task; + @Mock private ActionListener actionListener; + @Captor private ArgumentCaptor createDataSourceActionResponseArgumentCaptor; - @Captor - private ArgumentCaptor exceptionArgumentCaptor; + @Captor private ArgumentCaptor exceptionArgumentCaptor; @BeforeEach public void setUp() { - action = new TransportCreateDataSourceAction(transportService, - new ActionFilters(new HashSet<>()), dataSourceService); + action = + new TransportCreateDataSourceAction( + transportService, new ActionFilters(new HashSet<>()), dataSourceService); } @Test @@ -61,10 +57,10 @@ public void testDoExecute() { verify(dataSourceService, times(1)).createDataSource(dataSourceMetadata); Mockito.verify(actionListener) .onResponse(createDataSourceActionResponseArgumentCaptor.capture()); - CreateDataSourceActionResponse createDataSourceActionResponse - = createDataSourceActionResponseArgumentCaptor.getValue(); - Assertions.assertEquals("Created DataSource with name test_datasource", - createDataSourceActionResponse.getResult()); + CreateDataSourceActionResponse createDataSourceActionResponse = + createDataSourceActionResponseArgumentCaptor.getValue(); + Assertions.assertEquals( + "Created DataSource with name test_datasource", createDataSourceActionResponse.getResult()); } @Test @@ -72,7 +68,8 @@ public void testDoExecuteWithException() { DataSourceMetadata dataSourceMetadata = new DataSourceMetadata(); dataSourceMetadata.setName("test_datasource"); dataSourceMetadata.setConnector(DataSourceType.PROMETHEUS); - doThrow(new RuntimeException("Error")).when(dataSourceService) + doThrow(new RuntimeException("Error")) + .when(dataSourceService) .createDataSource(dataSourceMetadata); CreateDataSourceActionRequest request = new CreateDataSourceActionRequest(dataSourceMetadata); action.doExecute(task, request, actionListener); @@ -80,7 +77,6 @@ public void testDoExecuteWithException() { Mockito.verify(actionListener).onFailure(exceptionArgumentCaptor.capture()); Exception exception = exceptionArgumentCaptor.getValue(); Assertions.assertTrue(exception instanceof RuntimeException); - Assertions.assertEquals("Error", - exception.getMessage()); + Assertions.assertEquals("Error", exception.getMessage()); } } diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportDeleteDataSourceActionTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportDeleteDataSourceActionTest.java index 61b197a805..d836eecabe 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportDeleteDataSourceActionTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportDeleteDataSourceActionTest.java @@ -25,28 +25,23 @@ @ExtendWith(MockitoExtension.class) public class TransportDeleteDataSourceActionTest { - @Mock - private TransportService transportService; - @Mock - private TransportDeleteDataSourceAction action; - @Mock - private DataSourceServiceImpl dataSourceService; - @Mock - private Task task; - @Mock - private ActionListener actionListener; + @Mock private TransportService transportService; + @Mock private TransportDeleteDataSourceAction action; + @Mock private DataSourceServiceImpl dataSourceService; + @Mock private Task task; + @Mock private ActionListener actionListener; @Captor private ArgumentCaptor deleteDataSourceActionResponseArgumentCaptor; - @Captor - private ArgumentCaptor exceptionArgumentCaptor; + @Captor private ArgumentCaptor exceptionArgumentCaptor; @BeforeEach public void setUp() { - action = new TransportDeleteDataSourceAction(transportService, - new ActionFilters(new HashSet<>()), dataSourceService); + action = + new TransportDeleteDataSourceAction( + transportService, new ActionFilters(new HashSet<>()), dataSourceService); } @Test @@ -57,10 +52,10 @@ public void testDoExecute() { verify(dataSourceService, times(1)).deleteDataSource("test_datasource"); Mockito.verify(actionListener) .onResponse(deleteDataSourceActionResponseArgumentCaptor.capture()); - DeleteDataSourceActionResponse deleteDataSourceActionResponse - = deleteDataSourceActionResponseArgumentCaptor.getValue(); - Assertions.assertEquals("Deleted DataSource with name test_datasource", - deleteDataSourceActionResponse.getResult()); + DeleteDataSourceActionResponse deleteDataSourceActionResponse = + deleteDataSourceActionResponseArgumentCaptor.getValue(); + Assertions.assertEquals( + "Deleted DataSource with name test_datasource", deleteDataSourceActionResponse.getResult()); } @Test @@ -72,7 +67,6 @@ public void testDoExecuteWithException() { Mockito.verify(actionListener).onFailure(exceptionArgumentCaptor.capture()); Exception exception = exceptionArgumentCaptor.getValue(); Assertions.assertTrue(exception instanceof RuntimeException); - Assertions.assertEquals("Error", - exception.getMessage()); + Assertions.assertEquals("Error", exception.getMessage()); } } diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportGetDataSourceActionTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportGetDataSourceActionTest.java index 0546df643d..b6899421ef 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportGetDataSourceActionTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportGetDataSourceActionTest.java @@ -34,27 +34,22 @@ @ExtendWith(MockitoExtension.class) public class TransportGetDataSourceActionTest { - @Mock - private TransportService transportService; - @Mock - private TransportGetDataSourceAction action; - @Mock - private DataSourceServiceImpl dataSourceService; - @Mock - private Task task; - @Mock - private ActionListener actionListener; + @Mock private TransportService transportService; + @Mock private TransportGetDataSourceAction action; + @Mock private DataSourceServiceImpl dataSourceService; + @Mock private Task task; + @Mock private ActionListener actionListener; @Captor private ArgumentCaptor getDataSourceActionResponseArgumentCaptor; - @Captor - private ArgumentCaptor exceptionArgumentCaptor; + @Captor private ArgumentCaptor exceptionArgumentCaptor; @BeforeEach public void setUp() { - action = new TransportGetDataSourceAction(transportService, - new ActionFilters(new HashSet<>()), dataSourceService); + action = + new TransportGetDataSourceAction( + transportService, new ActionFilters(new HashSet<>()), dataSourceService); } @Test @@ -63,23 +58,22 @@ public void testDoExecute() { dataSourceMetadata.setName("test_datasource"); dataSourceMetadata.setConnector(DataSourceType.PROMETHEUS); GetDataSourceActionRequest request = new GetDataSourceActionRequest("test_datasource"); - when(dataSourceService.getDataSourceMetadata("test_datasource")) - .thenReturn(dataSourceMetadata); + when(dataSourceService.getDataSourceMetadata("test_datasource")).thenReturn(dataSourceMetadata); action.doExecute(task, request, actionListener); verify(dataSourceService, times(1)).getDataSourceMetadata("test_datasource"); Mockito.verify(actionListener).onResponse(getDataSourceActionResponseArgumentCaptor.capture()); - GetDataSourceActionResponse getDataSourceActionResponse - = getDataSourceActionResponseArgumentCaptor.getValue(); + GetDataSourceActionResponse getDataSourceActionResponse = + getDataSourceActionResponseArgumentCaptor.getValue(); JsonResponseFormatter dataSourceMetadataJsonResponseFormatter = - new JsonResponseFormatter<>( - JsonResponseFormatter.Style.PRETTY) { + new JsonResponseFormatter<>(JsonResponseFormatter.Style.PRETTY) { @Override protected Object buildJsonObject(DataSourceMetadata response) { return response; } }; - Assertions.assertEquals(dataSourceMetadataJsonResponseFormatter.format(dataSourceMetadata), + Assertions.assertEquals( + dataSourceMetadataJsonResponseFormatter.format(dataSourceMetadata), getDataSourceActionResponse.getResult()); DataSourceMetadata result = new Gson().fromJson(getDataSourceActionResponse.getResult(), DataSourceMetadata.class); @@ -100,18 +94,16 @@ public void testDoExecuteForGetAllDataSources() { action.doExecute(task, request, actionListener); verify(dataSourceService, times(1)).getDataSourceMetadata(false); Mockito.verify(actionListener).onResponse(getDataSourceActionResponseArgumentCaptor.capture()); - GetDataSourceActionResponse getDataSourceActionResponse - = getDataSourceActionResponseArgumentCaptor.getValue(); + GetDataSourceActionResponse getDataSourceActionResponse = + getDataSourceActionResponseArgumentCaptor.getValue(); JsonResponseFormatter> dataSourceMetadataJsonResponseFormatter = - new JsonResponseFormatter<>( - JsonResponseFormatter.Style.PRETTY) { + new JsonResponseFormatter<>(JsonResponseFormatter.Style.PRETTY) { @Override protected Object buildJsonObject(Set response) { return response; } }; - Type setType = new TypeToken>() { - }.getType(); + Type setType = new TypeToken>() {}.getType(); Assertions.assertEquals( dataSourceMetadataJsonResponseFormatter.format(Collections.singleton(dataSourceMetadata)), getDataSourceActionResponse.getResult()); @@ -131,7 +123,6 @@ public void testDoExecuteWithException() { Mockito.verify(actionListener).onFailure(exceptionArgumentCaptor.capture()); Exception exception = exceptionArgumentCaptor.getValue(); Assertions.assertTrue(exception instanceof RuntimeException); - Assertions.assertEquals("Error", - exception.getMessage()); + Assertions.assertEquals("Error", exception.getMessage()); } } diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportUpdateDataSourceActionTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportUpdateDataSourceActionTest.java index 2b9305c459..6e141918ca 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportUpdateDataSourceActionTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportUpdateDataSourceActionTest.java @@ -27,28 +27,23 @@ @ExtendWith(MockitoExtension.class) public class TransportUpdateDataSourceActionTest { - @Mock - private TransportService transportService; - @Mock - private TransportUpdateDataSourceAction action; - @Mock - private DataSourceServiceImpl dataSourceService; - @Mock - private Task task; - @Mock - private ActionListener actionListener; + @Mock private TransportService transportService; + @Mock private TransportUpdateDataSourceAction action; + @Mock private DataSourceServiceImpl dataSourceService; + @Mock private Task task; + @Mock private ActionListener actionListener; @Captor private ArgumentCaptor updateDataSourceActionResponseArgumentCaptor; - @Captor - private ArgumentCaptor exceptionArgumentCaptor; + @Captor private ArgumentCaptor exceptionArgumentCaptor; @BeforeEach public void setUp() { - action = new TransportUpdateDataSourceAction(transportService, - new ActionFilters(new HashSet<>()), dataSourceService); + action = + new TransportUpdateDataSourceAction( + transportService, new ActionFilters(new HashSet<>()), dataSourceService); } @Test @@ -62,10 +57,10 @@ public void testDoExecute() { verify(dataSourceService, times(1)).updateDataSource(dataSourceMetadata); Mockito.verify(actionListener) .onResponse(updateDataSourceActionResponseArgumentCaptor.capture()); - UpdateDataSourceActionResponse updateDataSourceActionResponse - = updateDataSourceActionResponseArgumentCaptor.getValue(); - Assertions.assertEquals("Updated DataSource with name test_datasource", - updateDataSourceActionResponse.getResult()); + UpdateDataSourceActionResponse updateDataSourceActionResponse = + updateDataSourceActionResponseArgumentCaptor.getValue(); + Assertions.assertEquals( + "Updated DataSource with name test_datasource", updateDataSourceActionResponse.getResult()); } @Test @@ -73,7 +68,8 @@ public void testDoExecuteWithException() { DataSourceMetadata dataSourceMetadata = new DataSourceMetadata(); dataSourceMetadata.setName("test_datasource"); dataSourceMetadata.setConnector(DataSourceType.PROMETHEUS); - doThrow(new RuntimeException("Error")).when(dataSourceService) + doThrow(new RuntimeException("Error")) + .when(dataSourceService) .updateDataSource(dataSourceMetadata); UpdateDataSourceActionRequest request = new UpdateDataSourceActionRequest(dataSourceMetadata); action.doExecute(task, request, actionListener); @@ -81,7 +77,6 @@ public void testDoExecuteWithException() { Mockito.verify(actionListener).onFailure(exceptionArgumentCaptor.capture()); Exception exception = exceptionArgumentCaptor.getValue(); Assertions.assertTrue(exception instanceof RuntimeException); - Assertions.assertEquals("Error", - exception.getMessage()); + Assertions.assertEquals("Error", exception.getMessage()); } } diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/utils/SchedulerTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/utils/SchedulerTest.java index e3dac306cd..ff23cdcabb 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/utils/SchedulerTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/utils/SchedulerTest.java @@ -19,27 +19,24 @@ @ExtendWith(MockitoExtension.class) public class SchedulerTest { - @Mock - private NodeClient nodeClient; + @Mock private NodeClient nodeClient; - @Mock - private ThreadPool threadPool; + @Mock private ThreadPool threadPool; @Test public void testSchedule() { Mockito.when(nodeClient.threadPool()).thenReturn(threadPool); Mockito.doAnswer( - invocation -> { - Runnable task = invocation.getArgument(0); - task.run(); - return null; - }) + invocation -> { + Runnable task = invocation.getArgument(0); + task.run(); + return null; + }) .when(threadPool) .schedule(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any()); AtomicBoolean isRun = new AtomicBoolean(false); Scheduler.schedule(nodeClient, () -> isRun.set(true)); Assert.assertTrue(isRun.get()); } - } diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/utils/XContentParserUtilsTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/utils/XContentParserUtilsTest.java index f47d0503e7..c0c05c0282 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/utils/XContentParserUtilsTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/utils/XContentParserUtilsTest.java @@ -30,7 +30,8 @@ public void testConvertToXContent() { XContentBuilder contentBuilder = XContentParserUtils.convertToXContent(dataSourceMetadata); String contentString = BytesReference.bytes(contentBuilder).utf8ToString(); - Assertions.assertEquals("{\"name\":\"testDS\",\"connector\":\"PROMETHEUS\",\"allowedRoles\":[\"prometheus_access\"],\"properties\":{\"prometheus.uri\":\"https://localhost:9090\"}}", + Assertions.assertEquals( + "{\"name\":\"testDS\",\"connector\":\"PROMETHEUS\",\"allowedRoles\":[\"prometheus_access\"],\"properties\":{\"prometheus.uri\":\"https://localhost:9090\"}}", contentString); } @@ -49,7 +50,6 @@ public void testToDataSourceMetadataFromJson() { Assertions.assertEquals(retrievedMetadata, dataSourceMetadata); Assertions.assertEquals("prometheus_access", retrievedMetadata.getAllowedRoles().get(0)); - } @SneakyThrows @@ -62,9 +62,12 @@ public void testToDataSourceMetadataFromJsonWithoutName() { Gson gson = new Gson(); String json = gson.toJson(dataSourceMetadata); - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { - XContentParserUtils.toDataSourceMetadata(json); - }); + IllegalArgumentException exception = + assertThrows( + IllegalArgumentException.class, + () -> { + XContentParserUtils.toDataSourceMetadata(json); + }); Assertions.assertEquals("name and connector are required fields.", exception.getMessage()); } @@ -78,9 +81,12 @@ public void testToDataSourceMetadataFromJsonWithoutConnector() { Gson gson = new Gson(); String json = gson.toJson(dataSourceMetadata); - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { - XContentParserUtils.toDataSourceMetadata(json); - }); + IllegalArgumentException exception = + assertThrows( + IllegalArgumentException.class, + () -> { + XContentParserUtils.toDataSourceMetadata(json); + }); Assertions.assertEquals("name and connector are required fields.", exception.getMessage()); } @@ -92,10 +98,12 @@ public void testToDataSourceMetadataFromJsonUsingUnknownObject() { Gson gson = new Gson(); String json = gson.toJson(hashMap); - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { - XContentParserUtils.toDataSourceMetadata(json); - }); + IllegalArgumentException exception = + assertThrows( + IllegalArgumentException.class, + () -> { + XContentParserUtils.toDataSourceMetadata(json); + }); Assertions.assertEquals("Unknown field: test", exception.getMessage()); } - }