ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nc...@apache.org
Subject git commit: AMBARI-6547. Alerts: Updates to DAOs for Dispatch and History (Jonathan Hurley via ncole)
Date Mon, 21 Jul 2014 14:38:05 GMT
Repository: ambari
Updated Branches:
  refs/heads/trunk 8afdb0b23 -> 26a07aacc


AMBARI-6547. Alerts: Updates to DAOs for Dispatch and History (Jonathan Hurley via ncole)


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

Branch: refs/heads/trunk
Commit: 26a07aacc6b44712c8dfd1b562cdb1a4b7b8df54
Parents: 8afdb0b
Author: Nate Cole <ncole@hortonworks.com>
Authored: Mon Jul 21 10:37:45 2014 -0400
Committer: Nate Cole <ncole@hortonworks.com>
Committed: Mon Jul 21 10:37:45 2014 -0400

----------------------------------------------------------------------
 .../ambari/server/orm/dao/AlertDispatchDAO.java |   6 +-
 .../apache/ambari/server/orm/dao/AlertsDAO.java | 103 ++++++++++-
 .../server/orm/entities/AlertCurrentEntity.java |  11 +-
 .../orm/entities/AlertDefinitionEntity.java     |   4 +-
 .../server/orm/entities/AlertGroupEntity.java   |   2 +-
 .../server/orm/entities/AlertHistoryEntity.java |   4 +-
 .../server/orm/entities/AlertNoticeEntity.java  |   2 +-
 .../server/orm/entities/AlertTargetEntity.java  |  13 +-
 .../entities/ServiceDesiredStateEntityPK.java   |   2 +-
 .../apache/ambari/server/orm/OrmTestHelper.java |  76 ++++++--
 .../server/orm/dao/AlertDefinitionDAOTest.java  | 112 +++++++++---
 .../server/orm/dao/AlertDispatchDAOTest.java    | 144 +++++++++++++++
 .../ambari/server/orm/dao/AlertsDAOTest.java    | 176 +++++++++++++++++++
 13 files changed, 590 insertions(+), 65 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/26a07aac/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 0fa803b..08c49d8 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
