public RunResponse restart(RunRequest runRequest) {
    RunResponse runResponse = restartRecursively(runRequest);
    if (runResponse.getStatus() == RunResponse.Status.NON_TERMINAL_ERROR) {
      LOG.error(
          "workflow instance {} does not support restart action as it is in a non-terminal status [{}]",
          runRequest.getWorkflowIdentity(),
          runResponse.getTimelineEvent().getMessage());
      throw new MaestroBadRequestException(
          Collections.emptyList(),
          "workflow instance %s does not support restart action as it is in a non-terminal status [%s]",
          runRequest.getWorkflowIdentity(),
          runResponse.getTimelineEvent().getMessage());
    }
    return runResponse;
  }
@Test
  public void testInvalidRestart() {
    when(runStrategyDao.startWithRunStrategy(any(), any())).thenReturn(1);

    WorkflowInstance wfInstance = new WorkflowInstance();
    wfInstance.setInitiator(new ManualInitiator());
    wfInstance.setStatus(WorkflowInstance.Status.IN_PROGRESS);
    wfInstance.setWorkflowInstanceId(10L);
    wfInstance.setWorkflowRunId(1L);
    wfInstance.setWorkflowId("test-workflow");
    RunRequest request =
        RunRequest.builder()
            .initiator(new ManualInitiator())
            .currentPolicy(RunPolicy.RESTART_FROM_BEGINNING)
            .restartConfig(
                RestartConfig.builder().addRestartNode("test-workflow", 1L, null).build())
            .build();
    when(instanceDao.getWorkflowInstance("test-workflow", 1L, Constants.LATEST_INSTANCE_RUN, true))
        .thenReturn(wfInstance);

    AssertHelper.assertThrows(
        "workflow instance does not support restart action",
        MaestroBadRequestException.class,
        "workflow instance [test-workflow][1] does not support restart action as it is in a non-terminal status [IN_PROGRESS]",
        () -> actionHandler.restart(request));

    wfInstance.setInitiator(new ManualInitiator());
    RunRequest request2 =
        RunRequest.builder()
            .initiator(new ManualInitiator())
            .currentPolicy(RunPolicy.START_FRESH_NEW_RUN)
            .restartConfig(
                RestartConfig.builder().addRestartNode("test-workflow", 1L, null).build())
            .build();
    AssertHelper.assertThrows(
        "Cannot restart a workflow instance for new instance",
        MaestroBadRequestException.class,
        "workflow instance [test-workflow][1] does not support restart action as it is in a non-terminal status [IN_PROGRESS]",
        () -> actionHandler.restart(request2));

    RunRequest request3 =
        RunRequest.builder()
            .initiator(new ManualInitiator())
            .currentPolicy(RunPolicy.RESTART_FROM_BEGINNING)
            .stepRunParams(Collections.singletonMap("job1", Collections.emptyMap()))
            .restartConfig(
                RestartConfig.builder().addRestartNode("test-workflow", 1L, null).build())
            .build();
    wfInstance.setStatus(WorkflowInstance.Status.FAILED);
    AssertHelper.assertThrows(
        "Cannot restart a workflow instance for new instance",
        IllegalArgumentException.class,
        "Cannot restart a workflow instance [test-workflow][10][1] as it does not match run request [test-workflow][1]",
        () -> actionHandler.restart(request3));
  }