44
55import com .azure .storage .blob .BlobClient ;
66import com .azure .storage .blob .BlobContainerClient ;
7+ import com .azure .storage .blob .models .BlobDownloadHeaders ;
8+ import com .azure .storage .blob .models .BlobDownloadResponse ;
79import com .azure .storage .blob .models .BlobHttpHeaders ;
8- import com .azure .storage .blob .models .BlobProperties ;
910import com .azure .storage .blob .models .BlobStorageException ;
1011
1112import org .junit .jupiter .api .BeforeEach ;
@@ -121,15 +122,7 @@ void download_uncompressedBlob_returnsOriginalPayload() {
121122 String expectedPayload = "Hello, uncompressed world!" ;
122123 byte [] payloadBytes = expectedPayload .getBytes (StandardCharsets .UTF_8 );
123124
124- doAnswer (inv -> {
125- OutputStream out = inv .getArgument (0 );
126- out .write (payloadBytes );
127- return null ;
128- }).when (mockBlobClient ).downloadStream (any (OutputStream .class ));
129-
130- BlobProperties properties = mock (BlobProperties .class );
131- when (properties .getContentEncoding ()).thenReturn (null );
132- when (mockBlobClient .getProperties ()).thenReturn (properties );
125+ mockDownloadResponse (payloadBytes , null );
133126
134127 BlobPayloadStore store = new BlobPayloadStore (mockContainerClient , options );
135128 String token = BlobPayloadStore .encodeToken ("durabletask-payloads" , "testblob" );
@@ -150,15 +143,7 @@ void download_gzipCompressedBlob_decompressesCorrectly() throws Exception {
150143 }
151144 byte [] compressedBytes = compressedBuffer .toByteArray ();
152145
153- doAnswer (inv -> {
154- OutputStream out = inv .getArgument (0 );
155- out .write (compressedBytes );
156- return null ;
157- }).when (mockBlobClient ).downloadStream (any (OutputStream .class ));
158-
159- BlobProperties properties = mock (BlobProperties .class );
160- when (properties .getContentEncoding ()).thenReturn ("gzip" );
161- when (mockBlobClient .getProperties ()).thenReturn (properties );
146+ mockDownloadResponse (compressedBytes , "gzip" );
162147
163148 BlobPayloadStore store = new BlobPayloadStore (mockContainerClient , options );
164149 String token = BlobPayloadStore .encodeToken ("durabletask-payloads" , "compressed-blob" );
@@ -172,7 +157,8 @@ void download_gzipCompressedBlob_decompressesCorrectly() throws Exception {
172157 void download_blobNotFound_throwsPayloadStorageException () {
173158 BlobStorageException notFound = mock (BlobStorageException .class );
174159 when (notFound .getStatusCode ()).thenReturn (404 );
175- doThrow (notFound ).when (mockBlobClient ).downloadStream (any (OutputStream .class ));
160+ doThrow (notFound ).when (mockBlobClient ).downloadStreamWithResponse (
161+ any (OutputStream .class ), isNull (), isNull (), isNull (), eq (false ), isNull (), any ());
176162
177163 BlobPayloadStore store = new BlobPayloadStore (mockContainerClient , options );
178164 String token = BlobPayloadStore .encodeToken ("durabletask-payloads" , "missing-blob" );
@@ -194,7 +180,8 @@ void download_nonMatchingContainer_throwsIllegalArgument() {
194180 void download_blobStorageError_throwsPayloadStorageException () {
195181 BlobStorageException serverError = mock (BlobStorageException .class );
196182 when (serverError .getStatusCode ()).thenReturn (500 );
197- doThrow (serverError ).when (mockBlobClient ).downloadStream (any (OutputStream .class ));
183+ doThrow (serverError ).when (mockBlobClient ).downloadStreamWithResponse (
184+ any (OutputStream .class ), isNull (), isNull (), isNull (), eq (false ), isNull (), any ());
198185
199186 BlobPayloadStore store = new BlobPayloadStore (mockContainerClient , options );
200187 String token = BlobPayloadStore .encodeToken ("durabletask-payloads" , "error-blob" );
@@ -228,15 +215,7 @@ void upload_download_roundTrip_withCompression() throws Exception {
228215 String token = store .upload (originalPayload );
229216
230217 // Now set up download to return the captured bytes
231- doAnswer (inv -> {
232- OutputStream out = inv .getArgument (0 );
233- out .write (capturedBytes [0 ]);
234- return null ;
235- }).when (mockBlobClient ).downloadStream (any (OutputStream .class ));
236-
237- BlobProperties properties = mock (BlobProperties .class );
238- when (properties .getContentEncoding ()).thenReturn ("gzip" );
239- when (mockBlobClient .getProperties ()).thenReturn (properties );
218+ mockDownloadResponse (capturedBytes [0 ], "gzip" );
240219
241220 // Need to map the token's blob name back to our mock
242221 String [] decoded = BlobPayloadStore .decodeToken (token );
@@ -269,15 +248,7 @@ void upload_download_roundTrip_withoutCompression() throws Exception {
269248 String token = store .upload (originalPayload );
270249
271250 // Set up download
272- doAnswer (inv -> {
273- OutputStream out = inv .getArgument (0 );
274- out .write (capturedBytes [0 ]);
275- return null ;
276- }).when (mockBlobClient ).downloadStream (any (OutputStream .class ));
277-
278- BlobProperties properties = mock (BlobProperties .class );
279- when (properties .getContentEncoding ()).thenReturn (null );
280- when (mockBlobClient .getProperties ()).thenReturn (properties );
251+ mockDownloadResponse (capturedBytes [0 ], null );
281252
282253 String [] decoded = BlobPayloadStore .decodeToken (token );
283254 when (mockContainerClient .getBlobClient (decoded [1 ])).thenReturn (mockBlobClient );
@@ -340,4 +311,25 @@ void upload_compressionProducesGzipHeaders() {
340311 BlobHttpHeaders capturedHeaders = headersCaptor .getValue ();
341312 assertEquals ("gzip" , capturedHeaders .getContentEncoding ());
342313 }
314+
315+ // ==================== Test helpers ====================
316+
317+ /**
318+ * Sets up the mockBlobClient to return a downloadStreamWithResponse that writes
319+ * the given bytes and returns headers with the specified content-encoding.
320+ */
321+ private void mockDownloadResponse (byte [] content , String contentEncoding ) {
322+ BlobDownloadHeaders downloadHeaders = mock (BlobDownloadHeaders .class );
323+ when (downloadHeaders .getContentEncoding ()).thenReturn (contentEncoding );
324+
325+ BlobDownloadResponse downloadResponse = mock (BlobDownloadResponse .class );
326+ when (downloadResponse .getDeserializedHeaders ()).thenReturn (downloadHeaders );
327+
328+ doAnswer (inv -> {
329+ OutputStream out = inv .getArgument (0 );
330+ out .write (content );
331+ return downloadResponse ;
332+ }).when (mockBlobClient ).downloadStreamWithResponse (
333+ any (OutputStream .class ), isNull (), isNull (), isNull (), eq (false ), isNull (), any ());
334+ }
343335}
0 commit comments