Commits

Alek Mierzwicki committed 7bceaef

JRJC-164 Testing delte issue with subtasks.

Comments (0)

Files changed (4)

api/src/main/java/com/atlassian/jira/rest/client/api/IssueRestClient.java

 	Promise<Issue> getIssue(String issueKey, Iterable<Expandos> expand);
 
 	/**
-	 * Deletes issue with given issueKey
+	 * Deletes issue with given issueKey. You can set {@code deleteSubtasks} to delete issue with subtasks. If issue have
+	 * subtasks and {@code deleteSubtasks} is set to false, then issue won't be deleted.
+	 *
 	 * @param issueKey issue key (like TST-1, or JRA-9)
+	 * @param deleteSubtasks Determines if subtask of issue should be also deleted. If false, and issue has subtasks, then it
+	 *                       won't be deleted.
 	 * @return Void
 	 * @since 2.0
 	 */
-	Promise<Void> deleteIssue(String issueKey);
+	Promise<Void> deleteIssue(String issueKey, boolean deleteSubtasks);
 
 	/**
 	 * Retrieves complete information (if the caller has permission) about watchers for selected issue.

core/src/main/java/com/atlassian/jira/rest/client/internal/async/AsynchronousIssueRestClient.java

 	}
 
 	@Override
-	public Promise<Void> deleteIssue(final String issueKey) {
-		return delete(UriBuilder.fromUri(baseUri).path("issue").path(issueKey).build());
+	public Promise<Void> deleteIssue(String issueKey, boolean deleteSubtasks) {
+		return delete(UriBuilder.fromUri(baseUri).path("issue").path(issueKey)
+				.queryParam("deleteSubtasks", deleteSubtasks).build());
 	}
 
 	@Override

test/src/main/java/com/atlassian/jira/rest/client/test/matchers/RestClientExceptionMatchers.java

 package com.atlassian.jira.rest.client.test.matchers;
 
-import com.atlassian.jira.functest.framework.matchers.IterableMatchers;
 import com.atlassian.jira.rest.client.api.RestClientException;
 import com.atlassian.jira.rest.client.api.domain.util.ErrorCollection;
 import com.google.common.collect.ImmutableList;
 import org.hamcrest.BaseMatcher;
 import org.hamcrest.Description;
 import org.hamcrest.Matcher;
+import org.hamcrest.Matchers;
 
 /**
  * Matchers for RestClientException
 			public boolean matches(final Object item) {
 				if (item instanceof RestClientException) {
 					final RestClientException ex = (RestClientException) item;
-					final org.hamcrest.Matcher<Iterable<String>> errorMessageMatcher = IterableMatchers
-							.containsFirst(expectedErrorMessage);
+					final Matcher<Iterable<? extends String>> errorMessageMatcher = Matchers
+							.contains(expectedErrorMessage);
 					return ex.getStatusCode().get().equals(statusCode)
 							&& ex.getErrorCollections().size() == 1
 							&& errorMessageMatcher.matches(ex.getErrorCollections().iterator().next().getErrorMessages());

test/src/test/java/it/AsynchronousIssueRestClientTest.java

 import com.atlassian.jira.nimblefunctests.annotation.Restore;
 import com.atlassian.jira.rest.client.IntegrationTestUtil;
 import com.atlassian.jira.rest.client.TestUtil;
+import com.atlassian.jira.rest.client.api.GetCreateIssueMetadataOptionsBuilder;
 import com.atlassian.jira.rest.client.api.IssueRestClient;
 import com.atlassian.jira.rest.client.api.RestClientException;
 import com.atlassian.jira.rest.client.api.domain.*;
 import com.atlassian.jira.rest.client.api.domain.input.AttachmentInput;
+import com.atlassian.jira.rest.client.api.domain.input.ComplexIssueInputFieldValue;
 import com.atlassian.jira.rest.client.api.domain.input.FieldInput;
+import com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder;
 import com.atlassian.jira.rest.client.api.domain.input.LinkIssuesInput;
 import com.atlassian.jira.rest.client.api.domain.input.TransitionInput;
 import com.atlassian.jira.rest.client.api.domain.util.ErrorCollection;
+import com.atlassian.jira.rest.client.internal.json.JsonParseUtil;
 import com.google.common.base.Function;
 import com.google.common.base.Predicate;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
 import org.apache.commons.io.IOUtils;
 import org.joda.time.DateTime;
 import org.junit.Assert;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.text.NumberFormat;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.Date;
+import java.util.List;
 import java.util.Locale;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import static com.atlassian.jira.rest.client.IntegrationTestUtil.USER2;
 import static com.atlassian.jira.rest.client.TestUtil.assertErrorCode;
 import static com.atlassian.jira.rest.client.TestUtil.assertExpectedErrorCollection;
+import static com.atlassian.jira.rest.client.api.domain.EntityHelper.findEntityByName;
 import static com.atlassian.jira.rest.client.internal.ServerVersionConstants.BN_JIRA_4_3;
 import static com.atlassian.jira.rest.client.internal.json.TestConstants.*;
 import static com.atlassian.jira.rest.client.test.matchers.RestClientExceptionMatchers.rceWithSingleError;
+import static com.google.common.collect.Iterables.toArray;
 import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.hasItem;
 import static org.hamcrest.Matchers.not;
 		assertEquals(issueKey, issue.getKey());
 
 		// delete issue
-		issueClient.deleteIssue(issueKey).claim();
+		issueClient.deleteIssue(issueKey, false).claim();
 
 		// verify
 		assertThatIssueNotExists(issueKey);
 	}
 
 	@Test
+	public void testDeleteIssueWithSubtasks() {
+		final IssueRestClient issueClient = client.getIssueClient();
+
+		// verify that issue exist and create subtask
+		final String issueKey = "TST-1";
+		final Issue issue = issueClient.getIssue(issueKey).claim();
+		assertEquals(issueKey, issue.getKey());
+		final BasicIssue subtask = addSubtaskToIssue(issue);
+		System.out.println(subtask);
+
+		// delete issue
+		issueClient.deleteIssue(issueKey, true).claim();
+
+		// verify
+		assertThatIssueNotExists(issueKey);
+		assertThatIssueNotExists(subtask.getKey());
+	}
+
+	@Test
+	public void testDeleteIssueWithSubtasksWhenDeleteSubtasksIsFalse() {
+		final IssueRestClient issueClient = client.getIssueClient();
+
+		// verify that issue exist and create subtask
+		final String issueKey = "TST-1";
+		final Issue issue = issueClient.getIssue(issueKey).claim();
+		assertEquals(issueKey, issue.getKey());
+		BasicIssue subtask = addSubtaskToIssue(issue);
+		System.out.println(subtask);
+
+		// delete issue
+		expectedException.expect(rceWithSingleError(400, String.format("The issue '%s' has subtasks.  "
+				+ "You must specify the 'deleteSubtasks' parameter to delete this issue and all its subtasks.", issueKey)));
+		issueClient.deleteIssue(issueKey, false).claim();
+	}
+
+	@Test
 	public void testDeleteIssueWhenNoSuchIssue() {
 		final IssueRestClient issueClient = client.getIssueClient();
 
 
 		// delete issue should thrown 404
 		expectedException.expect(rceWithSingleError(404, "Issue Does Not Exist"));
-		issueClient.deleteIssue(issueKey).claim();
+		issueClient.deleteIssue(issueKey, false).claim();
 	}
 
 	@Test
 
 		// delete issue should thrown 401
 		expectedException.expect(rceWithSingleError(401, "You do not have permission to delete issues in this project."));
-		issueClient.deleteIssue(issueKey).claim();
+		issueClient.deleteIssue(issueKey, false).claim();
 	}
 
 	@Test
 			assertThat(ex, rceWithSingleError(404, "Issue Does Not Exist"));
 		}
 	}
+
+	private BasicIssue addSubtaskToIssue(final Issue issue) {
+		// collect CreateIssueMetadata for project with key TST
+		final IssueRestClient issueClient = client.getIssueClient();
+		final Iterable<CimProject> metadataProjects = issueClient.getCreateIssueMetadata(
+				new GetCreateIssueMetadataOptionsBuilder().withProjectKeys(issue.getProject().getKey())
+						.withExpandedIssueTypesFields().build()).claim();
+
+		// select project and issue
+		assertEquals(1, Iterables.size(metadataProjects));
+		final CimProject project = metadataProjects.iterator().next();
+		final CimIssueType issueType = findEntityByName(project.getIssueTypes(), "Sub-task");
+
+		// build issue input
+		final String summary = "Some subtask";
+		final String description = "Some description for substask";
+
+		// prepare IssueInput
+		final IssueInputBuilder issueInputBuilder = new IssueInputBuilder(project, issueType, summary)
+				.setDescription(description)
+				.setFieldValue("parent", ComplexIssueInputFieldValue.with("key", issue.getKey()));
+
+		// create
+		final BasicIssue basicCreatedIssue = issueClient.createIssue(issueInputBuilder.build()).claim();
+		assertNotNull(basicCreatedIssue.getKey());
+
+		return basicCreatedIssue;
+	}
 }