use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method postAccountsTest.
/**
* Tests the handling of {@link Operations#ACCOUNTS} post requests.
* @throws Exception
*/
@Test
public void postAccountsTest() throws Exception {
Account accountToAdd = accountService.generateRandomAccount();
List<ByteBuffer> body = new LinkedList<>();
body.add(ByteBuffer.wrap(AccountCollectionSerde.serializeAccountsInJson(Collections.singleton(accountToAdd))));
body.add(null);
RestRequest restRequest = createRestRequest(RestMethod.POST, Operations.ACCOUNTS, null, body);
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
doOperation(restRequest, restResponseChannel);
assertEquals("Account not created correctly", accountToAdd, accountService.getAccountById(accountToAdd.getId()));
// test an invalid request case to ensure that it goes through the exception path
body = new LinkedList<>();
body.add(ByteBuffer.wrap("abcdefghijk".toString().getBytes(StandardCharsets.UTF_8)));
body.add(null);
restRequest = createRestRequest(RestMethod.POST, Operations.ACCOUNTS, null, body);
try {
doOperation(restRequest, new MockRestResponseChannel());
fail("Operation should have failed");
} catch (RestServiceException e) {
assertEquals("Error code not as expected", RestServiceErrorCode.BadRequest, e.getErrorCode());
}
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory 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 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 expectedAccount the expected account in the rest request.
* @param expectedContainer the expected container in the rest request.
* @throws Exception
*/
private void getHeadAndVerify(String blobId, ByteRange range, GetOption getOption, JSONObject expectedHeaders, Account expectedAccount, Container expectedContainer) throws Exception {
RestRequest restRequest = createRestRequest(RestMethod.HEAD, blobId, createRequestHeaders(range, getOption), null);
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
doOperation(restRequest, restResponseChannel);
assertEquals("Unexpected response status", range == null ? ResponseStatus.Ok : ResponseStatus.PartialContent, restResponseChannel.getStatus());
checkCommonGetHeadHeaders(restResponseChannel);
assertEquals(RestUtils.Headers.CONTENT_TYPE + " does not match " + RestUtils.Headers.AMBRY_CONTENT_TYPE, expectedHeaders.getString(RestUtils.Headers.AMBRY_CONTENT_TYPE), restResponseChannel.getHeader(RestUtils.Headers.CONTENT_TYPE));
assertEquals("Accept-Ranges not set correctly", "bytes", restResponseChannel.getHeader(RestUtils.Headers.ACCEPT_RANGES));
long contentLength = expectedHeaders.getLong(RestUtils.Headers.BLOB_SIZE);
if (range != null) {
Pair<String, Long> rangeAndLength = RestUtils.buildContentRangeAndLength(range, contentLength, false);
assertEquals("Content-Range does not match expected", rangeAndLength.getFirst(), restResponseChannel.getHeader(RestUtils.Headers.CONTENT_RANGE));
contentLength = rangeAndLength.getSecond();
} else {
assertNull("Content-Range header should not be set", restResponseChannel.getHeader(RestUtils.Headers.CONTENT_RANGE));
}
assertEquals(RestUtils.Headers.CONTENT_LENGTH + " does not match expected", Long.toString(contentLength), restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH));
verifyBlobProperties(expectedHeaders, restResponseChannel);
verifyAccountAndContainerHeaders(restResponseChannel, expectedAccount, expectedContainer);
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method getClusterMapSnapshotTest.
/**
* Tests the handling of {@link Operations#GET_CLUSTER_MAP_SNAPSHOT} requests.
* @throws Exception
*/
@Test
public void getClusterMapSnapshotTest() throws Exception {
RestRequest restRequest = createRestRequest(RestMethod.GET, Operations.GET_CLUSTER_MAP_SNAPSHOT, null, null);
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
doOperation(restRequest, restResponseChannel);
JSONObject expected = clusterMap.getSnapshot();
JSONObject actual = new JSONObject(new String(restResponseChannel.getResponseBody()));
// remove timestamps because they may differ
expected.remove(ClusterMapSnapshotConstants.TIMESTAMP_MS);
actual.remove(ClusterMapSnapshotConstants.TIMESTAMP_MS);
assertEquals("Snapshot does not match expected", expected.toString(), actual.toString());
// test a failure to ensure that it goes through the exception path
String msg = TestUtils.getRandomString(10);
clusterMap.setExceptionOnSnapshot(new RuntimeException(msg));
restRequest = createRestRequest(RestMethod.GET, Operations.GET_CLUSTER_MAP_SNAPSHOT, null, null);
try {
doOperation(restRequest, new MockRestResponseChannel());
fail("Operation should have failed");
} catch (RuntimeException e) {
assertEquals("Exception not as expected", msg, e.getMessage());
}
clusterMap.setExceptionOnSnapshot(null);
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method getReplicasTest.
/**
* Tests {@link GetReplicasHandler#getReplicas(String, RestResponseChannel)}
* <p/>
* For each {@link PartitionId} in the {@link ClusterMap}, a {@link BlobId} is created. The replica list returned from
* {@link GetReplicasHandler#getReplicas(String, RestResponseChannel)}is checked for equality against a locally
* obtained replica list.
* @throws Exception
*/
@Test
public void getReplicasTest() throws Exception {
List<? extends PartitionId> partitionIds = clusterMap.getWritablePartitionIds(null);
for (PartitionId partitionId : partitionIds) {
String originalReplicaStr = partitionId.getReplicaIds().toString().replace(", ", ",");
BlobId blobId = new BlobId(blobIdVersion, BlobId.BlobIdType.NATIVE, ClusterMap.UNKNOWN_DATACENTER_ID, Account.UNKNOWN_ACCOUNT_ID, Container.UNKNOWN_CONTAINER_ID, partitionId, false, BlobId.BlobDataType.DATACHUNK);
RestRequest restRequest = createRestRequest(RestMethod.GET, blobId.getID() + "/" + RestUtils.SubResource.Replicas, null, null);
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
doOperation(restRequest, restResponseChannel);
JSONObject response = new JSONObject(new String(restResponseChannel.getResponseBody()));
String returnedReplicasStr = response.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.RestRequest in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method getUserMetadataAndVerify.
/**
* Gets the user metadata 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.
* @throws Exception
*/
private void getUserMetadataAndVerify(String blobId, GetOption getOption, JSONObject expectedHeaders) 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.UserMetadata, 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));
verifyUserMetadataHeaders(expectedHeaders, restResponseChannel);
}
Aggregations