@Test
  public void testRemoveSubTasks() throws Exception {
    setUpIssue();
    final AtomicInteger deleteIssueCalled = new AtomicInteger(0);
    DefaultIssueDeleteHelper tested = newMockDeleteIssueHelper(deleteIssueCalled);

    final MockGenericValue subTask1 =
        createIssueGV(2L, PROJECT_ID, "sub task 1", "TST-10", null, null);
    final MutableIssue subTaskIssue1 = createNiceMock(MutableIssue.class);
    expect(subTaskIssue1.getId()).andReturn(2L).anyTimes();
    expect(subTaskIssue1.getGenericValue()).andReturn(subTask1).anyTimes();
    final MockGenericValue subTask2 =
        createIssueGV(3L, PROJECT_ID, "sub task 2", "TST-11", null, null);
    final MutableIssue subTaskIssue2 = createNiceMock(MutableIssue.class);
    expect(subTaskIssue2.getId()).andReturn(3L).anyTimes();
    expect(subTaskIssue2.getGenericValue()).andReturn(subTask2).anyTimes();

    replay(subTaskIssue1, subTaskIssue2);
    expect(mockIssueManager.getIssueObject(2L)).andReturn(subTaskIssue1);
    expect(mockIssueManager.getIssueObject(3L)).andReturn(subTaskIssue2);

    final MockGenericValue mockLinkGV1 =
        new DbIndependentMockGenericValue(
            "IssueLink", ImmutableMap.<String, Object>of("destination", 2L));
    final IssueLink issueLink1 = new IssueLink(mockLinkGV1, null, mockIssueManager);
    final MockGenericValue mockLinkGV2 =
        new DbIndependentMockGenericValue(
            "IssueLink", ImmutableMap.<String, Object>of("destination", 3L));
    final IssueLink issueLink2 = new IssueLink(mockLinkGV2, null, mockIssueManager);
    expect(mockSubTaskManager.getSubTaskIssueLinks(ISSUE_ID))
        .andReturn(ImmutableList.of(issueLink1, issueLink2));

    replayMocks();

    tested.removeSubTasks(null, issue, EventDispatchOption.ISSUE_DELETED, true);

    assertEquals(2, deleteIssueCalled.get());
    verifyMocks();
  }
  @Test
  public void shouldDeleteSubTaskWithLinks() throws Exception {
    setUpEmptyCustomFields();
    IssueDeleteHelper tested = getIssueDeleteHelper();
    setUpEmptyWatchers();
    MutableIssue parentIssue = createNiceMock(MutableIssue.class);
    expect(parentIssue.getId()).andReturn(PARENT_ISSUE_ID);
    replay(parentIssue);
    expect(mockSubTaskManager.isSubTask(issueGenericValue)).andReturn(true);
    expect(mockSubTaskManager.getParentIssueId(issueGenericValue)).andReturn(PARENT_ISSUE_ID);
    mockSubTaskManager.resetSequences(parentIssue);
    expectLastCall();
    expect(mockIssueManager.getIssueObject(PARENT_ISSUE_ID)).andReturn(parentIssue);

    replayMocks();
    tested.deleteIssue((User) null, issue, EventDispatchOption.ISSUE_DELETED, true);
    makeAssertions();
    assertTrue(capturedSendMailFlag);
  }
