Search in sources :

Example 81 with RestRequest

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

the class TailoredPeersClusterMap method doBadArgsTest.

// badArgsTest() helpers.
/**
 * Does the test where bad args are provided in the request to {@link GetPeersHandler}.
 * @param name the name of the host whose peers are required. Can be {@code null} if this param should be omitted.
 * @param port the port of the host whose peers are required. Can be {@code null} if this param should be omitted.
 * @param expectedErrorCode the {@link RestServiceErrorCode} expected in response.
 * @throws Exception
 */
private void doBadArgsTest(String name, String port, RestServiceErrorCode expectedErrorCode) throws Exception {
    StringBuilder uri = new StringBuilder(Operations.GET_PEERS + "?");
    if (name != null) {
        uri.append(GetPeersHandler.NAME_QUERY_PARAM).append("=").append(name);
    }
    if (port != null) {
        if (name != null) {
            uri.append("&");
        }
        uri.append(GetPeersHandler.PORT_QUERY_PARAM).append("=").append(port);
    }
    JSONObject data = new JSONObject();
    data.put(MockRestRequest.REST_METHOD_KEY, RestMethod.GET.name());
    data.put(MockRestRequest.URI_KEY, uri.toString());
    RestRequest restRequest = new MockRestRequest(data, null);
    try {
        sendRequestGetResponse(restRequest, new MockRestResponseChannel());
        fail("Request should have failed");
    } catch (RestServiceException e) {
        assertEquals("Unexpected RestServiceErrorCode", expectedErrorCode, 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) MockRestRequest(com.github.ambry.rest.MockRestRequest) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel)

Example 82 with RestRequest

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

the class GetSignedUrlHandlerTest method verifyFailureWithMsg.

/**
 * Verifies that attempting to get signed urls fails with the provided {@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 = new MockRestRequest(MockRestRequest.DUMMY_DATA, null);
    restRequest.setArg(RestUtils.InternalKeys.REQUEST_PATH, RequestPath.parse("/signedUrl", Collections.emptyMap(), Collections.emptyList(), "Ambry-test"));
    restRequest.setArg(RestUtils.Headers.URL_TYPE, RestMethod.POST.name());
    restRequest.setArg(RestUtils.Headers.TARGET_ACCOUNT_NAME, REF_ACCOUNT.getName());
    restRequest.setArg(RestUtils.Headers.TARGET_CONTAINER_NAME, REF_CONTAINER.getName());
    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) MockRestRequest(com.github.ambry.rest.MockRestRequest) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) IOException(java.io.IOException) RestServiceException(com.github.ambry.rest.RestServiceException)

Example 83 with RestRequest

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

the class NamedBlobPutHandlerTest method getRestRequest.

/**
 * Method to easily create a PUT {@link RestRequest}. This will set {@link RestUtils.InternalKeys#REQUEST_PATH} to a
 * valid {@link RequestPath} object.
 * @param headers any associated headers as a {@link JSONObject}.
 * @param path the path for the request.
 * @param requestBody the body of the request.
 * @return A {@link RestRequest} object that defines the request required by the input.
 */
private RestRequest getRestRequest(JSONObject headers, String path, byte[] requestBody) throws UnsupportedEncodingException, URISyntaxException, RestServiceException {
    RestRequest request = FrontendRestRequestServiceTest.createRestRequest(RestMethod.PUT, path, headers, new LinkedList<>(Arrays.asList(ByteBuffer.wrap(requestBody), null)));
    request.setArg(RestUtils.InternalKeys.REQUEST_PATH, RequestPath.parse(request, frontendConfig.pathPrefixesToRemove, CLUSTER_NAME));
    return request;
}
Also used : RestRequest(com.github.ambry.rest.RestRequest)

Example 84 with RestRequest

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

the class NamedBlobPutHandlerTest method doTtlRequiredEnforcementTest.

/**
 * Does the TTL required enforcement test by selecting the right verification methods based on container and frontend
 * config
 * @param container the {@link Container} to upload to
 * @param blobTtlSecs the TTL to set for the blob
 * @throws Exception
 */
private void doTtlRequiredEnforcementTest(Container container, long blobTtlSecs) throws Exception {
    JSONObject headers = new JSONObject();
    FrontendRestRequestServiceTest.setAmbryHeadersForPut(headers, blobTtlSecs, !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<>();
    namedBlobPutHandler.handle(request, restResponseChannel, future::done);
    if (container.isTtlRequired() && (blobTtlSecs == Utils.Infinite_Time || blobTtlSecs > frontendConfig.maxAcceptableTtlSecsIfTtlRequired)) {
        if (frontendConfig.failIfTtlRequiredButNotProvided) {
            try {
                future.get(TIMEOUT_SECS, TimeUnit.SECONDS);
                fail("Post should have failed");
            } catch (ExecutionException e) {
                RestServiceException rootCause = (RestServiceException) Utils.getRootCause(e);
                assertNotNull("Root cause should be a RestServiceException", rootCause);
                assertEquals("Incorrect RestServiceErrorCode", RestServiceErrorCode.InvalidArgs, rootCause.getErrorCode());
            }
        } else {
            verifySuccessResponseOnTtlEnforcement(future, content, blobTtlSecs, restResponseChannel, true);
        }
    } else {
        verifySuccessResponseOnTtlEnforcement(future, content, blobTtlSecs, restResponseChannel, false);
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) 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) ExecutionException(java.util.concurrent.ExecutionException)

Example 85 with RestRequest

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

the class PostAccountsHandlerTest method sendRequestGetResponse.

// helpers
// general
/**
 * Sends a request to the {@link PostAccountsHandler} and waits for the response.
 * @param requestBody body of the request in string form.
 * @param restResponseChannel the {@link RestResponseChannel} where headers will be set.
 * @throws Exception
 */
private void sendRequestGetResponse(String requestBody, RestResponseChannel restResponseChannel) throws Exception {
    JSONObject data = new JSONObject();
    data.put(MockRestRequest.REST_METHOD_KEY, RestMethod.POST.name());
    data.put(MockRestRequest.URI_KEY, Operations.ACCOUNTS);
    List<ByteBuffer> body = new LinkedList<>();
    body.add(ByteBuffer.wrap(requestBody.getBytes(StandardCharsets.UTF_8)));
    body.add(null);
    RestRequest restRequest = new MockRestRequest(data, body);
    restRequest.setArg(RestUtils.InternalKeys.REQUEST_PATH, RequestPath.parse(restRequest, null, null));
    FutureResult<ReadableStreamChannel> future = new FutureResult<>();
    handler.handle(restRequest, restResponseChannel, future::done);
    try {
        future.get(1, TimeUnit.SECONDS);
    } catch (ExecutionException e) {
        throw e.getCause() instanceof Exception ? (Exception) e.getCause() : new Exception(e.getCause());
    }
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) FutureResult(com.github.ambry.router.FutureResult) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) MockRestRequest(com.github.ambry.rest.MockRestRequest) ExecutionException(java.util.concurrent.ExecutionException) ByteBuffer(java.nio.ByteBuffer) LinkedList(java.util.LinkedList) ExecutionException(java.util.concurrent.ExecutionException) RestServiceException(com.github.ambry.rest.RestServiceException)

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