Search in sources :

Example 11 with ResponseParts

use of com.github.ambry.rest.NettyClient.ResponseParts in project ambry by linkedin.

the class FrontendIntegrationTest method getReplicasTest.

/**
 * Tests {@link RestUtils.SubResource#Replicas} requests
 * <p/>
 * For each {@link PartitionId} in the {@link ClusterMap}, a {@link BlobId} is created. The replica list returned from
 * server is checked for equality against a locally obtained replica list.
 * @throws Exception
 */
@Test
public void getReplicasTest() throws Exception {
    List<? extends PartitionId> partitionIds = CLUSTER_MAP.getWritablePartitionIds(null);
    for (PartitionId partitionId : partitionIds) {
        String originalReplicaStr = partitionId.getReplicaIds().toString().replace(", ", ",");
        BlobId blobId = new BlobId(CommonTestUtils.getCurrentBlobIdVersion(), BlobId.BlobIdType.NATIVE, ClusterMap.UNKNOWN_DATACENTER_ID, Account.UNKNOWN_ACCOUNT_ID, Container.UNKNOWN_CONTAINER_ID, partitionId, false, BlobId.BlobDataType.DATACHUNK);
        FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, blobId.getID() + "/" + RestUtils.SubResource.Replicas, Unpooled.buffer(0));
        ResponseParts responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
        HttpResponse response = getHttpResponse(responseParts);
        assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
        verifyTrackingHeaders(response);
        ByteBuffer content = getContent(responseParts.queue, HttpUtil.getContentLength(response));
        JSONObject responseJson = new JSONObject(new String(content.array()));
        String returnedReplicasStr = responseJson.get(GetReplicasHandler.REPLICAS_KEY).toString().replace("\"", "");
        assertEquals("Replica IDs returned for the BlobId do no match with the replicas IDs of partition", originalReplicaStr, returnedReplicasStr);
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) JSONObject(org.json.JSONObject) HttpResponse(io.netty.handler.codec.http.HttpResponse) ResponseParts(com.github.ambry.rest.NettyClient.ResponseParts) PartitionId(com.github.ambry.clustermap.PartitionId) BlobId(com.github.ambry.commons.BlobId) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 12 with ResponseParts

use of com.github.ambry.rest.NettyClient.ResponseParts in project ambry by linkedin.

the class FrontendIntegrationTest method getHeadAndVerify.

/**
 * Gets the headers 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 range the {@link ByteRange} for the request.
 * @param getOption the options to use while getting the blob.
 * @param expectedHeaders the expected headers in the response.
 * @param isPrivate {@code true} if the blob is expected to be private
 * @param accountName the expected account name in the response.
 * @param containerName the expected container name in the response.
 * @throws ExecutionException
 * @throws InterruptedException
 */
