@Test(groups = "slow", description = "Test Account DAO: duplicate emails throws an exception") public void testHandleDuplicateEmails() throws AccountApiException { final UUID accountId = UUID.randomUUID(); final AccountEmail email = new DefaultAccountEmail(accountId, "test@gmail.com"); Assert.assertEquals(accountDao.getEmailsByAccountId(accountId, internalCallContext).size(), 0); final AccountEmailModelDao accountEmailModelDao = new AccountEmailModelDao(email); accountDao.addEmail(accountEmailModelDao, internalCallContext); Assert.assertEquals(accountDao.getEmailsByAccountId(accountId, internalCallContext).size(), 1); try { accountDao.addEmail(accountEmailModelDao, internalCallContext); Assert.fail(); } catch (AccountApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.ACCOUNT_EMAIL_ALREADY_EXISTS.getCode()); } }
@Test(groups = "slow", description = "Test Account DAO: very long numbers", enabled = false) public void testOverlyLongPhoneNumber() throws AccountApiException { final AccountModelDao account = createTestAccount("12345678901234567890123456"); try { accountDao.create(account, internalCallContext); Assert.fail(); } catch (RuntimeException e) { Assert.assertTrue(e.getCause() instanceof SQLException); } }
@Override public List<AuditLogWithHistory> getAuditLogsWithHistoryForId(final UUID accountId, final AuditLevel auditLevel, final TenantContext tenantContext) throws AccountApiException { return accountDao.getAuditLogsWithHistoryForId(accountId, auditLevel, internalCallContextFactory.createInternalTenantContext(accountId, tenantContext)); }
@Test(groups = "slow", description = "Test Account DAO: basic update (1)") public void testUpdate() throws Exception { final AccountModelDao account = createTestAccount(); accountDao.create(account, internalCallContext); refreshCallContext(account.getId()); final AccountModelDao createdAccount = accountDao.getAccountByKey(account.getExternalKey(), internalCallContext); List<AuditLogWithHistory> auditLogsWithHistory = accountDao.getAuditLogsWithHistoryForId(account.getId(), AuditLevel.FULL, internalCallContext); Assert.assertEquals(auditLogsWithHistory.size(), 1); .build(); final AccountModelDao updatedAccount = new AccountModelDao(account.getId(), accountData); accountDao.update(updatedAccount, true, internalCallContext); final AccountModelDao retrievedAccount = accountDao.getAccountByKey(account.getExternalKey(), internalCallContext); checkAccountsEqual(retrievedAccount, updatedAccount); auditLogsWithHistory = accountDao.getAuditLogsWithHistoryForId(retrievedAccount.getId(), AuditLevel.FULL, internalCallContext); Assert.assertEquals(auditLogsWithHistory.size(), 2); .build(); final AccountModelDao updatedAccount2 = new AccountModelDao(account.getId(), accountData2); accountDao.update(updatedAccount2, true, internalCallContext); final AccountModelDao retrievedAccount2 = accountDao.getAccountByKey(account.getExternalKey(), internalCallContext); checkAccountsEqual(retrievedAccount2, updatedAccount2); auditLogsWithHistory = accountDao.getAuditLogsWithHistoryForId(retrievedAccount2.getId(), AuditLevel.FULL, internalCallContext); Assert.assertEquals(auditLogsWithHistory.size(), 3);
@Test(groups = "slow", description = "Test Account DAO: add and remove email") public void testAddRemoveAccountEmail() throws AccountApiException { final UUID accountId = UUID.randomUUID(); // Add a new e-mail final AccountEmail email = new DefaultAccountEmail(accountId, "test@gmail.com"); accountDao.addEmail(new AccountEmailModelDao(email), internalCallContext); final List<AccountEmailModelDao> accountEmails = accountDao.getEmailsByAccountId(accountId, internalCallContext); Assert.assertEquals(accountEmails.size(), 1); Assert.assertEquals(accountEmails.get(0).getAccountId(), accountId); Assert.assertEquals(accountEmails.get(0).getEmail(), email.getEmail()); // Verify audits final List<AuditLog> auditLogsForAccountEmail = auditDao.getAuditLogsForId(TableName.ACCOUNT_EMAIL, email.getId(), AuditLevel.FULL, internalCallContext); Assert.assertEquals(auditLogsForAccountEmail.size(), 1); Assert.assertEquals(auditLogsForAccountEmail.get(0).getChangeType(), ChangeType.INSERT); // Delete the e-mail accountDao.removeEmail(new AccountEmailModelDao(email), internalCallContext); Assert.assertEquals(accountDao.getEmailsByAccountId(accountId, internalCallContext).size(), 0); }
@Test(groups = "slow", description = "Test Account: basic DAO calls") public void testBasic() throws AccountApiException { final AccountModelDao account = createTestAccount(); accountDao.create(account, internalCallContext); refreshCallContext(account.getId()); // Retrieve by key AccountModelDao retrievedAccount = accountDao.getAccountByKey(account.getExternalKey(), internalCallContext); checkAccountsEqual(retrievedAccount, account); // Retrieve by id retrievedAccount = accountDao.getById(retrievedAccount.getId(), internalCallContext); checkAccountsEqual(retrievedAccount, account); // Retrieve all final Pagination<AccountModelDao> allAccounts = accountDao.getAll(internalCallContext); final List<AccountModelDao> all = ImmutableList.<AccountModelDao>copyOf(allAccounts); Assert.assertNotNull(all); Assert.assertEquals(all.size(), 1); checkAccountsEqual(all.get(0), account); // Verify audits final List<AuditLog> auditLogsForAccount = auditDao.getAuditLogsForId(TableName.ACCOUNT, account.getId(), AuditLevel.FULL, internalCallContext); Assert.assertEquals(auditLogsForAccount.size(), 1); Assert.assertEquals(auditLogsForAccount.get(0).getChangeType(), ChangeType.INSERT); }
@Test(groups = "slow", description = "Test Account DAO: payment method update") public void testUpdatePaymentMethod() throws Exception { final AccountModelDao account = createTestAccount(); accountDao.create(account, internalCallContext); refreshCallContext(account.getId()); final UUID newPaymentMethodId = UUID.randomUUID(); accountDao.updatePaymentMethod(account.getId(), newPaymentMethodId, internalCallContext); final AccountModelDao newAccount = accountDao.getById(account.getId(), internalCallContext); Assert.assertEquals(newAccount.getPaymentMethodId(), newPaymentMethodId); // And then set it to null (delete the default payment method) accountDao.updatePaymentMethod(account.getId(), null, internalCallContext); final AccountModelDao newAccountWithPMNull = accountDao.getById(account.getId(), internalCallContext); Assert.assertNull(newAccountWithPMNull.getPaymentMethodId()); }
@Test(groups = "slow", description = "Test Account DAO: basic update (2)") public void testShouldBeAbleToUpdateSomeFields() throws Exception { final AccountModelDao account = createTestAccount(); accountDao.create(account, internalCallContext); refreshCallContext(account.getId()); final MutableAccountData otherAccount = new DefaultAccount(account).toMutableAccountData(); otherAccount.setAddress1(UUID.randomUUID().toString()); otherAccount.setEmail(UUID.randomUUID().toString()); final AccountModelDao newAccount = new AccountModelDao(account.getId(), otherAccount); accountDao.update(newAccount, true, internalCallContext); final AccountModelDao retrievedAccount = accountDao.getById(account.getId(), internalCallContext); checkAccountsEqual(retrievedAccount, newAccount); }
@Test(groups = "slow", description = "Test Account DAO: retrieve by externalKey") public void testGetIdFromKey() throws AccountApiException { final AccountModelDao account = createTestAccount(); accountDao.create(account, internalCallContext); refreshCallContext(account.getId()); final UUID accountId = accountDao.getIdFromKey(account.getExternalKey(), internalCallContext); Assert.assertEquals(accountId, account.getId()); }
@Test(groups = "slow", description = "Test Account DAO: long numbers") public void testLongPhoneNumber() throws AccountApiException { final AccountModelDao account = createTestAccount("123456789012345678901234"); accountDao.create(account, internalCallContext); refreshCallContext(account.getId()); final AccountModelDao retrievedAccount = accountDao.getAccountByKey(account.getExternalKey(), internalCallContext); checkAccountsEqual(retrievedAccount, account); }
@Test(groups = "slow", description = "Test Account: verify audits") public void testAudits() throws AccountApiException { // Special test to verify audits - they are handled a bit differently due to the account record id (see EntitySqlDaoWrapperInvocationHandler#insertAudits) final AccountModelDao account1 = createTestAccount(); accountDao.create(account1, internalCallContext); refreshCallContext(account1.getId()); // Verify audits via account record id final DefaultAccountAuditLogs auditLogsForAccount1ViaAccountRecordId1 = auditDao.getAuditLogsForAccountRecordId(AuditLevel.FULL, internalCallContext); Assert.assertEquals(auditLogsForAccount1ViaAccountRecordId1.getAuditLogsForAccount().size(), 1); Assert.assertEquals(auditLogsForAccount1ViaAccountRecordId1.getAuditLogsForAccount().get(0).getChangeType(), ChangeType.INSERT); // Add an entry in the account_history table to make sure we pick up the right // record id / target record id / account record id in the audit_log table accountDao.updatePaymentMethod(account1.getId(), UUID.randomUUID(), internalCallContext); final AccountModelDao account2 = createTestAccount(); accountDao.create(account2, internalCallContext); refreshCallContext(account2.getId()); // Verify audits via account record id final DefaultAccountAuditLogs auditLogsForAccount2ViaAccountRecordId = auditDao.getAuditLogsForAccountRecordId(AuditLevel.FULL, internalCallContext); Assert.assertEquals(auditLogsForAccount2ViaAccountRecordId.getAuditLogsForAccount().size(), 1); Assert.assertEquals(auditLogsForAccount2ViaAccountRecordId.getAuditLogsForAccount().get(0).getChangeType(), ChangeType.INSERT); refreshCallContext(account1.getId()); final DefaultAccountAuditLogs auditLogsForAccount1ViaAccountRecordId2 = auditDao.getAuditLogsForAccountRecordId(AuditLevel.FULL, internalCallContext); Assert.assertEquals(auditLogsForAccount1ViaAccountRecordId2.getAuditLogsForAccount().size(), 2); Assert.assertEquals(auditLogsForAccount1ViaAccountRecordId2.getAuditLogsForAccount().get(0).getChangeType(), ChangeType.INSERT); Assert.assertEquals(auditLogsForAccount1ViaAccountRecordId2.getAuditLogsForAccount().get(1).getChangeType(), ChangeType.UPDATE); }
@Override public List<AccountEmail> getEmails(final UUID accountId, final InternalTenantContext context) { return ImmutableList.<AccountEmail>copyOf(Collections2.transform(accountDao.getEmailsByAccountId(accountId, context), new Function<AccountEmailModelDao, AccountEmail>() { @Override public AccountEmail apply(final AccountEmailModelDao input) { return new DefaultAccountEmail(input); } })); }
@Test(groups = "slow", description = "Test Account DAO: BCD of 0") public void testShouldBeAbleToHandleBCDOfZero() throws Exception { final AccountModelDao account = createTestAccount(0); accountDao.create(account, internalCallContext); refreshCallContext(account.getId()); // Same BCD (zero) final AccountModelDao retrievedAccount = accountDao.getById(account.getId(), internalCallContext); checkAccountsEqual(retrievedAccount, account); }
private void updateAccount(final UUID accountId, final AccountData account, final boolean treatNullValueAsReset, final CallContext context) throws AccountApiException { final AccountModelDao updatedAccountModelDao = new AccountModelDao(accountId, null, null, account, false); accountDao.update(updatedAccountModelDao, treatNullValueAsReset, internalCallContextFactory.createInternalCallContext(accountId, context)); }
protected Account getAccountByKey(final String key, final InternalTenantContext context) throws AccountApiException { final AccountModelDao accountModelDao = accountDao.getAccountByKey(key, context); if (accountModelDao == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_KEY, key); } final Account account = new DefaultAccount(accountModelDao); final Long recordId = nonEntityDao.retrieveRecordIdFromObject(account.getId(), ObjectType.ACCOUNT, recordIdCacheController); accountCacheController.putIfAbsent(recordId, new DefaultImmutableAccountData(account)); return account; }
@Override public UUID getIdFromKey(final String externalKey, final TenantContext context) throws AccountApiException { return accountDao.getIdFromKey(externalKey, internalCallContextFactory.createInternalTenantContextWithoutAccountRecordId(context)); }
@Override public void updatePaymentMethod(final UUID accountId, final UUID paymentMethodId, final InternalCallContext context) throws AccountApiException { accountDao.updatePaymentMethod(accountId, paymentMethodId, context); }
@Override public void removeEmail(final UUID accountId, final AccountEmail email, final CallContext context) { accountDao.removeEmail(new AccountEmailModelDao(email, false), internalCallContextFactory.createInternalCallContext(accountId, context)); }
@Override public void addEmail(final UUID accountId, final AccountEmail email, final CallContext context) throws AccountApiException { accountDao.addEmail(new AccountEmailModelDao(email), internalCallContextFactory.createInternalCallContext(accountId, context)); }
@Override public Pagination<AccountModelDao> build() { return accountDao.get(offset, limit, internalCallContextFactory.createInternalTenantContextWithoutAccountRecordId(context)); } },