values;
@XmlAttribute
protected String name;
/**
- * Gets the value of the content property.
+ * Gets the value of the values property.
*
*
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
- * This is why there is not a set
method for the content property.
+ * This is why there is not a set
method for the values property.
*
*
* For example, to add a new item, do as follows:
*
- * getContent().add(newItem);
+ * getValues().add(newItem);
*
*
*
*
* Objects of the following type(s) are allowed in the list
- * {@link String }
- * {@link JAXBElement }{@code <}{@link Issues.Issue.Field.Value }{@code >}
+ * {@link Issues.Issue.Field.Value }
*/
- public List getContent()
- {
- if (content == null) {
- content = new ArrayList();
+ public List getValues() {
+ if (values == null) {
+ values = new ArrayList();
}
- return this.content;
+ return this.values;
}
/**
@@ -719,8 +685,7 @@ public class Issues {
* @return possible object is
* {@link String }
*/
- public String getName()
- {
+ public String getName() {
return name;
}
@@ -730,8 +695,7 @@ public class Issues {
* @param value allowed object is
* {@link String }
*/
- public void setName(String value)
- {
+ public void setName(String value) {
this.name = value;
}
@@ -752,23 +716,45 @@ public class Issues {
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
- @XmlType(name = "")
+ @XmlType(name = "", propOrder = {
+ "content"
+ })
public static class Value {
+ @XmlValue
+ protected String content;
@XmlAttribute
protected String type;
-
@XmlAttribute
protected String role;
/**
+ * Gets the value of the content property.
+ *
+ * @return possible object is
+ * {@link String }
+ */
+ public String getContent() {
+ return content;
+ }
+
+ /**
+ * Sets the value of the content property.
+ *
+ * @param value allowed object is
+ * {@link String }
+ */
+ public void setContent(String value) {
+ this.content = value;
+ }
+
+ /**
* Gets the value of the type property.
*
* @return possible object is
* {@link String }
*/
- public String getType()
- {
+ public String getType() {
return type;
}
@@ -778,8 +764,7 @@ public class Issues {
* @param value allowed object is
* {@link String }
*/
- public void setType(String value)
- {
+ public void setType(String value) {
this.type = value;
}
@@ -789,8 +774,7 @@ public class Issues {
* @return possible object is
* {@link String }
*/
- public String getRole()
- {
+ public String getRole() {
return role;
}
@@ -800,11 +784,14 @@ public class Issues {
* @param value allowed object is
* {@link String }
*/
- public void setRole(String value)
- {
+ public void setRole(String value) {
this.role = value;
}
+
}
+
}
+
}
+
}
diff --git a/src/main/java/pl/com/it_crowd/youtrack/api/rest/ObjectFactory.java b/src/main/java/pl/com/it_crowd/youtrack/api/rest/ObjectFactory.java
index aaa5260..b10762e 100644
--- a/src/main/java/pl/com/it_crowd/youtrack/api/rest/ObjectFactory.java
+++ b/src/main/java/pl/com/it_crowd/youtrack/api/rest/ObjectFactory.java
@@ -2,15 +2,12 @@
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.1-833
// See http://java.sun.com/xml/jaxb
// Any modifications to this file will be lost upon recompilation of the source schema.
-// Generated on: 2011.12.05 at 08:56:01 AM CET
+// Generated on: 2011.12.16 at 09:34:48 AM CET
//
package pl.com.it_crowd.youtrack.api.rest;
-import javax.xml.bind.JAXBElement;
-import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
-import javax.xml.namespace.QName;
/**
* This object contains factory methods for each
@@ -28,69 +25,52 @@ import javax.xml.namespace.QName;
@XmlRegistry
public class ObjectFactory {
- private final static QName _IssuesIssueFieldValue_QNAME = new QName("", "value");
-
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: pl.com.it_crowd.youtrack.api.rest
*/
- public ObjectFactory()
- {
+ public ObjectFactory() {
}
/**
* Create an instance of {@link Issues.Issue.Comment.Value }
*/
- public Issues.Issue.Comment.Value createIssuesIssueCommentValue()
- {
+ public Issues.Issue.Comment.Value createIssuesIssueCommentValue() {
return new Issues.Issue.Comment.Value();
}
/**
- * Create an instance of {@link Issues.Issue.Comment }
- */
- public Issues.Issue.Comment createIssuesIssueComment()
- {
- return new Issues.Issue.Comment();
- }
-
- /**
* Create an instance of {@link Issues.Issue.Field }
*/
- public Issues.Issue.Field createIssuesIssueField()
- {
+ public Issues.Issue.Field createIssuesIssueField() {
return new Issues.Issue.Field();
}
/**
* Create an instance of {@link Issues }
*/
- public Issues createIssues()
- {
+ public Issues createIssues() {
return new Issues();
}
/**
- * Create an instance of {@link Issues.Issue }
+ * Create an instance of {@link Issues.Issue.Field.Value }
*/
- public Issues.Issue createIssuesIssue()
- {
- return new Issues.Issue();
+ public Issues.Issue.Field.Value createIssuesIssueFieldValue() {
+ return new Issues.Issue.Field.Value();
}
/**
- * Create an instance of {@link Issues.Issue.Field.Value }
+ * Create an instance of {@link Issues.Issue.Comment }
*/
- public Issues.Issue.Field.Value createIssuesIssueFieldValue()
- {
- return new Issues.Issue.Field.Value();
+ public Issues.Issue.Comment createIssuesIssueComment() {
+ return new Issues.Issue.Comment();
}
/**
- * Create an instance of {@link JAXBElement }{@code <}{@link Issues.Issue.Field.Value }{@code >}}
+ * Create an instance of {@link Issues.Issue }
*/
- @XmlElementDecl(namespace = "", name = "value", scope = Issues.Issue.Field.class)
- public JAXBElement createIssuesIssueFieldValue(Issues.Issue.Field.Value value)
- {
- return new JAXBElement(_IssuesIssueFieldValue_QNAME, Issues.Issue.Field.Value.class, Issues.Issue.Field.class, value);
+ public Issues.Issue createIssuesIssue() {
+ return new Issues.Issue();
}
+
}
diff --git a/src/main/java/pl/com/it_crowd/youtrack/api/rest/YoutrackAPI.java b/src/main/java/pl/com/it_crowd/youtrack/api/rest/YoutrackAPI.java
index 9b64c5f..75b49c7 100644
--- a/src/main/java/pl/com/it_crowd/youtrack/api/rest/YoutrackAPI.java
+++ b/src/main/java/pl/com/it_crowd/youtrack/api/rest/YoutrackAPI.java
@@ -1,59 +1,82 @@
package pl.com.it_crowd.youtrack.api.rest;
import com.gargoylesoftware.htmlunit.BrowserVersion;
+import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebResponse;
+import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.util.NameValuePair;
+import com.gargoylesoftware.htmlunit.xml.XmlPage;
+import org.apache.http.auth.AuthenticationException;
+import pl.com.it_crowd.youtrack.api.Issue;
import pl.com.it_crowd.youtrack.api.IssuesUnmarshaller;
import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
+import java.util.List;
public class YoutrackAPI {
+// ------------------------------ FIELDS ------------------------------
private String serviceLocation;
private WebClient webClient;
- public String getServiceLocation()
- {
- return serviceLocation;
- }
+// --------------------------- CONSTRUCTORS ---------------------------
- public YoutrackAPI(String serviceLocation)
- {
+ public YoutrackAPI(String serviceLocation) {
this.serviceLocation = serviceLocation;
this.webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
this.webClient.setJavaScriptEnabled(false);
this.webClient.setCssEnabled(false);
}
- public YoutrackAPI(String serviceLocation, String username, String password) throws IOException
- {
+ public YoutrackAPI(String serviceLocation, String username, String password) throws IOException, AuthenticationException {
this(serviceLocation);
login(username, password);
}
- public void login(String username, String password) throws IOException
- {
+// --------------------- GETTER / SETTER METHODS ---------------------
+
+ public String getServiceLocation() {
+ return serviceLocation;
+ }
+
+// -------------------------- OTHER METHODS --------------------------
+
+ public void login(String username, String password) throws IOException, AuthenticationException {
ArrayList requestParameters = new ArrayList();
requestParameters.add(new NameValuePair("login", username));
requestParameters.add(new NameValuePair("password", password));
WebRequest request = new WebRequest(new URL(serviceLocation + "/rest/user/login"), HttpMethod.POST);
request.setRequestParameters(requestParameters);
- WebResponse response = webClient.getPage(request).getWebResponse();
- System.out.println(response);
- System.out.println(response.getContentAsString());
+ try {
+ webClient.getPage(request);
+ } catch (FailingHttpStatusCodeException e) {
+ if (e.getStatusCode() == 403) {
+ DomNode error = ((XmlPage) webClient.getCurrentWindow().getEnclosedPage()).getFirstChild();
+ if (error != null) {
+ throw new AuthenticationException(error.getTextContent());
+ }
+ }
+ throw e;
+ }
}
- public Issues searchIssuesByProject(String project, String filter) throws JAXBException, IOException
- {
+ public List searchIssuesByProject(String project, String filter) throws JAXBException, IOException {
String url = serviceLocation + "/rest/issue/byproject/" + project + "?filter=" + filter;
- return IssuesUnmarshaller.unmarshal(webClient.getPage(url).getWebResponse().getContentAsStream());
+ WebResponse webResponse = webClient.getPage(url).getWebResponse();
+// System.out.println(webResponse.getContentAsString());
+ List issues = IssuesUnmarshaller.unmarshal(webResponse.getContentAsStream()).getIssue();
+ List result = new ArrayList();
+ for (Issues.Issue issue : issues) {
+ result.add(new Issue(issue));
+ }
+ return result;
}
}
diff --git a/src/main/xjb/bindings.xjb b/src/main/xjb/bindings.xjb
new file mode 100644
index 0000000..95d5880
--- /dev/null
+++ b/src/main/xjb/bindings.xjb
@@ -0,0 +1,18 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/xsd/issuesByProject.xsd b/src/main/xsd/issuesByProject.xsd
index a8e4eb0..261031c 100644
--- a/src/main/xsd/issuesByProject.xsd
+++ b/src/main/xsd/issuesByProject.xsd
@@ -1,5 +1,5 @@
-
+
@@ -7,10 +7,10 @@
-
+
-
+
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
new file mode 100644
index 0000000..fa86ee8
--- /dev/null
+++ b/src/test/java/pl/com/it_crowd/youtrack/api/rest/YoutrackAPITest.java
@@ -0,0 +1,53 @@
+package pl.com.it_crowd.youtrack.api.rest;
+
+import junit.framework.Assert;
+import org.apache.http.auth.AuthenticationException;
+import org.junit.Test;
+import pl.com.it_crowd.youtrack.api.Issue;
+
+import javax.xml.bind.JAXBException;
+import java.io.IOException;
+import java.util.List;
+
+/**
+ * This test requires setting JVM params youtrackUsername and youtrackPassword.
+ */
+public class YoutrackAPITest {
+// -------------------------- OTHER METHODS --------------------------
+
+ @Test(expected = AuthenticationException.class)
+ public void loginFailureTest() throws IOException, AuthenticationException {
+ final String username = "someFakeLogin";
+ final String password = "someFakePassword";
+ new YoutrackAPI("http://youtrack.it-crowd.com.pl", username, password);
+ }
+
+ @Test
+ public void loginTest() throws IOException, AuthenticationException {
+ final String username = getUsername();
+ final String password = getPassword();
+ new YoutrackAPI("http://youtrack.it-crowd.com.pl", username, password);
+ YoutrackAPI api = new YoutrackAPI("http://youtrack.it-crowd.com.pl");
+ api.login(username, password);
+ }
+
+ @Test
+ public void searchIssuesByProjectTest() throws IOException, AuthenticationException, JAXBException {
+ YoutrackAPI api = new YoutrackAPI("http://youtrack.it-crowd.com.pl", getUsername(), getPassword());
+ List issues = api.searchIssuesByProject("SM", null);
+ Assert.assertTrue(!issues.isEmpty());
+ for (Issue issue : issues) {
+ String summary = issue.getFieldValue(Issue.Fields.summary);
+ Assert.assertNotNull(summary);
+ Assert.assertTrue(!"".equals(summary.trim()));
+ }
+ }
+
+ private String getPassword() {
+ return System.getProperty("youtrackPassword");
+ }
+
+ private String getUsername() {
+ return System.getProperty("youtrackUsername");
+ }
+}