Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix not thrown exception #50

Merged
merged 3 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/main/java/org/twonote/rgwadmin4j/impl/RgwAdminImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,25 @@ public Optional<BucketInfo> 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
*
Expand Down Expand Up @@ -801,7 +820,7 @@ public void setIndividualBucketQuota(String userId, String bucket, long maxObjec
.url(urlBuilder.build())
.build();

safeCall(request);
call(request);
}

@Override
Expand Down Expand Up @@ -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) {
Expand Down
85 changes: 35 additions & 50 deletions src/test/java/org/twonote/rgwadmin4j/impl/RgwAdminImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand All @@ -112,7 +113,7 @@ public void createS3Credential() {
assertTrue(
"InvalidArgument".equals(e.getMessage()) // kraken
|| "NoSuchUser".equals(e.getMessage()) // luminous
);
);
}
});
}
Expand Down Expand Up @@ -146,7 +147,7 @@ public void removeS3Credential() {
"InvalidArgument".equals(e.getMessage())
|| // kraken
"NoSuchUser".equals(e.getMessage()) // luminous
);
);
}
});
}
Expand All @@ -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())
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -281,8 +273,7 @@ public void removeSwiftCredentialFromSubUser() {

@Test
@Ignore("See trimUsage()")
public void trimUserUsage() {
}
public void trimUserUsage() {}

@Test
public void trimUsage() {
Expand Down Expand Up @@ -316,8 +307,7 @@ public void trimUsage() {

@Test
@Ignore("See getUsage()")
public void getUserUsage() {
}
public void getUserUsage() {}

@Test
public void getUsage() {
Expand Down Expand Up @@ -404,11 +394,7 @@ public void createSubUser() {
RGW_ADMIN.createSubUser(userId, subUserId, permission, CredentialType.SWIFT);
assertEquals(permission, response.getPermission());
Optional<SwiftCredential> 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());
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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() {
Expand All @@ -523,8 +502,7 @@ public void userCapability() {

@Test
@Ignore("See userCapability()")
public void removeUserCapability() {
}
public void removeUserCapability() {}

@Test
public void removeBucket() throws Exception {
Expand Down Expand Up @@ -871,7 +849,7 @@ public void getAndSetUserQuota() {
assertTrue(
quota.getMaxSizeKb() == -1 // jewel
|| quota.getMaxSizeKb() == 0 // kraken
);
);

// set quota
RGW_ADMIN.setUserQuota(userId, 1, 1);
Expand All @@ -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
Expand All @@ -918,7 +899,7 @@ public void getAndSetBucketQuota() {
assertTrue(
quota.getMaxSizeKb() == -1 // jewel
|| quota.getMaxSizeKb() == 0 // kraken
);
);

// set quota
RGW_ADMIN.setBucketQuota(userId, 1, 1);
Expand All @@ -936,7 +917,7 @@ public void getAndSetBucketQuota() {
assertTrue(
quota.getMaxSizeKb() == -1 // jewel
|| quota.getMaxSizeKb() == 0 // kraken
);
);
});

// user not exist
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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();
Expand Down
Loading