use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class TailoredPeersClusterMap method doBadArgsTest.
// badArgsTest() helpers.
/**
* Does the test where bad args are provided in the request to {@link GetPeersHandler}.
* @param name the name of the host whose peers are required. Can be {@code null} if this param should be omitted.
* @param port the port of the host whose peers are required. Can be {@code null} if this param should be omitted.
* @param expectedErrorCode the {@link RestServiceErrorCode} expected in response.
* @throws Exception
*/
private void doBadArgsTest(String name, String port, RestServiceErrorCode expectedErrorCode) throws Exception {
StringBuilder uri = new StringBuilder(Operations.GET_PEERS + "?");
if (name != null) {
uri.append(GetPeersHandler.NAME_QUERY_PARAM).append("=").append(name);
}
if (port != null) {
if (name != null) {
uri.append("&");
}
uri.append(GetPeersHandler.PORT_QUERY_PARAM).append("=").append(port);
}
JSONObject data = new JSONObject();
data.put(MockRestRequest.REST_METHOD_KEY, RestMethod.GET.name());
data.put(MockRestRequest.URI_KEY, uri.toString());
RestRequest restRequest = new MockRestRequest(data, null);
try {
sendRequestGetResponse(restRequest, new MockRestResponseChannel());
fail("Request should have failed");
} catch (RestServiceException e) {
assertEquals("Unexpected RestServiceErrorCode", expectedErrorCode, e.getErrorCode());
}
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class GetSignedUrlHandlerTest method verifyFailureWithMsg.
/**
* Verifies that attempting to get signed urls 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.InternalKeys.REQUEST_PATH, RequestPath.parse("/signedUrl", Collections.emptyMap(), Collections.emptyList(), "Ambry-test"));
restRequest.setArg(RestUtils.Headers.URL_TYPE, RestMethod.POST.name());
restRequest.setArg(RestUtils.Headers.TARGET_ACCOUNT_NAME, REF_ACCOUNT.getName());
restRequest.setArg(RestUtils.Headers.TARGET_CONTAINER_NAME, REF_CONTAINER.getName());
try {
sendRequestGetResponse(restRequest, new MockRestResponseChannel());
fail("Request should have failed");
} catch (Exception e) {
assertEquals("Unexpected Exception", msg, e.getMessage());
}
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class NamedBlobPutHandlerTest method doTtlRequiredEnforcementTest.
/**
* 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, null, null, null);
byte[] content = TestUtils.getRandomBytes(1024);
RestRequest request = getRestRequest(headers, request_path, content);
RestResponseChannel restResponseChannel = new MockRestResponseChannel();
FutureResult<Void> future = new FutureResult<>();
namedBlobPutHandler.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 PostAccountsHandlerTest method badRequestsTest.
/**
* Test bad request cases.
* @throws Exception
*/
@Test
public void badRequestsTest() throws Exception {
ThrowingBiConsumer<String, RestServiceErrorCode> testAction = (requestBody, expectedErrorCode) -> {
TestUtils.assertException(RestServiceException.class, () -> sendRequestGetResponse(requestBody, new MockRestResponseChannel()), e -> assertEquals("Unexpected error code", expectedErrorCode, e.getErrorCode()));
};
// non json input
testAction.accept("ABC", RestServiceErrorCode.BadRequest);
// invalid json
testAction.accept(new JSONObject().append("accounts", "ABC").toString(), RestServiceErrorCode.BadRequest);
// AccountService update failure
accountService.setShouldUpdateSucceed(false);
testAction.accept(new String(AccountCollectionSerde.serializeAccountsInJson(Collections.emptyList())), RestServiceErrorCode.InternalServerError);
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class PostAccountsHandlerTest method validRequestsTest.
/**
* Test valid request cases.
* @throws Exception
*/
@Test
public void validRequestsTest() throws Exception {
ThrowingConsumer<Collection<Account>> testAction = accountsToUpdate -> {
String requestBody = new String(AccountCollectionSerde.serializeAccountsInJson(accountsToUpdate));
RestResponseChannel restResponseChannel = new MockRestResponseChannel();
sendRequestGetResponse(requestBody, restResponseChannel);
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)));
for (Account account : accountsToUpdate) {
assertEquals("Account in account service not as expected", account, accountService.getAccountById(account.getId()));
}
};
testAction.accept(Collections.emptyList());
// add new account
testAction.accept(Collections.singleton(accountService.generateRandomAccount()));
// update multiple accounts
testAction.accept(IntStream.range(0, 3).mapToObj(i -> {
Account account = accountService.createAndAddRandomAccount();
return new AccountBuilder(account).name(account.getName() + i).build();
}).collect(Collectors.toList()));
}
Aggregations