[s3metadataenricher] enrich with metadata when remote exists returns metadata
This commit is contained in:
parent
df0df49624
commit
e75b2a4892
2 changed files with 29 additions and 5 deletions
|
@ -8,7 +8,10 @@ import cats.effect.IO
|
||||||
trait S3MetaDataEnricher extends S3Client {
|
trait S3MetaDataEnricher extends S3Client {
|
||||||
|
|
||||||
def generateKey(c: Config)(file: File): String = {
|
def generateKey(c: Config)(file: File): String = {
|
||||||
s"${c.prefix}/${c.source.toPath.relativize(file.toPath)}"
|
val otherPath = file.toPath.toAbsolutePath
|
||||||
|
val sourcePath = c.source.toPath
|
||||||
|
val relativePath = sourcePath.relativize(otherPath)
|
||||||
|
s"${c.prefix}/$relativePath"
|
||||||
}
|
}
|
||||||
|
|
||||||
def enrichWithS3MetaData(c: Config): File => Stream[IO, Either[File, S3MetaData]] = {
|
def enrichWithS3MetaData(c: Config): File => Stream[IO, Either[File, S3MetaData]] = {
|
||||||
|
|
|
@ -2,17 +2,20 @@ package net.kemitix.s3thorp
|
||||||
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.nio.file.Paths
|
import java.nio.file.Paths
|
||||||
|
import java.time.Instant
|
||||||
|
|
||||||
|
import cats.effect.IO
|
||||||
import org.scalatest.FunSpec
|
import org.scalatest.FunSpec
|
||||||
|
|
||||||
class S3MetaDataEnricherSuite extends FunSpec {
|
class S3MetaDataEnricherSuite extends FunSpec {
|
||||||
|
|
||||||
|
private val sourcePath = "/root/from/here/"
|
||||||
|
private val source = Paths.get(sourcePath).toFile
|
||||||
|
private val prefix = "prefix"
|
||||||
|
private val config = Config("bucket", prefix, source)
|
||||||
|
|
||||||
new S3MetaDataEnricher {
|
new S3MetaDataEnricher {
|
||||||
describe("key generator") {
|
describe("key generator") {
|
||||||
val path = "/root/from/here"
|
|
||||||
val source = Paths.get(path).toFile
|
|
||||||
val prefix = "prefix"
|
|
||||||
val config = Config("bucket", prefix, source)
|
|
||||||
val subject = generateKey(config)_
|
val subject = generateKey(config)_
|
||||||
|
|
||||||
def resolve(subdir: String): File = {
|
def resolve(subdir: String): File = {
|
||||||
|
@ -35,4 +38,22 @@ class S3MetaDataEnricherSuite extends FunSpec {
|
||||||
}
|
}
|
||||||
override def objectHead(bucket: String, key: String) = ???
|
override def objectHead(bucket: String, key: String) = ???
|
||||||
}
|
}
|
||||||
|
|
||||||
|
describe("enrich with metadata") {
|
||||||
|
describe("when remote exists") {
|
||||||
|
val hash = "hash"
|
||||||
|
val lastModified = Instant.now()
|
||||||
|
new S3MetaDataEnricher {
|
||||||
|
override def objectHead(bucket: String, key: String) = IO(Some((hash, lastModified)))
|
||||||
|
it("returns metadata") {
|
||||||
|
val local = "localFile"
|
||||||
|
val localFile = new File(sourcePath + local)
|
||||||
|
val expectedMetadata = S3MetaData(localFile, s"$prefix/$local", hash, lastModified)
|
||||||
|
|
||||||
|
val result: Either[File, S3MetaData] =
|
||||||
|
enrichWithS3MetaData(config)(localFile).compile.toList.unsafeRunSync().head
|
||||||
|
assertResult(Right(expectedMetadata))(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue