From 4225e409ce932ea539ad2fd5773ea43ecbd3add3 Mon Sep 17 00:00:00 2001 From: Thomas Ploch Date: Sun, 15 Jan 2023 16:45:59 +0100 Subject: [PATCH] [TEST] `getObjectMetadata` should also refine `FileNotFoundException` While working on a use-case in which we first check for object existence via `getObjectMetadata` before we use `putObject` in order to save bandwidth, I realized that when using the test stub the tests were failing with an `FileNotFoundException`. This was due to the fact that `getObjectMetadata` does not refine the `FileNotFoundException` like `getObject` does. This PR adds refinement of these exceptions to `getObjectMetadata` analogous to `getObject`. --- src/main/scala/zio/s3/Test.scala | 15 ++++++++------- src/test/scala/zio/s3/S3Test.scala | 7 +++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main/scala/zio/s3/Test.scala b/src/main/scala/zio/s3/Test.scala index 2b9830aa..c79d89d0 100644 --- a/src/main/scala/zio/s3/Test.scala +++ b/src/main/scala/zio/s3/Test.scala @@ -16,8 +16,8 @@ package zio.s3 -import java.io.FileInputStream -import java.nio.file.StandardOpenOption +import java.io.{ FileInputStream, FileNotFoundException } +import java.nio.file.{ NoSuchFileException, StandardOpenOption } import java.nio.file.attribute.BasicFileAttributes import java.util.UUID import java.util.concurrent.CompletableFuture @@ -30,14 +30,12 @@ import zio.nio.file.Files import zio.s3.S3Bucket._ import zio.stream.{ Stream, ZStream } -import java.io.FileNotFoundException - /** * Stub Service which is back by a filesystem storage */ object Test { - private def fileNotFound(err: FileNotFoundException): S3Exception = + private def fileNotFound(err: Throwable): S3Exception = S3Exception .builder() .message("Key does not exist.") @@ -81,7 +79,7 @@ object Test { .scoped(ZIO.fromAutoCloseable(ZIO.attempt(new FileInputStream((path / bucketName / key).toFile)))) .flatMap(ZStream.fromInputStream(_, 2048)) .refineOrDie { - case e: FileNotFoundException => fileNotFound(e) + case e @ (_: FileNotFoundException | _: NoSuchFileException) => fileNotFound(e) } override def getObjectMetadata(bucketName: String, key: String): IO[S3Exception, ObjectMetadata] = @@ -91,7 +89,10 @@ object Test { file <- Files .readAttributes[BasicFileAttributes](path / bucketName / key) .map(p => ObjectMetadata(metadata, contentType, p.size())) - } yield file).orDie + } yield file) + .refineOrDie { + case e @ (_: FileNotFoundException | _: NoSuchFileException) => fileNotFound(e) + } override def listObjects( bucketName: String, diff --git a/src/test/scala/zio/s3/S3Test.scala b/src/test/scala/zio/s3/S3Test.scala index e82a47d4..776be7e0 100644 --- a/src/test/scala/zio/s3/S3Test.scala +++ b/src/test/scala/zio/s3/S3Test.scala @@ -191,6 +191,13 @@ object S3Suite { .fold(ex => ex.statusCode() == 404, _ => false) } yield assertTrue(succeed) }, + test("get object metadata - invalid identifier") { + for { + succeed <- getObjectMetadata(bucketName, UUID.randomUUID().toString) + .refineToOrDie[S3Exception] + .fold(ex => ex.statusCode() == 404, _ => false) + } yield assertTrue(succeed) + }, test("get nextObjects") { for { token <- listObjects(bucketName, ListObjectOptions.fromMaxKeys(1)).map(_.nextContinuationToken)