Skip to content

Commit

Permalink
write unit test for sft with no geometry
Browse files Browse the repository at this point in the history
  • Loading branch information
adeet1 committed Mar 22, 2024
1 parent e8e044d commit c385def
Showing 1 changed file with 45 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ import java.io.{File, RandomAccessFile}
import java.nio.file.Files
import java.nio.file.Paths
import scala.collection.mutable.ArrayBuffer

import com.fasterxml.jackson.databind.ObjectMapper
import com.networknt.schema.{JsonSchemaFactory, SpecVersion}
import com.networknt.schema.{JsonSchemaFactory, SpecVersion, ValidationMessage}

import scala.collection.mutable

@RunWith(classOf[JUnitRunner])
class ParquetReadWriteTest extends Specification with AllExpectations with LazyLogging {
Expand Down Expand Up @@ -116,6 +117,20 @@ class ParquetReadWriteTest extends Specification with AllExpectations with LazyL
}
}

// Helper method that validates the file metadata against the GeoParquet metadata json schema
def validateMetadata(metadataString: String): mutable.Set[ValidationMessage] = {
val schema = {
val schemaFile = new File(getClass.getClassLoader.getResource("geoparquet-metadata-schema.json").toURI)
val schemaReader = scala.io.Source.fromFile(schemaFile)
val schemaString = schemaReader.mkString
schemaReader.close()
JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4).getSchema(schemaString)
}
val metadata = new ObjectMapper().readTree(metadataString)

schema.validate(metadata).asScala
}

"SimpleFeatureParquetWriter" should {

"fail if a corrupt parquet file is written" >> {
Expand All @@ -138,31 +153,45 @@ class ParquetReadWriteTest extends Specification with AllExpectations with LazyL
}
}

"write parquet files" >> {
"write geoparquet files" >> {
val writer = SimpleFeatureParquetWriter.builder(new Path(f.toUri), sftConf).build()
WithClose(writer) { writer =>
features.foreach(writer.write)
}

Files.size(f) must beGreaterThan(0L)

// Check that the metadata in the file validates against the GeoParquet json schema
val schema = {
val schemaFile = new File(getClass.getClassLoader.getResource("geoparquet-metadata-schema.json").toURI)
val schemaReader = scala.io.Source.fromFile(schemaFile)
val schemaString = schemaReader.mkString
schemaReader.close()
JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4).getSchema(schemaString)
val metadata = writer.getFooter.getFileMetaData.getKeyValueMetaData.get(GeoParquetSchemaKey)
validateMetadata(metadata) must beEmpty
}

"write a geoparquet file for an sft with no geometry" >> {
val f = Files.createTempFile("geomesa", ".parquet")
val sft = SimpleFeatureTypes.createType("test", "name:String,age:Int,dtg:Date")
val sftConf = {
val c = new Configuration()
StorageConfiguration.setSft(c, sft)
// Use GZIP in tests but snappy in prod due to license issues
c.set(ParquetCompressionOpt, CompressionCodecName.GZIP.toString)
c
}
val metadata = {
val metadataString = writer.getFooter.getFileMetaData.getKeyValueMetaData.get(GeoParquetSchemaKey)
new ObjectMapper().readTree(metadataString)

val features = Seq(
ScalaSimpleFeature.create(sft, "1", "first", 100, "2017-01-01T00:00:00Z"),
ScalaSimpleFeature.create(sft, "2", null, 200, "2017-01-02T00:00:00Z"),
ScalaSimpleFeature.create(sft, "3", "third", 300, "2017-01-03T00:00:00Z")
)

val writer = SimpleFeatureParquetWriter.builder(new Path(f.toUri), sftConf).build()
WithClose(writer) { writer =>
features.foreach(writer.write)
}
Files.size(f) must beGreaterThan(0L)

schema.validate(metadata).asScala must beEmpty
val metadata = writer.getFooter.getFileMetaData.getKeyValueMetaData.get(GeoParquetSchemaKey)
validateMetadata(metadata) must beEmpty
}

"read parquet files" >> {
"read geoparquet files" >> {
val result = readFile(FilterCompat.NOOP, sftConf)
result mustEqual features
}
Expand Down

0 comments on commit c385def

Please sign in to comment.