[s3client] simple test for upload

This commit is contained in:
Paul Campbell 2019-05-10 19:51:25 +01:00
parent bc1bffc345
commit 41e38f5cee

View file

@ -1,15 +1,18 @@
package net.kemitix.s3thorp.awssdk package net.kemitix.s3thorp.awssdk
import java.io.File
import java.time.Instant import java.time.Instant
import cats.effect.IO import cats.effect.IO
import com.github.j5ik2o.reactive.aws.s3.S3AsyncClient
import com.github.j5ik2o.reactive.aws.s3.cats.S3CatsIOClient import com.github.j5ik2o.reactive.aws.s3.cats.S3CatsIOClient
import net.kemitix.s3thorp.Sync.{Bucket, LocalFile, RemoteKey}
import org.scalatest.FunSpec import org.scalatest.FunSpec
import software.amazon.awssdk.services.s3.model.{HeadObjectRequest, HeadObjectResponse, NoSuchKeyException} import software.amazon.awssdk.services.s3.model.{HeadObjectRequest, HeadObjectResponse, NoSuchKeyException, PutObjectRequest, PutObjectResponse}
class S3ClientSuite extends FunSpec { class S3ClientSuite extends FunSpec {
describe("testObjectHead") { describe("objectHead") {
def invoke(self: S3Client) = { def invoke(self: S3Client) = {
self.objectHead("bucket", "remoteKey").unsafeRunSync() self.objectHead("bucket", "remoteKey").unsafeRunSync()
} }
@ -17,29 +20,44 @@ class S3ClientSuite extends FunSpec {
describe("when underlying client response is okay") { describe("when underlying client response is okay") {
val expectedHash = "hash" val expectedHash = "hash"
val expectedLastModified = Instant.now val expectedLastModified = Instant.now
val underlyingClient = new S3CatsIOClient with JavaClientWrapper { val s3Client = new ThropS3Client(new S3CatsIOClient with JavaClientWrapper {
override def headObject(headObjectRequest: HeadObjectRequest) = override def headObject(headObjectRequest: HeadObjectRequest) =
IO(HeadObjectResponse.builder(). IO(HeadObjectResponse.builder().
eTag(expectedHash). eTag(expectedHash).
lastModified(expectedLastModified). lastModified(expectedLastModified).
build()) build())
} })
val s3Client = new ThropS3Client(underlyingClient)
it("should return Some(expected values)") { it("should return Some(expected values)") {
assertResult(Some((expectedHash, expectedLastModified)))(invoke(s3Client)) assertResult(Some((expectedHash, expectedLastModified)))(invoke(s3Client))
} }
} }
describe("when underlying client throws NoSuchKeyException") { describe("when underlying client throws NoSuchKeyException") {
val underlyingClient = new S3CatsIOClient with JavaClientWrapper { val s3Client = new ThropS3Client(new S3CatsIOClient with JavaClientWrapper {
override def headObject(headObjectRequest: HeadObjectRequest): IO[HeadObjectResponse] = override def headObject(headObjectRequest: HeadObjectRequest) =
IO(throw NoSuchKeyException.builder().build()) IO(throw NoSuchKeyException.builder().build())
} })
val s3Client = new ThropS3Client(underlyingClient)
it("should return None") { it("should return None") {
assertResult(None)(invoke(s3Client)) assertResult(None)(invoke(s3Client))
} }
} }
} }
describe("upload") {
describe("when uploading a file") {
val s3Client = new ThropS3Client(
new S3CatsIOClient with JavaClientWrapper {
override def putObject(putObjectRequest: PutObjectRequest, requestBody: RB) =
IO(PutObjectResponse.builder().build())
})
val localFile: LocalFile = new File("/some/file")
val bucket: Bucket = "a-bucket"
val remoteKey: RemoteKey = "prefix/file"
it("should not throw any exceptions") {
s3Client.upload(localFile, bucket, remoteKey)
}
}
}
} }