From e75b2a4892a1484b9e20bcc37661336c25063f84 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 9 May 2019 08:44:16 +0100 Subject: [PATCH] [s3metadataenricher] enrich with metadata when remote exists returns metadata --- .../kemitix/s3thorp/S3MetaDataEnricher.scala | 5 +++- .../s3thorp/S3MetaDataEnricherSuite.scala | 29 ++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/main/scala/net/kemitix/s3thorp/S3MetaDataEnricher.scala b/src/main/scala/net/kemitix/s3thorp/S3MetaDataEnricher.scala index 6b9d0da..5871ce5 100644 --- a/src/main/scala/net/kemitix/s3thorp/S3MetaDataEnricher.scala +++ b/src/main/scala/net/kemitix/s3thorp/S3MetaDataEnricher.scala @@ -8,7 +8,10 @@ import cats.effect.IO trait S3MetaDataEnricher extends S3Client { 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]] = { diff --git a/src/test/scala/net/kemitix/s3thorp/S3MetaDataEnricherSuite.scala b/src/test/scala/net/kemitix/s3thorp/S3MetaDataEnricherSuite.scala index e33c6d7..a614ef4 100644 --- a/src/test/scala/net/kemitix/s3thorp/S3MetaDataEnricherSuite.scala +++ b/src/test/scala/net/kemitix/s3thorp/S3MetaDataEnricherSuite.scala @@ -2,17 +2,20 @@ package net.kemitix.s3thorp import java.io.File import java.nio.file.Paths +import java.time.Instant +import cats.effect.IO import org.scalatest.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 { 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)_ def resolve(subdir: String): File = { @@ -35,4 +38,22 @@ class S3MetaDataEnricherSuite extends FunSpec { } 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) + } + } + } }