44package com .azure .storage .blob .specialized .cryptography ;
55
66import com .azure .core .client .traits .HttpTrait ;
7+ import com .azure .core .credential .TokenCredential ;
78import com .azure .core .cryptography .AsyncKeyEncryptionKey ;
89import com .azure .core .cryptography .AsyncKeyEncryptionKeyResolver ;
910import com .azure .core .http .HttpClient ;
1011import com .azure .core .http .HttpHeaderName ;
1112import com .azure .core .http .HttpHeaders ;
13+ import com .azure .core .http .HttpPipeline ;
14+ import com .azure .core .http .HttpPipelineBuilder ;
1215import com .azure .core .http .HttpResponse ;
16+ import com .azure .core .http .policy .BearerTokenAuthenticationPolicy ;
17+ import com .azure .core .http .policy .ExponentialBackoff ;
1318import com .azure .core .http .policy .HttpPipelinePolicy ;
19+ import com .azure .core .http .policy .HttpPolicyProviders ;
20+ import com .azure .core .http .policy .RetryPolicy ;
21+ import com .azure .core .http .policy .RetryStrategy ;
22+ import com .azure .core .http .policy .UserAgentPolicy ;
1423import com .azure .core .http .rest .Response ;
1524import com .azure .core .test .TestMode ;
1625import com .azure .core .test .TestProxyTestBase ;
1726import com .azure .core .test .models .CustomMatcher ;
1827import com .azure .core .test .models .TestProxySanitizer ;
1928import com .azure .core .test .models .TestProxySanitizerType ;
29+ import com .azure .core .util .Configuration ;
2030import com .azure .core .util .CoreUtils ;
31+ import com .azure .security .keyvault .keys .KeyServiceVersion ;
2132import com .azure .security .keyvault .keys .cryptography .models .KeyWrapAlgorithm ;
2233import com .azure .storage .blob .BlobAsyncClient ;
2334import com .azure .storage .blob .BlobClient ;
4758import java .nio .charset .StandardCharsets ;
4859import java .time .Duration ;
4960import java .time .OffsetDateTime ;
61+ import java .util .ArrayList ;
5062import java .util .Arrays ;
5163import java .util .Collections ;
64+ import java .util .List ;
5265import java .util .Random ;
5366import java .util .concurrent .ThreadLocalRandom ;
67+ import java .util .stream .Collectors ;
5468
5569import static com .azure .core .test .utils .TestUtils .assertArraysEqual ;
5670import static com .azure .core .test .utils .TestUtils .assertByteBuffersEqual ;
@@ -246,10 +260,16 @@ protected String setupBlobMatchCondition(EncryptedBlobClient ebbc, String match)
246260 : match ;
247261 }
248262
249- protected String setupBlobMatchCondition (EncryptedBlobAsyncClient ebbac , String match ) {
263+ protected Mono < String > setupBlobMatchCondition (EncryptedBlobAsyncClient ebbac , String match ) {
250264 return RECEIVED_ETAG .equals (match )
251- ? ebbac .getPropertiesWithResponse (null ).block ().getHeaders ().getValue (HttpHeaderName .ETAG )
252- : match ;
265+ ? ebbac .getProperties ().map (BlobProperties ::getETag )
266+ : Mono .justOrEmpty (match ).defaultIfEmpty ("null" );
267+ }
268+
269+ protected static List <String > convertNulls (String ... conditions ) {
270+ return Arrays .stream (conditions )
271+ .map (condition -> "null" .equals (condition ) ? null : condition )
272+ .collect (Collectors .toList ());
253273 }
254274
255275 /**
@@ -272,17 +292,21 @@ protected String setupBlobLeaseCondition(BlobClient bc, String leaseID) {
272292 return RECEIVED_LEASE_ID .equals (leaseID ) ? responseLeaseId : leaseID ;
273293 }
274294
275- protected String setupBlobLeaseCondition (BlobAsyncClient bac , String leaseID ) {
276- String responseLeaseId = null ;
295+ protected Mono < String > setupBlobLeaseCondition (BlobAsyncClient bac , String leaseID ) {
296+ Mono < String > responseLeaseId = null ;
277297 if (RECEIVED_LEASE_ID .equals (leaseID ) || GARBAGE_LEASE_ID .equals (leaseID )) {
278298 responseLeaseId = new BlobLeaseClientBuilder ()
279299 .blobAsyncClient (bac )
280300 .buildAsyncClient ()
281- .acquireLease (-1 )
282- .block ();
301+ .acquireLease (-1 );
283302 }
284303
285- return RECEIVED_LEASE_ID .equals (leaseID ) ? responseLeaseId : leaseID ;
304+ if (responseLeaseId == null ) {
305+ return Mono .justOrEmpty (leaseID ).defaultIfEmpty ("null" );
306+ }
307+
308+ return responseLeaseId .map (returnedLeaseId -> RECEIVED_LEASE_ID .equals (leaseID )
309+ ? returnedLeaseId : (leaseID == null ? "null" : leaseID ));
286310 }
287311
288312 protected static BlobLeaseClient createLeaseClient (BlobClient blobClient ) {
@@ -421,4 +445,42 @@ protected <T extends HttpTrait<T>, E extends Enum<E>> T instrument(T builder) {
421445 protected HttpClient getHttpClient () {
422446 return StorageCommonTestUtils .getHttpClient (interceptorManager );
423447 }
448+
449+ protected HttpPipeline getHttpPipeline (KeyServiceVersion serviceVersion ) {
450+ Configuration global = Configuration .getGlobalConfiguration ().clone ();
451+ TokenCredential credential ;
452+
453+ credential = StorageCommonTestUtils .getTokenCredential (interceptorManager );
454+
455+ // Closest to API goes first, closest to wire goes last.
456+ final List <HttpPipelinePolicy > policies = new ArrayList <>();
457+ policies .add (new UserAgentPolicy ("client_name" , "client_version" , global , serviceVersion ));
458+ HttpPolicyProviders .addBeforeRetryPolicies (policies );
459+ RetryStrategy strategy = new ExponentialBackoff (5 , Duration .ofSeconds (2 ), Duration .ofSeconds (16 ));
460+ policies .add (new RetryPolicy (strategy ));
461+ policies .add (new BearerTokenAuthenticationPolicy (credential , "https://vault.azure.net/.default" ));
462+ HttpPolicyProviders .addAfterRetryPolicies (policies );
463+
464+ if (getTestMode () == TestMode .RECORD ) {
465+ policies .add (interceptorManager .getRecordPolicy ());
466+ }
467+
468+ return new HttpPipelineBuilder ()
469+ .policies (policies .toArray (new HttpPipelinePolicy [0 ]))
470+ .httpClient (getHttpClient ())
471+ .build ();
472+ }
473+
474+ protected static void compareListToBuffer (List <ByteBuffer > buffers , ByteBuffer result ) {
475+ result .position (0 );
476+
477+ for (ByteBuffer buffer : buffers ) {
478+ buffer .position (0 );
479+ result .limit (result .position () + buffer .remaining ());
480+ assertByteBuffersEqual (buffer , result );
481+ result .position (result .position () + buffer .remaining ());
482+ }
483+
484+ assertEquals (0 , result .remaining ());
485+ }
424486}
0 commit comments