Repository: ambari
Updated Branches:
  refs/heads/trunk 378cf1366 -> 9159421b3


http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/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 6467b31..6876877 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
@@ -29,14 +29,10 @@ import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Collections;
 import java.util.Date;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 import java.util.TimeZone;
 import java.util.UUID;
 
-import junit.framework.Assert;
-
 import org.apache.ambari.server.controller.AlertHistoryRequest;
 import 
org.apache.ambari.server.controller.internal.AlertHistoryResourceProvider;
 import org.apache.ambari.server.controller.internal.PageRequestImpl;
@@ -60,17 +56,12 @@ 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.MaintenanceState;
-import org.apache.ambari.server.state.RepositoryVersionState;
 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;
@@ -91,7 +82,7 @@ public class AlertsDAOTest {
   final static Calendar calendar = 
Calendar.getInstance(TimeZone.getTimeZone("UTC"));
 
   private Clusters m_clusters;
-  private Long m_clusterId;
+  private Cluster m_cluster;
   private Injector m_injector;
   private OrmTestHelper m_helper;
   private AlertsDAO m_dao;
@@ -101,6 +92,7 @@ public class AlertsDAOTest {
   private ServiceComponentFactory m_componentFactory;
   private ServiceComponentHostFactory m_schFactory;
   private AmbariEventPublisher m_eventPublisher;
+  private EventBus m_synchronizedBus;
 
   private AlertDaoHelper m_alertHelper;
 
@@ -112,7 +104,6 @@ public class AlertsDAOTest {
     m_injector = Guice.createInjector(new InMemoryDefaultTestModule());
     m_injector.getInstance(GuiceJpaInitializer.class);
     m_helper = m_injector.getInstance(OrmTestHelper.class);
-    m_clusterId = m_helper.createCluster();
     m_dao = m_injector.getInstance(AlertsDAO.class);
     m_definitionDao = m_injector.getInstance(AlertDefinitionDAO.class);
     m_serviceFactory = m_injector.getInstance(ServiceFactory.class);
@@ -122,22 +113,27 @@ public class AlertsDAOTest {
     m_clusters = m_injector.getInstance(Clusters.class);
     m_alertHelper = m_injector.getInstance(AlertDaoHelper.class);
 
-    // register a listener
-    EventBus synchronizedBus = new EventBus();
-    
synchronizedBus.register(m_injector.getInstance(AlertMaintenanceModeListener.class));
-
     // !!! need a synchronous op for testing
+    m_synchronizedBus = new EventBus();
     Field field = AmbariEventPublisher.class.getDeclaredField("m_eventBus");
     field.setAccessible(true);
-    field.set(m_eventPublisher, synchronizedBus);
+    field.set(m_eventPublisher, m_synchronizedBus);
+
+    // install YARN so there is at least 1 service installed and no
+    // unexpected alerts since the test YARN service doesn't have any alerts
+    m_cluster = m_clusters.getClusterById(m_helper.createCluster());
+    m_helper.initializeClusterWithStack(m_cluster);
+    m_helper.addHost(m_clusters, m_cluster, HOSTNAME);
+    m_helper.installYarnService(m_cluster, m_serviceFactory,
+        m_componentFactory, m_schFactory, HOSTNAME);
 
     // 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(m_clusterId);
+      definition.setServiceName("YARN");
+      definition.setComponentName("Component " + i);
+      definition.setClusterId(m_cluster.getClusterId());
       definition.setHash(UUID.randomUUID().toString());
       definition.setScheduleInterval(Integer.valueOf(60));
       definition.setScope(Scope.SERVICE);
@@ -158,13 +154,13 @@ public class AlertsDAOTest {
       for (int i = 0; i < 10; i++) {
         AlertHistoryEntity history = new AlertHistoryEntity();
         history.setServiceName(definition.getServiceName());
-        history.setClusterId(m_clusterId);
+        history.setClusterId(m_cluster.getClusterId());
         history.setAlertDefinition(definition);
         history.setAlertLabel(definition.getDefinitionName() + " " + i);
         history.setAlertText(definition.getDefinitionName() + " " + i);
         history.setAlertTimestamp(calendar.getTimeInMillis());
+        history.setComponentName(definition.getComponentName());
         history.setHostName("h1");
-        history.setComponentName("Component " + i);
 
         history.setAlertState(AlertState.OK);
         if (i == 0 || i == 5) {
@@ -214,7 +210,7 @@ public class AlertsDAOTest {
    */
   @Test
   public void testFindAll() {
-    List<AlertHistoryEntity> alerts = m_dao.findAll(m_clusterId);
+    List<AlertHistoryEntity> alerts = m_dao.findAll(m_cluster.getClusterId());
     assertNotNull(alerts);
     assertEquals(50, alerts.size());
   }
@@ -235,18 +231,21 @@ public class AlertsDAOTest {
   @Test
   public void testFindCurrentByService() {
     List<AlertCurrentEntity> currentAlerts = m_dao.findCurrent();
+    int currentAlertExpectedCount = currentAlerts.size();
+    assertEquals(5, currentAlertExpectedCount);
+
     AlertCurrentEntity current = currentAlerts.get(0);
     AlertHistoryEntity history = current.getAlertHistory();
 
     assertNotNull(history);
 
-    currentAlerts = m_dao.findCurrentByService(m_clusterId,
+    currentAlerts = m_dao.findCurrentByService(m_cluster.getClusterId(),
         history.getServiceName());
 
     assertNotNull(currentAlerts);
-    assertEquals(1, currentAlerts.size());
+    assertEquals(currentAlertExpectedCount, currentAlerts.size());
 
-    currentAlerts = m_dao.findCurrentByService(m_clusterId, "foo");
+    currentAlerts = m_dao.findCurrentByService(m_cluster.getClusterId(), 
"foo");
 
     assertNotNull(currentAlerts);
     assertEquals(0, currentAlerts.size());
@@ -256,13 +255,13 @@ public class AlertsDAOTest {
    * Test looking up current by a host name.
    */
   @Test
-  public void testFindCurrentByHost() {
+  public void testFindCurrentByHost() throws Exception {
     // create a host
     AlertDefinitionEntity hostDef = new AlertDefinitionEntity();
     hostDef.setDefinitionName("Host Alert Definition ");
-    hostDef.setServiceName("HostService");
+    hostDef.setServiceName("YARN");
     hostDef.setComponentName(null);
-    hostDef.setClusterId(m_clusterId);
+    hostDef.setClusterId(m_cluster.getClusterId());
     hostDef.setHash(UUID.randomUUID().toString());
     hostDef.setScheduleInterval(Integer.valueOf(60));
     hostDef.setScope(Scope.HOST);
@@ -273,7 +272,7 @@ public class AlertsDAOTest {
     // history for the definition
     AlertHistoryEntity history = new AlertHistoryEntity();
     history.setServiceName(hostDef.getServiceName());
-    history.setClusterId(m_clusterId);
+    history.setClusterId(m_cluster.getClusterId());
     history.setAlertDefinition(hostDef);
     history.setAlertLabel(hostDef.getDefinitionName());
     history.setAlertText(hostDef.getDefinitionName());
@@ -289,12 +288,12 @@ public class AlertsDAOTest {
     m_dao.create(current);
 
     List<AlertCurrentEntity> currentAlerts = m_dao.findCurrentByHost(
-        m_clusterId, history.getHostName());
+        m_cluster.getClusterId(), history.getHostName());
 
     assertNotNull(currentAlerts);
     assertEquals(1, currentAlerts.size());
 
-    currentAlerts = m_dao.findCurrentByHost(m_clusterId, "foo");
+    currentAlerts = m_dao.findCurrentByHost(m_cluster.getClusterId(), "foo");
 
     assertNotNull(currentAlerts);
     assertEquals(0, currentAlerts.size());
@@ -310,21 +309,22 @@ public class AlertsDAOTest {
     allStates.add(AlertState.WARNING);
     allStates.add(AlertState.CRITICAL);
 
-    List<AlertHistoryEntity> history = m_dao.findAll(m_clusterId, allStates);
+    List<AlertHistoryEntity> history = m_dao.findAll(m_cluster.getClusterId(),
+        allStates);
     assertNotNull(history);
     assertEquals(50, history.size());
 
-    history = m_dao.findAll(m_clusterId,
+    history = m_dao.findAll(m_cluster.getClusterId(),
         Collections.singletonList(AlertState.OK));
     assertNotNull(history);
     assertEquals(40, history.size());
 
-    history = m_dao.findAll(m_clusterId,
+    history = m_dao.findAll(m_cluster.getClusterId(),
         Collections.singletonList(AlertState.CRITICAL));
     assertNotNull(history);
     assertEquals(10, history.size());
 
-    history = m_dao.findAll(m_clusterId,
+    history = m_dao.findAll(m_cluster.getClusterId(),
         Collections.singletonList(AlertState.WARNING));
     assertNotNull(history);
     assertEquals(0, history.size());
@@ -339,14 +339,14 @@ public class AlertsDAOTest {
     calendar.set(2014, Calendar.JANUARY, 1);
 
     // on or after 1/1/2014
-    List<AlertHistoryEntity> history = m_dao.findAll(m_clusterId,
+    List<AlertHistoryEntity> history = m_dao.findAll(m_cluster.getClusterId(),
         calendar.getTime(), null);
 
     assertNotNull(history);
     assertEquals(50, history.size());
 
     // on or before 1/1/2014
-    history = m_dao.findAll(m_clusterId, null, calendar.getTime());
+    history = m_dao.findAll(m_cluster.getClusterId(), null, 
calendar.getTime());
     assertNotNull(history);
     assertEquals(1, history.size());
 
@@ -357,17 +357,17 @@ public class AlertsDAOTest {
     calendar.set(2014, Calendar.JANUARY, 10);
     Date endDate = calendar.getTime();
 
-    history = m_dao.findAll(m_clusterId, startDate, endDate);
+    history = m_dao.findAll(m_cluster.getClusterId(), startDate, endDate);
     assertNotNull(history);
     assertEquals(6, history.size());
 
     // after 3/1
     calendar.set(2014, Calendar.MARCH, 5);
-    history = m_dao.findAll(m_clusterId, calendar.getTime(), null);
+    history = m_dao.findAll(m_cluster.getClusterId(), calendar.getTime(), 
null);
     assertNotNull(history);
     assertEquals(0, history.size());
 
-    history = m_dao.findAll(m_clusterId, endDate, startDate);
+    history = m_dao.findAll(m_cluster.getClusterId(), endDate, startDate);
     assertNotNull(history);
     assertEquals(0, history.size());
   }
@@ -375,10 +375,10 @@ public class AlertsDAOTest {
   @Test
   public void testFindCurrentByHostAndName() throws Exception {
     AlertCurrentEntity entity = m_dao.findCurrentByHostAndName(
-        m_clusterId.longValue(), "h2", "Alert Definition 1");
+        m_cluster.getClusterId(), "h2", "Alert Definition 1");
     assertNull(entity);
 
-    entity = m_dao.findCurrentByHostAndName(m_clusterId.longValue(), "h1",
+    entity = m_dao.findCurrentByHostAndName(m_cluster.getClusterId(), "h1",
         "Alert Definition 1");
 
     assertNotNull(entity);
@@ -391,16 +391,20 @@ public class AlertsDAOTest {
    */
   @Test
   public void testFindCurrentSummary() throws Exception {
-    AlertSummaryDTO summary = m_dao.findCurrentCounts(m_clusterId.longValue(),
+    AlertSummaryDTO summary = m_dao.findCurrentCounts(m_cluster.getClusterId(),
         null, null);
+
     assertEquals(5, summary.getOkCount());
 
-    AlertHistoryEntity h1 = 
m_dao.findCurrentByCluster(m_clusterId.longValue()).get(
+    AlertHistoryEntity h1 = 
m_dao.findCurrentByCluster(m_cluster.getClusterId()).get(
         2).getAlertHistory();
-    AlertHistoryEntity h2 = 
m_dao.findCurrentByCluster(m_clusterId.longValue()).get(
+
+    AlertHistoryEntity h2 = 
m_dao.findCurrentByCluster(m_cluster.getClusterId()).get(
         3).getAlertHistory();
-    AlertHistoryEntity h3 = 
m_dao.findCurrentByCluster(m_clusterId.longValue()).get(
+
+    AlertHistoryEntity h3 = 
m_dao.findCurrentByCluster(m_cluster.getClusterId()).get(
         4).getAlertHistory();
+
     h1.setAlertState(AlertState.WARNING);
     m_dao.merge(h1);
     h2.setAlertState(AlertState.CRITICAL);
@@ -413,7 +417,7 @@ public class AlertsDAOTest {
     int crit = 0;
     int unk = 0;
 
-    for (AlertCurrentEntity h : 
m_dao.findCurrentByCluster(m_clusterId.longValue())) {
+    for (AlertCurrentEntity h : 
m_dao.findCurrentByCluster(m_cluster.getClusterId())) {
       switch (h.getAlertHistory().getAlertState()) {
         case CRITICAL:
           crit++;
@@ -431,7 +435,7 @@ public class AlertsDAOTest {
 
     }
 
-    summary = m_dao.findCurrentCounts(m_clusterId.longValue(), null, null);
+    summary = m_dao.findCurrentCounts(m_cluster.getClusterId(), null, null);
     // !!! db-to-db compare
     assertEquals(ok, summary.getOkCount());
     assertEquals(warn, summary.getWarningCount());
@@ -444,20 +448,19 @@ public class AlertsDAOTest {
     assertEquals(1, summary.getCriticalCount());
     assertEquals(1, summary.getCriticalCount());
 
-    summary = m_dao.findCurrentCounts(m_clusterId.longValue(), "Service 0",
-        null);
-    assertEquals(1, summary.getOkCount());
-    assertEquals(0, summary.getWarningCount());
-    assertEquals(0, summary.getCriticalCount());
-    assertEquals(0, summary.getCriticalCount());
+    summary = m_dao.findCurrentCounts(m_cluster.getClusterId(), "YARN", null);
+    assertEquals(2, summary.getOkCount());
+    assertEquals(1, summary.getWarningCount());
+    assertEquals(1, summary.getCriticalCount());
+    assertEquals(1, summary.getCriticalCount());
 
-    summary = m_dao.findCurrentCounts(m_clusterId.longValue(), null, "h1");
+    summary = m_dao.findCurrentCounts(m_cluster.getClusterId(), null, "h1");
     assertEquals(2, summary.getOkCount());
     assertEquals(1, summary.getWarningCount());
     assertEquals(1, summary.getCriticalCount());
     assertEquals(1, summary.getCriticalCount());
 
-    summary = m_dao.findCurrentCounts(m_clusterId.longValue(), "foo", null);
+    summary = m_dao.findCurrentCounts(m_cluster.getClusterId(), "foo", null);
     assertEquals(0, summary.getOkCount());
     assertEquals(0, summary.getWarningCount());
     assertEquals(0, summary.getCriticalCount());
@@ -469,9 +472,9 @@ public class AlertsDAOTest {
     // definition
     AlertDefinitionEntity definition = new AlertDefinitionEntity();
     definition.setDefinitionName("many_per_cluster");
-    definition.setServiceName("ServiceName");
+    definition.setServiceName("YARN");
     definition.setComponentName(null);
-    definition.setClusterId(m_clusterId);
+    definition.setClusterId(m_cluster.getClusterId());
     definition.setHash(UUID.randomUUID().toString());
     definition.setScheduleInterval(Integer.valueOf(60));
     definition.setScope(Scope.SERVICE);
@@ -487,7 +490,7 @@ public class AlertsDAOTest {
     history.setAlertState(AlertState.OK);
     history.setAlertText("");
     history.setAlertTimestamp(Long.valueOf(1L));
-    history.setClusterId(m_clusterId);
+    history.setClusterId(m_cluster.getClusterId());
     history.setComponentName("");
     history.setHostName("h1");
     history.setServiceName("ServiceName");
@@ -506,7 +509,7 @@ public class AlertsDAOTest {
     history.setAlertState(AlertState.OK);
     history.setAlertText("");
     history.setAlertTimestamp(Long.valueOf(1L));
-    history.setClusterId(m_clusterId);
+    history.setClusterId(m_cluster.getClusterId());
     history.setComponentName("");
     history.setHostName("h2");
     history.setServiceName("ServiceName");
@@ -518,27 +521,27 @@ public class AlertsDAOTest {
     m_dao.merge(current);
 
     AlertSummaryDTO summary = m_dao.findAggregateCounts(
-        m_clusterId.longValue(), "many_per_cluster");
+        m_cluster.getClusterId(), "many_per_cluster");
     assertEquals(2, summary.getOkCount());
     assertEquals(0, summary.getWarningCount());
     assertEquals(0, summary.getCriticalCount());
     assertEquals(0, summary.getUnknownCount());
 
     AlertCurrentEntity c = m_dao.findCurrentByHostAndName(
-        m_clusterId.longValue(),
+        m_cluster.getClusterId(),
         "h2", "many_per_cluster");
     AlertHistoryEntity h = c.getAlertHistory();
     h.setAlertState(AlertState.CRITICAL);
     m_dao.merge(h);
 
-    summary = m_dao.findAggregateCounts(m_clusterId.longValue(),
+    summary = m_dao.findAggregateCounts(m_cluster.getClusterId(),
         "many_per_cluster");
     assertEquals(2, summary.getOkCount());
     assertEquals(0, summary.getWarningCount());
     assertEquals(1, summary.getCriticalCount());
     assertEquals(0, summary.getUnknownCount());
 
-    summary = m_dao.findAggregateCounts(m_clusterId.longValue(), "foo");
+    summary = m_dao.findAggregateCounts(m_cluster.getClusterId(), "foo");
     assertEquals(0, summary.getOkCount());
     assertEquals(0, summary.getWarningCount());
     assertEquals(0, summary.getCriticalCount());
@@ -601,7 +604,10 @@ public class AlertsDAOTest {
    */
   @Test
   public void testMaintenanceMode() throws Exception {
-    Cluster cluster = initializeNewCluster();
+    
m_synchronizedBus.register(m_injector.getInstance(AlertMaintenanceModeListener.class));
+
+    m_helper.installHdfsService(m_cluster, m_serviceFactory,
+        m_componentFactory, m_schFactory, HOSTNAME);
 
     List<AlertCurrentEntity> currents = m_dao.findCurrent();
     for (AlertCurrentEntity current : currents) {
@@ -613,7 +619,7 @@ public class AlertsDAOTest {
     namenode.setDefinitionName("NAMENODE");
     namenode.setServiceName("HDFS");
     namenode.setComponentName("NAMENODE");
-    namenode.setClusterId(cluster.getClusterId());
+    namenode.setClusterId(m_cluster.getClusterId());
     namenode.setHash(UUID.randomUUID().toString());
     namenode.setScheduleInterval(Integer.valueOf(60));
     namenode.setScope(Scope.ANY);
@@ -625,7 +631,7 @@ public class AlertsDAOTest {
     datanode.setDefinitionName("DATANODE");
     datanode.setServiceName("HDFS");
     datanode.setComponentName("DATANODE");
-    datanode.setClusterId(cluster.getClusterId());
+    datanode.setClusterId(m_cluster.getClusterId());
     datanode.setHash(UUID.randomUUID().toString());
     datanode.setScheduleInterval(Integer.valueOf(60));
     datanode.setScope(Scope.HOST);
@@ -637,7 +643,7 @@ public class AlertsDAOTest {
     aggregate.setDefinitionName("DATANODE_UP");
     aggregate.setServiceName("HDFS");
     aggregate.setComponentName(null);
-    aggregate.setClusterId(cluster.getClusterId());
+    aggregate.setClusterId(m_cluster.getClusterId());
     aggregate.setHash(UUID.randomUUID().toString());
     aggregate.setScheduleInterval(Integer.valueOf(60));
     aggregate.setScope(Scope.SERVICE);
@@ -650,7 +656,7 @@ public class AlertsDAOTest {
     nnHistory.setAlertState(AlertState.OK);
     nnHistory.setServiceName(namenode.getServiceName());
     nnHistory.setComponentName(namenode.getComponentName());
-    nnHistory.setClusterId(cluster.getClusterId());
+    nnHistory.setClusterId(m_cluster.getClusterId());
     nnHistory.setAlertDefinition(namenode);
     nnHistory.setAlertLabel(namenode.getDefinitionName());
     nnHistory.setAlertText(namenode.getDefinitionName());
@@ -670,7 +676,7 @@ public class AlertsDAOTest {
     dnHistory.setAlertState(AlertState.WARNING);
     dnHistory.setServiceName(datanode.getServiceName());
     dnHistory.setComponentName(datanode.getComponentName());
-    dnHistory.setClusterId(cluster.getClusterId());
+    dnHistory.setClusterId(m_cluster.getClusterId());
     dnHistory.setAlertDefinition(datanode);
     dnHistory.setAlertLabel(datanode.getDefinitionName());
     dnHistory.setAlertText(datanode.getDefinitionName());
@@ -690,7 +696,7 @@ public class AlertsDAOTest {
     aggregateHistory.setAlertState(AlertState.CRITICAL);
     aggregateHistory.setServiceName(aggregate.getServiceName());
     aggregateHistory.setComponentName(aggregate.getComponentName());
-    aggregateHistory.setClusterId(cluster.getClusterId());
+    aggregateHistory.setClusterId(m_cluster.getClusterId());
     aggregateHistory.setAlertDefinition(aggregate);
     aggregateHistory.setAlertLabel(aggregate.getDefinitionName());
     aggregateHistory.setAlertText(aggregate.getDefinitionName());
@@ -713,7 +719,7 @@ public class AlertsDAOTest {
     }
 
     // turn on HDFS MM
-    Service hdfs = 
m_clusters.getClusterById(cluster.getClusterId()).getService(
+    Service hdfs = 
m_clusters.getClusterById(m_cluster.getClusterId()).getService(
         "HDFS");
 
     hdfs.setMaintenanceState(MaintenanceState.ON);
@@ -735,7 +741,7 @@ public class AlertsDAOTest {
 
     // turn on host MM
     Host host = m_clusters.getHost(HOSTNAME);
-    host.setMaintenanceState(cluster.getClusterId(), MaintenanceState.ON);
+    host.setMaintenanceState(m_cluster.getClusterId(), MaintenanceState.ON);
 
     // only NAMENODE and DATANODE should be in MM; the aggregate should not
     // since the host is in MM
@@ -750,7 +756,7 @@ public class AlertsDAOTest {
     }
 
     // turn host MM off
-    host.setMaintenanceState(cluster.getClusterId(), MaintenanceState.OFF);
+    host.setMaintenanceState(m_cluster.getClusterId(), MaintenanceState.OFF);
 
     currents = m_dao.findCurrent();
     assertEquals(3, currents.size());
@@ -760,7 +766,7 @@ public class AlertsDAOTest {
 
     // turn a component MM on
     ServiceComponentHost nnComponent = null;
-    List<ServiceComponentHost> schs = 
cluster.getServiceComponentHosts(HOSTNAME);
+    List<ServiceComponentHost> schs = 
m_cluster.getServiceComponentHosts(HOSTNAME);
     for (ServiceComponentHost sch : schs) {
       if ("NAMENODE".equals(sch.getServiceComponentName())) {
         sch.setMaintenanceState(MaintenanceState.ON);
@@ -789,8 +795,9 @@ public class AlertsDAOTest {
    */
   @Test
   public void testAlertHistoryPredicate() throws Exception {
-    Cluster cluster = initializeNewCluster();
-    m_alertHelper.populateData(cluster);
+    m_helper.installHdfsService(m_cluster, m_serviceFactory,
+        m_componentFactory, m_schFactory, HOSTNAME);
+    m_alertHelper.populateData(m_cluster);
 
     Predicate clusterPredicate = null;
     Predicate hdfsPredicate = null;
@@ -881,8 +888,9 @@ public class AlertsDAOTest {
    */
   @Test
   public void testAlertHistoryPagination() throws Exception {
-    Cluster cluster = initializeNewCluster();
-    m_alertHelper.populateData(cluster);
+    m_helper.installHdfsService(m_cluster, m_serviceFactory,
+        m_componentFactory, m_schFactory, HOSTNAME);
+    m_alertHelper.populateData(m_cluster);
 
     AlertHistoryRequest request = new AlertHistoryRequest();
     request.Pagination = null;
@@ -920,8 +928,9 @@ public class AlertsDAOTest {
    */
   @Test
   public void testAlertHistorySorting() throws Exception {
-    Cluster cluster = initializeNewCluster();
-    m_alertHelper.populateData(cluster);
+    m_helper.installHdfsService(m_cluster, m_serviceFactory,
+        m_componentFactory, m_schFactory, HOSTNAME);
+    m_alertHelper.populateData(m_cluster);
 
     List<SortRequestProperty> sortProperties = new 
ArrayList<SortRequestProperty>();
     SortRequest sortRequest = new SortRequestImpl(sortProperties);
@@ -982,11 +991,14 @@ public class AlertsDAOTest {
     assertNotNull(currentAlerts);
     assertEquals(5, currentAlerts.size());
 
-    m_dao.removeCurrentByService("Service 1");
-    m_dao.removeCurrentByService("Service 2");
+    // assert none removed for HDFS
+    m_dao.removeCurrentByService("HDFS");
+    currentAlerts = m_dao.findCurrent();
+    assertEquals(5, currentAlerts.size());
 
+    m_dao.removeCurrentByService("YARN");
     currentAlerts = m_dao.findCurrent();
-    assertEquals(3, currentAlerts.size());
+    assertEquals(0, currentAlerts.size());
   }
 
   @Test
@@ -1013,7 +1025,7 @@ public class AlertsDAOTest {
     assertEquals(5, currentAlerts.size());
 
     AlertCurrentEntity entity = m_dao.findCurrentByHostAndName(
-        m_clusterId.longValue(), "h1", "Alert Definition 1");
+        m_cluster.getClusterId(), "h1", "Alert Definition 1");
 
     assertNotNull(entity);
 
@@ -1041,81 +1053,4 @@ public class AlertsDAOTest {
     currentAlerts = m_dao.findCurrent();
     assertEquals(4, currentAlerts.size());
   }
-
-  private Cluster initializeNewCluster() throws Exception {
-    String clusterName = "cluster-" + System.currentTimeMillis();
-    m_clusters.addCluster(clusterName);
-
-    Cluster cluster = m_clusters.getCluster(clusterName);
-    StackId stackId = new StackId("HDP", "2.0.6");
-    cluster.setDesiredStackVersion(stackId);
-    cluster.createClusterVersion(stackId.getStackName(), 
stackId.getStackVersion(), "admin", RepositoryVersionState.CURRENT);
-
-    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/9159421b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertReceivedListenerTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertReceivedListenerTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertReceivedListenerTest.java
index 8a162fd..0f98a3d 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertReceivedListenerTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertReceivedListenerTest.java
@@ -19,10 +19,7 @@ package org.apache.ambari.server.state.alerts;
 
 import static org.junit.Assert.assertEquals;
 
-import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 import java.util.UUID;
 
 import org.apache.ambari.server.events.AlertReceivedEvent;
@@ -38,23 +35,18 @@ import org.apache.ambari.server.state.Alert;
 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.Service;
-import org.apache.ambari.server.state.ServiceComponentHost;
+import org.apache.ambari.server.state.ServiceComponentFactory;
+import org.apache.ambari.server.state.ServiceComponentHostFactory;
+import org.apache.ambari.server.state.ServiceFactory;
 import org.apache.ambari.server.state.alert.Scope;
 import org.apache.ambari.server.state.alert.SourceType;
-import org.easymock.EasyMock;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
-import com.google.inject.Binder;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-import com.google.inject.Module;
 import com.google.inject.persist.PersistService;
-import com.google.inject.util.Modules;
-
 
 /**
  * Tests the {@link AlertReceivedListener}.
@@ -62,76 +54,66 @@ import com.google.inject.util.Modules;
 public class AlertReceivedListenerTest {
 
   private static final String ALERT_DEFINITION = "alert_definition_";
-  private static final String CLUSTER_NAME = "c1";
-  private static final String SERVICE = "Service";
-  private static final String COMPONENT = "Component";
   private static final String HOST1 = "h1";
   private static final String ALERT_LABEL = "My Label";
+  private Injector m_injector;
+  private AlertsDAO m_dao;
+  private AlertDefinitionDAO m_definitionDao;
 
-  private Long clusterId;
-  private Injector injector;
-  private OrmTestHelper helper;
-  private AlertsDAO dao;
-  private AlertDefinitionDAO definitionDao;
+  private Clusters m_clusters;
+  private Cluster m_cluster;
 
-  private Clusters clusters;
-  private Cluster cluster;
+  private OrmTestHelper m_helper;
+  private ServiceFactory m_serviceFactory;
+  private ServiceComponentFactory m_componentFactory;
+  private ServiceComponentHostFactory m_schFactory;
 
   @Before
   public void setup() throws Exception {
-    clusters = EasyMock.createNiceMock(Clusters.class);
-    cluster = EasyMock.createNiceMock(Cluster.class);
-
-    injector = Guice.createInjector(Modules.override(
-        new InMemoryDefaultTestModule()).with(new MockModule()));
-
-    injector.getInstance(GuiceJpaInitializer.class);
-    helper = injector.getInstance(OrmTestHelper.class);
-    clusterId = helper.createCluster();
-    dao = injector.getInstance(AlertsDAO.class);
-    definitionDao = injector.getInstance(AlertDefinitionDAO.class);
+    m_injector = Guice.createInjector(new InMemoryDefaultTestModule());
+    m_injector.getInstance(GuiceJpaInitializer.class);
 
-    List<Host> hosts = new ArrayList<Host>();
-    Host host = EasyMock.createNiceMock(Host.class);
-    EasyMock.expect(host.getHostName()).andReturn(HOST1).anyTimes();
-    hosts.add(host);
+    m_helper = m_injector.getInstance(OrmTestHelper.class);
+    m_clusters = m_injector.getInstance(Clusters.class);
+    m_serviceFactory = m_injector.getInstance(ServiceFactory.class);
+    m_componentFactory = m_injector.getInstance(ServiceComponentFactory.class);
+    m_schFactory = m_injector.getInstance(ServiceComponentHostFactory.class);
 
-    Map<String,Service> services = new HashMap<String, Service>();
-    services.put("Service 1", EasyMock.createNiceMock(Service.class));
+    // install YARN so there is at least 1 service installed and no
+    // unexpected alerts since the test YARN service doesn't have any alerts
+    m_cluster = m_helper.buildNewCluster(m_clusters, m_serviceFactory,
+        m_componentFactory, m_schFactory, HOST1);
 
-    List<ServiceComponentHost> schs = new ArrayList<ServiceComponentHost>();
-    ServiceComponentHost sch = 
EasyMock.createNiceMock(ServiceComponentHost.class);
-    EasyMock.expect(sch.getServiceComponentName()).andReturn("Component 
1").anyTimes();
-    schs.add(sch);
+    m_dao = m_injector.getInstance(AlertsDAO.class);
+    m_definitionDao = m_injector.getInstance(AlertDefinitionDAO.class);
 
-    // setup isValid expectations
-    
EasyMock.expect(clusters.getCluster(CLUSTER_NAME)).andReturn(cluster).anyTimes();
-    EasyMock.expect(clusters.getHosts()).andReturn(hosts).anyTimes();
-    EasyMock.expect(cluster.getServices()).andReturn(services).anyTimes();
-    
EasyMock.expect(cluster.getServiceComponentHosts(HOST1)).andReturn(schs).anyTimes();
-
-    EasyMock.replay(clusters, cluster, sch, host);
-
-    // create 5 definitions
+    // create 5 definitions, some with HDFS and some with YARN
     for (int i = 0; i < 5; i++) {
+      String serviceName = "HDFS";
+      String componentName = "DATANODE";
+      if (i >= 3) {
+        serviceName = "YARN";
+        componentName = "RESOURCEMANAGER";
+      }
+
       AlertDefinitionEntity definition = new AlertDefinitionEntity();
       definition.setDefinitionName(ALERT_DEFINITION + i);
-      definition.setServiceName(SERVICE + " " + i);
-      definition.setComponentName(COMPONENT + " " + i);
-      definition.setClusterId(clusterId);
+      definition.setServiceName(serviceName);
+      definition.setComponentName(componentName);
+      definition.setClusterId(m_cluster.getClusterId());
       definition.setHash(UUID.randomUUID().toString());
       definition.setScheduleInterval(Integer.valueOf(60));
       definition.setScope(Scope.SERVICE);
       definition.setSource("{\"type\" : \"SCRIPT\"}");
       definition.setSourceType(SourceType.SCRIPT);
-      definitionDao.create(definition);
+      m_definitionDao.create(definition);
     }
   }
 
   @After
   public void teardown() {
-    injector.getInstance(PersistService.class).stop();
-    injector = null;
+    m_injector.getInstance(PersistService.class).stop();
+    m_injector = null;
   }
 
   /**
@@ -140,38 +122,39 @@ public class AlertReceivedListenerTest {
   @Test
   public void testDisabledAlert() {
     String definitionName = ALERT_DEFINITION + "1";
-    String serviceName = "Service 1";
-    String componentName = "Component 1";
+    String componentName = "DATANODE";
 
-    Alert alert1 = new Alert(definitionName, null, serviceName, componentName,
+    Alert alert1 = new Alert(definitionName, null, "HDFS", componentName,
         HOST1, AlertState.OK);
 
-    alert1.setCluster(CLUSTER_NAME);
+    alert1.setCluster(m_cluster.getClusterName());
     alert1.setLabel(ALERT_LABEL);
-    alert1.setText(serviceName + " " + componentName + " is OK");
+    alert1.setText("HDFS " + componentName + " is OK");
     alert1.setTimestamp(1L);
 
     // verify that the listener works with a regular alert
-    AlertReceivedListener listener = 
injector.getInstance(AlertReceivedListener.class);
-    AlertReceivedEvent event1 = new AlertReceivedEvent(clusterId, alert1);
+    AlertReceivedListener listener = 
m_injector.getInstance(AlertReceivedListener.class);
+    AlertReceivedEvent event1 = new AlertReceivedEvent(
+        m_cluster.getClusterId(), alert1);
     listener.onAlertEvent(event1);
 
-    List<AlertCurrentEntity> allCurrent = dao.findCurrent();
+    List<AlertCurrentEntity> allCurrent = m_dao.findCurrent();
     assertEquals(1, allCurrent.size());
 
     // disable definition
-    AlertDefinitionEntity definition = definitionDao.findByName(clusterId, 
definitionName);
+    AlertDefinitionEntity definition = m_definitionDao.findByName(
+        m_cluster.getClusterId(), definitionName);
     definition.setEnabled(false);
-    definitionDao.merge(definition);
+    m_definitionDao.merge(definition);
 
     // remove disabled
-    dao.removeCurrentDisabledAlerts();
-    allCurrent = dao.findCurrent();
+    m_dao.removeCurrentDisabledAlerts();
+    allCurrent = m_dao.findCurrent();
     assertEquals(0, allCurrent.size());
 
     // verify no new alerts for disabled
     listener.onAlertEvent(event1);
-    allCurrent = dao.findCurrent();
+    allCurrent = m_dao.findCurrent();
     assertEquals(0, allCurrent.size());
   }
 
@@ -181,36 +164,36 @@ public class AlertReceivedListenerTest {
   @Test
   public void testInvalidHost() {
     String definitionName = ALERT_DEFINITION + "1";
-    String serviceName = "Service 1";
-    String componentName = "Component 1";
+    String componentName = "DATANODE";
 
-    Alert alert1 = new Alert(definitionName, null, serviceName, componentName,
+    Alert alert1 = new Alert(definitionName, null, "HDFS", componentName,
         HOST1, AlertState.OK);
 
-    alert1.setCluster(CLUSTER_NAME);
+    alert1.setCluster(m_cluster.getClusterName());
     alert1.setLabel(ALERT_LABEL);
-    alert1.setText(serviceName + " " + componentName + " is OK");
+    alert1.setText("HDFS " + componentName + " is OK");
     alert1.setTimestamp(1L);
 
     // verify that the listener works with a regular alert
-    AlertReceivedListener listener = 
injector.getInstance(AlertReceivedListener.class);
-    AlertReceivedEvent event1 = new AlertReceivedEvent(clusterId, alert1);
+    AlertReceivedListener listener = 
m_injector.getInstance(AlertReceivedListener.class);
+    AlertReceivedEvent event1 = new AlertReceivedEvent(
+        m_cluster.getClusterId(), alert1);
     listener.onAlertEvent(event1);
 
-    List<AlertCurrentEntity> allCurrent = dao.findCurrent();
+    List<AlertCurrentEntity> allCurrent = m_dao.findCurrent();
     assertEquals(1, allCurrent.size());
 
     // invalid host
     alert1.setHost("INVALID");
 
     // remove all
-    dao.removeCurrentByHost(HOST1);
-    allCurrent = dao.findCurrent();
+    m_dao.removeCurrentByHost(HOST1);
+    allCurrent = m_dao.findCurrent();
     assertEquals(0, allCurrent.size());
 
     // verify no new alerts for disabled
     listener.onAlertEvent(event1);
-    allCurrent = dao.findCurrent();
+    allCurrent = m_dao.findCurrent();
     assertEquals(0, allCurrent.size());
   }
 
@@ -219,22 +202,22 @@ public class AlertReceivedListenerTest {
    */
   @Test
   public void testInvalidAlertDefinition() {
-    String serviceName = "Service 1";
-    String componentName = "Component 1";
+    String componentName = "DATANODE";
 
-    Alert alert1 = new Alert("missing_alert_definition_name", null,
-        serviceName, componentName, HOST1, AlertState.OK);
+    Alert alert1 = new Alert("missing_alert_definition_name", null, "HDFS",
+        componentName, HOST1, AlertState.OK);
 
     alert1.setLabel(ALERT_LABEL);
-    alert1.setText(serviceName + " " + componentName + " is OK");
+    alert1.setText("HDFS " + componentName + " is OK");
     alert1.setTimestamp(1L);
 
     // bad alert definition name means no current alerts
-    AlertReceivedListener listener = 
injector.getInstance(AlertReceivedListener.class);
-    AlertReceivedEvent event1 = new AlertReceivedEvent(clusterId, alert1);
+    AlertReceivedListener listener = 
m_injector.getInstance(AlertReceivedListener.class);
+    AlertReceivedEvent event1 = new AlertReceivedEvent(
+        m_cluster.getClusterId(), alert1);
     listener.onAlertEvent(event1);
 
-    List<AlertCurrentEntity> allCurrent = dao.findCurrent();
+    List<AlertCurrentEntity> allCurrent = m_dao.findCurrent();
     assertEquals(0, allCurrent.size());
   }
 
@@ -244,47 +227,36 @@ public class AlertReceivedListenerTest {
   @Test
   public void testInvalidServiceComponentHost() {
     String definitionName = ALERT_DEFINITION + "1";
-    String serviceName = "Service 1";
-    String componentName = "Component 1";
+    String componentName = "DATANODE";
 
-    Alert alert1 = new Alert(definitionName, null, serviceName, componentName,
+    Alert alert1 = new Alert(definitionName, null, "HDFS", componentName,
         HOST1, AlertState.OK);
 
-    alert1.setCluster(CLUSTER_NAME);
+    alert1.setCluster(m_cluster.getClusterName());
     alert1.setLabel(ALERT_LABEL);
-    alert1.setText(serviceName + " " + componentName + " is OK");
+    alert1.setText("HDFS " + componentName + " is OK");
     alert1.setTimestamp(1L);
 
     // verify that the listener works with a regular alert
-    AlertReceivedListener listener = 
injector.getInstance(AlertReceivedListener.class);
-    AlertReceivedEvent event1 = new AlertReceivedEvent(clusterId, alert1);
+    AlertReceivedListener listener = 
m_injector.getInstance(AlertReceivedListener.class);
+    AlertReceivedEvent event1 = new AlertReceivedEvent(
+        m_cluster.getClusterId(), alert1);
     listener.onAlertEvent(event1);
 
-    List<AlertCurrentEntity> allCurrent = dao.findCurrent();
+    List<AlertCurrentEntity> allCurrent = m_dao.findCurrent();
     assertEquals(1, allCurrent.size());
 
     // invalid host
     alert1.setComponent("INVALID");
 
     // remove all
-    dao.removeCurrentByHost(HOST1);
-    allCurrent = dao.findCurrent();
+    m_dao.removeCurrentByHost(HOST1);
+    allCurrent = m_dao.findCurrent();
     assertEquals(0, allCurrent.size());
 
     // verify no new alerts for disabled
     listener.onAlertEvent(event1);
-    allCurrent = dao.findCurrent();
+    allCurrent = m_dao.findCurrent();
     assertEquals(0, allCurrent.size());
   }
-
-  /**
-   *
-   */
-  private class MockModule implements Module {
-    @Override
-    public void configure(Binder binder) {
-      binder.bind(Clusters.class).toInstance(clusters);
-      binder.bind(Cluster.class).toInstance(cluster);
-    }
-  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/9159421b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/AlertDataManagerTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/AlertDataManagerTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/AlertDataManagerTest.java
index 2be5b47..023d0ad 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/AlertDataManagerTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/AlertDataManagerTest.java
@@ -50,7 +50,12 @@ import 
org.apache.ambari.server.orm.entities.AlertNoticeEntity;
 import org.apache.ambari.server.orm.entities.AlertTargetEntity;
 import org.apache.ambari.server.state.Alert;
 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.MaintenanceState;
+import org.apache.ambari.server.state.ServiceComponentFactory;
+import org.apache.ambari.server.state.ServiceComponentHostFactory;
+import org.apache.ambari.server.state.ServiceFactory;
 import org.apache.ambari.server.state.alert.AggregateDefinitionMapping;
 import org.apache.ambari.server.state.alert.AggregateSource;
 import org.apache.ambari.server.state.alert.AlertDefinition;
@@ -78,80 +83,101 @@ import com.google.inject.persist.PersistService;
 public class AlertDataManagerTest {
 
   private static final String ALERT_DEFINITION = "Alert Definition 1";
-  private static final String SERVICE = "service1";
+  private static final String SERVICE = "HDFS";
   private static final String COMPONENT = "component1";
   private static final String HOST1 = "h1";
   private static final String HOST2 = "h2";
   private static final String ALERT_LABEL = "My Label";
 
-  private Long clusterId;
-  private Injector injector;
-  private OrmTestHelper helper;
-  private AlertsDAO dao;
-  private AlertDispatchDAO dispatchDao;
-  private AlertDefinitionDAO definitionDao;
+  private Injector m_injector;
+  private OrmTestHelper m_helper;
+  private Clusters m_clusters;
+  private Cluster m_cluster;
+  private AlertsDAO m_dao;
+  private AlertDispatchDAO m_dispatchDao;
+  private AlertDefinitionDAO m_definitionDao;
+
+  private ServiceFactory m_serviceFactory;
+  private ServiceComponentFactory m_componentFactory;
+  private ServiceComponentHostFactory m_schFactory;
 
   @Before
   public void setup() throws Exception {
-    injector = Guice.createInjector(new InMemoryDefaultTestModule());
-    injector.getInstance(GuiceJpaInitializer.class);
-    helper = injector.getInstance(OrmTestHelper.class);
-    clusterId = helper.createCluster();
-    dao = injector.getInstance(AlertsDAO.class);
-    dispatchDao = injector.getInstance(AlertDispatchDAO.class);
-    definitionDao = injector.getInstance(AlertDefinitionDAO.class);
+    m_injector = Guice.createInjector(new InMemoryDefaultTestModule());
+    m_injector.getInstance(GuiceJpaInitializer.class);
+    m_helper = m_injector.getInstance(OrmTestHelper.class);
+    m_dao = m_injector.getInstance(AlertsDAO.class);
+    m_dispatchDao = m_injector.getInstance(AlertDispatchDAO.class);
+    m_definitionDao = m_injector.getInstance(AlertDefinitionDAO.class);
+    m_clusters = m_injector.getInstance(Clusters.class);
+    m_serviceFactory = m_injector.getInstance(ServiceFactory.class);
+    m_componentFactory = m_injector.getInstance(ServiceComponentFactory.class);
+    m_schFactory = m_injector.getInstance(ServiceComponentHostFactory.class);
+
+    // install YARN so there is at least 1 service installed and no
+    // unexpected alerts since the test YARN service doesn't have any alerts
+    m_cluster = m_helper.buildNewCluster(m_clusters, m_serviceFactory,
+        m_componentFactory, m_schFactory, HOST1);
+
+    m_helper.addHost(m_clusters, m_cluster, HOST2);
 
     // 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.setServiceName(SERVICE);
+      definition.setComponentName(COMPONENT);
+      definition.setClusterId(m_cluster.getClusterId());
       definition.setHash(UUID.randomUUID().toString());
       definition.setScheduleInterval(Integer.valueOf(60));
       definition.setScope(Scope.SERVICE);
       definition.setSource("{\"type\" : \"SCRIPT\"}");
       definition.setSourceType(SourceType.SCRIPT);
-      definitionDao.create(definition);
+      m_definitionDao.create(definition);
     }
   }
 
   @After
   public void teardown() {
-    injector.getInstance(PersistService.class).stop();
-    injector = null;
+    m_injector.getInstance(PersistService.class).stop();
+    m_injector = null;
   }
 
   @Test
   public void testAlertRecords() {
-    Alert alert1 = new Alert(ALERT_DEFINITION, null, SERVICE, COMPONENT, 
HOST1, AlertState.OK);
+    Alert alert1 = new Alert(ALERT_DEFINITION, null, SERVICE, COMPONENT, HOST1,
+        AlertState.OK);
     alert1.setLabel(ALERT_LABEL);
     alert1.setText("Component component1 is OK");
     alert1.setTimestamp(1L);
 
-    Alert alert2 = new Alert(ALERT_DEFINITION, null, SERVICE, COMPONENT, 
HOST2, AlertState.CRITICAL);
+    Alert alert2 = new Alert(ALERT_DEFINITION, null, SERVICE, COMPONENT, HOST2,
+        AlertState.CRITICAL);
     alert2.setLabel(ALERT_LABEL);
     alert2.setText("Component component2 is not OK");
 
-    AlertReceivedListener listener = 
injector.getInstance(AlertReceivedListener.class);
+    AlertReceivedListener listener = 
m_injector.getInstance(AlertReceivedListener.class);
 
-    AlertReceivedEvent event1 = new AlertReceivedEvent(clusterId.longValue(),
+    AlertReceivedEvent event1 = new AlertReceivedEvent(
+        m_cluster.getClusterId(),
         alert1);
 
-    AlertReceivedEvent event2 = new AlertReceivedEvent(clusterId.longValue(),
+    AlertReceivedEvent event2 = new AlertReceivedEvent(
+        m_cluster.getClusterId(),
         alert2);
 
     listener.onAlertEvent(event1);
     listener.onAlertEvent(event2);
 
-    List<AlertCurrentEntity> allCurrent = 
dao.findCurrentByService(clusterId.longValue(), SERVICE);
+    List<AlertCurrentEntity> allCurrent = m_dao.findCurrentByService(
+        m_cluster.getClusterId(), SERVICE);
     assertEquals(2, allCurrent.size());
 
-    List<AlertHistoryEntity> allHistory = dao.findAll(clusterId.longValue());
+    List<AlertHistoryEntity> allHistory = 
m_dao.findAll(m_cluster.getClusterId());
     assertEquals(2, allHistory.size());
 
-    AlertCurrentEntity current = 
dao.findCurrentByHostAndName(clusterId.longValue(), HOST1, ALERT_DEFINITION);
+    AlertCurrentEntity current = m_dao.findCurrentByHostAndName(
+        m_cluster.getClusterId(), HOST1, ALERT_DEFINITION);
     assertNotNull(current);
     assertEquals(HOST1, current.getAlertHistory().getHostName());
     assertEquals(ALERT_DEFINITION, 
current.getAlertHistory().getAlertDefinition().getDefinitionName());
@@ -165,17 +191,20 @@ public class AlertDataManagerTest {
     Long historyId = current.getAlertHistory().getAlertId();
 
     // no new history since the state is the same
-    Alert alert3 = new Alert(ALERT_DEFINITION, null, SERVICE, COMPONENT, 
HOST1, AlertState.OK);
+    Alert alert3 = new Alert(ALERT_DEFINITION, null, SERVICE, COMPONENT, HOST1,
+        AlertState.OK);
     alert3.setLabel(ALERT_LABEL);
     alert3.setText("Component component1 is OK");
     alert3.setTimestamp(2L);
 
-    AlertReceivedEvent event3 = new AlertReceivedEvent(clusterId.longValue(),
+    AlertReceivedEvent event3 = new AlertReceivedEvent(
+        m_cluster.getClusterId(),
         alert3);
 
     listener.onAlertEvent(event3);
 
-    current = dao.findCurrentByHostAndName(clusterId.longValue(), HOST1, 
ALERT_DEFINITION);
+    current = m_dao.findCurrentByHostAndName(m_cluster.getClusterId(), HOST1,
+        ALERT_DEFINITION);
     assertNotNull(current);
     assertEquals(currentId, current.getAlertId());
     assertEquals(historyId, current.getAlertHistory().getAlertId());
@@ -187,24 +216,27 @@ public class AlertDataManagerTest {
     assertEquals(1L, current.getOriginalTimestamp().longValue());
     assertEquals(2L, current.getLatestTimestamp().longValue());
 
-    allCurrent = dao.findCurrentByService(clusterId.longValue(), SERVICE);
+    allCurrent = m_dao.findCurrentByService(m_cluster.getClusterId(), SERVICE);
     assertEquals(2, allCurrent.size());
 
-    allHistory = dao.findAll(clusterId.longValue());
+    allHistory = m_dao.findAll(m_cluster.getClusterId());
     assertEquals(2, allHistory.size());
 
     // change to warning
-    Alert alert4 = new Alert(ALERT_DEFINITION, null, SERVICE, COMPONENT, 
HOST1, AlertState.WARNING);
+    Alert alert4 = new Alert(ALERT_DEFINITION, null, SERVICE, COMPONENT, HOST1,
+        AlertState.WARNING);
     alert4.setLabel(ALERT_LABEL);
     alert4.setText("Component component1 is about to go down");
     alert4.setTimestamp(3L);
 
-    AlertReceivedEvent event4 = new AlertReceivedEvent(clusterId.longValue(),
+    AlertReceivedEvent event4 = new AlertReceivedEvent(
+        m_cluster.getClusterId(),
         alert4);
 
     listener.onAlertEvent(event4);
 
-    current = dao.findCurrentByHostAndName(clusterId.longValue(), HOST1, 
ALERT_DEFINITION);
+    current = m_dao.findCurrentByHostAndName(m_cluster.getClusterId(), HOST1,
+        ALERT_DEFINITION);
     assertNotNull(current);
     assertEquals(current.getAlertId(), currentId);
     assertFalse(historyId.equals(current.getAlertHistory().getAlertId()));
@@ -216,10 +248,10 @@ public class AlertDataManagerTest {
     assertEquals(3L, current.getOriginalTimestamp().longValue());
     assertEquals(3L, current.getLatestTimestamp().longValue());
 
-    allCurrent = dao.findCurrentByService(clusterId.longValue(), SERVICE);
+    allCurrent = m_dao.findCurrentByService(m_cluster.getClusterId(), SERVICE);
     assertEquals(2, allCurrent.size());
 
-    allHistory = dao.findAll(clusterId.longValue());
+    allHistory = m_dao.findAll(m_cluster.getClusterId());
     assertEquals(3, allHistory.size());
   }
 
@@ -231,24 +263,24 @@ public class AlertDataManagerTest {
    */
   @Test
   public void testAlertNotices() throws Exception {
-    List<AlertNoticeEntity> notices = dispatchDao.findAllNotices();
+    List<AlertNoticeEntity> notices = m_dispatchDao.findAllNotices();
     assertEquals( 0, notices.size() );
 
-    List<AlertDefinitionEntity> definitions = definitionDao.findAll(clusterId);
+    List<AlertDefinitionEntity> definitions = 
m_definitionDao.findAll(m_cluster.getClusterId());
     AlertDefinitionEntity definition = definitions.get(0);
 
     AlertHistoryEntity history = new AlertHistoryEntity();
     history.setServiceName(definition.getServiceName());
-    history.setClusterId(clusterId);
+    history.setClusterId(m_cluster.getClusterId());
     history.setAlertDefinition(definition);
     history.setAlertLabel(definition.getDefinitionName());
     history.setAlertText(definition.getDefinitionName());
     history.setAlertTimestamp(System.currentTimeMillis());
     history.setHostName(HOST1);
     history.setAlertState(AlertState.OK);
-    dao.create(history);
+    m_dao.create(history);
 
-    List<AlertHistoryEntity> histories = dao.findAll(clusterId);
+    List<AlertHistoryEntity> histories = 
m_dao.findAll(m_cluster.getClusterId());
     assertEquals(1, histories.size());
 
     AlertCurrentEntity currentAlert = new AlertCurrentEntity();
@@ -256,26 +288,28 @@ public class AlertDataManagerTest {
     currentAlert.setMaintenanceState(MaintenanceState.OFF);
     currentAlert.setOriginalTimestamp(System.currentTimeMillis());
     currentAlert.setLatestTimestamp(System.currentTimeMillis());
-    dao.create(currentAlert);
+    m_dao.create(currentAlert);
 
-    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_cluster.getClusterId(), targets);
     group.addAlertDefinition( definitions.get(0) );
-    dispatchDao.merge(group);
+    m_dispatchDao.merge(group);
 
     Alert alert1 = new Alert(ALERT_DEFINITION, null, SERVICE, COMPONENT, HOST1,
         AlertState.OK);
 
-    AlertStateChangeEvent event = new AlertStateChangeEvent(clusterId, alert1,
+    AlertStateChangeEvent event = new AlertStateChangeEvent(
+        m_cluster.getClusterId(), alert1,
         currentAlert, AlertState.CRITICAL);
 
-    AlertStateChangedListener listener = 
injector.getInstance(AlertStateChangedListener.class);
+    AlertStateChangedListener listener = 
m_injector.getInstance(AlertStateChangedListener.class);
     listener.onAlertEvent(event);
 
-    notices = dispatchDao.findAllNotices();
+    notices = m_dispatchDao.findAllNotices();
     assertEquals(1, notices.size());
   }
 
@@ -286,22 +320,22 @@ public class AlertDataManagerTest {
     definition.setDefinitionName("to_aggregate");
     definition.setLabel("My Label");
     definition.setLabel("My Description");
-    definition.setServiceName("SERVICE");
+    definition.setServiceName(SERVICE);
     definition.setComponentName(null);
-    definition.setClusterId(clusterId);
+    definition.setClusterId(m_cluster.getClusterId());
     definition.setHash(UUID.randomUUID().toString());
     definition.setScheduleInterval(Integer.valueOf(60));
     definition.setScope(Scope.HOST);
     definition.setSource("{\"type\" : \"SCRIPT\"}");
     definition.setSourceType(SourceType.SCRIPT);
-    definitionDao.create(definition);
+    m_definitionDao.create(definition);
 
     // create aggregate of definition
     AlertDefinitionEntity aggDef = new AlertDefinitionEntity();
     aggDef.setDefinitionName("aggregate_test");
-    aggDef.setServiceName("SERVICE");
+    aggDef.setServiceName(SERVICE);
     aggDef.setComponentName(null);
-    aggDef.setClusterId(clusterId);
+    aggDef.setClusterId(m_cluster.getClusterId());
     aggDef.setHash(UUID.randomUUID().toString());
     aggDef.setScheduleInterval(Integer.valueOf(60));
     aggDef.setScope(Scope.SERVICE);
@@ -336,7 +370,7 @@ public class AlertDataManagerTest {
 
     aggDef.setSource(gson.toJson(source));
     aggDef.setSourceType(SourceType.AGGREGATE);
-    definitionDao.create(aggDef);
+    m_definitionDao.create(aggDef);
 
     // add current and history across four hosts
     for (int i = 0; i < 4; i++) {
@@ -347,21 +381,21 @@ public class AlertDataManagerTest {
       history.setAlertState(AlertState.OK);
       history.setAlertText("OK");
       history.setAlertTimestamp(Long.valueOf(1));
-      history.setClusterId(clusterId);
+      history.setClusterId(m_cluster.getClusterId());
       history.setComponentName(definition.getComponentName());
       history.setHostName("h" + (i+1));
       history.setServiceName(definition.getServiceName());
-      dao.create(history);
+      m_dao.create(history);
 
       AlertCurrentEntity current = new AlertCurrentEntity();
       current.setAlertHistory(history);
       current.setLatestText(history.getAlertText());
       current.setLatestTimestamp(Long.valueOf(1L));
       current.setOriginalTimestamp(Long.valueOf(1L));
-      dao.merge(current);
+      m_dao.merge(current);
     }
 
-    AlertEventPublisher publisher = 
injector.getInstance(AlertEventPublisher.class);
+    AlertEventPublisher publisher = 
m_injector.getInstance(AlertEventPublisher.class);
 
     // !!! need a synchronous op for testing
     field = AlertEventPublisher.class.getDeclaredField("m_eventBus");
@@ -377,17 +411,17 @@ public class AlertDataManagerTest {
       }
     });
 
-    AlertAggregateListener listener = 
injector.getInstance(AlertAggregateListener.class);
+    AlertAggregateListener listener = 
m_injector.getInstance(AlertAggregateListener.class);
     AlertDefinitionFactory factory = new AlertDefinitionFactory();
-    AggregateDefinitionMapping aggregateMapping = 
injector.getInstance(AggregateDefinitionMapping.class);
+    AggregateDefinitionMapping aggregateMapping = 
m_injector.getInstance(AggregateDefinitionMapping.class);
 
     AlertDefinition aggregateDefinition = factory.coerce(aggDef);
-    aggregateMapping.registerAggregate(clusterId.longValue(),
+    aggregateMapping.registerAggregate(m_cluster.getClusterId(),
         aggregateDefinition );
 
     AggregateSource as = (AggregateSource) aggregateDefinition.getSource();
     AlertDefinition aggregatedDefinition = 
aggregateMapping.getAggregateDefinition(
-        clusterId.longValue(), as.getAlertName());
+        m_cluster.getClusterId(), as.getAlertName());
 
     assertNotNull(aggregatedDefinition);
 
@@ -400,7 +434,8 @@ public class AlertDataManagerTest {
         definition.getComponentName(),
         "h1",
         AlertState.OK);
-    AlertReceivedEvent event = new AlertReceivedEvent(clusterId.longValue(), 
alert);
+    AlertReceivedEvent event = new AlertReceivedEvent(m_cluster.getClusterId(),
+        alert);
 
     listener.onAlertEvent(event);
     assertNotNull(ref.get());
@@ -408,10 +443,10 @@ public class AlertDataManagerTest {
     assertTrue(ref.get().getText().indexOf("0/4") > -1);
 
     // check if one is critical, still ok
-    AlertCurrentEntity current = dao.findCurrentByHostAndName(
-        clusterId.longValue(), "h1", definition.getDefinitionName());
+    AlertCurrentEntity current = m_dao.findCurrentByHostAndName(
+        m_cluster.getClusterId(), "h1", definition.getDefinitionName());
     current.getAlertHistory().setAlertState(AlertState.CRITICAL);
-    dao.merge(current.getAlertHistory());
+    m_dao.merge(current.getAlertHistory());
 
     listener.onAlertEvent(event);
     assertEquals("aggregate_test", ref.get().getName());
@@ -419,10 +454,11 @@ public class AlertDataManagerTest {
     assertTrue(ref.get().getText().indexOf("1/4") > -1);
 
     // two are either warning or critical, warning
-    current = dao.findCurrentByHostAndName(
-        clusterId.longValue(), "h2", definition.getDefinitionName());
+    current = m_dao.findCurrentByHostAndName(
+m_cluster.getClusterId(), "h2",
+        definition.getDefinitionName());
     current.getAlertHistory().setAlertState(AlertState.WARNING);
-    dao.merge(current.getAlertHistory());
+    m_dao.merge(current.getAlertHistory());
 
     listener.onAlertEvent(event);
     assertEquals("aggregate_test", ref.get().getName());
@@ -430,10 +466,11 @@ public class AlertDataManagerTest {
     assertTrue(ref.get().getText().indexOf("2/4") > -1);
 
     // three make it critical
-    current = dao.findCurrentByHostAndName(
-        clusterId.longValue(), "h3", definition.getDefinitionName());
+    current = m_dao.findCurrentByHostAndName(
+m_cluster.getClusterId(), "h3",
+        definition.getDefinitionName());
     current.getAlertHistory().setAlertState(AlertState.CRITICAL);
-    dao.merge(current.getAlertHistory());
+    m_dao.merge(current.getAlertHistory());
 
     listener.onAlertEvent(event);
     assertEquals("aggregate_test", ref.get().getName());

Reply via email to