Search in sources :

Example 11 with RestRequest

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

the class NamedBlobPutHandlerTest method putBlobAndVerify.

/**
 * Make a stitch blob call using {@link PostBlobHandler} and verify the result of the operation.
 * @param errorChecker if non-null, expect an exception to be thrown by the post flow and verify it using this
 *                     {@link ThrowingConsumer}.
 * @param ttl the ttl used for the blob
 * @throws Exception
 */
private void putBlobAndVerify(ThrowingConsumer<ExecutionException> errorChecker, long ttl) throws Exception {
    JSONObject headers = new JSONObject();
    FrontendRestRequestServiceTest.setAmbryHeadersForPut(headers, ttl, !REF_CONTAINER.isCacheable(), SERVICE_ID, CONTENT_TYPE, OWNER_ID, null, null, null);
    byte[] content = TestUtils.getRandomBytes(1024);
    RestRequest request = getRestRequest(headers, request_path, content);
    RestResponseChannel restResponseChannel = new MockRestResponseChannel();
    FutureResult<Void> future = new FutureResult<>();
    idConverterFactory.lastInput = null;
    idConverterFactory.lastBlobInfo = null;
    idConverterFactory.lastConvertedId = null;
    namedBlobPutHandler.handle(request, restResponseChannel, future::done);
    if (errorChecker == null) {
        future.get(TIMEOUT_SECS, TimeUnit.SECONDS);
        assertEquals("Unexpected location header", idConverterFactory.lastConvertedId, restResponseChannel.getHeader(RestUtils.Headers.LOCATION));
        InMemoryRouter.InMemoryBlob blob = router.getActiveBlobs().get(idConverterFactory.lastInput);
        assertEquals("Unexpected blob content stored", ByteBuffer.wrap(content), blob.getBlob());
        assertEquals("Unexpected TTL in blob", ttl, blob.getBlobProperties().getTimeToLiveInSeconds());
        assertEquals("Unexpected TTL in named blob DB", ttl, idConverterFactory.lastBlobInfo.getBlobProperties().getTimeToLiveInSeconds());
        assertEquals("Unexpected response status", restResponseChannel.getStatus(), ResponseStatus.Ok);
    } else {
        TestUtils.assertException(ExecutionException.class, () -> future.get(TIMEOUT_SECS, TimeUnit.SECONDS), errorChecker);
    }
}
Also used : InMemoryRouter(com.github.ambry.router.InMemoryRouter) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) FutureResult(com.github.ambry.router.FutureResult) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel)

Example 12 with RestRequest

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

the class AmbryUrlSigningServiceTest method notSignedRequestFailuresTest.

/**
 * Tests for some failure scenarios in verification.
 * @throws Exception
 */
@Test
public void notSignedRequestFailuresTest() throws Exception {
    // positive test done in signAndVerifyTest()
    AmbryUrlSigningService signer = getUrlSignerWithDefaults(new MockTime());
    RestRequest request = getRequestFromUrl(RestMethod.GET, "/");
    assertFalse("Request should not be declared signed", signer.isRequestSigned(request));
    ensureVerificationFailure(signer, request, RestServiceErrorCode.InternalServerError);
    request.setArg(RestUtils.Headers.URL_TYPE, RestMethod.POST.name());
    assertFalse("Request should not be declared signed", signer.isRequestSigned(request));
    ensureVerificationFailure(signer, request, RestServiceErrorCode.InternalServerError);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) MockTime(com.github.ambry.utils.MockTime) Test(org.junit.Test)

Example 13 with RestRequest

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

the class AmbryUrlSigningServiceTest method doSignAndVerifyTest.

// signAndVerifyTest() tests helpers
/**
 * Generates and verifies signed URLs. Also tests some failure scenarios.
 * @param signer the {@link AmbryUrlSigningService} to use.
 * @param restMethod the {@link RestMethod} to verify.
 * @param time the {@link Time} instance to use.
 * @throws Exception
 */
private void doSignAndVerifyTest(AmbryUrlSigningService signer, RestMethod restMethod, Time time) throws Exception {
    long urlTtl = Math.min(Utils.getRandomLong(TestUtils.RANDOM, 2000) + 2000, MAX_URL_TTL_SECS);
    String randomHeaderVal = TestUtils.getRandomString(10);
    long maxUploadSize = Utils.getRandomLong(TestUtils.RANDOM, 4001) + 2000;
    // all defaults overridden
    RestRequest request = getUrlSignRequest(restMethod, urlTtl, randomHeaderVal, maxUploadSize, false);
    String url = signer.getSignedUrl(request);
    verifySignedUrl(signer, url, restMethod, randomHeaderVal, maxUploadSize, Utils.Infinite_Time, false);
    time.sleep(TimeUnit.SECONDS.toMillis(urlTtl + 1));
    ensureVerificationFailure(signer, getRequestFromUrl(restMethod, url), RestServiceErrorCode.Unauthorized);
    // chunk upload case, should use special stitched chunk upload settings.
    request = getUrlSignRequest(restMethod, urlTtl, randomHeaderVal, maxUploadSize, true);
    url = signer.getSignedUrl(request);
    verifySignedUrl(signer, url, restMethod, randomHeaderVal, CHUNK_UPLOAD_MAX_CHUNK_SIZE, CHUNK_UPLOAD_INITIAL_CHUNK_TTL_SECS, true);
    time.sleep(TimeUnit.SECONDS.toMillis(urlTtl + 1));
    ensureVerificationFailure(signer, getRequestFromUrl(restMethod, url), RestServiceErrorCode.Unauthorized);
    // no defaults overridden
    request = getUrlSignRequest(restMethod, Utils.Infinite_Time, randomHeaderVal, null, false);
    url = signer.getSignedUrl(request);
    verifySignedUrl(signer, url, restMethod, randomHeaderVal, DEFAULT_MAX_UPLOAD_SIZE, Utils.Infinite_Time, false);
    // change RestMethod and ensure verification failure
    ensureVerificationFailure(signer, getRequestFromUrl(RestMethod.UNKNOWN, url), RestServiceErrorCode.Unauthorized);
    time.sleep(TimeUnit.SECONDS.toMillis(DEFAULT_URL_TTL_SECS + 1));
    ensureVerificationFailure(signer, getRequestFromUrl(restMethod, url), RestServiceErrorCode.Unauthorized);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest)