private void getHeadAndVerify(String blobId, ByteRange range, GetOption getOption, HttpHeaders expectedHeaders, boolean isPrivate, String accountName, String containerName) throws ExecutionException, InterruptedException, RestServiceException {
    HttpHeaders headers = new DefaultHttpHeaders();
    if (range != null) {
        headers.add(RestUtils.Headers.RANGE, RestTestUtils.getRangeHeaderString(range));
    }
    if (getOption != null) {
        headers.add(RestUtils.Headers.GET_OPTION, getOption.toString());
    }
    FullHttpRequest httpRequest = buildRequest(HttpMethod.HEAD, blobId, headers, null);
    ResponseParts responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
    HttpResponse response = getHttpResponse(responseParts);
    assertEquals("Unexpected response status", range == null ? HttpResponseStatus.OK : HttpResponseStatus.PARTIAL_CONTENT, response.status());
    checkCommonGetHeadHeaders(response.headers());
    long contentLength = Long.parseLong(expectedHeaders.get(RestUtils.Headers.BLOB_SIZE));
    if (range != null) {
        Pair<String, Long> rangeAndLength = RestUtils.buildContentRangeAndLength(range, contentLength);
        assertEquals("Content-Range header not set correctly", rangeAndLength.getFirst(), response.headers().get(RestUtils.Headers.CONTENT_RANGE));
        contentLength = rangeAndLength.getSecond();
    } else {
        assertNull("Content-Range header should not be set", response.headers().get(RestUtils.Headers.CONTENT_RANGE));
    }
    assertEquals("Accept-Ranges not set correctly", "bytes", response.headers().get(RestUtils.Headers.ACCEPT_RANGES));
    assertEquals(RestUtils.Headers.CONTENT_LENGTH + " does not match expected", contentLength, HttpUtil.getContentLength(response));
    assertEquals(RestUtils.Headers.CONTENT_TYPE + " does not match " + RestUtils.Headers.AMBRY_CONTENT_TYPE, expectedHeaders.get(RestUtils.Headers.AMBRY_CONTENT_TYPE), response.headers().get(HttpHeaderNames.CONTENT_TYPE));
    verifyBlobProperties(expectedHeaders, isPrivate, response);
    verifyAccountAndContainerHeaders(accountName, containerName, response);
    discardContent(responseParts.queue, 1);
    assertTrue("Channel should be active", HttpUtil.isKeepAlive(response));
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpResponse(io.netty.handler.codec.http.HttpResponse) ResponseParts(com.github.ambry.rest.NettyClient.ResponseParts)

Example 13 with ResponseParts

use of com.github.ambry.rest.NettyClient.ResponseParts in project ambry by linkedin.

the class FrontendIntegrationTest method verifyDeleted.

/**
 * Verifies that a request returns the right response code  once the blob has been deleted.
 * @param httpRequest the {@link FullHttpRequest} to send to the server.
 * @param expectedStatusCode the expected {@link HttpResponseStatus}.
 * @throws ExecutionException
 * @throws InterruptedException
 */
private void verifyDeleted(FullHttpRequest httpRequest, HttpResponseStatus expectedStatusCode) throws ExecutionException, InterruptedException {
    ResponseParts responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
    HttpResponse response = getHttpResponse(responseParts);
    assertEquals("Unexpected response status", expectedStatusCode, response.status());
    assertTrue("No Date header", response.headers().get(HttpHeaderNames.DATE, null) != null);
    discardContent(responseParts.queue, 1);
    assertTrue("Channel should be active", HttpUtil.isKeepAlive(response));
}
Also used : HttpResponse(io.netty.handler.codec.http.HttpResponse) ResponseParts(com.github.ambry.rest.NettyClient.ResponseParts)

Example 14 with ResponseParts

use of com.github.ambry.rest.NettyClient.ResponseParts in project ambry by linkedin.

the class FrontendIntegrationTest 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 {@link ByteRange} for the request.
 * @param getOption the options to use while getting the blob.
 * @param expectedHeaders the expected headers in the response.
 * @param isPrivate {@code true} if the blob is private, {@code false} if not.
 * @param expectedContent the expected content of the blob.
 * @throws ExecutionException
 * @throws InterruptedException
 */
private void getBlobAndVerify(String blobId, ByteRange range, GetOption getOption, HttpHeaders expectedHeaders, boolean isPrivate, ByteBuffer expectedContent) throws ExecutionException, InterruptedException, RestServiceException {
    HttpHeaders headers = new DefaultHttpHeaders();
    if (range != null) {
        headers.add(RestUtils.Headers.RANGE, RestTestUtils.getRangeHeaderString(range));
    }
    if (getOption != null) {
        headers.add(RestUtils.Headers.GET_OPTION, getOption.toString());
    }
    FullHttpRequest httpRequest = buildRequest(HttpMethod.GET, blobId, headers, null);
    ResponseParts responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
    verifyGetBlobResponse(responseParts, range, expectedHeaders, isPrivate, expectedContent);
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) ResponseParts(com.github.ambry.rest.NettyClient.ResponseParts)

Example 15 with ResponseParts

use of com.github.ambry.rest.NettyClient.ResponseParts in project ambry by linkedin.

the class FrontendIntegrationTest method getContainer.

/**
 * Get a container from given account.
 * @param accountName name of account which container belongs to.
 * @param containerName name of container
 * @return the requested container.
 * @throws Exception
 */
private Container getContainer(String accountName, String containerName) throws Exception {
    HttpHeaders headers = new DefaultHttpHeaders();
    headers.add(RestUtils.Headers.TARGET_ACCOUNT_NAME, accountName);
    headers.add(RestUtils.Headers.TARGET_CONTAINER_NAME, containerName);
    FullHttpRequest request = buildRequest(HttpMethod.GET, Operations.ACCOUNTS_CONTAINERS, headers, null);
    ResponseParts responseParts = nettyClient.sendRequest(request, null, null).get();
    HttpResponse response = getHttpResponse(responseParts);
    assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
    verifyTrackingHeaders(response);
    short accountId = Short.parseShort(response.headers().get(RestUtils.Headers.TARGET_ACCOUNT_ID));
    ByteBuffer content = getContent(responseParts.queue, HttpUtil.getContentLength(response));
    return AccountCollectionSerde.containersFromInputStreamInJson(new ByteArrayInputStream(content.array()), accountId).iterator().next();
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpResponse(io.netty.handler.codec.http.HttpResponse) ResponseParts(com.github.ambry.rest.NettyClient.ResponseParts) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ResponseParts (com.github.ambry.rest.NettyClient.ResponseParts)22 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)21 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)21 HttpResponse (io.netty.handler.codec.http.HttpResponse)18 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)15 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)15 ByteBuffer (java.nio.ByteBuffer)11 Test (org.junit.Test)9 Container (com.github.ambry.account.Container)5 HttpRequest (io.netty.handler.codec.http.HttpRequest)5 Account (com.github.ambry.account.Account)4 HttpPostRequestEncoder (io.netty.handler.codec.http.multipart.HttpPostRequestEncoder)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 NettyConfig (com.github.ambry.config.NettyConfig)2 ByteRange (com.github.ambry.router.ByteRange)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 JSONObject (org.json.JSONObject)2 ClusterMap (com.github.ambry.clustermap.ClusterMap)1 MockClusterMap (com.github.ambry.clustermap.MockClusterMap)1