use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method verifyUpdateBlobTtlResponse.
/**
* Verifies that a request returns the right response code once the blob's TTL has been updated.
* @param restRequest the {@link RestRequest} to send to {@link FrontendRestRequestService}.
* @throws Exception
*/
private void verifyUpdateBlobTtlResponse(RestRequest restRequest) throws Exception {
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
doOperation(restRequest, restResponseChannel);
assertEquals("Unexpected response status", ResponseStatus.Ok, restResponseChannel.getStatus());
assertTrue("No Date header", restResponseChannel.getHeader(RestUtils.Headers.DATE) != null);
assertEquals("Content-Length is not 0", "0", restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH));
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method getBlobAndVerify.
/**
* Gets the blob with blob ID {@code blobId} and verifies that the headers and content match with what is expected.
* @param blobId the blob ID of the blob to GET.
* @param range the optional {@link ByteRange} for the request.
* @param getOption the options to use while getting the blob.
* @param expectedHeaders the expected headers in the response.
* @param expectedContent the expected content of the blob.
* @param expectedAccount the expected account in the rest request.
* @param expectedContainer the expected container in the rest request.
* @throws Exception
*/
private void getBlobAndVerify(String blobId, ByteRange range, GetOption getOption, JSONObject expectedHeaders, ByteBuffer expectedContent, Account expectedAccount, Container expectedContainer) throws Exception {
RestRequest restRequest = createRestRequest(RestMethod.GET, blobId, createRequestHeaders(range, getOption), null);
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
doOperation(restRequest, restResponseChannel);
verifyGetBlobResponse(restRequest, restResponseChannel, range, expectedHeaders, expectedContent, expectedAccount, expectedContainer);
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method badResponseHandlerAndRestRequestTest.
/**
* Checks reactions of all methods in {@link FrontendRestRequestService} to bad {@link RestResponseHandler} and
* {@link RestRequest} implementations.
* @throws Exception
*/
@Test
public void badResponseHandlerAndRestRequestTest() throws Exception {
// What happens inside FrontendRestRequestService during this test?
// 1. Since the RestRequest throws errors, FrontendRestRequestService will attempt to submit response with exception
// to FrontendTestResponseHandler.
// 2. The submission will fail because FrontendTestResponseHandler has been shutdown.
// 3. FrontendRestRequestService will directly complete the request over the RestResponseChannel with the *original*
// exception.
// 4. It will then try to release resources but closing the RestRequest will also throw an exception. This exception
// is swallowed.
// What the test is looking for -> No exceptions thrown when the handle is run and the original exception arrives
// safely.
responseHandler.shutdown();
for (String methodName : new String[] { "handleGet", "handlePost", "handleHead", "handleDelete", "handleOptions", "handlePut" }) {
Method method = FrontendRestRequestService.class.getDeclaredMethod(methodName, RestRequest.class, RestResponseChannel.class);
responseHandler.reset();
RestRequest restRequest = new BadRestRequest();
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
method.invoke(frontendRestRequestService, restRequest, restResponseChannel);
Exception e = restResponseChannel.getException();
assertTrue("Unexpected exception", e instanceof IllegalStateException || e instanceof NullPointerException);
}
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method getAndUseSignedUrlTest.
/**
* Tests the handling of {@link Operations#GET_SIGNED_URL} requests.
* @throws Exception
*/
@Test
public void getAndUseSignedUrlTest() throws Exception {
// setup
int contentLength = 10;
ByteBuffer content = ByteBuffer.wrap(TestUtils.getRandomBytes(contentLength));
long blobTtl = 7200;
String serviceId = "getAndUseSignedUrlTest";
String contentType = "application/octet-stream";
String ownerId = "getAndUseSignedUrlTest";
JSONObject headers = new JSONObject();
headers.put(RestUtils.Headers.URL_TYPE, RestMethod.POST.name());
setAmbryHeadersForPut(headers, blobTtl, !refContainer.isCacheable(), serviceId, contentType, ownerId, refAccount.getName(), refContainer.getName(), null);
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);
// POST
// Get signed URL
RestRequest getSignedUrlRequest = createRestRequest(RestMethod.GET, Operations.GET_SIGNED_URL, headers, null);
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
doOperation(getSignedUrlRequest, restResponseChannel);
assertEquals("Account not as expected", refAccount, getSignedUrlRequest.getArgs().get(RestUtils.InternalKeys.TARGET_ACCOUNT_KEY));
assertEquals("Container not as expected", refContainer, getSignedUrlRequest.getArgs().get(RestUtils.InternalKeys.TARGET_CONTAINER_KEY));
assertEquals("Unexpected response status", ResponseStatus.Ok, restResponseChannel.getStatus());
String signedPostUrl = restResponseChannel.getHeader(RestUtils.Headers.SIGNED_URL);
assertNotNull("Did not get a signed POST URL", signedPostUrl);
// Use signed URL to POST
List<ByteBuffer> contents = new LinkedList<>();
contents.add(content);
contents.add(null);
RestRequest postSignedRequest = createRestRequest(RestMethod.POST, signedPostUrl, null, contents);
restResponseChannel = new MockRestResponseChannel();
doOperation(postSignedRequest, restResponseChannel);
String blobId = verifyPostAndReturnBlobId(postSignedRequest, restResponseChannel, refAccount, refContainer);
// verify POST
headers.put(RestUtils.Headers.BLOB_SIZE, contentLength);
getBlobAndVerify(blobId, null, null, headers, content, refAccount, refContainer);
getBlobInfoAndVerify(blobId, null, headers, refAccount, refContainer);
// GET
// Get signed URL
JSONObject getHeaders = new JSONObject();
getHeaders.put(RestUtils.Headers.URL_TYPE, RestMethod.GET.name());
blobId = blobId.startsWith("/") ? blobId.substring(1) : blobId;
getHeaders.put(RestUtils.Headers.BLOB_ID, blobId);
getSignedUrlRequest = createRestRequest(RestMethod.GET, Operations.GET_SIGNED_URL, getHeaders, null);
restResponseChannel = new MockRestResponseChannel();
doOperation(getSignedUrlRequest, restResponseChannel);
assertEquals("Account not as expected", refAccount, getSignedUrlRequest.getArgs().get(RestUtils.InternalKeys.TARGET_ACCOUNT_KEY));
assertEquals("Container not as expected", refContainer, getSignedUrlRequest.getArgs().get(RestUtils.InternalKeys.TARGET_CONTAINER_KEY));
assertEquals("Unexpected response status", ResponseStatus.Ok, restResponseChannel.getStatus());
String signedGetUrl = restResponseChannel.getHeader(RestUtils.Headers.SIGNED_URL);
assertNotNull("Did not get a signed GET URL", signedGetUrl);
// Use URL to GET blob
RestRequest getSignedRequest = createRestRequest(RestMethod.GET, signedGetUrl, null, null);
restResponseChannel = new MockRestResponseChannel();
doOperation(getSignedRequest, restResponseChannel);
verifyGetBlobResponse(getSignedRequest, restResponseChannel, null, headers, content, refAccount, refContainer);
// one error scenario to exercise exception path
verifyOperationFailure(createRestRequest(RestMethod.GET, Operations.GET_SIGNED_URL, null, null), RestServiceErrorCode.MissingArgs);
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method verifyUndeleteOK.
/**
* Verifies that a request returns the right response code once the blob has been undeleted.
* @param restRequest the {@link RestRequest} to send to {@link FrontendRestRequestService}.
* @throws Exception
*/
private void verifyUndeleteOK(RestRequest restRequest) throws Exception {
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
doOperation(restRequest, restResponseChannel);
assertEquals("Unexpected response status", ResponseStatus.Ok, restResponseChannel.getStatus());
assertTrue("No Date header", restResponseChannel.getHeader(RestUtils.Headers.DATE) != null);
assertEquals("Content-Length is not 0", "0", restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH));
}
Aggregations