@@ -397,6 +397,154 @@ private void testAnalyticsRetentionPolicies(ServiceClient client, ServicePropert
397397 assertServicePropertiesAreEqual (props , callDownloadServiceProperties (client ));
398398 }
399399
400+ /**
401+ * Test delete retention policy for blobs
402+ *
403+ * @throws StorageException
404+ * @throws InterruptedException
405+ */
406+ @ Test
407+ public void testValidDeleteRetentionPolicy () throws StorageException , InterruptedException {
408+ ServiceClient client = TestHelper .createCloudBlobClient ();
409+
410+ // average setting
411+ testValidDeleteRetentionPolicy (client , true , 5 );
412+
413+ // minimum setting
414+ testValidDeleteRetentionPolicy (client , true , 1 );
415+
416+ // maximum setting
417+ testValidDeleteRetentionPolicy (client , true , 365 );
418+
419+ // disable setting
420+ testValidDeleteRetentionPolicy (client , false , 5 );
421+ }
422+
423+ private void testValidDeleteRetentionPolicy (ServiceClient client , boolean enabled ,
424+ Integer interval )
425+ throws StorageException , InterruptedException {
426+
427+ try {
428+ ServiceProperties expectedServiceProperties = new ServiceProperties ();
429+ expectedServiceProperties .setDefaultServiceVersion (Constants .HeaderConstants .TARGET_STORAGE_VERSION );
430+
431+ if (enabled ) {
432+ expectedServiceProperties .getDeleteRetentionPolicy ().setEnabled (true );
433+ expectedServiceProperties .getDeleteRetentionPolicy ().setRetentionIntervalInDays (interval );
434+ callUploadServiceProps (client , expectedServiceProperties , null );
435+ } else {
436+ // interval and retained versions per blob would both be ignored by the service in case the policy is not enabled
437+ ServiceProperties propertiesToUpload = new ServiceProperties ();
438+ propertiesToUpload .getDeleteRetentionPolicy ().setEnabled (false );
439+ propertiesToUpload .getDeleteRetentionPolicy ().setRetentionIntervalInDays (interval );
440+
441+ expectedServiceProperties .getDeleteRetentionPolicy ().setEnabled (false );
442+ callUploadServiceProps (client , propertiesToUpload , null );
443+ }
444+
445+ // verify
446+ assertServicePropertiesAreEqual (expectedServiceProperties , callDownloadServiceProperties (client ));
447+ }
448+ finally {
449+ // reset the delete retention policy
450+ ServiceProperties disabledDeleteRetentionPolicy = new ServiceProperties ();
451+ disabledDeleteRetentionPolicy .getDeleteRetentionPolicy ().setEnabled (false );
452+ callUploadServiceProps (client , disabledDeleteRetentionPolicy , null );
453+ }
454+ }
455+
456+ /**
457+ * Test invalid delete retention policy for blobs
458+ *
459+ * @throws StorageException
460+ * @throws InterruptedException
461+ */
462+ @ Test
463+ public void testInvalidDeleteRetentionPolicy () throws StorageException , InterruptedException {
464+ ServiceClient client = TestHelper .createCloudBlobClient ();
465+
466+ // Should not work with 0 days
467+ testInvalidDeleteRetentionPolicy (client , true , 0 );
468+
469+ // Should not work with <0 days
470+ testInvalidDeleteRetentionPolicy (client , true , -1 );
471+
472+ // Should not work with 366 days
473+ testInvalidDeleteRetentionPolicy (client , true , 366 );
474+
475+
476+ // Should not work with interval as null
477+ testInvalidDeleteRetentionPolicy (client , true , null );
478+ }
479+
480+ private void testInvalidDeleteRetentionPolicy (ServiceClient client , boolean enabled ,
481+ Integer interval )
482+ throws StorageException , InterruptedException {
483+
484+ // Arrange
485+ ServiceProperties serviceProperties = new ServiceProperties ();
486+ serviceProperties .setDefaultServiceVersion (Constants .HeaderConstants .TARGET_STORAGE_VERSION );
487+
488+ if (enabled ) {
489+ serviceProperties .getDeleteRetentionPolicy ().setEnabled (true );
490+ }
491+ else {
492+ serviceProperties .getDeleteRetentionPolicy ().setEnabled (false );
493+ }
494+ serviceProperties .getDeleteRetentionPolicy ().setRetentionIntervalInDays (interval );
495+
496+ // Failure is expected since the retention policy is invalid
497+ try {
498+ callUploadServiceProps (client , serviceProperties , null );
499+ fail ("No exception received. An invalid delete retention policy should have raised an exception." );
500+ }
501+ catch (StorageException e ) {
502+ assertEquals (e .errorCode , "InvalidXmlDocument" );
503+ }
504+ catch (IllegalArgumentException e ) {
505+ assertTrue (e .getMessage ().contains ("argument must not be null" ));
506+ }
507+ catch (Exception e ) {
508+ fail ("Invalid exception " + e .getClass () + " received when expecting StorageException" );
509+ }
510+ }
511+
512+ /**
513+ * Test empty delete retention policy for blobs
514+ *
515+ * @throws StorageException
516+ * @throws InterruptedException
517+ */
518+ @ Test
519+ public void testEmptyDeleteRetentionPolicy () throws StorageException , InterruptedException {
520+ ServiceClient client = TestHelper .createCloudBlobClient ();
521+
522+ try {
523+ // set up initial delete retention policy
524+ ServiceProperties currentServiceProperties = new ServiceProperties ();
525+ currentServiceProperties .setDefaultServiceVersion (Constants .HeaderConstants .TARGET_STORAGE_VERSION );
526+ currentServiceProperties .getDeleteRetentionPolicy ().setEnabled (true );
527+ currentServiceProperties .getDeleteRetentionPolicy ().setRetentionIntervalInDays (5 );
528+ callUploadServiceProps (client , currentServiceProperties , null );
529+
530+ // verify
531+ assertServicePropertiesAreEqual (currentServiceProperties , callDownloadServiceProperties (client ));
532+
533+ // try to upload empty retention policy
534+ ServiceProperties emptyServiceProperties = new ServiceProperties ();
535+ callUploadServiceProps (client , emptyServiceProperties , null );
536+
537+ // verify
538+ assertServicePropertiesAreEqual (currentServiceProperties , callDownloadServiceProperties (client ));
539+ }
540+ finally {
541+ // reset the delete retention policy
542+ ServiceProperties disabledDeleteRetentionPolicy = new ServiceProperties ();
543+ disabledDeleteRetentionPolicy .getDeleteRetentionPolicy ().setEnabled (false );
544+ callUploadServiceProps (client , disabledDeleteRetentionPolicy , null );
545+ }
546+ }
547+
400548 /**
401549 * Test CORS with different rules.
402550 *
@@ -974,6 +1122,15 @@ private static void assertServicePropertiesAreEqual(ServiceProperties propsA, Se
9741122 assertNull (propsA .getCors ());
9751123 assertNull (propsB .getCors ());
9761124 }
1125+
1126+ if (propsA .getDeleteRetentionPolicy () != null && propsB .getDeleteRetentionPolicy () != null ) {
1127+ assertEquals (propsA .getDeleteRetentionPolicy ().getEnabled (), propsB .getDeleteRetentionPolicy ().getEnabled ());
1128+ assertEquals (propsA .getDeleteRetentionPolicy ().getRetentionIntervalInDays (), propsB .getDeleteRetentionPolicy ().getRetentionIntervalInDays ());
1129+ }
1130+ else {
1131+ assertNull (propsA .getDeleteRetentionPolicy ());
1132+ assertNull (propsB .getDeleteRetentionPolicy ());
1133+ }
9771134 }
9781135
9791136 /**
0 commit comments