ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From swa...@apache.org
Subject ambari git commit: AMBARI-14249. Failing unit tests / Entity removal using the entity manager instead of named queries. (Laszlo Puskas via swagle)
Date Mon, 07 Dec 2015 21:56:37 GMT
Repository: ambari
Updated Branches:
  refs/heads/branch-2.2 00134fe76 -> 893955a59


AMBARI-14249. Failing unit tests / Entity removal using the entity manager instead of named
queries. (Laszlo Puskas via swagle)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/893955a5
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/893955a5
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/893955a5

Branch: refs/heads/branch-2.2
Commit: 893955a598018e48e5745d02faa32b0fe440874c
Parents: 00134fe
Author: Siddharth Wagle <swagle@hortonworks.com>
Authored: Mon Dec 7 13:56:20 2015 -0800
Committer: Siddharth Wagle <swagle@hortonworks.com>
Committed: Mon Dec 7 13:56:20 2015 -0800

----------------------------------------------------------------------
 .../ambari/server/orm/dao/AlertDispatchDAO.java     | 11 +++++++----
 .../org/apache/ambari/server/orm/dao/AlertsDAO.java | 16 +++++++++-------
 .../server/orm/entities/AlertHistoryEntity.java     |  2 ++
 .../server/orm/entities/AlertNoticeEntity.java      |  1 +
 .../server/orm/dao/AlertDefinitionDAOTest.java      |  3 ++-
 .../ambari/server/orm/dao/AlertDispatchDAOTest.java | 11 ++++++++++-
 .../apache/ambari/server/orm/dao/AlertsDAOTest.java | 12 ++++++++++++
 .../ambari/server/orm/dao/RequestDAOTest.java       |  3 +++
 8 files changed, 46 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/893955a5/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDispatchDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDispatchDAO.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDispatchDAO.java
