@Override protected AccountApiException generateAlreadyExistsException(final AccountModelDao account, final InternalCallContext context) { return new AccountApiException(ErrorCode.ACCOUNT_ALREADY_EXISTS, account.getExternalKey()); }
@Override public AccountModelDao getAccountByKey(final String externalKey, final InternalTenantContext context) { for (final Map<Long, AccountModelDao> accountRow : entities.values()) { final AccountModelDao account = accountRow.values().iterator().next(); if (account.getExternalKey().equals(externalKey)) { return account; } } return null; }
currentAccount.getExternalKey() != null && !currentAccount.getExternalKey().equals(externalKey)) { throw new IllegalArgumentException(String.format("Killbill doesn't support updating the account external key: new=%s, current=%s", externalKey, currentAccount.getExternalKey()));
public DefaultImmutableAccountData(final AccountModelDao account) { this(account.getId(), account.getExternalKey(), account.getCurrency(), account.getTimeZone(), AccountDateTimeUtils.getFixedOffsetTimeZone(account), account.getReferenceTime()); }
@Override public Pagination<AccountModelDao> searchAccounts(final String searchKey, final Long offset, final Long limit, final InternalTenantContext context) { final Collection<AccountModelDao> results = new LinkedList<AccountModelDao>(); int maxNbRecords = 0; for (final AccountModelDao account : getAll(context)) { maxNbRecords++; if ((account.getName() != null && account.getName().contains(searchKey)) || (account.getEmail() != null && account.getEmail().contains(searchKey)) || (account.getExternalKey() != null && account.getExternalKey().contains(searchKey)) || (account.getCompanyName() != null && account.getCompanyName().contains(searchKey))) { results.add(account); } } return DefaultPagination.<AccountModelDao>build(offset, limit, maxNbRecords, results); }
@Test(groups = "slow", description = "Test Account: verify minimal set of required fields") public void testMinimalFields() throws Exception { final String email = UUID.randomUUID().toString(); final String name = UUID.randomUUID().toString(); final AccountData accountData = new DefaultMutableAccountData(null, email, name, 0, null, null, false, 0, null, clock.getUTCNow(), null, null, null, null, null, null, null, null, null, null, null, false); final AccountModelDao account = new AccountModelDao(UUID.randomUUID(), accountData); accountDao.create(account, internalCallContext); final AccountModelDao retrievedAccount = accountDao.getById(account.getId(), internalCallContext); checkAccountsEqual(retrievedAccount, account); // Verify a default external key was set Assert.assertEquals(retrievedAccount.getExternalKey(), retrievedAccount.getId().toString()); // Verify a default time zone was set Assert.assertEquals(retrievedAccount.getTimeZone(), DateTimeZone.UTC); }
@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()); }
Assert.assertEquals(account.getExternalKey(), externalKey); Assert.assertEquals(account.getEmail(), email); Assert.assertEquals(account.getName(), name);
@Override public Account createAccount(final AccountData data, final CallContext context) throws AccountApiException { // Not transactional, but there is a db constraint on that column if (data.getExternalKey() != null && getIdFromKey(data.getExternalKey(), context) != null) { throw new AccountApiException(ErrorCode.ACCOUNT_ALREADY_EXISTS, data.getExternalKey()); } final InternalCallContext internalContext = internalCallContextFactory.createInternalCallContextWithoutAccountRecordId(context); if (data.getParentAccountId() != null) { // verify that parent account exists if parentAccountId is not null final ImmutableAccountData immutableAccountData = immutableAccountInternalApi.getImmutableAccountDataById(data.getParentAccountId(), internalContext); if (immutableAccountData == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_ID, data.getParentAccountId()); } } final AccountModelDao account = new AccountModelDao(data); if (null != account.getExternalKey() && account.getExternalKey().length() > 255) { throw new AccountApiException(ErrorCode.EXTERNAL_KEY_LIMIT_EXCEEDED); } accountDao.create(account, internalCallContextFactory.createInternalCallContextWithoutAccountRecordId(context)); return new DefaultAccount(account); }
@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: 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); }
public static void checkAccountsEqual(final AccountModelDao retrievedAccount, final AccountModelDao account) { if (retrievedAccount == null || account == null) { Assert.assertNull(retrievedAccount); Assert.assertNull(account); return; } // Check all fields but createdDate/updatedDate (comes from the callcontext) Assert.assertEquals(retrievedAccount.getId(), account.getId()); Assert.assertEquals(retrievedAccount.getExternalKey(), account.getExternalKey()); Assert.assertEquals(retrievedAccount.getEmail(), account.getEmail()); Assert.assertEquals(retrievedAccount.getName(), account.getName()); Assert.assertEquals(retrievedAccount.getFirstNameLength(), account.getFirstNameLength()); Assert.assertEquals(retrievedAccount.getCurrency(), account.getCurrency()); Assert.assertEquals(retrievedAccount.getBillingCycleDayLocal(), account.getBillingCycleDayLocal()); Assert.assertEquals(retrievedAccount.getPaymentMethodId(), account.getPaymentMethodId()); Assert.assertEquals(retrievedAccount.getTimeZone(), account.getTimeZone()); Assert.assertEquals(retrievedAccount.getLocale(), account.getLocale()); Assert.assertEquals(retrievedAccount.getAddress1(), account.getAddress1()); Assert.assertEquals(retrievedAccount.getAddress2(), account.getAddress2()); Assert.assertEquals(retrievedAccount.getCompanyName(), account.getCompanyName()); Assert.assertEquals(retrievedAccount.getCity(), account.getCity()); Assert.assertEquals(retrievedAccount.getStateOrProvince(), account.getStateOrProvince()); Assert.assertEquals(retrievedAccount.getCountry(), account.getCountry()); Assert.assertEquals(retrievedAccount.getPostalCode(), account.getPostalCode()); Assert.assertEquals(retrievedAccount.getPhone(), account.getPhone()); Assert.assertEquals(retrievedAccount.getMigrated(), account.getMigrated()); }
final AccountModelDao createdAccount = accountDao.getAccountByKey(account.getExternalKey(), internalCallContext); List<AuditLogWithHistory> auditLogsWithHistory = accountDao.getAuditLogsWithHistoryForId(account.getId(), AuditLevel.FULL, internalCallContext); Assert.assertEquals(auditLogsWithHistory.size(), 1); Assert.assertEquals(history1.getAccountRecordId(), createdAccount.getRecordId()); Assert.assertEquals(history1.getTenantRecordId(), createdAccount.getTenantRecordId()); Assert.assertEquals(history1.getExternalKey(), createdAccount.getExternalKey()); Assert.assertEquals(history1.getMigrated(), createdAccount.getMigrated()); Assert.assertEquals(history1.getTimeZone(), createdAccount.getTimeZone()); final AccountModelDao retrievedAccount = accountDao.getAccountByKey(account.getExternalKey(), internalCallContext); checkAccountsEqual(retrievedAccount, updatedAccount); auditLogsWithHistory = accountDao.getAuditLogsWithHistoryForId(retrievedAccount.getId(), AuditLevel.FULL, internalCallContext); Assert.assertEquals(history2.getAccountRecordId(), retrievedAccount.getRecordId()); Assert.assertEquals(history2.getTenantRecordId(), retrievedAccount.getTenantRecordId()); Assert.assertEquals(history2.getExternalKey(), retrievedAccount.getExternalKey()); Assert.assertEquals(history2.getMigrated(), retrievedAccount.getMigrated()); Assert.assertEquals(history2.getTimeZone(), retrievedAccount.getTimeZone()); final AccountModelDao retrievedAccount2 = accountDao.getAccountByKey(account.getExternalKey(), internalCallContext); checkAccountsEqual(retrievedAccount2, updatedAccount2); auditLogsWithHistory = accountDao.getAuditLogsWithHistoryForId(retrievedAccount2.getId(), AuditLevel.FULL, internalCallContext); Assert.assertEquals(history3.getAccountRecordId(), retrievedAccount2.getRecordId()); Assert.assertEquals(history3.getTenantRecordId(), retrievedAccount2.getTenantRecordId()); Assert.assertEquals(history3.getExternalKey(), retrievedAccount2.getExternalKey()); Assert.assertEquals(history3.getMigrated(), retrievedAccount2.getMigrated()); Assert.assertEquals(history3.getTimeZone(), retrievedAccount2.getTimeZone());
private List<ChangedField> calculateChangedFields(final AccountModelDao oldData, final AccountModelDao newData, final DateTime changeDate) { final List<ChangedField> tmpChangedFields = new ArrayList<ChangedField>(); addIfValueChanged(tmpChangedFields, "externalKey", oldData.getExternalKey(), newData.getExternalKey(), changeDate); addIfValueChanged(tmpChangedFields, "email", oldData.getEmail(), newData.getEmail(), changeDate); addIfValueChanged(tmpChangedFields, "firstName", oldData.getName(), newData.getName(), changeDate); addIfValueChanged(tmpChangedFields, "currency", (oldData.getCurrency() != null) ? oldData.getCurrency().toString() : null, (newData.getCurrency() != null) ? newData.getCurrency().toString() : null, changeDate); addIfValueChanged(tmpChangedFields, "billCycleDayLocal", String.valueOf(oldData.getBillingCycleDayLocal()), String.valueOf(newData.getBillingCycleDayLocal()), changeDate); addIfValueChanged(tmpChangedFields, "paymentMethodId", (oldData.getPaymentMethodId() != null) ? oldData.getPaymentMethodId().toString() : null, (newData.getPaymentMethodId() != null) ? newData.getPaymentMethodId().toString() : null, changeDate); addIfValueChanged(tmpChangedFields, "locale", oldData.getLocale(), newData.getLocale(), changeDate); addIfValueChanged(tmpChangedFields, "timeZone", (oldData.getTimeZone() == null) ? null : oldData.getTimeZone().toString(), (newData.getTimeZone() == null) ? null : newData.getTimeZone().toString(), changeDate); addIfValueChanged(tmpChangedFields, "address1", oldData.getAddress1(), newData.getAddress1(), changeDate); addIfValueChanged(tmpChangedFields, "address2", oldData.getAddress2(), newData.getAddress2(), changeDate); addIfValueChanged(tmpChangedFields, "city", oldData.getCity(), newData.getCity(), changeDate); addIfValueChanged(tmpChangedFields, "stateOrProvince", oldData.getStateOrProvince(), newData.getStateOrProvince(), changeDate); addIfValueChanged(tmpChangedFields, "country", oldData.getCountry(), newData.getCountry(), changeDate); addIfValueChanged(tmpChangedFields, "postalCode", oldData.getPostalCode(), newData.getPostalCode(), changeDate); addIfValueChanged(tmpChangedFields, "phone", oldData.getPhone(), newData.getPhone(), changeDate); return tmpChangedFields; }
public DefaultAccountData(final AccountModelDao d) { this(d.getExternalKey(), d.getName(), d.getFirstNameLength(), d.getEmail(), d.getBillingCycleDayLocal(), d.getCurrency() != null ? d.getCurrency().name() : null, d.getParentAccountId(), d.getIsPaymentDelegatedToParent(), d.getPaymentMethodId(), d.getReferenceTime() != null ? d.getReferenceTime().toString() : null, d.getTimeZone() != null ? d.getTimeZone().getID() : null, d.getLocale(), d.getAddress1(), d.getAddress2(), d.getCompanyName(), d.getCity(), d.getStateOrProvince(), d.getPostalCode(), d.getCountry(), d.getPhone(), d.getNotes(), d.getMigrated()); }
public DefaultAccount(final AccountModelDao accountModelDao) { this(accountModelDao.getId(), accountModelDao.getCreatedDate(), accountModelDao.getUpdatedDate(), accountModelDao.getExternalKey(), accountModelDao.getEmail(), accountModelDao.getName(), accountModelDao.getFirstNameLength(), accountModelDao.getCurrency(), accountModelDao.getParentAccountId(), accountModelDao.getIsPaymentDelegatedToParent(), accountModelDao.getBillingCycleDayLocal(), accountModelDao.getPaymentMethodId(), accountModelDao.getReferenceTime(), accountModelDao.getTimeZone(), accountModelDao.getLocale(), accountModelDao.getAddress1(), accountModelDao.getAddress2(), accountModelDao.getCompanyName(), accountModelDao.getCity(), accountModelDao.getStateOrProvince(), accountModelDao.getCountry(), accountModelDao.getPostalCode(), accountModelDao.getPhone(), accountModelDao.getNotes(), accountModelDao.getMigrated()); }
public void mergeWithDelegate(final AccountModelDao currentAccount) { setExternalKey(currentAccount.getExternalKey());
@Override protected AccountApiException generateAlreadyExistsException(final AccountModelDao account, final InternalCallContext context) { return new AccountApiException(ErrorCode.ACCOUNT_ALREADY_EXISTS, account.getExternalKey()); }
public DefaultImmutableAccountData(final AccountModelDao account) { this(account.getId(), account.getExternalKey(), account.getCurrency(), account.getTimeZone(), AccountDateTimeUtils.getFixedOffsetTimeZone(account), account.getReferenceTime()); }
@Override public Account createAccount(final AccountData data, final CallContext context) throws AccountApiException { // Not transactional, but there is a db constraint on that column if (data.getExternalKey() != null && getIdFromKey(data.getExternalKey(), context) != null) { throw new AccountApiException(ErrorCode.ACCOUNT_ALREADY_EXISTS, data.getExternalKey()); } final InternalCallContext internalContext = internalCallContextFactory.createInternalCallContextWithoutAccountRecordId(context); if (data.getParentAccountId() != null) { // verify that parent account exists if parentAccountId is not null final ImmutableAccountData immutableAccountData = immutableAccountInternalApi.getImmutableAccountDataById(data.getParentAccountId(), internalContext); if (immutableAccountData == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_ID, data.getParentAccountId()); } } final AccountModelDao account = new AccountModelDao(data); if (null != account.getExternalKey() && account.getExternalKey().length() > 255) { throw new AccountApiException(ErrorCode.EXTERNAL_KEY_LIMIT_EXCEEDED); } accountDao.create(account, internalCallContextFactory.createInternalCallContextWithoutAccountRecordId(context)); return new DefaultAccount(account); }