use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class GetAccountsHandlerTest method validRequestsTest.
/**
* Test valid request cases.
* @throws Exception
*/
@Test
public void validRequestsTest() throws Exception {
Account account = accountService.createAndAddRandomAccount();
ThrowingBiConsumer<RestRequest, Collection<Account>> testAction = (request, expectedAccounts) -> {
RestResponseChannel restResponseChannel = new MockRestResponseChannel();
ReadableStreamChannel channel = sendRequestGetResponse(request, restResponseChannel);
assertNotNull("There should be a response", channel);
Assert.assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
assertEquals("Content-type is not as expected", RestUtils.JSON_CONTENT_TYPE, restResponseChannel.getHeader(RestUtils.Headers.CONTENT_TYPE));
assertEquals("Content-length is not as expected", channel.getSize(), Integer.parseInt((String) restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
RetainingAsyncWritableChannel asyncWritableChannel = new RetainingAsyncWritableChannel((int) channel.getSize());
channel.readInto(asyncWritableChannel, null).get();
assertEquals("Accounts do not match", new HashSet<>(expectedAccounts), new HashSet<>(AccountCollectionSerde.accountsFromInputStreamInJson(asyncWritableChannel.consumeContentAsInputStream())));
};
testAction.accept(createRestRequest(null, null, null, Operations.ACCOUNTS), accountService.getAllAccounts());
testAction.accept(createRestRequest(account.getName(), null, null, Operations.ACCOUNTS), Collections.singleton(account));
testAction.accept(createRestRequest(null, Short.toString(account.getId()), null, Operations.ACCOUNTS), Collections.singleton(account));
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class GetAccountsHandlerTest method createRestRequest.
// helpers
// general
/**
* Creates a {@link RestRequest} for a GET /accounts or /accounts/containers request
* @param accountName if set, add this account name as a request header.
* @param accountId if set, add this account ID as a request header.
* @param containerName if set, add this container name as request header.
* @param operation the operation this request will perform.
* @return the {@link RestRequest}
* @throws Exception
*/
private RestRequest createRestRequest(String accountName, String accountId, String containerName, String operation) throws Exception {
JSONObject data = new JSONObject();
data.put(MockRestRequest.REST_METHOD_KEY, RestMethod.GET.name());
data.put(MockRestRequest.URI_KEY, operation);
JSONObject headers = new JSONObject();
if (accountName != null) {
headers.put(RestUtils.Headers.TARGET_ACCOUNT_NAME, accountName);
}
if (accountId != null) {
headers.put(RestUtils.Headers.TARGET_ACCOUNT_ID, accountId);
}
if (containerName != null) {
headers.put(RestUtils.Headers.TARGET_CONTAINER_NAME, containerName);
}
data.put(MockRestRequest.HEADERS_KEY, headers);
RestRequest restRequest = new MockRestRequest(data, null);
restRequest.setArg(RestUtils.InternalKeys.REQUEST_PATH, RequestPath.parse(restRequest, null, null));
return restRequest;
}
use of com.github.ambry.rest.RestRequest 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);
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class GetAccountsHandlerTest method badRequestsTest.
/**
* Test bad request cases.
* @throws Exception
*/
@Test
public void badRequestsTest() throws Exception {
Account existingAccount = accountService.createAndAddRandomAccount();
Account nonExistentAccount = accountService.generateRandomAccount();
ThrowingBiConsumer<RestRequest, RestServiceErrorCode> testAction = (request, expectedErrorCode) -> {
TestUtils.assertException(RestServiceException.class, () -> sendRequestGetResponse(request, new MockRestResponseChannel()), e -> assertEquals("Unexpected error code", expectedErrorCode, e.getErrorCode()));
};
// cannot supply both ID and name
testAction.accept(createRestRequest(existingAccount.getName(), Short.toString(existingAccount.getId()), null, Operations.ACCOUNTS), RestServiceErrorCode.BadRequest);
// non-numerical ID
testAction.accept(createRestRequest(null, "ABC", null, Operations.ACCOUNTS), RestServiceErrorCode.InvalidArgs);
// account that doesn't exist
testAction.accept(createRestRequest(nonExistentAccount.getName(), null, null, Operations.ACCOUNTS), RestServiceErrorCode.NotFound);
testAction.accept(createRestRequest(null, Short.toString(nonExistentAccount.getId()), null, Operations.ACCOUNTS), RestServiceErrorCode.NotFound);
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class TailoredPeersClusterMap method handleGoodCaseTest.
/**
* Tests for the good cases where the datanodes are correct. The peers obtained from the response is compared
* against the ground truth.
* @throws Exception
*/
@Test
public void handleGoodCaseTest() throws Exception {
for (String datanode : TailoredPeersClusterMap.DATANODE_NAMES) {
RestRequest restRequest = getRestRequest(datanode);
RestResponseChannel restResponseChannel = new MockRestResponseChannel();
ReadableStreamChannel channel = sendRequestGetResponse(restRequest, restResponseChannel);
assertNotNull("There should be a response", channel);
Assert.assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
assertEquals("Content-type is not as expected", RestUtils.JSON_CONTENT_TYPE, restResponseChannel.getHeader(RestUtils.Headers.CONTENT_TYPE));
assertEquals("Content-length is not as expected", channel.getSize(), Integer.parseInt((String) restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
Set<String> expectedPeers = clusterMap.getPeers(datanode);
Set<String> peersFromResponse = getPeersFromResponse(RestTestUtils.getJsonizedResponseBody(channel));
assertEquals("Peer list returned does not match expected for " + datanode, expectedPeers, peersFromResponse);
}
}
Aggregations