1. Atlassian
  2. Project: Atlassian
  3. jira-rest-java-client

Commits

Aleksander Mierzwicki [Atlassian]  committed 53d1f77

JRJC-164 Added support for deleting issue.

  • Participants
  • Parent commits 072b196
  • Branches master

Comments (0)

Files changed (4)

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

View file
 	Promise<Issue> getIssue(String issueKey, Iterable<Expandos> expand);
 
 	/**
+	 * Deletes issue with given issueKey
+	 * @param issueKey issue key (like TST-1, or JRA-9)
+	 * @return Void
+	 * @since 2.0
+	 */
+	Promise<Void> deleteIssue(String issueKey);
+
+	/**
 	 * Retrieves complete information (if the caller has permission) about watchers for selected issue.
 	 *
 	 * @param watchersUri URI of watchers resource for selected issue. Usually obtained by calling <code>Issue.getWatchers().getSelf()</code>

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

View file
 	}
 
 	@Override
+	public Promise<Void> deleteIssue(final String issueKey) {
+		return delete(UriBuilder.fromUri(baseUri).path("issue").path(issueKey).build());
+	}
+
+	@Override
 	public Promise<Watchers> getWatchers(final URI watchersUri) {
 		return getAndParse(watchersUri, watchersParser);
 	}

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

View file
+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;
+
+/**
+ * Matchers for RestClientException
+ *
+ * @since v2.0
+ */
+public class RestClientExceptionMatchers {
+
+	public static Matcher<RestClientException> rceWithSingleError(final Integer statusCode, final String expectedErrorMessage) {
+		return new BaseMatcher<RestClientException>() {
+
+			@Override
+			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);
+					return ex.getStatusCode().get().equals(statusCode)
+							&& ex.getErrorCollections().size() == 1
+							&& errorMessageMatcher.matches(ex.getErrorCollections().iterator().next().getErrorMessages());
+
+				}
+				return false;
+			}
+
+			@Override
+			public void describeTo(final Description description) {
+				final ErrorCollection expectedErrorCollection = ErrorCollection.builder()
+						.errorMessage(expectedErrorMessage).status(statusCode) .build();
+
+				final RestClientException expectedException = new RestClientException(
+						ImmutableList.of(expectedErrorCollection), statusCode);
+
+				description.appendText("<"+expectedException.toString()+">");
+			}
+		};
+	}
+}

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

View file
 import com.atlassian.jira.rest.client.IntegrationTestUtil;
 import com.atlassian.jira.rest.client.TestUtil;
 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.FieldInput;
 import org.apache.commons.io.IOUtils;
 import org.joda.time.DateTime;
 import org.junit.Assert;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 
 import javax.annotation.Nullable;
 import javax.ws.rs.core.Response;
 import static com.atlassian.jira.rest.client.TestUtil.assertExpectedErrorCollection;
 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 org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.hasItem;
 import static org.hamcrest.Matchers.not;
 @Restore(DEFAULT_JIRA_DUMP_FILE)
 public class AsynchronousIssueRestClientTest extends AbstractAsynchronousRestClientTest {
 
+	@Rule
+	public ExpectedException expectedException = ExpectedException.none();
+
 	public static final String UTF8_FILE_BODY = "File body encoded in utf8: Ka\u017a\u0144 i \u017c\u00f3\u0142to\u015b\u0107 b\u0119d\u0105! | \u1f55\u03b1\u03bb\u03bf\u03bd \u03d5\u03b1\u03b3\u03b5\u1fd6\u03bd \u03b4\u1f7b\u03bd\u03b1\u03bc\u03b1\u03b9\u0387 \u03c4\u03bf\u1fe6\u03c4\u03bf \u03bf\u1f54 \u03bc\u03b5 \u03b2\u03bb\u1f71\u03c0\u03c4\u03b5\u03b9 \u0411\u0438 \u0448\u0438\u043b \u0438\u0434\u044d\u0439 \u0447\u0430\u0434\u043d\u0430, \u043d\u0430\u0434\u0430\u0434 \u0445\u043e\u0440\u0442\u043e\u0439 \u0431\u0438\u0448., or 2\u03c0R";
 	public static final String UTF8_FILE_NAME = "utf8 file name Ka\u017a\u0144 i \u017c\u00f3\u0142to\u015b\u0107 b\u0119d\u0105! | \u1f55\u03b1\u03bb\u03bf\u03bd \u03d5\u03b1\u03b3\u03b5\u1fd6\u03bd \u03b4\u1f7b\u03bd\u03b1\u03bc\u03b1\u03b9\u0387 \u03c4\u03bf\u1fe6\u03c4\u03bf \u03bf\u1f54 \u03bc\u03b5 \u03b2\u03bb\u1f71\u03c0\u03c4\u03b5\u03b9 \u0411\u0438 \u0448\u0438\u043b \u0438\u0434\u044d\u0439 \u0447\u0430\u0434\u043d\u0430, \u043d\u0430\u0434\u0430\u0434 \u0445\u043e\u0440\u0442\u043e\u0439 \u0431\u0438\u0448., or 2\u03c0R";
 
 	}
 
 	@Test
+	public void testDeleteIssue() {
+		final IssueRestClient issueClient = client.getIssueClient();
+
+		// verify that issue exist
+		final String issueKey = "TST-1";
+		final Issue issue = issueClient.getIssue(issueKey).claim();
+		assertEquals(issueKey, issue.getKey());
+
+		// delete issue
+		issueClient.deleteIssue(issueKey).claim();
+
+		// verify
+		assertThatIssueNotExists(issueKey);
+	}
+
+	@Test
+	public void testDeleteIssueWhenNoSuchIssue() {
+		final IssueRestClient issueClient = client.getIssueClient();
+
+		// verify that issue exist
+		final String issueKey = "TST-999";
+		assertThatIssueNotExists(issueKey);
+
+		// delete issue should thrown 404
+		expectedException.expect(rceWithSingleError(404, "Issue Does Not Exist"));
+		issueClient.deleteIssue(issueKey).claim();
+	}
+
+	@Test
+	public void testDeleteIssueWithoutDeletePermission() {
+		setAnonymousMode();
+		final IssueRestClient issueClient = client.getIssueClient();
+
+		// verify that issue doesn't exist
+		final String issueKey = "ANONEDIT-2";
+		final Issue issue = issueClient.getIssue(issueKey).claim();
+		assertEquals(issueKey, issue.getKey());
+
+		// delete issue should thrown 401
+		expectedException.expect(rceWithSingleError(401, "You do not have permission to delete issues in this project."));
+		issueClient.deleteIssue(issueKey).claim();
+	}
+
+	@Test
 	public void testTransitionWithNumericCustomFieldAndInteger() throws Exception {
 		final Issue issue = client.getIssueClient().getIssue("TST-1").claim();
 		assertNull(issue.getField(NUMERIC_CUSTOMFIELD_ID).getValue());
 		assertTrue(Iterables.contains(transitionsAfterTransition, stopProgressTransition));
 	}
 
+	private void assertThatIssueNotExists(String issueKey) {
+		try {
+			final Issue issue = client.getIssueClient().getIssue(issueKey).claim();
+			fail("It looks that issue exists, and it should not be here! issue = " + issue);
+		} catch (RestClientException ex) {
+			assertThat(ex, rceWithSingleError(404, "Issue Does Not Exist"));
+		}
+	}
 }