@Test
  public void shouldAddCustomFieldParamsOnDelete() throws Exception {
    final CustomField customField1 = createMock(CustomField.class);
    expect(customField1.getId()).andReturn("customfield_10000");
    expect(customField1.getValue(issue)).andReturn("Value1");
    final CustomField customField2 = createMock(CustomField.class);
    expect(customField2.getId()).andReturn("customfield_10001");
    expect(customField2.getValue(issue)).andReturn("Value2");
    replay(customField1, customField2);

    expect(customFieldManager.getCustomFieldObjects(issue))
        .andReturn(ImmutableList.of(customField1, customField2));
    IssueDeleteHelper tested = getIssueDeleteHelper();
    setUpEmptyWatchers();
    expect(mockSubTaskManager.isSubTask(issueGenericValue)).andReturn(false);
    replayMocks();

    tested.deleteIssue((User) null, issue, EventDispatchOption.ISSUE_DELETED, true);
    makeAssertions();
    verify(customField1, customField2);
    final Map<String, Object> expected =
        ImmutableMap.<String, Object>of(
            "customfield_10000", "Value1", "customfield_10001", "Value2");
    assertEquals(expected, capturedEventParams.get(IssueEvent.CUSTOM_FIELDS_PARAM_NAME));
    assertTrue(capturedSendMailFlag);
  }
  @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);
  }
  private Collection getAllRelevantIssueTypeObjects() {
    if (allRelevantIssueTypeObjects == null) {
      if (subTaskManager.isSubTasksEnabled()) {
        allRelevantIssueTypeObjects = constantsManager.getAllIssueTypeObjects();
      } else {
        allRelevantIssueTypeObjects = constantsManager.getRegularIssueTypeObjects();
      }
    }

    return allRelevantIssueTypeObjects;
  }
  @Test
  public void shouldDeleteNotSubtask() throws Exception {
    setUpEmptyCustomFields();
    IssueDeleteHelper tested = getIssueDeleteHelper();
    setUpEmptyWatchers();
    expect(mockSubTaskManager.isSubTask(issueGenericValue)).andReturn(false);
    replayMocks();

    tested.deleteIssue((User) null, issue, EventDispatchOption.ISSUE_DELETED, false);
    makeAssertions();
    assertFalse(capturedSendMailFlag);
  }
  @Test
  public void shouldAddWatchersParamOnDelete() throws Exception {
    setUpEmptyCustomFields();
    IssueDeleteHelper tested = getIssueDeleteHelper();
    expect(mockIssueManager.getWatchers(issue))
        .andReturn(ImmutableList.<User>of(new MockUser("one"), new MockUser("two")));
    expect(mockSubTaskManager.isSubTask(issueGenericValue)).andReturn(false);
    replayMocks();

    tested.deleteIssue((User) null, issue, EventDispatchOption.ISSUE_DELETED, true);
    makeAssertions();
    final List<User> expected = ImmutableList.<User>of(new MockUser("one"), new MockUser("two"));
    assertEquals(expected, capturedEventParams.get(IssueEvent.WATCHERS_PARAM_NAME));
    assertTrue(capturedSendMailFlag);
  }
  @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();
  }
 public Collection getAllIssueTypes() throws Exception {
   if (subTaskManager.isSubTasksEnabled()) {
     return constantsManager.getAllIssueTypeObjects();
   } else {
     final ArrayList returnValues = new ArrayList(constantsManager.getRegularIssueTypeObjects());
     // Now, since subtasks are disabled we want to make sure we add any subtask issue types that
     // are already
     // selected in the custom field and make sure that the sort order is the same as when we call
     // getAllIssueTypeObjects
     final List intersection =
         new ArrayList(
             CollectionUtils.intersection(
                 constantsManager.getSubTaskIssueTypes(),
                 getCustomField().getAssociatedIssueTypes()));
     Collections.sort(intersection);
     for (final Object anIntersection : intersection) {
       final GenericValue genericValue = (GenericValue) anIntersection;
       returnValues.add(0, constantsManager.getIssueTypeObject(genericValue.getString("id")));
     }
     return returnValues;
   }
 }
 private void defaultSubtaskSetup() {
   expect(mockSubTaskManager.getSubTaskIssueLinks(ISSUE_ID))
       .andReturn(Collections.<IssueLink>emptyList());
 }