/** * Assert that a {@link Container} exists in the {@link AccountService}. * @param container The {@link Container} to assert. * @param accountService The {@link AccountService} to assert {@link Container} existence. */ static void assertContainerInAccountService(Container container, AccountService accountService) { Container containerFoundById = accountService.getAccountById(container.getParentAccountId()).getContainerById(container.getId()); Container containerFoundByName = accountService.getAccountById(container.getParentAccountId()).getContainerByName(container.getName()); assertEquals("Container got by id from accountService/account does not match container got by name.", containerFoundById, containerFoundByName); assertEquals("Container got by id from accountService/account does not match the container to assert", containerFoundById, container); }
/** * Tests updating a {@link Account}, which has the same id and name as an existing record, and will replace the * existing record. This test corresponds to case A specified in the JavaDoc of {@link AccountService}. * @throws Exception Any unexpected exception. */ @Test public void testNonConflictingUpdateCaseA() throws Exception { accountService = mockHelixAccountServiceFactory.getAccountService(); // write two accounts (1, "a") and (2, "b") writeAccountsForConflictTest(); Account accountToUpdate = accountService.getAccountById((short) 1); Collection<Account> nonConflictAccounts = Collections.singleton(new AccountBuilder(accountToUpdate).status(AccountStatus.ACTIVE).build()); updateAccountsAndAssertAccountExistence(nonConflictAccounts, 2, true); }
accountService.removeAccountUpdateConsumer(accountUpdateConsumer); Account account = accountService.getAccountById(refAccountId); Account updatedAccount = new AccountBuilder(account).name(account.getName() + "-extra").build(); accountsToUpdate = new HashSet<>(Collections.singleton(updatedAccount));
/** * Tests updating a {@link Account}, which has the same id as an existing record and a non-conflicting name with any * of the existing record. The new record will replace the existing record. This test corresponds to case B specified * in the JavaDoc of {@link AccountService}. * @throws Exception Any unexpected exception. */ @Test public void testNonConflictingUpdateCaseB() throws Exception { accountService = mockHelixAccountServiceFactory.getAccountService(); // write two accounts (1, "a") and (2, "b") writeAccountsForConflictTest(); Account accountToUpdate = accountService.getAccountById((short) 1); Collection<Account> nonConflictAccounts = Collections.singleton((new AccountBuilder(accountToUpdate).status(AccountStatus.ACTIVE).build())); updateAccountsAndAssertAccountExistence(nonConflictAccounts, 2, true); }
/** * Tests updating a {@link Account}, which has a new id but a name conflicting with an existing record. The update * operation will fail. This test corresponds to case D specified in the JavaDoc of {@link AccountService}. * @throws Exception Any unexpected exception. */ @Test public void testConflictingUpdateCaseD() throws Exception { accountService = mockHelixAccountServiceFactory.getAccountService(); // write two accounts (1, "a") and (2, "b") writeAccountsForConflictTest(); Collection<Account> conflictAccounts = Collections.singleton((new AccountBuilder((short) 3, "a", AccountStatus.INACTIVE).build())); assertFalse("Wrong return value from update operation.", accountService.updateAccounts(conflictAccounts)); assertEquals("Wrong account number in HelixAccountService", 2, accountService.getAllAccounts().size()); assertNull("Wrong account got from HelixAccountService", accountService.getAccountById((short) 3)); }
@Test public void testAllMethods() throws Exception { assertEquals("Wrong account", null, accountService.getAccountById(Utils.getRandomShort(random))); assertEquals("Wrong account", InMemoryUnknownAccountService.UNKNOWN_ACCOUNT, accountService.getAccountById((short) -1)); assertEquals("Wrong account", InMemoryUnknownAccountService.UNKNOWN_ACCOUNT, accountService.getAccountByName(UtilsTest.getRandomString(10))); assertEquals("Wrong size of account collection", 1, accountService.getAllAccounts().size()); // updating the InMemoryUnknownAccountService should fail. Account account = new AccountBuilder((short) 1, "a", Account.AccountStatus.INACTIVE).build(); assertFalse("Wrong return value from an unsuccessful update operation", accountService.updateAccounts(Collections.singletonList(account))); assertEquals("Wrong size of account collection", 1, accountService.getAllAccounts().size()); try { accountService.getAllAccounts().add(account); fail("Should have thrown."); } catch (UnsupportedOperationException e) { // expected } accountService.close(); }
/** * 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. */ 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); } }
/** * Tests updating a {@link Account}, which has the same id as an existing record, but the name conflicting with * another existing record. The update operation will fail. This test corresponds to case E specified in the JavaDoc * of {@link AccountService}. * @throws Exception Any unexpected exception. */ @Test public void testConflictingUpdateCaseE() throws Exception { accountService = mockHelixAccountServiceFactory.getAccountService(); // write two accounts (1, "a") and (2, "b") writeAccountsForConflictTest(); Collection<Account> conflictAccounts = Collections.singleton((new AccountBuilder((short) 1, "b", AccountStatus.INACTIVE).build())); assertFalse("Wrong return value from update operation.", accountService.updateAccounts(conflictAccounts)); assertEquals("Wrong account number in HelixAccountService", 2, accountService.getAllAccounts().size()); assertEquals("Wrong account name got from HelixAccountService", "a", accountService.getAccountById((short) 1).getName()); }
/** * Test updating an account with a conflicting expected snapshot version. */ @Test public void testConflictingSnapshotVersionUpdate() throws Exception { accountService = mockHelixAccountServiceFactory.getAccountService(); // write two accounts (1, "a") and (2, "b") writeAccountsForConflictTest(); Account expectedAccount = accountService.getAccountById((short) 1); int currentSnapshotVersion = expectedAccount.getSnapshotVersion(); for (int snapshotVersionOffset : new int[]{-2, -1, 1}) { int snapshotVersionToUse = currentSnapshotVersion + snapshotVersionOffset; Collection<Account> conflictAccounts = Collections.singleton( new AccountBuilder((short) 1, "c", AccountStatus.INACTIVE).snapshotVersion(snapshotVersionToUse).build()); assertFalse("Wrong return value from update operation.", accountService.updateAccounts(conflictAccounts)); assertEquals("Wrong account number in HelixAccountService", 2, accountService.getAllAccounts().size()); Account account = accountService.getAccountById((short) 1); assertEquals("Account should not have been updated", expectedAccount, account); assertEquals("Snapshot version should not have been updated", currentSnapshotVersion, account.getSnapshotVersion()); } Collection<Account> validAccounts = Collections.singleton( new AccountBuilder((short) 1, "c", AccountStatus.INACTIVE).snapshotVersion(currentSnapshotVersion).build()); updateAccountsAndAssertAccountExistence(validAccounts, 2, true); }
AccountBuilder accountBuilder = new AccountBuilder(accountService.getAccountById(refAccountId)); accountsToUpdate = Collections.singletonList(accountBuilder.addOrUpdateContainer(refContainer).build()); updateAccountsAndAssertAccountExistence(accountsToUpdate, 1 + NUM_REF_ACCOUNT, true);