Skip to content

Commit

Permalink
[TEST] getObjectMetadata should also refine FileNotFoundException
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
tPl0ch committed Jan 15, 2023
1 parent 96d5e3c commit 4225e40
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/main/scala/zio/s3/Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.")
Expand Down Expand Up @@ -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] =
Expand All @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions src/test/scala/zio/s3/S3Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4225e40

Please sign in to comment.