Skip to content

Commit

Permalink
Add more test akka#3253
Browse files Browse the repository at this point in the history
  • Loading branch information
sfali committed Sep 7, 2024
1 parent 1cbb77e commit da8d82f
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 9 deletions.
11 changes: 6 additions & 5 deletions azure-storage/src/test/java/docs/javadsl/StorageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import scala.Option;

import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -128,7 +129,7 @@ public void putAppendBlob() throws Exception {
//#put-append-blob
final Source<Optional<ObjectMetadata>, NotUsed> source =
BlobService.putAppendBlock(containerName() + "/" + blobName(),
PutAppendBlock.create(ContentTypes.TEXT_PLAIN_UTF8));
PutAppendBlock.create(ContentTypes.TEXT_PLAIN_UTF8));

final CompletionStage<Optional<ObjectMetadata>> optionalCompletionStage = source.runWith(Sink.head(), system);
//#put-append-blob
Expand All @@ -139,7 +140,7 @@ public void putAppendBlob() throws Exception {

@Test
public void getBlob() throws Exception {
mockGetBlob();
mockGetBlob(Option.empty(), Option.empty());

//#get-blob
final Source<ByteString, CompletionStage<ObjectMetadata>> source =
Expand Down Expand Up @@ -213,7 +214,7 @@ public void createFile() throws Exception {
//#create-file
final Source<Optional<ObjectMetadata>, NotUsed> source =
FileService.createFile(containerName() + "/" + blobName(),
CreateFile.create(contentLength(), ContentTypes.TEXT_PLAIN_UTF8));
CreateFile.create(contentLength(), ContentTypes.TEXT_PLAIN_UTF8));

final CompletionStage<Optional<ObjectMetadata>> optionalCompletionStage = source.runWith(Sink.head(), system);
//#create-file
Expand All @@ -237,7 +238,7 @@ public void updateRange() throws Exception {
final Source<Optional<ObjectMetadata>, NotUsed> source =
FileService.updateRange(containerName() + "/" + blobName(),
UpdateFileRange.create(contentRange(), ContentTypes.TEXT_PLAIN_UTF8),
Source.single(ByteString.fromString(payload())));
Source.single(ByteString.fromString(payload())));

final CompletionStage<Optional<ObjectMetadata>> optionalCompletionStage = source.runWith(Sink.head(), system);
//#update-range
Expand All @@ -251,7 +252,7 @@ public void updateRange() throws Exception {

@Test
public void getFile() throws Exception {
mockGetBlob();
mockGetBlob(Option.empty(), Option.empty());

//#get-file
final Source<ByteString, CompletionStage<ObjectMetadata>> source =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import com.github.tomakehurst.wiremock.http.Request
import com.github.tomakehurst.wiremock.stubbing.{ServeEvent, StubMapping}
import com.typesafe.config.{Config, ConfigFactory}

import scala.util.Try

abstract class StorageWireMockBase(_system: ActorSystem, val _wireMockServer: WireMockServer) extends TestKit(_system) {

private val port = _wireMockServer.port()
Expand Down Expand Up @@ -102,9 +104,25 @@ abstract class StorageWireMockBase(_system: ActorSystem, val _wireMockServer: Wi
)
)

protected def mockGetBlob(): StubMapping =
protected def mockGetBlob(versionId: Option[String] = None, leaseId: Option[String] = None): StubMapping =
mock.register(
get(urlEqualTo(s"/$AccountName/$containerName/$blobName"))
get(urlPathEqualTo(s"/$AccountName/$containerName/$blobName"))
.withQueryParam("versionId", toStringValuePattern(versionId))
.withHeader(LeaseIdHeaderKey, toStringValuePattern(leaseId))
.willReturn(
aResponse()
.withStatus(200)
.withHeader(ETag.name, ETagValue)
.withBody(payload)
)
)

protected def mockGetBlobWithServerSideEncryption(): StubMapping =
mock.register(
get(urlPathEqualTo(s"/$AccountName/$containerName/$blobName"))
.withHeader("x-ms-encryption-algorithm", equalTo("AES256"))
.withHeader("x-ms-encryption-key", equalTo("SGVsbG9Xb3JsZA=="))
.withHeader("x-ms-encryption-key-sha256", equalTo("hy5OUM6ZkNiwQTMMR8nd0Rvsa1A66ThqmdqFhOm7EsQ="))
.willReturn(
aResponse()
.withStatus(200)
Expand Down Expand Up @@ -188,6 +206,19 @@ abstract class StorageWireMockBase(_system: ActorSystem, val _wireMockServer: Wi
)
)

protected def mock404s(): StubMapping =
mock.register(
any(anyUrl())
.willReturn(aResponse().withStatus(404).withBody("""
|<Error>
| <Code>ResourceNotFound</Code>
| <Message>The specified resource doesn't exist.</Message>
|</Error>
|""".stripMargin))
)

private def toStringValuePattern(maybeValue: Option[String]) = maybeValue.map(equalTo).getOrElse(absent())

private def stopWireMockServer(): Unit = _wireMockServer.stop()
}

Expand Down Expand Up @@ -234,7 +265,7 @@ object StorageWireMockBase {
val headerName = `Content-Length`.name

val updatedRequest =
Option(request.header(headerName)).map(_.firstValue()) match {
Try(request.getHeader(headerName)).toOption match {
case Some(contentLengthValue) =>
RequestWrapper
.create()
Expand Down
68 changes: 67 additions & 1 deletion azure-storage/src/test/scala/docs/scaladsl/StorageSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
package docs.scaladsl

import akka.NotUsed
import akka.http.scaladsl.model.ContentTypes
import akka.http.scaladsl.model.{ContentTypes, StatusCodes}
import akka.stream.alpakka.azure.storage.StorageException
import akka.stream.alpakka.azure.storage.headers.ServerSideEncryption
import akka.stream.alpakka.azure.storage.requests._
import akka.stream.alpakka.azure.storage.scaladsl.StorageWireMockBase
import akka.stream.alpakka.azure.storage.scaladsl.StorageWireMockBase.ETagRawValue
Expand Down Expand Up @@ -149,6 +151,70 @@ class StorageSpec
eventualText.futureValue.map(_.utf8String).mkString("") shouldBe payload
}

"get blob with versionId" in {
mockGetBlob(Some("versionId"))

import akka.stream.alpakka.azure.storage.scaladsl.BlobService
import akka.stream.alpakka.azure.storage.ObjectMetadata

val source: Source[ByteString, Future[ObjectMetadata]] =
BlobService.getBlob(objectPath = s"$containerName/$blobName", GetBlob().withVersionId("versionId"))

val eventualText = source.toMat(Sink.seq)(Keep.right).run()

eventualText.futureValue.map(_.utf8String).mkString("") shouldBe payload
}

"get blob with optional header" in {
mockGetBlob(leaseId = Some("leaseId"))

import akka.stream.alpakka.azure.storage.scaladsl.BlobService
import akka.stream.alpakka.azure.storage.ObjectMetadata

val source: Source[ByteString, Future[ObjectMetadata]] =
BlobService.getBlob(objectPath = s"$containerName/$blobName", GetBlob().withLeaseId("leaseId"))

val eventualText = source.toMat(Sink.seq)(Keep.right).run()

eventualText.futureValue.map(_.utf8String).mkString("") shouldBe payload
}

"get blob with ServerSideEncryption" in {
mockGetBlobWithServerSideEncryption()

import akka.stream.alpakka.azure.storage.scaladsl.BlobService
import akka.stream.alpakka.azure.storage.ObjectMetadata

val source: Source[ByteString, Future[ObjectMetadata]] =
BlobService.getBlob(objectPath = s"$containerName/$blobName",
GetBlob().withServerSideEncryption(ServerSideEncryption.customerKey("SGVsbG9Xb3JsZA==")))

val eventualText = source.toMat(Sink.seq)(Keep.right).run()

eventualText.futureValue.map(_.utf8String).mkString("") shouldBe payload
}

"get blob from non-existing container" in {
mock404s()

import akka.stream.alpakka.azure.storage.scaladsl.BlobService
import akka.stream.alpakka.azure.storage.ObjectMetadata

val source: Source[ByteString, Future[ObjectMetadata]] =
BlobService.getBlob(objectPath = s"$containerName/$blobName", GetBlob())

val eventualMetadata = source.toMat(Sink.seq)(Keep.right).run()
eventualMetadata.failed.futureValue shouldBe
StorageException(
statusCode = StatusCodes.NotFound,
errorCode = "ResourceNotFound",
errorMessage = "The specified resource doesn't exist.",
resourceName = None,
resourceValue = None,
reason = None
)
}

"get blob range" in {
mockGetBlobWithRange()

Expand Down

0 comments on commit da8d82f

Please sign in to comment.