Skip to content

Commit

Permalink
Fix test and jacoco passing
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 970b5bf commit 721c64f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
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 static org.opensearch.sql.datasources.utils.XContentParserUtils.*;

import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
Expand Down Expand Up @@ -307,7 +306,20 @@ void testPatchDefaultDataSource() {
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"));
new HashMap<>(
Map.of(
NAME_FIELD,
"testDS",
DESCRIPTION_FIELD,
"test",
CONNECTOR_FIELD,
"PROMETHEUS",
ALLOWED_ROLES_FIELD,
new ArrayList<>(),
PROPERTIES_FIELD,
Map.of(),
RESULT_INDEX_FIELD,
""));
DataSourceMetadata getData =
metadata("testDS", DataSourceType.OPENSEARCH, Collections.emptyList(), ImmutableMap.of());
when(dataSourceMetadataStorage.getDataSourceMetadata("testDS"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,39 @@ public void testToDataSourceMetadataFromJson() {
@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(),
CONNECTOR_FIELD,
"PROMETHEUS",
RESULT_INDEX_FIELD,
""));
Map.of(
NAME_FIELD,
"test_DS",
DESCRIPTION_FIELD,
"test",
ALLOWED_ROLES_FIELD,
new ArrayList<>(),
PROPERTIES_FIELD,
Map.of(),
CONNECTOR_FIELD,
"PROMETHEUS",
RESULT_INDEX_FIELD,
"");

Map<String, Object> dataSourceDataConnectorRemoved =
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(parsedData, dataSourceDataConnectorRemoved);
Assertions.assertEquals("test", parsedData.get(DESCRIPTION_FIELD));
}

Expand Down Expand Up @@ -151,4 +164,21 @@ public void testToDataSourceMetadataFromJsonUsingUnknownObject() {
});
Assertions.assertEquals("Unknown field: test", exception.getMessage());
}

@SneakyThrows
@Test
public void testToMapFromJsonUsingUnknownObject() {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("test", "test");
Gson gson = new Gson();
String json = gson.toJson(hashMap);

IllegalArgumentException exception =
assertThrows(
IllegalArgumentException.class,
() -> {
XContentParserUtils.toMap(json);
});
Assertions.assertEquals("Unknown field: test", exception.getMessage());
}
}

0 comments on commit 721c64f

Please sign in to comment.