Ejemplo n.º 3
0
  /**
   * Initialises this MultiBulkMoveBean given a list of issues.
   *
   * <p>If this MultiBulkMoveBean links a BulkEditBean with parent issues to BulkEditBeans with
   * subtasks, then include the parent BulkEditBean in the parentBulkEditBean parameter. Otherwise
   * you can pass null.
   *
   * @param issues Issues for this MultiBulkMoveBean.
   * @param parentBulkEditBean If this MultiBulkMoveBean represents subtasks, then this is the
   *     BulkEditBean that contains the parents of the subtasks, otherwise null.
   */
  public void initFromIssues(List issues, BulkEditBean parentBulkEditBean) {
    // Ensure that the order is kept
    issuesInContext = (ListOrderedMap) ListOrderedMap.decorate(new MultiHashMap());
    regularIssues = new ListOrderedMap();
    subTaskIssues = new ArrayList<Issue>();

    // First pass stores att the
    for (final Object issue2 : issues) {
      MutableIssue issue = (MutableIssue) issue2;
      if (!issue.isSubTask()) {
        regularIssues.put(issue.getId(), issue);
      } else {
        subTaskIssues.add(issue);
      }
    }

    // Split it up by context, also check special rule that you can't move sub tasks & its parent
    // all in the same go
    for (final Object issue1 : issues) {
      MutableIssue issue = (MutableIssue) issue1;
      // NOTE: we only do this for the bulk move operation, this is likely the correct behavior for
      // the
      // bulk move operation but I am certain that it is not correct for the bulk migrate operation
      // JRA-10244.
      // In bulk move the wizard will prompt the user with subtask information once it has collected
      // the project
      // information about the parent issues, this is not need in the the issue type scheme
      // migration since you
      // will never be changing the project
      // TODO: Why test for operation name?
      if (BulkMigrateOperation.OPERATION_NAME.equals(operationName)
          && issue.isSubTask()
          && regularIssues.containsKey(issue.getParentId())) {
        log.info(
            "Sub issue: "
                + issue.getKey()
                + " : discarded since parent was also present in the bulk move");
        subTasksDiscarded++;
      } else {
        issuesInContext.put(
            new IssueContextImpl(issue.getProjectObject(), issue.getIssueTypeObject()), issue);
      }
    }

    // Set the bulk edit bean.. sort the keys by project
    bulkEditBeans = new ListOrderedMap();
    List keys = new ArrayList(issuesInContext.keySet());
    Collections.sort(keys);

    for (final Object key : keys) {
      IssueContext context = (IssueContext) key;
      Collection issuesForContext = (Collection) issuesInContext.get(context);

      BulkEditBean bulkEditBean = new BulkEditBeanImpl(issueManager);
      bulkEditBean.initSelectedIssues(issuesForContext);
      bulkEditBean.setOperationName(operationName);
      bulkEditBean.setTargetProject(context.getProjectObject());
      bulkEditBean.setTargetIssueTypeId(
          context.getIssueTypeObject() != null ? context.getIssueTypeObject().getId() : null);
      // Set the Parent BulkEditBean - used by subtask BulkEditBean's to get to the new version of
      // the subtask's parents.
      bulkEditBean.setParentBulkEditBean(parentBulkEditBean);

      bulkEditBeans.put(context, bulkEditBean);
    }
  }
  @Override
  public boolean handleMessage(Message message, MessageHandlerContext context)
      throws MessagingException {
    log.debug("CreateIssueHandler.handleMessage");

    if (!canHandleMessage(message, context.getMonitor())) {
      return deleteEmail;
    }

    try {
      // get either the sender of the message, or the default reporter
      User reporter = getReporter(message, context);

      // no reporter - so reject the message
      if (reporter == null) {
        final String error = getI18nBean().getText("admin.mail.no.default.reporter");
        context.getMonitor().warning(error);
        context.getMonitor().messageRejected(message, error);
        return false;
      }

      final Project project = getProject(message);

      log.debug("Project = " + project);
      if (project == null) {
        final String text = getI18nBean().getText("admin.mail.no.project.configured");
        context.getMonitor().warning(text);
        context.getMonitor().messageRejected(message, text);
        return false;
      }

      // Check that the license is valid before allowing issues to be created
      // This checks for: evaluation licenses expired, user limit licenses where limit has been
      // exceeded
      ErrorCollection errorCollection = new SimpleErrorCollection();
      // Note: want English locale here for logging purposes
      I18nHelper i18nHelper = new I18nBean(Locale.ENGLISH);

      getIssueCreationHelperBean().validateLicense(errorCollection, i18nHelper);
      if (errorCollection.hasAnyErrors()) {
        context
            .getMonitor()
            .warning(
                getI18nBean()
                    .getText(
                        "admin.mail.bad.license", errorCollection.getErrorMessages().toString()));
        return false;
      }

      // If user does not have create permissions, there's no point proceeding. Error out here to
      // avoid a stack
      // trace blow up from the WorkflowManager later on.
      if (!getPermissionManager().hasPermission(Permissions.CREATE_ISSUE, project, reporter, true)
          && reporter.getDirectoryId() != -1) {
        final String error =
            getI18nBean().getText("admin.mail.no.create.permission", reporter.getName());
        context.getMonitor().warning(error);
        context.getMonitor().messageRejected(message, error);
        return false;
      }

      log.debug("Issue Type Key = = " + issueType);

      if (!hasValidIssueType()) {
        context.getMonitor().warning(getI18nBean().getText("admin.mail.invalid.issue.type"));
        return false;
      }
      String summary = message.getSubject();
      if (!TextUtils.stringSet(summary)) {
        context.getMonitor().error(getI18nBean().getText("admin.mail.no.subject"));
        return false;
      }
      if (summary.length() > SummarySystemField.MAX_LEN.intValue()) {
        context.getMonitor().info("Truncating summary field because it is too long: " + summary);
        summary = summary.substring(0, SummarySystemField.MAX_LEN.intValue() - 3) + "...";
      }

      // JRA-7646 - check if priority/description is hidden - if so, do not set
      String priority = null;
      String description = null;

      if (!getFieldVisibilityManager()
          .isFieldHiddenInAllSchemes(
              project.getId(),
              IssueFieldConstants.PRIORITY,
              Collections.singletonList(issueType))) {
        priority = getPriority(message);
      }

      if (!getFieldVisibilityManager()
          .isFieldHiddenInAllSchemes(
              project.getId(),
              IssueFieldConstants.DESCRIPTION,
              Collections.singletonList(issueType))) {
        description = getDescription(reporter, message);
      }

      MutableIssue issueObject = getIssueFactory().getIssue();
      issueObject.setProjectObject(project);
      issueObject.setSummary(summary);
      issueObject.setDescription(description);
      issueObject.setIssueTypeId(issueType);
      issueObject.setReporter(reporter);

      // if no valid assignee found, attempt to assign to default assignee
      User assignee = null;
      if (ccAssignee) {
        assignee = getFirstValidAssignee(message.getAllRecipients(), project);
      }
      if (assignee == null) {
        assignee = getAssigneeResolver().getDefaultAssignee(issueObject, Collections.EMPTY_MAP);
      }

      if (assignee != null) {
        issueObject.setAssignee(assignee);
      }

      issueObject.setPriorityId(priority);

      // Ensure issue level security is correct
      setDefaultSecurityLevel(issueObject);

      /*
       * + FIXME -- set cf defaults @todo +
       */
      // set default custom field values
      // CustomFieldValuesHolder cfvh = new CustomFieldValuesHolder(issueType, project.getId());
      // fields.put("customFields", CustomFieldUtils.getCustomFieldValues(cfvh.getCustomFields()));
      Map<String, Object> fields = new HashMap<String, Object>();
      fields.put("issue", issueObject);
      // TODO: How is this supposed to work? There is no issue created yet; ID = null.
      // wseliga note: Ineed I think that such call does not make sense - it will be always null
      MutableIssue originalIssue = getIssueManager().getIssueObject(issueObject.getId());

      // Give the CustomFields a chance to set their default values JRA-11762
      List<CustomField> customFieldObjects =
          ComponentAccessor.getCustomFieldManager().getCustomFieldObjects(issueObject);
      for (CustomField customField : customFieldObjects) {
        issueObject.setCustomFieldValue(customField, customField.getDefaultValue(issueObject));
      }

      fields.put(WorkflowFunctionUtils.ORIGINAL_ISSUE_KEY, originalIssue);
      final Issue issue = context.createIssue(reporter, issueObject);

      if (issue != null) {
        // Add Cc'ed users as watchers if params set - JRA-9983
        if (ccWatcher) {
          addCcWatchersToIssue(message, issue, reporter, context, context.getMonitor());
        }

        // Record the message id of this e-mail message so we can track replies to this message
        // and associate them with this issue
        recordMessageId(
            MailThreadManager.ISSUE_CREATED_FROM_EMAIL, message, issue.getId(), context);
      }

      // TODO: if this throws an error, then the issue is already created, but the email not deleted
      // - we will keep "handling" this email over and over :(
      createAttachmentsForMessage(message, issue, context);

      return true;
    } catch (Exception e) {
      context.getMonitor().warning(getI18nBean().getText("admin.mail.unable.to.create.issue"), e);
    }

    // something went wrong - don't delete the message
    return false;
  }
 private void setUpIssue() {
   expect(issue.getId()).andReturn(ISSUE_ID).anyTimes();
   expect(issue.getGenericValue()).andReturn(issueGenericValue).anyTimes();
 }