@Test
  public void testServerActionWOService() throws Exception {
    Properties properties = new Properties();
    Configuration conf = new Configuration(properties);
    Clusters fsm = mock(Clusters.class);
    UnitOfWork unitOfWork = mock(UnitOfWork.class);
    AgentCommandsPublisher agentCommandsPublisher = mock(AgentCommandsPublisher.class);

    Map<String, String> payload = new HashMap<>();
    final Stage s = getStageWithServerAction(1, 977, payload, "test", 300, false, false);
    List<Stage> stages = Collections.singletonList(s);

    ActionDBAccessor db = mock(ActionDBAccessor.class);
    HostRoleCommandDAO hostRoleCommandDAOMock = mock(HostRoleCommandDAO.class);
    Mockito.doNothing().when(hostRoleCommandDAOMock).publishTaskCreateEvent(anyListOf(HostRoleCommand.class));

    RequestEntity request = mock(RequestEntity.class);
    when(request.getClusterHostInfo()).thenReturn(CLUSTER_HOST_INFO);
    when(request.isExclusive()).thenReturn(false);
    when(db.getRequestEntity(anyLong())).thenReturn(request);

    when(db.getCommandsInProgressCount()).thenReturn(stages.size());
    when(db.getFirstStageInProgressPerRequest()).thenReturn(stages);
    doAnswer(new Answer<Void>() {
      @Override
      public Void answer(InvocationOnMock invocation) throws Throwable {
        String host = (String) invocation.getArguments()[0];
        String role = (String) invocation.getArguments()[3];
        CommandReport commandReport = (CommandReport) invocation.getArguments()[4];

        HostRoleCommand command = null;
        if (null == host) {
          command = s.getHostRoleCommand(null, role);
        } else {
          command = s.getHostRoleCommand(host, role);
        }

        command.setStatus(HostRoleStatus.valueOf(commandReport.getStatus()));
        return null;
      }
    }).when(db).updateHostRoleState(nullable(String.class), anyLong(), anyLong(), anyString(), any(CommandReport.class));

    doAnswer(new Answer<List<HostRoleCommand>>() {
      @Override
      public List<HostRoleCommand> answer(InvocationOnMock invocation) throws Throwable {
        String role = (String) invocation.getArguments()[0];
        HostRoleStatus status = (HostRoleStatus) invocation.getArguments()[1];

        HostRoleCommand task = s.getHostRoleCommand(null, role);

        if (task.getStatus() == status) {
          return Arrays.asList(task);
        } else {
          return Collections.emptyList();
        }
      }
    }).when(db).getTasksByRoleAndStatus(anyString(), any(HostRoleStatus.class));

    doAnswer(new Answer<HostRoleCommand>() {
      @Override
      public HostRoleCommand answer(InvocationOnMock invocation) throws Throwable {
        return s.getHostRoleCommand(null, "AMBARI_SERVER_ACTION");
      }
    }).when(db).getTask(anyLong());

    ServerActionExecutor.init(injector);
    ActionScheduler scheduler = new ActionScheduler(100, 50, db, fsm, 3,
        new HostsMap((String) null), unitOfWork, null, conf, entityManagerProviderMock,
        hostRoleCommandDAOMock, (HostRoleCommandFactory)null, agentCommandsPublisher);

    int cycleCount = 0;
    while (!stages.get(0).getHostRoleStatus(null, "AMBARI_SERVER_ACTION")
        .equals(HostRoleStatus.COMPLETED) && cycleCount++ <= MAX_CYCLE_ITERATIONS) {
      scheduler.doWork();
      scheduler.getServerActionExecutor().doWork();
    }

    assertEquals(stages.get(0).getHostRoleStatus(null, "AMBARI_SERVER_ACTION"),
            HostRoleStatus.COMPLETED);
  }
Checks what happens when stage has an execution command for
host component that has been recently deleted
@throws Exception
ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionScheduler.java