use of com.github.ambry.rest.MockRestResponseChannel 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.MockRestResponseChannel in project ambry by linkedin.
the class PostBlobHandlerTest method doTtlRequiredEnforcementTest.
// ttlRequiredEnforcementTest() helpers
/**
* Does the TTL required enforcement test by selecting the right verification methods based on container and frontend
* config
* @param container the {@link Container} to upload to
* @param blobTtlSecs the TTL to set for the blob
* @throws Exception
*/
private void doTtlRequiredEnforcementTest(Container container, long blobTtlSecs) throws Exception {
JSONObject headers = new JSONObject();
FrontendRestRequestServiceTest.setAmbryHeadersForPut(headers, blobTtlSecs, !container.isCacheable(), SERVICE_ID, CONTENT_TYPE, OWNER_ID, REF_ACCOUNT.getName(), container.getName(), null);
byte[] content = TestUtils.getRandomBytes(1024);
RestRequest request = getRestRequest(headers, "/", content);
RestResponseChannel restResponseChannel = new MockRestResponseChannel();
FutureResult<Void> future = new FutureResult<>();
postBlobHandler.handle(request, restResponseChannel, future::done);
if (container.isTtlRequired() && (blobTtlSecs == Utils.Infinite_Time || blobTtlSecs > frontendConfig.maxAcceptableTtlSecsIfTtlRequired)) {
if (frontendConfig.failIfTtlRequiredButNotProvided) {
try {
future.get(TIMEOUT_SECS, TimeUnit.SECONDS);
fail("Post should have failed");
} catch (ExecutionException e) {
RestServiceException rootCause = (RestServiceException) Utils.getRootCause(e);
assertNotNull("Root cause should be a RestServiceException", rootCause);
assertEquals("Incorrect RestServiceErrorCode", RestServiceErrorCode.InvalidArgs, rootCause.getErrorCode());
}
} else {
verifySuccessResponseOnTtlEnforcement(future, content, blobTtlSecs, restResponseChannel, true);
}
} else {
verifySuccessResponseOnTtlEnforcement(future, content, blobTtlSecs, restResponseChannel, false);
}
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class TtlUpdateHandlerTest method verifyFailureWithErrorCode.
/**
* Verifies that processing {@code restRequest} fails with {@code errorCode}
* @param restRequest the {@link RestRequest} that is expected to fail
* @param errorCode the {@link RestServiceErrorCode} that it should fail with
* @throws Exception
*/
private void verifyFailureWithErrorCode(RestRequest restRequest, RestServiceErrorCode errorCode) throws Exception {
try {
sendRequestGetResponse(restRequest, new MockRestResponseChannel());
fail("Request should have failed");
} catch (RestServiceException e) {
assertEquals("Unexpected RestServiceErrorCode", errorCode, e.getErrorCode());
}
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class TtlUpdateHandlerTest method verifyTtlUpdate.
// handleGoodCaseTest()
/**
* Verifies that the TTL of the blob is updated
* @param restRequest the {@link RestRequest} to get a signed URL.
* @param expectedAccount the {@link Account} that should be populated in {@link RestRequest}.
* @param expectedContainer the {@link Container} that should be populated in {@link RestRequest}.
* @throws Exception
*/
private void verifyTtlUpdate(RestRequest restRequest, Account expectedAccount, Container expectedContainer) throws Exception {
assertTtl(TTL_SECS);
RestResponseChannel restResponseChannel = new MockRestResponseChannel();
sendRequestGetResponse(restRequest, restResponseChannel);
assertEquals("ResponseStatus not as expected", ResponseStatus.Ok, restResponseChannel.getStatus());
assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
assertEquals("Content-length is not as expected", 0, Integer.parseInt((String) restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
assertEquals("Account not as expected", expectedAccount, restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_ACCOUNT_KEY));
assertEquals("Container not as expected", expectedContainer, restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_CONTAINER_KEY));
assertTtl(Utils.Infinite_Time);
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class TtlUpdateHandlerTest method verifyFailureWithMsg.
/**
* Verifies that attempting to update TTL fails with the provided {@code msg}.
* @param msg the message in the {@link Exception} that will be thrown.
* @throws Exception
*/
private void verifyFailureWithMsg(String msg) throws Exception {
RestRequest restRequest = new MockRestRequest(MockRestRequest.DUMMY_DATA, null);
restRequest.setArg(RestUtils.Headers.BLOB_ID, blobId);
restRequest.setArg(RestUtils.Headers.SERVICE_ID, SERVICE_ID);
try {
sendRequestGetResponse(restRequest, new MockRestResponseChannel());
fail("Request should have failed");
} catch (Exception e) {
if (!msg.equals(e.getMessage())) {
throw e;
}
}
}
Aggregations