From 1790e0ca310f2c539d620d846c546a4339f73ba2 Mon Sep 17 00:00:00 2001 From: Magnus Lundberg Date: Fri, 8 Apr 2016 11:23:13 +0200 Subject: [PATCH] Added project category and email to project --- src/main/java/net/rcarz/jiraclient/Field.java | 2 + .../java/net/rcarz/jiraclient/Project.java | 12 ++ .../net/rcarz/jiraclient/ProjectCategory.java | 97 +++++++++++++++ .../net/rcarz/jiraclient/ProjectTest.java | 60 ++++++++++ src/test/java/net/rcarz/jiraclient/Utils.java | 112 ++++++++++++++++++ 5 files changed, 283 insertions(+) create mode 100644 src/main/java/net/rcarz/jiraclient/ProjectCategory.java create mode 100644 src/test/java/net/rcarz/jiraclient/ProjectTest.java diff --git a/src/main/java/net/rcarz/jiraclient/Field.java b/src/main/java/net/rcarz/jiraclient/Field.java index 87c0bdc..ccdb6c6 100644 --- a/src/main/java/net/rcarz/jiraclient/Field.java +++ b/src/main/java/net/rcarz/jiraclient/Field.java @@ -357,6 +357,8 @@ public final class Field { result = (T)new Priority(restclient, (JSONObject)r); else if (type == Project.class) result = (T)new Project(restclient, (JSONObject)r); + else if (type == ProjectCategory.class) + result = (T)new ProjectCategory(restclient, (JSONObject)r); else if (type == RemoteLink.class) result = (T)new RemoteLink(restclient, (JSONObject)r); else if (type == Resolution.class) diff --git a/src/main/java/net/rcarz/jiraclient/Project.java b/src/main/java/net/rcarz/jiraclient/Project.java index f60f5cd..fb674f6 100644 --- a/src/main/java/net/rcarz/jiraclient/Project.java +++ b/src/main/java/net/rcarz/jiraclient/Project.java @@ -41,6 +41,8 @@ public class Project extends Resource { private List issueTypes = null; private List versions = null; private Map roles = null; + private ProjectCategory category = null; + private String email = null; /** * Creates a project from a JSON payload. @@ -73,6 +75,8 @@ public class Project extends Resource { restclient); versions = Field.getResourceArray(Version.class, map.get("versions"), restclient); roles = Field.getMap(String.class, String.class, map.get("roles")); + category = Field.getResource(ProjectCategory.class, map.get( "projectCategory" ), restclient); + email = Field.getString( map.get("email")); } /** @@ -170,5 +174,13 @@ public class Project extends Resource { public Map getRoles() { return roles; } + + public ProjectCategory getCategory() { + return category; + } + + public String getEmail() { + return email; + } } diff --git a/src/main/java/net/rcarz/jiraclient/ProjectCategory.java b/src/main/java/net/rcarz/jiraclient/ProjectCategory.java new file mode 100644 index 0000000..1c7c48a --- /dev/null +++ b/src/main/java/net/rcarz/jiraclient/ProjectCategory.java @@ -0,0 +1,97 @@ +/** + * jira-client - a simple JIRA REST client + * Copyright (c) 2013 Bob Carroll (bob.carroll@alum.rit.edu) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.rcarz.jiraclient; + +import java.util.Map; + +import net.sf.json.JSON; +import net.sf.json.JSONObject; + +/** + * Represents a project category. + */ +public class ProjectCategory extends Resource { + + private String name = null; + private String description = null; + + /** + * Creates a category from a JSON payload. + * + * @param restclient REST client instance + * @param json JSON payload + */ + protected ProjectCategory(RestClient restclient, JSONObject json) { + super(restclient); + + if (json != null) + deserialise(json); + } + + private void deserialise(JSONObject json) { + Map map = json; + + self = Field.getString(map.get("self")); + id = Field.getString(map.get("id")); + description = Field.getString(map.get("description")); + name = Field.getString(map.get("name")); + } + + /** + * Retrieves the given status record. + * + * @param restclient REST client instance + * @param id Internal JIRA ID of the status + * + * @return a status instance + * + * @throws JiraException when the retrieval fails + */ + public static ProjectCategory get(RestClient restclient, String id) + throws JiraException { + + JSON result = null; + + try { + result = restclient.get(getBaseUri() + "projectCategory/" + id); + } catch (Exception ex) { + throw new JiraException("Failed to retrieve status " + id, ex); + } + + if (!(result instanceof JSONObject)) + throw new JiraException("JSON payload is malformed"); + + return new ProjectCategory(restclient, (JSONObject)result); + } + + @Override + public String toString() { + return getName(); + } + + public String getDescription() { + return description; + } + + public String getName() { + return name; + } +} + diff --git a/src/test/java/net/rcarz/jiraclient/ProjectTest.java b/src/test/java/net/rcarz/jiraclient/ProjectTest.java new file mode 100644 index 0000000..8121f6e --- /dev/null +++ b/src/test/java/net/rcarz/jiraclient/ProjectTest.java @@ -0,0 +1,60 @@ +package net.rcarz.jiraclient; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; + +public class ProjectTest { + + @Test + public void testCreateProject() { + new Project(null, Utils.getTestProject()); + } + + @Test + public void projectDataTest() + { + String email = "from-jira@example.com"; + String assigneeType = "PROJECT_LEAD"; + String name = "Example"; + String self = "http://www.example.com/jira/rest/api/2/project/EX"; + String id = "10000"; + String key = "EX"; + String description = "This project was created as an example for REST."; + Project project = new Project(null, Utils.getTestProject()); + + assertEquals(description, project.getDescription()); + assertEquals(name, project.getName()); + assertEquals(id, project.getId()); + assertEquals(email, project.getEmail()); + assertEquals(assigneeType, project.getAssigneeType()); + assertTrue(project.getVersions().isEmpty()); + assertEquals(name, project.getName()); + assertEquals(self, project.getSelf()); + assertEquals( key, project.getKey()); + } + @Test + public void projectIssueTypesTest() + { + Project project = new Project(null, Utils.getTestProject()); + assertEquals(2, project.getIssueTypes().size()); + assertEquals("Task", project.getIssueTypes().get(0).getName()); + assertEquals("Bug", project.getIssueTypes().get(1).getName()); + } + @Test + public void projectCategoryTest() + { + String name = "FIRST"; + String id = "10000"; + String description = "First Project Category"; + + Project project = new Project(null, Utils.getTestProject()); + assertNotNull(project.getCategory()); + assertEquals(description, project.getCategory().getDescription()); + + assertEquals(name, project.getCategory().getName()); + assertEquals(id, project.getCategory().getId()); + } +} diff --git a/src/test/java/net/rcarz/jiraclient/Utils.java b/src/test/java/net/rcarz/jiraclient/Utils.java index b06ef1e..1ac16f9 100644 --- a/src/test/java/net/rcarz/jiraclient/Utils.java +++ b/src/test/java/net/rcarz/jiraclient/Utils.java @@ -281,4 +281,116 @@ public class Utils { return jsonObject; } + public static JSONObject getTestProject() { + JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON("{" + + " \"expand\": \"description,lead,url,projectKeys\"," + + " \"self\": \"http://www.example.com/jira/rest/api/2/project/EX\"," + + " \"id\": \"10000\"," + + " \"key\": \"EX\"," + + " \"description\": \"This project was created as an example for REST.\"," + + " \"lead\": {" + + " \"self\": \"http://www.example.com/jira/rest/api/2/user?username=fred\"," + + " \"name\": \"fred\"," + + " \"avatarUrls\": {" + + " \"48x48\": \"http://www.example.com/jira/secure/useravatar?size=large&ownerId=fred\"," + + " \"24x24\": \"http://www.example.com/jira/secure/useravatar?size=small&ownerId=fred\"," + + " \"16x16\": \"http://www.example.com/jira/secure/useravatar?size=xsmall&ownerId=fred\"," + + " \"32x32\": \"http://www.example.com/jira/secure/useravatar?size=medium&ownerId=fred\"" + + " }," + + " \"displayName\": \"Fred F. User\"," + + " \"active\": false" + + " }," + + " \"components\": [" + + " {" + + " \"self\": \"http://www.example.com/jira/rest/api/2/component/10000\"," + + " \"id\": \"10000\"," + + " \"name\": \"Component 1\"," + + " \"description\": \"This is a JIRA component\"," + + " \"lead\": {" + + " \"self\": \"http://www.example.com/jira/rest/api/2/user?username=fred\"," + + " \"name\": \"fred\"," + + " \"avatarUrls\": {" + + " \"48x48\": \"http://www.example.com/jira/secure/useravatar?size=large&ownerId=fred\"," + + " \"24x24\": \"http://www.example.com/jira/secure/useravatar?size=small&ownerId=fred\"," + + " \"16x16\": \"http://www.example.com/jira/secure/useravatar?size=xsmall&ownerId=fred\"," + + " \"32x32\": \"http://www.example.com/jira/secure/useravatar?size=medium&ownerId=fred\"" + + " }," + + " \"displayName\": \"Fred F. User\"," + + " \"active\": false" + + " }," + + " \"assigneeType\": \"PROJECT_LEAD\"," + + " \"assignee\": {" + + " \"self\": \"http://www.example.com/jira/rest/api/2/user?username=fred\"," + + " \"name\": \"fred\"," + + " \"avatarUrls\": {" + + " \"48x48\": \"http://www.example.com/jira/secure/useravatar?size=large&ownerId=fred\"," + + " \"24x24\": \"http://www.example.com/jira/secure/useravatar?size=small&ownerId=fred\"," + + " \"16x16\": \"http://www.example.com/jira/secure/useravatar?size=xsmall&ownerId=fred\"," + + " \"32x32\": \"http://www.example.com/jira/secure/useravatar?size=medium&ownerId=fred\"" + + " }," + + " \"displayName\": \"Fred F. User\"," + + " \"active\": false" + + " }," + + " \"realAssigneeType\": \"PROJECT_LEAD\"," + + " \"realAssignee\": {" + + " \"self\": \"http://www.example.com/jira/rest/api/2/user?username=fred\"," + + " \"name\": \"fred\"," + + " \"avatarUrls\": {" + + " \"48x48\": \"http://www.example.com/jira/secure/useravatar?size=large&ownerId=fred\"," + + " \"24x24\": \"http://www.example.com/jira/secure/useravatar?size=small&ownerId=fred\"," + + " \"16x16\": \"http://www.example.com/jira/secure/useravatar?size=xsmall&ownerId=fred\"," + + " \"32x32\": \"http://www.example.com/jira/secure/useravatar?size=medium&ownerId=fred\"" + + " }," + + " \"displayName\": \"Fred F. User\"," + + " \"active\": false" + + " }," + + " \"isAssigneeTypeValid\": false," + + " \"project\": \"HSP\"," + + " \"projectId\": 10000" + + " }" + + " ]," + + " \"issueTypes\": [" + + " {" + + " \"self\": \"http://localhost:8090/jira/rest/api/2.0/issueType/3\"," + + " \"id\": \"3\"," + + " \"description\": \"A task that needs to be done.\"," + + " \"iconUrl\": \"http://localhost:8090/jira/images/icons/issuetypes/task.png\"," + + " \"name\": \"Task\"," + + " \"subtask\": false," + + " \"avatarId\": 1" + + " }," + + " {" + + " \"self\": \"http://localhost:8090/jira/rest/api/2.0/issueType/1\"," + + " \"id\": \"1\"," + + " \"description\": \"A problem with the software.\"," + + " \"iconUrl\": \"http://localhost:8090/jira/images/icons/issuetypes/bug.png\"," + + " \"name\": \"Bug\"," + + " \"subtask\": false," + + " \"avatarId\": 10002" + + " }" + + " ]," + + " \"url\": \"http://www.example.com/jira/browse/EX\"," + + " \"email\": \"from-jira@example.com\"," + + " \"assigneeType\": \"PROJECT_LEAD\"," + + " \"versions\": []," + + " \"name\": \"Example\"," + + " \"roles\": {" + + " \"Developers\": \"http://www.example.com/jira/rest/api/2/project/EX/role/10000\"" + + " }," + + " \"avatarUrls\": {" + + " \"48x48\": \"http://www.example.com/jira/secure/projectavatar?size=large&pid=10000\"," + + " \"24x24\": \"http://www.example.com/jira/secure/projectavatar?size=small&pid=10000\"," + + " \"16x16\": \"http://www.example.com/jira/secure/projectavatar?size=xsmall&pid=10000\"," + + " \"32x32\": \"http://www.example.com/jira/secure/projectavatar?size=medium&pid=10000\"" + + " }," + + " \"projectCategory\": {" + + " \"self\": \"http://www.example.com/jira/rest/api/2/projectCategory/10000\"," + + " \"id\": \"10000\"," + + " \"name\": \"FIRST\"," + + " \"description\": \"First Project Category\"" + + " }" + + "}"); + return jsonObject; + } + }