ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jonathanhur...@apache.org
Subject [1/2] AMBARI-7778 - Alerts: Expose Alert Notices via REST APIs (jonathanhurley)
Date Wed, 15 Oct 2014 19:50:55 GMT
Repository: ambari
Updated Branches:
  refs/heads/trunk ec5ec6c7c -> 3befc557a


http://git-wip-us.apache.org/repos/asf/ambari/blob/3befc557/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 015acc0..430bede 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
@@ -24,12 +24,21 @@ import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
 
+import junit.framework.Assert;
+
+import org.apache.ambari.server.controller.AlertNoticeRequest;
+import org.apache.ambari.server.controller.internal.AlertNoticeResourceProvider;
+import org.apache.ambari.server.controller.spi.Predicate;
+import org.apache.ambari.server.controller.utilities.PredicateBuilder;
+import org.apache.ambari.server.orm.AlertDaoHelper;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.OrmTestHelper;
@@ -39,7 +48,19 @@ import org.apache.ambari.server.orm.entities.AlertHistoryEntity;
 import org.apache.ambari.server.orm.entities.AlertNoticeEntity;
 import org.apache.ambari.server.orm.entities.AlertTargetEntity;
 import org.apache.ambari.server.state.AlertState;
+import org.apache.ambari.server.state.Cluster;
+import org.apache.ambari.server.state.Clusters;
+import org.apache.ambari.server.state.Host;
+import org.apache.ambari.server.state.HostState;
 import org.apache.ambari.server.state.NotificationState;
+import org.apache.ambari.server.state.Service;
+import org.apache.ambari.server.state.ServiceComponent;
+import org.apache.ambari.server.state.ServiceComponentFactory;
+import org.apache.ambari.server.state.ServiceComponentHost;
+import org.apache.ambari.server.state.ServiceComponentHostFactory;
+import org.apache.ambari.server.state.ServiceFactory;
+import org.apache.ambari.server.state.StackId;
+import org.apache.ambari.server.state.State;
 import org.apache.ambari.server.state.alert.Scope;
 import org.apache.ambari.server.state.alert.SourceType;
 import org.junit.After;
