killbill-memoizeit

account: fix mergeWithDelegate, once again The conditions

8/7/2012 3:30:04 PM

Details

diff --git a/account/src/main/java/com/ning/billing/account/api/DefaultAccount.java b/account/src/main/java/com/ning/billing/account/api/DefaultAccount.java
index 4e0838d..fcd0a89 100644
--- a/account/src/main/java/com/ning/billing/account/api/DefaultAccount.java
+++ b/account/src/main/java/com/ning/billing/account/api/DefaultAccount.java
@@ -221,27 +221,38 @@ public class DefaultAccount extends EntityBase implements Account {
     public Account mergeWithDelegate(final Account currentAccount) {
         final DefaultMutableAccountData accountData = new DefaultMutableAccountData(this);
 
-        if (externalKey != null ? !externalKey.equals(currentAccount.getExternalKey()) : currentAccount.getExternalKey() != null) {
-            throw new IllegalArgumentException(String.format("Killbill doesn't support updating the account external key yet: this=%s, delegate=%s",
+        if (externalKey != null && currentAccount.getExternalKey() != null && !currentAccount.getExternalKey().equals(externalKey)) {
+            throw new IllegalArgumentException(String.format("Killbill doesn't support updating the account external key yet: new=%s, current=%s",
                                                              externalKey, currentAccount.getExternalKey()));
+        } else {
+            // Default to current value
+            accountData.setExternalKey(currentAccount.getExternalKey());
         }
 
-        if (currency != null ? !currency.equals(currentAccount.getCurrency()) : currentAccount.getCurrency() != null) {
-            throw new IllegalArgumentException(String.format("Killbill doesn't support updating the account currency yet: this=%s, delegate=%s",
+        if (currency != null && currentAccount.getCurrency() != null && !currentAccount.getCurrency().equals(currency)) {
+            throw new IllegalArgumentException(String.format("Killbill doesn't support updating the account currency yet: new=%s, current=%s",
                                                              currency, currentAccount.getCurrency()));
+        } else {
+            // Default to current value
+            accountData.setCurrency(currentAccount.getCurrency());
         }
 
-        if (currentAccount.getBillCycleDay() != null && currentAccount.getBillCycleDay().getDayOfMonthLocal() != 0 && currentAccount.getBillCycleDay().getDayOfMonthUTC() != 0) {
+        if (billCycleDay != null && currentAccount.getBillCycleDay() != null && currentAccount.getBillCycleDay().getDayOfMonthLocal() != 0 && currentAccount.getBillCycleDay().getDayOfMonthUTC() != 0) {
             // We can't just use .equals here as the BillCycleDay class might not have implemented it
             if ((billCycleDay.getDayOfMonthUTC() != currentAccount.getBillCycleDay().getDayOfMonthUTC() ||
                  billCycleDay.getDayOfMonthLocal() != currentAccount.getBillCycleDay().getDayOfMonthLocal())) {
-                throw new IllegalArgumentException(String.format("Killbill doesn't support updating the account BCD yet: this=%s, delegate=%s",
+                throw new IllegalArgumentException(String.format("Killbill doesn't support updating the account BCD yet: new=%s, current=%s",
                                                                  billCycleDay, currentAccount.getBillCycleDay()));
             }
-        } else {
+        } else if (billCycleDay != null) {
+            // Junction sets it
             accountData.setBillCycleDay(billCycleDay);
+        } else {
+            // Default to current value
+            accountData.setBillCycleDay(currentAccount.getBillCycleDay());
         }
 
+        // Set all updatable fields with the new values if non null, otherwise defaults to the current values
         accountData.setEmail(Objects.firstNonNull(email, currentAccount.getEmail()));
         accountData.setName(Objects.firstNonNull(name, currentAccount.getName()));
         accountData.setFirstNameLength(Objects.firstNonNull(firstNameLength, currentAccount.getFirstNameLength()));
diff --git a/account/src/test/java/com/ning/billing/account/dao/TestAccountDao.java b/account/src/test/java/com/ning/billing/account/dao/TestAccountDao.java
index 941b66f..3c41f1d 100644
--- a/account/src/test/java/com/ning/billing/account/dao/TestAccountDao.java
+++ b/account/src/test/java/com/ning/billing/account/dao/TestAccountDao.java
@@ -34,6 +34,7 @@ import com.ning.billing.account.api.BillCycleDay;
 import com.ning.billing.account.api.DefaultAccount;
 import com.ning.billing.account.api.DefaultAccountEmail;
 import com.ning.billing.account.api.DefaultBillCycleDay;
+import com.ning.billing.account.api.DefaultMutableAccountData;
 import com.ning.billing.account.api.MutableAccountData;
 import com.ning.billing.catalog.api.Currency;
 import com.ning.billing.mock.MockAccountBuilder;
@@ -321,6 +322,28 @@ public class TestAccountDao extends AccountDaoTestBase {
     }
 
     @Test(groups = "slow")
+    public void testShouldBeAbleToPassNullForSomeFieldsToAvoidUpdate() throws Exception {
+        final Account account = createTestAccount();
+        accountDao.create(account, context);
+
+        // Update the address and leave other fields null
+        final MutableAccountData mutableAccountData = new DefaultMutableAccountData(null, null, null, 0, null, null, null,
+                                                                                    null, null, null, null, null, null, null,
+                                                                                    null, null, null, null, false, false);
+        final String newAddress1 = UUID.randomUUID().toString();
+        mutableAccountData.setAddress1(newAddress1);
+
+        final DefaultAccount newAccount = new DefaultAccount(account.getId(), mutableAccountData);
+        accountDao.update(newAccount, context);
+
+        Assert.assertEquals(accountDao.getById(account.getId()).getAddress1(), newAddress1);
+        Assert.assertEquals(accountDao.getById(account.getId()).getAddress2(), account.getAddress2());
+        Assert.assertEquals(accountDao.getById(account.getId()).getCurrency(), account.getCurrency());
+        Assert.assertEquals(accountDao.getById(account.getId()).getExternalKey(), account.getExternalKey());
+        Assert.assertEquals(accountDao.getById(account.getId()).getBillCycleDay(), account.getBillCycleDay());
+    }
+
+    @Test(groups = "slow")
     public void testShouldBeAbleToHandleOtherBCDClass() throws Exception {
         final Account account = createTestAccount();
         accountDao.create(account, context);