Skip to content

Commit

Permalink
unit tests for put (create) page and append block akka#3253
Browse files Browse the repository at this point in the history
  • Loading branch information
sfali committed Aug 29, 2024
1 parent 96601a8 commit a4e0687
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ object BlobService {
AzureStorageStream.putPageOrAppendBlock(
objectPath,
StorageHeaders()
.withContentLengthHeader(0L)
.withContentTypeHeader(contentType)
.withBlobTypeHeader(BlobTypeHeader.PageBlobHeader)
.withPageBlobContentLengthHeader(maxBlockSize)
Expand All @@ -145,7 +144,6 @@ object BlobService {
AzureStorageStream.putPageOrAppendBlock(
objectPath,
StorageHeaders()
.withContentLengthHeader(0L)
.withContentTypeHeader(contentType)
.withBlobTypeHeader(BlobTypeHeader.AppendBlobHeader)
.withLeaseIdHeader(leaseId)
Expand Down
41 changes: 39 additions & 2 deletions azure-storage/src/test/java/docs/javadsl/StorageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public void createContainer() throws Exception {
// header, secondly once that resolve then we get `akka.http.scaladsl.model.EntityStreamException`.
@Ignore("Fix this test case")
@Test
public void putBlob() throws Exception {
mockPutBlob();
public void putBlockBlob() throws Exception {
mockPutBlockBlob();

//#put-block-blob
final Source<Optional<ObjectMetadata>, NotUsed> source =
Expand All @@ -94,6 +94,43 @@ public void putBlob() throws Exception {
Assert.assertTrue(optionalObjectMetadata.isPresent());
}

@Test
public void putPageBlob() throws Exception {
mockPutPageBlob();

//#put-page-blob
final Source<Optional<ObjectMetadata>, NotUsed> source =
BlobService.putPageBlock(containerName() + "/" + blobName(),
ContentTypes.TEXT_PLAIN_UTF8,
512L,
Optional.of(0),
Optional.empty());

final CompletionStage<Optional<ObjectMetadata>> optionalCompletionStage = source.runWith(Sink.head(), system);
//#put-page-blob

final var optionalObjectMetadata = optionalCompletionStage.toCompletableFuture().get();
Assert.assertTrue(optionalObjectMetadata.isPresent());
}

@Test
public void putAppendBlob() throws Exception {
mockPutAppendBlob();

//#put-append-blob
final Source<Optional<ObjectMetadata>, NotUsed> source =
BlobService.putAppendBlock(containerName() + "/" + blobName(),
ContentTypes.TEXT_PLAIN_UTF8,
Optional.empty());

final CompletionStage<Optional<ObjectMetadata>> optionalCompletionStage = source.runWith(Sink.head(), system);
//#put-append-blob

final var optionalObjectMetadata = optionalCompletionStage.toCompletableFuture().get();
Assert.assertTrue(optionalObjectMetadata.isPresent());
}


@Test
public void getBlob() throws Exception {
mockGetBlob();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ abstract class StorageWireMockBase(_system: ActorSystem, val _wireMockServer: Wi
)
)

protected def mockPutBlob(): StubMapping =
protected def mockPutBlockBlob(): StubMapping =
mock.register(
put(urlEqualTo(s"/$AccountName/$containerName/$blobName"))
.withHeader(BlobTypeHeaderKey, equalTo(BlockBlobType))
.withRequestBody(equalTo(payload))
.willReturn(
aResponse()
Expand All @@ -67,6 +68,34 @@ abstract class StorageWireMockBase(_system: ActorSystem, val _wireMockServer: Wi
)
)

protected def mockPutPageBlob(): StubMapping =
mock.register(
put(urlEqualTo(s"/$AccountName/$containerName/$blobName"))
.withHeader(BlobTypeHeaderKey, equalTo(PageBlobType))
.withHeader(PageBlobContentLengthHeaderKey, equalTo("512"))
.withHeader(PageBlobSequenceNumberHeaderKey, equalTo("0"))
.willReturn(
aResponse()
.withStatus(201)
.withHeader(ETag.name, ETagValue)
.withHeader(`Content-Length`.name, "0")
.withHeader(`Content-Type`.name, "text/plain; charset=UTF-8")
)
)

protected def mockPutAppendBlob(): StubMapping =
mock.register(
put(urlEqualTo(s"/$AccountName/$containerName/$blobName"))
.withHeader(BlobTypeHeaderKey, equalTo(AppendBlobType))
.willReturn(
aResponse()
.withStatus(201)
.withHeader(ETag.name, ETagValue)
.withHeader(`Content-Length`.name, "0")
.withHeader(`Content-Type`.name, "text/plain; charset=UTF-8")
)
)

protected def mockGetBlob(): StubMapping =
mock.register(
get(urlEqualTo(s"/$AccountName/$containerName/$blobName"))
Expand Down
58 changes: 56 additions & 2 deletions azure-storage/src/test/scala/docs/scaladsl/StorageSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ class StorageSpec
// TODO: There are couple of issues, firstly there are two `Content-Length` headers being added, one by `putBlob`
// function and secondly by, most likely, by WireMock. Need to to figure out how to tell WireMock not to add `Content-Length`
// header, secondly once that resolve then we get `akka.http.scaladsl.model.EntityStreamException`.
"put blob" ignore {
mockPutBlob()
"put block blob" ignore {
mockPutBlockBlob()

//#put-block-blob
import akka.stream.alpakka.azure.storage.scaladsl.BlobService
Expand All @@ -82,6 +82,60 @@ class StorageSpec
objectMetadata.eTag shouldBe Some(ETagRawValue)
}

// TODO: There are couple of issues, firstly there are two `Content-Length` headers being added, one by `putBlob`
// function and secondly by, most likely, by WireMock. Need to to figure out how to tell WireMock not to add `Content-Length`
// header, secondly once that resolve then we get `akka.http.scaladsl.model.EntityStreamException`.
"put page blob" in {
mockPutPageBlob()

//#put-page-blob
import akka.stream.alpakka.azure.storage.scaladsl.BlobService
import akka.stream.alpakka.azure.storage.ObjectMetadata

val source: Source[Option[ObjectMetadata], NotUsed] =
BlobService.putPageBlock(
objectPath = s"$containerName/$blobName",
contentType = ContentTypes.`text/plain(UTF-8)`,
maxBlockSize = 512L,
blobSequenceNumber = Some(0)
)

val eventualMaybeMetadata: Future[Option[ObjectMetadata]] = source.runWith(Sink.head)
//#put-page-blob

val maybeObjectMetadata = eventualMaybeMetadata.futureValue
maybeObjectMetadata shouldBe defined
val objectMetadata = maybeObjectMetadata.get
objectMetadata.contentLength shouldBe 0L
objectMetadata.eTag shouldBe Some(ETagRawValue)
}

// TODO: There are couple of issues, firstly there are two `Content-Length` headers being added, one by `putBlob`
// function and secondly by, most likely, by WireMock. Need to to figure out how to tell WireMock not to add `Content-Length`
// header, secondly once that resolve then we get `akka.http.scaladsl.model.EntityStreamException`.
"put append blob" in {
mockPutAppendBlob()

//#put-append-blob
import akka.stream.alpakka.azure.storage.scaladsl.BlobService
import akka.stream.alpakka.azure.storage.ObjectMetadata

val source: Source[Option[ObjectMetadata], NotUsed] =
BlobService.putAppendBlock(
objectPath = s"$containerName/$blobName",
contentType = ContentTypes.`text/plain(UTF-8)`
)

val eventualMaybeMetadata: Future[Option[ObjectMetadata]] = source.runWith(Sink.head)
//#put-append-blob

val maybeObjectMetadata = eventualMaybeMetadata.futureValue
maybeObjectMetadata shouldBe defined
val objectMetadata = maybeObjectMetadata.get
objectMetadata.contentLength shouldBe 0L
objectMetadata.eTag shouldBe Some(ETagRawValue)
}

"get blob" in {
mockGetBlob()

Expand Down

0 comments on commit a4e0687

Please sign in to comment.