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);
}
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);
}
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()));
}
}
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()));
}
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));
}
Aggregations