Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
odisseus committed Nov 8, 2024
1 parent accd916 commit 5b67887
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ class S3Upload(private val s3: S3, private val timeout: Duration = Duration(5, "
val failedUploads = uploadFiles(plan.getNewEntries.asScala)
val failureSummary = new StringBuilder()
if (failedRemoves.nonEmpty) {
failureSummary.append(s"Failed to remove ${failedRemoves.size} entries: \n")
failureSummary.append(s"Failed to remove ${failedRemoves.size} entries:\n")
failedRemoves.foreach { case (entry, e) =>
failureSummary.append(s" $entry: \n ${e.getMessage}\n")
failureSummary.append(s" $entry:\n ${e.getMessage}\n")
}
}
if (failedUploads.nonEmpty) {
failureSummary.append(s"Failed to upload ${failedUploads.size} entries: \n")
failureSummary.append(s"Failed to upload ${failedUploads.size} entries:\n")
failedUploads.foreach { case (entry, e) =>
failureSummary.append(s" $entry: \n ${e.getMessage}\n")
failureSummary.append(s" $entry:\n ${e.getMessage}\n")
}
}
if (failureSummary.nonEmpty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import org.scalamock.scalatest.MockFactory
import org.scalatest.funsuite.AnyFunSuite
import software.amazon.awssdk.core.sync.RequestBody
import software.amazon.awssdk.services.s3.S3Client
import software.amazon.awssdk.services.s3.model.{S3Object, PutObjectRequest, DeleteObjectRequest}
import software.amazon.awssdk.services.s3.model.{DeleteObjectRequest, PutObjectRequest, S3Object}

import java.time.Instant
import scala.jdk.CollectionConverters._
import scala.util.Try

class JdkIndexesS3OperationsTest extends AnyFunSuite with MockFactory {

Expand Down Expand Up @@ -46,4 +47,44 @@ class JdkIndexesS3OperationsTest extends AnyFunSuite with MockFactory {

}

test("single server error") {
// Given
val newEntries = Seq(
"shared-index-jdk.metadata.json" -> "application/json",
"shared-index-jdk.ijx.xz" -> "application/xz",
"shared-index-jdk.sha256" -> "application/octet-stream"
).map { case (key, contentType) =>
new CdnUploadDataEntry(key, contentType, () => Array.emptyByteArray)
}.asJava
val removeEntries = Seq().asJava
val updatePlan = new CdnUpdatePlan(newEntries, removeEntries)
val s3Upload = new S3Upload(s3)
(s3Client.putObject(_: PutObjectRequest, _: RequestBody)).when(*, *).throws(new Exception("Server error")).once()

// When
val exception = Try {
s3Upload.updateS3Indexes(updatePlan)
}.failed.get

// Then
Seq(
"shared-index-jdk.metadata.json" -> "application/json",
"shared-index-jdk.ijx.xz" -> "application/xz",
"shared-index-jdk.sha256" -> "application/octet-stream"
).foreach { case (key, contentType) =>
(s3Client.putObject(_: PutObjectRequest, _: RequestBody)).verify(where {
(req: PutObjectRequest, _: RequestBody) =>
req.contentType() == contentType &&
req.key() == "/path/to/indexes/" + key
})
}
val expectedErrorMessage =
"""Failed to upload 1 entries:
| UploadDataEntry(key=shared-index-jdk.metadata.json, type=application/json):
| Server error
|""".stripMargin

assert(exception.getMessage == expectedErrorMessage)
}

}

0 comments on commit 5b67887

Please sign in to comment.