Search in sources :

Example 11 with MockRestResponseChannel

use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.

the class UndeleteHandlerTest method verifyUndelete.

/**
 * Verifies that the blob is undeleted
 * @param restRequest the {@link RestRequest} to get a signed URL.
 * @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 verifyUndelete(RestRequest restRequest, Account expectedAccount, Container expectedContainer) throws Exception {
    try {
        router.getBlob(blobId, new GetBlobOptionsBuilder().build()).get(1, TimeUnit.SECONDS);
        fail("Get blob should fail on a deleted blob");
    } catch (ExecutionException e) {
        RouterException routerException = (RouterException) e.getCause();
        assertEquals(RouterErrorCode.BlobDeleted, routerException.getErrorCode());
    }
    RestResponseChannel restResponseChannel = new MockRestResponseChannel();
    sendRequestGetResponse(restRequest, restResponseChannel);
    assertEquals("ResponseStatus not as expected", ResponseStatus.Ok, restResponseChannel.getStatus());
    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("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));
    router.getBlob(blobId, new GetBlobOptionsBuilder().build()).get(1, TimeUnit.SECONDS);
}
Also used : GetBlobOptionsBuilder(com.github.ambry.router.GetBlobOptionsBuilder) RouterException(com.github.ambry.router.RouterException) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) ExecutionException(java.util.concurrent.ExecutionException) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel)

Example 12 with MockRestResponseChannel

use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.

the class FrontendTestUrlSigningServiceFactory method releaseResourcesTest.

/**
 * Tests releasing of resources if response submission fails.
 * @throws JSONException
 * @throws UnsupportedEncodingException
 * @throws URISyntaxException
 */
@Test
public void releaseResourcesTest() throws JSONException, UnsupportedEncodingException, URISyntaxException {
    responseHandler.shutdown();
    // handleResponse of FrontendTestResponseHandler throws exception because it has been shutdown.
    try {
        RestRequest restRequest = createRestRequest(RestMethod.GET, "/", null, null);
        MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
        ReadableStreamChannel channel = new ByteBufferReadableStreamChannel(ByteBuffer.allocate(0));
        assertTrue("RestRequest channel not open", restRequest.isOpen());
        assertTrue("ReadableStreamChannel not open", channel.isOpen());
        frontendRestRequestService.submitResponse(restRequest, restResponseChannel, channel, null);
        assertFalse("ReadableStreamChannel is still open", channel.isOpen());
        // null ReadableStreamChannel
        restRequest = createRestRequest(RestMethod.GET, "/", null, null);
        restResponseChannel = new MockRestResponseChannel();
        assertTrue("RestRequest channel not open", restRequest.isOpen());
        frontendRestRequestService.submitResponse(restRequest, restResponseChannel, null, null);
        // bad RestRequest (close() throws IOException)
        channel = new ByteBufferReadableStreamChannel(ByteBuffer.allocate(0));
        restResponseChannel = new MockRestResponseChannel();
        assertTrue("ReadableStreamChannel not open", channel.isOpen());
        frontendRestRequestService.submitResponse(new BadRestRequest(), restResponseChannel, channel, null);
        // bad ReadableStreamChannel (close() throws IOException)
        restRequest = createRestRequest(RestMethod.GET, "/", null, null);
        restResponseChannel = new MockRestResponseChannel();
        assertTrue("RestRequest channel not open", restRequest.isOpen());
        frontendRestRequestService.submitResponse(restRequest, restResponseChannel, new BadRSC(), null);
    } finally {
        frontendRestRequestService.setupResponseHandler(responseHandler);
        responseHandler.start();
    }
}
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 13 with MockRestResponseChannel

use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.

the class FrontendTestUrlSigningServiceFactory method checkRouterExceptionPipeline.

/**
 * Checks that the exception received by submitting {@code restRequest} to {@link FrontendRestRequestService} matches
 * what was expected.
 * @param expectedExceptionMsg the expected exception message.
 * @param restRequest the {@link RestRequest} to submit to {@link FrontendRestRequestService}.
 * @throws Exception
 */
private void checkRouterExceptionPipeline(String expectedExceptionMsg, RestRequest restRequest) throws Exception {
    try {
        doOperation(restRequest, new MockRestResponseChannel());
        fail("Operation " + restRequest.getRestMethod() + " should have failed because an external service would have thrown an exception");
    } catch (RestServiceException | RuntimeException e) {
        // catching RestServiceException because RouterException should have been converted.
        // RuntimeException might get bubbled up as is.
        assertEquals("Unexpected exception message", expectedExceptionMsg, Utils.getRootCause(e).getMessage());
        // Nothing should be closed.
        assertTrue("RestRequest channel is not open", restRequest.isOpen());
        restRequest.close();
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel)

Example 14 with MockRestResponseChannel

use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.

the class FrontendTestUrlSigningServiceFactory method verifyDeleteAccepted.

/**
 * Verifies that a request returns the right response code  once the blob has been deleted.
 * @param restRequest the {@link RestRequest} to send to {@link FrontendRestRequestService}.
 * @throws Exception
 */
private void verifyDeleteAccepted(RestRequest restRequest) throws Exception {
    MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
    doOperation(restRequest, restResponseChannel);
    assertEquals("Unexpected response status", ResponseStatus.Accepted, 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 15 with MockRestResponseChannel

use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.

the class FrontendTestUrlSigningServiceFactory method deleteServiceIdTest.

/**
 * Test that the correct service ID is sent to the router on deletes.
 * @throws Exception
 */
@Test
public void deleteServiceIdTest() throws Exception {
    FrontendTestRouter testRouter = new FrontendTestRouter();
    frontendRestRequestService = new FrontendRestRequestService(frontendConfig, frontendMetrics, testRouter, clusterMap, idConverterFactory, securityServiceFactory, urlSigningService, idSigningService, null, accountService, accountAndContainerInjector, datacenterName, hostname, clusterName, accountStatsStore, QUOTA_MANAGER);
    frontendRestRequestService.setupResponseHandler(responseHandler);
    frontendRestRequestService.start();
    JSONObject headers = new JSONObject();
    String serviceId = "service-id";
    headers.put(RestUtils.Headers.SERVICE_ID, serviceId);
    doOperation(createRestRequest(RestMethod.DELETE, referenceBlobIdStr, headers, null), new MockRestResponseChannel());
    assertEquals(serviceId, testRouter.deleteServiceId);
    doOperation(createRestRequest(RestMethod.DELETE, referenceBlobIdStr, null, null), new MockRestResponseChannel());
    assertNull("Service ID should not have been set for this delete", testRouter.deleteServiceId);
}
Also used : JSONObject(org.json.JSONObject) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Test(org.junit.Test) RestUtilsTest(com.github.ambry.rest.RestUtilsTest) StorageStatsUtilTest(com.github.ambry.server.StorageStatsUtilTest)

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