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
`NoSuchFileException`.

This was due to the fact that `getObjectMetadata` does not refine the
`NoSuchFileException` like `getObject` does with `FileNotFoundException`.

This commit adds refinement of these exceptions to `getObjectMetadata`
analogous to `getObject`. The `fileNotFound` function needs to be adapted
to `Throwable` because `java.nio.*` and `java.io.*` do not share an error
hierarchy.
  • Loading branch information
tPl0ch committed Jan 16, 2023
1 parent 83dc194 commit 393d1ab
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
13 changes: 7 additions & 6 deletions zio-s3/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 @@ -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: NoSuchFileException => fileNotFound(e)
}

override def listObjects(
bucketName: String,
Expand Down
7 changes: 7 additions & 0 deletions zio-s3/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 393d1ab

Please sign in to comment.