public RunResponse restart(RunRequest runRequest, boolean blocking) {
    if (!runRequest.isFreshRun()
        && runRequest.getCurrentPolicy() != RunPolicy.RESTART_FROM_SPECIFIC) {
      updateRunRequestForRestartFromInlineRoot(runRequest);
    }

    RunResponse runResponse = actionHandler.restartRecursively(runRequest);
    if (runResponse.getStatus() == RunResponse.Status.DELEGATED) {
      return restartDirectly(runResponse, runRequest, blocking);
    }
    return runResponse;
  }
@Test
  public void testRestartFromInlineRootWithinForeach() {
    when(instanceDao.getWorkflowInstance(
            "sample-minimal-wf", 1, Constants.LATEST_INSTANCE_RUN, true))
        .thenReturn(instance);
    when(instance.getStatus()).thenReturn(WorkflowInstance.Status.FAILED);

    ForeachInitiator initiator = new ForeachInitiator();
    UpstreamInitiator.Info parent = new UpstreamInitiator.Info();
    parent.setWorkflowId("root-wf");
    parent.setInstanceId(123);
    parent.setRunId(1);
    parent.setStepId("foreach-step");
    parent.setStepAttemptId(1);
    initiator.setAncestors(Collections.singletonList(parent));
    when(instance.getInitiator()).thenReturn(initiator);

    WorkflowInstanceAggregatedInfo aggregatedInfo = mock(WorkflowInstanceAggregatedInfo.class);
    when(instance.getAggregatedInfo()).thenReturn(aggregatedInfo);
    StepAggregatedView aggregatedView = mock(StepAggregatedView.class);
    when(aggregatedInfo.getStepAggregatedViews()).thenReturn(singletonMap("job1", aggregatedView));
    when(aggregatedView.getStatus()).thenReturn(StepInstance.Status.FATALLY_FAILED);

    when(actionHandler.restartRecursively(any()))
        .thenReturn(RunResponse.builder().status(RunResponse.Status.WORKFLOW_RUN_CREATED).build());

    RunRequest runRequest =
        RunRequest.builder()
            .requester(user)
            .currentPolicy(RunPolicy.RESTART_FROM_INCOMPLETE)
            .restartConfig(
                RestartConfig.builder()
                    .addRestartNode("sample-minimal-wf", 1, "job1")
                    .restartPolicy(RunPolicy.RESTART_FROM_INCOMPLETE)
                    .stepRestartParams(
                        Collections.singletonMap(
                            "job1",
                            Collections.singletonMap(
                                "param1", ParamDefinition.buildParamDefinition("param1", "foo"))))
                    .build())
            .build();

    RunResponse response = stepActionHandler.restart(runRequest, true);

    ArgumentCaptor<RunRequest> requestCaptor = ArgumentCaptor.forClass(RunRequest.class);
    Mockito.verify(actionHandler, Mockito.times(1)).restartRecursively(requestCaptor.capture());
    RunRequest request = requestCaptor.getValue();

    assertEquals(runRequest, request);
    assertEquals(
        Collections.singletonList(new RestartConfig.RestartNode("root-wf", 123, null)),
        request.getRestartConfig().getRestartPath());
    assertEquals(RunResponse.Status.WORKFLOW_RUN_CREATED, response.getStatus());
  }