Search in sources :

Example 16 with MockRestRequest

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

the class NoSizeRSC method readPartByPartTest.

/**
 * Tests reading {@link ReadableStreamChannelInputStream} part by part.
 * @param in the data that the {@link ReadableStreamChannelInputStream} should contain.
 * @throws Exception
 */
private void readPartByPartTest(byte[] in) throws Exception {
    // channel with size and one piece of content.
    ReadableStreamChannel channel = new ByteBufferReadableStreamChannel(ByteBuffer.wrap(in));
    InputStream stream = new ReadableStreamChannelInputStream(channel);
    doReadPartByPartTest(stream, in);
    stream.close();
    // channel with no size but one piece of content.
    channel = new NoSizeRSC(ByteBuffer.wrap(in));
    stream = new ReadableStreamChannelInputStream(channel);
    doReadPartByPartTest(stream, in);
    stream.close();
    // channel with no size and multiple pieces of content.
    List<ByteBuffer> contents = splitContent(in, CONTENT_SPLIT_PART_COUNT);
    contents.add(null);
    channel = new MockRestRequest(MockRestRequest.DUMMY_DATA, contents);
    stream = new ReadableStreamChannelInputStream(channel);
    doReadPartByPartTest(stream, in);
    stream.close();
}
Also used : ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) InputStream(java.io.InputStream) MockRestRequest(com.github.ambry.rest.MockRestRequest) ByteBuffer(java.nio.ByteBuffer)

Example 17 with MockRestRequest

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

the class FrontendTestUrlSigningServiceFactory method createRestRequest.

// helpers
// general
/**
 * Method to easily create {@link RestRequest} objects containing a specific request.
 * @param restMethod the {@link RestMethod} desired.
 * @param uri string representation of the desired URI.
 * @param headers any associated headers as a {@link JSONObject}.
 * @param contents the content that accompanies the request.
 * @return A {@link RestRequest} object that defines the request required by the input.
 * @throws JSONException
 * @throws UnsupportedEncodingException
 * @throws URISyntaxException
 */
static RestRequest createRestRequest(RestMethod restMethod, String uri, JSONObject headers, List<ByteBuffer> contents) throws JSONException, UnsupportedEncodingException, URISyntaxException {
    JSONObject request = new JSONObject();
    request.put(MockRestRequest.REST_METHOD_KEY, restMethod.name());
    request.put(MockRestRequest.URI_KEY, uri);
    if (headers != null) {
        request.put(MockRestRequest.HEADERS_KEY, headers);
    }
    return new MockRestRequest(request, contents);
}
Also used : JSONObject(org.json.JSONObject) MockRestRequest(com.github.ambry.rest.MockRestRequest)

Example 18 with MockRestRequest

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

the class AmbrySecurityServiceTest method createRestRequest.

/**
 * Method to easily create {@link RestRequest} objects containing a specific request.
 * @param restMethod the {@link RestMethod} desired.
 * @param uri string representation of the desired URI.
 * @param headers any associated headers as a {@link JSONObject}.
 * @return A {@link RestRequest} object that defines the request required by the input.
 * @throws org.json.JSONException
 * @throws java.io.UnsupportedEncodingException
 * @throws java.net.URISyntaxException
 */
private RestRequest createRestRequest(RestMethod restMethod, String uri, JSONObject headers) throws JSONException, UnsupportedEncodingException, URISyntaxException, RestServiceException {
    JSONObject request = new JSONObject();
    request.put(MockRestRequest.REST_METHOD_KEY, restMethod.name());
    request.put(MockRestRequest.URI_KEY, uri);
    if (headers != null) {
        request.put(MockRestRequest.HEADERS_KEY, headers);
    }
    RestRequest restRequest = new MockRestRequest(request, null);
    restRequest.setArg(RestUtils.InternalKeys.REQUEST_PATH, RequestPath.parse(restRequest, FRONTEND_CONFIG.pathPrefixesToRemove, CLUSTER_NAME));
    return restRequest;
}
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 19 with MockRestRequest

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

the class GetAccountsHandlerTest method createRestRequest.

// helpers
// general
/**
 * Creates a {@link RestRequest} for a GET /accounts or /accounts/containers request
 * @param accountName if set, add this account name as a request header.
 * @param accountId if set, add this account ID as a request header.
 * @param containerName if set, add this container name as request header.
 * @param operation the operation this request will perform.
 * @return the {@link RestRequest}
 * @throws Exception
 */
private RestRequest createRestRequest(String accountName, String accountId, String containerName, String operation) throws Exception {
    JSONObject data = new JSONObject();
    data.put(MockRestRequest.REST_METHOD_KEY, RestMethod.GET.name());
    data.put(MockRestRequest.URI_KEY, operation);
    JSONObject headers = new JSONObject();
    if (accountName != null) {
        headers.put(RestUtils.Headers.TARGET_ACCOUNT_NAME, accountName);
    }
    if (accountId != null) {
        headers.put(RestUtils.Headers.TARGET_ACCOUNT_ID, accountId);
    }
    if (containerName != null) {
        headers.put(RestUtils.Headers.TARGET_CONTAINER_NAME, containerName);
    }
    data.put(MockRestRequest.HEADERS_KEY, headers);
    RestRequest restRequest = new MockRestRequest(data, null);
    restRequest.setArg(RestUtils.InternalKeys.REQUEST_PATH, RequestPath.parse(restRequest, null, null));
    return restRequest;
}
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 20 with MockRestRequest

use of com.github.ambry.rest.MockRestRequest 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)

Aggregations

MockRestRequest (com.github.ambry.rest.MockRestRequest)34 JSONObject (org.json.JSONObject)20 RestRequest (com.github.ambry.rest.RestRequest)18 RestServiceException (com.github.ambry.rest.RestServiceException)7 Test (org.junit.Test)7 ByteBuffer (java.nio.ByteBuffer)6 ReadableStreamChannel (com.github.ambry.router.ReadableStreamChannel)5 MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)4 InputStream (java.io.InputStream)4 IOException (java.io.IOException)3 ExecutionException (java.util.concurrent.ExecutionException)3 MetricRegistry (com.codahale.metrics.MetricRegistry)2 VerifiableProperties (com.github.ambry.config.VerifiableProperties)2 BlobProperties (com.github.ambry.messageformat.BlobProperties)2 LinkedList (java.util.LinkedList)2 Properties (java.util.Properties)2 Callback (com.github.ambry.commons.Callback)1 ServerMetrics (com.github.ambry.commons.ServerMetrics)1 ServerConfig (com.github.ambry.config.ServerConfig)1 BlobInfo (com.github.ambry.messageformat.BlobInfo)1