@Test
  public void testGetTasksByRequestId() throws Exception {
    ActionManager am = injector.getInstance(ActionManager.class);

    final long requestId1 = am.getNextRequestId();
    final long requestId2 = am.getNextRequestId();
    final long requestId3 = am.getNextRequestId();
    final String cluster1 = getUniqueName();
    final String hostName1 = getUniqueName();
    final String context = "Test invocation";

    StackId stackID = new StackId("HDP-0.1");
    clusters.addCluster(cluster1, stackID);
    Cluster c = clusters.getCluster(cluster1);
    Long clusterId = c.getClusterId();

    helper.getOrCreateRepositoryVersion(stackID, stackID.getStackVersion());
    clusters.addHost(hostName1);
    setOsFamily(clusters.getHost(hostName1), "redhat", "5.9");

    clusters.mapAndPublishHostsToCluster(new HashSet<String>(){
      {add(hostName1);}}, cluster1);


    List<Stage> stages = new ArrayList<>();
    stages.add(stageFactory.createNew(requestId1, "/a1", cluster1, clusterId, context,
        "", ""));
    stages.get(0).setStageId(1);
    stages.get(0).addHostRoleExecutionCommand(hostName1, Role.HBASE_MASTER,
            RoleCommand.START,
            new ServiceComponentHostStartEvent(Role.HBASE_MASTER.toString(),
                    hostName1, System.currentTimeMillis()),
            cluster1, "HBASE", false, false);

    stages.add(stageFactory.createNew(requestId1, "/a2", cluster1, clusterId, context,
      "", ""));
    stages.get(1).setStageId(2);
    stages.get(1).addHostRoleExecutionCommand(hostName1, Role.HBASE_CLIENT,
            RoleCommand.START,
            new ServiceComponentHostStartEvent(Role.HBASE_CLIENT.toString(),
                    hostName1, System.currentTimeMillis()), cluster1, "HBASE", false, false);

    stages.add(stageFactory.createNew(requestId1, "/a3", cluster1, clusterId, context,
      "", ""));
    stages.get(2).setStageId(3);
    stages.get(2).addHostRoleExecutionCommand(hostName1, Role.HBASE_CLIENT,
            RoleCommand.START,
            new ServiceComponentHostStartEvent(Role.HBASE_CLIENT.toString(),
                    hostName1, System.currentTimeMillis()), cluster1, "HBASE", false, false);

    Request request = new Request(stages, "", clusters);
    actionDB.persistActions(request);

    stages.clear();
    stages.add(stageFactory.createNew(requestId2, "/a4", cluster1, clusterId, context,
      "", ""));
    stages.get(0).setStageId(4);
    stages.get(0).addHostRoleExecutionCommand(hostName1, Role.HBASE_CLIENT,
            RoleCommand.START,
            new ServiceComponentHostStartEvent(Role.HBASE_CLIENT.toString(),
                    hostName1, System.currentTimeMillis()), cluster1, "HBASE", false, false);

    stages.add(stageFactory.createNew(requestId2, "/a5", cluster1, clusterId, context,
      "", ""));
    stages.get(1).setStageId(5);
    stages.get(1).addHostRoleExecutionCommand(hostName1, Role.HBASE_CLIENT,
            RoleCommand.START,
            new ServiceComponentHostStartEvent(Role.HBASE_CLIENT.toString(),
                    hostName1, System.currentTimeMillis()), cluster1, "HBASE", false, false);

    request = new Request(stages, "", clusters);
    actionDB.persistActions(request);

    // Add a stage to execute a task as server-side action on the Ambari server
    ServiceComponentHostServerActionEvent serviceComponentHostServerActionEvent =
        new ServiceComponentHostServerActionEvent(Role.AMBARI_SERVER_ACTION.toString(), null, System.currentTimeMillis());
    stages.clear();
    stages.add(stageFactory.createNew(requestId3, "/a6", cluster1, clusterId, context,
      "", ""));
    stages.get(0).setStageId(6);
    stages.get(0).addServerActionCommand("some.action.class.name", null, Role.AMBARI_SERVER_ACTION,
        RoleCommand.EXECUTE, cluster1, serviceComponentHostServerActionEvent, null, null, null, null, false, false);
    assertEquals("_internal_ambari", stages.get(0).getOrderedHostRoleCommands().get(0).getHostName());
    request = new Request(stages, "", clusters);
    actionDB.persistActions(request);

    org.apache.ambari.server.controller.spi.Request spiRequest = PropertyHelper.getReadRequest(
        TaskResourceProvider.TASK_CLUSTER_NAME_PROPERTY_ID,
        TaskResourceProvider.TASK_REQUEST_ID_PROPERTY_ID,
        TaskResourceProvider.TASK_STAGE_ID_PROPERTY_ID);

    // request ID 1 has 3 tasks
    Predicate predicate = new PredicateBuilder().property(
        TaskResourceProvider.TASK_REQUEST_ID_PROPERTY_ID).equals(requestId1).toPredicate();

    List<HostRoleCommandEntity> entities = hostRoleCommandDAO.findAll(spiRequest, predicate);
    Assert.assertEquals(3, entities.size());

    Long taskId = entities.get(0).getTaskId();

    // request just a task by ID
    predicate = new PredicateBuilder().property(
        TaskResourceProvider.TASK_REQUEST_ID_PROPERTY_ID).equals(requestId1).and().property(
            TaskResourceProvider.TASK_ID_PROPERTY_ID).equals(taskId).toPredicate();

    entities = hostRoleCommandDAO.findAll(spiRequest, predicate);
    Assert.assertEquals(1, entities.size());

    // request ID 2 has 2 tasks
    predicate = new PredicateBuilder().property(
        TaskResourceProvider.TASK_REQUEST_ID_PROPERTY_ID).equals(requestId2).toPredicate();

    entities = hostRoleCommandDAO.findAll(spiRequest, predicate);
    Assert.assertEquals(2, entities.size());

    // a single task from request 1 and all tasks from request 2 will total 3
    predicate = new PredicateBuilder().property(
        TaskResourceProvider.TASK_REQUEST_ID_PROPERTY_ID).equals(requestId1).and().property(
            TaskResourceProvider.TASK_ID_PROPERTY_ID).equals(taskId).or().property(
                TaskResourceProvider.TASK_REQUEST_ID_PROPERTY_ID).equals(requestId2).toPredicate();

    entities = hostRoleCommandDAO.findAll(spiRequest, predicate);
    Assert.assertEquals(3, entities.size());
  }
Create a cluster with a service, and verify that the request tasks have the correct output log and error log paths.
ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java