use of com.github.ambry.utils.ThrowingBiConsumer in project ambry by linkedin.
the class GetAccountsHandlerTest method getSingleContainerSuccessTest.
/**
* Test success case of getting single container.
* @throws Exception
*/
@Test
public void getSingleContainerSuccessTest() throws Exception {
Account existingAccount = accountService.createAndAddRandomAccount();
Container existingContainer = existingAccount.getAllContainers().iterator().next();
ThrowingBiConsumer<RestRequest, Container> testAction = (request, expectedContainer) -> {
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("Container does not match", Collections.singletonList(expectedContainer), AccountCollectionSerde.containersFromInputStreamInJson(asyncWritableChannel.consumeContentAsInputStream(), existingAccount.getId()));
};
testAction.accept(createRestRequest(existingAccount.getName(), null, existingContainer.getName(), Operations.ACCOUNTS_CONTAINERS), existingContainer);
}
use of com.github.ambry.utils.ThrowingBiConsumer 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.utils.ThrowingBiConsumer 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.utils.ThrowingBiConsumer 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.utils.ThrowingBiConsumer 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