@@ -84,7 +84,7 @@ public class AlertDispatchDAO {
   @RequiresSession
   public AlertGroupEntity findGroupByName(String groupName) {
     TypedQuery<AlertGroupEntity> query = entityManagerProvider.get().createNamedQuery(
-        "AlertGroup.findByName", AlertGroupEntity.class);
+        "AlertGroupEntity.findByName", AlertGroupEntity.class);
 
     query.setParameter("groupName", groupName);
 
@@ -104,7 +104,7 @@ public class AlertDispatchDAO {
   @RequiresSession
   public AlertGroupEntity findGroupByName(long clusterId, String groupName) {
     TypedQuery<AlertGroupEntity> query = entityManagerProvider.get().createNamedQuery(
-        "AlertGroup.findByNameInCluster", AlertGroupEntity.class);
+        "AlertGroupEntity.findByNameInCluster", AlertGroupEntity.class);
 
     query.setParameter("clusterId", clusterId);
     query.setParameter("groupName", groupName);
@@ -123,7 +123,7 @@ public class AlertDispatchDAO {
   @RequiresSession
   public AlertTargetEntity findTargetByName(String targetName) {
     TypedQuery<AlertTargetEntity> query = entityManagerProvider.get().createNamedQuery(
-        "AlertGroup.findByName", AlertTargetEntity.class);
+        "AlertTargetEntity.findByName", AlertTargetEntity.class);
 
     query.setParameter("targetName", targetName);
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/26a07aac/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 9f36860..f9f1f45 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
@@ -33,8 +33,8 @@ import com.google.inject.persist.Transactional;
 
 /**
  * The {@link AlertsDAO} class manages the {@link AlertHistoryEntity} and
- * {@link AlertCurrentEntity} instances.Each {@link AlertHistoryEntity} is known
- * as an "alert" that has been triggered and received.
+ * {@link AlertCurrentEntity} instances. Each {@link AlertHistoryEntity} is
+ * known as an "alert" that has been triggered and received.
  */
 @Singleton
 public class AlertsDAO {
@@ -65,7 +65,7 @@ public class AlertsDAO {
   /**
    * Gets all alerts stored in the database across all clusters.
    * 
-   * @return all alerts or empty list if none exist (never {@code null}).
+   * @return all alerts or an empty list if none exist (never {@code null}).
    */
   @RequiresSession
   public List<AlertHistoryEntity> findAll() {
@@ -78,7 +78,7 @@ public class AlertsDAO {
   /**
    * Gets all alerts stored in the database for the given cluster.
    * 
-   * @return all alerts in the specified cluster or empty list if none exist
+   * @return all alerts in the specified cluster or an empty list if none exist
    *         (never {@code null}).
    */
   @RequiresSession
@@ -92,6 +92,56 @@ public class AlertsDAO {
   }
 
   /**
+   * Gets the current alerts.
+   * 
+   * @return the current alerts or an empty list if none exist (never
+   *         {@code null}).
+   */
+  @RequiresSession
+  public List<AlertCurrentEntity> findCurrent() {
+    TypedQuery<AlertCurrentEntity> query = entityManagerProvider.get().createNamedQuery(
+        "AlertCurrentEntity.findAll", AlertCurrentEntity.class);
+
+    return daoUtils.selectList(query);
+  }
+
+  /**
+   * Gets the current alerts for a given service.
+   * 
+   * @return the current alerts for the given service or an empty list if none
+   *         exist (never {@code null}).
+   */
+  @RequiresSession
+  public List<AlertCurrentEntity> findCurrentByService(long clusterId,
+      String serviceName) {
+    TypedQuery<AlertCurrentEntity> query = entityManagerProvider.get().createNamedQuery(
+        "AlertCurrentEntity.findByService", AlertCurrentEntity.class);
+
+    query.setParameter("clusterId", clusterId);
+    query.setParameter("serviceName", serviceName);
+
+    return daoUtils.selectList(query);
+  }
+
+  /**
+   * Gets the current alerts for a given host.
+   * 
+   * @return the current alerts for the given host or an empty list if none
+   *         exist (never {@code null}).
+   */
+  @RequiresSession
+  public List<AlertCurrentEntity> findCurrentByHost(long clusterId,
+      String hostName) {
+    TypedQuery<AlertCurrentEntity> query = entityManagerProvider.get().createNamedQuery(
+        "AlertCurrentEntity.findByHost", AlertCurrentEntity.class);
+
+    query.setParameter("clusterId", clusterId);
+    query.setParameter("hostName", hostName);
+
+    return daoUtils.selectList(query);
+  }
+
+  /**
    * Persists a new alert.
    * 
    * @param alert
@@ -135,4 +185,49 @@ public class AlertsDAO {
   public void remove(AlertHistoryEntity alert) {
     entityManagerProvider.get().remove(merge(alert));
   }
+
+  /**
+   * Persists a new current alert.
+   * 
+   * @param alert
+   *          the current alert to persist (not {@code null}).
+   */
+  @Transactional
+  public void create(AlertCurrentEntity alert) {
+    entityManagerProvider.get().persist(alert);
+  }
+
+  /**
+   * Refresh the state of the current alert from the database.
+   * 
+   * @param alert
+   *          the current alert to refresh (not {@code null}).
+   */
+  @Transactional
+  public void refresh(AlertCurrentEntity alert) {
+    entityManagerProvider.get().refresh(alert);
+  }
+
+  /**
+   * Merge the speicified current alert with the existing alert in the database.
+   * 
+   * @param alert
+   *          the current alert to merge (not {@code null}).
+   * @return the updated current alert with merged content (never {@code null}).
+   */
+  @Transactional
+  public AlertCurrentEntity merge(AlertCurrentEntity alert) {
+    return entityManagerProvider.get().merge(alert);
+  }
+
+  /**
+   * Removes the specified current alert from the database.
+   * 
+   * @param alert
+   *          the current alert to remove.
+   */
+  @Transactional
+  public void remove(AlertCurrentEntity alert) {
+    entityManagerProvider.get().remove(merge(alert));
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/26a07aac/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertCurrentEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertCurrentEntity.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertCurrentEntity.java
index 65c2d0f..6da5723 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertCurrentEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertCurrentEntity.java
@@ -21,10 +21,9 @@ import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.EnumType;
 import javax.persistence.Enumerated;
-import javax.persistence.GeneratedValue;
-import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
+import javax.persistence.NamedQueries;
 import javax.persistence.NamedQuery;
 import javax.persistence.OneToOne;
 import javax.persistence.Table;
@@ -41,12 +40,14 @@ import org.apache.ambari.server.state.MaintenanceState;
  */
 @Entity
 @Table(name = "alert_current")
-@NamedQuery(name = "AlertCurrentEntity.findAll", query = "SELECT currentAlert FROM AlertCurrentEntity
currentAlert")
+@NamedQueries({
+    @NamedQuery(name = "AlertCurrentEntity.findAll", query = "SELECT alert FROM AlertCurrentEntity
alert"),
+    @NamedQuery(name = "AlertCurrentEntity.findByService", query = "SELECT alert FROM AlertCurrentEntity
alert JOIN alert.alertHistory history WHERE history.clusterId = :clusterId AND history.serviceName
= :serviceName"),
+    @NamedQuery(name = "AlertCurrentEntity.findByHost", query = "SELECT alert FROM AlertCurrentEntity
alert JOIN alert.alertHistory history WHERE history.clusterId = :clusterId AND history.hostName
= :hostName") })
 public class AlertCurrentEntity {
 
   @Id
-  @GeneratedValue(strategy = GenerationType.TABLE)
-  @Column(name = "alert_id", unique = true, nullable = false, updatable = false)
+  @Column(name = "alert_id", nullable = false, updatable = false)
   private Long alertId;
 
   @Column(name = "latest_timestamp", nullable = false)

http://git-wip-us.apache.org/repos/asf/ambari/blob/26a07aac/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertDefinitionEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertDefinitionEntity.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertDefinitionEntity.java
index 997fdfd..da36c0c 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertDefinitionEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertDefinitionEntity.java
@@ -46,10 +46,10 @@ public class AlertDefinitionEntity {
 
   @Id
   @GeneratedValue(strategy = GenerationType.TABLE)
-  @Column(name = "definition_id", unique = true, nullable = false, updatable = false)
+  @Column(name = "definition_id", nullable = false, updatable = false)
   private Long definitionId;
 
-  @Column(name = "alert_source", nullable = false, length = 2147483647)
+  @Column(name = "alert_source", nullable = false, length = 32672)
   private String source;
 
   @Column(name = "cluster_id", nullable = false)

http://git-wip-us.apache.org/repos/asf/ambari/blob/26a07aac/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertGroupEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertGroupEntity.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertGroupEntity.java
index 12b1fff..a88d1ac 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertGroupEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertGroupEntity.java
@@ -49,7 +49,7 @@ public class AlertGroupEntity {
 
   @Id
   @GeneratedValue(strategy = GenerationType.TABLE)
-  @Column(name = "group_id", unique = true, nullable = false, updatable = false)
+  @Column(name = "group_id", nullable = false, updatable = false)
   private Long groupId;
 
   @Column(name = "cluster_id", nullable = false)

http://git-wip-us.apache.org/repos/asf/ambari/blob/26a07aac/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 2b01514..6533340 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
@@ -49,7 +49,7 @@ public class AlertHistoryEntity {
 
   @Id
   @GeneratedValue(strategy = GenerationType.TABLE)
-  @Column(name = "alert_id", unique = true, nullable = false, updatable = false)
+  @Column(name = "alert_id", nullable = false, updatable = false)
   private Long alertId;
 
   @Column(name = "alert_instance", length = 255)
@@ -62,7 +62,7 @@ public class AlertHistoryEntity {
   @Column(name = "alert_state", nullable = false, length = 255)
   private AlertState alertState;
 
-  @Column(name = "alert_text", length = 2147483647)
+  @Column(name = "alert_text", length = 32672)
   private String alertText;
 
   @Column(name = "alert_timestamp", nullable = false)

http://git-wip-us.apache.org/repos/asf/ambari/blob/26a07aac/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 8f84b49..ce64ae2 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
@@ -45,7 +45,7 @@ public class AlertNoticeEntity {
 
   @Id
   @GeneratedValue(strategy = GenerationType.TABLE)
-  @Column(name = "notification_id", unique = true, nullable = false, updatable = false)
+  @Column(name = "notification_id", nullable = false, updatable = false)
   private Long notificationId;
 
   @Enumerated(value = EnumType.STRING)

http://git-wip-us.apache.org/repos/asf/ambari/blob/26a07aac/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertTargetEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertTargetEntity.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertTargetEntity.java
index 32a2979..227931e 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertTargetEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertTargetEntity.java
@@ -44,7 +44,7 @@ public class AlertTargetEntity {
 
   @Id
   @GeneratedValue(strategy = GenerationType.TABLE)
-  @Column(name = "target_id", unique = true, nullable = false, updatable = false)
+  @Column(name = "target_id", nullable = false, updatable = false)
   private Long targetId;
 
   @Column(length = 1024)
@@ -53,7 +53,7 @@ public class AlertTargetEntity {
   @Column(name = "notification_type", nullable = false, length = 64)
   private String notificationType;
 
-  @Column(length = 2147483647)
+  @Column(length = 32672)
   private String properties;
 
   @Column(name = "target_name", unique = true, nullable = false, length = 255)
@@ -183,17 +183,20 @@ public class AlertTargetEntity {
    */
   @Override
   public boolean equals(Object object) {
-    if (this == object)
+    if (this == object) {
       return true;
+    }
 
-    if (object == null || getClass() != object.getClass())
+    if (object == null || getClass() != object.getClass()) {
       return false;
+    }
 
     AlertTargetEntity that = (AlertTargetEntity) object;
 
     if (targetId != null ? !targetId.equals(that.targetId)
-        : that.targetId != null)
+        : that.targetId != null) {
       return false;
+    }
 
     return true;
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/26a07aac/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceDesiredStateEntityPK.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceDesiredStateEntityPK.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceDesiredStateEntityPK.java
index 8fce36a..54dc8b6 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceDesiredStateEntityPK.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceDesiredStateEntityPK.java
@@ -39,7 +39,7 @@ public class ServiceDesiredStateEntityPK implements Serializable {
   private String serviceName;
 
   @Id
-  @Column(name = "service_name", nullable = false, insertable = true, updatable = true, length
= 2147483647, precision = 0)
+  @Column(name = "service_name", nullable = false, insertable = true, updatable = true, length
= 32672, precision = 0)
   public String getServiceName() {
     return serviceName;
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/26a07aac/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java
index 821cc3c..5e77a2d 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java
@@ -18,44 +18,74 @@
 
 package org.apache.ambari.server.orm;
 
-import com.google.inject.Inject;
-import com.google.inject.Injector;
-import com.google.inject.Provider;
-import com.google.inject.Singleton;
-import com.google.inject.persist.Transactional;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import javax.persistence.EntityManager;
+
 import org.apache.ambari.server.Role;
 import org.apache.ambari.server.RoleCommand;
 import org.apache.ambari.server.actionmanager.HostRoleStatus;
-import org.apache.ambari.server.orm.dao.*;
-import org.apache.ambari.server.orm.entities.*;
+import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
+import org.apache.ambari.server.orm.dao.ClusterDAO;
+import org.apache.ambari.server.orm.dao.HostDAO;
+import org.apache.ambari.server.orm.dao.HostRoleCommandDAO;
+import org.apache.ambari.server.orm.dao.RequestDAO;
+import org.apache.ambari.server.orm.dao.RoleDAO;
+import org.apache.ambari.server.orm.dao.StageDAO;
+import org.apache.ambari.server.orm.dao.UserDAO;
+import org.apache.ambari.server.orm.entities.ClusterEntity;
+import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
+import org.apache.ambari.server.orm.entities.HostEntity;
+import org.apache.ambari.server.orm.entities.HostRoleCommandEntity;
+import org.apache.ambari.server.orm.entities.HostStateEntity;
+import org.apache.ambari.server.orm.entities.PrincipalEntity;
+import org.apache.ambari.server.orm.entities.PrincipalTypeEntity;
+import org.apache.ambari.server.orm.entities.RequestEntity;
+import org.apache.ambari.server.orm.entities.RoleEntity;
+import org.apache.ambari.server.orm.entities.StageEntity;
+import org.apache.ambari.server.orm.entities.UserEntity;
 import org.apache.ambari.server.state.HostState;
 import org.springframework.security.crypto.password.PasswordEncoder;
+import org.springframework.util.Assert;
 
-import javax.persistence.EntityManager;
-import java.util.*;
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+import com.google.inject.Provider;
+import com.google.inject.Singleton;
+import com.google.inject.persist.Transactional;
 
 @Singleton
 public class OrmTestHelper {
 
   @Inject
   public Provider<EntityManager> entityManagerProvider;
+
   @Inject
   public Injector injector;
+
   @Inject
   public UserDAO userDAO;
+
   @Inject
   public RoleDAO roleDAO;
 
+  @Inject
+  public AlertDefinitionDAO alertDefinitionDAO;
+
   public EntityManager getEntityManager() {
     return entityManagerProvider.get();
   }
 
   /**
    * creates some test data
-    */
+   */
   @Transactional
   public void createDefaultData() {
-
     ClusterEntity clusterEntity = new ClusterEntity();
     clusterEntity.setClusterName("test_cluster1");
     clusterEntity.setClusterInfo("test_cluster_info1");
@@ -77,7 +107,7 @@ public class OrmTestHelper {
 
     clusterEntity.setHostEntities(hostEntities);
 
-    //both sides of relation should be set when modifying in runtime
+    // both sides of relation should be set when modifying in runtime
     host1.setClusterEntities(Arrays.asList(clusterEntity));
     host2.setClusterEntities(Arrays.asList(clusterEntity));
 
@@ -103,7 +133,6 @@ public class OrmTestHelper {
     getEntityManager().persist(hostStateEntity1);
     getEntityManager().persist(hostStateEntity2);
     getEntityManager().persist(clusterServiceEntity);
-
   }
 
   @Transactional
@@ -214,7 +243,26 @@ public class OrmTestHelper {
     hostRoleCommandDAO.create(commandEntity2);
     hostDAO.merge(host1);
     hostDAO.merge(host2);
-
   }
 
+  /**
+   * Creates an empty cluster with an ID.
+   * 
+   * @return the cluster ID.
+   */
+  @Transactional
+  public Long createCluster() {
+    ClusterDAO clusterDAO = injector.getInstance(ClusterDAO.class);
+
+    ClusterEntity clusterEntity = new ClusterEntity();
+    clusterEntity.setClusterName("test_cluster1");
+    clusterEntity.setClusterInfo("test_cluster_info1");
+
+    clusterDAO.create(clusterEntity);
+
+    clusterEntity = clusterDAO.findByName(clusterEntity.getClusterName());
+    Assert.notNull(clusterEntity);
+    Assert.isTrue(clusterEntity.getClusterId() > 0);
+    return clusterEntity.getClusterId();
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/26a07aac/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 ca2d24e..de2f5d3 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
@@ -28,19 +28,25 @@ import static org.junit.Assert.assertSame;
 
 import java.util.Collections;
 import java.util.List;
+import java.util.UUID;
 
 import javax.persistence.EntityManager;
 import javax.persistence.TypedQuery;
 
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
+import org.apache.ambari.server.orm.OrmTestHelper;
 import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
+import org.junit.AfterClass;
+import org.junit.Assert;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
 
 import com.google.inject.Guice;
 import com.google.inject.Injector;
 import com.google.inject.Provider;
+import com.google.inject.persist.PersistService;
 
 /**
  * Tests {@link AlertDefinitionDAO} for interacting with
@@ -49,11 +55,41 @@ import com.google.inject.Provider;
 @SuppressWarnings("unchecked")
 public class AlertDefinitionDAOTest {
 
-  AlertDefinitionDAO dao;
-  Provider<EntityManager> entityManagerProvider = createStrictMock(Provider.class);
+  AlertDefinitionDAO realDAO;
+  AlertDefinitionDAO mockDAO;
+  Provider<EntityManager> mockEntityManagerProvider = createStrictMock(Provider.class);
   EntityManager entityManager = createStrictMock(EntityManager.class);
 
-  private Injector injector;
+  private static Injector injector;
+  private static Long clusterId;
+
+  @BeforeClass
+  public static void beforeClass() {
+    injector = Guice.createInjector(new InMemoryDefaultTestModule());
+    injector.getInstance(GuiceJpaInitializer.class);
+    AlertDefinitionDAO alertDefinitionDAO = injector.getInstance(AlertDefinitionDAO.class);
+    clusterId = injector.getInstance(OrmTestHelper.class).createCluster();
+
+    for (int i = 0; i < 8; i++) {
+      AlertDefinitionEntity definition = new AlertDefinitionEntity();
+      definition.setDefinitionName("Alert Definition " + i);
+      definition.setServiceName("HDFS");
+      definition.setComponentName(null);
+      definition.setClusterId(clusterId);
+      definition.setHash(UUID.randomUUID().toString());
+      definition.setScheduleInterval(60L);
+      definition.setScope("SERVICE");
+      definition.setSource("Source " + i);
+      definition.setSourceType("SCRIPT");
+      alertDefinitionDAO.create(definition);
+    }
+  }
+
+  @AfterClass
+  public static void afterClass() {
+    injector.getInstance(PersistService.class).stop();
+    injector = null;
+  }
 
   /**
    * 
@@ -64,14 +100,16 @@ public class AlertDefinitionDAOTest {
     injector.getInstance(GuiceJpaInitializer.class);
     injector.injectMembers(this);
 
-    reset(entityManagerProvider);
-    expect(entityManagerProvider.get()).andReturn(entityManager).atLeastOnce();
-    replay(entityManagerProvider);
+    reset(mockEntityManagerProvider);
+    expect(mockEntityManagerProvider.get()).andReturn(entityManager).atLeastOnce();
+    replay(mockEntityManagerProvider);
 
-    dao = new AlertDefinitionDAO();
-    injector.injectMembers(dao);
+    realDAO = new AlertDefinitionDAO();
+    mockDAO = new AlertDefinitionDAO();
+    injector.injectMembers(realDAO);
+    injector.injectMembers(mockDAO);
 
-    dao.entityManagerProvider = entityManagerProvider;
+    mockDAO.entityManagerProvider = mockEntityManagerProvider;
   }
 
   /**
@@ -95,10 +133,19 @@ public class AlertDefinitionDAOTest {
 
     replay(query, entityManager);
 
-    AlertDefinitionEntity result = dao.findByName(12345L, "alert-definition-1");
+    AlertDefinitionEntity result = mockDAO.findByName(12345L,
+        "alert-definition-1");
 
     assertSame(result, entity);
-    verify(entityManagerProvider, entityManager);
+    verify(mockEntityManagerProvider, entityManager);
+
+    List<AlertDefinitionEntity> definitions = realDAO.findAll();
+    Assert.assertNotNull(definitions);
+    AlertDefinitionEntity definition = definitions.get(2);
+    AlertDefinitionEntity retrieved = realDAO.findByName(
+        definition.getClusterId(), definition.getDefinitionName());
+
+    Assert.assertEquals(definition, retrieved);
   }
 
   /**
@@ -117,11 +164,15 @@ public class AlertDefinitionDAOTest {
 
     replay(query, entityManager);
 
-    List<AlertDefinitionEntity> entities = dao.findAll();
+    List<AlertDefinitionEntity> entities = mockDAO.findAll();
 
     assertSame(1, entities.size());
     assertSame(entity, entities.get(0));
-    verify(entityManagerProvider, entityManager);
+    verify(mockEntityManagerProvider, entityManager);
+
+    List<AlertDefinitionEntity> definitions = realDAO.findAll();
+    Assert.assertNotNull(definitions);
+    Assert.assertEquals(8, definitions.size());
   }
 
   /**
@@ -136,10 +187,17 @@ public class AlertDefinitionDAOTest {
 
     replay(entityManager);
 
-    AlertDefinitionEntity result = dao.findById(12345L);
+    AlertDefinitionEntity result = mockDAO.findById(12345L);
 
     assertSame(result, entity);
-    verify(entityManagerProvider, entityManager);
+    verify(mockEntityManagerProvider, entityManager);
+
+    List<AlertDefinitionEntity> definitions = realDAO.findAll();
+    Assert.assertNotNull(definitions);
+    AlertDefinitionEntity definition = definitions.get(2);
+    AlertDefinitionEntity retrieved = realDAO.findById(definition.getDefinitionId());
+
+    Assert.assertEquals(definition, retrieved);
   }
 
   @Test
@@ -150,10 +208,10 @@ public class AlertDefinitionDAOTest {
     entityManager.refresh(eq(entity));
     replay(entityManager);
 
-    dao.entityManagerProvider = entityManagerProvider;
-    dao.refresh(entity);
+    mockDAO.entityManagerProvider = mockEntityManagerProvider;
+    mockDAO.refresh(entity);
 
-    verify(entityManagerProvider, entityManager);
+    verify(mockEntityManagerProvider, entityManager);
   }
 
   @Test
@@ -164,10 +222,10 @@ public class AlertDefinitionDAOTest {
     entityManager.persist(eq(entity));
     replay(entityManager);
 
-    dao.entityManagerProvider = entityManagerProvider;
-    dao.create(entity);
+    mockDAO.entityManagerProvider = mockEntityManagerProvider;
+    mockDAO.create(entity);
 
-    verify(entityManagerProvider, entityManager);
+    verify(mockEntityManagerProvider, entityManager);
   }
 
   @Test
@@ -179,10 +237,10 @@ public class AlertDefinitionDAOTest {
     expect(entityManager.merge(eq(entity))).andReturn(entity2);
     replay(entityManager);
 
-    dao.entityManagerProvider = entityManagerProvider;
-    assertSame(entity2, dao.merge(entity));
+    mockDAO.entityManagerProvider = mockEntityManagerProvider;
+    assertSame(entity2, mockDAO.merge(entity));
 
-    verify(entityManagerProvider, entityManager);
+    verify(mockEntityManagerProvider, entityManager);
   }
 
   @Test
@@ -195,9 +253,9 @@ public class AlertDefinitionDAOTest {
     entityManager.remove(eq(entity2));
     replay(entityManager);
 
-    dao.entityManagerProvider = entityManagerProvider;
-    dao.remove(entity);
+    mockDAO.entityManagerProvider = mockEntityManagerProvider;
+    mockDAO.remove(entity);
 
-    verify(entityManagerProvider, entityManager);
+    verify(mockEntityManagerProvider, entityManager);
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/26a07aac/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
new file mode 100644
index 0000000..3b6aaa0
--- /dev/null
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDispatchDAOTest.java
@@ -0,0 +1,144 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF 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.apache.ambari.server.orm.dao;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.ambari.server.orm.GuiceJpaInitializer;
+import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
+import org.apache.ambari.server.orm.OrmTestHelper;
+import org.apache.ambari.server.orm.entities.AlertGroupEntity;
+import org.apache.ambari.server.orm.entities.AlertTargetEntity;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.persist.PersistService;
+
+/**
+ * Tests {@link AlertDispatchDAO}.
+ */
+public class AlertDispatchDAOTest {
+
+  static Long clusterId;
+  static Injector injector;
+  AlertDispatchDAO dao;
+
+  @BeforeClass
+  public static void beforeClass() {
+    injector = Guice.createInjector(new InMemoryDefaultTestModule());
+    injector.getInstance(GuiceJpaInitializer.class);
+    clusterId = injector.getInstance(OrmTestHelper.class).createCluster();
+    AlertDispatchDAO alertDispatchDAO = injector.getInstance(AlertDispatchDAO.class);
+
+    Set<AlertTargetEntity> targets = new HashSet<AlertTargetEntity>();
+    for (int i = 0; i < 5; i++) {
+      AlertTargetEntity target = new AlertTargetEntity();
+      target.setDescription("Target Description " + i);
+      target.setNotificationType("EMAIL");
+      target.setProperties("Target Properties " + i);
+      target.setTargetName("Target Name " + i);
+      alertDispatchDAO.create(target);
+      targets.add(target);
+    }
+
+    for (int i = 0; i < 10; i++) {
+      AlertGroupEntity group = new AlertGroupEntity();
+      group.setDefault(false);
+      group.setGroupName("Group Name " + i);
+      group.setClusterId(clusterId);
+      group.setAlertTargets(targets);
+      alertDispatchDAO.create(group);
+    }
+
+  }
+
+  /**
+   * 
+   */
+  @AfterClass
+  public static void afterClass() {
+    injector.getInstance(PersistService.class).stop();
+    injector = null;
+  }
+
+  /**
+   * 
+   */
+  @Before
+  public void setup() {
+    dao = new AlertDispatchDAO();
+    injector.injectMembers(dao);
+  }
+
+  /**
+   * 
+   */
+  @Test
+  public void testFindAllTargets() {
+    List<AlertTargetEntity> targets = dao.findAllTargets();
+    Assert.assertNotNull(targets);
+    Assert.assertEquals(5, targets.size());
+  }
+
+  /**
+   * 
+   */
+  @Test
+  public void testFindTargetByName() {
+    List<AlertTargetEntity> targets = dao.findAllTargets();
+    Assert.assertNotNull(targets);
+    AlertTargetEntity target = targets.get(3);
+
+    AlertTargetEntity actual = dao.findTargetByName(target.getTargetName());
+    Assert.assertEquals(target, actual);
+  }
+
+  /**
+   * 
+   */
+  @Test
+  public void testFindAllGroups() {
+    List<AlertGroupEntity> groups = dao.findAllGroups();
+    Assert.assertNotNull(groups);
+    Assert.assertEquals(10, groups.size());
+  }
+
+  /**
+   * 
+   */
+  @Test
+  public void testFindGroupByName() {
+    List<AlertGroupEntity> groups = dao.findAllGroups();
+    Assert.assertNotNull(groups);
+    AlertGroupEntity group = groups.get(3);
+
+    AlertGroupEntity actual = dao.findGroupByName(group.getClusterId(),
+        group.getGroupName());
+
+    Assert.assertEquals(group, actual);
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/26a07aac/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
new file mode 100644
index 0000000..f2adcaa
--- /dev/null
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertsDAOTest.java
@@ -0,0 +1,176 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF 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.apache.ambari.server.orm.dao;
+
+import java.util.Date;
+import java.util.List;
+import java.util.UUID;
+
+import org.apache.ambari.server.orm.GuiceJpaInitializer;
+import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
+import org.apache.ambari.server.orm.OrmTestHelper;
+import org.apache.ambari.server.orm.entities.AlertCurrentEntity;
+import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
+import org.apache.ambari.server.orm.entities.AlertHistoryEntity;
+import org.apache.ambari.server.state.AlertState;
+import org.apache.ambari.server.state.MaintenanceState;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.persist.PersistService;
+
+/**
+ * Tests {@link AlertsDAO}.
+ */
+public class AlertsDAOTest {
+
+  static Long clusterId;
+  static Injector injector;
+  AlertsDAO dao;
+
+  @BeforeClass
+  public static void beforeClass() {
+    injector = Guice.createInjector(new InMemoryDefaultTestModule());
+    injector.getInstance(GuiceJpaInitializer.class);
+    clusterId = injector.getInstance(OrmTestHelper.class).createCluster();
+    AlertsDAO alertDAO = injector.getInstance(AlertsDAO.class);
+    AlertDefinitionDAO alertDefinitionDAO = injector.getInstance(AlertDefinitionDAO.class);
+
+    // create 5 definitions
+    for (int i = 0; i < 5; i++) {
+      AlertDefinitionEntity definition = new AlertDefinitionEntity();
+      definition.setDefinitionName("Alert Definition " + i);
+      definition.setServiceName("Service " + i);
+      definition.setComponentName(null);
+      definition.setClusterId(clusterId);
+      definition.setHash(UUID.randomUUID().toString());
+      definition.setScheduleInterval(60L);
+      definition.setScope("SERVICE");
+      definition.setSource("Source " + i);
+      definition.setSourceType("SCRIPT");
+      alertDefinitionDAO.create(definition);
+    }
+
+    List<AlertDefinitionEntity> definitions = alertDefinitionDAO.findAll();
+    Assert.assertNotNull(definitions);
+    Assert.assertEquals(5, definitions.size());
+
+    // create 5 historical alerts for each definition
+    for (AlertDefinitionEntity definition : definitions) {
+      for (int i = 0; i < 5; i++) {
+        AlertHistoryEntity history = new AlertHistoryEntity();
+        history.setServiceName(definition.getServiceName());
+        history.setClusterId(clusterId);
+        history.setAlertDefinition(definition);
+        history.setAlertLabel(definition.getDefinitionName() + " " + i);
+        history.setAlertState(AlertState.OK);
+        history.setAlertText(definition.getDefinitionName() + " " + i);
+        history.setAlertTimestamp(new Date().getTime());
+        alertDAO.create(history);
+      }
+    }
+
+    // for each definition, create a current alert
+    for (AlertDefinitionEntity definition : definitions) {
+      List<AlertHistoryEntity> alerts = alertDAO.findAll();
+      AlertHistoryEntity history = null;
+      for (AlertHistoryEntity alert : alerts) {
+        if (definition.equals(alert.getAlertDefinition())) {
+          history = alert;
+        }
+      }
+
+      Assert.assertNotNull(history);
+
+      AlertCurrentEntity current = new AlertCurrentEntity();
+      current.setAlertId(history.getAlertId());
+      current.setAlertHistory(history);
+      current.setLatestTimestamp(new Date().getTime());
+      current.setOriginalTimestamp(new Date().getTime() - 10800000);
+      current.setMaintenanceState(MaintenanceState.OFF);
+      alertDAO.create(current);
+    }
+  }
+
+  /**
+   * 
+   */
+  @AfterClass
+  public static void afterClass() {
+    injector.getInstance(PersistService.class).stop();
+    injector = null;
+  }
+
+  /**
+   * 
+   */
+  @Before
+  public void setup() {
+    dao = new AlertsDAO();
+    injector.injectMembers(dao);
+  }
+
+  /**
+   * 
+   */
+  @Test
+  public void testFindAll() {
+    List<AlertHistoryEntity> alerts = dao.findAll(clusterId);
+    Assert.assertNotNull(alerts);
+    Assert.assertEquals(25, alerts.size());
+  }
+
+  /**
+   * 
+   */
+  @Test
+  public void testFindAllCurrent() {
+    List<AlertCurrentEntity> currentAlerts = dao.findCurrent();
+    Assert.assertNotNull(currentAlerts);
+    Assert.assertEquals(5, currentAlerts.size());
+  }
+
+  /**
+   * 
+   */
+  @Test
+  public void testFindCurrentByService() {
+    List<AlertCurrentEntity> currentAlerts = dao.findCurrent();
+    AlertCurrentEntity current = currentAlerts.get(0);
+    AlertHistoryEntity history = current.getAlertHistory();
+    
+    Assert.assertNotNull(history);    
+    
+    currentAlerts = dao.findCurrentByService(clusterId,
+        history.getServiceName());
+
+    Assert.assertNotNull(currentAlerts);
+    Assert.assertEquals(1, currentAlerts.size());
+
+    currentAlerts = dao.findCurrentByService(clusterId, "foo");
+
+    Assert.assertNotNull(currentAlerts);
+    Assert.assertEquals(0, currentAlerts.size());
+  }
+}


Mime
View raw message