/** * Assert a collection of {@link Account}s exist in the {@link AccountService}. * @param accounts The collection of {@link Account}s to assert their existence. * @param expectedAccountCount The expected number of {@link Account}s in the {@link AccountService}. * @param accountService The {@link AccountService} to assert {@link Account}s existence. */ static void assertAccountsInAccountService(Collection<Account> accounts, int expectedAccountCount, AccountService accountService) { assertEquals("Wrong number of accounts in accountService", expectedAccountCount, accountService.getAllAccounts().size()); for (Account account : accounts) { assertAccountInAccountService(account, accountService); } }
/** * Does several operations: * 1. Writes a {@link ZNRecord} to {@link HelixPropertyStore} without notifying listeners; * 2. Starts up a {@link HelixAccountService} that should fetch the {@link ZNRecord}; * 3. Updates (creates) one more {@link Account} through the {@link HelixAccountService}; * 4. Writes the same {@link ZNRecord} to {@link HelixPropertyStore} and publishes a message for {@link Account} update; * * If the {@link ZNRecord} is good, it should not affect updating operation. * @param zNRecord A {@link ZNRecord} to write to {@link HelixPropertyStore}. * @param isGoodZNRecord {code true} to indicate the {@link ZNRecord} is good, which should not affect updating * operation; {@code false} otherwise. * @throws Exception Any unexpected exception. */ private void updateAndWriteZNRecord(ZNRecord zNRecord, boolean isGoodZNRecord) throws Exception { writeZNRecordToHelixPropertyStore(zNRecord, false); accountService = mockHelixAccountServiceFactory.getAccountService(); assertEquals("Number of account is wrong", 0, accountService.getAllAccounts().size()); updateAccountsAndAssertAccountExistence(Collections.singletonList(refAccount), isGoodZNRecord ? 1 : 0, isGoodZNRecord); writeZNRecordToHelixPropertyStore(zNRecord, true); if (isGoodZNRecord) { assertAccountInAccountService(refAccount, accountService); } else { assertEquals("Number of accounts is wrong.", 0, accountService.getAllAccounts().size()); } }
/** * 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); assertFalse(accountService.updateAccounts(accounts)); assertEquals("Number of account is wrong.", 1, accountService.getAllAccounts().size()); assertAccountInAccountService(account1, accountService); }