Example 14 with RestRequest

use of com.github.ambry.rest.RestRequest 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 15 with RestRequest

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

the class FrontendTestUrlSigningServiceFactory method getStatsReportTest.

/**
 * Tests the handling of {@link Operations#STATS_REPORT} get requests.
 * @throws Exception
 */
@Test
public void getStatsReportTest() throws Exception {
    AggregatedAccountStorageStats aggregatedAccountStorageStats = new AggregatedAccountStorageStats(StorageStatsUtilTest.generateRandomAggregatedAccountStorageStats((short) 1, 10, 10, 1000L, 2, 100));
    AggregatedPartitionClassStorageStats aggregatedPartitionClassStorageStats = new AggregatedPartitionClassStorageStats(StorageStatsUtilTest.generateRandomAggregatedPartitionClassStorageStats(new String[] { "default", "newClass" }, (short) 1, 10, 10, 1000L, 2, 100));
    doAnswer(invocation -> {
        String clusterName = invocation.getArgument(0);
        if (clusterName.equals(CLUSTER_NAME)) {
            return aggregatedAccountStorageStats;
        } else {
            return null;
        }
    }).when(accountStatsStore).queryAggregatedAccountStorageStatsByClusterName(anyString());
    doAnswer(invocation -> {
        String clusterName = invocation.getArgument(0);
        if (clusterName.equals(CLUSTER_NAME)) {
            return aggregatedPartitionClassStorageStats;
        } else {
            return null;
        }
    }).when(accountStatsStore).queryAggregatedPartitionClassStorageStatsByClusterName(anyString());
    ObjectMapper mapper = new ObjectMapper();
    // construct a request to get account stats
    JSONObject headers = new JSONObject();
    headers.put(RestUtils.Headers.CLUSTER_NAME, CLUSTER_NAME);
    headers.put(RestUtils.Headers.GET_STATS_REPORT_TYPE, StatsReportType.ACCOUNT_REPORT.name());
    RestRequest request = createRestRequest(RestMethod.GET, Operations.STATS_REPORT, headers, null);
    MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
    doOperation(request, restResponseChannel);
    assertEquals("Storage stats mismatch", aggregatedAccountStorageStats.getStorageStats(), mapper.readValue(restResponseChannel.getResponseBody(), AggregatedAccountStorageStats.class).getStorageStats());
    // construct a request to get partition class stats
    headers = new JSONObject();
    headers.put(RestUtils.Headers.CLUSTER_NAME, CLUSTER_NAME);
    headers.put(RestUtils.Headers.GET_STATS_REPORT_TYPE, StatsReportType.PARTITION_CLASS_REPORT.name());
    request = createRestRequest(RestMethod.GET, Operations.STATS_REPORT, headers, null);
    restResponseChannel = new MockRestResponseChannel();
    doOperation(request, restResponseChannel);
    assertEquals("Storage stats mismatch", aggregatedPartitionClassStorageStats.getStorageStats(), mapper.readValue(restResponseChannel.getResponseBody(), AggregatedPartitionClassStorageStats.class).getStorageStats());
    // test clustername not found case to ensure that it goes through the exception path
    headers = new JSONObject();
    headers.put(RestUtils.Headers.CLUSTER_NAME, "WRONG_CLUSTER");
    headers.put(RestUtils.Headers.GET_STATS_REPORT_TYPE, StatsReportType.ACCOUNT_REPORT.name());
    request = createRestRequest(RestMethod.GET, Operations.STATS_REPORT, headers, null);
    try {
        doOperation(request, new MockRestResponseChannel());
        fail("Operation should have failed");
    } catch (RestServiceException e) {
        assertEquals("ErrorCode not as expected", RestServiceErrorCode.NotFound, e.getErrorCode());
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) AggregatedAccountStorageStats(com.github.ambry.server.storagestats.AggregatedAccountStorageStats) AggregatedPartitionClassStorageStats(com.github.ambry.server.storagestats.AggregatedPartitionClassStorageStats) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test) RestUtilsTest(com.github.ambry.rest.RestUtilsTest) StorageStatsUtilTest(com.github.ambry.server.StorageStatsUtilTest)

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