-
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.
Revert "Feature/revert template converter (aws-cloudformation#107)"
This reverts commit 52e4d9d.
- Loading branch information
1 parent
52e4d9d
commit b003bf0
Showing
8 changed files
with
270 additions
and
3 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
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
40 changes: 40 additions & 0 deletions
40
...datasource/src/main/java/software/amazon/kendra/datasource/convert/TemplateConverter.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,40 @@ | ||
package software.amazon.kendra.datasource.convert; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import software.amazon.awssdk.core.document.Document; | ||
import software.amazon.kendra.datasource.DataSourceConfiguration; | ||
import software.amazon.kendra.datasource.TemplateConfiguration; | ||
import software.amazon.kendra.datasource.utils.DocumentTypeAdapter; | ||
|
||
public class TemplateConverter { | ||
private static final Gson builder = new GsonBuilder() | ||
.registerTypeAdapter(Document.class, new DocumentTypeAdapter()) | ||
.setPrettyPrinting() | ||
.create(); | ||
|
||
public static software.amazon.awssdk.services.kendra.model.TemplateConfiguration toSdkDataSourceConfiguration(TemplateConfiguration model) { | ||
if (model == null) { | ||
return null; | ||
} | ||
return software.amazon.awssdk.services.kendra.model.TemplateConfiguration.builder() | ||
.template(builder.fromJson(model.getTemplate(), Document.class)) | ||
.build(); | ||
} | ||
|
||
public static DataSourceConfiguration toModelDataSourceConfiguration( | ||
software.amazon.awssdk.services.kendra.model.TemplateConfiguration templateConfiguration) { | ||
return DataSourceConfiguration.builder() | ||
.templateConfiguration(toModel(templateConfiguration)) | ||
.build(); | ||
} | ||
|
||
private static TemplateConfiguration toModel(software.amazon.awssdk.services.kendra.model.TemplateConfiguration sdk) { | ||
if (sdk == null) { | ||
return null; | ||
} | ||
return TemplateConfiguration.builder() | ||
.template(builder.toJson(sdk.template(), Document.class)) | ||
.build(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...datasource/src/main/java/software/amazon/kendra/datasource/utils/DocumentTypeAdapter.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,33 @@ | ||
package software.amazon.kendra.datasource.utils; | ||
|
||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonParseException; | ||
import com.google.gson.JsonSerializationContext; | ||
import com.google.gson.JsonSerializer; | ||
import software.amazon.awssdk.core.document.Document; | ||
import software.amazon.awssdk.protocols.json.internal.unmarshall.document.DocumentUnmarshaller; | ||
import software.amazon.awssdk.protocols.jsoncore.JsonNode; | ||
import software.amazon.awssdk.protocols.jsoncore.JsonNodeParser; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
public class DocumentTypeAdapter implements JsonDeserializer<Document>, JsonSerializer<Document> { | ||
private final DocumentUnmarshaller documentUnmarshaller = new DocumentUnmarshaller(); | ||
|
||
@Override | ||
public Document deserialize( | ||
final JsonElement jsonElement, final Type type, final JsonDeserializationContext context) | ||
throws JsonParseException { | ||
final JsonNodeParser jsonNodeParser = JsonNodeParser.create(); | ||
final JsonNode jsonNode = jsonNodeParser.parse(jsonElement.toString()); | ||
return jsonNode.visit(documentUnmarshaller); | ||
} | ||
|
||
@Override | ||
public JsonElement serialize( | ||
final Document document, final Type type, final JsonSerializationContext context) { | ||
return context.serialize(document.unwrap()); | ||
} | ||
} |
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
88 changes: 88 additions & 0 deletions
88
...source/src/test/java/software/amazon/kendra/datasource/convert/TemplateConverterTest.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,88 @@ | ||
package software.amazon.kendra.datasource.convert; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.JsonObject; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.junit.jupiter.api.Test; | ||
import software.amazon.awssdk.core.document.Document; | ||
import software.amazon.kendra.datasource.DataSourceConfiguration; | ||
import software.amazon.kendra.datasource.TemplateConfiguration; | ||
import software.amazon.kendra.datasource.utils.DocumentTypeAdapter; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
|
||
public class TemplateConverterTest { | ||
private static final String DATASOURCE_CONFIGURATION = | ||
"testdata/datasourceconfiguration/valid_datasource_configuration.json"; | ||
|
||
@Test | ||
public void testSdkDataSourceConfiguration_Null() { | ||
assertThat(TemplateConverter.toSdkDataSourceConfiguration(null)) | ||
.isEqualTo(null); | ||
} | ||
|
||
@Test | ||
public void testModelDataSourceConfiguration_Null() { | ||
assertThat(TemplateConverter.toModelDataSourceConfiguration(null) | ||
.getTemplateConfiguration()).isEqualTo(null); | ||
} | ||
|
||
@Test | ||
public void testSDKDataSourceConfigurationWithValidConfig() throws IOException { | ||
DataSourceConfiguration dataSourceConfiguration = DataSourceConfiguration.builder() | ||
.templateConfiguration(TemplateConfiguration.builder() | ||
.template(readFileFromLocal(DATASOURCE_CONFIGURATION)) | ||
.build()) | ||
.build(); | ||
|
||
software.amazon.awssdk.services.kendra.model.TemplateConfiguration expectedDataSourceConfiguration = | ||
software.amazon.awssdk.services.kendra.model.TemplateConfiguration.builder() | ||
.template(getTemplate(DATASOURCE_CONFIGURATION)).build(); | ||
|
||
assertThat(TemplateConverter.toSdkDataSourceConfiguration(dataSourceConfiguration.getTemplateConfiguration())) | ||
.isEqualTo(expectedDataSourceConfiguration); | ||
|
||
} | ||
|
||
@Test | ||
public void testModelDataSourceConfigurationWithValidConfig() throws IOException { | ||
software.amazon.awssdk.services.kendra.model.TemplateConfiguration sdkDataSourceConfiguration = | ||
software.amazon.awssdk.services.kendra.model.TemplateConfiguration.builder() | ||
.template(getTemplate(DATASOURCE_CONFIGURATION)).build(); | ||
|
||
DataSourceConfiguration expectedDataSourceConfiguration = DataSourceConfiguration.builder() | ||
.templateConfiguration(TemplateConfiguration.builder() | ||
.template(readFileFromLocal(DATASOURCE_CONFIGURATION)) | ||
.build()) | ||
.build(); | ||
|
||
Gson gson = new Gson(); | ||
DataSourceConfiguration result = TemplateConverter.toModelDataSourceConfiguration(sdkDataSourceConfiguration); | ||
JsonObject resultTemplateConfig = gson.fromJson(result.getTemplateConfiguration().getTemplate(), JsonObject.class); | ||
|
||
JsonObject expectedDataSourceConfigurationTemplateConfig = gson.fromJson( | ||
expectedDataSourceConfiguration.getTemplateConfiguration().getTemplate(), JsonObject.class | ||
); | ||
|
||
assertThat(expectedDataSourceConfigurationTemplateConfig) | ||
.isEqualTo(resultTemplateConfig); | ||
} | ||
|
||
private Document getTemplate(String filePath) throws IOException { | ||
Gson builder = new GsonBuilder() | ||
.registerTypeAdapter(Document.class, new DocumentTypeAdapter()) | ||
.create(); | ||
return builder.fromJson(this.readFileFromLocal(filePath), Document.class); | ||
} | ||
|
||
private String readFileFromLocal(String filePath) throws IOException { | ||
return FileUtils.readFileToString(new File(filePath), Charset.defaultCharset()); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...source/src/test/java/software/amazon/kendra/datasource/utils/DocumentTypeAdapterTest.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,35 @@ | ||
package software.amazon.kendra.datasource.utils; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import software.amazon.awssdk.core.document.Document; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class DocumentTypeAdapterTest { | ||
|
||
private static final String TYPE = "type"; | ||
|
||
private static final String SERIALIZED_DOCUMENT = "{\"type\":\"test\"}"; | ||
private static final Document DOCUMENT = Document.mapBuilder().putString(TYPE, "test").build(); | ||
private Gson gson; | ||
|
||
@BeforeEach | ||
void setup() { | ||
this.gson = new GsonBuilder() | ||
.registerTypeAdapter(Document.class, new DocumentTypeAdapter()) | ||
.create(); | ||
} | ||
|
||
@Test | ||
public void documentSerialization_valid_argument_success() { | ||
assertEquals(SERIALIZED_DOCUMENT, gson.toJson(DOCUMENT, Document.class)); | ||
} | ||
|
||
@Test | ||
public void documentDeserialization_valid_argument_success() { | ||
assertEquals(DOCUMENT, gson.fromJson(SERIALIZED_DOCUMENT, Document.class)); | ||
} | ||
} |