From f037b52e1893522913e86018c987907ee325ced9 Mon Sep 17 00:00:00 2001 From: Bernard Labno Date: Tue, 3 Jul 2012 10:19:33 +0000 Subject: [PATCH] Execute command on issue. Refactored Filter. --- src/main/java/pl/com/it_crowd/youtrack/api/Command.java | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/pl/com/it_crowd/youtrack/api/Filter.java | 251 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/pl/com/it_crowd/youtrack/api/FilterHelper.java | 149 ----------------------------------------------------------------------------------------------------------------------------------------------------- src/main/java/pl/com/it_crowd/youtrack/api/IssueWrapper.java | 29 +++++++++++------------------ src/main/java/pl/com/it_crowd/youtrack/api/YoutrackAPI.java | 70 ++++++++++++++++++++++++++++++++++++++++++++++------------------------ src/main/java/pl/com/it_crowd/youtrack/api/defaults/DateValues.java | 29 +++++++++++++++++++++++++++++ src/main/java/pl/com/it_crowd/youtrack/api/defaults/Fields.java | 42 ++++++++++++++++++++++++++++++++++++++++++ src/main/java/pl/com/it_crowd/youtrack/api/defaults/StateValues.java | 39 +++++++++++++++++++++++++++++++++++++++ src/main/java/pl/com/it_crowd/youtrack/api/exceptions/NoResultFoundException.java | 8 ++------ src/test/java/pl/com/it_crowd/youtrack/api/rest/CommandTest.java | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/test/java/pl/com/it_crowd/youtrack/api/rest/FilterTest.java | 151 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/test/java/pl/com/it_crowd/youtrack/api/rest/YoutrackAPITest.java | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 12 files changed, 754 insertions(+), 198 deletions(-) create mode 100644 src/main/java/pl/com/it_crowd/youtrack/api/Command.java create mode 100644 src/main/java/pl/com/it_crowd/youtrack/api/Filter.java delete mode 100644 src/main/java/pl/com/it_crowd/youtrack/api/FilterHelper.java create mode 100644 src/main/java/pl/com/it_crowd/youtrack/api/defaults/DateValues.java create mode 100644 src/main/java/pl/com/it_crowd/youtrack/api/defaults/Fields.java create mode 100644 src/main/java/pl/com/it_crowd/youtrack/api/defaults/StateValues.java create mode 100644 src/test/java/pl/com/it_crowd/youtrack/api/rest/CommandTest.java create mode 100644 src/test/java/pl/com/it_crowd/youtrack/api/rest/FilterTest.java diff --git a/src/main/java/pl/com/it_crowd/youtrack/api/Command.java b/src/main/java/pl/com/it_crowd/youtrack/api/Command.java new file mode 100644 index 0000000..116df84 --- /dev/null +++ b/src/main/java/pl/com/it_crowd/youtrack/api/Command.java @@ -0,0 +1,67 @@ +package pl.com.it_crowd.youtrack.api; + +import pl.com.it_crowd.youtrack.api.defaults.Fields; +import pl.com.it_crowd.youtrack.api.defaults.StateValues; + +public class Command { +// ------------------------------ FIELDS ------------------------------ + + protected StringBuilder command = new StringBuilder(); + +// -------------------------- STATIC METHODS -------------------------- + + public static Command assigneeCommand(String assignee) + { + return new Command().assignee(assignee); + } + + public static Command stateCommand(String state) + { + return new Command().state(state); + } + + public static Command stateCommand(StateValues state) + { + return new Command().state(state); + } + +// --------------------------- CONSTRUCTORS --------------------------- + + private Command() + { + } + +// ------------------------ CANONICAL METHODS ------------------------ + + @Override + public String toString() + { + return command.toString().trim(); + } + +// -------------------------- OTHER METHODS -------------------------- + + public Command assignee(String assignee) + { + return command(Fields.assignee, assignee); + } + + public Command state(String state) + { + return command(Fields.state, state); + } + + public Command state(StateValues state) + { + if (state.getCommandValue() == null) { + throw new IllegalArgumentException("Cannot set readonly state: " + state); + } + return state(state.getCommandValue()); + } + + private Command command(Fields command, String argument) + { + this.command.append(" ").append(command.getCommand()).append(" ").append(argument); + return this; + } +} diff --git a/src/main/java/pl/com/it_crowd/youtrack/api/Filter.java b/src/main/java/pl/com/it_crowd/youtrack/api/Filter.java new file mode 100644 index 0000000..11a91bc --- /dev/null +++ b/src/main/java/pl/com/it_crowd/youtrack/api/Filter.java @@ -0,0 +1,251 @@ +package pl.com.it_crowd.youtrack.api; + +import pl.com.it_crowd.youtrack.api.defaults.DateValues; +import pl.com.it_crowd.youtrack.api.defaults.Fields; +import pl.com.it_crowd.youtrack.api.defaults.StateValues; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.List; + +public final class Filter { +// ------------------------------ FIELDS ------------------------------ + + private List conditions = new ArrayList(); + + private long maxResults; + +// -------------------------- STATIC METHODS -------------------------- + + public static Filter createdFilter(String date) + { + return new Filter().created(date); + } + + public static Filter createdFilter(DateValues date) + { + return new Filter().created(date); + } + + public static Filter descriptionFilter(String description) + { + return new Filter().description(description); + } + + public static Filter issueIdFilter(String issueId) + { + return new Filter().issueId(issueId); + } + + public static Filter projectFilter(String project) + { + return new Filter().project(project); + } + + public static Filter reporterFilter(String reporter) + { + return new Filter().reporter(reporter); + } + + public static Filter resolvedFilter(String date) + { + return new Filter().resolved(date); + } + + public static Filter resolvedFilter(DateValues date) + { + return new Filter().resolved(date); + } + + public static Filter stateFilter(StateValues state) + { + return new Filter().state(state); + } + + public static Filter stateFilter(String state) + { + return new Filter().state(state); + } + + public static Filter summaryFilter(String summary) + { + return new Filter().summary(summary); + } + + public static Filter unresolvedFilter() + { + return new Filter().unresolved(); + } + + public static Filter updatedFilter(String date) + { + return new Filter().updated(date); + } + + public static Filter updatedFilter(DateValues date) + { + return new Filter().updated(date); + } + + public static Filter updaterFilter(String updater) + { + return new Filter().updater(updater); + } + +// --------------------------- CONSTRUCTORS --------------------------- + + private Filter() + { + + } + +// ------------------------ CANONICAL METHODS ------------------------ + + @Override + public String toString() + { + StringBuilder builder = new StringBuilder(); + String space; + try { + space = URLEncoder.encode(" ", "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + for (Condition condition : conditions) { + builder.append(space); + if (condition.field != null) { + builder.append(condition.field.getCommand()); + builder.append(":"); + } + try { + builder.append(URLEncoder.encode(condition.value, "UTF-8")); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + if (maxResults > 0) { + builder.append("&max=").append(maxResults); + } + return builder.length() > space.length() ? builder.substring(space.length()) : builder.toString(); + } + +// -------------------------- OTHER METHODS -------------------------- + + public Filter created(DateValues date) + { + return created(date.getFilterValue()); + } + + public Filter created(String date) + { + conditions.add(new Condition(Fields.created, date)); + return this; + } + + public Filter description(String description) + { + conditions.add(new Condition(Fields.description, description)); + return this; + } + + public Filter freeText(String text) + { + conditions.add(new Condition(null, text)); + return this; + } + + public Filter issueId(String issueId) + { + conditions.add(new Condition(Fields.issueId, issueId)); + return this; + } + + public Filter maxResults(long maxResults) + { + this.maxResults = maxResults; + return this; + } + + public Filter project(String project) + { + conditions.add(new Condition(Fields.projectShortName, project)); + return this; + } + + public Filter reporter(String reporter) + { + conditions.add(new Condition(Fields.reporterName, reporter)); + return this; + } + + public Filter resolved(DateValues date) + + { + return resolved(date.getFilterValue()); + } + + public Filter resolved(String date) + { + conditions.add(new Condition(Fields.resolved, date)); + return this; + } + + public Filter state(StateValues state) + { + return state(state.getFilterValue()); + } + + public Filter summary(String summary) + { + conditions.add(new Condition(Fields.summary, summary)); + return this; + } + + public Filter unresolved() + { + conditions.add(new Condition(Fields.state, StateValues.Unresolved.getFilterValue())); + return this; + } + + public Filter updated(DateValues date) + { + return updated(date.getFilterValue()); + } + + public Filter updated(String date) + { + conditions.add(new Condition(Fields.updated, date)); + return this; + } + + public Filter updater(String updater) + { + conditions.add(new Condition(Fields.updaterName, updater)); + return this; + } + + private Filter state(String state) + { + conditions.add(new Condition(Fields.state, state)); + return this; + } + +// -------------------------- INNER CLASSES -------------------------- + + private class Condition { +// ------------------------------ FIELDS ------------------------------ + + private Fields field; + + private String value; + +// --------------------------- CONSTRUCTORS --------------------------- + + private Condition(Fields field, String value) + { + this.field = field; + this.value = value; + } + } +} diff --git a/src/main/java/pl/com/it_crowd/youtrack/api/FilterHelper.java b/src/main/java/pl/com/it_crowd/youtrack/api/FilterHelper.java deleted file mode 100644 index 051d035..0000000 --- a/src/main/java/pl/com/it_crowd/youtrack/api/FilterHelper.java +++ /dev/null @@ -1,149 +0,0 @@ -package pl.com.it_crowd.youtrack.api; - -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; -import java.util.ArrayList; -import java.util.List; - -public final class FilterHelper { -// -------------------------- STATIC METHODS -------------------------- - - public static Filter issueId(String issueId) - { - return new Filter().issueId(issueId); - } - - public static Filter summary(String summary) - { - return new Filter().summary(summary); - } - - public static Filter unresolved() - { - return new Filter().unresolved(); - } - -// --------------------------- CONSTRUCTORS --------------------------- - - private FilterHelper() - { - - } - -// -------------------------- INNER CLASSES -------------------------- - - public static class Filter { -// ------------------------------ FIELDS ------------------------------ - - private List conditions = new ArrayList(); - - private long maxResults; - -// --------------------------- CONSTRUCTORS --------------------------- - - private Filter() - { - - } - -// ------------------------ CANONICAL METHODS ------------------------ - - @Override - public String toString() - { - StringBuilder builder = new StringBuilder(); - String space; - try { - space = URLEncoder.encode(" ", "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - for (Condition condition : conditions) { - builder.append(space); - if (condition.key != null) { - builder.append(condition.key); - builder.append(":"); - } - try { - builder.append(URLEncoder.encode(condition.value, "UTF-8")); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - } - if (maxResults > 0) { - builder.append("&max=").append(maxResults); - } - return builder.toString().trim(); - } - -// -------------------------- OTHER METHODS -------------------------- - - public Filter issueId(String issueId) - { - conditions.add(new Condition(Key.ISSUE_ID, issueId)); - return this; - } - - public Filter maxResults(long maxResults) - { - this.maxResults = maxResults; - return this; - } - - public Filter summary(String summary) - { - conditions.add(new Condition(null, summary)); - return this; - } - - public Filter unresolved() - { - conditions.add(new Condition(Key.STATE, "Unresolved")); - return this; - } - -// -------------------------- ENUMERATIONS -------------------------- - - private enum Key { - ISSUE_ID("issue id"), - STATE("state"); - -// ------------------------------ FIELDS ------------------------------ - - private String key; - -// --------------------------- CONSTRUCTORS --------------------------- - - Key(String key) - { - this.key = key; - } - -// ------------------------ CANONICAL METHODS ------------------------ - - @Override - public String toString() - { - return key; - } - } - -// -------------------------- INNER CLASSES -------------------------- - - private class Condition { -// ------------------------------ FIELDS ------------------------------ - - private Key key; - - private String value; - -// --------------------------- CONSTRUCTORS --------------------------- - - private Condition(Key key, String value) - { - this.key = key; - this.value = value; - } - } - } -} diff --git a/src/main/java/pl/com/it_crowd/youtrack/api/IssueWrapper.java b/src/main/java/pl/com/it_crowd/youtrack/api/IssueWrapper.java index b94f96b..4a9e4b3 100644 --- a/src/main/java/pl/com/it_crowd/youtrack/api/IssueWrapper.java +++ b/src/main/java/pl/com/it_crowd/youtrack/api/IssueWrapper.java @@ -2,12 +2,14 @@ package pl.com.it_crowd.youtrack.api; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import pl.com.it_crowd.youtrack.api.defaults.Fields; import pl.com.it_crowd.youtrack.api.rest.Comment; import pl.com.it_crowd.youtrack.api.rest.Field; import pl.com.it_crowd.youtrack.api.rest.Issue; import java.io.Serializable; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -33,7 +35,7 @@ public class IssueWrapper implements Serializable { for (Object o : issue.getFieldOrComment()) { if (o instanceof Field) { Field field = (Field) o; - fieldMap.put(field.getName(), field); + fieldMap.put(field.getName().toLowerCase(), field); } else if (o instanceof Comment) { comments.add((Comment) o); } else { @@ -44,6 +46,11 @@ public class IssueWrapper implements Serializable { // -------------------------- OTHER METHODS -------------------------- + public List getComments() + { + return Collections.unmodifiableList(comments); + } + public Field getField(String field) { return fieldMap.get(field); @@ -51,7 +58,7 @@ public class IssueWrapper implements Serializable { public String getFieldValue(String fieldName) { - Field field = getField(fieldName); + Field field = getField(fieldName.toLowerCase()); if (field == null) { return null; } @@ -59,27 +66,13 @@ public class IssueWrapper implements Serializable { return values.isEmpty() ? null : values.get(0).getContent(); } - public String getFieldValue(Fields fieldName) + public String getFieldValue(Fields field) { - return getFieldValue(fieldName.name()); + return getFieldValue(field.name()); } public String getId() { return issue.getId(); } - -// -------------------------- ENUMERATIONS -------------------------- - - public enum Fields { - created, - description, - numberInProject, - projectShortName, - reporterName, - resolved, - summary, - updated, - updaterName - } } diff --git a/src/main/java/pl/com/it_crowd/youtrack/api/YoutrackAPI.java b/src/main/java/pl/com/it_crowd/youtrack/api/YoutrackAPI.java index 023e82a..4285735 100644 --- a/src/main/java/pl/com/it_crowd/youtrack/api/YoutrackAPI.java +++ b/src/main/java/pl/com/it_crowd/youtrack/api/YoutrackAPI.java @@ -97,6 +97,11 @@ public class YoutrackAPI { return new DefaultHttpClient(cm); } + private static boolean isBlank(String string) + { + return string != null && string.trim().length() > 0; + } + // --------------------------- CONSTRUCTORS --------------------------- public YoutrackAPI(String serviceLocation) @@ -125,6 +130,37 @@ public class YoutrackAPI { // -------------------------- OTHER METHODS -------------------------- + public void command(String issueId, String command) throws IOException + { + command(issueId, command, null, null, null, null); + } + + public void command(String issueId, Command command) throws IOException + { + command(issueId, command.toString()); + } + + public void command(String issueId, String command, String comment, String group, Boolean disableNotifications, String runAs) throws IOException + { + final HttpPost request = new HttpPost(serviceLocation + "/rest/issue/" + issueId + "/execute"); + final List parameters = new ArrayList(); + parameters.add(new BasicNameValuePair("command", command)); + if (isBlank(comment)) { + parameters.add(new BasicNameValuePair("comment", comment)); + } + if (isBlank(group)) { + parameters.add(new BasicNameValuePair("group", group)); + } + if (disableNotifications != null) { + parameters.add(new BasicNameValuePair("disableNotifications", disableNotifications.toString())); + } + if (isBlank(runAs)) { + parameters.add(new BasicNameValuePair("runAs", runAs)); + } + request.setEntity(new UrlEncodedFormEntity(parameters)); + execute(request); + } + /** * Creates new issue on Youtrack. * @@ -176,15 +212,9 @@ public class YoutrackAPI { final String responseString; try { responseString = execute(new HttpGet(uri)); - } catch (HttpResponseException e) { + } catch (YoutrackErrorException e) { if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) { - try { - final String error = ErrorUnmarshaller.unmarshal(e.getMessage()); - throw new NoResultFoundException(error); - } catch (JAXBException e1) { - // TODO we should log on debug level the JAXBException - throw e; - } + throw new NoResultFoundException(e.getMessage(), e); } else { throw e; } @@ -210,21 +240,7 @@ public class YoutrackAPI { { final HttpPost request = new HttpPost(serviceLocation + "/rest/user/login"); request.setEntity(new UrlEncodedFormEntity(Arrays.asList(new BasicNameValuePair("login", username), new BasicNameValuePair("password", password)))); - try { - execute(request); - } catch (HttpResponseException e) { - if (e.getStatusCode() == HttpStatus.SC_FORBIDDEN) { - try { - final String error = ErrorUnmarshaller.unmarshal(e.getMessage()); - throw new YoutrackErrorException(error, e.getStatusCode()); - } catch (JAXBException e1) { - // TODO we should log on debug level the JAXBException - throw e; - } - } else { - throw e; - } - } + execute(request); } public List searchIssuesByProject(String project, Object filter) throws JAXBException, IOException @@ -291,7 +307,13 @@ public class YoutrackAPI { final HttpEntity entity = response.getEntity(); String responseText = entity == null ? null : EntityUtils.toString(entity); if (statusLine.getStatusCode() >= 300) { - throw new HttpResponseException(statusLine.getStatusCode(), responseText); + try { + final String error = ErrorUnmarshaller.unmarshal(responseText); + throw new YoutrackErrorException(error, statusLine.getStatusCode()); + } catch (JAXBException e) { + // TODO we should log on debug level the JAXBException + throw new HttpResponseException(statusLine.getStatusCode(), responseText); + } } if (entity == null) { throw new ClientProtocolException("Response contains no content"); diff --git a/src/main/java/pl/com/it_crowd/youtrack/api/defaults/DateValues.java b/src/main/java/pl/com/it_crowd/youtrack/api/defaults/DateValues.java new file mode 100644 index 0000000..4cf4f7c --- /dev/null +++ b/src/main/java/pl/com/it_crowd/youtrack/api/defaults/DateValues.java @@ -0,0 +1,29 @@ +package pl.com.it_crowd.youtrack.api.defaults; + +public enum DateValues { + Today, Yesterday, Saturday, Friday, Thursday, Wednesday, Tuesday, Monday, Sunday, ThisWeek("{This week}"), LastWeek("{Last week}"), TwoWeeksAgo( + "{Two weeks ago}"), ThreeWeeksAgo("{Three weeks ago}"), ThisMonth("{This month}"), LastMonth("{Last month}"); + +// ------------------------------ FIELDS ------------------------------ + + private String filterValue; + +// --------------------------- CONSTRUCTORS --------------------------- + + private DateValues() + { + filterValue = name(); + } + + private DateValues(String filterValue) + { + this.filterValue = filterValue; + } + +// --------------------- GETTER / SETTER METHODS --------------------- + + public String getFilterValue() + { + return filterValue; + } +} diff --git a/src/main/java/pl/com/it_crowd/youtrack/api/defaults/Fields.java b/src/main/java/pl/com/it_crowd/youtrack/api/defaults/Fields.java new file mode 100644 index 0000000..ec7b932 --- /dev/null +++ b/src/main/java/pl/com/it_crowd/youtrack/api/defaults/Fields.java @@ -0,0 +1,42 @@ +package pl.com.it_crowd.youtrack.api.defaults; + +public enum Fields { + assignee, + issueId("issue id"), + state, + created("created"), + description, + numberInProject(null), + projectShortName("project"), + reporterName("reporter"), + resolved("resolved date"), + summary, + updated, + updaterName("updated by"); + +// ------------------------------ FIELDS ------------------------------ + + private String command; + +// --------------------------- CONSTRUCTORS --------------------------- + + private Fields() + { + command = name(); + } + + private Fields(String command) + { + this.command = command; + } + +// --------------------- GETTER / SETTER METHODS --------------------- + + public String getCommand() + { + if (command == null) { + throw new UnsupportedOperationException("There is no command for field: " + name()); + } + return command; + } +} diff --git a/src/main/java/pl/com/it_crowd/youtrack/api/defaults/StateValues.java b/src/main/java/pl/com/it_crowd/youtrack/api/defaults/StateValues.java new file mode 100644 index 0000000..9bf42d3 --- /dev/null +++ b/src/main/java/pl/com/it_crowd/youtrack/api/defaults/StateValues.java @@ -0,0 +1,39 @@ +package pl.com.it_crowd.youtrack.api.defaults; + +public enum StateValues { + Submitted, Open, InProgress("{In Progress}", "In Progress"), ToBeDiscussed("{To be discussed}", "To be discussed"), Reopened, CantReproduce( + "{Can't Reproduce}", "Can't Reproduce"), Duplicate, Fixed, WontFix("{Won't fix}", "Won't fix"), + Incomplete, Obsolete, Verified, New, Resolved("Resolved", null), Unresolved("Unresolved", null); + +// ------------------------------ FIELDS ------------------------------ + + private String commandValue; + + private String filterValue; + +// --------------------------- CONSTRUCTORS --------------------------- + + private StateValues() + { + filterValue = name(); + commandValue = name(); + } + + private StateValues(String filterValue, String commandValue) + { + this.filterValue = filterValue; + this.commandValue = commandValue; + } + +// --------------------- GETTER / SETTER METHODS --------------------- + + public String getCommandValue() + { + return commandValue; + } + + public String getFilterValue() + { + return filterValue; + } +} diff --git a/src/main/java/pl/com/it_crowd/youtrack/api/exceptions/NoResultFoundException.java b/src/main/java/pl/com/it_crowd/youtrack/api/exceptions/NoResultFoundException.java index effe9e7..ebd7ace 100644 --- a/src/main/java/pl/com/it_crowd/youtrack/api/exceptions/NoResultFoundException.java +++ b/src/main/java/pl/com/it_crowd/youtrack/api/exceptions/NoResultFoundException.java @@ -3,12 +3,8 @@ package pl.com.it_crowd.youtrack.api.exceptions; public class NoResultFoundException extends RuntimeException { // --------------------------- CONSTRUCTORS --------------------------- - public NoResultFoundException() + public NoResultFoundException(String message, Throwable cause) { - } - - public NoResultFoundException(String message) - { - super(message); + super(message, cause); } } diff --git a/src/test/java/pl/com/it_crowd/youtrack/api/rest/CommandTest.java b/src/test/java/pl/com/it_crowd/youtrack/api/rest/CommandTest.java new file mode 100644 index 0000000..24235c7 --- /dev/null +++ b/src/test/java/pl/com/it_crowd/youtrack/api/rest/CommandTest.java @@ -0,0 +1,50 @@ +package pl.com.it_crowd.youtrack.api.rest; + +import junit.framework.Assert; +import org.junit.Test; +import pl.com.it_crowd.youtrack.api.Command; +import pl.com.it_crowd.youtrack.api.defaults.StateValues; + +public class CommandTest { +// -------------------------- OTHER METHODS -------------------------- + + @Test + public void assignee() + { + Assert.assertEquals("state Fixed assignee Tomek", Command.stateCommand("Fixed").assignee("Tomek").toString()); + } + + @Test + public void assigneeChaining() + { + Assert.assertEquals("assignee Jacek assignee Tomek assignee Bernard", + Command.assigneeCommand("Jacek").assignee("Tomek").assignee("Bernard").toString()); + } + + @Test + public void assigneeCommand() + { + Assert.assertEquals("assignee Jacek", Command.assigneeCommand("Jacek").toString()); + } + + @Test + public void state() + { + Assert.assertEquals("assignee Tomek state Open", Command.assigneeCommand("Tomek").state("Open").toString()); + Assert.assertEquals("assignee Tomek state To be discussed", Command.assigneeCommand("Tomek").state(StateValues.ToBeDiscussed).toString()); + } + + @Test + public void stateChaining() + { + Assert.assertEquals("state Fixed state Open state Obsolete", Command.stateCommand("Fixed").state("Open").state("Obsolete").toString()); + Assert.assertEquals("state Fixed state Open state Obsolete", Command.stateCommand("Fixed").state(StateValues.Open).state("Obsolete").toString()); + } + + @Test + public void stateCommand() + { + Assert.assertEquals("state Fixed", Command.stateCommand("Fixed").toString()); + Assert.assertEquals("state In Progress", Command.stateCommand(StateValues.InProgress).toString()); + } +} diff --git a/src/test/java/pl/com/it_crowd/youtrack/api/rest/FilterTest.java b/src/test/java/pl/com/it_crowd/youtrack/api/rest/FilterTest.java new file mode 100644 index 0000000..9757ef8 --- /dev/null +++ b/src/test/java/pl/com/it_crowd/youtrack/api/rest/FilterTest.java @@ -0,0 +1,151 @@ +package pl.com.it_crowd.youtrack.api.rest; + +import junit.framework.Assert; +import org.junit.Test; +import pl.com.it_crowd.youtrack.api.Filter; +import pl.com.it_crowd.youtrack.api.defaults.DateValues; +import pl.com.it_crowd.youtrack.api.defaults.StateValues; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +public class FilterTest { +// -------------------------- STATIC METHODS -------------------------- + + private static String encodeURL(String s) + { + try { + return URLEncoder.encode(s, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + +// -------------------------- OTHER METHODS -------------------------- + + @Test + public void chaining() + { + final String reporterBernard = "reporter:bernard"; + final String updaterJacek = "updated by:Jacek"; + final String projectTST_QA = "project:TST_QA"; + final String resolvedSunday = "resolved date:Sunday"; + final String updatedThreeWeeksAgo = "updated:" + encodeURL("{Three weeks ago}"); + final String createdLastMonth = "created:" + encodeURL("{Last month}"); + final String stateDuplicate = "state:" + encodeURL("Duplicate"); + final String space = encodeURL(" "); + Assert.assertEquals(stateDuplicate + space + createdLastMonth, Filter.stateFilter(StateValues.Duplicate).created(DateValues.LastMonth).toString()); + Assert.assertEquals(createdLastMonth + space + stateDuplicate, Filter.createdFilter(DateValues.LastMonth).state(StateValues.Duplicate).toString()); + Assert.assertEquals(createdLastMonth + space + stateDuplicate + space + "description:pikachu", + Filter.createdFilter(DateValues.LastMonth).state(StateValues.Duplicate).description("pikachu").toString()); + Assert.assertEquals(reporterBernard + space + projectTST_QA, Filter.reporterFilter("bernard").project("TST_QA").toString()); + Assert.assertEquals(projectTST_QA + space + reporterBernard, Filter.projectFilter("TST_QA").reporter("bernard").toString()); + Assert.assertEquals(resolvedSunday + space + updatedThreeWeeksAgo, Filter.resolvedFilter("Sunday").updated(DateValues.ThreeWeeksAgo).toString()); + Assert.assertEquals(updatedThreeWeeksAgo + space + resolvedSunday, Filter.updatedFilter(DateValues.ThreeWeeksAgo).resolved("Sunday").toString()); + Assert.assertEquals(updatedThreeWeeksAgo + space + updaterJacek, Filter.updatedFilter("{Three weeks ago}").updater("Jacek").toString()); + Assert.assertEquals(updaterJacek + space + updatedThreeWeeksAgo, Filter.updaterFilter("Jacek").updated("{Three weeks ago}").toString()); + } + + @Test + public void createdFilter() + { + Assert.assertEquals("created:" + encodeURL("2012-01-01"), Filter.createdFilter("2012-01-01").toString()); + Assert.assertEquals("created:" + encodeURL("{Last month}"), Filter.createdFilter(DateValues.LastMonth).toString()); + Assert.assertEquals("created:" + encodeURL("{This month}"), Filter.createdFilter(DateValues.ThisMonth).toString()); + Assert.assertEquals("created:" + encodeURL("{Two weeks ago}"), Filter.createdFilter(DateValues.TwoWeeksAgo).toString()); + Assert.assertEquals("created:" + encodeURL("{Three weeks ago}"), Filter.createdFilter(DateValues.ThreeWeeksAgo).toString()); + Assert.assertEquals("created:" + encodeURL("{Last week}"), Filter.createdFilter(DateValues.LastWeek).toString()); + Assert.assertEquals("created:" + encodeURL("{This week}"), Filter.createdFilter(DateValues.ThisWeek).toString()); + Assert.assertEquals("created:" + encodeURL("Saturday"), Filter.createdFilter(DateValues.Saturday).toString()); + Assert.assertEquals("created:" + encodeURL("Friday"), Filter.createdFilter(DateValues.Friday).toString()); + Assert.assertEquals("created:" + encodeURL("Thursday"), Filter.createdFilter(DateValues.Thursday).toString()); + Assert.assertEquals("created:" + encodeURL("Wednesday"), Filter.createdFilter(DateValues.Wednesday).toString()); + Assert.assertEquals("created:" + encodeURL("Tuesday"), Filter.createdFilter(DateValues.Tuesday).toString()); + Assert.assertEquals("created:" + encodeURL("Monday"), Filter.createdFilter(DateValues.Monday).toString()); + Assert.assertEquals("created:" + encodeURL("Sunday"), Filter.createdFilter(DateValues.Sunday).toString()); + } + + @Test + public void descriptionFilter() + { + Assert.assertEquals("description:" + encodeURL("Wacek Ramtamtamski's"), Filter.descriptionFilter("Wacek Ramtamtamski's").toString()); + } + + @Test + public void projectFilter() + { + Assert.assertEquals("project:TST", Filter.projectFilter("TST").toString()); + } + + @Test + public void reporterFilter() + { + Assert.assertEquals("reporter:bernard", Filter.reporterFilter("bernard").toString()); + } + + @Test + public void resolvedFilter() + { + Assert.assertEquals("resolved date:" + encodeURL("2012-01-01"), Filter.resolvedFilter("2012-01-01").toString()); + Assert.assertEquals("resolved date:" + encodeURL("{Last month}"), Filter.resolvedFilter(DateValues.LastMonth).toString()); + Assert.assertEquals("resolved date:" + encodeURL("{This month}"), Filter.resolvedFilter(DateValues.ThisMonth).toString()); + Assert.assertEquals("resolved date:" + encodeURL("{Two weeks ago}"), Filter.resolvedFilter(DateValues.TwoWeeksAgo).toString()); + Assert.assertEquals("resolved date:" + encodeURL("{Three weeks ago}"), Filter.resolvedFilter(DateValues.ThreeWeeksAgo).toString()); + Assert.assertEquals("resolved date:" + encodeURL("{Last week}"), Filter.resolvedFilter(DateValues.LastWeek).toString()); + Assert.assertEquals("resolved date:" + encodeURL("{This week}"), Filter.resolvedFilter(DateValues.ThisWeek).toString()); + Assert.assertEquals("resolved date:" + encodeURL("Saturday"), Filter.resolvedFilter(DateValues.Saturday).toString()); + Assert.assertEquals("resolved date:" + encodeURL("Friday"), Filter.resolvedFilter(DateValues.Friday).toString()); + Assert.assertEquals("resolved date:" + encodeURL("Thursday"), Filter.resolvedFilter(DateValues.Thursday).toString()); + Assert.assertEquals("resolved date:" + encodeURL("Wednesday"), Filter.resolvedFilter(DateValues.Wednesday).toString()); + Assert.assertEquals("resolved date:" + encodeURL("Tuesday"), Filter.resolvedFilter(DateValues.Tuesday).toString()); + Assert.assertEquals("resolved date:" + encodeURL("Monday"), Filter.resolvedFilter(DateValues.Monday).toString()); + Assert.assertEquals("resolved date:" + encodeURL("Sunday"), Filter.resolvedFilter(DateValues.Sunday).toString()); + } + + @Test + public void stateFilter() + { + Assert.assertEquals("state:" + encodeURL("{Can't Reproduce}"), Filter.stateFilter(StateValues.CantReproduce).toString()); + Assert.assertEquals("state:" + encodeURL("Verified"), Filter.stateFilter(StateValues.Verified).toString()); + Assert.assertEquals("state:" + encodeURL("Submitted"), Filter.stateFilter(StateValues.Submitted).toString()); + Assert.assertEquals("state:" + encodeURL("Reopened"), Filter.stateFilter(StateValues.Reopened).toString()); + Assert.assertEquals("state:" + encodeURL("{To be discussed}"), Filter.stateFilter(StateValues.ToBeDiscussed).toString()); + Assert.assertEquals("state:" + encodeURL("Duplicate"), Filter.stateFilter(StateValues.Duplicate).toString()); + Assert.assertEquals("state:" + encodeURL("Resolved"), Filter.stateFilter(StateValues.Resolved).toString()); + Assert.assertEquals("state:" + encodeURL("Fixed"), Filter.stateFilter(StateValues.Fixed).toString()); + Assert.assertEquals("state:" + encodeURL("Incomplete"), Filter.stateFilter(StateValues.Incomplete).toString()); + Assert.assertEquals("state:" + encodeURL("{In Progress}"), Filter.stateFilter(StateValues.InProgress).toString()); + Assert.assertEquals("state:" + encodeURL("New"), Filter.stateFilter(StateValues.New).toString()); + Assert.assertEquals("state:" + encodeURL("Obsolete"), Filter.stateFilter(StateValues.Obsolete).toString()); + Assert.assertEquals("state:" + encodeURL("Open"), Filter.stateFilter(StateValues.Open).toString()); + Assert.assertEquals("state:" + encodeURL("{Won't fix}"), Filter.stateFilter(StateValues.WontFix).toString()); + } + + @Test + public void updatedFilter() + { + Assert.assertEquals("updated:" + encodeURL("Today"), Filter.updatedFilter(DateValues.Today).toString()); + Assert.assertEquals("updated:" + encodeURL("Yesterday"), Filter.updatedFilter(DateValues.Yesterday).toString()); + Assert.assertEquals("updated:" + encodeURL("2012-01-01"), Filter.updatedFilter("2012-01-01").toString()); + Assert.assertEquals("updated:" + encodeURL("{Last month}"), Filter.updatedFilter(DateValues.LastMonth).toString()); + Assert.assertEquals("updated:" + encodeURL("{This month}"), Filter.updatedFilter(DateValues.ThisMonth).toString()); + Assert.assertEquals("updated:" + encodeURL("{Two weeks ago}"), Filter.updatedFilter(DateValues.TwoWeeksAgo).toString()); + Assert.assertEquals("updated:" + encodeURL("{Three weeks ago}"), Filter.updatedFilter(DateValues.ThreeWeeksAgo).toString()); + Assert.assertEquals("updated:" + encodeURL("{Last week}"), Filter.updatedFilter(DateValues.LastWeek).toString()); + Assert.assertEquals("updated:" + encodeURL("{This week}"), Filter.updatedFilter(DateValues.ThisWeek).toString()); + Assert.assertEquals("updated:" + encodeURL("Saturday"), Filter.updatedFilter(DateValues.Saturday).toString()); + Assert.assertEquals("updated:" + encodeURL("Friday"), Filter.updatedFilter(DateValues.Friday).toString()); + Assert.assertEquals("updated:" + encodeURL("Thursday"), Filter.updatedFilter(DateValues.Thursday).toString()); + Assert.assertEquals("updated:" + encodeURL("Wednesday"), Filter.updatedFilter(DateValues.Wednesday).toString()); + Assert.assertEquals("updated:" + encodeURL("Tuesday"), Filter.updatedFilter(DateValues.Tuesday).toString()); + Assert.assertEquals("updated:" + encodeURL("Monday"), Filter.updatedFilter(DateValues.Monday).toString()); + Assert.assertEquals("updated:" + encodeURL("Sunday"), Filter.updatedFilter(DateValues.Sunday).toString()); + } + + @Test + + public void updaterFilter() + { + Assert.assertEquals("updated by:bernard", Filter.updaterFilter("bernard").toString()); + } +} diff --git a/src/test/java/pl/com/it_crowd/youtrack/api/rest/YoutrackAPITest.java b/src/test/java/pl/com/it_crowd/youtrack/api/rest/YoutrackAPITest.java index 905a2a8..085face 100644 --- a/src/test/java/pl/com/it_crowd/youtrack/api/rest/YoutrackAPITest.java +++ b/src/test/java/pl/com/it_crowd/youtrack/api/rest/YoutrackAPITest.java @@ -4,8 +4,11 @@ import junit.framework.Assert; import org.apache.http.HttpStatus; import org.apache.http.auth.AuthenticationException; import org.junit.Test; +import pl.com.it_crowd.youtrack.api.Command; import pl.com.it_crowd.youtrack.api.IssueWrapper; import pl.com.it_crowd.youtrack.api.YoutrackAPI; +import pl.com.it_crowd.youtrack.api.defaults.Fields; +import pl.com.it_crowd.youtrack.api.defaults.StateValues; import pl.com.it_crowd.youtrack.api.exceptions.NoResultFoundException; import pl.com.it_crowd.youtrack.api.exceptions.YoutrackErrorException; @@ -16,11 +19,72 @@ import java.util.List; /** * This test requires Youtrack instance with "Test(TST)" project to be running and expects following JVM params: * youtrackLocation, youtrackUsername and youtrackPassword. + * There should be no assigner "wacek" for project TST. + * There should be following assigners for TST project: bernard,root. + * Ticket TST-2 should be deleted. */ public class YoutrackAPITest { // -------------------------- OTHER METHODS -------------------------- @Test + public void commandAllStatesTest() throws IOException, JAXBException + { + final YoutrackAPI api = new YoutrackAPI(getServiceLocation(), getUsername(), getPassword()); + final String issueId = "TST-1"; + IssueWrapper issue; + final StateValues[] stateValueses = StateValues.values(); + for (StateValues state : stateValueses) { + if (state.getCommandValue() != null && !StateValues.Duplicate.equals(state)) { + api.command(issueId, Command.stateCommand(state)); + issue = api.getIssue(issueId); + Assert.assertNotNull(issue); + Assert.assertEquals(state.getCommandValue(), issue.getFieldValue(Fields.state)); + } + } + } + + @Test + public void commandTest() throws IOException, JAXBException + { + final YoutrackAPI api = new YoutrackAPI(getServiceLocation(), getUsername(), getPassword()); + final String issueId = "TST-1"; + IssueWrapper issue; + try { + api.command(issueId, Command.assigneeCommand("wacek").toString()); + Assert.fail("Command should fail"); + } catch (YoutrackErrorException e) { +// There is no such assignee + Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, e.getStatusCode()); + } + api.command(issueId, Command.assigneeCommand("bernard").assignee("root")); + issue = api.getIssue(issueId); + Assert.assertNotNull(issue); + Assert.assertEquals("root", issue.getFieldValue(Fields.assignee)); + + api.command(issueId, Command.assigneeCommand("bernard")); + issue = api.getIssue(issueId); + Assert.assertNotNull(issue); + Assert.assertEquals("bernard", issue.getFieldValue(Fields.assignee)); + + api.command(issueId, Command.stateCommand(StateValues.InProgress)); + issue = api.getIssue(issueId); + Assert.assertNotNull(issue); + Assert.assertEquals("In Progress", issue.getFieldValue(Fields.state)); + + api.command(issueId, Command.stateCommand(StateValues.InProgress).assignee("root")); + issue = api.getIssue(issueId); + Assert.assertNotNull(issue); + Assert.assertEquals("In Progress", issue.getFieldValue(Fields.state)); + Assert.assertEquals("root", issue.getFieldValue(Fields.assignee)); + + api.command(issueId, Command.assigneeCommand("bernard").state(StateValues.New)); + issue = api.getIssue(issueId); + Assert.assertNotNull(issue); + Assert.assertEquals("New", issue.getFieldValue(Fields.state)); + Assert.assertEquals("bernard", issue.getFieldValue(Fields.assignee)); + } + + @Test public void createIssueTest() throws IOException, AuthenticationException, JAXBException { YoutrackAPI api = new YoutrackAPI(getServiceLocation(), getUsername(), getPassword()); @@ -35,6 +99,7 @@ public class YoutrackAPITest { YoutrackAPI api = new YoutrackAPI(getServiceLocation(), getUsername(), getPassword()); final IssueWrapper issue = api.getIssue("TST-1"); Assert.assertNotNull(issue); + Assert.assertEquals("1", issue.getFieldValue(Fields.numberInProject)); try { api.getIssue("TST-2"); @@ -81,7 +146,7 @@ public class YoutrackAPITest { List issues = api.searchIssuesByProject("TST", null); Assert.assertTrue(!issues.isEmpty()); for (IssueWrapper issue : issues) { - String summary = issue.getFieldValue(IssueWrapper.Fields.summary); + String summary = issue.getFieldValue(Fields.summary); Assert.assertNotNull(summary); Assert.assertTrue(!"".equals(summary.trim())); } -- libgit2 0.24.0