@Test(groups = "fast", description="Test Account event deserialization") public void testDefaultAccountChangeEvent() throws Exception { final List<ChangedField> changes = new ArrayList<ChangedField>(); changes.add(new DefaultChangedField("fieldXX", "valueX", "valueXXX", clock.getUTCNow())); changes.add(new DefaultChangedField("fieldYY", "valueY", "valueYYY", clock.getUTCNow())); final AccountChangeInternalEvent e = new DefaultAccountChangeEvent(changes, UUID.randomUUID(), 1L, 2L, null); final String json = mapper.writeValueAsString(e); final Class<?> claz = Class.forName("org.killbill.billing.account.api.user.DefaultAccountChangeEvent"); final Object obj = mapper.readValue(json, claz); Assert.assertTrue(obj.equals(e)); }
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 DefaultAccountChangeEvent(final UUID id, final AccountModelDao oldData, final AccountModelDao newData, final Long searchKey1, final Long searchKey2, final UUID userToken, final DateTime changeDate) { super(searchKey1, searchKey2, userToken); this.accountId = id; this.changedFields = calculateChangedFields(oldData, newData, changeDate); }
@Override public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws EntityPersistenceException, EventBusException { final AccountSqlDao transactional = entitySqlDaoWrapperFactory.become(AccountSqlDao.class); final AccountModelDao currentAccount = transactional.getById(accountId.toString(), context); if (currentAccount == null) { throw new EntityPersistenceException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_ID, accountId); } // Check if an update is really needed. If not, bail early to avoid sending an extra event on the bus if ((currentAccount.getPaymentMethodId() == null && paymentMethodId == null) || (currentAccount.getPaymentMethodId() != null && currentAccount.getPaymentMethodId().equals(paymentMethodId))) { return null; } final String thePaymentMethodId = paymentMethodId != null ? paymentMethodId.toString() : null; final AccountModelDao account = (AccountModelDao) transactional.updatePaymentMethod(accountId.toString(), thePaymentMethodId, context); final AccountChangeInternalEvent changeEvent = new DefaultAccountChangeEvent(accountId, currentAccount, account, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken(), context.getCreatedDate()); try { eventBus.postFromTransaction(changeEvent, entitySqlDaoWrapperFactory.getHandle().getConnection()); } catch (final EventBusException e) { log.warn("Failed to post account change event for accountId='{}'", accountId, e); } return null; } });
public DefaultAccountChangeEvent(final UUID id, final AccountModelDao oldData, final AccountModelDao newData, final Long searchKey1, final Long searchKey2, final UUID userToken, final DateTime changeDate) { super(searchKey1, searchKey2, userToken); this.accountId = id; this.changedFields = calculateChangedFields(oldData, newData, changeDate); }
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; }
@Override public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws EventBusException, AccountApiException { final AccountSqlDao transactional = entitySqlDaoWrapperFactory.become(AccountSqlDao.class); final UUID accountId = specifiedAccount.getId(); final AccountModelDao currentAccount = transactional.getById(accountId.toString(), context); if (currentAccount == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_ID, accountId); } specifiedAccount.validateAccountUpdateInput(currentAccount, treatNullValueAsReset); if (!treatNullValueAsReset) { // Set unspecified (null) fields to their current values specifiedAccount.mergeWithDelegate(currentAccount); } transactional.update(specifiedAccount, context); final AccountChangeInternalEvent changeEvent = new DefaultAccountChangeEvent(accountId, currentAccount, specifiedAccount, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken(), context.getCreatedDate()); try { eventBus.postFromTransaction(changeEvent, entitySqlDaoWrapperFactory.getHandle().getConnection()); } catch (final EventBusException e) { log.warn("Failed to post account change event for accountId='{}'", accountId, e); } return null; } });
@Override public void update(final AccountModelDao account, final boolean treatNullValueAsReset, final InternalCallContext context) { super.update(account, context); final AccountModelDao currentAccount = getById(account.getId(), context); final Long accountRecordId = getRecordId(account.getId(), context); final long tenantRecordId = context == null ? InternalCallContextFactory.INTERNAL_TENANT_RECORD_ID : context.getTenantRecordId(); final AccountChangeInternalEvent changeEvent = new DefaultAccountChangeEvent(account.getId(), currentAccount, account, accountRecordId, tenantRecordId, UUID.randomUUID(), clock.getUTCNow()); if (changeEvent.hasChanges()) { try { eventBus.post(changeEvent); } catch (final EventBusException ex) { Assert.fail(ex.toString()); } } }
@Override public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws EntityPersistenceException, EventBusException { final AccountSqlDao transactional = entitySqlDaoWrapperFactory.become(AccountSqlDao.class); final AccountModelDao currentAccount = transactional.getById(accountId.toString(), context); if (currentAccount == null) { throw new EntityPersistenceException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_ID, accountId); } // Check if an update is really needed. If not, bail early to avoid sending an extra event on the bus if ((currentAccount.getPaymentMethodId() == null && paymentMethodId == null) || (currentAccount.getPaymentMethodId() != null && currentAccount.getPaymentMethodId().equals(paymentMethodId))) { return null; } final String thePaymentMethodId = paymentMethodId != null ? paymentMethodId.toString() : null; final AccountModelDao account = (AccountModelDao) transactional.updatePaymentMethod(accountId.toString(), thePaymentMethodId, context); final AccountChangeInternalEvent changeEvent = new DefaultAccountChangeEvent(accountId, currentAccount, account, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken(), context.getCreatedDate()); try { eventBus.postFromTransaction(changeEvent, entitySqlDaoWrapperFactory.getHandle().getConnection()); } catch (final EventBusException e) { log.warn("Failed to post account change event for accountId='{}'", accountId, e); } return null; } });
@Override public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws EventBusException, AccountApiException { final AccountSqlDao transactional = entitySqlDaoWrapperFactory.become(AccountSqlDao.class); final UUID accountId = specifiedAccount.getId(); final AccountModelDao currentAccount = transactional.getById(accountId.toString(), context); if (currentAccount == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_ID, accountId); } specifiedAccount.validateAccountUpdateInput(currentAccount, treatNullValueAsReset); if (!treatNullValueAsReset) { // Set unspecified (null) fields to their current values specifiedAccount.mergeWithDelegate(currentAccount); } transactional.update(specifiedAccount, context); final AccountChangeInternalEvent changeEvent = new DefaultAccountChangeEvent(accountId, currentAccount, specifiedAccount, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken(), context.getCreatedDate()); try { eventBus.postFromTransaction(changeEvent, entitySqlDaoWrapperFactory.getHandle().getConnection()); } catch (final EventBusException e) { log.warn("Failed to post account change event for accountId='{}'", accountId, e); } return null; } });