use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method submitResponseTest.
/**
* Tests
* {@link FrontendRestRequestService#submitResponse(RestRequest, RestResponseChannel, ReadableStreamChannel, Exception)}.
* @throws JSONException
* @throws UnsupportedEncodingException
* @throws URISyntaxException
*/
@Test
public void submitResponseTest() throws JSONException, UnsupportedEncodingException, URISyntaxException {
String exceptionMsg = TestUtils.getRandomString(10);
responseHandler.shutdown();
// handleResponse of FrontendTestResponseHandler throws exception because it has been shutdown.
try {
// there is an exception already.
RestRequest restRequest = createRestRequest(RestMethod.GET, "/", null, null);
assertTrue("RestRequest channel is not open", restRequest.isOpen());
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
frontendRestRequestService.submitResponse(restRequest, restResponseChannel, null, new RuntimeException(exceptionMsg));
assertEquals("Unexpected exception message", exceptionMsg, restResponseChannel.getException().getMessage());
// there is no exception and exception thrown when the response is submitted.
restRequest = createRestRequest(RestMethod.GET, "/", null, null);
assertTrue("RestRequest channel is not open", restRequest.isOpen());
restResponseChannel = new MockRestResponseChannel();
ReadableStreamChannel response = new ByteBufferReadableStreamChannel(ByteBuffer.allocate(0));
assertTrue("Response channel is not open", response.isOpen());
frontendRestRequestService.submitResponse(restRequest, restResponseChannel, response, null);
assertNotNull("There is no cause of failure", restResponseChannel.getException());
// resources should have been cleaned up.
assertFalse("Response channel is not cleaned up", response.isOpen());
} finally {
frontendRestRequestService.setupResponseHandler(responseHandler);
responseHandler.start();
}
// verify tracking infos are attached accordingly.
RestRequest restRequest;
MockRestResponseChannel restResponseChannel;
for (String header : RestUtils.TrackingHeaders.TRACKING_HEADERS) {
restRequest = createRestRequest(RestMethod.GET, "/", null, null);
restResponseChannel = new MockRestResponseChannel();
frontendRestRequestService.submitResponse(restRequest, restResponseChannel, null, null);
assertTrue("Response header should not contain tracking info", restResponseChannel.getHeader(header) == null);
}
restRequest = createRestRequest(RestMethod.GET, "/", null, null);
restRequest.setArg(RestUtils.InternalKeys.SEND_TRACKING_INFO, new Boolean(true));
restResponseChannel = new MockRestResponseChannel();
frontendRestRequestService.submitResponse(restRequest, restResponseChannel, null, null);
assertEquals("Unexpected or missing tracking info", datacenterName, restResponseChannel.getHeader(RestUtils.TrackingHeaders.DATACENTER_NAME));
assertEquals("Unexpected or missing tracking info", hostname, restResponseChannel.getHeader(RestUtils.TrackingHeaders.FRONTEND_NAME));
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method verifyAccountAndContainerFromBlobId.
/**
* Verifies presence of {@link Account} and {@link Container} injected into {@link RestRequest} using a
* blobId string, for get/head/delete operations.
* @param blobId The blobId string to get/head/delete.
* @param expectedAccount The expected {@link Account} to verify its presence in {@link RestRequest}.
* @param expectedContainer The expected {@link Container} to verify its presence in {@link RestRequest}.
* @param expectedRestErrorCode The expected {@link RestServiceErrorCode} to verify.
* @throws Exception
*/
private void verifyAccountAndContainerFromBlobId(String blobId, Account expectedAccount, Container expectedContainer, RestServiceErrorCode expectedRestErrorCode) throws Exception {
if (blobId.startsWith("/")) {
blobId = blobId.substring(1);
}
// PUT is verified in the tests of the individual handlers.
for (RestMethod restMethod : Lists.newArrayList(RestMethod.GET, RestMethod.HEAD, RestMethod.DELETE)) {
RestRequest restRequest = createRestRequest(restMethod, "/" + blobId, null, null);
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
try {
doOperation(restRequest, restResponseChannel);
if (expectedRestErrorCode != null) {
fail("Should have thrown");
}
} catch (RestServiceException e) {
assertEquals("Wrong RestServiceErrorCode", expectedRestErrorCode, e.getErrorCode());
}
BlobId deserializedId = new BlobId(blobId, clusterMap);
// Because BlobInfo is not fetched on deletes, V1 Blob IDs will never be reassigned to a known account/container.
boolean alwaysExpectUnknown = restMethod == RestMethod.DELETE && deserializedId.getAccountId() == Account.UNKNOWN_ACCOUNT_ID && deserializedId.getContainerId() == Container.UNKNOWN_CONTAINER_ID;
assertEquals("Wrong account object in RestRequest's args", alwaysExpectUnknown ? InMemAccountService.UNKNOWN_ACCOUNT : expectedAccount, restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_ACCOUNT_KEY));
assertEquals("Wrong container object in RestRequest's args", alwaysExpectUnknown ? Container.UNKNOWN_CONTAINER : expectedContainer, restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_CONTAINER_KEY));
}
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method doNullInputsForFunctionsTest.
// nullInputsForFunctionsTest() helpers
/**
* Checks for reaction to null input in {@code methodName} in {@link FrontendRestRequestService}.
* @param methodName the name of the method to invoke.
* @throws Exception
*/
private void doNullInputsForFunctionsTest(String methodName) throws Exception {
Method method = FrontendRestRequestService.class.getDeclaredMethod(methodName, RestRequest.class, RestResponseChannel.class);
RestRequest restRequest = createRestRequest(RestMethod.GET, "/", null, null);
RestResponseChannel restResponseChannel = new MockRestResponseChannel();
responseHandler.reset();
try {
method.invoke(frontendRestRequestService, null, restResponseChannel);
fail("Method [" + methodName + "] should have failed because RestRequest is null");
} catch (InvocationTargetException e) {
assertEquals("Unexpected exception class", IllegalArgumentException.class, e.getTargetException().getClass());
}
responseHandler.reset();
try {
method.invoke(frontendRestRequestService, restRequest, null);
fail("Method [" + methodName + "] should have failed because RestResponseChannel is null");
} catch (InvocationTargetException e) {
assertEquals("Unexpected exception class", IllegalArgumentException.class, e.getTargetException().getClass());
}
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method postBlobAndVerify.
/**
* Posts a blob with the given {@code headers} and {@code content}.
* @param headers the headers of the new blob that get converted to blob properties.
* @param content the content of the blob.
* @param expectedAccount the expected {@link Account} instance injected into the {@link RestRequest}.
* @param expectedContainer the expected {@link Container} instance injected into the {@link RestRequest}.
* @return the blob ID of the blob.
* @throws Exception
*/
private String postBlobAndVerify(JSONObject headers, ByteBuffer content, Account expectedAccount, Container expectedContainer) throws Exception {
List<ByteBuffer> contents = new LinkedList<>();
contents.add(content);
contents.add(null);
RestRequest restRequest = createRestRequest(RestMethod.POST, "/", headers, contents);
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
doOperation(restRequest, restResponseChannel);
return verifyPostAndReturnBlobId(restRequest, restResponseChannel, expectedAccount, expectedContainer);
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method verifyOperationsAfterDelete.
/**
* Verifies that the right {@link ResponseStatus} is returned for GET, HEAD, TTL update and DELETE once a blob is
* deleted.
* @param blobId the ID of the blob that was deleted.
* @param expectedHeaders the expected headers in the response if the right options are provided.
* @param expectedContent the expected content of the blob if the right options are provided.
* @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 verifyOperationsAfterDelete(String blobId, JSONObject expectedHeaders, ByteBuffer expectedContent, Account expectedAccount, Container expectedContainer) throws Exception {
RestRequest restRequest = createRestRequest(RestMethod.GET, blobId, null, null);
verifyOperationFailure(restRequest, RestServiceErrorCode.Deleted);
restRequest = createRestRequest(RestMethod.HEAD, blobId, null, null);
verifyOperationFailure(restRequest, RestServiceErrorCode.Deleted);
JSONObject headers = new JSONObject();
setUpdateTtlHeaders(headers, blobId, "verifyOperationsAfterDelete");
restRequest = createRestRequest(RestMethod.PUT, Operations.UPDATE_TTL, headers, null);
verifyOperationFailure(restRequest, RestServiceErrorCode.Deleted);
restRequest = createRestRequest(RestMethod.DELETE, blobId, null, null);
verifyDeleteAccepted(restRequest);
GetOption[] options = { GetOption.Include_Deleted_Blobs, GetOption.Include_All };
for (GetOption option : options) {
getBlobAndVerify(blobId, null, option, expectedHeaders, expectedContent, expectedAccount, expectedContainer);
getNotModifiedBlobAndVerify(blobId, option);
getUserMetadataAndVerify(blobId, option, expectedHeaders);
getBlobInfoAndVerify(blobId, option, expectedHeaders, expectedAccount, expectedContainer);
getHeadAndVerify(blobId, null, option, expectedHeaders, expectedAccount, expectedContainer);
}
}
Aggregations