Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Ho <dxho@amazon.com>
  • Loading branch information
derek-ho committed Oct 13, 2023
1 parent 8ca5e03 commit 0ac557a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,25 @@ public static DataSourceMetadata toDataSourceMetadata(String json) throws IOExce
}
}

/**
* Converts json string to Map.
*
* @param json jsonstring.
* @return DataSourceData
* @throws IOException IOException.
*/
public static Map<String, Object> toMap(String json) throws IOException {
try (XContentParser parser =
XContentType.JSON
.xContent()
.createParser(
NamedXContentRegistry.EMPTY,
DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
json)) {
return toMap(parser);
}
}

/**
* Converts DataSourceMetadata to XContentBuilder.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
import static org.opensearch.sql.analysis.DataSourceSchemaIdentifierNameResolver.DEFAULT_DATASOURCE_NAME;
import static org.opensearch.sql.datasources.utils.XContentParserUtils.DESCRIPTION_FIELD;
import static org.opensearch.sql.datasources.utils.XContentParserUtils.NAME_FIELD;

import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
Expand Down Expand Up @@ -264,6 +266,7 @@ void testGetDataSourceMetadataSetWithDefaultDatasource() {

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

DataSourceMetadata dataSourceMetadata =
metadata("testDS", DataSourceType.OPENSEARCH, Collections.emptyList(), ImmutableMap.of());
Expand All @@ -289,6 +292,14 @@ void testUpdateDefaultDataSource() {
unsupportedOperationException.getMessage());
}

@Test
void testPatchDataSourceSuccessCase() {
Map<String, Object> dataSourceData = new HashMap<>(Map.of(NAME_FIELD, "test", DESCRIPTION_FIELD, "test"));

dataSourceService.patchDataSource(dataSourceData);
verify(dataSourceMetadataStorage, times(1)).updateDataSourceMetadata(any());
}

@Test
void testDeleteDatasource() {
dataSourceService.deleteDataSource("testDS");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.opensearch.sql.datasources.utils;

import static org.junit.jupiter.api.Assertions.assertThrows;
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;
import java.util.Map;
Expand Down Expand Up @@ -52,6 +55,19 @@ public void testToDataSourceMetadataFromJson() {
Assertions.assertEquals("prometheus_access", retrievedMetadata.getAllowedRoles().get(0));
}

@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, ""));
Gson gson = new Gson();
String json = gson.toJson(dataSourceData);

Map<String, Object> parsedData = XContentParserUtils.toMap(json);

Assertions.assertEquals(parsedData, dataSourceData);
Assertions.assertEquals("test", parsedData.get(DESCRIPTION_FIELD));
}

@SneakyThrows
@Test
public void testToDataSourceMetadataFromJsonWithoutName() {
Expand All @@ -71,6 +87,22 @@ public void testToDataSourceMetadataFromJsonWithoutName() {
Assertions.assertEquals("name and connector are required fields.", exception.getMessage());
}

@SneakyThrows
@Test
public void testToMapFromJsonWithoutName() {
Map<String, Object> dataSourceData = new HashMap<>(Map.of(DESCRIPTION_FIELD, "test"));
Gson gson = new Gson();
String json = gson.toJson(dataSourceData);

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

@SneakyThrows
@Test
public void testToDataSourceMetadataFromJsonWithoutConnector() {
Expand Down

0 comments on commit 0ac557a

Please sign in to comment.