Search in sources :

Example 71 with RestRequest

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));
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Test(org.junit.Test) RestUtilsTest(com.github.ambry.rest.RestUtilsTest) StorageStatsUtilTest(com.github.ambry.server.StorageStatsUtilTest)

Example 72 with RestRequest

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));
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) BlobId(com.github.ambry.commons.BlobId) RestMethod(com.github.ambry.rest.RestMethod)

Example 73 with RestRequest

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());
    }
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) RestMethod(com.github.ambry.rest.RestMethod) Method(java.lang.reflect.Method) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 74 with RestRequest

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);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) ByteBuffer(java.nio.ByteBuffer) LinkedList(java.util.LinkedList)

Example 75 with RestRequest

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);
    }
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) GetOption(com.github.ambry.protocol.GetOption)

Aggregations

RestRequest (com.github.ambry.rest.RestRequest)102 MockRestRequest (com.github.ambry.rest.MockRestRequest)82 MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)54 JSONObject (org.json.JSONObject)50 Test (org.junit.Test)46 RestServiceException (com.github.ambry.rest.RestServiceException)34 RestResponseChannel (com.github.ambry.rest.RestResponseChannel)26 RestMethod (com.github.ambry.rest.RestMethod)23 Account (com.github.ambry.account.Account)18 StorageStatsUtilTest (com.github.ambry.server.StorageStatsUtilTest)18 ExecutionException (java.util.concurrent.ExecutionException)18 ByteBuffer (java.nio.ByteBuffer)17 RestUtils (com.github.ambry.rest.RestUtils)16 RestUtilsTest (com.github.ambry.rest.RestUtilsTest)16 ReadableStreamChannel (com.github.ambry.router.ReadableStreamChannel)16 RestServiceErrorCode (com.github.ambry.rest.RestServiceErrorCode)15 MetricRegistry (com.codahale.metrics.MetricRegistry)14 Container (com.github.ambry.account.Container)14 RequestPath (com.github.ambry.rest.RequestPath)14 FutureResult (com.github.ambry.router.FutureResult)14