@@ -1559,6 +1559,129 @@ public void testFile(TestConfig.TestFile file, BucketManager bucketManager) thro
15591559 });
15601560 }
15611561
1562+ @ Test
1563+ public void testBatchRestoreArchive () throws Exception {
1564+
1565+ testFileWithHandler (new TestFileHandler () {
1566+ @ Override
1567+ public void testFile (TestConfig .TestFile file , BucketManager bucketManager ) throws IOException {
1568+ String bucket = file .getBucketName ();
1569+ String key = file .getKey ();
1570+ String keyToTestPrefix = "batch_restore_archive" ;
1571+
1572+ BucketManager .BatchOperations deleteOps = new BucketManager .BatchOperations ();
1573+ try {
1574+ List <String > keys = new ArrayList <>();
1575+ for (int i = 0 ; i < 5 ; i ++) {
1576+ keys .add (keyToTestPrefix + "_" + i );
1577+ }
1578+
1579+ for (String keyToTest : keys ) {
1580+ // if stat, delete
1581+ try {
1582+ Response resp = bucketManager .statResponse (bucket , keyToTest );
1583+ if (resp .statusCode == 200 ) bucketManager .delete (bucket , keyToTest );
1584+ } catch (QiniuException ex ) {
1585+ System .out .println ("file " + keyToTest + " not exists, ok." );
1586+ }
1587+ }
1588+
1589+ // copy and changeType to Archive
1590+ BucketManager .BatchOperations copyOps = new BucketManager .BatchOperations ();
1591+ BucketManager .BatchOperations copyAfterArchiveOps = new BucketManager .BatchOperations ();
1592+ BucketManager .BatchOperations changeTypeOps = new BucketManager .BatchOperations ();
1593+ BucketManager .BatchOperations restoreArchiveOps = new BucketManager .BatchOperations ();
1594+ for (String keyToTest : keys ) {
1595+ copyOps .addCopyOp (bucket , key , bucket , keyToTest );
1596+ copyAfterArchiveOps .addCopyOp (bucket , keyToTest , bucket , keyToTest + "_copy" );
1597+ deleteOps .addDeleteOp (bucket , keyToTest , keyToTest + "_copy" );
1598+ changeTypeOps .addChangeTypeOps (bucket , StorageType .Archive , keyToTest );
1599+ restoreArchiveOps .addRestoreArchiveOps (bucket , 1 , keyToTest );
1600+ }
1601+ Response copyResponse = bucketManager .batch (copyOps );
1602+ Assert .assertEquals (200 , copyResponse .statusCode );
1603+
1604+ Response changeTypeResponse = bucketManager .batch (changeTypeOps );
1605+ Assert .assertEquals (200 , changeTypeResponse .statusCode );
1606+
1607+ // 验证归档不可 copy
1608+ try {
1609+ Response copyAfterArchiveResponse = bucketManager .batch (copyAfterArchiveOps );
1610+ String bodyString = copyAfterArchiveResponse .bodyString ();
1611+ Assert .assertNotEquals (200 , "batch copy can't be success" + bodyString );
1612+ } catch (QiniuException ex ) {
1613+ Assert .assertEquals (400 , ex .response .statusCode );
1614+ System .out .println (ex .response .bodyString ());
1615+ }
1616+
1617+ // restoreArchive
1618+ Response restoreResponse = bucketManager .batch (restoreArchiveOps );
1619+ String bodyString = restoreResponse .bodyString ();
1620+ System .out .println (bodyString );
1621+ Assert .assertEquals (200 , restoreResponse .statusCode );
1622+
1623+ //test for 400 Bad Request {"error":"invalid freeze after days"}
1624+ try {
1625+ restoreResponse = bucketManager .batch (restoreArchiveOps );
1626+ bodyString = restoreResponse .bodyString ();
1627+ System .out .println (bodyString );
1628+ } catch (QiniuException ex ) {
1629+ Assert .assertEquals (400 , ex .response .statusCode );
1630+ System .out .println (ex .response .bodyString ());
1631+ }
1632+
1633+ long checkStart = new Date ().getTime ();
1634+ boolean shouldCheck = true ;
1635+ boolean success = false ;
1636+ while (shouldCheck ) {
1637+ // 验证解归档可 copy
1638+ try {
1639+ Response copyAfterArchiveResponse = bucketManager .batch (copyAfterArchiveOps );
1640+ bodyString = copyAfterArchiveResponse .bodyString ();
1641+ System .out .println (bodyString );
1642+ // 可以 copy 但文件已存在
1643+ if (bodyString .contains ("\" code\" :614" )) {
1644+ success = true ;
1645+ break ;
1646+ }
1647+ } catch (QiniuException ex ) {
1648+ System .out .println (ex .response .bodyString ());
1649+ if (ex .response .statusCode == 400 ) {
1650+ success = true ;
1651+ break ;
1652+ }
1653+ }
1654+
1655+ long current = new Date ().getTime ();
1656+ if (current - checkStart > 1000 * 60 * 5.5 ) {
1657+ shouldCheck = false ;
1658+ }
1659+
1660+ try {
1661+ Thread .sleep (1000 * 10 );
1662+ } catch (InterruptedException e ) {
1663+ e .printStackTrace ();
1664+ }
1665+ }
1666+
1667+ Assert .assertTrue ("can copy after restore archive" , success );
1668+ } catch (QiniuException e ) {
1669+ Assert .fail (bucket + ":" + key + " > " + keyToTestPrefix + " >> " + e .response .toString ());
1670+ } finally {
1671+ try {
1672+ Response response = bucketManager .batch (deleteOps );
1673+ String bodyString = response .bodyString ();
1674+ System .out .println (bodyString );
1675+ Assert .assertTrue (bodyString , response .statusCode == 200 || response .statusCode == 298 );
1676+ } catch (QiniuException ex ) {
1677+ Assert .assertEquals (400 , ex .response .statusCode );
1678+ System .out .println (ex .response .bodyString ());
1679+ }
1680+ }
1681+ }
1682+ });
1683+ }
1684+
15621685 /**
15631686 * 测试noIndexPage
15641687 *
0 commit comments