public static WorkflowInstanceAggregatedInfo computeAggregatedView(
      WorkflowInstance workflowInstance, boolean statusKnown) {
    if (workflowInstance == null) {
      // returning empty object since cannot access state of the current instance run
      return new WorkflowInstanceAggregatedInfo();
    }

    WorkflowInstanceAggregatedInfo instanceAggregated =
        computeAggregatedViewNoStatus(workflowInstance);
    if (statusKnown || workflowInstance.getAggregatedInfo() == null) {
      instanceAggregated.setWorkflowInstanceStatus(workflowInstance.getStatus());
    } else {
      computeAndSetAggregatedInstanceStatus(workflowInstance, instanceAggregated);
    }
    return instanceAggregated;
  }
@Test
  public void testAggregatedViewFailed() {
    WorkflowInstance run1 =
        getGenericWorkflowInstance(
            1, WorkflowInstance.Status.FAILED, RunPolicy.START_FRESH_NEW_RUN, null);

    Workflow runtimeWorkflow = mock(Workflow.class);

    Map<String, StepRuntimeState> decodedOverview = new LinkedHashMap<>();
    decodedOverview.put("step1", generateStepState(StepInstance.Status.SUCCEEDED, 1L, 2L));
    decodedOverview.put("step2", generateStepState(StepInstance.Status.SUCCEEDED, 3L, 4L));
    decodedOverview.put("step3", generateStepState(StepInstance.Status.FATALLY_FAILED, 5L, 6L));

    WorkflowRuntimeOverview wro = mock(WorkflowRuntimeOverview.class);
    doReturn(decodedOverview).when(wro).decodeStepOverview(run1.getRuntimeDag());
    run1.setRuntimeOverview(wro);
    run1.setRuntimeWorkflow(runtimeWorkflow);

    WorkflowInstanceAggregatedInfo aggregated =
        AggregatedViewHelper.computeAggregatedView(run1, false);

    assertEquals(1L, aggregated.getStepAggregatedViews().get("step1").getStartTime().longValue());
    assertEquals(3L, aggregated.getStepAggregatedViews().get("step2").getStartTime().longValue());
    assertEquals(5L, aggregated.getStepAggregatedViews().get("step3").getStartTime().longValue());
    assertEquals(WorkflowInstance.Status.FAILED, aggregated.getWorkflowInstanceStatus());

    WorkflowInstance run2 =
        getGenericWorkflowInstance(
            2,
            WorkflowInstance.Status.SUCCEEDED,
            RunPolicy.RESTART_FROM_INCOMPLETE,
            RestartPolicy.RESTART_FROM_INCOMPLETE);

    Map<String, StepRuntimeState> decodedOverview2 = new LinkedHashMap<>();
    decodedOverview2.put("step3", generateStepState(StepInstance.Status.SUCCEEDED, 11L, 12L));

    Map<String, StepTransition> run2Dag = new LinkedHashMap<>();
    run2Dag.put("step3", new StepTransition());
    run2.setRuntimeDag(run2Dag);

    doReturn(run1)
        .when(workflowInstanceDao)
        .getWorkflowInstanceRun(run2.getWorkflowId(), run2.getWorkflowInstanceId(), 1L);
    run2.setAggregatedInfo(AggregatedViewHelper.computeAggregatedView(run1, false));

    assertEquals(3, run2.getAggregatedInfo().getStepAggregatedViews().size());
    assertEquals(
        1L,
        run2.getAggregatedInfo().getStepAggregatedViews().get("step1").getStartTime().longValue());
    assertEquals(
        3L,
        run2.getAggregatedInfo().getStepAggregatedViews().get("step2").getStartTime().longValue());
    assertEquals(
        5L,
        run2.getAggregatedInfo().getStepAggregatedViews().get("step3").getStartTime().longValue());

    WorkflowRuntimeOverview wro2 = mock(WorkflowRuntimeOverview.class);
    doReturn(decodedOverview2).when(wro2).decodeStepOverview(run2.getRuntimeDag());
    run2.setRuntimeOverview(wro2);
    run2.setRuntimeWorkflow(runtimeWorkflow);

    WorkflowInstanceAggregatedInfo aggregated2 =
        AggregatedViewHelper.computeAggregatedView(run2, false);

    assertEquals(3, aggregated2.getStepAggregatedViews().size());
    assertEquals(
        StepInstance.Status.SUCCEEDED,
        aggregated2.getStepAggregatedViews().get("step1").getStatus());
    assertEquals(
        StepInstance.Status.SUCCEEDED,
        aggregated2.getStepAggregatedViews().get("step2").getStatus());
    assertEquals(
        StepInstance.Status.SUCCEEDED,
        aggregated2.getStepAggregatedViews().get("step3").getStatus());
    assertEquals(1L, aggregated2.getStepAggregatedViews().get("step1").getStartTime().longValue());
    assertEquals(3L, aggregated2.getStepAggregatedViews().get("step2").getStartTime().longValue());
    assertEquals(11L, aggregated2.getStepAggregatedViews().get("step3").getStartTime().longValue());
    assertEquals(WorkflowInstance.Status.SUCCEEDED, aggregated2.getWorkflowInstanceStatus());
  }