Search in sources :

Example 46 with RestRequest

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

the class FrontendTestUrlSigningServiceFactory method deleteBlobAndVerify.

/**
 * Deletes the blob with blob ID {@code blobId} and verifies the response returned.
 * @param blobId the blob ID of the blob to DELETE.
 * @throws Exception
 */
private void deleteBlobAndVerify(String blobId) throws Exception {
    RestRequest restRequest = createRestRequest(RestMethod.DELETE, blobId, null, null);
    verifyDeleteAccepted(restRequest);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest)

Example 47 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 input the input string
 * @param expectedOutput the expected output from the {@code idConverter}.
 * @throws Exception
 */
private void testConversion(IdConverter idConverter, RestMethod restMethod, String input, String expectedOutput) throws Exception {
    JSONObject requestData = new JSONObject();
    requestData.put(MockRestRequest.REST_METHOD_KEY, restMethod);
    requestData.put(MockRestRequest.URI_KEY, "/");
    RestRequest restRequest = new MockRestRequest(requestData, null);
    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 48 with RestRequest

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

the class AmbryUrlSigningServiceTest method verifySignedUrl.

/**
 * Verifies that a signed URL contains parameters as provided and passes verification.
 * @param signer the {@link AmbryUrlSigningService} to use.
 * @param url the signed URL.
 * @param restMethod the {@link RestMethod} intended by {@code url}.
 * @param randomHeaderVal the expected value of {@link #RANDOM_AMBRY_HEADER}.
 * @param maxUploadSize the expected value of {@link RestUtils.Headers#MAX_UPLOAD_SIZE}.
 * @throws Exception
 */
private void verifySignedUrl(AmbryUrlSigningService signer, String url, RestMethod restMethod, String randomHeaderVal, long maxUploadSize) throws Exception {
    RestRequest signedRequest = getRequestFromUrl(restMethod, url);
    assertTrue("Request should be declared as signed", signer.isRequestSigned(signedRequest));
    signer.verifySignedRequest(signedRequest);
    Map<String, Object> args = signedRequest.getArgs();
    assertEquals("URL type not as expected", restMethod.name(), args.get(RestUtils.Headers.URL_TYPE).toString());
    assertEquals("Random header value is not as expected", randomHeaderVal, args.get(RANDOM_AMBRY_HEADER).toString());
    if (restMethod.equals(RestMethod.POST)) {
        assertEquals("Max upload size not as expected", maxUploadSize, Long.parseLong(args.get(RestUtils.Headers.MAX_UPLOAD_SIZE).toString()));
    }
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject)

Example 49 with RestRequest

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

the class FrontendTestUrlSigningServiceFactory method doConditionalDeleteTest.

/**
 * Tests blob conditional DELETE operations on the given {@code container}.
 * @param toPostAccount the {@link Account} to use in post headers. Can be {@code null} if only using service ID.
 * @param toPostContainer the {@link Container} to use in post headers. Can be {@code null} if only using service ID.
 * @param serviceId the serviceId to use for the POST
 * @param isPrivate the isPrivate flag to pass as part of the POST
 * @param expectedAccount the {@link Account} details that are eventually expected to be populated.
 * @param expectedContainer the {@link Container} details that are eventually expected to be populated.
 * @throws Exception
 */
private void doConditionalDeleteTest(Account toPostAccount, Container toPostContainer, String serviceId, boolean isPrivate, Account expectedAccount, Container expectedContainer) throws Exception {
    final int CONTENT_LENGTH = 1024;
    ByteBuffer content = ByteBuffer.wrap(TestUtils.getRandomBytes(CONTENT_LENGTH));
    String contentType = "application/octet-stream";
    String ownerId = "postGetHeadDeleteOwnerID";
    JSONObject headers = new JSONObject();
    String accountNameInPost = toPostAccount != null ? toPostAccount.getName() : null;
    String containerNameInPost = toPostContainer != null ? toPostContainer.getName() : null;
    setAmbryHeadersForPut(headers, 7200, isPrivate, serviceId, contentType, ownerId, accountNameInPost, containerNameInPost);
    Map<String, String> userMetadata = new HashMap<>();
    userMetadata.put(RestUtils.Headers.USER_META_DATA_HEADER_PREFIX + "key1", "value1");
    userMetadata.put(RestUtils.Headers.USER_META_DATA_HEADER_PREFIX + "key2", "value2");
    RestUtilsTest.setUserMetadataHeaders(headers, userMetadata);
    // perform POST, GET, HEAD successfully before DELETE
    String blobId = postBlobAndVerify(headers, content, expectedAccount, expectedContainer);
    headers.put(RestUtils.Headers.BLOB_SIZE, (long) CONTENT_LENGTH);
    getBlobAndVerify(blobId, null, null, headers, content, expectedAccount, expectedContainer);
    getHeadAndVerify(blobId, null, null, headers, expectedAccount, expectedContainer);
    // test Conditional Delete failure because only container name is set
    RestResponseChannel restResponseChannel = new MockRestResponseChannel();
    JSONObject headers2 = new JSONObject();
    setAccountAndContainerHeaders(headers2, null, containerNameInPost);
    RestRequest restRequest = createRestRequest(RestMethod.DELETE, blobId, headers2, null);
    try {
        doOperation(restRequest, restResponseChannel);
        fail("Operation should have failed because only container name is set");
    } catch (RestServiceException e) {
        assertEquals("AmbryBlobStorageService should have thrown a BadRequest exception", RestServiceErrorCode.BadRequest, e.getErrorCode());
    }
    // test Conditional Delete failure because of incorrect account name
    restResponseChannel = new MockRestResponseChannel();
    setAccountAndContainerHeaders(headers, "INCORRECT_ACCOUNT_NAME", containerNameInPost);
    restRequest = createRestRequest(RestMethod.DELETE, blobId, headers, null);
    try {
        doOperation(restRequest, restResponseChannel);
        fail("Operation should have failed because incorrect account name");
    } catch (RestServiceException e) {
        assertEquals("AmbryBlobStorageService should have thrown a PreconditionFailed exception", RestServiceErrorCode.PreconditionFailed, e.getErrorCode());
    }
    // test Conditional Delete failure because of incorrect container name
    restResponseChannel = new MockRestResponseChannel();
    setAccountAndContainerHeaders(headers, accountNameInPost, "INCORRECT_CONTAINER_NAME");
    restRequest = createRestRequest(RestMethod.DELETE, blobId, headers, null);
    try {
        doOperation(restRequest, restResponseChannel);
        fail("Operation should have failed because incorrect container name");
    } catch (RestServiceException e) {
        assertEquals("AmbryBlobStorageService should have thrown a PreconditionFailed exception", RestServiceErrorCode.PreconditionFailed, e.getErrorCode());
    }
    // test Conditional Delete succeeds
    setAccountAndContainerHeaders(headers, accountNameInPost, containerNameInPost);
    restRequest = createRestRequest(RestMethod.DELETE, blobId, headers, null);
    verifyDeleteAccepted(restRequest);
    // check GET, HEAD and DELETE after delete.
    verifyOperationsAfterDelete(blobId, headers, content, expectedAccount, expectedContainer);
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) ByteBuffer(java.nio.ByteBuffer)

Example 50 with RestRequest

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

the class PutBlobOptionsTest method testOptions.

/**
 * Test that the chunk upload and max size options can be assigned and retrieved correctly.
 * @throws Exception
 */
@Test
public void testOptions() throws Exception {
    PutBlobOptions options = new PutBlobOptionsBuilder().chunkUpload(true).build();
    assertTrue("chunkUpload from options not as expected.", options.isChunkUpload());
    assertEquals("maxUploadSize from options not as expected.", Long.MAX_VALUE, options.getMaxUploadSize());
    options = new PutBlobOptionsBuilder().chunkUpload(false).maxUploadSize(3).build();
    assertFalse("chunkUpload from options not as expected.", options.isChunkUpload());
    assertEquals("maxUploadSize from options not as expected.", 3, options.getMaxUploadSize());
    JSONObject header = new JSONObject();
    header.put(MockRestRequest.URI_KEY, "/");
    header.put(MockRestRequest.REST_METHOD_KEY, RestMethod.GET.name());
    RestRequest restRequest = new MockRestRequest(header, null);
    options = new PutBlobOptionsBuilder().restRequest(restRequest).build();
    assertEquals("RestRequest mismatch", restRequest, options.getRestRequest());
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) MockRestRequest(com.github.ambry.rest.MockRestRequest) Test(org.junit.Test)

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