forked from opensearch-project/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change DataSourceType from enum to class (opensearch-project#2730)
* Change DataSourceType from enum to class Signed-off-by: Tomoyuki Morita <[email protected]> * Fix test failure Signed-off-by: Tomoyuki Morita <[email protected]> * Fix serialization issue Signed-off-by: Tomoyuki Morita <[email protected]> * Fix format Signed-off-by: Tomoyuki Morita <[email protected]> * Fix integTest Signed-off-by: Tomoyuki Morita <[email protected]> * Fix style Signed-off-by: Tomoyuki Morita <[email protected]> * Fix failing test Signed-off-by: Tomoyuki Morita <[email protected]> * Address comment Signed-off-by: Tomoyuki Morita <[email protected]> * Fix DataSourceType to allow registering new type Signed-off-by: Tomoyuki Morita <[email protected]> --------- Signed-off-by: Tomoyuki Morita <[email protected]>
- Loading branch information
1 parent
5c02a66
commit c86fd87
Showing
14 changed files
with
302 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
core/src/main/java/org/opensearch/sql/utils/SerializeUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.utils; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonParseException; | ||
import com.google.gson.JsonPrimitive; | ||
import com.google.gson.JsonSerializationContext; | ||
import com.google.gson.JsonSerializer; | ||
import java.lang.reflect.Type; | ||
import lombok.experimental.UtilityClass; | ||
import org.opensearch.sql.datasource.model.DataSourceType; | ||
|
||
@UtilityClass | ||
public class SerializeUtils { | ||
private static class DataSourceTypeSerializer implements JsonSerializer<DataSourceType> { | ||
@Override | ||
public JsonElement serialize( | ||
DataSourceType dataSourceType, | ||
Type type, | ||
JsonSerializationContext jsonSerializationContext) { | ||
return new JsonPrimitive(dataSourceType.name()); | ||
} | ||
} | ||
|
||
private static class DataSourceTypeDeserializer implements JsonDeserializer<DataSourceType> { | ||
@Override | ||
public DataSourceType deserialize( | ||
JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) | ||
throws JsonParseException { | ||
return DataSourceType.fromString(jsonElement.getAsString()); | ||
} | ||
} | ||
|
||
public static GsonBuilder getGsonBuilder() { | ||
return new GsonBuilder() | ||
.registerTypeAdapter(DataSourceType.class, new DataSourceTypeSerializer()) | ||
.registerTypeAdapter(DataSourceType.class, new DataSourceTypeDeserializer()); | ||
} | ||
|
||
public static Gson buildGson() { | ||
return getGsonBuilder().create(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
core/src/test/java/org/opensearch/sql/datasource/model/DataSourceTypeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.datasource.model; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class DataSourceTypeTest { | ||
@Test | ||
public void fromString_succeed() { | ||
testFromString("PROMETHEUS", DataSourceType.PROMETHEUS); | ||
testFromString("OPENSEARCH", DataSourceType.OPENSEARCH); | ||
testFromString("SPARK", DataSourceType.SPARK); | ||
testFromString("S3GLUE", DataSourceType.S3GLUE); | ||
|
||
testFromString("prometheus", DataSourceType.PROMETHEUS); | ||
} | ||
|
||
private void testFromString(String name, DataSourceType expectedType) { | ||
assertEquals(expectedType, DataSourceType.fromString(name)); | ||
} | ||
|
||
@Test | ||
public void fromStringWithUnknownName_throws() { | ||
assertThrows(IllegalArgumentException.class, () -> DataSourceType.fromString("UnknownName")); | ||
} | ||
|
||
@Test | ||
public void registerExistingType_throwsException() { | ||
assertThrows( | ||
IllegalArgumentException.class, | ||
() -> DataSourceType.register(new DataSourceType("s3glue"))); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
core/src/test/java/org/opensearch/sql/utils/SerializeUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.utils; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.gson.Gson; | ||
import org.junit.jupiter.api.Test; | ||
import org.opensearch.sql.datasource.model.DataSourceMetadata; | ||
import org.opensearch.sql.datasource.model.DataSourceStatus; | ||
import org.opensearch.sql.datasource.model.DataSourceType; | ||
|
||
class SerializeUtilsTest { | ||
@Test | ||
public void buildGson_serializeDataSourceTypeAsString() { | ||
DataSourceMetadata dataSourceMetadata = | ||
new DataSourceMetadata.Builder() | ||
.setName("DATASOURCE_NAME") | ||
.setDescription("DESCRIPTION") | ||
.setConnector(DataSourceType.S3GLUE) | ||
.setAllowedRoles(ImmutableList.of("ROLE")) | ||
.setResultIndex("query_execution_result_test") | ||
.setDataSourceStatus(DataSourceStatus.ACTIVE) | ||
.build(); | ||
|
||
Gson gson = SerializeUtils.buildGson(); | ||
String json = gson.toJson(dataSourceMetadata); | ||
|
||
// connector should be serialized as string (not as object) | ||
assertJsonAttribute(json, "connector", "S3GLUE"); | ||
// other attribute should be serialized as normal | ||
assertJsonAttribute(json, "name", "DATASOURCE_NAME"); | ||
assertJsonAttribute(json, "description", "DESCRIPTION"); | ||
assertJsonAttribute(json, "resultIndex", "query_execution_result_test"); | ||
assertJsonAttribute(json, "status", "ACTIVE"); | ||
assertTrue(json.contains("\"allowedRoles\":[\"ROLE\"]")); | ||
} | ||
|
||
private void assertJsonAttribute(String json, String attribute, String value) { | ||
assertTrue(json.contains("\"" + attribute + "\":\"" + value + "\"")); | ||
} | ||
|
||
@Test | ||
public void buildGson_deserializeDataSourceTypeFromString() { | ||
String json = | ||
"{\"name\":\"DATASOURCE_NAME\"," | ||
+ "\"description\":\"DESCRIPTION\"," | ||
+ "\"connector\":\"S3GLUE\"," | ||
+ "\"allowedRoles\":[\"ROLE\"]," | ||
+ "\"properties\":{}," | ||
+ "\"resultIndex\":\"query_execution_result_test\"," | ||
+ "\"status\":\"ACTIVE\"" | ||
+ "}"; | ||
|
||
Gson gson = SerializeUtils.buildGson(); | ||
DataSourceMetadata metadata = gson.fromJson(json, DataSourceMetadata.class); | ||
|
||
assertEquals(DataSourceType.S3GLUE, metadata.getConnector()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.