index 1f1aa45..d73c70e 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDispatchDAO.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDispatchDAO.java
@@ -684,12 +684,15 @@ public class AlertDispatchDAO {
   @Transactional
   public void removeNoticeByDefinitionId(long definitionId) {
     EntityManager entityManager = entityManagerProvider.get();
-    TypedQuery<AlertNoticeEntity> currentQuery = entityManager.createNamedQuery(
-        "AlertNoticeEntity.removeByDefinitionId", AlertNoticeEntity.class);
 
+    TypedQuery<AlertNoticeEntity> currentQuery = entityManager.createNamedQuery(
+        "AlertNoticeEntity.findByDefinitionId", AlertNoticeEntity.class);
     currentQuery.setParameter("definitionId", definitionId);
-    currentQuery.executeUpdate();
-    entityManager.clear();
+
+    for (AlertNoticeEntity alertNoticeEntity : currentQuery.getResultList()) {
+      entityManager.remove(alertNoticeEntity);
+    }
+
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/ambari/blob/893955a5/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertsDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertsDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertsDAO.java
index 145f841..1c7d0c1 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertsDAO.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertsDAO.java
@@ -736,18 +736,20 @@ public class AlertsDAO {
   public void removeByDefinitionId(long definitionId) {
     EntityManager entityManager = m_entityManagerProvider.get();
     TypedQuery<AlertCurrentEntity> currentQuery = entityManager.createNamedQuery(
-        "AlertCurrentEntity.removeByDefinitionId", AlertCurrentEntity.class);
-
+        "AlertCurrentEntity.findByDefinitionId", AlertCurrentEntity.class);
     currentQuery.setParameter("definitionId", definitionId);
-    currentQuery.executeUpdate();
 
-    TypedQuery<AlertHistoryEntity> historyQuery = entityManager.createNamedQuery(
-        "AlertHistoryEntity.removeByDefinitionId", AlertHistoryEntity.class);
+    for (AlertCurrentEntity alertCurrentEntity : currentQuery.getResultList()) {
+      entityManager.remove(alertCurrentEntity);
+    }
 
+    TypedQuery<AlertHistoryEntity> historyQuery = entityManager.createNamedQuery(
+        "AlertHistoryEntity.findByDefinitionId", AlertHistoryEntity.class);
     historyQuery.setParameter("definitionId", definitionId);
-    historyQuery.executeUpdate();
 
-    entityManager.clear();
+    for (AlertHistoryEntity alertHistoryEntity : historyQuery.getResultList()) {
+      entityManager.remove(alertHistoryEntity);
+    }
 
     // if caching is enabled, invalidate the cache to force the latest values
     // back from the DB

http://git-wip-us.apache.org/repos/asf/ambari/blob/893955a5/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertHistoryEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertHistoryEntity.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertHistoryEntity.java
index 9091398..6963eeb 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertHistoryEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertHistoryEntity.java
@@ -51,6 +51,8 @@ import org.apache.ambari.server.state.AlertState;
     @NamedQuery(name = "AlertHistoryEntity.findAllInClusterBetweenDates", query = "SELECT
alertHistory FROM AlertHistoryEntity alertHistory WHERE alertHistory.clusterId = :clusterId
AND alertHistory.alertTimestamp BETWEEN :startDate AND :endDate"),
     @NamedQuery(name = "AlertHistoryEntity.findAllInClusterBeforeDate", query = "SELECT alertHistory
FROM AlertHistoryEntity alertHistory WHERE alertHistory.clusterId = :clusterId AND alertHistory.alertTimestamp
<= :beforeDate"),
     @NamedQuery(name = "AlertHistoryEntity.findAllInClusterAfterDate", query = "SELECT alertHistory
FROM AlertHistoryEntity alertHistory WHERE alertHistory.clusterId = :clusterId AND alertHistory.alertTimestamp
>= :afterDate"),
+    @NamedQuery(name = "AlertHistoryEntity.findByDefinitionId", query = "SELECT alertHistory
FROM AlertHistoryEntity " +
+        "alertHistory WHERE alertHistory.alertDefinition.definitionId = :definitionId"),
     @NamedQuery(name = "AlertHistoryEntity.removeByDefinitionId", query = "DELETE FROM AlertHistoryEntity
alertHistory WHERE alertHistory.alertDefinition.definitionId = :definitionId") })
 public class AlertHistoryEntity {
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/893955a5/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertNoticeEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertNoticeEntity.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertNoticeEntity.java
index c2f61b3..777321e 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertNoticeEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertNoticeEntity.java
@@ -49,6 +49,7 @@ import org.apache.ambari.server.state.NotificationState;
     @NamedQuery(name = "AlertNoticeEntity.findAll", query = "SELECT notice FROM AlertNoticeEntity
notice"),
     @NamedQuery(name = "AlertNoticeEntity.findByState", query = "SELECT notice FROM AlertNoticeEntity
notice WHERE notice.notifyState = :notifyState"),
     @NamedQuery(name = "AlertNoticeEntity.findByUuid", query = "SELECT notice FROM AlertNoticeEntity
notice WHERE notice.uuid = :uuid"),
+    @NamedQuery(name = "AlertNoticeEntity.findByDefinitionId", query = "SELECT notice FROM
AlertNoticeEntity notice WHERE notice.alertHistory.alertDefinition.definitionId = :definitionId"),
     @NamedQuery(name = "AlertNoticeEntity.removeByDefinitionId", query = "DELETE FROM AlertNoticeEntity
notice WHERE notice.alertHistory.alertDefinition.definitionId = :definitionId") })
 public class AlertNoticeEntity {
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/893955a5/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAOTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAOTest.java
index d0ad386..c7f1de9 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAOTest.java
@@ -381,7 +381,8 @@ public class AlertDefinitionDAOTest {
     ClusterDAO clusterDAO = injector.getInstance(ClusterDAO.class);
     ClusterEntity clusterEntity = clusterDAO.findById(clusterId);
     clusterDAO.refresh(clusterEntity);
-
+    injector.getInstance(UnitOfWork.class).end();
+    injector.getInstance(UnitOfWork.class).begin();
     Clusters clusters = injector.getInstance(Clusters.class);
     Cluster cluster = clusters.getClusterById(clusterId);
     cluster.delete();

http://git-wip-us.apache.org/repos/asf/ambari/blob/893955a5/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDispatchDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDispatchDAOTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDispatchDAOTest.java
index 9b269b4..f6b30ed 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDispatchDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDispatchDAOTest.java
@@ -513,6 +513,8 @@ public class AlertDispatchDAOTest {
     target = m_dao.findTargetById(target.getTargetId());
     assertNull(target);
 
+    m_dao.refresh(group);
+
     group = m_dao.findGroupById(group.getGroupId());
     assertNotNull(group);
 
@@ -663,7 +665,9 @@ public class AlertDispatchDAOTest {
   public void testAlertNoticePredicate() throws Exception {
     Cluster cluster = m_helper.buildNewCluster(m_clusters, m_serviceFactory,
         m_componentFactory, m_schFactory, HOSTNAME);
-
+    // end the UnitOfWork to clean the cache
+    m_injector.getInstance(UnitOfWork.class).end();
+    m_injector.getInstance(UnitOfWork.class).begin();
     m_alertHelper.populateData(cluster);
 
     Predicate clusterPredicate = null;
@@ -742,6 +746,9 @@ public class AlertDispatchDAOTest {
     Cluster cluster = m_helper.buildNewCluster(m_clusters, m_serviceFactory,
         m_componentFactory, m_schFactory, HOSTNAME);
 
+    m_injector.getInstance(UnitOfWork.class).end();
+    m_injector.getInstance(UnitOfWork.class).begin();
+
     m_alertHelper.populateData(cluster);
 
     AlertNoticeRequest request = new AlertNoticeRequest();
@@ -782,6 +789,8 @@ public class AlertDispatchDAOTest {
   public void testAlertNoticeSorting() throws Exception {
     Cluster cluster = m_helper.buildNewCluster(m_clusters, m_serviceFactory,
         m_componentFactory, m_schFactory, HOSTNAME);
+    m_injector.getInstance(UnitOfWork.class).end();
+    m_injector.getInstance(UnitOfWork.class).begin();
 
     m_alertHelper.populateData(cluster);
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/893955a5/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertsDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertsDAOTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertsDAOTest.java
index 0bbe998..b224203 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertsDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertsDAOTest.java
@@ -1175,6 +1175,10 @@ public class AlertsDAOTest {
   public void testAlertHistoryPredicate() throws Exception {
     m_helper.installHdfsService(m_cluster, m_serviceFactory,
         m_componentFactory, m_schFactory, HOSTNAME);
+
+    m_injector.getInstance(UnitOfWork.class).end();
+    m_injector.getInstance(UnitOfWork.class).begin();
+
     m_alertHelper.populateData(m_cluster);
 
     Predicate clusterPredicate = null;
@@ -1268,6 +1272,10 @@ public class AlertsDAOTest {
   public void testAlertHistoryPagination() throws Exception {
     m_helper.installHdfsService(m_cluster, m_serviceFactory,
         m_componentFactory, m_schFactory, HOSTNAME);
+
+    m_injector.getInstance(UnitOfWork.class).end();
+    m_injector.getInstance(UnitOfWork.class).begin();
+
     m_alertHelper.populateData(m_cluster);
 
     AlertHistoryRequest request = new AlertHistoryRequest();
@@ -1308,6 +1316,10 @@ public class AlertsDAOTest {
   public void testAlertHistorySorting() throws Exception {
     m_helper.installHdfsService(m_cluster, m_serviceFactory,
         m_componentFactory, m_schFactory, HOSTNAME);
+
+    m_injector.getInstance(UnitOfWork.class).end();
+    m_injector.getInstance(UnitOfWork.class).begin();
+
     m_alertHelper.populateData(m_cluster);
 
     List<SortRequestProperty> sortProperties = new ArrayList<SortRequestProperty>();

http://git-wip-us.apache.org/repos/asf/ambari/blob/893955a5/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/RequestDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/RequestDAOTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/RequestDAOTest.java
index edc1428..5509869 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/RequestDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/RequestDAOTest.java
@@ -48,6 +48,7 @@ import org.junit.Test;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
 import com.google.inject.persist.PersistService;
+import com.google.inject.persist.UnitOfWork;
 
 /**
  * RequestDAO unit tests
@@ -71,11 +72,13 @@ public class RequestDAOTest {
     hostRoleCommandDAO = injector.getInstance(HostRoleCommandDAO.class);
     hostDAO = injector.getInstance(HostDAO.class);
     requestDAO = injector.getInstance(RequestDAO.class);
+    injector.getInstance(UnitOfWork.class).begin();
 
   }
 
   @After
   public void teardown() throws AmbariException {
+    injector.getInstance(UnitOfWork.class).end();
     injector.getInstance(PersistService.class).stop();
   }
 


Mime
View raw message