killbill-memoizeit

account: use clock in DefaultChangedField This relates

2/8/2016 6:08:40 PM

Details

diff --git a/account/src/main/java/org/killbill/billing/account/api/DefaultChangedField.java b/account/src/main/java/org/killbill/billing/account/api/DefaultChangedField.java
index 7ee0e32..8b11cfb 100644
--- a/account/src/main/java/org/killbill/billing/account/api/DefaultChangedField.java
+++ b/account/src/main/java/org/killbill/billing/account/api/DefaultChangedField.java
@@ -1,7 +1,9 @@
 /*
  * Copyright 2010-2013 Ning, Inc.
+ * Copyright 2014-2016 Groupon, Inc
+ * Copyright 2014-2016 The Billing Project, LLC
  *
- * Ning licenses this file to you under the Apache License, version 2.0
+ * The Billing Project licenses this file to you under the Apache License, version 2.0
  * (the "License"); you may not use this file except in compliance with the
  * License.  You may obtain a copy of the License at:
  *
@@ -17,7 +19,6 @@
 package org.killbill.billing.account.api;
 
 import org.joda.time.DateTime;
-
 import org.killbill.billing.events.ChangedField;
 
 import com.fasterxml.jackson.annotation.JsonCreator;
@@ -41,12 +42,6 @@ public class DefaultChangedField implements ChangedField {
         this.newValue = newValue;
     }
 
-    public DefaultChangedField(final String fieldName,
-                               final String oldValue,
-                               final String newValue) {
-        this(fieldName, oldValue, newValue, new DateTime());
-    }
-
     @Override
     public String getFieldName() {
         return fieldName;
@@ -124,5 +119,4 @@ public class DefaultChangedField implements ChangedField {
         }
         return true;
     }
-
 }
diff --git a/account/src/main/java/org/killbill/billing/account/api/user/DefaultAccountChangeEvent.java b/account/src/main/java/org/killbill/billing/account/api/user/DefaultAccountChangeEvent.java
index 9ee7111..d68f0dc 100644
--- a/account/src/main/java/org/killbill/billing/account/api/user/DefaultAccountChangeEvent.java
+++ b/account/src/main/java/org/killbill/billing/account/api/user/DefaultAccountChangeEvent.java
@@ -1,7 +1,9 @@
 /*
  * Copyright 2010-2013 Ning, Inc.
+ * Copyright 2014-2016 Groupon, Inc
+ * Copyright 2014-2016 The Billing Project, LLC
  *
- * Ning licenses this file to you under the Apache License, version 2.0
+ * The Billing Project licenses this file to you under the Apache License, version 2.0
  * (the "License"); you may not use this file except in compliance with the
  * License.  You may obtain a copy of the License at:
  *
@@ -17,9 +19,11 @@
 package org.killbill.billing.account.api.user;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 import java.util.UUID;
 
+import org.joda.time.DateTime;
 import org.killbill.billing.account.api.DefaultChangedField;
 import org.killbill.billing.account.dao.AccountModelDao;
 import org.killbill.billing.events.AccountChangeInternalEvent;
@@ -47,10 +51,16 @@ public class DefaultAccountChangeEvent extends BusEventBase implements AccountCh
         this.changedFields = changedFields;
     }
 
-    public DefaultAccountChangeEvent(final UUID id, final AccountModelDao oldData, final AccountModelDao newData, final Long searchKey1, final Long searchKey2, final UUID userToken) {
+    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);
+        this.changedFields = calculateChangedFields(oldData, newData, changeDate);
     }
 
     @JsonIgnore
@@ -73,7 +83,7 @@ public class DefaultAccountChangeEvent extends BusEventBase implements AccountCh
     @JsonIgnore
     @Override
     public boolean hasChanges() {
-        return (changedFields.size() > 0);
+        return (!changedFields.isEmpty());
     }
 
     @Override
@@ -116,57 +126,56 @@ public class DefaultAccountChangeEvent extends BusEventBase implements AccountCh
         return true;
     }
 
-    private List<ChangedField> calculateChangedFields(final AccountModelDao oldData, final AccountModelDao newData) {
-
+    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());
+                          oldData.getExternalKey(), newData.getExternalKey(), changeDate);
 
         addIfValueChanged(tmpChangedFields, "email",
-                          oldData.getEmail(), newData.getEmail());
+                          oldData.getEmail(), newData.getEmail(), changeDate);
 
         addIfValueChanged(tmpChangedFields, "firstName",
-                          oldData.getName(), newData.getName());
+                          oldData.getName(), newData.getName(), changeDate);
 
         addIfValueChanged(tmpChangedFields, "currency",
                           (oldData.getCurrency() != null) ? oldData.getCurrency().toString() : null,
-                          (newData.getCurrency() != null) ? newData.getCurrency().toString() : null);
+                          (newData.getCurrency() != null) ? newData.getCurrency().toString() : null, changeDate);
 
         addIfValueChanged(tmpChangedFields,
                           "billCycleDayLocal",
-                          String.valueOf(oldData.getBillingCycleDayLocal()), String.valueOf(newData.getBillingCycleDayLocal()));
+                          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);
+                          (newData.getPaymentMethodId() != null) ? newData.getPaymentMethodId().toString() : null, changeDate);
 
-        addIfValueChanged(tmpChangedFields, "locale", oldData.getLocale(), newData.getLocale());
+        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());
+                          (newData.getTimeZone() == null) ? null : newData.getTimeZone().toString(), changeDate);
 
-        addIfValueChanged(tmpChangedFields, "address1", oldData.getAddress1(), newData.getAddress1());
-        addIfValueChanged(tmpChangedFields, "address2", oldData.getAddress2(), newData.getAddress2());
-        addIfValueChanged(tmpChangedFields, "city", oldData.getCity(), newData.getCity());
-        addIfValueChanged(tmpChangedFields, "stateOrProvince", oldData.getStateOrProvince(), newData.getStateOrProvince());
-        addIfValueChanged(tmpChangedFields, "country", oldData.getCountry(), newData.getCountry());
-        addIfValueChanged(tmpChangedFields, "postalCode", oldData.getPostalCode(), newData.getPostalCode());
-        addIfValueChanged(tmpChangedFields, "phone", oldData.getPhone(), newData.getPhone());
+        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;
     }
 
-    private void addIfValueChanged(final List<ChangedField> inputList, final String key, final String oldData, final String newData) {
+    private void addIfValueChanged(final Collection<ChangedField> inputList, final String key, final String oldData, final String newData, final DateTime changeDate) {
         // If both null => no changes
         if (newData == null && oldData == null) {
             // If only one is null
         } else if (newData == null || oldData == null) {
-            inputList.add(new DefaultChangedField(key, oldData, newData));
+            inputList.add(new DefaultChangedField(key, oldData, newData, changeDate));
             // If neither are null we can safely compare values
         } else if (!newData.equals(oldData)) {
-            inputList.add(new DefaultChangedField(key, oldData, newData));
+            inputList.add(new DefaultChangedField(key, oldData, newData, changeDate));
         }
     }
 }
diff --git a/account/src/main/java/org/killbill/billing/account/dao/DefaultAccountDao.java b/account/src/main/java/org/killbill/billing/account/dao/DefaultAccountDao.java
index 31c33e3..a106fb0 100644
--- a/account/src/main/java/org/killbill/billing/account/dao/DefaultAccountDao.java
+++ b/account/src/main/java/org/killbill/billing/account/dao/DefaultAccountDao.java
@@ -1,7 +1,9 @@
 /*
  * Copyright 2010-2013 Ning, Inc.
+ * Copyright 2014-2016 Groupon, Inc
+ * Copyright 2014-2016 The Billing Project, LLC
  *
- * Ning licenses this file to you under the Apache License, version 2.0
+ * The Billing Project licenses this file to you under the Apache License, version 2.0
  * (the "License"); you may not use this file except in compliance with the
  * License.  You may obtain a copy of the License at:
  *
@@ -57,6 +59,7 @@ public class DefaultAccountDao extends EntityDaoBase<AccountModelDao, Account, A
 
     private final PersistentBus eventBus;
     private final InternalCallContextFactory internalCallContextFactory;
+    private final Clock clock;
 
     @Inject
     public DefaultAccountDao(final IDBI dbi, final PersistentBus eventBus, final Clock clock, final CacheControllerDispatcher cacheControllerDispatcher,
@@ -64,6 +67,7 @@ public class DefaultAccountDao extends EntityDaoBase<AccountModelDao, Account, A
         super(new EntitySqlDaoTransactionalJdbiWrapper(dbi, clock, cacheControllerDispatcher, nonEntityDao), AccountSqlDao.class);
         this.eventBus = eventBus;
         this.internalCallContextFactory = internalCallContextFactory;
+        this.clock = clock;
     }
 
     @Override
@@ -157,8 +161,8 @@ public class DefaultAccountDao extends EntityDaoBase<AccountModelDao, Account, A
                                                                                              specifiedAccount,
                                                                                              context.getAccountRecordId(),
                                                                                              context.getTenantRecordId(),
-                                                                                             context.getUserToken()
-                );
+                                                                                             context.getUserToken(),
+                                                                                             clock.getUTCNow());
                 try {
                     eventBus.postFromTransaction(changeEvent, entitySqlDaoWrapperFactory.getHandle().getConnection());
                 } catch (final EventBusException e) {
@@ -195,8 +199,8 @@ public class DefaultAccountDao extends EntityDaoBase<AccountModelDao, Account, A
                 final AccountChangeInternalEvent changeEvent = new DefaultAccountChangeEvent(accountId, currentAccount, account,
                                                                                              context.getAccountRecordId(),
                                                                                              context.getTenantRecordId(),
-                                                                                             context.getUserToken()
-                );
+                                                                                             context.getUserToken(),
+                                                                                             clock.getUTCNow());
 
                 try {
                     eventBus.postFromTransaction(changeEvent, entitySqlDaoWrapperFactory.getHandle().getConnection());
diff --git a/account/src/test/java/org/killbill/billing/account/api/user/TestDefaultAccountUserApiWithMocks.java b/account/src/test/java/org/killbill/billing/account/api/user/TestDefaultAccountUserApiWithMocks.java
index f3d7863..6cf12f7 100644
--- a/account/src/test/java/org/killbill/billing/account/api/user/TestDefaultAccountUserApiWithMocks.java
+++ b/account/src/test/java/org/killbill/billing/account/api/user/TestDefaultAccountUserApiWithMocks.java
@@ -1,7 +1,7 @@
 /*
  * Copyright 2010-2013 Ning, Inc.
- * Copyright 2014-2015 Groupon, Inc
- * Copyright 2014-2015 The Billing Project, LLC
+ * Copyright 2014-2016 Groupon, Inc
+ * Copyright 2014-2016 The Billing Project, LLC
  *
  * The Billing Project licenses this file to you under the Apache License, version 2.0
  * (the "License"); you may not use this file except in compliance with the
@@ -52,7 +52,7 @@ public class TestDefaultAccountUserApiWithMocks extends AccountTestSuiteNoDB {
 
     @BeforeMethod(groups = "fast")
     public void setUp() throws Exception {
-        accountDao = new MockAccountDao(Mockito.mock(PersistentBus.class));
+        accountDao = new MockAccountDao(Mockito.mock(PersistentBus.class), clock);
         accountUserApi = new DefaultAccountUserApi(accountDao, nonEntityDao, controllerDispatcher, internalFactory);
     }
 
diff --git a/account/src/test/java/org/killbill/billing/account/api/user/TestEventJson.java b/account/src/test/java/org/killbill/billing/account/api/user/TestEventJson.java
index 23ccdf7..23e3aa3 100644
--- a/account/src/test/java/org/killbill/billing/account/api/user/TestEventJson.java
+++ b/account/src/test/java/org/killbill/billing/account/api/user/TestEventJson.java
@@ -37,8 +37,8 @@ public class TestEventJson extends AccountTestSuiteNoDB {
     @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"));
-        changes.add(new DefaultChangedField("fieldYY", "valueY", "valueYYY"));
+        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);
diff --git a/account/src/test/java/org/killbill/billing/account/dao/MockAccountDao.java b/account/src/test/java/org/killbill/billing/account/dao/MockAccountDao.java
index a06aa3f..0c33683 100644
--- a/account/src/test/java/org/killbill/billing/account/dao/MockAccountDao.java
+++ b/account/src/test/java/org/killbill/billing/account/dao/MockAccountDao.java
@@ -1,7 +1,9 @@
 /*
  * Copyright 2010-2013 Ning, Inc.
+ * Copyright 2014-2016 Groupon, Inc
+ * Copyright 2014-2016 The Billing Project, LLC
  *
- * Ning licenses this file to you under the Apache License, version 2.0
+ * The Billing Project licenses this file to you under the Apache License, version 2.0
  * (the "License"); you may not use this file except in compliance with the
  * License.  You may obtain a copy of the License at:
  *
@@ -16,13 +18,12 @@
 
 package org.killbill.billing.account.dao;
 
+import java.util.Collection;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 
-import org.testng.Assert;
-
 import org.killbill.billing.BillingExceptionBase;
 import org.killbill.billing.ErrorCode;
 import org.killbill.billing.account.api.Account;
@@ -33,8 +34,6 @@ import org.killbill.billing.account.api.DefaultMutableAccountData;
 import org.killbill.billing.account.api.user.DefaultAccountChangeEvent;
 import org.killbill.billing.account.api.user.DefaultAccountCreationEvent;
 import org.killbill.billing.account.api.user.DefaultAccountCreationEvent.DefaultAccountData;
-import org.killbill.bus.api.PersistentBus;
-import org.killbill.bus.api.PersistentBus.EventBusException;
 import org.killbill.billing.callcontext.InternalCallContext;
 import org.killbill.billing.callcontext.InternalTenantContext;
 import org.killbill.billing.events.AccountChangeInternalEvent;
@@ -42,6 +41,10 @@ import org.killbill.billing.util.callcontext.InternalCallContextFactory;
 import org.killbill.billing.util.entity.DefaultPagination;
 import org.killbill.billing.util.entity.Pagination;
 import org.killbill.billing.util.entity.dao.MockEntityDaoBase;
+import org.killbill.bus.api.PersistentBus;
+import org.killbill.bus.api.PersistentBus.EventBusException;
+import org.killbill.clock.Clock;
+import org.testng.Assert;
 
 import com.google.common.base.Predicate;
 import com.google.common.collect.ImmutableList;
@@ -52,10 +55,12 @@ public class MockAccountDao extends MockEntityDaoBase<AccountModelDao, Account, 
 
     private final MockEntityDaoBase<AccountEmailModelDao, AccountEmail, AccountApiException> accountEmailSqlDao = new MockEntityDaoBase<AccountEmailModelDao, AccountEmail, AccountApiException>();
     private final PersistentBus eventBus;
+    private final Clock clock;
 
     @Inject
-    public MockAccountDao(final PersistentBus eventBus) {
+    public MockAccountDao(final PersistentBus eventBus, final Clock clock) {
         this.eventBus = eventBus;
+        this.clock = clock;
     }
 
     @Override
@@ -81,8 +86,8 @@ public class MockAccountDao extends MockEntityDaoBase<AccountModelDao, Account, 
         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()
-        );
+                                                                                     accountRecordId, tenantRecordId, UUID.randomUUID(),
+                                                                                     clock.getUTCNow());
         if (changeEvent.hasChanges()) {
             try {
                 eventBus.post(changeEvent);
@@ -106,7 +111,7 @@ public class MockAccountDao extends MockEntityDaoBase<AccountModelDao, Account, 
 
     @Override
     public Pagination<AccountModelDao> searchAccounts(final String searchKey, final Long offset, final Long limit, final InternalTenantContext context) {
-        final List<AccountModelDao> results = new LinkedList<AccountModelDao>();
+        final Collection<AccountModelDao> results = new LinkedList<AccountModelDao>();
         int maxNbRecords = 0;
         for (final AccountModelDao account : getAll(context)) {
             maxNbRecords++;
@@ -145,7 +150,7 @@ public class MockAccountDao extends MockEntityDaoBase<AccountModelDao, Account, 
     public void addEmail(final AccountEmailModelDao email, final InternalCallContext context) {
         try {
             accountEmailSqlDao.create(email, context);
-        } catch (BillingExceptionBase billingExceptionBase) {
+        } catch (final BillingExceptionBase billingExceptionBase) {
             Assert.fail(billingExceptionBase.toString());
         }
     }
@@ -170,5 +175,4 @@ public class MockAccountDao extends MockEntityDaoBase<AccountModelDao, Account, 
         final AccountModelDao account = getById(accountId, context);
         return account != null ? account.getBillingCycleDayLocal() : 0;
     }
-
 }