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);
}
}
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));
}
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));
}
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);
}
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();
}
Aggregations