Search in sources :

Example 41 with Account

use of com.github.ambry.account.Account 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);
    }
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) UtilsTest(com.github.ambry.utils.UtilsTest) Test(org.junit.Test)

Example 42 with Account

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

the class HelixAccountServiceTest method checkBackupFile.

/**
 * Check that the provided backup file matches the data in the corresponding serialized accounts.
 * @param expectedAccounts the expected {@link Account}s.
 * @param backupPath the {@link Path} to the backup file.
 * @throws JSONException
 */
private void checkBackupFile(Collection<Account> expectedAccounts, Path backupPath) throws JSONException, IOException {
    try (BufferedReader reader = Files.newBufferedReader(backupPath)) {
        JSONArray accountArray = new JSONArray(new JSONTokener(reader));
        int arrayLength = accountArray.length();
        assertEquals("unexpected array size", expectedAccounts.size(), arrayLength);
        Set<Account> expectedAccountSet = new HashSet<>(expectedAccounts);
        for (int i = 0; i < arrayLength; i++) {
            JSONObject accountJson = accountArray.getJSONObject(i);
            Account account = Account.fromJson(accountJson);
            assertTrue("unexpected account in array: " + accountJson.toString(), expectedAccountSet.contains(account));
        }
    }
}
Also used : JSONTokener(org.json.JSONTokener) Account(com.github.ambry.account.Account) JSONObject(org.json.JSONObject) BufferedReader(java.io.BufferedReader) JSONArray(org.json.JSONArray) HashSet(java.util.HashSet)

Example 43 with Account

use of com.github.ambry.account.Account 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());
}
Also used : Account(com.github.ambry.account.Account) ArrayList(java.util.ArrayList) Container(com.github.ambry.account.Container) ContainerBuilder(com.github.ambry.account.ContainerBuilder) AccountBuilder(com.github.ambry.account.AccountBuilder)

Example 44 with Account

use of com.github.ambry.account.Account 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);
    }
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container)

Example 45 with Account

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

the class HelixAccountServiceTest method testReadConflictAccountDataFromHelixPropertyStoreCase3.

/**
 * Tests a series of operations.
 * 1. PrePopulates account (1, "a");
 * 2. Starts up a {@link HelixAccountService};
 * 3. Remote copy adds a new account (2, "b"), and the update has not been propagated to the {@link HelixAccountService};
 * 4. The {@link HelixAccountService} attempts to update an account (3, "b"), which should fail because it will eventually
 *    conflict with the remote copy;
 * @throws Exception Any unexpected exception.
 */
@Test
public void testReadConflictAccountDataFromHelixPropertyStoreCase3() throws Exception {
    Account account1 = new AccountBuilder((short) 1, "a", AccountStatus.INACTIVE).build();
    List<Account> accounts = Collections.singletonList(account1);
    writeAccountsToHelixPropertyStore(accounts, false);
    accountService = mockHelixAccountServiceFactory.getAccountService();
    assertAccountInAccountService(account1, accountService);
    Account account2 = new AccountBuilder((short) 2, "b", AccountStatus.INACTIVE).build();
    accounts = Collections.singletonList(account2);
    writeAccountsToHelixPropertyStore(accounts, false);
    Account conflictingAccount = new AccountBuilder((short) 3, "b", AccountStatus.INACTIVE).build();
    accounts = Collections.singletonList(conflictingAccount);
    assertUpdateAccountsFails(accounts, AccountServiceErrorCode.InternalError, accountService);
    assertEquals("Number of account is wrong.", 1, accountService.getAllAccounts().size());
    assertAccountInAccountService(account1, accountService);
}
Also used : Account(com.github.ambry.account.Account) Test(org.junit.Test)

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