Skip to content

Commit

Permalink
Fix tests and increase code cov
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Ho <[email protected]>
  • Loading branch information
derek-ho committed Oct 13, 2023
1 parent 1051d50 commit af4c924
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

package org.opensearch.sql.datasources.storage;

import static org.opensearch.sql.datasources.utils.XContentParserUtils.PROPERTIES_FIELD;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -265,16 +263,6 @@ private DataSourceMetadata encryptDecryptAuthenticationData(
return dataSourceMetadata;
}

// Encrypt and Decrypt irrespective of auth type.If properties name ends in username, password,
// secret_key and access_key.
private Map<String, Object> encryptDecryptAuthenticationData(
Map<String, Object> dataSourceData, Boolean isEncryption) {
Map<String, String> propertiesMap = (Map<String, String>) dataSourceData.get(PROPERTIES_FIELD);
handleBasicAuthPropertiesEncryptionDecryption(propertiesMap, isEncryption);
handleSigV4PropertiesEncryptionDecryption(propertiesMap, isEncryption);
return dataSourceData;
}

private void handleBasicAuthPropertiesEncryptionDecryption(
Map<String, String> propertiesMap, Boolean isEncryption) {
ArrayList<String> list = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ public static DataSourceMetadata toDataSourceMetadata(String json) throws IOExce
*/
public static Map<String, Object> toMap(String json) throws IOException {
try (XContentParser parser =
XContentType.JSON
.xContent()
.createParser(
NamedXContentRegistry.EMPTY,
DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
json)) {
XContentType.JSON
.xContent()
.createParser(
NamedXContentRegistry.EMPTY,
DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
json)) {
return toMap(parser);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,6 @@ void testGetDataSourceMetadataSetWithDefaultDatasource() {

@Test
void testUpdateDataSourceSuccessCase() {
doNothing().when(dataSourceUserAuthorizationHelper).authorizeDataSource(any());

DataSourceMetadata dataSourceMetadata =
metadata("testDS", DataSourceType.OPENSEARCH, Collections.emptyList(), ImmutableMap.of());
dataSourceService.updateDataSource(dataSourceMetadata);
Expand Down Expand Up @@ -295,15 +293,12 @@ void testUpdateDefaultDataSource() {
@Test
void testPatchDataSourceSuccessCase() {
// Tests that patch underlying implementation is to call update
Map<String, Object> dataSourceData = new HashMap<>(Map.of(NAME_FIELD, "testDS", DESCRIPTION_FIELD, "test"));
DataSourceMetadata getData = metadata(
"testDS",
DataSourceType.OPENSEARCH,
Collections.emptyList(),
ImmutableMap.of());
Map<String, Object> dataSourceData =
new HashMap<>(Map.of(NAME_FIELD, "testDS", DESCRIPTION_FIELD, "test"));
DataSourceMetadata getData =
metadata("testDS", DataSourceType.OPENSEARCH, Collections.emptyList(), ImmutableMap.of());
when(dataSourceMetadataStorage.getDataSourceMetadata("testDS"))
.thenReturn(
Optional.ofNullable(getData));
.thenReturn(Optional.ofNullable(getData));

dataSourceService.patchDataSource(dataSourceData);
verify(dataSourceMetadataStorage, times(1)).updateDataSourceMetadata(any());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static org.opensearch.sql.datasources.utils.XContentParserUtils.*;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -58,7 +57,21 @@ public void testToDataSourceMetadataFromJson() {
@SneakyThrows
@Test
public void testToMapFromJson() {
Map<String, Object> dataSourceData = new HashMap<>(Map.of(NAME_FIELD, "test_DS", DESCRIPTION_FIELD, "test", ALLOWED_ROLES_FIELD, new ArrayList<>(), PROPERTIES_FIELD, Map.of(), RESULT_INDEX_FIELD, ""));
Map<String, Object> dataSourceData =
new HashMap<>(
Map.of(
NAME_FIELD,
"test_DS",
DESCRIPTION_FIELD,
"test",
ALLOWED_ROLES_FIELD,
new ArrayList<>(),
PROPERTIES_FIELD,
Map.of(),
CONNECTOR_FIELD,
"PROMETHEUS",
RESULT_INDEX_FIELD,
""));
Gson gson = new Gson();
String json = gson.toJson(dataSourceData);

Expand Down Expand Up @@ -95,11 +108,11 @@ public void testToMapFromJsonWithoutName() {
String json = gson.toJson(dataSourceData);

IllegalArgumentException exception =
assertThrows(
IllegalArgumentException.class,
() -> {
XContentParserUtils.toMap(json);
});
assertThrows(
IllegalArgumentException.class,
() -> {
XContentParserUtils.toMap(json);
});
Assertions.assertEquals("Name is a required field.", exception.getMessage());
}

Expand Down

0 comments on commit af4c924

Please sign in to comment.