hdfs.getServiceComponent(DATANODE).addServiceComponentHost(DummyHostname1).persist();
ActionQueue aq = new ActionQueue();
HeartBeatHandler handler = getHeartBeatHandler(am, aq);
ServiceComponentHost serviceComponentHost1 = clusters.getCluster(DummyCluster).getService(HDFS).
getServiceComponent(DATANODE).getServiceComponentHost(DummyHostname1);
serviceComponentHost1.setState(State.INSTALLED);
HeartBeat hb = new HeartBeat();
hb.setTimestamp(System.currentTimeMillis());
hb.setResponseId(0);
hb.setHostname(DummyHostname1);
hb.setNodeStatus(new HostStatus(Status.HEALTHY, DummyHostStatus));
List<CommandReport> reports = new ArrayList<CommandReport>();
CommandReport cr = new CommandReport();
cr.setActionId(StageUtils.getActionId(requestId, stageId));
cr.setTaskId(1);
cr.setClusterName(DummyCluster);
cr.setServiceName(HDFS);
cr.setRole(DATANODE);
cr.setStatus(HostRoleStatus.IN_PROGRESS.toString());
cr.setStdErr("none");
cr.setStdOut("dummy output");
cr.setExitCode(777);
cr.setRoleCommand("START");
reports.add(cr);
hb.setReports(reports);
hb.setComponentStatus(new ArrayList<ComponentStatus>());
handler.handleHeartBeat(hb);
assertEquals("Host state should be " + State.INSTALLED,
State.INSTALLED, serviceComponentHost1.getState());
hb.setTimestamp(System.currentTimeMillis());
hb.setResponseId(1);
cr.setStatus(HostRoleStatus.COMPLETED.toString());
cr.setExitCode(0);
handler.handleHeartBeat(hb);
assertEquals("Host state should be " + State.STARTED,
State.STARTED, serviceComponentHost1.getState());
hb.setTimestamp(System.currentTimeMillis());
hb.setResponseId(2);
cr.setStatus(HostRoleStatus.IN_PROGRESS.toString());
cr.setRoleCommand("STOP");
cr.setExitCode(777);
handler.handleHeartBeat(hb);
assertEquals("Host state should be " + State.STARTED,
State.STARTED, serviceComponentHost1.getState());
hb.setTimestamp(System.currentTimeMillis());
hb.setResponseId(3);
cr.setStatus(HostRoleStatus.COMPLETED.toString());
cr.setExitCode(0);
handler.handleHeartBeat(hb);
assertEquals("Host state should be " + State.INSTALLED,
State.INSTALLED, serviceComponentHost1.getState());
// validate the transitions when there is no heartbeat
serviceComponentHost1.setState(State.STARTING);
cr.setStatus(HostRoleStatus.IN_PROGRESS.toString());
cr.setExitCode(777);
cr.setRoleCommand("START");
hb.setResponseId(4);
handler.handleHeartBeat(hb);
assertEquals("Host state should be " + State.STARTING,
State.STARTING, serviceComponentHost1.getState());
cr.setStatus(HostRoleStatus.COMPLETED.toString());
cr.setExitCode(0);
hb.setResponseId(5);
handler.handleHeartBeat(hb);
assertEquals("Host state should be " + State.STARTED,
State.STARTED, serviceComponentHost1.getState());
serviceComponentHost1.setState(State.STOPPING);
cr.setStatus(HostRoleStatus.IN_PROGRESS.toString());
cr.setExitCode(777);
cr.setRoleCommand("STOP");
hb.setResponseId(6);
handler.handleHeartBeat(hb);
assertEquals("Host state should be " + State.STOPPING,
State.STOPPING, serviceComponentHost1.getState());
cr.setStatus(HostRoleStatus.COMPLETED.toString());
cr.setExitCode(0);
hb.setResponseId(7);
handler.handleHeartBeat(hb);
assertEquals("Host state should be " + State.INSTALLED,
State.INSTALLED, serviceComponentHost1.getState());
}