diff --git a/src/main/java/org/twonote/rgwadmin4j/impl/RgwAdminImpl.java b/src/main/java/org/twonote/rgwadmin4j/impl/RgwAdminImpl.java index 92ebf19..a9890a6 100644 --- a/src/main/java/org/twonote/rgwadmin4j/impl/RgwAdminImpl.java +++ b/src/main/java/org/twonote/rgwadmin4j/impl/RgwAdminImpl.java @@ -587,6 +587,25 @@ public Optional getBucketInfo(String bucketName) { throw new RuntimeException("Server should not return more than one bucket"); } + private String call(Request request) { + try (Response response = client.newCall(request).execute()) { + if (response.code() == 404) { + throw new RgwAdminException(404, "not found"); + } + if (!response.isSuccessful()) { + throw ErrorUtils.parseError(response); + } + ResponseBody body = response.body(); + if (body != null) { + return response.body().string(); + } else { + return null; + } + } catch (IOException e) { + throw new RgwAdminException(500, "IOException", e); + } + } + /** * Guarantee that the request is execute success and the connection is closed * @@ -801,7 +820,7 @@ public void setIndividualBucketQuota(String userId, String bucket, long maxObjec .url(urlBuilder.build()) .build(); - safeCall(request); + call(request); } @Override @@ -829,7 +848,7 @@ public void setUserQuota(String userId, String quotaType, long maxObjects, long .url(urlBuilder.build()) .build(); - safeCall(request); + call(request); } private String buildQuotaConfig(long maxObjects, long maxSizeKB) { diff --git a/src/test/java/org/twonote/rgwadmin4j/impl/RgwAdminImplTest.java b/src/test/java/org/twonote/rgwadmin4j/impl/RgwAdminImplTest.java index e1f3f04..a9ee86b 100644 --- a/src/test/java/org/twonote/rgwadmin4j/impl/RgwAdminImplTest.java +++ b/src/test/java/org/twonote/rgwadmin4j/impl/RgwAdminImplTest.java @@ -18,6 +18,8 @@ import java.util.Optional; import java.util.UUID; import java.util.stream.Collectors; + +import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; import org.twonote.rgwadmin4j.model.BucketInfo; @@ -97,8 +99,7 @@ public void createS3Credential() { String secretKey = v.getUserId() + "-adminSecretKey"; response = RGW_ADMIN.createS3Credential(v.getUserId(), accessKey, secretKey); assertTrue( - response - .stream() + response.stream() .anyMatch( v1 -> accessKey.equals(v1.getAccessKey()) @@ -112,7 +113,7 @@ public void createS3Credential() { assertTrue( "InvalidArgument".equals(e.getMessage()) // kraken || "NoSuchUser".equals(e.getMessage()) // luminous - ); + ); } }); } @@ -146,7 +147,7 @@ public void removeS3Credential() { "InvalidArgument".equals(e.getMessage()) || // kraken "NoSuchUser".equals(e.getMessage()) // luminous - ); + ); } }); } @@ -170,8 +171,7 @@ public void createS3CredentialForSubUser() { response = RGW_ADMIN.createS3CredentialForSubUser(userId, subUserId, accessKey, secretKey); assertTrue( - response - .stream() + response.stream() .anyMatch( v1 -> absSubUserId.equals(v1.getUserId()) @@ -200,11 +200,7 @@ public void removeS3CredentialFromSubUser() { // basic RGW_ADMIN.removeS3CredentialFromSubUser(userId, subUserId, keyToDelete.getAccessKey()); assertFalse( - RGW_ADMIN - .getUserInfo(userId) - .get() - .getS3Credentials() - .stream() + RGW_ADMIN.getUserInfo(userId).get().getS3Credentials().stream() .anyMatch( k -> keyToDelete @@ -267,11 +263,7 @@ public void removeSwiftCredentialFromSubUser() { // basic RGW_ADMIN.removeSwiftCredentialFromSubUser(userId, subUserId); assertFalse( - RGW_ADMIN - .getUserInfo(userId) - .get() - .getSwiftCredentials() - .stream() + RGW_ADMIN.getUserInfo(userId).get().getSwiftCredentials().stream() .anyMatch( k -> absSubUserId.equals( @@ -281,8 +273,7 @@ public void removeSwiftCredentialFromSubUser() { @Test @Ignore("See trimUsage()") - public void trimUserUsage() { - } + public void trimUserUsage() {} @Test public void trimUsage() { @@ -316,8 +307,7 @@ public void trimUsage() { @Test @Ignore("See getUsage()") - public void getUserUsage() { - } + public void getUserUsage() {} @Test public void getUsage() { @@ -404,11 +394,7 @@ public void createSubUser() { RGW_ADMIN.createSubUser(userId, subUserId, permission, CredentialType.SWIFT); assertEquals(permission, response.getPermission()); Optional keyResponse = - RGW_ADMIN - .getUserInfo(userId) - .get() - .getSwiftCredentials() - .stream() + RGW_ADMIN.getUserInfo(userId).get().getSwiftCredentials().stream() .filter(k -> absSubUserId.equals(k.getUserId())) .findFirst(); assertTrue(keyResponse.isPresent()); @@ -436,9 +422,7 @@ public void _createSubUser() { // test subuser in s3 S3Credential key = - response2 - .getS3Credentials() - .stream() + response2.getS3Credentials().stream() .filter(e -> fullSubUserId.equals(e.getUserId())) .findFirst() .get(); @@ -475,28 +459,23 @@ public void checkBucketIndex() { @Ignore("See getAndSetUserQuota()") @Test - public void getUserQuota() { - } + public void getUserQuota() {} @Ignore("See getAndSetUserQuota()") @Test - public void setUserQuota() { - } + public void setUserQuota() {} @Ignore("See getAndSetBucketQuota()") @Test - public void getBucketQuota() { - } + public void getBucketQuota() {} @Ignore("See getAndSetBucketQuota()") @Test - public void setBucketQuota() { - } + public void setBucketQuota() {} @Test @Ignore("See userCapability()") - public void addUserCapability() { - } + public void addUserCapability() {} @Test public void userCapability() { @@ -523,8 +502,7 @@ public void userCapability() { @Test @Ignore("See userCapability()") - public void removeUserCapability() { - } + public void removeUserCapability() {} @Test public void removeBucket() throws Exception { @@ -871,7 +849,7 @@ public void getAndSetUserQuota() { assertTrue( quota.getMaxSizeKb() == -1 // jewel || quota.getMaxSizeKb() == 0 // kraken - ); + ); // set quota RGW_ADMIN.setUserQuota(userId, 1, 1); @@ -890,10 +868,13 @@ public void getAndSetUserQuota() { "InvalidArgument".equals(e.getMessage()) || // kraken "NoSuchUser".equals(e.getMessage()) // luminous - ); + ); } - - RGW_ADMIN.setUserQuota(UUID.randomUUID().toString(), 1, 1); + RgwAdminException rgwAdminException = + Assert.assertThrows( + RgwAdminException.class, + () -> RGW_ADMIN.setUserQuota(UUID.randomUUID().toString(), 1, 1)); + assertEquals(rgwAdminException.status(), 404); } @Test @@ -918,7 +899,7 @@ public void getAndSetBucketQuota() { assertTrue( quota.getMaxSizeKb() == -1 // jewel || quota.getMaxSizeKb() == 0 // kraken - ); + ); // set quota RGW_ADMIN.setBucketQuota(userId, 1, 1); @@ -936,7 +917,7 @@ public void getAndSetBucketQuota() { assertTrue( quota.getMaxSizeKb() == -1 // jewel || quota.getMaxSizeKb() == 0 // kraken - ); + ); }); // user not exist @@ -948,10 +929,14 @@ public void getAndSetBucketQuota() { "InvalidArgument".equals(e.getMessage()) || // kraken "NoSuchUser".equals(e.getMessage()) // luminous - ); + ); } - RGW_ADMIN.setBucketQuota(UUID.randomUUID().toString(), 1, 1); + RgwAdminException rgwAdminException = + Assert.assertThrows( + RgwAdminException.class, + () -> RGW_ADMIN.setBucketQuota(UUID.randomUUID().toString(), 1, 1)); + assertEquals(rgwAdminException.status(), 404); } @Test @@ -976,7 +961,7 @@ public void getAndSetSpecificBucketQuota() { assertTrue( quota.getMaxSizeKb() == -1 // jewel || quota.getMaxSizeKb() == 0 // kraken - ); + ); // set quota RGW_ADMIN.setIndividualBucketQuota(userId, bucketName, 1, 1); @@ -988,7 +973,7 @@ public void getAndSetSpecificBucketQuota() { assertTrue( quota.getMaxSizeKb() == -1 // jewel || quota.getMaxSizeKb() == 0 // kraken - ); + ); // not shown by getBucketInfo() quota = RGW_ADMIN.getBucketInfo(bucketName).get().getBucketQuota();