Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.parquet.avro;

import static org.assertj.core.api.Assertions.assertThat;

import com.google.common.collect.Lists;
import java.io.File;
import java.io.IOException;
Expand All @@ -34,7 +36,6 @@
import org.apache.parquet.hadoop.ParquetWriter;
import org.apache.parquet.hadoop.util.HadoopInputFile;
import org.apache.parquet.variant.Variant;
import org.junit.Assert;
import org.junit.rules.TemporaryFolder;

public class AvroTestUtil {
Expand Down Expand Up @@ -111,7 +112,7 @@ public static <D> File write(TemporaryFolder temp, GenericData model, Schema sch
public static <D> File write(TemporaryFolder temp, Configuration conf, GenericData model, Schema schema, D... data)
throws IOException {
File file = temp.newFile();
Assert.assertTrue(file.delete());
assertThat(file.delete()).isTrue();

try (ParquetWriter<D> writer = AvroParquetWriter.<D>builder(new Path(file.toString()))
.withDataModel(model)
Expand All @@ -136,30 +137,30 @@ public static Configuration conf(String name, boolean value) {
* E.g. values in an object may be ordered differently in the binary.
*/
static void assertEquivalent(Variant expected, Variant actual) {
Assert.assertEquals(expected.getType(), actual.getType());
assertThat(actual.getType()).isEqualTo(expected.getType());
switch (expected.getType()) {
case STRING:
// Short strings may use the compact or extended representation.
Assert.assertEquals(expected.getString(), actual.getString());
assertThat(actual.getString()).isEqualTo(expected.getString());
break;
case ARRAY:
Assert.assertEquals(expected.numArrayElements(), actual.numArrayElements());
assertThat(actual.numArrayElements()).isEqualTo(expected.numArrayElements());
for (int i = 0; i < expected.numArrayElements(); ++i) {
assertEquivalent(expected.getElementAtIndex(i), actual.getElementAtIndex(i));
}
break;
case OBJECT:
Assert.assertEquals(expected.numObjectElements(), actual.numObjectElements());
assertThat(actual.numObjectElements()).isEqualTo(expected.numObjectElements());
for (int i = 0; i < expected.numObjectElements(); ++i) {
Variant.ObjectField expectedField = expected.getFieldAtIndex(i);
Variant.ObjectField actualField = actual.getFieldAtIndex(i);
Assert.assertEquals(expectedField.key, actualField.key);
assertThat(actualField.key).isEqualTo(expectedField.key);
assertEquivalent(expectedField.value, actualField.value);
}
break;
default:
// All other types have a single representation, and must be bit-for-bit identical.
Assert.assertEquals(expected.getValueBuffer(), actual.getValueBuffer());
assertThat(actual.getValueBuffer()).isEqualTo(expected.getValueBuffer());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import static org.apache.parquet.avro.AvroTestUtil.optionalField;
import static org.apache.parquet.avro.AvroTestUtil.primitive;
import static org.apache.parquet.avro.AvroTestUtil.record;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.io.IOException;
import java.util.Arrays;
Expand All @@ -41,7 +43,6 @@
import org.apache.parquet.io.InvalidRecordException;
import org.apache.parquet.schema.MessageType;
import org.apache.parquet.schema.MessageTypeParser;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
Expand Down Expand Up @@ -1046,10 +1047,9 @@ public void testListOfSingleElementStructsWithElementField() throws Exception {
final MessageType fileSchema;
try (ParquetFileReader reader = ParquetFileReader.open(new Configuration(), test)) {
fileSchema = reader.getFileMetaData().getSchema();
Assert.assertEquals(
"Converted schema should assume 2-layer structure",
oldSchema,
new AvroSchemaConverter(OLD_BEHAVIOR_CONF).convert(fileSchema));
assertThat(new AvroSchemaConverter(OLD_BEHAVIOR_CONF).convert(fileSchema))
.as("Converted schema should assume 2-layer structure")
.isEqualTo(oldSchema);
}

// both should default to the 2-layer structure
Expand All @@ -1066,10 +1066,9 @@ public void testListOfSingleElementStructsWithElementField() throws Exception {
instance(structWithElementField, "element", 34.0F)));

// check the schema
Assert.assertEquals(
"Converted schema should assume 3-layer structure",
newSchema,
new AvroSchemaConverter(NEW_BEHAVIOR_CONF).convert(fileSchema));
assertThat(new AvroSchemaConverter(NEW_BEHAVIOR_CONF).convert(fileSchema))
.as("Converted schema should assume 3-layer structure")
.isEqualTo(newSchema);
assertReaderContains(newBehaviorReader(test), newSchema, newRecord);

// check that this works with compatible nested schemas
Expand Down Expand Up @@ -1118,9 +1117,10 @@ public void testIsElementTypeRequiredRepeatedRecord() {
+ "}");
Schema avroSchema = new AvroSchemaConverter().convert(parquetSchema);

Assert.assertTrue(AvroRecordConverter.isElementType(
parquetSchema.getType("list_field").asGroupType().getType("list_field_tuple"),
avroSchema.getFields().get(0).schema()));
assertThat(AvroRecordConverter.isElementType(
parquetSchema.getType("list_field").asGroupType().getType("list_field_tuple"),
avroSchema.getFields().get(0).schema()))
.isTrue();

// Test `array` style naming
parquetSchema = MessageTypeParser.parseMessageType("message SchemaWithList {\n"
Expand All @@ -1132,9 +1132,10 @@ public void testIsElementTypeRequiredRepeatedRecord() {
+ "}");
avroSchema = new AvroSchemaConverter().convert(parquetSchema);

Assert.assertTrue(AvroRecordConverter.isElementType(
parquetSchema.getType("list_field"),
avroSchema.getFields().get(0).schema()));
assertThat(AvroRecordConverter.isElementType(
parquetSchema.getType("list_field"),
avroSchema.getFields().get(0).schema()))
.isTrue();
}

@Test
Expand All @@ -1150,10 +1151,12 @@ public void testIsElementTypeInt96Element() {
+ " }\n"
+ "}");
Schema avroSchema = new AvroSchemaConverter(configuration).convert(parquetSchema);
Assert.assertTrue(AvroRecordConverter.isElementType(
parquetSchema.getType("list").asGroupType().getType("list"),
AvroSchemaConverter.getNonNull(avroSchema.getFields().get(0).schema())
.getElementType()));
assertThat(AvroRecordConverter.isElementType(
parquetSchema.getType("list").asGroupType().getType("list"),
AvroSchemaConverter.getNonNull(
avroSchema.getFields().get(0).schema())
.getElementType()))
.isTrue();
}

@Test
Expand All @@ -1168,9 +1171,10 @@ public void testIsElementTypeOptionalRepeatedRecord() {
+ "}");
Schema avroSchema = new AvroSchemaConverter().convert(parquetSchema);

Assert.assertTrue(AvroRecordConverter.isElementType(
parquetSchema.getType("list_field").asGroupType().getType("list_field_tuple"),
avroSchema.getFields().get(0).schema()));
assertThat(AvroRecordConverter.isElementType(
parquetSchema.getType("list_field").asGroupType().getType("list_field_tuple"),
avroSchema.getFields().get(0).schema()))
.isTrue();

// Test `array` style naming
parquetSchema = MessageTypeParser.parseMessageType("message SchemaWithList {\n"
Expand All @@ -1182,9 +1186,10 @@ public void testIsElementTypeOptionalRepeatedRecord() {
+ "}");
avroSchema = new AvroSchemaConverter().convert(parquetSchema);

Assert.assertTrue(AvroRecordConverter.isElementType(
parquetSchema.getType("list_field"),
avroSchema.getFields().get(0).schema()));
assertThat(AvroRecordConverter.isElementType(
parquetSchema.getType("list_field"),
avroSchema.getFields().get(0).schema()))
.isTrue();
}

@Test
Expand Down Expand Up @@ -1246,7 +1251,9 @@ public void testIsElementTypeFailsInvalidSchema() throws Exception {
AvroReadSupport.setAvroReadSchema(oldConfWithSchema, newSchema);

AvroParquetReader<GenericRecord> reader = new AvroParquetReader<>(oldConfWithSchema, test);
Assert.assertThrows(InvalidRecordException.class, reader::read);
assertThatThrownBy(reader::read)
.isInstanceOf(InvalidRecordException.class)
.hasMessage("Parquet/Avro schema mismatch. Avro field 'element' not found.");
}

public <T extends IndexedRecord> AvroParquetReader<T> oldBehaviorReader(Path path) throws IOException {
Expand Down Expand Up @@ -1275,11 +1282,14 @@ public <T extends IndexedRecord> void assertReaderContains(
AvroParquetReader<T> reader, Schema expectedSchema, T... expectedRecords) throws IOException {
for (T expectedRecord : expectedRecords) {
T actualRecord = reader.read();
Assert.assertEquals("Should match expected schema", expectedSchema, actualRecord.getSchema());
Assert.assertEquals("Should match the expected record", expectedRecord, actualRecord);
assertThat(actualRecord.getSchema())
.as("Should match expected schema")
.isEqualTo(expectedSchema);
assertThat(actualRecord).as("Should match the expected record").isEqualTo(expectedRecord);
}
Assert.assertNull(
"Should only contain " + expectedRecords.length + " record" + (expectedRecords.length == 1 ? "" : "s"),
reader.read());
assertThat(reader.read())
.as("Should only contain " + expectedRecords.length + " record"
+ (expectedRecords.length == 1 ? "" : "s"))
.isNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
*/
package org.apache.parquet.avro;

import static org.assertj.core.api.Assertions.assertThat;

import org.apache.avro.generic.GenericData;
import org.apache.hadoop.conf.Configuration;
import org.junit.Assert;
import org.junit.Test;

public class TestAvroDataSupplier {
Expand All @@ -36,9 +37,8 @@ public GenericData get() {
public void testSetSupplierMethod() {
Configuration conf = new Configuration(false);
AvroReadSupport.setAvroDataSupplier(conf, GenericDataSupplier.class);
Assert.assertEquals(
"Should contain the class name",
"org.apache.parquet.avro.TestAvroDataSupplier$GenericDataSupplier",
conf.get(AvroReadSupport.AVRO_DATA_SUPPLIER));
assertThat(conf.get(AvroReadSupport.AVRO_DATA_SUPPLIER))
.as("Should contain the class name")
.isEqualTo("org.apache.parquet.avro.TestAvroDataSupplier$GenericDataSupplier");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
*/
package org.apache.parquet.avro;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.CALLS_REAL_METHODS;

import com.google.common.collect.Lists;
Expand Down Expand Up @@ -61,16 +58,16 @@ public void testModelForSpecificRecordWithLogicalTypes() {

// Test that model is generated correctly
Collection<Conversion<?>> conversions = model.getConversions();
assertEquals(conversions.size(), 3);
assertNotNull(model.getConversionByClass(Instant.class));
assertNotNull(model.getConversionByClass(LocalDate.class));
assertNotNull(model.getConversionByClass(LocalTime.class));
assertThat(conversions).hasSize(3);
assertThat(model.getConversionByClass(Instant.class)).isNotNull();
assertThat(model.getConversionByClass(LocalDate.class)).isNotNull();
assertThat(model.getConversionByClass(LocalTime.class)).isNotNull();
}

@Test
public void testModelForSpecificRecordWithoutLogicalTypes() {
SpecificData model = AvroRecordConverter.getModelForSchema(Car.SCHEMA$);
assertTrue(model.getConversions().isEmpty());
assertThat(model.getConversions()).isEmpty();
}

@Test
Expand All @@ -83,7 +80,7 @@ public void testModelForGenericRecord() {
Lists.newArrayList(new Schema.Field("strField", Schema.create(Schema.Type.STRING)))));

// There is no class "someSchema" on the classpath, so should return null
assertNull(model);
assertThat(model).isNull();
}

// Test logical type support for older Avro versions
Expand All @@ -95,18 +92,18 @@ public void testModelForSpecificRecordWithLogicalTypesWithDeprecatedAvro1_8() {
SpecificData model = AvroRecordConverter.getModelForSchema(LogicalTypesTestDeprecated.SCHEMA$);
// Test that model is generated correctly
Collection<Conversion<?>> conversions = model.getConversions();
assertEquals(3, conversions.size());
assertNotNull(model.getConversionByClass(Instant.class));
assertNotNull(model.getConversionByClass(LocalDate.class));
assertNotNull(model.getConversionByClass(LocalTime.class));
assertThat(conversions).hasSize(3);
assertThat(model.getConversionByClass(Instant.class)).isNotNull();
assertThat(model.getConversionByClass(LocalDate.class)).isNotNull();
assertThat(model.getConversionByClass(LocalTime.class)).isNotNull();

// Test that model is generated correctly when record contains only nested logical types
model = AvroRecordConverter.getModelForSchema(NestedOnlyLogicalTypesDeprecated.SCHEMA$);
// Test that model is generated correctly
conversions = model.getConversions();
assertEquals(2, conversions.size());
assertNotNull(model.getConversionByClass(LocalDate.class));
assertNotNull(model.getConversionByClass(LocalTime.class));
assertThat(conversions).hasSize(2);
assertThat(model.getConversionByClass(LocalDate.class)).isNotNull();
assertThat(model.getConversionByClass(LocalTime.class)).isNotNull();
}

@Test
Expand All @@ -117,10 +114,10 @@ public void testModelForSpecificRecordWithLogicalTypesWithDeprecatedAvro1_7() {
final SpecificData model = AvroRecordConverter.getModelForSchema(LogicalTypesTestDeprecated.SCHEMA$);
// Test that model is generated correctly
Collection<Conversion<?>> conversions = model.getConversions();
assertEquals(conversions.size(), 3);
assertNotNull(model.getConversionByClass(Instant.class));
assertNotNull(model.getConversionByClass(LocalDate.class));
assertNotNull(model.getConversionByClass(LocalTime.class));
assertThat(conversions).hasSize(3);
assertThat(model.getConversionByClass(Instant.class)).isNotNull();
assertThat(model.getConversionByClass(LocalDate.class)).isNotNull();
assertThat(model.getConversionByClass(LocalTime.class)).isNotNull();
}

// Pseudo generated code with bug from avro compiler < 1.8
Expand Down
Loading
Loading