/**
   * @param prereqAction the prerequisite for this action
   * @return Returns the rebootAction (if any)
   */
  public Action scheduleRebootAction(Action prereqAction) {

    // All actions must be scheduled against the host server.

    Action rebootAction =
        ActionManager.scheduleRebootAction(
            this.getUser(), this.getHostServer(), this.getScheduleDate());
    log.debug("** Created rebootAction");
    rebootAction.setPrerequisite(prereqAction);
    rebootAction.setEarliestAction(this.getScheduleDate());
    rebootAction.setOrg(this.getUser().getOrg());
    rebootAction.setName(rebootAction.getActionType().getName());
    log.debug("** saving reboot action: " + rebootAction.getName());
    ActionFactory.save(rebootAction);
    log.debug("** Saved rebootAction: " + rebootAction.getId());

    return rebootAction;
  }
  /**
   * Tests save().
   *
   * @throws Exception if something bad happens
   */
  @SuppressWarnings("unchecked")
  public void testSave() throws Exception {
    RhnMockHttpServletRequest request = TestUtils.getRequestWithSessionAndUser();
    user = new RequestContext(request).getCurrentUser();

    ActionChainSaveAction saveAction = new ActionChainSaveAction();
    String label = TestUtils.randomString();
    ActionChain actionChain = ActionChainFactory.createActionChain(label, user);
    for (int i = 0; i < 6; i++) {
      Action action = ActionFactory.createAction(ActionFactory.TYPE_ERRATA);
      action.setOrg(user.getOrg());
      ActionFactory.save(action);
      ActionChainFactory.queueActionChainEntry(
          action, actionChain, ServerFactoryTest.createTestServer(user), i / 2);
    }
    Action lastAction = ActionFactory.createAction(ActionFactory.TYPE_ERRATA);
    lastAction.setOrg(user.getOrg());
    ActionFactory.save(lastAction);
    ActionChainFactory.queueActionChainEntry(
        lastAction, actionChain, ServerFactoryTest.createTestServer(user), 3);

    String newLabel = TestUtils.randomString();
    List<Long> deletedEntries = new LinkedList<Long>();
    deletedEntries.add(lastAction.getId());
    List<Integer> deletedSortOrders = new LinkedList<Integer>();
    deletedSortOrders.add(0);
    deletedSortOrders.add(3);
    List<Integer> reorderedSortOrders = new LinkedList<Integer>();
    reorderedSortOrders.add(2);
    reorderedSortOrders.add(1);

    String resultString =
        saveAction.save(
            actionChain.getId(),
            newLabel,
            deletedEntries,
            deletedSortOrders,
            reorderedSortOrders,
            request);

    Map<String, Object> result = (Map<String, Object>) new JSONReader().read(resultString);
    assertEquals(true, result.get(ActionChainSaveAction.SUCCESS_FIELD));
    assertEquals(
        LocalizationService.getInstance().getMessage("actionchain.jsp.saved"),
        result.get(ActionChainSaveAction.TEXT_FIELD));
    assertEquals(newLabel, actionChain.getLabel());

    Set<ActionChainEntry> entries = actionChain.getEntries();
    assertEquals(4, entries.size());

    List<ActionChainEntry> sortedEntries = new LinkedList<ActionChainEntry>();
    sortedEntries.addAll(entries);
    Collections.sort(
        sortedEntries,
        new Comparator<ActionChainEntry>() {
          public int compare(ActionChainEntry entry1, ActionChainEntry entry2) {
            return entry1.getId().compareTo(entry2.getId());
          }
        });
    for (int i = 0; i < sortedEntries.size(); i++) {
      assertEquals((Integer) (1 - i / 2), sortedEntries.get(i).getSortOrder());
    }
  }