Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
tuliren committed Oct 23, 2021
1 parent 301de42 commit e2f0772
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.airbyte.integrations.destination.s3.avro;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectWriter;
import io.airbyte.commons.jackson.MoreMappers;
import io.airbyte.commons.resources.MoreResources;
import io.airbyte.commons.util.MoreIterators;
import java.util.function.Function;
import java.util.stream.Stream;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import tech.allegro.schema.json2avro.converter.JsonAvroConverter;
import tech.allegro.schema.json2avro.converter.JsonHelper;

/**
* This file tests both the schema and object converter.
*/
public class JsonToAvroObjectConversionTest {

private static final ObjectWriter WRITER = MoreMappers.initMapper().writer();
private static final JsonToAvroSchemaConverter SCHEMA_CONVERTER = new JsonToAvroSchemaConverter();

public static class JsonToAvroConverterTestCaseProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(final ExtensionContext context) throws Exception {
final JsonNode testCases = JsonHelper.deserialize(MoreResources.readResource("json_avro_converter.json"));
return MoreIterators.toList(testCases.elements()).stream().map(testCase -> Arguments.of(
testCase.get("schemaName").asText(),
testCase.get("namespace").asText(),
testCase.get("jsonSchema"),
testCase.get("jsonObject"),
testCase.get("avroSchema"),
testCase.get("avroObject"),
testCase.has("name_transformer") && testCase.get("name_transformer").asBoolean()));
}
}

@ParameterizedTest
@ArgumentsSource(JsonToAvroConverterTestCaseProvider.class)
public void testJsonToAvroConverter(final String schemaName,
final String namespace,
final JsonNode jsonSchema,
final JsonNode jsonObject,
final JsonNode avroSchema,
final JsonNode avroObject,
final boolean useNameTransformer)
throws JsonProcessingException {
final JsonAvroConverter objectConverter = JsonAvroConverter.builder()
.setNameTransformer(useNameTransformer ? String::toUpperCase : Function.identity())
.build();
final Schema actualAvroSchema = SCHEMA_CONVERTER.getAvroSchema(jsonSchema, schemaName, namespace, true);
final GenericData.Record actualAvroObject = objectConverter.convertToGenericDataRecord(WRITER.writeValueAsBytes(jsonObject), actualAvroSchema);
assertEquals(avroObject, JsonHelper.deserialize(actualAvroObject.toString()), String.format("Test for %s failed", schemaName));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static class GetFieldTypeTestCaseProvider implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(final ExtensionContext context) throws Exception {
final JsonNode testCases = Jsons.deserialize(MoreResources.readResource("parquet/json_schema_converter/get_field_type.json"));
final JsonNode testCases = Jsons.deserialize(MoreResources.readResource("parquet/json_schema_converter/field_conversion_test_cases.json"));
return MoreIterators.toList(testCases.elements()).stream().map(testCase -> Arguments.of(
testCase.get("fieldName").asText(),
testCase.get("jsonFieldSchema"),
Expand All @@ -78,7 +78,7 @@ public static class GetAvroSchemaTestCaseProvider implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(final ExtensionContext context) throws Exception {
final JsonNode testCases = Jsons.deserialize(MoreResources.readResource("parquet/json_schema_converter/get_avro_schema.json"));
final JsonNode testCases = Jsons.deserialize(MoreResources.readResource("parquet/json_schema_converter/schema_conversion_test_cases.json"));
return MoreIterators.toList(testCases.elements()).stream().map(testCase -> Arguments.of(
testCase.get("schemaName").asText(),
testCase.get("namespace").asText(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{

}
]

0 comments on commit e2f0772

Please sign in to comment.