Search in sources :

Example 6 with ThrowingBiConsumer

use of com.github.ambry.utils.ThrowingBiConsumer 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);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) FutureResult(com.github.ambry.router.FutureResult) ThrowingConsumer(com.github.ambry.utils.ThrowingConsumer) AccountCollectionSerde(com.github.ambry.account.AccountCollectionSerde) RequestPath(com.github.ambry.rest.RequestPath) HashSet(java.util.HashSet) JSONObject(org.json.JSONObject) TestUtils(com.github.ambry.utils.TestUtils) RetainingAsyncWritableChannel(com.github.ambry.commons.RetainingAsyncWritableChannel) Container(com.github.ambry.account.Container) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) MetricRegistry(com.codahale.metrics.MetricRegistry) RestMethod(com.github.ambry.rest.RestMethod) Collection(java.util.Collection) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) Test(org.junit.Test) ThrowingBiConsumer(com.github.ambry.utils.ThrowingBiConsumer) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) RestServiceException(com.github.ambry.rest.RestServiceException) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Account(com.github.ambry.account.Account) RestUtils(com.github.ambry.rest.RestUtils) Assert(org.junit.Assert) RestRequest(com.github.ambry.rest.RestRequest) Collections(java.util.Collections) InMemAccountService(com.github.ambry.account.InMemAccountService) RestServiceException(com.github.ambry.rest.RestServiceException) Account(com.github.ambry.account.Account) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) Test(org.junit.Test)

Example 7 with ThrowingBiConsumer

use of com.github.ambry.utils.ThrowingBiConsumer 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);
}
Also used : IntStream(java.util.stream.IntStream) MockRestRequest(com.github.ambry.rest.MockRestRequest) FrontendConfig(com.github.ambry.config.FrontendConfig) FutureResult(com.github.ambry.router.FutureResult) ByteBuffer(java.nio.ByteBuffer) ThrowingConsumer(com.github.ambry.utils.ThrowingConsumer) AccountCollectionSerde(com.github.ambry.account.AccountCollectionSerde) RequestPath(com.github.ambry.rest.RequestPath) JSONObject(org.json.JSONObject) TestUtils(com.github.ambry.utils.TestUtils) LinkedList(java.util.LinkedList) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) MetricRegistry(com.codahale.metrics.MetricRegistry) Properties(java.util.Properties) RestMethod(com.github.ambry.rest.RestMethod) VerifiableProperties(com.github.ambry.config.VerifiableProperties) Collection(java.util.Collection) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) Test(org.junit.Test) AccountBuilder(com.github.ambry.account.AccountBuilder) ThrowingBiConsumer(com.github.ambry.utils.ThrowingBiConsumer) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) RestServiceException(com.github.ambry.rest.RestServiceException) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Account(com.github.ambry.account.Account) RestUtils(com.github.ambry.rest.RestUtils) Assert(org.junit.Assert) RestRequest(com.github.ambry.rest.RestRequest) Collections(java.util.Collections) InMemAccountService(com.github.ambry.account.InMemAccountService) RestServiceException(com.github.ambry.rest.RestServiceException) JSONObject(org.json.JSONObject) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) Test(org.junit.Test)

Aggregations

MetricRegistry (com.codahale.metrics.MetricRegistry)7 MockRestRequest (com.github.ambry.rest.MockRestRequest)7 MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)7 RestMethod (com.github.ambry.rest.RestMethod)7 RestRequest (com.github.ambry.rest.RestRequest)7 RestResponseChannel (com.github.ambry.rest.RestResponseChannel)7 RestServiceErrorCode (com.github.ambry.rest.RestServiceErrorCode)7 RestServiceException (com.github.ambry.rest.RestServiceException)7 RestUtils (com.github.ambry.rest.RestUtils)7 FutureResult (com.github.ambry.router.FutureResult)7 ReadableStreamChannel (com.github.ambry.router.ReadableStreamChannel)7 TestUtils (com.github.ambry.utils.TestUtils)7 ThrowingBiConsumer (com.github.ambry.utils.ThrowingBiConsumer)7 TimeUnit (java.util.concurrent.TimeUnit)7 JSONObject (org.json.JSONObject)7 Assert (org.junit.Assert)7 Test (org.junit.Test)7 Account (com.github.ambry.account.Account)6 AccountCollectionSerde (com.github.ambry.account.AccountCollectionSerde)6 InMemAccountService (com.github.ambry.account.InMemAccountService)6