Search in sources :

Example 26 with RestRequest

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

the class GetClusterMapSnapshotHandlerTest method verifyFailureWithMsg.

/**
 * Verifies that attempting to get the snapshot of the cluster map fails with {@code msg}.
 * @param msg the message in the {@link Exception} that will be thrown.
 * @throws Exception
 */
private void verifyFailureWithMsg(String msg) throws Exception {
    RestRequest restRequest = createRestRequest();
    try {
        sendRequestGetResponse(restRequest, new MockRestResponseChannel());
        fail("Request should have failed");
    } catch (Exception e) {
        assertEquals("Unexpected Exception", msg, e.getMessage());
    }
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) IOException(java.io.IOException)

Example 27 with RestRequest

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

the class AmbryIdConverterFactoryTest method testConversion.

/**
 * Tests the conversion by the {@code idConverter}.
 * @param idConverter the {@link IdConverter} instance to use.
 * @param restMethod the {@link RestMethod} of the {@link RestRequest} that will be created.
 * @param signedIdMetadata the headers of the {@link RestRequest}.
 * @param expectedOutput the expected output from the {@code idConverter}.
 * @param input the input string
 * @throws Exception
 */
private void testConversion(IdConverter idConverter, RestMethod restMethod, Map<String, String> signedIdMetadata, String expectedOutput, String input) throws Exception {
    JSONObject requestData = new JSONObject();
    requestData.put(MockRestRequest.REST_METHOD_KEY, restMethod.name());
    requestData.put(MockRestRequest.URI_KEY, "/");
    RestRequest restRequest = new MockRestRequest(requestData, null);
    if (signedIdMetadata != null) {
        restRequest.setArg(RestUtils.InternalKeys.SIGNED_ID_METADATA_KEY, signedIdMetadata);
    }
    IdConversionCallback callback = new IdConversionCallback();
    assertEquals("Converted ID does not match expected (Future)", expectedOutput, idConverter.convert(restRequest, input, callback).get());
    assertEquals("Converted ID does not match expected (Callback)", expectedOutput, callback.result);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) MockRestRequest(com.github.ambry.rest.MockRestRequest)

Example 28 with RestRequest

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

the class AmbrySecurityServiceTest method preProcessRequestTest.

/**
 * Tests for {@link AmbrySecurityService#preProcessRequest(RestRequest, Callback)}
 * @throws Exception
 */
@Test
public void preProcessRequestTest() throws Exception {
    RestMethod[] methods = new RestMethod[] { RestMethod.POST, RestMethod.GET, RestMethod.DELETE, RestMethod.HEAD, RestMethod.OPTIONS, RestMethod.PUT };
    for (RestMethod restMethod : methods) {
        // add a header that is prohibited
        JSONObject headers = new JSONObject();
        headers.put(RestUtils.InternalKeys.KEEP_ALIVE_ON_ERROR_HINT, true);
        RestRequest restRequest = createRestRequest(restMethod, "/", headers);
        try {
            securityService.preProcessRequest(restRequest).get(1, TimeUnit.SECONDS);
            Assert.fail("Should have failed because the request contains a prohibited header: " + RestUtils.InternalKeys.KEEP_ALIVE_ON_ERROR_HINT);
        } catch (ExecutionException e) {
            RestServiceException rse = (RestServiceException) Utils.getRootCause(e);
            Assert.assertEquals("Should be a bad request", RestServiceErrorCode.BadRequest, rse.getErrorCode());
        }
    }
    // verify request args regarding to tracking is set accordingly
    RestRequest restRequest = createRestRequest(RestMethod.GET, "/", null);
    securityService.preProcessRequest(restRequest).get();
    Assert.assertTrue("The arg with key: ambry-internal-keys-send-tracking-info should be set to true", (Boolean) restRequest.getArgs().get(RestUtils.InternalKeys.SEND_TRACKING_INFO));
    Properties properties = new Properties();
    properties.setProperty("frontend.attach.tracking.info", "false");
    FrontendConfig frontendConfig = new FrontendConfig(new VerifiableProperties(properties));
    SecurityService securityServiceWithTrackingDisabled = new AmbrySecurityService(frontendConfig, new FrontendMetrics(new MetricRegistry()), URL_SIGNING_SERVICE_FACTORY.getUrlSigningService(), hostLevelThrottler, QUOTA_MANAGER);
    restRequest = createRestRequest(RestMethod.GET, "/", null);
    securityServiceWithTrackingDisabled.preProcessRequest(restRequest);
    Assert.assertFalse("The arg with key: ambry-internal-keys-send-tracking-info should be set to false", (Boolean) restRequest.getArgs().get(RestUtils.InternalKeys.SEND_TRACKING_INFO));
}
Also used : VerifiableProperties(com.github.ambry.config.VerifiableProperties) MetricRegistry(com.codahale.metrics.MetricRegistry) BlobProperties(com.github.ambry.messageformat.BlobProperties) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) RestServiceException(com.github.ambry.rest.RestServiceException) FrontendConfig(com.github.ambry.config.FrontendConfig) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) ExecutionException(java.util.concurrent.ExecutionException) RestMethod(com.github.ambry.rest.RestMethod) Test(org.junit.Test)

