Search in sources :

Example 1 with DeleteBucketCnameRequest

use of com.aliyun.oss.model.DeleteBucketCnameRequest in project aliyun-oss-java-sdk by aliyun.

the class BucketCnameTest method testUnormaladdBucketCname.

@Test
public void testUnormaladdBucketCname() {
    final String bucketName = "unormal-set-bucket-cname";
    // parameter invalid
    try {
        ossClient.addBucketCname(new AddBucketCnameRequest(bucketName));
        Assert.fail("Set bucket cname should not be successful");
    } catch (Exception e) {
        Assert.assertTrue(e instanceof NullPointerException);
    }
    try {
        ossClient.deleteBucketCname(new DeleteBucketCnameRequest(bucketName));
        Assert.fail("Delete bucket cname should not be successful");
    } catch (Exception e) {
        Assert.assertTrue(e instanceof NullPointerException);
    }
    // bucket non-existent
    try {
        ossClient.addBucketCname(new AddBucketCnameRequest(bucketName).withDomain(domains[0]));
        Assert.fail("Set bucket cname should not be successful");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.NO_SUCH_BUCKET, e.getErrorCode());
    }
    try {
        ossClient.getBucketCname(bucketName);
        Assert.fail("get bucket cname should not be successful");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.NO_SUCH_BUCKET, e.getErrorCode());
    }
    try {
        ossClient.deleteBucketCname(new DeleteBucketCnameRequest(bucketName).withDomain(domains[0]));
        Assert.fail("Delete bucket cname should not be successful");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.NO_SUCH_BUCKET, e.getErrorCode());
    }
    // domain invalid
    try {
        ossClient.createBucket(bucketName);
        try {
            ossClient.addBucketCname(new AddBucketCnameRequest(bucketName).withDomain("your.com"));
            Assert.fail("Set bucket cname should not be successful");
        } catch (OSSException e) {
            Assert.assertEquals("NoSuchCnameInRecord", e.getErrorCode());
        }
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        ossClient.deleteBucket(bucketName);
    }
}
Also used : DeleteBucketCnameRequest(com.aliyun.oss.model.DeleteBucketCnameRequest) OSSException(com.aliyun.oss.OSSException) AddBucketCnameRequest(com.aliyun.oss.model.AddBucketCnameRequest) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 2 with DeleteBucketCnameRequest

use of com.aliyun.oss.model.DeleteBucketCnameRequest in project aliyun-oss-java-sdk by aliyun.

the class BucketCnameTest method testNormalAddBucketCname.

@Test
@SuppressWarnings("deprecation")
public void testNormalAddBucketCname() {
    final String bucketName = "normal-add-bucket-cname";
    Date curDate;
    try {
        ossClient.createBucket(bucketName);
        // set cname
        ossClient.addBucketCname(new AddBucketCnameRequest(bucketName).withDomain(domains[0]));
        curDate = new Date(System.currentTimeMillis());
        waitForCacheExpiration(5);
        List<CnameConfiguration> cnames = ossClient.getBucketCname(bucketName);
        Assert.assertEquals(cnames.size(), 1);
        Assert.assertEquals(cnames.get(0).getDomain(), domains[0]);
        Assert.assertEquals(cnames.get(0).getStatus(), CnameConfiguration.CnameStatus.Enabled);
        Assert.assertEquals(cnames.get(0).getLastMofiedTime().getYear(), curDate.getYear());
        Assert.assertEquals(cnames.get(0).getLastMofiedTime().getMonth(), curDate.getMonth());
        Assert.assertEquals(cnames.get(0).getLastMofiedTime().getDay(), curDate.getDay());
        System.out.println(cnames.get(0));
        ossClient.deleteBucketCname(bucketName, domains[0]);
        cnames = ossClient.getBucketCname(bucketName);
        Assert.assertEquals(cnames.size(), 0);
        // set multi cname
        for (String domain : domains) {
            AddBucketCnameRequest request = new AddBucketCnameRequest(bucketName);
            request.setDomain(domain);
            ossClient.addBucketCname(request);
        }
        curDate = new Date(System.currentTimeMillis());
        waitForCacheExpiration(5);
        cnames = ossClient.getBucketCname(bucketName);
        Assert.assertEquals(cnames.size(), domains.length);
        for (int i = 0; i < cnames.size(); i++) {
            System.out.println(cnames.get(i));
            Assert.assertEquals(cnames.get(i).getDomain(), domains[i]);
            Assert.assertEquals(cnames.get(0).getStatus(), CnameConfiguration.CnameStatus.Enabled);
            Assert.assertEquals(cnames.get(0).getLastMofiedTime().getYear(), curDate.getYear());
            Assert.assertEquals(cnames.get(0).getLastMofiedTime().getMonth(), curDate.getMonth());
            Assert.assertEquals(cnames.get(0).getLastMofiedTime().getDay(), curDate.getDay());
        }
        for (String domain : domains) {
            DeleteBucketCnameRequest req = new DeleteBucketCnameRequest(bucketName);
            req.setDomain(domain);
            ossClient.deleteBucketCname(req);
        }
        cnames = ossClient.getBucketCname(bucketName);
        Assert.assertEquals(cnames.size(), 0);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    } finally {
        ossClient.deleteBucket(bucketName);
    }
}
Also used : DeleteBucketCnameRequest(com.aliyun.oss.model.DeleteBucketCnameRequest) CnameConfiguration(com.aliyun.oss.model.CnameConfiguration) AddBucketCnameRequest(com.aliyun.oss.model.AddBucketCnameRequest) Date(java.util.Date) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Aggregations

OSSException (com.aliyun.oss.OSSException)2 AddBucketCnameRequest (com.aliyun.oss.model.AddBucketCnameRequest)2 DeleteBucketCnameRequest (com.aliyun.oss.model.DeleteBucketCnameRequest)2 Test (org.junit.Test)2 CnameConfiguration (com.aliyun.oss.model.CnameConfiguration)1 Date (java.util.Date)1