Search in sources :

Example 91 with Account

use of com.github.ambry.account.Account 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);
}
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) 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 92 with Account

use of com.github.ambry.account.Account 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 93 with Account

use of com.github.ambry.account.Account in project ambry by linkedin.

the class FrontendIntegrationTestBase method updateAccountsAndVerify.

/**
 * Call the {@code POST /accounts} API to update account metadata and verify that the update succeeded.
 * @param accountService {@link AccountService} object.
 * @param accounts the accounts to replace or add using the {@code POST /accounts} call.
 */
void updateAccountsAndVerify(AccountService accountService, Account... accounts) throws Exception {
    byte[] accountUpdateJson = AccountCollectionSerde.serializeAccountsInJson(Arrays.asList(accounts));
    FullHttpRequest request = buildRequest(HttpMethod.POST, Operations.ACCOUNTS, null, ByteBuffer.wrap(accountUpdateJson));
    NettyClient.ResponseParts responseParts = nettyClient.sendRequest(request, null, null).get();
    HttpResponse response = getHttpResponse(responseParts);
    assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
    verifyTrackingHeaders(response);
    assertNoContent(responseParts.queue, 1);
    for (Account account : accounts) {
        assertEquals("Update not reflected in AccountService", account, accountService.getAccountById(account.getId()));
    }
}
Also used : NettyClient(com.github.ambry.rest.NettyClient) Account(com.github.ambry.account.Account) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpResponse(io.netty.handler.codec.http.HttpResponse)

Example 94 with Account

use of com.github.ambry.account.Account 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()));
}
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) Account(com.github.ambry.account.Account) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Collection(java.util.Collection) AccountBuilder(com.github.ambry.account.AccountBuilder) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Test(org.junit.Test)

Example 95 with Account

use of com.github.ambry.account.Account in project ambry by linkedin.

the class AmbrySecurityServiceTest method testHeadBlob.

/**
 * Tests {@link AmbrySecurityService#processResponse(RestRequest, RestResponseChannel, BlobInfo, Callback)} for
 * {@link RestMethod#HEAD}.
 * @param blobInfo the {@link BlobInfo} of the blob for which {@link RestMethod#HEAD} is required.
 * @param range the {@link ByteRange} used for a range request, or {@code null} for non-ranged requests.
 * @throws Exception
 */
private void testHeadBlob(BlobInfo blobInfo, ByteRange range) throws Exception {
    MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
    JSONObject headers = range != null ? new JSONObject().put(RestUtils.Headers.RANGE, RestTestUtils.getRangeHeaderString(range)) : null;
    RestRequest restRequest = createRestRequest(RestMethod.HEAD, "/", headers);
    Pair<Account, Container> accountAndContainer = getAccountAndContainer(blobInfo.getBlobProperties());
    insertAccountAndContainer(restRequest, accountAndContainer.getFirst(), accountAndContainer.getSecond());
    securityService.processResponse(restRequest, restResponseChannel, blobInfo).get();
    Assert.assertEquals("ProcessResponse status should have been set", range == null ? ResponseStatus.Ok : ResponseStatus.PartialContent, restResponseChannel.getStatus());
    verifyHeadersForHead(blobInfo.getBlobProperties(), range, restResponseChannel);
    verifyAccountAndContainerHeaders(restResponseChannel, accountAndContainer.getFirst(), accountAndContainer.getSecond());
    Assert.assertEquals("LifeVersion mismatch", Short.toString(blobInfo.getLifeVersion()), restResponseChannel.getHeader(RestUtils.Headers.LIFE_VERSION));
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel)

Aggregations

Account (com.github.ambry.account.Account)114 Container (com.github.ambry.account.Container)87 Test (org.junit.Test)67 RestServiceException (com.github.ambry.rest.RestServiceException)24 ArrayList (java.util.ArrayList)22 RestRequest (com.github.ambry.rest.RestRequest)18 JSONObject (org.json.JSONObject)18 MockRestRequest (com.github.ambry.rest.MockRestRequest)17 VerifiableProperties (com.github.ambry.config.VerifiableProperties)16 HashMap (java.util.HashMap)15 HashSet (java.util.HashSet)15 AccountBuilder (com.github.ambry.account.AccountBuilder)14 MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)14 ContainerBuilder (com.github.ambry.account.ContainerBuilder)13 Properties (java.util.Properties)13 MetricRegistry (com.codahale.metrics.MetricRegistry)12 InMemAccountService (com.github.ambry.account.InMemAccountService)12 ByteBuffer (java.nio.ByteBuffer)12 RestMethod (com.github.ambry.rest.RestMethod)11 Map (java.util.Map)11