Search in sources :

Example 6 with MockRestRequest

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

the class PostAccountContainersHandlerTest method createRestRequest.

/**
 * Creates a {@link RestRequest} for a /accounts/updateContainers request
 * @param requestBody body of the request in string form.
 * @param accountName if set, add this account name as a request header.
 * @param accountId if set, add this account ID as a request header.
 * @return the {@link RestRequest}
 * @throws Exception
 */
private RestRequest createRestRequest(String requestBody, String accountName, String accountId) throws Exception {
    JSONObject data = new JSONObject();
    data.put(MockRestRequest.REST_METHOD_KEY, RestMethod.POST.name());
    data.put(MockRestRequest.URI_KEY, Operations.ACCOUNTS_CONTAINERS);
    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);
    }
    data.put(MockRestRequest.HEADERS_KEY, headers);
    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));
    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) ByteBuffer(java.nio.ByteBuffer) LinkedList(java.util.LinkedList)

Example 7 with MockRestRequest

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

the class UndeleteHandlerTest method handleGoodCaseTest.

/**
 * Tests the case where undelete succeeds
 * @throws Exception
 */
@Test
public void handleGoodCaseTest() throws Exception {
    setupBlob();
    // 1. Test undelete a deleted blob
    RestRequest restRequest = new MockRestRequest(MockRestRequest.DUMMY_DATA, null);
    restRequest.setArg(RestUtils.Headers.BLOB_ID, blobId);
    restRequest.setArg(RestUtils.Headers.SERVICE_ID, SERVICE_ID);
    verifyUndelete(restRequest, REF_ACCOUNT, REF_CONTAINER);
    // 2. Test undelete it again
    router.deleteBlob(blobId, SERVICE_ID).get(1, TimeUnit.SECONDS);
    restRequest = new MockRestRequest(MockRestRequest.DUMMY_DATA, null);
    restRequest.setArg(RestUtils.Headers.BLOB_ID, blobId);
    restRequest.setArg(RestUtils.Headers.SERVICE_ID, SERVICE_ID);
    verifyUndelete(restRequest, REF_ACCOUNT, REF_CONTAINER);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) MockRestRequest(com.github.ambry.rest.MockRestRequest) Test(org.junit.Test)

Example 8 with MockRestRequest

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

the class UndeleteHandlerTest method badArgsTest.

/**
 * Tests for expected failures with bad arguments
 * @throws Exception
 */
private void badArgsTest() throws Exception {
    setupBlob();
    RestRequest restRequest = new MockRestRequest(MockRestRequest.DUMMY_DATA, null);
    restRequest.setArg(RestUtils.Headers.BLOB_ID, blobId);
    // no service ID
    verifyFailureWithErrorCode(restRequest, RestServiceErrorCode.MissingArgs);
    restRequest = new MockRestRequest(MockRestRequest.DUMMY_DATA, null);
    restRequest.setArg(RestUtils.Headers.SERVICE_ID, SERVICE_ID);
    // no blob ID
    verifyFailureWithErrorCode(restRequest, RestServiceErrorCode.MissingArgs);
    restRequest = new MockRestRequest(MockRestRequest.DUMMY_DATA, null);
    // not a valid blob ID
    restRequest.setArg(RestUtils.Headers.BLOB_ID, "abcd");
    idConverterFactory.translation = "abcd";
    restRequest.setArg(RestUtils.Headers.SERVICE_ID, SERVICE_ID);
    verifyFailureWithErrorCode(restRequest, RestServiceErrorCode.BadRequest);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) MockRestRequest(com.github.ambry.rest.MockRestRequest)

Example 9 with MockRestRequest

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

the class TailoredPeersClusterMap method getRestRequest.

/**
 * Get a {@link RestRequest} that requests for the peers of the given {@code datanode}
 * @param datanode the name of the datanode (host:port) whose peers are required.
 * @return a {@link RestRequest} that requests for the peers of the given {@code datanode}
 * @throws Exception
 */
private RestRequest getRestRequest(String datanode) throws Exception {
    String[] parts = datanode.split(":");
    JSONObject data = new JSONObject();
    data.put(MockRestRequest.REST_METHOD_KEY, RestMethod.GET.name());
    data.put(MockRestRequest.URI_KEY, Operations.GET_PEERS + "?" + GetPeersHandler.NAME_QUERY_PARAM + "=" + parts[0] + "&" + GetPeersHandler.PORT_QUERY_PARAM + "=" + parts[1]);
    return new MockRestRequest(data, null);
}
Also used : JSONObject(org.json.JSONObject) MockRestRequest(com.github.ambry.rest.MockRestRequest)

Example 10 with MockRestRequest

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

the class GetSignedUrlHandlerTest method handleGoodCaseTest.

/**
 * Handles the case where url signing succeeds
 * @throws Exception
 */
@Test
public void handleGoodCaseTest() throws Exception {
    urlSigningServiceFactory.signedUrlToReturn = TestUtils.getRandomString(10);
    // POST
    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());
    verifySigningUrl(restRequest, urlSigningServiceFactory.signedUrlToReturn, REF_ACCOUNT, REF_CONTAINER);
    idConverterFactory.translation = testBlobId.getID();
    // GET (also makes sure that the IDConverter is used)
    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.GET.name());
    // add a random string. IDConverter will convert it
    restRequest.setArg(RestUtils.Headers.BLOB_ID, TestUtils.getRandomString(10));
    verifySigningUrl(restRequest, urlSigningServiceFactory.signedUrlToReturn, REF_ACCOUNT, REF_CONTAINER);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) MockRestRequest(com.github.ambry.rest.MockRestRequest) Test(org.junit.Test)

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