Search in sources :

Example 51 with RestRequest

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

the class FrontendTestUrlSigningServiceFactory method updateBlobTtlAndVerify.

/**
 * Updates the TTL of the blob with blob ID {@code blobId} and verifies that the operation succeeded.
 * @param blobId the blob ID of the blob to HEAD.
 * @param expectedHeaders the expected headers in the GET response triggered for verification.
 * @param expectedAccount the expected account in the GET response triggered for verification. Also used to attach
 *                        preconditions if required
 * @param expectedContainer the expected container in the GET response triggered for verification. Also used to attach
 *                        preconditions if required
 * @param attachPreconditions if {@code true}, attaches preconditions to the request
 * @throws Exception
 */
private void updateBlobTtlAndVerify(String blobId, JSONObject expectedHeaders, Account expectedAccount, Container expectedContainer, boolean attachPreconditions) throws Exception {
    JSONObject headers = new JSONObject();
    setUpdateTtlHeaders(headers, blobId, "updateBlobTtlAndVerify");
    if (attachPreconditions) {
        setAccountAndContainerHeaders(headers, expectedAccount.getName(), expectedContainer.getName());
    }
    RestRequest restRequest = createRestRequest(RestMethod.PUT, Operations.UPDATE_TTL, headers, null);
    verifyUpdateBlobTtlResponse(restRequest);
    expectedHeaders.remove(RestUtils.Headers.TTL);
    getBlobInfoAndVerify(blobId, GetOption.None, expectedHeaders, expectedAccount, expectedContainer);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject)

Example 52 with RestRequest

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

the class FrontendTestUrlSigningServiceFactory method oldStyleUserMetadataTest.

/**
 * Tests how metadata that has not been POSTed in the form of headers is returned.
 * @throws Exception
 */
@Test
public void oldStyleUserMetadataTest() throws Exception {
    ByteBuffer content = ByteBuffer.allocate(0);
    BlobProperties blobProperties = new BlobProperties(0, "userMetadataTestOldStyleServiceID", Account.UNKNOWN_ACCOUNT_ID, Container.UNKNOWN_CONTAINER_ID, false);
    byte[] usermetadata = TestUtils.getRandomBytes(25);
    String blobId = router.putBlob(blobProperties, usermetadata, new ByteBufferReadableStreamChannel(content), new PutBlobOptionsBuilder().build()).get();
    RestUtils.SubResource[] subResources = { RestUtils.SubResource.UserMetadata, RestUtils.SubResource.BlobInfo };
    for (RestUtils.SubResource subResource : subResources) {
        RestRequest restRequest = createRestRequest(RestMethod.GET, blobId + "/" + subResource, null, null);
        MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
        doOperation(restRequest, restResponseChannel);
        assertEquals("Unexpected response status for " + subResource, ResponseStatus.Ok, restResponseChannel.getStatus());
        assertEquals("Unexpected Content-Type for " + subResource, "application/octet-stream", restResponseChannel.getHeader(RestUtils.Headers.CONTENT_TYPE));
        assertEquals("Unexpected Content-Length for " + subResource, usermetadata.length, Integer.parseInt(restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
        assertArrayEquals("Unexpected user metadata for " + subResource, usermetadata, restResponseChannel.getResponseBody());
    }
}
Also used : ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) PutBlobOptionsBuilder(com.github.ambry.router.PutBlobOptionsBuilder) BlobProperties(com.github.ambry.messageformat.BlobProperties) RestUtils(com.github.ambry.rest.RestUtils) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) RestUtilsTest(com.github.ambry.rest.RestUtilsTest) StorageStatsUtilTest(com.github.ambry.server.StorageStatsUtilTest)

Example 53 with RestRequest

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

the class FrontendTestUrlSigningServiceFactory method getBlobInfoAndVerify.

/**
 * Gets the blob info of the blob with blob ID {@code blobId} and verifies them against what is expected.
 * @param blobId the blob ID of the blob to HEAD.
 * @param getOption the options to use while getting the blob.
 * @param expectedHeaders the expected headers in the response.
 * @param expectedAccount the expected account in the rest request.
 * @param expectedContainer the expected container in the rest request.
 * @throws Exception
 */
