@Test
public void testStateCommandsWithAlertsGeneration() throws AmbariException, InterruptedException,
        InvalidStateTransitionException {

  StackId stackId = new StackId("HDP-2.0.7");
  Clusters clusters = injector.getInstance(Clusters.class);
  clusters.addHost(hostname1);
  setOsFamily(clusters.getHost(hostname1), "redhat", "6.3");
  clusters.addHost(hostname2);
  setOsFamily(clusters.getHost(hostname2), "redhat", "6.3");
  clusters.addCluster(clusterName, stackId);
  Cluster cluster = clusters.getCluster(clusterName);
  cluster.setDesiredStackVersion(stackId);
  helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion());
  cluster.createClusterVersion(stackId, stackId.getStackVersion(), "admin",
      RepositoryVersionState.INSTALLING);
  Set<String> hostNames = new HashSet<String>(){{
    add(hostname1);
    add(hostname2);
  }};
  clusters.mapAndPublishHostsToCluster(hostNames, clusterName);
  Service hdfs = cluster.addService(serviceName);
  hdfs.addServiceComponent(Role.DATANODE.name());
  hdfs.getServiceComponent(Role.DATANODE.name()).addServiceComponentHost(hostname1);
  hdfs.addServiceComponent(Role.NAMENODE.name());
  hdfs.getServiceComponent(Role.NAMENODE.name()).addServiceComponentHost(hostname1);
  hdfs.addServiceComponent(Role.SECONDARY_NAMENODE.name());
  hdfs.getServiceComponent(Role.SECONDARY_NAMENODE.name()).addServiceComponentHost(hostname1);
  hdfs.getServiceComponent(Role.DATANODE.name()).getServiceComponentHost(hostname1).setState(State.INSTALLED);
  hdfs.getServiceComponent(Role.NAMENODE.name()).getServiceComponentHost(hostname1).setState(State.INSTALLED);
  hdfs.getServiceComponent(Role.SECONDARY_NAMENODE.name()).getServiceComponentHost(hostname1).setState(State.INSTALLED);
  ActionQueue aq = new ActionQueue();
  ActionManager am = mock(ActionManager.class);
  HeartbeatMonitor hm = new HeartbeatMonitor(clusters, aq, am,
    heartbeatMonitorWakeupIntervalMS, injector);
  HeartBeatHandler handler = new HeartBeatHandler(clusters, aq, am, injector);
  Register reg = new Register();
  reg.setHostname(hostname1);
  reg.setResponseId(12);
  reg.setTimestamp(System.currentTimeMillis() - 300);
  reg.setAgentVersion(ambariMetaInfo.getServerVersion());
  HostInfo hi = new HostInfo();
  hi.setOS("Centos5");
  reg.setHardwareProfile(hi);
  handler.handleRegistration(reg);
  HeartBeat hb = new HeartBeat();
  hb.setHostname(hostname1);
  hb.setNodeStatus(new HostStatus(HostStatus.Status.HEALTHY, "cool"));
  hb.setTimestamp(System.currentTimeMillis());
  hb.setResponseId(12);
  handler.handleHeartBeat(hb);
  List<StatusCommand> cmds = hm.generateStatusCommands(hostname1);
  assertEquals("HeartbeatMonitor should generate StatusCommands for host1",
      3, cmds.size());
  assertEquals("HDFS", cmds.get(0).getServiceName());
  cmds = hm.generateStatusCommands(hostname2);
  assertTrue("HeartbeatMonitor should not generate StatusCommands for host2 because it has no services", cmds.isEmpty());
}