4
4
package com .azure .storage .blob .specialized .cryptography ;
5
5
6
6
import com .azure .core .client .traits .HttpTrait ;
7
+ import com .azure .core .credential .TokenCredential ;
7
8
import com .azure .core .cryptography .AsyncKeyEncryptionKey ;
8
9
import com .azure .core .cryptography .AsyncKeyEncryptionKeyResolver ;
9
10
import com .azure .core .http .HttpClient ;
10
11
import com .azure .core .http .HttpHeaderName ;
11
12
import com .azure .core .http .HttpHeaders ;
13
+ import com .azure .core .http .HttpPipeline ;
14
+ import com .azure .core .http .HttpPipelineBuilder ;
12
15
import com .azure .core .http .HttpResponse ;
16
+ import com .azure .core .http .policy .BearerTokenAuthenticationPolicy ;
17
+ import com .azure .core .http .policy .ExponentialBackoff ;
13
18
import 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 ;
14
23
import com .azure .core .http .rest .Response ;
15
24
import com .azure .core .test .TestMode ;
16
25
import com .azure .core .test .TestProxyTestBase ;
17
26
import com .azure .core .test .models .CustomMatcher ;
18
27
import com .azure .core .test .models .TestProxySanitizer ;
19
28
import com .azure .core .test .models .TestProxySanitizerType ;
29
+ import com .azure .core .util .Configuration ;
20
30
import com .azure .core .util .CoreUtils ;
31
+ import com .azure .security .keyvault .keys .KeyServiceVersion ;
21
32
import com .azure .security .keyvault .keys .cryptography .models .KeyWrapAlgorithm ;
22
33
import com .azure .storage .blob .BlobAsyncClient ;
23
34
import com .azure .storage .blob .BlobClient ;
47
58
import java .nio .charset .StandardCharsets ;
48
59
import java .time .Duration ;
49
60
import java .time .OffsetDateTime ;
61
+ import java .util .ArrayList ;
50
62
import java .util .Arrays ;
51
63
import java .util .Collections ;
64
+ import java .util .List ;
52
65
import java .util .Random ;
53
66
import java .util .concurrent .ThreadLocalRandom ;
67
+ import java .util .stream .Collectors ;
54
68
55
69
import static com .azure .core .test .utils .TestUtils .assertArraysEqual ;
56
70
import static com .azure .core .test .utils .TestUtils .assertByteBuffersEqual ;
@@ -246,10 +260,16 @@ protected String setupBlobMatchCondition(EncryptedBlobClient ebbc, String match)
246
260
: match ;
247
261
}
248
262
249
- protected String setupBlobMatchCondition (EncryptedBlobAsyncClient ebbac , String match ) {
263
+ protected Mono < String > setupBlobMatchCondition (EncryptedBlobAsyncClient ebbac , String match ) {
250
264
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 ());
253
273
}
254
274
255
275
/**
@@ -272,17 +292,21 @@ protected String setupBlobLeaseCondition(BlobClient bc, String leaseID) {
272
292
return RECEIVED_LEASE_ID .equals (leaseID ) ? responseLeaseId : leaseID ;
273
293
}
274
294
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 ;
277
297
if (RECEIVED_LEASE_ID .equals (leaseID ) || GARBAGE_LEASE_ID .equals (leaseID )) {
278
298
responseLeaseId = new BlobLeaseClientBuilder ()
279
299
.blobAsyncClient (bac )
280
300
.buildAsyncClient ()
281
- .acquireLease (-1 )
282
- .block ();
301
+ .acquireLease (-1 );
283
302
}
284
303
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 ));
286
310
}
287
311
288
312
protected static BlobLeaseClient createLeaseClient (BlobClient blobClient ) {
@@ -421,4 +445,42 @@ protected <T extends HttpTrait<T>, E extends Enum<E>> T instrument(T builder) {
421
445
protected HttpClient getHttpClient () {
422
446
return StorageCommonTestUtils .getHttpClient (interceptorManager );
423
447
}
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
+ }
424
486
}
0 commit comments