private void getBlobInfoAndVerify(String blobId, GetOption getOption, JSONObject expectedHeaders, Account expectedAccount, Container expectedContainer) throws Exception {
    JSONObject headers = new JSONObject();
    if (getOption != null) {
        headers.put(RestUtils.Headers.GET_OPTION, getOption.toString());
    }
    RestRequest restRequest = createRestRequest(RestMethod.GET, blobId + "/" + RestUtils.SubResource.BlobInfo, headers, null);
    MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
    doOperation(restRequest, restResponseChannel);
    assertEquals("Unexpected response status", ResponseStatus.Ok, restResponseChannel.getStatus());
    checkCommonGetHeadHeaders(restResponseChannel);
    assertEquals("Content-Length is not 0", "0", restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH));
    assertNull("Accept-Ranges should not be set", restResponseChannel.getHeader(RestUtils.Headers.ACCEPT_RANGES));
    assertNull("Content-Range header should not be set", restResponseChannel.getHeader(RestUtils.Headers.CONTENT_RANGE));
    verifyBlobProperties(expectedHeaders, restResponseChannel);
    verifyUserMetadataHeaders(expectedHeaders, restResponseChannel);
    verifyAccountAndContainerHeaders(restResponseChannel, expectedAccount, expectedContainer);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel)

Example 54 with RestRequest

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

the class FrontendTestUrlSigningServiceFactory method getPeersTest.

/**
 * Tests the handling of {@link Operations#GET_PEERS} requests.
 * @throws Exception
 */
@Test
public void getPeersTest() throws Exception {
    frontendRestRequestService.shutdown();
    TailoredPeersClusterMap clusterMap = new TailoredPeersClusterMap();
    frontendRestRequestService = new FrontendRestRequestService(frontendConfig, frontendMetrics, router, clusterMap, idConverterFactory, securityServiceFactory, urlSigningService, idSigningService, null, accountService, accountAndContainerInjector, datacenterName, hostname, clusterName, accountStatsStore, QUOTA_MANAGER);
    frontendRestRequestService.setupResponseHandler(responseHandler);
    frontendRestRequestService.start();
    // test good requests
    for (String datanode : TailoredPeersClusterMap.DATANODE_NAMES) {
        String[] parts = datanode.split(":");
        String baseUri = Operations.GET_PEERS + "?" + GetPeersHandler.NAME_QUERY_PARAM + "=" + parts[0] + "&" + GetPeersHandler.PORT_QUERY_PARAM + "=" + parts[1];
        String[] uris = { baseUri, "/" + baseUri };
        for (String uri : uris) {
            MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
            doOperation(createRestRequest(RestMethod.GET, uri, null, null), restResponseChannel);
            byte[] peerStrBytes = restResponseChannel.getResponseBody();
            Set<String> peersFromResponse = GetPeersHandlerTest.getPeersFromResponse(new JSONObject(new String(peerStrBytes)));
            Set<String> expectedPeers = clusterMap.getPeers(datanode);
            assertEquals("Peer list returned does not match expected for " + datanode, expectedPeers, peersFromResponse);
        }
    }
    // test one bad request
    RestRequest restRequest = createRestRequest(RestMethod.GET, Operations.GET_PEERS, null, null);
    verifyOperationFailure(restRequest, RestServiceErrorCode.MissingArgs);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Test(org.junit.Test) RestUtilsTest(com.github.ambry.rest.RestUtilsTest) StorageStatsUtilTest(com.github.ambry.server.StorageStatsUtilTest)

Example 55 with RestRequest

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

the class FrontendTestUrlSigningServiceFactory method getBlobAndVerify.

/**
 * Gets the blob with blob ID {@code blobId} and verifies that the headers and content match with what is expected.
 * @param blobId the blob ID of the blob to GET.
 * @param range the optional {@link ByteRange} for the request.
 * @param getOption the options to use while getting the blob.
 * @param expectedHeaders the expected headers in the response.
 * @param expectedContent the expected content of the blob.
 * @param expectedAccount the expected account in the rest request.
 * @param expectedContainer the expected container in the rest request.
 * @throws Exception
 */
private void getBlobAndVerify(String blobId, ByteRange range, GetOption getOption, JSONObject expectedHeaders, ByteBuffer expectedContent, Account expectedAccount, Container expectedContainer) throws Exception {
    RestRequest restRequest = createRestRequest(RestMethod.GET, blobId, createRequestHeaders(range, getOption), null);
    MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
    doOperation(restRequest, restResponseChannel);
    verifyGetBlobResponse(restRequest, restResponseChannel, range, expectedHeaders, expectedContent, expectedAccount, expectedContainer);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel)

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