/**
   * Tests that an exception is thrown if you try to execute a "route" command on an already routed
   * document.
   */
  @Test
  public void testRouteAlreadyRoutedDocument() throws Exception {
    WorkflowDocument document =
        WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user1"), DOCUMENT_TYPE_NAME);
    document.route("");

    assertTrue("Document should be ENROUTE.", document.isEnroute());
    assertFalse("There should not be a request to ewestfal.", document.isApprovalRequested());

    // verify that only 1 action taken has been performed
    Collection<ActionTakenValue> actionTakens =
        KEWServiceLocator.getActionTakenService().findByDocumentId(document.getDocumentId());
    assertEquals("There should be only 1 action taken.", 1, actionTakens.size());

    // now try and route the document again, an exception should be thrown
    try {
      document.route("");
      fail("A WorkflowException should have been thrown.");
    } catch (InvalidActionTakenException e) {
      e.printStackTrace();
    }

    // verify that there is still only 1 action taken (the transaction above should have rolled
    // back)
    actionTakens =
        KEWServiceLocator.getActionTakenService().findByDocumentId(document.getDocumentId());
    assertEquals("There should still be only 1 action taken.", 1, actionTakens.size());
  }
  /**
   * Tests that an exception is not thrown if you try to execute a "route" command on a document you
   * did not initiate.
   */
  @Test
  public void testRouteDocumentAsNonInitiatorUser() throws Exception {
    WorkflowDocument firstDocument =
        WorkflowDocumentFactory.createDocument(
            getPrincipalIdForName("user1"), DOCUMENT_TYPE_POLICY_TEST_NAME);
    WorkflowDocument document =
        WorkflowDocumentFactory.loadDocument(
            getPrincipalIdForName("user2"), firstDocument.getDocumentId());
    try {
      document.route("");
    } catch (Exception e) {
      e.printStackTrace();
      fail(
          "Exception thrown but should not have have been... Exception was of type "
              + e.getClass().getName()
              + " and message was "
              + e.getMessage());
    }
    document =
        WorkflowDocumentFactory.loadDocument(
            getPrincipalIdForName("user1"), firstDocument.getDocumentId());
    assertEquals(
        "Document should be in Enroute status.", DocumentStatus.ENROUTE, document.getStatus());

    // verify that there is 1 action taken
    Collection<ActionTakenValue> actionTakens =
        KEWServiceLocator.getActionTakenService().findByDocumentId(document.getDocumentId());
    assertEquals("There should be 1 action taken.", 1, actionTakens.size());
  }
  /** This method converts everything except for the parent and child requests */
  private static void populateActionRequest(
      ActionRequestValue actionRequestBo,
      ActionRequest actionRequest,
      RouteNodeInstanceLoader routeNodeInstanceLoader) {

    actionRequestBo.setActionRequested(actionRequest.getActionRequested().getCode());
    if (!StringUtils.isBlank(actionRequest.getId())) {
      actionRequestBo.setActionRequestId(actionRequest.getId());
    }

    if (actionRequest.getActionTaken() != null) {
      // actionRequestBo.setActionTaken(ActionTakenValue.from(actionRequest.getActionTaken()));
      if (!StringUtils.isBlank(actionRequest.getActionTaken().getId())) {
        actionRequestBo.setActionTaken(
            KEWServiceLocator.getActionTakenService()
                .findByActionTakenId(actionRequest.getActionTaken().getId()));
      }
    }
    actionRequestBo.setAnnotation(actionRequest.getAnnotation());
    if (actionRequest.getRequestPolicy() != null) {
      actionRequestBo.setApprovePolicy(actionRequest.getRequestPolicy().getCode());
    }
    actionRequestBo.setCreateDate(new Timestamp(actionRequest.getDateCreated().getMillis()));
    actionRequestBo.setCurrentIndicator(actionRequest.isCurrent());
    if (actionRequest.getDelegationType() != null) {
      actionRequestBo.setDelegationType(actionRequest.getDelegationType());
    }
    // actionRequestBo.setDocVersion(actionRequest.?);
    actionRequestBo.setForceAction(actionRequest.isForceAction());
    actionRequestBo.setPriority(actionRequest.getPriority());
    actionRequestBo.setRouteLevel(actionRequest.getRouteLevel());
    actionRequestBo.setQualifiedRoleName(actionRequest.getQualifiedRoleName());
    actionRequestBo.setQualifiedRoleNameLabel(actionRequest.getQualifiedRoleNameLabel());
    actionRequestBo.setRecipientTypeCd(actionRequest.getRecipientType().getCode());
    actionRequestBo.setResponsibilityDesc(actionRequest.getResponsibilityDescription());
    if (!StringUtils.isBlank(actionRequest.getResponsibilityId())) {
      actionRequestBo.setResponsibilityId(actionRequest.getResponsibilityId());
    }
    actionRequestBo.setRoleName(actionRequest.getRoleName());
    String documentId = actionRequest.getDocumentId();
    if (documentId != null) {
      actionRequestBo.setDocumentId(documentId);
      actionRequestBo.setRouteHeader(
          KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentId));
    }

    actionRequestBo.setStatus(actionRequest.getStatus().getCode());
    actionRequestBo.setPrincipalId(actionRequest.getPrincipalId());
    actionRequestBo.setGroupId(actionRequest.getGroupId());

    if (routeNodeInstanceLoader != null
        && !StringUtils.isBlank(actionRequest.getRouteNodeInstanceId())) {
      actionRequestBo.setNodeInstance(
          routeNodeInstanceLoader.load(actionRequest.getRouteNodeInstanceId()));
    }
  }