use of com.github.ambry.account.Container in project ambry by linkedin.
the class FrontendIntegrationTest method postGetHeadDeleteTest.
/**
* Tests blob POST, GET, HEAD and DELETE operations.
* @throws Exception
*/
@Test
public void postGetHeadDeleteTest() throws Exception {
// add some accounts
Account refAccount = ACCOUNT_SERVICE.createAndAddRandomAccount();
Container publicContainer = refAccount.getContainerById(Container.DEFAULT_PUBLIC_CONTAINER_ID);
Container privateContainer = refAccount.getContainerById(Container.DEFAULT_PRIVATE_CONTAINER_ID);
int refContentSize = FRONTEND_CONFIG.frontendChunkedGetResponseThresholdInBytes * 3;
for (int i = 0; i < 2; i++) {
ACCOUNT_SERVICE.createAndAddRandomAccount();
}
// with valid account and containers
for (Account account : ACCOUNT_SERVICE.getAllAccounts()) {
if (account.getId() != Account.UNKNOWN_ACCOUNT_ID) {
for (Container container : account.getAllContainers()) {
doPostGetHeadDeleteTest(refContentSize, account, container, account.getName(), !container.isCacheable(), account.getName(), container.getName(), false);
}
}
}
// valid account and container names but only serviceId passed as part of POST
doPostGetHeadDeleteTest(refContentSize, null, null, refAccount.getName(), false, refAccount.getName(), publicContainer.getName(), false);
doPostGetHeadDeleteTest(refContentSize, null, null, refAccount.getName(), true, refAccount.getName(), privateContainer.getName(), false);
// unrecognized serviceId
doPostGetHeadDeleteTest(refContentSize, null, null, "unknown_service_id", false, null, null, false);
doPostGetHeadDeleteTest(refContentSize, null, null, "unknown_service_id", true, null, null, false);
// different sizes
for (int contentSize : new int[] { 0, FRONTEND_CONFIG.frontendChunkedGetResponseThresholdInBytes - 1, FRONTEND_CONFIG.frontendChunkedGetResponseThresholdInBytes, refContentSize }) {
doPostGetHeadDeleteTest(contentSize, refAccount, publicContainer, refAccount.getName(), !publicContainer.isCacheable(), refAccount.getName(), publicContainer.getName(), false);
}
}
use of com.github.ambry.account.Container in project ambry by linkedin.
the class AccountTestUtils method generateRefAccounts.
/**
* Randomly generates a collection of {@link Account}s, which do not have the same id or name. The {@link Container}s
* of the same {@link Account} also do not have the same id or name.
* @param idToRefAccountMap A map from id to {@link Account} to populate with the generated {@link Account}s.
* @param idToRefContainerMap A map from name to {@link Account} to populate with the generated {@link Account}s.
* @param accountIdSet A set of ids that could not be used to generate {@link Account}s.
* @param accountCount The number of {@link Account}s to generate.
* @param containerCountPerAccount The number of {@link Container}s per {@link Account} to generate.
*/
public static void generateRefAccounts(Map<Short, Account> idToRefAccountMap, Map<Short, Map<Short, Container>> idToRefContainerMap, Set<Short> accountIdSet, int accountCount, int containerCountPerAccount) {
idToRefAccountMap.clear();
idToRefContainerMap.clear();
for (int i = 0; i < accountCount; i++) {
short accountId = Utils.getRandomShort(random);
if (!accountIdSet.add(accountId)) {
i--;
continue;
}
String accountName = UUID.randomUUID().toString();
Account.AccountStatus accountStatus = random.nextBoolean() ? Account.AccountStatus.ACTIVE : Account.AccountStatus.INACTIVE;
List<Container> containers = new ArrayList<>();
List<ContainerBuilder> containerBuilders = generateContainerBuilders(containerCountPerAccount, accountId);
containers.addAll(containerBuilders.stream().map(ContainerBuilder::build).collect(Collectors.toList()));
Map<Short, Container> idToContainers = containers.stream().collect(Collectors.toMap(Container::getId, Function.identity()));
Account account = new AccountBuilder(accountId, accountName, accountStatus).containers(containers).build();
assertEquals("Wrong number of generated containers for the account", containerCountPerAccount, account.getAllContainers().size());
idToRefAccountMap.put(accountId, account);
idToRefContainerMap.put(accountId, idToContainers);
}
assertEquals("Wrong number of generated accounts", accountCount, idToRefAccountMap.size());
}
use of com.github.ambry.account.Container in project ambry by linkedin.
the class AccountTestUtils method assertAccountInAccountService.
/**
* Asserts that an {@link Account} exists in the {@link AccountService}.
* @param account The {@link Account} to assert existence.
* @param accountService The {@link AccountService} to assert {@link Account} existence.
*/
public static void assertAccountInAccountService(Account account, AccountService accountService) {
Account accountFoundById = accountService.getAccountById(account.getId());
Account accountFoundByName = accountService.getAccountByName(account.getName());
assertEquals("Account got by name from accountService does not match account to assert.", account, accountFoundByName);
assertEquals("Account got by id from accountService does not match the account to assert", account, accountFoundById);
assertEquals("The number of containers in the account is wrong.", accountFoundById.getAllContainers().size(), account.getAllContainers().size());
for (Container container : account.getAllContainers()) {
assertContainerInAccountService(container, accountService);
}
}
use of com.github.ambry.account.Container in project ambry by linkedin.
the class AccountContainerTest method buildAccountJson.
/**
* Construct an account JSON object in the version specified by {@link Account#CURRENT_JSON_VERSION}.
* @param account The {@link Account} to serialize
* @param incrementSnapshotVersion True to increase the snapshot version.
* @return The {@link JSONObject}.
* @throws JSONException
*/
private JSONObject buildAccountJson(Account account, boolean incrementSnapshotVersion) throws JSONException {
JSONObject metadata = new JSONObject();
metadata.put(Account.JSON_VERSION_KEY, CURRENT_JSON_VERSION);
metadata.put(ACCOUNT_ID_KEY, account.getId());
metadata.put(ACCOUNT_NAME_KEY, account.getName());
metadata.put(Account.STATUS_KEY, account.getStatus().name());
metadata.put(Account.SNAPSHOT_VERSION_KEY, incrementSnapshotVersion ? account.getSnapshotVersion() + 1 : account.getSnapshotVersion());
metadata.put(Account.LAST_MODIFIED_TIME_KEY, account.getLastModifiedTime());
metadata.put(ACL_INHERITED_BY_CONTAINER_KEY, account.isAclInheritedByContainer());
JSONArray containerArray = new JSONArray();
for (Container container : account.getAllContainers()) {
containerArray.put(buildContainerJson(container));
}
metadata.put(CONTAINERS_KEY, containerArray);
metadata.put(QUOTA_RESOURCE_TYPE_KEY, account.getQuotaResourceType().name());
return metadata;
}
use of com.github.ambry.account.Container in project ambry by linkedin.
the class AccountContainerTest method testAccountBuilder.
/**
* Tests building an {@link Account} using {@link AccountBuilder}.
* @throws JSONException
*/
@Test
public void testAccountBuilder() throws JSONException {
// build an account with arguments supplied
AccountBuilder accountBuilder = new AccountBuilder(refAccountId, refAccountName, refAccountStatus, refQuotaResourceType).snapshotVersion(refAccountSnapshotVersion).aclInheritedByContainer(refAccountAclInheritedByContainer);
Account accountByBuilder = accountBuilder.build();
assertAccountAgainstReference(accountByBuilder, false, false);
// set containers
for (int i = 0; i < CONTAINER_COUNT; i++) {
Container container = containerFromJson(containerJsonList.get(i), refAccountId);
accountBuilder.addOrUpdateContainer(container);
}
accountByBuilder = accountBuilder.build();
assertAccountAgainstReference(accountByBuilder, true, true);
// build an account from existing account
accountBuilder = new AccountBuilder(accountByBuilder);
Account account2ByBuilder = accountBuilder.build();
assertAccountAgainstReference(account2ByBuilder, true, true);
// clear containers
Account account3ByBuilder = new AccountBuilder(account2ByBuilder).containers(null).build();
assertAccountAgainstReference(account3ByBuilder, false, false);
assertTrue("Container list should be empty.", account3ByBuilder.getAllContainers().isEmpty());
}
Aggregations