public void validateIdentity(WorkflowInstance toRestart) {
    // instance id will not always match for foreach
    if (initiator.getType() == Initiator.Type.FOREACH) {
      Checks.checkTrue(
          getRestartWorkflowId().equals(toRestart.getWorkflowId()),
          "Cannot restart a FOREACH iteration [%s] as it does not match run request [%s]",
          toRestart.getWorkflowId(),
          getRestartWorkflowId());
    } else {
      Checks.checkTrue(
          getRestartWorkflowId().equals(toRestart.getWorkflowId())
              && getRestartInstanceId() == toRestart.getWorkflowInstanceId(),
          "Cannot restart a workflow instance %s as it does not match run request %s",
          toRestart.getIdentity(),
          getWorkflowIdentity());
    }
  }
@Test
  public void testValidateIdentityForForeachRun() {
    RestartConfig config = RestartConfig.builder().addRestartNode("foo", 1, "bar").build();
    RunRequest runRequest =
        RunRequest.builder()
            .currentPolicy(RunPolicy.RESTART_FROM_INCOMPLETE)
            .restartConfig(config)
            .initiator(UpstreamInitiator.withType(Initiator.Type.FOREACH))
            .build();
    WorkflowInstance instance = new WorkflowInstance();
    instance.setWorkflowId("foo");
    instance.setWorkflowInstanceId(2);
    runRequest.validateIdentity(instance);

    instance.setWorkflowId("foo1");
    AssertHelper.assertThrows(
        "Mismatch instance identity",
        IllegalArgumentException.class,
        "Cannot restart a FOREACH iteration ",
        () -> runRequest.validateIdentity(instance));
  }