@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()); } }
@Override public UUID getIdFromKey(final String externalKey, final InternalTenantContext context) throws AccountApiException { if (externalKey == null) { throw new AccountApiException(ErrorCode.ACCOUNT_CANNOT_MAP_NULL_KEY, ""); } return transactionalSqlDao.execute(true, new EntitySqlDaoTransactionWrapper<UUID>() { @Override public UUID inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception { return entitySqlDaoWrapperFactory.become(AccountSqlDao.class).getIdFromKey(externalKey, context); } }); }
@Override public void updateAccount(final Account account, final CallContext context) { final Iterator<Account> iterator = accounts.iterator(); while (iterator.hasNext()) { final Account account1 = iterator.next(); if (account.getId().equals(account1.getId())) { iterator.remove(); break; } } try { createAccount(account, context); } catch (final AccountApiException e) { Assert.fail(e.toString()); } }
public PaymentApiException(final AccountApiException e) { super(e, e.getCode(), e.getMessage()); }
@Override public void handleKillbillEvent(final ExtBusEvent killbillEvent) { logService.log(LogService.LOG_INFO, "Received external event " + killbillEvent.toString()); // Only looking at account creation if (killbillEvent.getEventType() != ExtBusEventType.ACCOUNT_CREATION) { return; } final TenantContext tenantContext = new TenantContext() { @Override public UUID getTenantId() { return null; } }; try { Account account = killbillAPI.getAccountUserApi().getAccountById(killbillEvent.getAccountId(), tenantContext); testDao.insertAccountExternalKey(account.getExternalKey()); } catch (AccountApiException e) { logService.log(LogService.LOG_ERROR, e.getMessage()); } } }
public PaymentApiException(final AccountApiException e) { super(e, e.getCode(), e.getMessage()); }
accountId = account.getId(); } catch (final AccountApiException e) { Assert.fail(e.getMessage()); return null;
private AccountModelDao getAccountModelDaoByRecordId(final Long recordId, final InternalTenantContext context) throws AccountApiException { final AccountModelDao accountModelDao = accountDao.getByRecordId(recordId, context); if (accountModelDao == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_RECORD_ID, recordId); } return accountModelDao; }
@Test(groups = "slow", description = "Test Account creation with External Key over limit") public void testCreateAccountWithExternalKeyOverLimit() throws Exception { AccountModelDao accountModelDao = createTestAccount(); // Set an externalKey of 256 characters (over limit) accountModelDao.setExternalKey("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis,."); final AccountData accountData = new DefaultAccount(accountModelDao); try { accountUserApi.createAccount(accountData, callContext); Assert.fail(); } catch (final AccountApiException e) { assertEquals(e.getCode(), ErrorCode.EXTERNAL_KEY_LIMIT_EXCEEDED.getCode()); } }
@Override public void updateAccount(final Account account, final CallContext context) { final Iterator<Account> iterator = accounts.iterator(); while (iterator.hasNext()) { final Account account1 = iterator.next(); if (account.getId().equals(account1.getId())) { iterator.remove(); break; } } try { createAccount(account, context); } catch (final AccountApiException e) { Assert.fail(e.toString()); } }
protected Account getAccountByRecordId(final Long recordId, final InternalTenantContext context) throws AccountApiException { final Account account = getAccountByRecordIdInternal(recordId, context); if (account == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_RECORD_ID, recordId); } return account; }
@Test(groups = "slow", description = "Test Account creation with same External Key in different tenants") public void testCreateAccountWithSameExternalKeyInDifferentTenants() throws Exception { final AccountData accountData = createAccountData(); final Account account1 = accountUserApi.createAccount(accountData, callContext); try { // Same tenant accountUserApi.createAccount(accountData, callContext); Assert.fail(); } catch (final AccountApiException e) { assertEquals(e.getCode(), ErrorCode.ACCOUNT_ALREADY_EXISTS.getCode()); } final TenantSqlDao tenantSqlDao = dbi.onDemand(TenantSqlDao.class); final TenantModelDao tenant2 = new TenantModelDao(); tenantSqlDao.create(tenant2, internalCallContext); final CallContext callContext2 = new DefaultCallContext(account1.getId(), tenant2.getId(), callContext.getUserName(), callContext.getCallOrigin(), callContext.getUserType(), callContext.getUserToken(), clock); final Account account2 = accountUserApi.createAccount(accountData, callContext2); Assert.assertEquals(account1.getExternalKey(), account2.getExternalKey()); Assert.assertNotEquals(account1.getId(), account2.getId()); } }
@Override protected AccountApiException generateAlreadyExistsException(final AccountModelDao account, final InternalCallContext context) { return new AccountApiException(ErrorCode.ACCOUNT_ALREADY_EXISTS, account.getExternalKey()); }
@Override public Response toResponse(final AccountApiException exception) { if (exception.getCode() == ErrorCode.ACCOUNT_ALREADY_EXISTS.getCode()) { return buildConflictingRequestResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.ACCOUNT_CANNOT_CHANGE_EXTERNAL_KEY.getCode()) { return buildBadRequestResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.ACCOUNT_CANNOT_MAP_NULL_KEY.getCode()) { return buildBadRequestResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.ACCOUNT_CREATION_FAILED.getCode()) { return buildInternalErrorResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_ID.getCode()) { return buildNotFoundResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_KEY.getCode()) { return buildNotFoundResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.ACCOUNT_INVALID_NAME.getCode()) { return buildBadRequestResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.ACCOUNT_UPDATE_FAILED.getCode()) { return buildInternalErrorResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_RECORD_ID.getCode()) { return buildNotFoundResponse(exception, uriInfo); } else { return fallback(exception, uriInfo); } } }
@Override public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception { final AccountEmailSqlDao transactional = entitySqlDaoWrapperFactory.become(AccountEmailSqlDao.class); if (transactional.getById(email.getId().toString(), context) != null) { throw new AccountApiException(ErrorCode.ACCOUNT_EMAIL_ALREADY_EXISTS, email.getId()); } createAndRefresh(transactional, email, context); return null; } });
@Override public void updateAccount(final String externalKey, final AccountData accountData, final CallContext context) throws AccountApiException { final Account currentAccount = getAccountByKey(externalKey, context); if (currentAccount == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_KEY, externalKey); } updateAccount(currentAccount.getId(), accountData, false, context); }
protected Account getAccountById(final UUID accountId, final InternalTenantContext context) throws AccountApiException { final Long recordId = nonEntityDao.retrieveRecordIdFromObject(accountId, ObjectType.ACCOUNT, recordIdCacheController); final Account account = getAccountByRecordIdInternal(recordId, context); if (account == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_ID, accountId); } accountCacheController.putIfAbsent(recordId, new DefaultImmutableAccountData(account)); return account; }
@Override public void updateBCD(final String externalKey, final int bcd, final InternalCallContext context) throws AccountApiException { final Account currentAccount = getAccountByKey(externalKey, context); if (currentAccount == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_KEY, externalKey); } if (currentAccount.getBillCycleDayLocal() != DefaultMutableAccountData.DEFAULT_BILLING_CYCLE_DAY_LOCAL) { throw new AccountApiException(ErrorCode.ACCOUNT_UPDATE_FAILED); } final MutableAccountData mutableAccountData = currentAccount.toMutableAccountData(); mutableAccountData.setBillCycleDayLocal(bcd); final AccountModelDao accountToUpdate = new AccountModelDao(currentAccount.getId(), mutableAccountData); bcdCacheController.remove(currentAccount.getId()); bcdCacheController.putIfAbsent(currentAccount.getId(), new Integer(bcd)); accountDao.update(accountToUpdate, true, context); }
@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); }
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; }