use of com.github.ambry.rest.RestResponseChannel in project ambry by linkedin.
the class GetSignedUrlHandlerTest method verifySigningUrl.
// handleGoodCaseTest() helpers
/**
* Verifies that a singed URL is returned and it matches what is expected
* @param restRequest the {@link RestRequest} to get a signed URL.
* @param urlExpected the URL that should be returned.
* @param expectedAccount the {@link Account} that should be populated in {@link RestRequest}.
* @param expectedContainer the {@link Container} that should be populated in {@link RestRequest}.
* @throws Exception
*/
private void verifySigningUrl(RestRequest restRequest, String urlExpected, Account expectedAccount, Container expectedContainer) throws Exception {
RestResponseChannel restResponseChannel = new MockRestResponseChannel();
sendRequestGetResponse(restRequest, restResponseChannel);
Assert.assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
assertEquals("Content-length is not as expected", 0, Integer.parseInt((String) restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
assertEquals("Signed URL is not as expected", urlExpected, restResponseChannel.getHeader(RestUtils.Headers.SIGNED_URL));
assertEquals("Account not as expected", expectedAccount, restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_ACCOUNT_KEY));
assertEquals("Container not as expected", expectedContainer, restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_CONTAINER_KEY));
}
use of com.github.ambry.rest.RestResponseChannel in project ambry by linkedin.
the class SimpleAmbryCostModelPolicyTest method testCalculateRequestCost.
@Test
public void testCalculateRequestCost() throws Exception {
SimpleAmbryCostModelPolicy requestCostPolicy = new SimpleAmbryCostModelPolicy();
RestResponseChannel restResponseChannel = mock(RestResponseChannel.class);
when(restResponseChannel.getHeader(anyString())).thenReturn(0);
String blobUri = "/AAYIAQSSAAgAAQAAAAAAABpFymbGwe7sRBWYa5OPlkcNHQ.bin";
// test for a 4 MB GET request.
BlobInfo blobInfo = getBlobInfo(4 * MB);
RestRequest restRequest = createMockRequestWithMethod(RestMethod.GET, blobUri, -1);
Map<String, Double> costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
verifyReadCost(costMap, 1);
// test for a small GET request (fractional CU).
blobInfo = getBlobInfo(6 * MB);
costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
verifyReadCost(costMap, 2);
// test for a GET request of blob of size 0.
blobInfo = getBlobInfo(0);
restRequest = createMockRequestWithMethod(RestMethod.GET, blobUri, -1);
costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
verifyReadCost(costMap, 1);
// test for a small POST request (fractional storage cost).
blobInfo = getBlobInfo(8 * MB);
restRequest = createMockRequestWithMethod(RestMethod.POST, blobUri, 8 * MB);
costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
verifyWriteCost(costMap, 2, 8 * 1024 * 1024 / (double) QuotaUtils.BYTES_IN_GB);
// test for a large POST request.
blobInfo = getBlobInfo(4 * GB);
restRequest = createMockRequestWithMethod(RestMethod.POST, blobUri, 4 * GB);
costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
verifyWriteCost(costMap, 1024, 4);
// test for a POST request of blob of size 0.
blobInfo = getBlobInfo(0);
restRequest = createMockRequestWithMethod(RestMethod.POST, blobUri, 0);
costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
verifyWriteCost(costMap, 1, 0);
// test for a HEAD request.
restRequest = createMockRequestWithMethod(RestMethod.HEAD, blobUri, -1);
costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
verifyReadCost(costMap, 1);
// test for a DELETE request.
restRequest = createMockRequestWithMethod(RestMethod.DELETE, blobUri, -1);
costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
verifyWriteCost(costMap, 1, 0.0);
// test for a PUT request.
restRequest = createMockRequestWithMethod(RestMethod.PUT, blobUri, -1);
costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
verifyWriteCost(costMap, 1, 0.0);
// test for PUT with null blob info.
costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, null);
verifyWriteCost(costMap, 1, 0.0);
// test BlobInfo and UserMetadata GET requests
blobInfo = getBlobInfo(40 * GB);
restRequest = createMockRequestWithMethod(RestMethod.GET, blobUri + "/BlobInfo", -1);
costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
verifyReadCost(costMap, 1);
restRequest = createMockRequestWithMethod(RestMethod.GET, blobUri + "/UserMetadata", -1);
costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
verifyReadCost(costMap, 1);
// Plain GET should use blob size
restRequest = createMockRequestWithMethod(RestMethod.GET, blobUri, -1);
costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
verifyReadCost(costMap, 10240);
// TODO add a range request case with large range
}
use of com.github.ambry.rest.RestResponseChannel in project ambry by linkedin.
the class SimpleRequestQuotaCostPolicyTest method testCalculateRequestQuotaCharge.
@Test
public void testCalculateRequestQuotaCharge() throws Exception {
QuotaConfig quotaConfig = new QuotaConfig(new VerifiableProperties(new Properties()));
SimpleRequestQuotaCostPolicy quotaRequestCostPolicy = new SimpleRequestQuotaCostPolicy(quotaConfig);
RestResponseChannel restResponseChannel = mock(RestResponseChannel.class);
when(restResponseChannel.getHeader(anyString())).thenReturn(0);
String blobUri = "/AAYIAQSSAAgAAQAAAAAAABpFymbGwe7sRBWYa5OPlkcNHQ.bin";
// test for a 4 MB GET request.
long blobSize = 4 * MB;
RestRequest restRequest = createMockRequestWithMethod(RestMethod.GET, blobUri, -1);
Map<String, Double> costMap = quotaRequestCostPolicy.calculateRequestQuotaCharge(restRequest, blobSize);
verifyReadCost(costMap, Math.ceil(blobSize / (double) quotaConfig.quotaAccountingUnit));
// test for a small GET request (fractional CU).
blobSize = 6 * MB;
costMap = quotaRequestCostPolicy.calculateRequestQuotaCharge(restRequest, blobSize);
verifyReadCost(costMap, Math.ceil(blobSize / (double) quotaConfig.quotaAccountingUnit));
// test for a GET request of blob of size 0.
restRequest = createMockRequestWithMethod(RestMethod.GET, blobUri, -1);
costMap = quotaRequestCostPolicy.calculateRequestQuotaCharge(restRequest, 0);
verifyReadCost(costMap, 1);
// test for a GET request of blob of size 512.
blobSize = 512;
restRequest = createMockRequestWithMethod(RestMethod.GET, blobUri, -1);
costMap = quotaRequestCostPolicy.calculateRequestQuotaCharge(restRequest, blobSize);
verifyReadCost(costMap, 1);
// test for a small POST request (fractional storage cost).
blobSize = 8 * MB;
restRequest = createMockRequestWithMethod(RestMethod.POST, blobUri, blobSize);
costMap = quotaRequestCostPolicy.calculateRequestQuotaCharge(restRequest, blobSize);
verifyWriteCost(costMap, Math.ceil(blobSize / (double) quotaConfig.quotaAccountingUnit), 8 * 1024 * 1024 / (double) QuotaUtils.BYTES_IN_GB);
// test for a large POST request.
blobSize = 4 * QuotaUtils.BYTES_IN_GB;
restRequest = createMockRequestWithMethod(RestMethod.POST, blobUri, blobSize);
costMap = quotaRequestCostPolicy.calculateRequestQuotaCharge(restRequest, blobSize);
verifyWriteCost(costMap, Math.ceil(blobSize / (double) quotaConfig.quotaAccountingUnit), 4);
// test for a POST request of blob of size 0.
restRequest = createMockRequestWithMethod(RestMethod.POST, blobUri, 0);
costMap = quotaRequestCostPolicy.calculateRequestQuotaCharge(restRequest, 0);
verifyWriteCost(costMap, 1, 0);
// test for a POST request of blob of size 512.
restRequest = createMockRequestWithMethod(RestMethod.POST, blobUri, 0);
costMap = quotaRequestCostPolicy.calculateRequestQuotaCharge(restRequest, 512);
verifyWriteCost(costMap, 1, 0);
// test for a HEAD request.
restRequest = createMockRequestWithMethod(RestMethod.HEAD, blobUri, -1);
costMap = quotaRequestCostPolicy.calculateRequestQuotaCharge(restRequest, quotaConfig.quotaAccountingUnit);
verifyReadCost(costMap, 1);
// test for a DELETE request.
restRequest = createMockRequestWithMethod(RestMethod.DELETE, blobUri, -1);
costMap = quotaRequestCostPolicy.calculateRequestQuotaCharge(restRequest, quotaConfig.quotaAccountingUnit);
verifyWriteCost(costMap, 1, 0.0);
// test for a PUT request.
restRequest = createMockRequestWithMethod(RestMethod.PUT, blobUri, -1);
costMap = quotaRequestCostPolicy.calculateRequestQuotaCharge(restRequest, quotaConfig.quotaAccountingUnit);
verifyWriteCost(costMap, 1, 0.0);
}
use of com.github.ambry.rest.RestResponseChannel in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method doConditionalDeleteTest.
/**
* Tests blob conditional DELETE operations on the given {@code container}.
* @param toPostAccount the {@link Account} to use in post headers. Can be {@code null} if only using service ID.
* @param toPostContainer the {@link Container} to use in post headers. Can be {@code null} if only using service ID.
* @param serviceId the serviceId to use for the POST
* @param isPrivate the isPrivate flag to pass as part of the POST
* @param expectedAccount the {@link Account} details that are eventually expected to be populated.
* @param expectedContainer the {@link Container} details that are eventually expected to be populated.
* @throws Exception
*/
private void doConditionalDeleteTest(Account toPostAccount, Container toPostContainer, String serviceId, boolean isPrivate, Account expectedAccount, Container expectedContainer) throws Exception {
final int CONTENT_LENGTH = 1024;
ByteBuffer content = ByteBuffer.wrap(TestUtils.getRandomBytes(CONTENT_LENGTH));
String contentType = "application/octet-stream";
String ownerId = "postGetHeadDeleteOwnerID";
JSONObject headers = new JSONObject();
String accountNameInPost = toPostAccount != null ? toPostAccount.getName() : null;
String containerNameInPost = toPostContainer != null ? toPostContainer.getName() : null;
setAmbryHeadersForPut(headers, 7200, isPrivate, serviceId, contentType, ownerId, accountNameInPost, containerNameInPost);
Map<String, String> userMetadata = new HashMap<>();
userMetadata.put(RestUtils.Headers.USER_META_DATA_HEADER_PREFIX + "key1", "value1");
userMetadata.put(RestUtils.Headers.USER_META_DATA_HEADER_PREFIX + "key2", "value2");
RestUtilsTest.setUserMetadataHeaders(headers, userMetadata);
// perform POST, GET, HEAD successfully before DELETE
String blobId = postBlobAndVerify(headers, content, expectedAccount, expectedContainer);
headers.put(RestUtils.Headers.BLOB_SIZE, (long) CONTENT_LENGTH);
getBlobAndVerify(blobId, null, null, headers, content, expectedAccount, expectedContainer);
getHeadAndVerify(blobId, null, null, headers, expectedAccount, expectedContainer);
// test Conditional Delete failure because only container name is set
RestResponseChannel restResponseChannel = new MockRestResponseChannel();
JSONObject headers2 = new JSONObject();
setAccountAndContainerHeaders(headers2, null, containerNameInPost);
RestRequest restRequest = createRestRequest(RestMethod.DELETE, blobId, headers2, null);
try {
doOperation(restRequest, restResponseChannel);
fail("Operation should have failed because only container name is set");
} catch (RestServiceException e) {
assertEquals("AmbryBlobStorageService should have thrown a BadRequest exception", RestServiceErrorCode.BadRequest, e.getErrorCode());
}
// test Conditional Delete failure because of incorrect account name
restResponseChannel = new MockRestResponseChannel();
setAccountAndContainerHeaders(headers, "INCORRECT_ACCOUNT_NAME", containerNameInPost);
restRequest = createRestRequest(RestMethod.DELETE, blobId, headers, null);
try {
doOperation(restRequest, restResponseChannel);
fail("Operation should have failed because incorrect account name");
} catch (RestServiceException e) {
assertEquals("AmbryBlobStorageService should have thrown a PreconditionFailed exception", RestServiceErrorCode.PreconditionFailed, e.getErrorCode());
}
// test Conditional Delete failure because of incorrect container name
restResponseChannel = new MockRestResponseChannel();
setAccountAndContainerHeaders(headers, accountNameInPost, "INCORRECT_CONTAINER_NAME");
restRequest = createRestRequest(RestMethod.DELETE, blobId, headers, null);
try {
doOperation(restRequest, restResponseChannel);
fail("Operation should have failed because incorrect container name");
} catch (RestServiceException e) {
assertEquals("AmbryBlobStorageService should have thrown a PreconditionFailed exception", RestServiceErrorCode.PreconditionFailed, e.getErrorCode());
}
// test Conditional Delete succeeds
setAccountAndContainerHeaders(headers, accountNameInPost, containerNameInPost);
restRequest = createRestRequest(RestMethod.DELETE, blobId, headers, null);
verifyDeleteAccepted(restRequest);
// check GET, HEAD and DELETE after delete.
verifyOperationsAfterDelete(blobId, headers, content, expectedAccount, expectedContainer);
}
use of com.github.ambry.rest.RestResponseChannel in project ambry by linkedin.
the class AmbrySecurityServiceTest method testExceptionCasesProcessResponse.
/**
* Tests exception cases for
* {@link SecurityService#processResponse(RestRequest, RestResponseChannel, BlobInfo, Callback)}
* @param restMethod the {@link RestMethod} of the request to be made
* @param restResponseChannel the {@link RestResponseChannel} to write responses over.
* @param blobInfo the {@link BlobInfo} to be used for the {@link RestRequest}
* @param expectedErrorCode the {@link RestServiceErrorCode} expected in the exception returned.
* @throws Exception
*/
private void testExceptionCasesProcessResponse(RestMethod restMethod, RestResponseChannel restResponseChannel, BlobInfo blobInfo, RestServiceErrorCode expectedErrorCode) throws Exception {
ThrowingConsumer<ExecutionException> errorAction = e -> {
Assert.assertTrue("Exception should have been an instance of RestServiceException", e.getCause() instanceof RestServiceException);
RestServiceException re = (RestServiceException) e.getCause();
Assert.assertEquals("Unexpected RestServerErrorCode (Future)", expectedErrorCode, re.getErrorCode());
};
RestRequest restRequest = createRestRequest(restMethod, "/", null);
TestUtils.assertException(ExecutionException.class, () -> securityService.processResponse(restRequest, restResponseChannel, blobInfo).get(), errorAction);
}
Aggregations