@@ -55,38 +76,51 @@ import com.google.inject.persist.PersistService;
  */
 public class AlertDispatchDAOTest {
 
-  Long clusterId;
-  Injector injector;
-  AlertDispatchDAO dao;
-  AlertDefinitionDAO definitionDao;
-  AlertsDAO alertsDao;
-  OrmTestHelper helper;
+  private final static String HOSTNAME = "c6401.ambari.apache.org";
+
+  private Clusters m_clusters;
+  private Long m_clusterId;
+  private Injector m_injector;
+  private AlertDispatchDAO m_dao;
+  private AlertDefinitionDAO m_definitionDao;
+  private AlertsDAO m_alertsDao;
+  private OrmTestHelper m_helper;
+
+  private ServiceFactory m_serviceFactory;
+  private ServiceComponentFactory m_componentFactory;
+  private ServiceComponentHostFactory m_schFactory;
+  private AlertDaoHelper m_alertHelper;
 
   /**
    *
    */
   @Before
   public void setup() throws Exception {
-    injector = Guice.createInjector(new InMemoryDefaultTestModule());
-    injector.getInstance(GuiceJpaInitializer.class);
-    dao = injector.getInstance(AlertDispatchDAO.class);
-    alertsDao = injector.getInstance(AlertsDAO.class);
-    definitionDao = injector.getInstance(AlertDefinitionDAO.class);
-    helper = injector.getInstance(OrmTestHelper.class);
-
-    clusterId = helper.createCluster();
+    m_injector = Guice.createInjector(new InMemoryDefaultTestModule());
+    m_injector.getInstance(GuiceJpaInitializer.class);
+    m_dao = m_injector.getInstance(AlertDispatchDAO.class);
+    m_alertsDao = m_injector.getInstance(AlertsDAO.class);
+    m_definitionDao = m_injector.getInstance(AlertDefinitionDAO.class);
+    m_helper = m_injector.getInstance(OrmTestHelper.class);
+    m_serviceFactory = m_injector.getInstance(ServiceFactory.class);
+    m_componentFactory = m_injector.getInstance(ServiceComponentFactory.class);
+    m_schFactory = m_injector.getInstance(ServiceComponentHostFactory.class);
+    m_clusters = m_injector.getInstance(Clusters.class);
+    m_alertHelper = m_injector.getInstance(AlertDaoHelper.class);
+
+    m_clusterId = m_helper.createCluster();
     Set<AlertTargetEntity> targets = createTargets();
 
     for (int i = 0; i < 10; i++) {
       AlertGroupEntity group = new AlertGroupEntity();
       group.setDefault(false);
       group.setGroupName("Group Name " + i);
-      group.setClusterId(clusterId);
+      group.setClusterId(m_clusterId);
       for (AlertTargetEntity alertTarget : targets) {
         group.addAlertTarget(alertTarget);
       }
 
-      dao.create(group);
+      m_dao.create(group);
     }
   }
 
@@ -95,8 +129,8 @@ public class AlertDispatchDAOTest {
    */
   @After
   public void teardown() throws Exception {
-    injector.getInstance(PersistService.class).stop();
-    injector = null;
+    m_injector.getInstance(PersistService.class).stop();
+    m_injector = null;
   }
 
   /**
@@ -104,7 +138,7 @@ public class AlertDispatchDAOTest {
    */
   @Test
   public void testFindAllTargets() throws Exception {
-    List<AlertTargetEntity> targets = dao.findAllTargets();
+    List<AlertTargetEntity> targets = m_dao.findAllTargets();
     assertNotNull(targets);
     assertEquals(5, targets.size());
   }
@@ -113,7 +147,7 @@ public class AlertDispatchDAOTest {
    * @throws Exception
    */
   public void testFindTargetsByIds() throws Exception {
-    List<AlertTargetEntity> targets = dao.findAllTargets();
+    List<AlertTargetEntity> targets = m_dao.findAllTargets();
     assertNotNull(targets);
     assertEquals(5, targets.size());
 
@@ -122,7 +156,7 @@ public class AlertDispatchDAOTest {
     ids.add(targets.get(1).getTargetId());
     ids.add(99999L);
 
-    targets = dao.findTargetsById(ids);
+    targets = m_dao.findTargetsById(ids);
     assertEquals(2, targets.size());
   }
 
@@ -131,11 +165,11 @@ public class AlertDispatchDAOTest {
    */
   @Test
   public void testFindTargetByName() throws Exception {
-    List<AlertTargetEntity> targets = dao.findAllTargets();
+    List<AlertTargetEntity> targets = m_dao.findAllTargets();
     assertNotNull(targets);
     AlertTargetEntity target = targets.get(3);
 
-    AlertTargetEntity actual = dao.findTargetByName(target.getTargetName());
+    AlertTargetEntity actual = m_dao.findTargetByName(target.getTargetName());
     assertEquals(target, actual);
   }
 
@@ -144,7 +178,7 @@ public class AlertDispatchDAOTest {
    */
   @Test
   public void testFindAllGroups() throws Exception {
-    List<AlertGroupEntity> groups = dao.findAllGroups();
+    List<AlertGroupEntity> groups = m_dao.findAllGroups();
     assertNotNull(groups);
     assertEquals(10, groups.size());
   }
@@ -154,11 +188,11 @@ public class AlertDispatchDAOTest {
    */
   @Test
   public void testFindGroupByName() throws Exception {
-    List<AlertGroupEntity> groups = dao.findAllGroups();
+    List<AlertGroupEntity> groups = m_dao.findAllGroups();
     assertNotNull(groups);
     AlertGroupEntity group = groups.get(3);
 
-    AlertGroupEntity actual = dao.findGroupByName(group.getClusterId(),
+    AlertGroupEntity actual = m_dao.findGroupByName(group.getClusterId(),
         group.getGroupName());
 
     assertEquals(group, actual);
@@ -169,12 +203,12 @@ public class AlertDispatchDAOTest {
    */
   @Test
   public void testCreateGroup() throws Exception {
-    AlertTargetEntity target = helper.createAlertTarget();
+    AlertTargetEntity target = m_helper.createAlertTarget();
     Set<AlertTargetEntity> targets = new HashSet<AlertTargetEntity>();
     targets.add(target);
 
-    AlertGroupEntity group = helper.createAlertGroup(clusterId, targets);
-    AlertGroupEntity actual = dao.findGroupById(group.getGroupId());
+    AlertGroupEntity group = m_helper.createAlertGroup(m_clusterId, targets);
+    AlertGroupEntity actual = m_dao.findGroupById(group.getGroupId());
     assertNotNull(group);
 
     assertEquals(group.getGroupName(), actual.getGroupName());
@@ -190,29 +224,29 @@ public class AlertDispatchDAOTest {
   public void testGroupDefinitions() throws Exception {
     List<AlertDefinitionEntity> definitions = createDefinitions();
 
-    AlertGroupEntity group = helper.createAlertGroup(clusterId, null);
+    AlertGroupEntity group = m_helper.createAlertGroup(m_clusterId, null);
 
-    group = dao.findGroupById(group.getGroupId());
+    group = m_dao.findGroupById(group.getGroupId());
     assertNotNull(group);
 
     for (AlertDefinitionEntity definition : definitions) {
       group.addAlertDefinition(definition);
     }
 
-    dao.merge(group);
+    m_dao.merge(group);
 
-    group = dao.findGroupByName(group.getGroupName());
+    group = m_dao.findGroupByName(group.getGroupName());
     assertEquals(definitions.size(), group.getAlertDefinitions().size());
 
     for (AlertDefinitionEntity definition : definitions) {
       assertTrue(group.getAlertDefinitions().contains(definition));
     }
 
-    definitionDao.refresh(definitions.get(0));
-    definitionDao.remove(definitions.get(0));
+    m_definitionDao.refresh(definitions.get(0));
+    m_definitionDao.remove(definitions.get(0));
     definitions.remove(0);
 
-    group = dao.findGroupByName(group.getGroupName());
+    group = m_dao.findGroupByName(group.getGroupName());
     assertEquals(definitions.size(), group.getAlertDefinitions().size());
 
     for (AlertDefinitionEntity definition : definitions) {
@@ -225,14 +259,14 @@ public class AlertDispatchDAOTest {
    */
   @Test
   public void testCreateTarget() throws Exception {
-    int targetCount = dao.findAllTargets().size();
+    int targetCount = m_dao.findAllTargets().size();
 
-    AlertTargetEntity target = helper.createAlertTarget();
+    AlertTargetEntity target = m_helper.createAlertTarget();
     Set<AlertTargetEntity> targets = new HashSet<AlertTargetEntity>();
     targets.add(target);
 
-    AlertGroupEntity group = helper.createAlertGroup(clusterId, targets);
-    AlertTargetEntity actual = dao.findTargetById(target.getTargetId());
+    AlertGroupEntity group = m_helper.createAlertGroup(m_clusterId, targets);
+    AlertTargetEntity actual = m_dao.findTargetById(target.getTargetId());
     assertNotNull(actual);
 
     assertEquals(target.getTargetName(), actual.getTargetName());
@@ -246,7 +280,7 @@ public class AlertDispatchDAOTest {
 
     assertEquals(group, actualGroup);
 
-    assertEquals(targetCount + 1, dao.findAllTargets().size());
+    assertEquals(targetCount + 1, m_dao.findAllTargets().size());
   }
 
   /**
@@ -254,32 +288,32 @@ public class AlertDispatchDAOTest {
    */
   @Test
   public void testDeleteGroup() throws Exception {
-    int targetCount = dao.findAllTargets().size();
+    int targetCount = m_dao.findAllTargets().size();
 
-    AlertGroupEntity group = helper.createAlertGroup(clusterId, null);
-    AlertTargetEntity target = helper.createAlertTarget();
-    assertEquals(targetCount + 1, dao.findAllTargets().size());
+    AlertGroupEntity group = m_helper.createAlertGroup(m_clusterId, null);
+    AlertTargetEntity target = m_helper.createAlertTarget();
+    assertEquals(targetCount + 1, m_dao.findAllTargets().size());
 
-    group = dao.findGroupById(group.getGroupId());
+    group = m_dao.findGroupById(group.getGroupId());
     assertNotNull(group);
     assertNotNull(group.getAlertTargets());
     assertEquals(0, group.getAlertTargets().size());
 
     group.addAlertTarget(target);
-    dao.merge(group);
+    m_dao.merge(group);
 
-    group = dao.findGroupById(group.getGroupId());
+    group = m_dao.findGroupById(group.getGroupId());
     assertNotNull(group);
     assertNotNull(group.getAlertTargets());
     assertEquals(1, group.getAlertTargets().size());
 
-    dao.remove(group);
-    group = dao.findGroupById(group.getGroupId());
+    m_dao.remove(group);
+    group = m_dao.findGroupById(group.getGroupId());
     assertNull(group);
 
-    target = dao.findTargetById(target.getTargetId());
+    target = m_dao.findTargetById(target.getTargetId());
     assertNotNull(target);
-    assertEquals(targetCount + 1, dao.findAllTargets().size());
+    assertEquals(targetCount + 1, m_dao.findAllTargets().size());
   }
 
   /**
@@ -287,13 +321,13 @@ public class AlertDispatchDAOTest {
    */
   @Test
   public void testDeleteTarget() throws Exception {
-    AlertTargetEntity target = helper.createAlertTarget();
-    target = dao.findTargetById(target.getTargetId());
+    AlertTargetEntity target = m_helper.createAlertTarget();
+    target = m_dao.findTargetById(target.getTargetId());
     assertNotNull(target);
 
-    dao.remove(target);
+    m_dao.remove(target);
 
-    target = dao.findTargetById(target.getTargetId());
+    target = m_dao.findTargetById(target.getTargetId());
     assertNull(target);
   }
 
@@ -302,24 +336,24 @@ public class AlertDispatchDAOTest {
    */
   @Test
   public void testDeleteAssociatedTarget() throws Exception {
-    AlertTargetEntity target = helper.createAlertTarget();
+    AlertTargetEntity target = m_helper.createAlertTarget();
     Set<AlertTargetEntity> targets = new HashSet<AlertTargetEntity>();
     targets.add(target);
 
-    AlertGroupEntity group = helper.createAlertGroup(clusterId, targets);
+    AlertGroupEntity group = m_helper.createAlertGroup(m_clusterId, targets);
     assertEquals(1, group.getAlertTargets().size());
 
-    target = dao.findTargetById(target.getTargetId());
-    dao.refresh(target);
+    target = m_dao.findTargetById(target.getTargetId());
+    m_dao.refresh(target);
 
     assertNotNull(target);
     assertEquals(1, target.getAlertGroups().size());
 
-    dao.remove(target);
-    target = dao.findTargetById(target.getTargetId());
+    m_dao.remove(target);
+    target = m_dao.findTargetById(target.getTargetId());
     assertNull(target);
 
-    group = dao.findGroupById(group.getGroupId());
+    group = m_dao.findGroupById(group.getGroupId());
     assertNotNull(group);
 
     assertEquals(0, group.getAlertTargets().size());
@@ -330,20 +364,20 @@ public class AlertDispatchDAOTest {
    */
   @Test
   public void testUpdateGroup() throws Exception {
-    AlertTargetEntity target = helper.createAlertTarget();
+    AlertTargetEntity target = m_helper.createAlertTarget();
     Set<AlertTargetEntity> targets = new HashSet<AlertTargetEntity>();
     targets.add(target);
 
     String groupName = "Group Name " + System.currentTimeMillis();
 
-    AlertGroupEntity group = helper.createAlertGroup(clusterId, null);
+    AlertGroupEntity group = m_helper.createAlertGroup(m_clusterId, null);
 
-    group = dao.findGroupById(group.getGroupId());
+    group = m_dao.findGroupById(group.getGroupId());
     group.setGroupName(groupName + "FOO");
     group.setDefault(true);
 
-    dao.merge(group);
-    group = dao.findGroupById(group.getGroupId());
+    m_dao.merge(group);
+    group = m_dao.findGroupById(group.getGroupId());
 
     assertEquals(groupName + "FOO", group.getGroupName());
     assertEquals(true, group.isDefault());
@@ -351,9 +385,9 @@ public class AlertDispatchDAOTest {
     assertEquals(0, group.getAlertTargets().size());
 
     group.addAlertTarget(target);
-    dao.merge(group);
+    m_dao.merge(group);
 
-    group = dao.findGroupById(group.getGroupId());
+    group = m_dao.findGroupById(group.getGroupId());
     assertEquals(targets, group.getAlertTargets());
   }
 
@@ -365,22 +399,22 @@ public class AlertDispatchDAOTest {
   @Test
   public void testFindGroupsByDefinition() throws Exception {
     List<AlertDefinitionEntity> definitions = createDefinitions();
-    AlertGroupEntity group = helper.createAlertGroup(clusterId, null);
+    AlertGroupEntity group = m_helper.createAlertGroup(m_clusterId, null);
 
-    group = dao.findGroupById(group.getGroupId());
+    group = m_dao.findGroupById(group.getGroupId());
     assertNotNull(group);
 
     for (AlertDefinitionEntity definition : definitions) {
       group.addAlertDefinition(definition);
     }
 
-    dao.merge(group);
+    m_dao.merge(group);
 
-    group = dao.findGroupByName(group.getGroupName());
+    group = m_dao.findGroupByName(group.getGroupName());
     assertEquals(definitions.size(), group.getAlertDefinitions().size());
 
     for (AlertDefinitionEntity definition : definitions) {
-      List<AlertGroupEntity> groups = dao.findGroupsByDefinition(definition);
+      List<AlertGroupEntity> groups = m_dao.findGroupsByDefinition(definition);
       assertEquals(1, groups.size());
       assertEquals(group.getGroupId(), groups.get(0).getGroupId());
     }
@@ -396,26 +430,104 @@ public class AlertDispatchDAOTest {
 
     AlertHistoryEntity history = new AlertHistoryEntity();
     history.setServiceName(definition.getServiceName());
-    history.setClusterId(clusterId);
+    history.setClusterId(m_clusterId);
     history.setAlertDefinition(definition);
     history.setAlertLabel("Label");
     history.setAlertState(AlertState.OK);
     history.setAlertText("Alert Text");
     history.setAlertTimestamp(System.currentTimeMillis());
-    alertsDao.create(history);
+    m_alertsDao.create(history);
 
-    AlertTargetEntity target = helper.createAlertTarget();
+    AlertTargetEntity target = m_helper.createAlertTarget();
 
     AlertNoticeEntity notice = new AlertNoticeEntity();
     notice.setUuid(UUID.randomUUID().toString());
     notice.setAlertTarget(target);
     notice.setAlertHistory(history);
     notice.setNotifyState(NotificationState.PENDING);
-    dao.create(notice);
+    m_dao.create(notice);
 
-    AlertNoticeEntity actual = dao.findNoticeByUuid(notice.getUuid());
+    AlertNoticeEntity actual = m_dao.findNoticeByUuid(notice.getUuid());
     assertEquals(notice.getNotificationId(), actual.getNotificationId());
-    assertNull(dao.findNoticeByUuid("DEADBEEF"));
+    assertNull(m_dao.findNoticeByUuid("DEADBEEF"));
+  }
+
+
+  /**
+   * Tests that the Ambari {@link Predicate} can be converted and submitted to
+   * JPA correctly to return a restricted result set.
+   *
+   * @throws Exception
+   */
+  @Test
+  public void testAlertNoticePredicate() throws Exception {
+    Cluster cluster = initializeNewCluster();
+    m_alertHelper.populateData(cluster);
+
+    Predicate clusterPredicate = null;
+    Predicate hdfsPredicate = null;
+    Predicate yarnPredicate = null;
+    Predicate adminPredicate = null;
+    Predicate adminOrOperatorPredicate = null;
+    Predicate pendingPredicate = null;
+    Predicate noticeIdPredicate = null;
+
+    clusterPredicate = new PredicateBuilder().property(
+        AlertNoticeResourceProvider.ALERT_NOTICE_CLUSTER_NAME).equals("c1").toPredicate();
+
+    hdfsPredicate = new PredicateBuilder().property(
+        AlertNoticeResourceProvider.ALERT_NOTICE_SERVICE_NAME).equals("HDFS").toPredicate();
+
+    yarnPredicate = new PredicateBuilder().property(
+        AlertNoticeResourceProvider.ALERT_NOTICE_SERVICE_NAME).equals("YARN").toPredicate();
+
+    adminPredicate = new PredicateBuilder().property(
+        AlertNoticeResourceProvider.ALERT_NOTICE_TARGET_NAME).equals(
+        "Administrators").toPredicate();
+
+    adminOrOperatorPredicate = new PredicateBuilder().property(
+        AlertNoticeResourceProvider.ALERT_NOTICE_TARGET_NAME).equals(
+        "Administrators").or().property(
+        AlertNoticeResourceProvider.ALERT_NOTICE_TARGET_NAME).equals(
+        "Operators").toPredicate();
+
+    pendingPredicate = new PredicateBuilder().property(
+        AlertNoticeResourceProvider.ALERT_NOTICE_STATE).equals(
+        NotificationState.PENDING.name()).toPredicate();
+
+    AlertNoticeRequest request = new AlertNoticeRequest();
+    request.Predicate = clusterPredicate;
+
+    List<AlertNoticeEntity> notices = m_dao.findAllNotices(request);
+    assertEquals(2, notices.size());
+
+    request.Predicate = hdfsPredicate;
+    notices = m_dao.findAllNotices(request);
+    assertEquals(1, notices.size());
+
+    request.Predicate = yarnPredicate;
+    notices = m_dao.findAllNotices(request);
+    assertEquals(1, notices.size());
+
+    request.Predicate = adminPredicate;
+    notices = m_dao.findAllNotices(request);
+    assertEquals(1, notices.size());
+
+    request.Predicate = adminOrOperatorPredicate;
+    notices = m_dao.findAllNotices(request);
+    assertEquals(2, notices.size());
+
+    request.Predicate = pendingPredicate;
+    notices = m_dao.findAllNotices(request);
+    assertEquals(1, notices.size());
+
+    noticeIdPredicate = new PredicateBuilder().property(
+        AlertNoticeResourceProvider.ALERT_NOTICE_ID).equals(
+        notices.get(0).getNotificationId()).toPredicate();
+
+    request.Predicate = noticeIdPredicate;
+    notices = m_dao.findAllNotices(request);
+    assertEquals(1, notices.size());
   }
 
   /**
@@ -427,16 +539,16 @@ public class AlertDispatchDAOTest {
       definition.setDefinitionName("Alert Definition " + i);
       definition.setServiceName("HDFS");
       definition.setComponentName(null);
-      definition.setClusterId(clusterId);
+      definition.setClusterId(m_clusterId);
       definition.setHash(UUID.randomUUID().toString());
       definition.setScheduleInterval(60);
       definition.setScope(Scope.SERVICE);
       definition.setSource("{\"type\" : \"SCRIPT\"}");
       definition.setSourceType(SourceType.SCRIPT);
-      definitionDao.create(definition);
+      m_definitionDao.create(definition);
     }
 
-    List<AlertDefinitionEntity> alertDefinitions = definitionDao.findAll();
+    List<AlertDefinitionEntity> alertDefinitions = m_definitionDao.findAll();
     assertEquals(8, alertDefinitions.size());
     return alertDefinitions;
   }
@@ -453,10 +565,85 @@ public class AlertDispatchDAOTest {
       target.setNotificationType("EMAIL");
       target.setProperties("Target Properties " + i);
       target.setTargetName("Target Name " + i);
-      dao.create(target);
+      m_dao.create(target);
       targets.add(target);
     }
 
     return targets;
   }
+
+  private Cluster initializeNewCluster() throws Exception {
+    String clusterName = "cluster-" + System.currentTimeMillis();
+    m_clusters.addCluster(clusterName);
+
+    Cluster cluster = m_clusters.getCluster(clusterName);
+    cluster.setDesiredStackVersion(new StackId("HDP", "2.0.6"));
+
+    addHost();
+    m_clusters.mapHostToCluster(HOSTNAME, cluster.getClusterName());
+
+    installHdfsService(cluster);
+    return cluster;
+  }
+
+  /**
+   * @throws Exception
+   */
+  private void addHost() throws Exception {
+    m_clusters.addHost(HOSTNAME);
+
+    Host host = m_clusters.getHost(HOSTNAME);
+    Map<String, String> hostAttributes = new HashMap<String, String>();
+    hostAttributes.put("os_family", "redhat");
+    hostAttributes.put("os_release_version", "6.4");
+    host.setHostAttributes(hostAttributes);
+    host.setState(HostState.HEALTHY);
+    host.persist();
+  }
+
+  /**
+   * Calls {@link Service#persist()} to mock a service install along with
+   * creating a single {@link Host} and {@link ServiceComponentHost}.
+   */
+  private void installHdfsService(Cluster cluster) throws Exception {
+    String serviceName = "HDFS";
+    Service service = m_serviceFactory.createNew(cluster, serviceName);
+    cluster.addService(service);
+    service.persist();
+    service = cluster.getService(serviceName);
+    Assert.assertNotNull(service);
+
+    ServiceComponent datanode = m_componentFactory.createNew(service,
+        "DATANODE");
+
+    service.addServiceComponent(datanode);
+    datanode.setDesiredState(State.INSTALLED);
+    datanode.persist();
+
+    ServiceComponentHost sch = m_schFactory.createNew(datanode, HOSTNAME);
+
+    datanode.addServiceComponentHost(sch);
+    sch.setDesiredState(State.INSTALLED);
+    sch.setState(State.INSTALLED);
+    sch.setDesiredStackVersion(new StackId("HDP-2.0.6"));
+    sch.setStackVersion(new StackId("HDP-2.0.6"));
+
+    sch.persist();
+
+    ServiceComponent namenode = m_componentFactory.createNew(service,
+        "NAMENODE");
+
+    service.addServiceComponent(namenode);
+    namenode.setDesiredState(State.INSTALLED);
+    namenode.persist();
+
+    sch = m_schFactory.createNew(namenode, HOSTNAME);
+    namenode.addServiceComponentHost(sch);
+    sch.setDesiredState(State.INSTALLED);
+    sch.setState(State.INSTALLED);
+    sch.setDesiredStackVersion(new StackId("HDP-2.0.6"));
+    sch.setStackVersion(new StackId("HDP-2.0.6"));
+
+    sch.persist();
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/3befc557/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 c3da07f..5886ab4 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
@@ -43,6 +43,7 @@ import org.apache.ambari.server.controller.spi.Predicate;
 import org.apache.ambari.server.controller.utilities.PredicateBuilder;
 import org.apache.ambari.server.events.listeners.AlertMaintenanceModeListener;
 import org.apache.ambari.server.events.publishers.AmbariEventPublisher;
+import org.apache.ambari.server.orm.AlertDaoHelper;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.OrmTestHelper;
@@ -94,6 +95,8 @@ public class AlertsDAOTest {
   private ServiceComponentHostFactory m_schFactory;
   private AmbariEventPublisher m_eventPublisher;
 
+  private AlertDaoHelper m_alertHelper;
+
   /**
    *
    */
@@ -110,6 +113,7 @@ public class AlertsDAOTest {
     m_schFactory = m_injector.getInstance(ServiceComponentHostFactory.class);
     m_eventPublisher = m_injector.getInstance(AmbariEventPublisher.class);
     m_clusters = m_injector.getInstance(Clusters.class);
+    m_alertHelper = m_injector.getInstance(AlertDaoHelper.class);
 
     // register a listener
     EventBus synchronizedBus = new EventBus();
@@ -778,88 +782,7 @@ public class AlertsDAOTest {
   @Test
   public void testAlertHistoryPredicate() throws Exception {
     Cluster cluster = initializeNewCluster();
-
-    // remove any definitions and start over
-    List<AlertDefinitionEntity> definitions = m_definitionDao.findAll();
-    for (AlertDefinitionEntity definition : definitions) {
-      m_definitionDao.remove(definition);
-    }
-
-    // create some definitions
-    AlertDefinitionEntity namenode = new AlertDefinitionEntity();
-    namenode.setDefinitionName("NAMENODE");
-    namenode.setServiceName("HDFS");
-    namenode.setComponentName("NAMENODE");
-    namenode.setClusterId(cluster.getClusterId());
-    namenode.setHash(UUID.randomUUID().toString());
-    namenode.setScheduleInterval(Integer.valueOf(60));
-    namenode.setScope(Scope.ANY);
-    namenode.setSource("{\"type\" : \"SCRIPT\"}");
-    namenode.setSourceType(SourceType.SCRIPT);
-    m_definitionDao.create(namenode);
-
-    AlertDefinitionEntity datanode = new AlertDefinitionEntity();
-    datanode.setDefinitionName("DATANODE");
-    datanode.setServiceName("HDFS");
-    datanode.setComponentName("DATANODE");
-    datanode.setClusterId(cluster.getClusterId());
-    datanode.setHash(UUID.randomUUID().toString());
-    datanode.setScheduleInterval(Integer.valueOf(60));
-    datanode.setScope(Scope.HOST);
-    datanode.setSource("{\"type\" : \"SCRIPT\"}");
-    datanode.setSourceType(SourceType.SCRIPT);
-    m_definitionDao.create(datanode);
-
-    AlertDefinitionEntity aggregate = new AlertDefinitionEntity();
-    aggregate.setDefinitionName("YARN_AGGREGATE");
-    aggregate.setServiceName("YARN");
-    aggregate.setComponentName(null);
-    aggregate.setClusterId(cluster.getClusterId());
-    aggregate.setHash(UUID.randomUUID().toString());
-    aggregate.setScheduleInterval(Integer.valueOf(60));
-    aggregate.setScope(Scope.SERVICE);
-    aggregate.setSource("{\"type\" : \"SCRIPT\"}");
-    aggregate.setSourceType(SourceType.SCRIPT);
-    m_definitionDao.create(aggregate);
-
-    // create some history
-    AlertHistoryEntity nnHistory = new AlertHistoryEntity();
-    nnHistory.setAlertState(AlertState.OK);
-    nnHistory.setServiceName(namenode.getServiceName());
-    nnHistory.setComponentName(namenode.getComponentName());
-    nnHistory.setClusterId(cluster.getClusterId());
-    nnHistory.setAlertDefinition(namenode);
-    nnHistory.setAlertLabel(namenode.getDefinitionName());
-    nnHistory.setAlertText(namenode.getDefinitionName());
-    nnHistory.setAlertTimestamp(calendar.getTimeInMillis());
-    nnHistory.setHostName(HOSTNAME);
-    m_dao.create(nnHistory);
-
-    AlertHistoryEntity dnHistory = new AlertHistoryEntity();
-    dnHistory.setAlertState(AlertState.WARNING);
-    dnHistory.setServiceName(datanode.getServiceName());
-    dnHistory.setComponentName(datanode.getComponentName());
-    dnHistory.setClusterId(cluster.getClusterId());
-    dnHistory.setAlertDefinition(datanode);
-    dnHistory.setAlertLabel(datanode.getDefinitionName());
-    dnHistory.setAlertText(datanode.getDefinitionName());
-    dnHistory.setAlertTimestamp(calendar.getTimeInMillis());
-    dnHistory.setHostName(HOSTNAME);
-    m_dao.create(dnHistory);
-
-    AlertHistoryEntity aggregateHistory = new AlertHistoryEntity();
-    aggregateHistory.setAlertState(AlertState.CRITICAL);
-    aggregateHistory.setServiceName(aggregate.getServiceName());
-    aggregateHistory.setComponentName(aggregate.getComponentName());
-    aggregateHistory.setClusterId(cluster.getClusterId());
-    aggregateHistory.setAlertDefinition(aggregate);
-    aggregateHistory.setAlertLabel(aggregate.getDefinitionName());
-    aggregateHistory.setAlertText(aggregate.getDefinitionName());
-    aggregateHistory.setAlertTimestamp(calendar.getTimeInMillis());
-    m_dao.create(aggregateHistory);
-
-    List<AlertHistoryEntity> histories = m_dao.findAll();
-    assertEquals(3, histories.size());
+    m_alertHelper.populateData(cluster);
 
     Predicate clusterPredicate = null;
     Predicate hdfsPredicate = null;
@@ -868,6 +791,7 @@ public class AlertsDAOTest {
     Predicate clusterAndHdfsAndCriticalPredicate = null;
     Predicate hdfsAndCriticalOrWarningPredicate = null;
     Predicate alertNamePredicate = null;
+    Predicate historyIdPredicate = null;
 
     clusterPredicate = new PredicateBuilder().property(
         AlertHistoryResourceProvider.ALERT_HISTORY_CLUSTER_NAME).equals("c1").toPredicate();
@@ -875,7 +799,7 @@ public class AlertsDAOTest {
     AlertHistoryRequest request = new AlertHistoryRequest();
 
     request.Predicate = clusterPredicate;
-    histories = m_dao.findAll(request);
+    List<AlertHistoryEntity> histories = m_dao.findAll(request);
     assertEquals(3, histories.size());
 
     hdfsPredicate = new PredicateBuilder().property(
@@ -932,6 +856,14 @@ public class AlertsDAOTest {
     request.Predicate = alertNamePredicate;
     histories = m_dao.findAll(request);
     assertEquals(1, histories.size());
+
+    historyIdPredicate = new PredicateBuilder().property(
+        AlertHistoryResourceProvider.ALERT_HISTORY_ID).equals(
+        histories.get(0).getAlertId()).toPredicate();
+
+    request.Predicate = historyIdPredicate;
+    histories = m_dao.findAll(request);
+    assertEquals(1, histories.size());
   }
 
   private Cluster initializeNewCluster() throws Exception {


Mime
View raw message