Search in sources :

Example 36 with MockRestResponseChannel

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));
}
Also used : MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel)

Example 37 with MockRestResponseChannel

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

Example 38 with MockRestResponseChannel

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);
    }
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) RestMethod(com.github.ambry.rest.RestMethod) Method(java.lang.reflect.Method) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) JSONException(org.json.JSONException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RestServiceException(com.github.ambry.rest.RestServiceException) IOException(java.io.IOException) RouterException(com.github.ambry.router.RouterException) URISyntaxException(java.net.URISyntaxException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AccountServiceException(com.github.ambry.account.AccountServiceException) Test(org.junit.Test) RestUtilsTest(com.github.ambry.rest.RestUtilsTest) StorageStatsUtilTest(com.github.ambry.server.StorageStatsUtilTest)

Example 39 with MockRestResponseChannel

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);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) ByteBuffer(java.nio.ByteBuffer) LinkedList(java.util.LinkedList) Test(org.junit.Test) RestUtilsTest(com.github.ambry.rest.RestUtilsTest) StorageStatsUtilTest(com.github.ambry.server.StorageStatsUtilTest)

Example 40 with MockRestResponseChannel

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));
}
Also used : MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel)

Aggregations

MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)72 RestRequest (com.github.ambry.rest.RestRequest)57 MockRestRequest (com.github.ambry.rest.MockRestRequest)51 JSONObject (org.json.JSONObject)39 RestServiceException (com.github.ambry.rest.RestServiceException)36 Test (org.junit.Test)34 RestResponseChannel (com.github.ambry.rest.RestResponseChannel)27 RestMethod (com.github.ambry.rest.RestMethod)19 Account (com.github.ambry.account.Account)18 FutureResult (com.github.ambry.router.FutureResult)17 ReadableStreamChannel (com.github.ambry.router.ReadableStreamChannel)17 ExecutionException (java.util.concurrent.ExecutionException)17 StorageStatsUtilTest (com.github.ambry.server.StorageStatsUtilTest)16 RestUtils (com.github.ambry.rest.RestUtils)15 ByteBuffer (java.nio.ByteBuffer)15 MetricRegistry (com.codahale.metrics.MetricRegistry)14 RestServiceErrorCode (com.github.ambry.rest.RestServiceErrorCode)14 RestUtilsTest (com.github.ambry.rest.RestUtilsTest)14 TestUtils (com.github.ambry.utils.TestUtils)14 TimeUnit (java.util.concurrent.TimeUnit)14