Example 29 with RestRequest

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

the class CostPolicyTestUtils method createMockRequestWithMethod.

/**
 * Creates a mock {@link RestRequest} object for test.
 * @param restMethod {@link RestMethod} of the RestRequest.
 * @param bytesReceived number of bytes received in the request.
 * @return RestRequest object.
 */
public static RestRequest createMockRequestWithMethod(RestMethod restMethod, String uri, long bytesReceived) throws RestServiceException {
    RestRequest restRequest = mock(RestRequest.class);
    when(restRequest.getRestMethod()).thenReturn(restMethod);
    when(restRequest.getBlobBytesReceived()).thenReturn(bytesReceived);
    when(restRequest.getBytesReceived()).thenReturn(bytesReceived);
    if (restMethod == RestMethod.POST) {
        when(restRequest.getUri()).thenReturn("/");
        when(restRequest.getPath()).thenReturn("/");
    } else {
        when(restRequest.getUri()).thenReturn(uri);
        when(restRequest.getPath()).thenReturn(uri);
    }
    RequestPath requestPath = RequestPath.parse(restRequest, null, "ambry-test");
    if (restMethod == RestMethod.PUT && bytesReceived != -1) {
        requestPath = RequestPath.parse("/" + Operations.NAMED_BLOB, Collections.emptyMap(), Collections.emptyList(), "ambry-test");
    }
    Map<String, Object> args = new HashMap<>();
    args.put(RestUtils.InternalKeys.REQUEST_PATH, requestPath);
    when(restRequest.getArgs()).thenReturn(args);
    return restRequest;
}
Also used : RequestPath(com.github.ambry.rest.RequestPath) RestRequest(com.github.ambry.rest.RestRequest) HashMap(java.util.HashMap)

Example 30 with RestRequest

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

the class GetStatsReportHandlerTest method handleBadCaseTest.

@Test
public void handleBadCaseTest() throws Exception {
    ThrowingBiConsumer<RestRequest, RestServiceErrorCode> testAction = (request, expectedErrorCode) -> {
        TestUtils.assertException(RestServiceException.class, () -> sendRequestGetResponse(request, new MockRestResponseChannel()), e -> assertEquals(expectedErrorCode, e.getErrorCode()));
    };
    RestRequest request = createRestRequest("WRONG_CLUSTER", StatsReportType.ACCOUNT_REPORT.name());
    testAction.accept(request, RestServiceErrorCode.NotFound);
    request = createRestRequest(null, StatsReportType.ACCOUNT_REPORT.name());
    testAction.accept(request, RestServiceErrorCode.MissingArgs);
    request = createRestRequest(CLUSTER_NAME, "WRONG_STATS_REPORT_TYPE");
    testAction.accept(request, RestServiceErrorCode.BadRequest);
    request = createRestRequest(CLUSTER_NAME, null);
    testAction.accept(request, RestServiceErrorCode.MissingArgs);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) AggregatedPartitionClassStorageStats(com.github.ambry.server.storagestats.AggregatedPartitionClassStorageStats) FutureResult(com.github.ambry.router.FutureResult) RestTestUtils(com.github.ambry.rest.RestTestUtils) JSONObject(org.json.JSONObject) TestUtils(com.github.ambry.utils.TestUtils) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) MetricRegistry(com.codahale.metrics.MetricRegistry) RestMethod(com.github.ambry.rest.RestMethod) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) Test(org.junit.Test) ThrowingBiConsumer(com.github.ambry.utils.ThrowingBiConsumer) AccountStatsStore(com.github.ambry.accountstats.AccountStatsStore) TimeUnit(java.util.concurrent.TimeUnit) AggregatedAccountStorageStats(com.github.ambry.server.storagestats.AggregatedAccountStorageStats) Mockito(org.mockito.Mockito) RestServiceException(com.github.ambry.rest.RestServiceException) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) StatsReportType(com.github.ambry.server.StatsReportType) RestUtils(com.github.ambry.rest.RestUtils) Assert(org.junit.Assert) RestRequest(com.github.ambry.rest.RestRequest) StorageStatsUtilTest(com.github.ambry.server.StorageStatsUtilTest) RestServiceException(com.github.ambry.rest.RestServiceException) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) Test(org.junit.Test) 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