killbill-memoizeit

util: Fix EntityDaoBase#create to throw the correct exception

11/3/2017 11:35:23 PM

Details

diff --git a/util/src/main/java/org/killbill/billing/util/entity/dao/EntityDaoBase.java b/util/src/main/java/org/killbill/billing/util/entity/dao/EntityDaoBase.java
index f06589f..8f02941 100644
--- a/util/src/main/java/org/killbill/billing/util/entity/dao/EntityDaoBase.java
+++ b/util/src/main/java/org/killbill/billing/util/entity/dao/EntityDaoBase.java
@@ -18,7 +18,11 @@
 
 package org.killbill.billing.util.entity.dao;
 
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.List;
 import java.util.UUID;
 
 import org.killbill.billing.BillingExceptionBase;
@@ -32,39 +36,78 @@ import org.killbill.billing.util.entity.Pagination;
 import org.killbill.billing.util.entity.dao.DefaultPaginationSqlDaoHelper.Ordering;
 import org.killbill.billing.util.entity.dao.DefaultPaginationSqlDaoHelper.PaginationIteratorBuilder;
 
+import com.google.common.collect.ImmutableList;
+
 public abstract class EntityDaoBase<M extends EntityModelDao<E>, E extends Entity, U extends BillingExceptionBase> implements EntityDao<M, E, U> {
 
     protected final EntitySqlDaoTransactionalJdbiWrapper transactionalSqlDao;
     protected final DefaultPaginationSqlDaoHelper paginationHelper;
 
     private final Class<? extends EntitySqlDao<M, E>> realSqlDao;
+    private final Class<U> targetExceptionClass;
 
     public EntityDaoBase(final EntitySqlDaoTransactionalJdbiWrapper transactionalSqlDao, final Class<? extends EntitySqlDao<M, E>> realSqlDao) {
         this.transactionalSqlDao = transactionalSqlDao;
         this.realSqlDao = realSqlDao;
         this.paginationHelper = new DefaultPaginationSqlDaoHelper(transactionalSqlDao);
+
+        try {
+            final Type genericSuperclass = this.getClass().getGenericSuperclass();
+            targetExceptionClass = (genericSuperclass instanceof ParameterizedType) ?
+                (Class<U>) Class.forName(((ParameterizedType) genericSuperclass).getActualTypeArguments()[2].getTypeName()) :
+                                   null; // Mock class
+        } catch (final ClassNotFoundException e) {
+            throw new RuntimeException(e);
+        }
     }
 
     @Override
     public void create(final M entity, final InternalCallContext context) throws U {
-        final M refreshedEntity = transactionalSqlDao.execute(getCreateEntitySqlDaoTransactionWrapper(entity, context));
+        final M refreshedEntity = transactionalSqlDao.execute(targetExceptionClass, getCreateEntitySqlDaoTransactionWrapper(entity, context));
         // Populate the caches only after the transaction has been committed, in case of rollbacks
         transactionalSqlDao.populateCaches(refreshedEntity);
     }
 
-    protected EntitySqlDaoTransactionWrapper<M> getCreateEntitySqlDaoTransactionWrapper(final M entity, final InternalCallContext context) {
-        return new EntitySqlDaoTransactionWrapper<M>() {
+
+    public void create(final Iterable<M> entities, final InternalCallContext context) throws U {
+        final List<M> refreshedEntities = transactionalSqlDao.execute(targetExceptionClass, getCreateEntitySqlDaoTransactionWrapper(entities, context));
+        // Populate the caches only after the transaction has been committed, in case of rollbacks
+        for (M refreshedEntity : refreshedEntities) {
+            transactionalSqlDao.populateCaches(refreshedEntity);
+        }
+    }
+
+
+    protected EntitySqlDaoTransactionWrapper<List<M>> getCreateEntitySqlDaoTransactionWrapper(final Iterable<M> entities, final InternalCallContext context) {
+
+        final List<M> result = new ArrayList<M>();
+        return new EntitySqlDaoTransactionWrapper<List<M>>() {
             @Override
-            public M inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception {
+            public List<M> inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception {
+
                 final EntitySqlDao<M, E> transactional = entitySqlDaoWrapperFactory.become(realSqlDao);
 
-                if (checkEntityAlreadyExists(transactional, entity, context)) {
-                    throw generateAlreadyExistsException(entity, context);
+                for (M entity : entities) {
+                    if (checkEntityAlreadyExists(transactional, entity, context)) {
+                        throw generateAlreadyExistsException(entity, context);
+                    }
+                    final M refreshedEntity = createAndRefresh(transactional, entity, context);
+                    result.add(refreshedEntity);
+                    postBusEventFromTransaction(entity, refreshedEntity, ChangeType.INSERT, entitySqlDaoWrapperFactory, context);
                 }
-                final M refreshedEntity = createAndRefresh(transactional, entity, context);
+                return result;
+            }
+        };
+    }
 
-                postBusEventFromTransaction(entity, refreshedEntity, ChangeType.INSERT, entitySqlDaoWrapperFactory, context);
-                return refreshedEntity;
+
+    protected EntitySqlDaoTransactionWrapper<M> getCreateEntitySqlDaoTransactionWrapper(final M entity, final InternalCallContext context) {
+        final EntitySqlDaoTransactionWrapper<List<M>> entityWrapperList = getCreateEntitySqlDaoTransactionWrapper(ImmutableList.<M>of(entity), context);
+        return new EntitySqlDaoTransactionWrapper<M>() {
+            @Override
+            public M inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception {
+                final List<M> result = entityWrapperList.inTransaction(entitySqlDaoWrapperFactory);
+                return result.isEmpty() ? null : result.get(0);
             }
         };
     }
diff --git a/util/src/main/java/org/killbill/billing/util/entity/dao/EntitySqlDaoTransactionalJdbiWrapper.java b/util/src/main/java/org/killbill/billing/util/entity/dao/EntitySqlDaoTransactionalJdbiWrapper.java
index b16601d..0366e3a 100644
--- a/util/src/main/java/org/killbill/billing/util/entity/dao/EntitySqlDaoTransactionalJdbiWrapper.java
+++ b/util/src/main/java/org/killbill/billing/util/entity/dao/EntitySqlDaoTransactionalJdbiWrapper.java
@@ -18,6 +18,8 @@
 
 package org.killbill.billing.util.entity.dao;
 
+import javax.annotation.Nullable;
+
 import org.killbill.billing.util.cache.CacheControllerDispatcher;
 import org.killbill.billing.util.callcontext.InternalCallContextFactory;
 import org.killbill.billing.util.dao.NonEntityDao;
@@ -102,11 +104,11 @@ public class EntitySqlDaoTransactionalJdbiWrapper {
      * @param <E>                            checked exception which can be thrown from the transaction
      * @return result from the transaction fo type ReturnType
      */
-    public <ReturnType, E extends Exception> ReturnType execute(final Class<E> exception, final EntitySqlDaoTransactionWrapper<ReturnType> entitySqlDaoTransactionWrapper) throws E {
+    public <ReturnType, E extends Exception> ReturnType execute(@Nullable final Class<E> exception, final EntitySqlDaoTransactionWrapper<ReturnType> entitySqlDaoTransactionWrapper) throws E {
         try {
             return execute(entitySqlDaoTransactionWrapper);
         } catch (RuntimeException e) {
-            if (e.getCause() != null && e.getCause().getClass().isAssignableFrom(exception)) {
+            if (e.getCause() != null && exception != null && e.getCause().getClass().isAssignableFrom(exception)) {
                 throw (E) e.getCause();
             } else if (e.getCause() != null && e.getCause() instanceof RuntimeException) {
                 throw (RuntimeException) e.getCause();
diff --git a/util/src/test/java/org/killbill/billing/util/dao/Kombucha.java b/util/src/test/java/org/killbill/billing/util/dao/Kombucha.java
new file mode 100644
index 0000000..dd081f2
--- /dev/null
+++ b/util/src/test/java/org/killbill/billing/util/dao/Kombucha.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright 2014-2017 Groupon, Inc
+ * Copyright 2014-2017 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
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.killbill.billing.util.dao;
+
+import org.killbill.billing.util.entity.Entity;
+
+interface Kombucha extends Entity {}
diff --git a/util/src/test/java/org/killbill/billing/util/dao/KombuchaModelDao.java b/util/src/test/java/org/killbill/billing/util/dao/KombuchaModelDao.java
new file mode 100644
index 0000000..b9fe468
--- /dev/null
+++ b/util/src/test/java/org/killbill/billing/util/dao/KombuchaModelDao.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright 2014-2017 Groupon, Inc
+ * Copyright 2014-2017 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
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.killbill.billing.util.dao;
+
+import org.killbill.billing.util.entity.dao.EntityModelDao;
+
+interface KombuchaModelDao extends EntityModelDao<Kombucha> {}
diff --git a/util/src/test/java/org/killbill/billing/util/dao/KombuchaSqlDao.java b/util/src/test/java/org/killbill/billing/util/dao/KombuchaSqlDao.java
new file mode 100644
index 0000000..a5827f6
--- /dev/null
+++ b/util/src/test/java/org/killbill/billing/util/dao/KombuchaSqlDao.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2014-2017 Groupon, Inc
+ * Copyright 2014-2017 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
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.killbill.billing.util.dao;
+
+import org.killbill.billing.util.entity.dao.EntitySqlDao;
+import org.killbill.commons.jdbi.template.KillBillSqlDaoStringTemplate;
+import org.skife.jdbi.v2.sqlobject.SqlQuery;
+
+@KillBillSqlDaoStringTemplate("/org/killbill/billing/util/dao/Kombucha.sql.stg")
+interface KombuchaSqlDao extends EntitySqlDao<KombuchaModelDao, Kombucha> {
+
+    @SqlQuery
+    public boolean isIsTimeForKombucha();
+}
diff --git a/util/src/test/java/org/killbill/billing/util/dao/TestEntityBaseDaoException.java b/util/src/test/java/org/killbill/billing/util/dao/TestEntityBaseDaoException.java
new file mode 100644
index 0000000..82d8eaa
--- /dev/null
+++ b/util/src/test/java/org/killbill/billing/util/dao/TestEntityBaseDaoException.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2014-2017 Groupon, Inc
+ * Copyright 2014-2017 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
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.killbill.billing.util.dao;
+
+import java.util.UUID;
+
+import org.joda.time.DateTime;
+import org.killbill.billing.ErrorCode;
+import org.killbill.billing.callcontext.InternalCallContext;
+import org.killbill.billing.security.SecurityApiException;
+import org.killbill.billing.util.UtilTestSuiteWithEmbeddedDB;
+import org.killbill.billing.util.entity.dao.EntityDaoBase;
+import org.killbill.billing.util.entity.dao.EntitySqlDao;
+import org.killbill.billing.util.entity.dao.EntitySqlDaoTransactionalJdbiWrapper;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class TestEntityBaseDaoException extends UtilTestSuiteWithEmbeddedDB {
+
+    public static class TestEntityBaseDao extends EntityDaoBase<KombuchaModelDao, Kombucha, SecurityApiException> {
+
+        public TestEntityBaseDao(final EntitySqlDaoTransactionalJdbiWrapper transactionalSqlDao, final Class<? extends EntitySqlDao<KombuchaModelDao, Kombucha>> realSqlDao) {
+            super(transactionalSqlDao, realSqlDao);
+        }
+
+        @Override
+        protected SecurityApiException generateAlreadyExistsException(final KombuchaModelDao entity, final InternalCallContext context) {
+            return new SecurityApiException(ErrorCode.__UNKNOWN_ERROR_CODE);
+        }
+
+        @Override
+        protected boolean checkEntityAlreadyExists(final EntitySqlDao<KombuchaModelDao, Kombucha> KombuchaSqlDao, final KombuchaModelDao entity, final InternalCallContext context) {
+            return true;
+        }
+    }
+
+    @Test(groups = "slow")
+    public void testWithCreateException() throws Exception {
+        final EntitySqlDaoTransactionalJdbiWrapper entitySqlDaoTransactionalJdbiWrapper = new EntitySqlDaoTransactionalJdbiWrapper(dbi, clock, null, nonEntityDao, null);
+        final TestEntityBaseDao test = new TestEntityBaseDao(entitySqlDaoTransactionalJdbiWrapper, KombuchaSqlDao.class);
+
+        final KombuchaModelDao entity = new KombuchaModelDao() {
+            @Override
+            public Long getRecordId() {
+                return null;
+            }
+            @Override
+            public Long getAccountRecordId() {
+                return null;
+            }
+            @Override
+            public Long getTenantRecordId() {
+                return null;
+            }
+            @Override
+            public TableName getTableName() {
+                return null;
+            }
+            @Override
+            public TableName getHistoryTableName() {
+                return null;
+            }
+            @Override
+            public UUID getId() {
+                return null;
+            }
+            @Override
+            public DateTime getCreatedDate() {
+                return null;
+            }
+            @Override
+            public DateTime getUpdatedDate() {
+                return null;
+            }
+        };
+
+        try {
+            test.create(entity, internalCallContext);
+            Assert.fail("test should throw SecurityApiException");
+        } catch (final SecurityApiException e) {
+            Assert.assertEquals(e.getCode(), ErrorCode.__UNKNOWN_ERROR_CODE.getCode());
+        }
+
+    }
+
+}
diff --git a/util/src/test/java/org/killbill/billing/util/dao/TestStringTemplateInheritanceWithJdbi.java b/util/src/test/java/org/killbill/billing/util/dao/TestStringTemplateInheritanceWithJdbi.java
index d2dc3a1..fa36842 100644
--- a/util/src/test/java/org/killbill/billing/util/dao/TestStringTemplateInheritanceWithJdbi.java
+++ b/util/src/test/java/org/killbill/billing/util/dao/TestStringTemplateInheritanceWithJdbi.java
@@ -16,29 +16,13 @@
 
 package org.killbill.billing.util.dao;
 
-import org.skife.jdbi.v2.sqlobject.SqlQuery;
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
 import org.killbill.billing.util.UtilTestSuiteWithEmbeddedDB;
-import org.killbill.billing.util.entity.Entity;
-import org.killbill.billing.util.entity.dao.EntityModelDao;
-import org.killbill.billing.util.entity.dao.EntitySqlDao;
-import org.killbill.commons.jdbi.template.KillBillSqlDaoStringTemplate;
 
 public class TestStringTemplateInheritanceWithJdbi extends UtilTestSuiteWithEmbeddedDB {
 
-    private static interface Kombucha extends Entity {}
-
-    private static interface KombuchaModelDao extends EntityModelDao<Kombucha> {}
-
-    @KillBillSqlDaoStringTemplate("/org/killbill/billing/util/dao/Kombucha.sql.stg")
-    private static interface KombuchaSqlDao extends EntitySqlDao<KombuchaModelDao, Kombucha> {
-
-        @SqlQuery
-        public boolean isIsTimeForKombucha();
-    }
-
     @Test(groups = "slow")
     public void testInheritQueries() throws Exception {
         final KombuchaSqlDao dao = dbi.onDemand(KombuchaSqlDao.class);