Details
diff --git a/account/src/main/java/com/ning/billing/account/api/user/DefaultAccountUserApi.java b/account/src/main/java/com/ning/billing/account/api/user/DefaultAccountUserApi.java
index fd17c86..3e9074f 100644
--- a/account/src/main/java/com/ning/billing/account/api/user/DefaultAccountUserApi.java
+++ b/account/src/main/java/com/ning/billing/account/api/user/DefaultAccountUserApi.java
@@ -29,6 +29,7 @@ import com.ning.billing.account.api.MigrationAccountData;
import com.ning.billing.account.dao.AccountDao;
import com.ning.billing.util.clock.Clock;
import com.ning.billing.util.customfield.CustomField;
+import com.ning.billing.util.entity.EntityPersistenceException;
import com.ning.billing.util.tag.Tag;
public class DefaultAccountUserApi implements com.ning.billing.account.api.AccountUserApi {
@@ -47,7 +48,12 @@ public class DefaultAccountUserApi implements com.ning.billing.account.api.Accou
account.addFields(fields);
account.addTags(tags);
- dao.create(account);
+ try {
+ dao.create(account);
+ } catch (EntityPersistenceException e) {
+ throw new AccountApiException(e, ErrorCode.ACCOUNT_CREATION_FAILED);
+ }
+
return account;
}
@@ -73,7 +79,11 @@ public class DefaultAccountUserApi implements com.ning.billing.account.api.Accou
@Override
public void updateAccount(final Account account) throws AccountApiException {
- dao.update(account);
+ try {
+ dao.update(account);
+ } catch (EntityPersistenceException e) {
+ throw new AccountApiException(e, ErrorCode.ACCOUNT_UPDATE_FAILED);
+ }
}
@Override
@@ -82,8 +92,14 @@ public class DefaultAccountUserApi implements com.ning.billing.account.api.Accou
if(accountId == null) {
throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_KEY, externalKey);
}
- Account account = new DefaultAccount(accountId, accountData);
- dao.update(account);
+
+ Account account = new DefaultAccount(accountId, accountData);
+
+ try {
+ dao.update(account);
+ } catch (EntityPersistenceException e) {
+ throw new AccountApiException(e, ErrorCode.ACCOUNT_UPDATE_FAILED);
+ }
}
@Override
@@ -100,7 +116,12 @@ public class DefaultAccountUserApi implements com.ning.billing.account.api.Accou
account.addFields(fields);
account.addTags(tags);
- dao.create(account);
+ try {
+ dao.create(account);
+ } catch (EntityPersistenceException e) {
+ throw new AccountApiException(e, ErrorCode.ACCOUNT_CREATION_FAILED);
+ }
+
return account;
}
}
diff --git a/account/src/main/java/com/ning/billing/account/dao/AccountSqlDao.java b/account/src/main/java/com/ning/billing/account/dao/AccountSqlDao.java
index 42ec5a8..2eefcf0 100644
--- a/account/src/main/java/com/ning/billing/account/dao/AccountSqlDao.java
+++ b/account/src/main/java/com/ning/billing/account/dao/AccountSqlDao.java
@@ -66,7 +66,6 @@ public interface AccountSqlDao extends EntityDao<Account>, Transactional<Account
@SqlUpdate
public void update(@AccountBinder Account account);
- @Override
@SqlUpdate
public void deleteByKey(@Bind("externalKey") final String key);
diff --git a/account/src/main/java/com/ning/billing/account/dao/DefaultAccountDao.java b/account/src/main/java/com/ning/billing/account/dao/DefaultAccountDao.java
index 623aa01..6a1665e 100644
--- a/account/src/main/java/com/ning/billing/account/dao/DefaultAccountDao.java
+++ b/account/src/main/java/com/ning/billing/account/dao/DefaultAccountDao.java
@@ -20,6 +20,7 @@ import java.sql.DataTruncation;
import java.util.List;
import java.util.UUID;
+import com.ning.billing.util.entity.EntityPersistenceException;
import org.skife.jdbi.v2.IDBI;
import org.skife.jdbi.v2.Transaction;
import org.skife.jdbi.v2.TransactionStatus;
@@ -87,7 +88,7 @@ public class DefaultAccountDao implements AccountDao {
}
@Override
- public void create(final Account account) throws AccountApiException {
+ public void create(final Account account) throws EntityPersistenceException {
final String key = account.getExternalKey();
try {
accountSqlDao.inTransaction(new Transaction<Void, AccountSqlDao>() {
@@ -107,10 +108,10 @@ public class DefaultAccountDao implements AccountDao {
}
});
} catch (RuntimeException re) {
- if (re.getCause() instanceof AccountApiException) {
- throw (AccountApiException) re.getCause();
+ if (re.getCause() instanceof EntityPersistenceException) {
+ throw (EntityPersistenceException) re.getCause();
} else if (re.getCause() instanceof DataTruncation) {
- throw new AccountApiException(ErrorCode.DATA_TRUNCATION, re.getCause().getMessage());
+ throw new EntityPersistenceException(ErrorCode.DATA_TRUNCATION, re.getCause().getMessage());
} else {
throw re;
}
@@ -118,7 +119,7 @@ public class DefaultAccountDao implements AccountDao {
}
@Override
- public void update(final Account account) throws AccountApiException {
+ public void update(final Account account) throws EntityPersistenceException {
try {
accountSqlDao.inTransaction(new Transaction<Void, AccountSqlDao>() {
@Override
@@ -147,8 +148,8 @@ public class DefaultAccountDao implements AccountDao {
}
});
} catch (RuntimeException re) {
- if (re.getCause() instanceof AccountApiException) {
- throw (AccountApiException) re.getCause();
+ if (re.getCause() instanceof EntityPersistenceException) {
+ throw (EntityPersistenceException) re.getCause();
} else {
throw re;
}
diff --git a/account/src/test/java/com/ning/billing/account/dao/TestSimpleAccountDao.java b/account/src/test/java/com/ning/billing/account/dao/TestSimpleAccountDao.java
index 3c726aa..e3b3a1e 100644
--- a/account/src/test/java/com/ning/billing/account/dao/TestSimpleAccountDao.java
+++ b/account/src/test/java/com/ning/billing/account/dao/TestSimpleAccountDao.java
@@ -24,6 +24,7 @@ import static org.testng.Assert.fail;
import java.util.List;
import java.util.UUID;
+import com.ning.billing.util.entity.EntityPersistenceException;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.testng.annotations.Test;
@@ -69,7 +70,7 @@ public class TestSimpleAccountDao extends AccountDaoTestBase {
}
@Test
- public void testBasic() throws AccountApiException {
+ public void testBasic() throws EntityPersistenceException {
Account a = createTestAccountBuilder().build();
accountDao.create(a);
String key = a.getExternalKey();
@@ -89,7 +90,7 @@ public class TestSimpleAccountDao extends AccountDaoTestBase {
// simple test to ensure long phone numbers can be stored
@Test
- public void testLongPhoneNumber() throws AccountApiException {
+ public void testLongPhoneNumber() throws EntityPersistenceException {
Account account = createTestAccountBuilder().phone("123456789012345678901234").build();
accountDao.create(account);
@@ -99,13 +100,13 @@ public class TestSimpleAccountDao extends AccountDaoTestBase {
// simple test to ensure excessively long phone numbers cannot be stored
@Test(expectedExceptions = {AccountApiException.class})
- public void testOverlyLongPhoneNumber() throws AccountApiException {
+ public void testOverlyLongPhoneNumber() throws EntityPersistenceException {
Account account = createTestAccountBuilder().phone("12345678901234567890123456").build();
accountDao.create(account);
}
@Test
- public void testGetById() throws AccountApiException {
+ public void testGetById() throws EntityPersistenceException {
Account account = createTestAccountBuilder().build();
UUID id = account.getId();
String key = account.getExternalKey();
@@ -124,7 +125,7 @@ public class TestSimpleAccountDao extends AccountDaoTestBase {
}
@Test
- public void testCustomFields() throws AccountApiException {
+ public void testCustomFields() throws EntityPersistenceException {
Account account = createTestAccountBuilder().build();
String fieldName = "testField1";
String fieldValue = "testField1_value";
@@ -139,7 +140,7 @@ public class TestSimpleAccountDao extends AccountDaoTestBase {
}
@Test
- public void testTags() throws AccountApiException {
+ public void testTags() throws EntityPersistenceException {
Account account = createTestAccountBuilder().build();
TagDefinition definition = new DefaultTagDefinition("Test Tag", "For testing only", "Test System");
TagDefinitionSqlDao tagDescriptionDao = dbi.onDemand(TagDefinitionSqlDao.class);
@@ -161,7 +162,7 @@ public class TestSimpleAccountDao extends AccountDaoTestBase {
}
@Test
- public void testGetIdFromKey() throws AccountApiException {
+ public void testGetIdFromKey() throws EntityPersistenceException {
Account account = createTestAccountBuilder().build();
accountDao.create(account);
@@ -378,9 +379,9 @@ public class TestSimpleAccountDao extends AccountDaoTestBase {
null, null, null, null, null, null, null, null, null, null,null, null);
accountDao.update(updatedAccount);
}
-
+
@Test(groups={"slow"},enabled=true)
- public void testDelete() throws AccountApiException {
+ public void testDelete() throws AccountApiException, EntityPersistenceException {
Account a = createTestAccountBuilder().build();
accountDao.create(a);
diff --git a/api/src/main/java/com/ning/billing/ErrorCode.java b/api/src/main/java/com/ning/billing/ErrorCode.java
index a90c6aa..0ec7b4a 100644
--- a/api/src/main/java/com/ning/billing/ErrorCode.java
+++ b/api/src/main/java/com/ning/billing/ErrorCode.java
@@ -111,6 +111,8 @@ public enum ErrorCode {
ACCOUNT_DOES_NOT_EXIST_FOR_KEY(3003, "Account does not exist for key %s"),
ACCOUNT_CANNOT_MAP_NULL_KEY(3004, "An attempt was made to get the id for a <null> external key."),
ACCOUNT_CANNOT_CHANGE_EXTERNAL_KEY(3005, "External keys cannot be updated. Original key remains: %s"),
+ ACCOUNT_CREATION_FAILED(3006, "Account creation failed."),
+ ACCOUNT_UPDATE_FAILED(3007, "Account update failed."),
/*
*
diff --git a/api/src/main/java/com/ning/billing/util/entity/EntityPersistenceException.java b/api/src/main/java/com/ning/billing/util/entity/EntityPersistenceException.java
new file mode 100644
index 0000000..d1715c2
--- /dev/null
+++ b/api/src/main/java/com/ning/billing/util/entity/EntityPersistenceException.java
@@ -0,0 +1,20 @@
+package com.ning.billing.util.entity;
+
+import com.ning.billing.BillingExceptionBase;
+import com.ning.billing.ErrorCode;
+
+public class EntityPersistenceException extends BillingExceptionBase {
+ private static final long serialVersionUID = 1L;
+
+ public EntityPersistenceException(Throwable cause, int code, final String msg) {
+ super(cause, code, msg);
+ }
+
+ public EntityPersistenceException(Throwable cause, ErrorCode code, final Object... args) {
+ super(cause, code, args);
+ }
+
+ public EntityPersistenceException(ErrorCode code, final Object... args) {
+ super(code, args);
+ }
+}
diff --git a/payment/src/test/java/com/ning/billing/payment/api/TestPaymentApi.java b/payment/src/test/java/com/ning/billing/payment/api/TestPaymentApi.java
index 39ae479..e6ae543 100644
--- a/payment/src/test/java/com/ning/billing/payment/api/TestPaymentApi.java
+++ b/payment/src/test/java/com/ning/billing/payment/api/TestPaymentApi.java
@@ -26,6 +26,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.UUID;
+import com.ning.billing.util.entity.EntityPersistenceException;
import org.apache.commons.lang.RandomStringUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
@@ -63,7 +64,7 @@ public abstract class TestPaymentApi {
}
@Test(enabled=true)
- public void testCreateCreditCardPayment() throws AccountApiException {
+ public void testCreateCreditCardPayment() throws AccountApiException, EntityPersistenceException {
final DateTime now = new DateTime(DateTimeZone.UTC);
final Account account = testHelper.createTestCreditCardAccount();
final Invoice invoice = testHelper.createTestInvoice(account, now, Currency.USD);
@@ -114,7 +115,7 @@ public abstract class TestPaymentApi {
}
- private PaymentProviderAccount setupAccountWithPaypalPaymentMethod() throws AccountApiException {
+ private PaymentProviderAccount setupAccountWithPaypalPaymentMethod() throws AccountApiException, EntityPersistenceException {
final Account account = testHelper.createTestPayPalAccount();
paymentApi.createPaymentProviderAccount(account);
@@ -143,14 +144,14 @@ public abstract class TestPaymentApi {
}
@Test(enabled=true)
- public void testCreatePaypalPaymentMethod() throws AccountApiException {
+ public void testCreatePaypalPaymentMethod() throws AccountApiException, EntityPersistenceException {
PaymentProviderAccount account = setupAccountWithPaypalPaymentMethod();
assertNotNull(account);
Either<PaymentError, List<PaymentMethodInfo>> paymentMethodsOrError = paymentApi.getPaymentMethods(account.getAccountKey());
}
@Test(enabled=true)
- public void testUpdatePaymentProviderAccountContact() throws AccountApiException {
+ public void testUpdatePaymentProviderAccountContact() throws AccountApiException, EntityPersistenceException {
final Account account = testHelper.createTestPayPalAccount();
paymentApi.createPaymentProviderAccount(account);
@@ -172,7 +173,7 @@ public abstract class TestPaymentApi {
}
@Test(enabled=true)
- public void testCannotDeleteDefaultPaymentMethod() throws AccountApiException {
+ public void testCannotDeleteDefaultPaymentMethod() throws AccountApiException, EntityPersistenceException {
PaymentProviderAccount account = setupAccountWithPaypalPaymentMethod();
Either<PaymentError, Void> errorOrVoid = paymentApi.deletePaymentMethod(account.getAccountKey(), account.getDefaultPaymentMethodId());
@@ -181,7 +182,7 @@ public abstract class TestPaymentApi {
}
@Test(enabled=true)
- public void testDeleteNonDefaultPaymentMethod() throws AccountApiException {
+ public void testDeleteNonDefaultPaymentMethod() throws AccountApiException, EntityPersistenceException {
final Account account = testHelper.createTestPayPalAccount();
paymentApi.createPaymentProviderAccount(account);
diff --git a/payment/src/test/java/com/ning/billing/payment/TestHelper.java b/payment/src/test/java/com/ning/billing/payment/TestHelper.java
index 1ffda5c..7d164b1 100644
--- a/payment/src/test/java/com/ning/billing/payment/TestHelper.java
+++ b/payment/src/test/java/com/ning/billing/payment/TestHelper.java
@@ -19,13 +19,13 @@ package com.ning.billing.payment;
import java.math.BigDecimal;
import java.util.UUID;
+import com.ning.billing.util.entity.EntityPersistenceException;
import org.apache.commons.lang.RandomStringUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import com.google.inject.Inject;
import com.ning.billing.account.api.Account;
-import com.ning.billing.account.api.AccountApiException;
import com.ning.billing.account.api.user.AccountBuilder;
import com.ning.billing.account.dao.AccountDao;
import com.ning.billing.catalog.api.Currency;
@@ -46,7 +46,7 @@ public class TestHelper {
}
// These helper methods can be overridden in a plugin implementation
- public Account createTestCreditCardAccount() throws AccountApiException {
+ public Account createTestCreditCardAccount() throws EntityPersistenceException {
final String name = "First" + RandomStringUtils.randomAlphanumeric(5) + " " + "Last" + RandomStringUtils.randomAlphanumeric(5);
final String externalKey = RandomStringUtils.randomAlphanumeric(10);
final Account account = new AccountBuilder(UUID.randomUUID()).name(name)
@@ -61,7 +61,7 @@ public class TestHelper {
return account;
}
- public Account createTestPayPalAccount() throws AccountApiException {
+ public Account createTestPayPalAccount() throws EntityPersistenceException {
final String name = "First" + RandomStringUtils.randomAlphanumeric(5) + " " + "Last" + RandomStringUtils.randomAlphanumeric(5);
final String externalKey = RandomStringUtils.randomAlphanumeric(10);
final Account account = new AccountBuilder(UUID.randomUUID()).name(name)
diff --git a/payment/src/test/java/com/ning/billing/payment/TestNotifyInvoicePaymentApi.java b/payment/src/test/java/com/ning/billing/payment/TestNotifyInvoicePaymentApi.java
index e691f25..e38f14f 100644
--- a/payment/src/test/java/com/ning/billing/payment/TestNotifyInvoicePaymentApi.java
+++ b/payment/src/test/java/com/ning/billing/payment/TestNotifyInvoicePaymentApi.java
@@ -20,6 +20,7 @@ import static org.testng.Assert.assertNotNull;
import java.util.UUID;
+import com.ning.billing.util.entity.EntityPersistenceException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Guice;
@@ -63,7 +64,7 @@ public class TestNotifyInvoicePaymentApi {
}
@Test
- public void testNotifyPaymentSuccess() throws AccountApiException {
+ public void testNotifyPaymentSuccess() throws AccountApiException, EntityPersistenceException {
final Account account = testHelper.createTestCreditCardAccount();
final Invoice invoice = testHelper.createTestInvoice(account);
@@ -81,7 +82,7 @@ public class TestNotifyInvoicePaymentApi {
}
@Test
- public void testNotifyPaymentFailure() throws AccountApiException {
+ public void testNotifyPaymentFailure() throws AccountApiException, EntityPersistenceException {
final Account account = testHelper.createTestCreditCardAccount();
final Invoice invoice = testHelper.createTestInvoice(account);
diff --git a/util/src/main/java/com/ning/billing/util/entity/EntityDao.java b/util/src/main/java/com/ning/billing/util/entity/EntityDao.java
index 3e68158..4ef7d03 100644
--- a/util/src/main/java/com/ning/billing/util/entity/EntityDao.java
+++ b/util/src/main/java/com/ning/billing/util/entity/EntityDao.java
@@ -27,10 +27,10 @@ import com.ning.billing.account.api.AccountApiException;
public interface EntityDao<T extends Entity> {
@SqlUpdate
- public void create(@BindBean final T entity) throws AccountApiException;
+ public void create(@BindBean final T entity) throws EntityPersistenceException;
@SqlUpdate
- public void update(@BindBean final T entity) throws AccountApiException;
+ public void update(@BindBean final T entity) throws EntityPersistenceException;
@SqlQuery
public T getById(@Bind("id") final String id);
@@ -40,7 +40,4 @@ public interface EntityDao<T extends Entity> {
@SqlUpdate
public void test();
-
- @SqlUpdate
- public void deleteByKey(String key) throws AccountApiException;
}