use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method badRangeHeaderTest.
/**
* Test that GET operations fail with the expected error code when a bad range header is provided.
* @throws Exception
*/
@Test
public void badRangeHeaderTest() throws Exception {
JSONObject headers = new JSONObject();
headers.put(RestUtils.Headers.RANGE, "adsfksakdfsdfkdaklf");
try {
doOperation(createRestRequest(RestMethod.GET, "/", headers, null), new MockRestResponseChannel());
fail("GET operation should have failed because of an invalid range header");
} catch (RestServiceException e) {
assertEquals("Unexpected error code", RestServiceErrorCode.InvalidArgs, e.getErrorCode());
}
}
use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method putRequestWithProhibitedHeader.
/**
* Put with prohibited headers.
* @param header The header that is prohibited.
* @throws Exception
*/
private void putRequestWithProhibitedHeader(String header) throws Exception {
JSONObject headers = new JSONObject();
setAmbryHeadersForPut(headers, 7200, true, "someServiceId", "application/octet-stream", "someOwnerId", "someAccountName", "someContainerName");
headers.put(header, "adsfksakdfsdfkdaklf");
try {
doOperation(createRestRequest(RestMethod.POST, "/", headers, null), new MockRestResponseChannel());
fail("Should have thrown");
} catch (RestServiceException e) {
assertEquals("Unexpected error code", RestServiceErrorCode.BadRequest, e.getErrorCode());
}
}
use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method useServiceWithoutStartTest.
/**
* This tests for exceptions thrown when an {@link AmbryBlobStorageService} instance is used without calling
* {@link AmbryBlobStorageService#start()} first.
* @throws Exception
*/
@Test
public void useServiceWithoutStartTest() throws Exception {
ambryBlobStorageService = getAmbryBlobStorageService();
// not fine to use without start.
try {
doOperation(createRestRequest(RestMethod.GET, "/", null, null), new MockRestResponseChannel());
fail("Should not have been able to use AmbryBlobStorageService without start");
} catch (RestServiceException e) {
assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.ServiceUnavailable, e.getErrorCode());
}
}
use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.
the class FrontendUtilsTest method testGetBlobIdFromString.
/**
* Tests {@link FrontendUtils#getBlobIdFromString(String, ClusterMap)}
* @throws IOException
* @throws RestServiceException
*/
@Test
public void testGetBlobIdFromString() throws IOException, RestServiceException {
// good path
byte[] bytes = new byte[2];
ClusterMap referenceClusterMap = new MockClusterMap();
TestUtils.RANDOM.nextBytes(bytes);
BlobId.BlobIdType referenceType = TestUtils.RANDOM.nextBoolean() ? BlobId.BlobIdType.NATIVE : BlobId.BlobIdType.CRAFTED;
TestUtils.RANDOM.nextBytes(bytes);
byte referenceDatacenterId = bytes[0];
short referenceAccountId = getRandomShort(TestUtils.RANDOM);
short referenceContainerId = getRandomShort(TestUtils.RANDOM);
PartitionId referencePartitionId = referenceClusterMap.getWritablePartitionIds().get(0);
boolean referenceIsEncrypted = TestUtils.RANDOM.nextBoolean();
BlobId blobId = new BlobId(BlobId.BLOB_ID_V3, referenceType, referenceDatacenterId, referenceAccountId, referenceContainerId, referencePartitionId, referenceIsEncrypted);
BlobId regeneratedBlobId = FrontendUtils.getBlobIdFromString(blobId.getID(), referenceClusterMap);
assertEquals("BlobId mismatch", blobId, regeneratedBlobId);
assertBlobIdFieldValues(regeneratedBlobId, referenceType, referenceDatacenterId, referenceAccountId, referenceContainerId, referencePartitionId, referenceIsEncrypted);
// bad path
try {
FrontendUtils.getBlobIdFromString(blobId.getID().substring(1), referenceClusterMap);
fail("Should have thrown exception for bad blobId ");
} catch (RestServiceException e) {
assertEquals("RestServiceErrorCode mismatch", RestServiceErrorCode.BadRequest, e.getErrorCode());
}
}
use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method verifyAccountAndContainerFromBlobId.
/**
* Verifies presence of {@link Account} and {@link Container} injected into {@link RestRequest} using a
* blobId string, for get/head/delete operations.
* @param blobId The blobId string to get/head/delete.
* @param expectedAccount The expected {@link Account} to verify its presence in {@link RestRequest}.
* @param expectedContainer The expected {@link Container} to verify its presence in {@link RestRequest}.
* @param expectedRestErrorCode The expected {@link RestServiceErrorCode} to verify.
* @throws Exception
*/
private void verifyAccountAndContainerFromBlobId(String blobId, Account expectedAccount, Container expectedContainer, RestServiceErrorCode expectedRestErrorCode) throws Exception {
if (blobId.startsWith("/")) {
blobId = blobId.substring(1);
}
for (RestMethod restMethod : Lists.newArrayList(RestMethod.GET, RestMethod.HEAD, RestMethod.DELETE)) {
RestRequest restRequest = createRestRequest(restMethod, "/" + blobId, null, null);
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
try {
doOperation(restRequest, restResponseChannel);
if (expectedRestErrorCode != null) {
fail("Should have thrown");
}
} catch (RestServiceException e) {
assertEquals("Wrong RestServiceErrorCode", expectedRestErrorCode, e.getErrorCode());
}
BlobId deserializedId = new BlobId(blobId, clusterMap);
// Because BlobInfo is not fetched on deletes, V1 Blob IDs will never be reassigned to a known account/container.
boolean alwaysExpectUnknown = restMethod == RestMethod.DELETE && deserializedId.getAccountId() == Account.UNKNOWN_ACCOUNT_ID && deserializedId.getContainerId() == Container.UNKNOWN_CONTAINER_ID;
assertEquals("Wrong account object in RestRequest's args", alwaysExpectUnknown ? InMemAccountService.UNKNOWN_ACCOUNT : expectedAccount, restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_ACCOUNT_KEY));
assertEquals("Wrong container object in RestRequest's args", alwaysExpectUnknown ? Container.UNKNOWN_CONTAINER : expectedContainer, restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_CONTAINER_KEY));
}
}
Aggregations