public static WorkflowInstance.Status deriveAggregatedStatus(
      MaestroWorkflowInstanceDao instanceDao,
      WorkflowSummary summary,
      WorkflowInstance.Status runStatus,
      WorkflowRuntimeOverview overviewToUpdate) {
    if (!summary.isFreshRun() && runStatus == WorkflowInstance.Status.SUCCEEDED) {
      WorkflowInstance instance =
          instanceDao.getWorkflowInstanceRun(
              summary.getWorkflowId(), summary.getWorkflowInstanceId(), summary.getWorkflowRunId());
      instance.setRuntimeOverview(overviewToUpdate);
      instance.setStatus(runStatus);
      WorkflowInstance.Status aggStatus =
          AggregatedViewHelper.computeAggregatedView(instance, false).getWorkflowInstanceStatus();
      if (aggStatus != runStatus) {
        overviewToUpdate.setRunStatus(runStatus);
        return aggStatus;
      }
    }
    return runStatus;
  }
@Test
  public void testDeriveAggregatedStatus() {
    WorkflowInstance instance =
        getGenericWorkflowInstance(
            2,
            WorkflowInstance.Status.SUCCEEDED,
            RunPolicy.RESTART_FROM_SPECIFIC,
            RestartPolicy.RESTART_FROM_BEGINNING);
    instance.getRuntimeDag().remove("step1");

    MaestroWorkflowInstanceDao instanceDao = mock(MaestroWorkflowInstanceDao.class);
    when(instanceDao.getWorkflowInstanceRun(any(), anyLong(), anyLong())).thenReturn(instance);

    WorkflowSummary summary = mock(WorkflowSummary.class);
    when(summary.isFreshRun()).thenReturn(false);
    Workflow runtimeWorkflow = mock(Workflow.class);
    Step step1 = mock(Step.class);
    when(step1.getId()).thenReturn("step1");
    Step step2 = mock(Step.class);
    when(step2.getId()).thenReturn("step2");
    Step step3 = mock(Step.class);
    when(step3.getId()).thenReturn("step3");
    when(runtimeWorkflow.getSteps()).thenReturn(Arrays.asList(step1, step2, step3));
    instance.setRuntimeWorkflow(runtimeWorkflow);
    WorkflowInstanceAggregatedInfo baseline = new WorkflowInstanceAggregatedInfo();
    baseline.setWorkflowInstanceStatus(WorkflowInstance.Status.FAILED);
    baseline
        .getStepAggregatedViews()
        .put("step1", generateStepAggregated(StepInstance.Status.SUCCEEDED, 11L, 12L));
    baseline
        .getStepAggregatedViews()
        .put("step2", generateStepAggregated(StepInstance.Status.FATALLY_FAILED, 11L, 12L));
    baseline
        .getStepAggregatedViews()
        .put("step3", generateStepAggregated(StepInstance.Status.STOPPED, 11L, 12L));
    instance.setAggregatedInfo(baseline);

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

    WorkflowRuntimeOverview overview = mock(WorkflowRuntimeOverview.class);
    doReturn(decodedOverview).when(overview).decodeStepOverview(instance.getRuntimeDag());

    WorkflowInstance.Status actual =
        AggregatedViewHelper.deriveAggregatedStatus(
            instanceDao, summary, WorkflowInstance.Status.FAILED, overview);
    assertEquals(WorkflowInstance.Status.FAILED, actual);
    verify(overview, times(0)).setRunStatus(any());

    actual =
        AggregatedViewHelper.deriveAggregatedStatus(
            instanceDao, summary, WorkflowInstance.Status.SUCCEEDED, overview);
    assertEquals(WorkflowInstance.Status.FAILED, actual);
    verify(overview, times(1)).setRunStatus(WorkflowInstance.Status.SUCCEEDED);
  }