From e099ff8111359abb76ab355f24468be7b846fd31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Krein=C3=B8e?= Date: Wed, 25 May 2016 09:25:34 +0200 Subject: [PATCH] Added support for fetching a Jira filter --- AUTHORS.md | 3 +- .../java/net/rcarz/jiraclient/Filter.java | 74 +++++++++++++++++++ .../java/net/rcarz/jiraclient/JiraClient.java | 10 +++ .../java/net/rcarz/jiraclient/FilterTest.java | 29 ++++++++ 4 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/rcarz/jiraclient/Filter.java create mode 100644 src/test/java/net/rcarz/jiraclient/FilterTest.java diff --git a/AUTHORS.md b/AUTHORS.md index 3449339..3b155c7 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -3,4 +3,5 @@ Kyle Chaplin @chaplinkyle Alesandro Lang @alesandroLang Javier Molina @javinovich Joseph McCarthy -Magnus Lundberg \ No newline at end of file +Magnus Lundberg +Anders Kreinøe @Kreinoee diff --git a/src/main/java/net/rcarz/jiraclient/Filter.java b/src/main/java/net/rcarz/jiraclient/Filter.java new file mode 100644 index 0000000..f2756f9 --- /dev/null +++ b/src/main/java/net/rcarz/jiraclient/Filter.java @@ -0,0 +1,74 @@ +package net.rcarz.jiraclient; + +import net.sf.json.JSON; +import net.sf.json.JSONObject; + +import java.net.URI; +import java.util.Map; + +/** + * Represens a Jira filter. + */ +public class Filter extends Resource { + + private String name; + private String jql; + private boolean favourite; + + public Filter(RestClient restclient, JSONObject json) { + super(restclient); + + if (json != null) + deserialise(json); + } + + private void deserialise(JSONObject json) { + Map map = json; + + id = Field.getString(map.get("id")); + self = Field.getString(map.get("self")); + name = Field.getString(map.get("name")); + jql = Field.getString(map.get("jql")); + favourite = Field.getBoolean(map.get("favourite")); + } + + public boolean isFavourite() { + return favourite; + } + + public String getJql() { + return jql; + } + + public String getName() { + return name; + } + + public static Filter get(final RestClient restclient, final String id) throws JiraException { + JSON result = null; + + try { + URI uri = restclient.buildURI(getBaseUri() + "filter/" + id); + result = restclient.get(uri); + } catch (Exception ex) { + throw new JiraException("Failed to retrieve filter with id " + id, ex); + } + + if (!(result instanceof JSONObject)) { + throw new JiraException("JSON payload is malformed"); + } + + return new Filter(restclient, (JSONObject) result); + } + + @Override + public String toString() { + return "Filter{" + + "favourite=" + favourite + + ", name='" + name + '\'' + + ", jql='" + jql + '\'' + + '}'; + } + + +} diff --git a/src/main/java/net/rcarz/jiraclient/JiraClient.java b/src/main/java/net/rcarz/jiraclient/JiraClient.java index 18edb4f..70dd1b7 100644 --- a/src/main/java/net/rcarz/jiraclient/JiraClient.java +++ b/src/main/java/net/rcarz/jiraclient/JiraClient.java @@ -354,6 +354,16 @@ public class JiraClient { startAt); } + /** + * Retrieve the jira filter with the supplied id. + * @param id id of the filter to retreive. + * @return The Jira filter with the supplied id + * @throws JiraException + */ + public Filter getFilter(final String id) throws JiraException { + return Filter.get(restclient, id); + } + /** * * @return a list of all priorities available in the Jira installation diff --git a/src/test/java/net/rcarz/jiraclient/FilterTest.java b/src/test/java/net/rcarz/jiraclient/FilterTest.java new file mode 100644 index 0000000..a54edfd --- /dev/null +++ b/src/test/java/net/rcarz/jiraclient/FilterTest.java @@ -0,0 +1,29 @@ +package net.rcarz.jiraclient; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * Test cases for stuff relating to filters. + */ +public class FilterTest { + + @Test + public void testGetFilter() throws JiraException { + JiraClient jira = new JiraClient("https://jira.atlassian.com/", null); + + String id = "12844"; + Filter filter = jira.getFilter(id); + + assertNotNull(filter); + assertEquals("with id " + id, id, filter.getId()); + final String expectedName = "All JIRA Bugs"; + assertEquals("with name " + expectedName, expectedName, filter.getName()); + final String expectedJql = "project = 10240 AND issuetype = 1 ORDER BY key DESC"; + assertEquals("with jql: " + expectedJql, expectedJql, filter.getJql()); + assertEquals("None favourite", false, filter.isFavourite()); + } + +}