public Optional<Details> restartForeachInstance(
      RunRequest request, WorkflowInstance instance, String foreachStepId, long restartRunId) {
    request.updateForDownstreamIfNeeded(foreachStepId, instance);
    workflowHelper.updateWorkflowInstance(instance, request);
    instance.setWorkflowRunId(restartRunId);
    return instanceDao.runWorkflowInstances(
        instance.getWorkflowId(), Collections.singletonList(instance), 1);
  }
@Test
  public void testRestartForeachInstance() {
    doNothing().when(workflowHelper).updateWorkflowInstance(any(), any());
    instance.setWorkflowId("maestro_foreach_x");
    RestartConfig restartConfig =
        RestartConfig.builder()
            .restartPolicy(RunPolicy.RESTART_FROM_INCOMPLETE)
            .downstreamPolicy(RunPolicy.RESTART_FROM_BEGINNING)
            .addRestartNode("maestro_foreach_x", 1, null)
            .addRestartNode("sample-minimal-wf", 1, "foreach-step")
            .build();

    ForeachInitiator initiator = new ForeachInitiator();
    UpstreamInitiator.Info parent = new UpstreamInitiator.Info();
    parent.setWorkflowId("maestro_foreach_x");
    parent.setInstanceId(1);
    parent.setRunId(1);
    parent.setStepId("foreach-step");
    parent.setStepAttemptId(1);
    initiator.setAncestors(Collections.singletonList(parent));

    RunRequest runRequest =
        RunRequest.builder()
            .initiator(initiator)
            .currentPolicy(RunPolicy.RESTART_FROM_INCOMPLETE)
            .restartConfig(restartConfig)
            .build();

    assertFalse(
        actionHandler
            .restartForeachInstance(runRequest, instance, "foreach-step", 100)
            .isPresent());
    verify(instanceDao, times(1)).runWorkflowInstances(any(), any(), anyInt());
    assertEquals(100, instance.getWorkflowRunId());

    ArgumentCaptor<RunRequest> captor = ArgumentCaptor.forClass(RunRequest.class);
    verify(workflowHelper, times(1)).updateWorkflowInstance(any(), captor.capture());

    RunRequest res = captor.getValue();
    assertEquals(RunPolicy.RESTART_FROM_INCOMPLETE, res.getCurrentPolicy());
    assertEquals(
        RestartConfig.builder()
            .restartPolicy(RunPolicy.RESTART_FROM_INCOMPLETE)
            .downstreamPolicy(RunPolicy.RESTART_FROM_BEGINNING)
            .addRestartNode("maestro_foreach_x", 1, null)
            .build(),
        res.getRestartConfig());
  }