String getWorkflowOwner() {
    if (runParams != null && runParams.containsKey(Constants.WORKFLOW_OWNER_PARAM)) {
      // should be system initiated run
      if (initiator.getType().isRestartable()) {
        throw new MaestroBadRequestException(
            Collections.emptyList(),
            "Should not have OWNER set for non system initiated workflow id=[%s]",
            workflowId);
      }
      // owner info is a literal string
      ParamDefinition ownerParam = runParams.get(Constants.WORKFLOW_OWNER_PARAM);
      Checks.checkTrue(
          ownerParam.isLiteral(),
          "System initiator run should set owner as literal instead of using expression: [%s]",
          ownerParam.getExpression());
      return ownerParam.getValue();
    } else {
      return runProperties.getOwner().getName();
    }
  }
@Test
  public void testGetWorkflowOwner() throws Exception {
    WorkflowInstance instance =
        loadObject(
            "fixtures/instances/sample-workflow-instance-created.json", WorkflowInstance.class);
    RunRequest runRequest =
        RunRequest.builder()
            .currentPolicy(RunPolicy.START_FRESH_NEW_RUN)
            .requester(User.create("tester"))
            .runParams(
                Collections.singletonMap(
                    "owner", ParamDefinition.buildParamDefinition("owner", "tester2")))
            .build();

    InstanceWrapper instanceWrapper = InstanceWrapper.from(instance, runRequest);
    AssertHelper.assertThrows(
        "Should not have OWNER set for non system initiated workflow",
        MaestroBadRequestException.class,
        "Should not have OWNER set for non system initiated workflow",
        instanceWrapper::getWorkflowOwner);

    runRequest =
        RunRequest.builder()
            .currentPolicy(RunPolicy.START_FRESH_NEW_RUN)
            .initiator(UpstreamInitiator.withType(Initiator.Type.FOREACH))
            .runParams(
                Collections.singletonMap(
                    "owner", StringParamDefinition.builder().expression("tester2").build()))
            .build();
    instanceWrapper = InstanceWrapper.from(instance, runRequest);
    AssertHelper.assertThrows(
        "System initiator run should set owner as literal instead of using expression",
        IllegalArgumentException.class,
        "System initiator run should set owner as literal instead of using expression",
        instanceWrapper::getWorkflowOwner);

    runRequest =
        RunRequest.builder()
            .currentPolicy(RunPolicy.START_FRESH_NEW_RUN)
            .initiator(UpstreamInitiator.withType(Initiator.Type.FOREACH))
            .runParams(
                Collections.singletonMap(
                    "owner", ParamDefinition.buildParamDefinition("owner", "tester2")))
            .build();
    instanceWrapper = InstanceWrapper.from(instance, runRequest);
    assertEquals("tester2", instanceWrapper.getWorkflowOwner());
  }