From e43b7dc0e3cb1a34064d130df7629da7ac479151 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 9 May 2019 18:34:17 +0100 Subject: [PATCH] [s3uploader] implement with (basic) test --- .../net/kemitix/s3thorp/S3Uploader.scala | 22 +++++++++------- src/main/scala/net/kemitix/s3thorp/Sync.scala | 2 +- .../net/kemitix/s3thorp/S3UploaderSuite.scala | 26 +++++++++++++++++++ 3 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 src/test/scala/net/kemitix/s3thorp/S3UploaderSuite.scala diff --git a/src/main/scala/net/kemitix/s3thorp/S3Uploader.scala b/src/main/scala/net/kemitix/s3thorp/S3Uploader.scala index 1c054bc..2f988a3 100644 --- a/src/main/scala/net/kemitix/s3thorp/S3Uploader.scala +++ b/src/main/scala/net/kemitix/s3thorp/S3Uploader.scala @@ -7,15 +7,19 @@ import cats.effect.IO import net.kemitix.s3thorp.Main.putStrLn import net.kemitix.s3thorp.awssdk.S3Client -import scala.concurrent.Promise +trait S3Uploader + extends S3Client + with KeyGenerator { -trait S3Uploader extends S3Client { - - def performUpload: File => Stream[IO, Promise[Unit]] = - file => Stream.eval(for { - _ <- putStrLn(s"uploading: $file") - // upload - p = Promise[Unit]() - } yield p) + def performUpload(c: Config): File => Stream[IO, Unit] = { + val remoteKey = generateKey(c) _ + file => + Stream.eval(for { + _ <- putStrLn(s"uploading: $file") + key = remoteKey(file) + _ <- upload(file, c.bucket, key) + _ <- putStrLn(s"uploaded: ${c.bucket}/$key") + } yield ()) + } } diff --git a/src/main/scala/net/kemitix/s3thorp/Sync.scala b/src/main/scala/net/kemitix/s3thorp/Sync.scala index 74fdd19..528ad83 100644 --- a/src/main/scala/net/kemitix/s3thorp/Sync.scala +++ b/src/main/scala/net/kemitix/s3thorp/Sync.scala @@ -26,7 +26,7 @@ class Sync(s3Client: S3Client) streamDirectoryPaths(c.source).flatMap( enrichWithS3MetaData(c)).flatMap( uploadRequiredFilter).flatMap( - performUpload).compile.drain + performUpload(c)).compile.drain } } yield () diff --git a/src/test/scala/net/kemitix/s3thorp/S3UploaderSuite.scala b/src/test/scala/net/kemitix/s3thorp/S3UploaderSuite.scala new file mode 100644 index 0000000..5d0c3e1 --- /dev/null +++ b/src/test/scala/net/kemitix/s3thorp/S3UploaderSuite.scala @@ -0,0 +1,26 @@ +package net.kemitix.s3thorp + +import java.io.File + +import cats.effect.IO +import net.kemitix.s3thorp.Sync.{Bucket, LastModified, LocalFile, MD5Hash, RemoteKey} +import org.scalatest.FunSpec + +class S3UploaderSuite extends FunSpec { + + new S3Uploader { + override def objectHead(bucket: String, key: String): IO[Option[(MD5Hash, LastModified)]] = ??? + override def upload(localFile: LocalFile, bucket: Bucket, remoteKey: RemoteKey): IO[Unit] = IO() + + describe("upload") { + val config: Config = Config("bucket", "prefix", new File("/path/to/files")) + def invoke(file: File) = + performUpload(config)(file).compile.toList.unsafeRunSync() + it("should return") { + val result = invoke(new File("/path/to/files/a-file-to-upload.txt")) + assertResult(List(()))(result) + } + } + + } +}