use of com.github.ambry.rest.RestServiceErrorCode 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(MockClusterMap.DEFAULT_PARTITION_CLASS).get(0);
boolean referenceIsEncrypted = TestUtils.RANDOM.nextBoolean();
List<Short> versions = Arrays.stream(BlobId.getAllValidVersions()).filter(version -> version >= BlobId.BLOB_ID_V3).collect(Collectors.toList());
for (short version : versions) {
BlobId blobId = new BlobId(version, referenceType, referenceDatacenterId, referenceAccountId, referenceContainerId, referencePartitionId, referenceIsEncrypted, BlobId.BlobDataType.DATACHUNK);
BlobId regeneratedBlobId = FrontendUtils.getBlobIdFromString(blobId.getID(), referenceClusterMap);
assertEquals("BlobId mismatch", blobId, regeneratedBlobId);
assertBlobIdFieldValues(regeneratedBlobId, referenceType, referenceDatacenterId, referenceAccountId, referenceContainerId, referencePartitionId, version >= BlobId.BLOB_ID_V4 && 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.RestServiceErrorCode in project ambry by linkedin.
the class GetStatsReportHandlerTest method handleBadCaseTest.
@Test
public void handleBadCaseTest() throws Exception {
ThrowingBiConsumer<RestRequest, RestServiceErrorCode> testAction = (request, expectedErrorCode) -> {
TestUtils.assertException(RestServiceException.class, () -> sendRequestGetResponse(request, new MockRestResponseChannel()), e -> assertEquals(expectedErrorCode, e.getErrorCode()));
};
RestRequest request = createRestRequest("WRONG_CLUSTER", StatsReportType.ACCOUNT_REPORT.name());
testAction.accept(request, RestServiceErrorCode.NotFound);
request = createRestRequest(null, StatsReportType.ACCOUNT_REPORT.name());
testAction.accept(request, RestServiceErrorCode.MissingArgs);
request = createRestRequest(CLUSTER_NAME, "WRONG_STATS_REPORT_TYPE");
testAction.accept(request, RestServiceErrorCode.BadRequest);
request = createRestRequest(CLUSTER_NAME, null);
testAction.accept(request, RestServiceErrorCode.MissingArgs);
}
use of com.github.ambry.rest.RestServiceErrorCode in project ambry by linkedin.
the class AmbrySecurityServiceTest method testExceptionCasesProcessResponse.
/**
* Tests exception cases for
* {@link SecurityService#processResponse(RestRequest, RestResponseChannel, BlobInfo, Callback)}
* @param restMethod the {@link RestMethod} of the request to be made
* @param restResponseChannel the {@link RestResponseChannel} to write responses over.
* @param blobInfo the {@link BlobInfo} to be used for the {@link RestRequest}
* @param expectedErrorCode the {@link RestServiceErrorCode} expected in the exception returned.
* @throws Exception
*/
private void testExceptionCasesProcessResponse(RestMethod restMethod, RestResponseChannel restResponseChannel, BlobInfo blobInfo, RestServiceErrorCode expectedErrorCode) throws Exception {
ThrowingConsumer<ExecutionException> errorAction = e -> {
Assert.assertTrue("Exception should have been an instance of RestServiceException", e.getCause() instanceof RestServiceException);
RestServiceException re = (RestServiceException) e.getCause();
Assert.assertEquals("Unexpected RestServerErrorCode (Future)", expectedErrorCode, re.getErrorCode());
};
RestRequest restRequest = createRestRequest(restMethod, "/", null);
TestUtils.assertException(ExecutionException.class, () -> securityService.processResponse(restRequest, restResponseChannel, blobInfo).get(), errorAction);
}
use of com.github.ambry.rest.RestServiceErrorCode in project ambry by linkedin.
the class PostAccountContainersHandlerTest method badRequestsTest.
/**
* Test bad request cases.
* @throws Exception
*/
@Test
public void badRequestsTest() throws Exception {
ThrowingBiConsumer<RestRequest, RestServiceErrorCode> testAction = (request, expectedErrorCode) -> {
TestUtils.assertException(RestServiceException.class, () -> sendRequestGetResponse(request, new MockRestResponseChannel()), e -> assertEquals("Unexpected error code", expectedErrorCode, e.getErrorCode()));
};
String accountName = theAccount.getName();
// Empty container list should fail
String emptyContainers = new String(AccountCollectionSerde.serializeContainersInJson(Collections.emptyList()));
RestRequest request = createRestRequest(emptyContainers, accountName, null);
testAction.accept(request, RestServiceErrorCode.BadRequest);
// non json input
request = createRestRequest("ABC", accountName, null);
testAction.accept(request, RestServiceErrorCode.BadRequest);
// invalid json
String invalidJson = new JSONObject().append("accounts", "ABC").toString();
request = createRestRequest(invalidJson, accountName, null);
testAction.accept(request, RestServiceErrorCode.BadRequest);
// No account specified
String oneContainer = new String(AccountCollectionSerde.serializeContainersInJson(Collections.singleton(accountService.getRandomContainer(theAccount.getId()))));
request = createRestRequest(oneContainer, null, null);
testAction.accept(request, RestServiceErrorCode.BadRequest);
// AccountService update failure
accountService.setShouldUpdateSucceed(false);
request = createRestRequest(oneContainer, accountName, null);
testAction.accept(request, RestServiceErrorCode.InternalServerError);
}
use of com.github.ambry.rest.RestServiceErrorCode in project ambry by linkedin.
the class GetAccountsHandlerTest method getSingleContainerFailureTest.
/**
* Test failure case of getting single container.
* @throws Exception
*/
@Test
public void getSingleContainerFailureTest() throws Exception {
ThrowingBiConsumer<RestRequest, RestServiceErrorCode> testAction = (request, expectedErrorCode) -> {
TestUtils.assertException(RestServiceException.class, () -> sendRequestGetResponse(request, new MockRestResponseChannel()), e -> assertEquals("Unexpected error code", expectedErrorCode, e.getErrorCode()));
};
// 1. invalid header (i.e. missing container name)
testAction.accept(createRestRequest("test-account", null, null, Operations.ACCOUNTS_CONTAINERS), RestServiceErrorCode.MissingArgs);
// 2. account not found
testAction.accept(createRestRequest("fake-account", null, "fake-container", Operations.ACCOUNTS_CONTAINERS), RestServiceErrorCode.NotFound);
}
Aggregations