PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/atlassian-intellij-connector/src/test/java/com/atlassian/theplugin/jira/api/JIRARssClientTest.java

https://bitbucket.org/chadum/connector-idea-2.4.2-crucible
Java | 259 lines | 179 code | 43 blank | 37 comment | 0 complexity | d0c7c9b2eea183a9a11741e5ca0838e7 MD5 | raw file
Possible License(s): BSD-3-Clause, BSD-2-Clause, Apache-2.0
  1. /**
  2. * Copyright (C) 2008 Atlassian
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.atlassian.theplugin.jira.api;
  17. import com.atlassian.connector.commons.api.ConnectionCfg;
  18. import com.atlassian.connector.commons.jira.beans.JIRAProjectBean;
  19. import com.atlassian.connector.commons.jira.beans.JIRAQueryFragment;
  20. import com.atlassian.connector.commons.jira.rss.JIRAException;
  21. import com.atlassian.connector.commons.jira.rss.JIRARssClient;
  22. import com.atlassian.connector.intellij.remoteapi.IntelliJHttpSessionCallbackImpl;
  23. import com.atlassian.theplugin.commons.ServerType;
  24. import com.atlassian.theplugin.commons.cfg.JiraServerCfg;
  25. import com.atlassian.theplugin.commons.cfg.Server;
  26. import com.atlassian.theplugin.commons.cfg.ServerIdImpl;
  27. import com.atlassian.theplugin.commons.cfg.UserCfg;
  28. import com.atlassian.theplugin.commons.exception.HttpProxySettingsException;
  29. import com.atlassian.theplugin.commons.jira.JiraServerData;
  30. import com.atlassian.theplugin.commons.remoteapi.RemoteApiMalformedUrlException;
  31. import com.atlassian.theplugin.commons.remoteapi.RemoteApiSessionExpiredException;
  32. import com.atlassian.theplugin.commons.remoteapi.rest.AbstractHttpSession;
  33. import com.atlassian.theplugin.commons.remoteapi.rest.HttpSessionCallback;
  34. import junit.framework.TestCase;
  35. import org.apache.commons.httpclient.HttpClient;
  36. import org.apache.commons.httpclient.HttpMethod;
  37. import org.jdom.Document;
  38. import org.jdom.JDOMException;
  39. import org.jdom.input.SAXBuilder;
  40. import javax.security.sasl.AuthenticationException;
  41. import java.io.IOException;
  42. import java.io.InputStream;
  43. import java.util.ArrayList;
  44. import java.util.List;
  45. public class JIRARssClientTest extends TestCase {
  46. private String mostRecentUrl;
  47. public void testAssignedIssues() throws Exception {
  48. JIRARssClient rss = getClasspathJIRARssClient("http://www.server.com", null, null, "/jira/assignedIssues.xml");
  49. /*
  50. // first try unauthenticated and test the URL is correct
  51. rss.getAssignedIssues("anyone");
  52. assertEquals("http://www.server.com/sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml?resolution=-1&assignee=anyone&sorter/field=updated&sorter/order=DESC&tempMax=100", mostRecentUrl);
  53. // now try authenticated
  54. rss = getClasspathJIRARssClient("http://www.server.com", "user", "pass", "/jira/api/assignedIssues.xml");
  55. List list = rss.getAssignedIssues("anyone");
  56. assertEquals("http://www.server.com/sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml?resolution=-1&assignee=anyone&sorter/field=updated&sorter/order=DESC&tempMax=100&os_username=user&os_password=pass", mostRecentUrl);
  57. assertEquals(7, list.size());
  58. JIRAIssueBean firstIssue = new JIRAIssueBean();
  59. firstIssue.setServerUrl("http://www.server.com");
  60. firstIssue.setKey("PL-94");
  61. firstIssue.setSummary("NullPointerException on wrong URL to Bamboo server");
  62. assertEquals(firstIssue, list.get(0));
  63. */
  64. }
  65. public void testAuthenticationException_PL_1827() throws RemoteApiMalformedUrlException, JIRAException {
  66. Server srv = new Server() {
  67. ServerIdImpl serverId = new ServerIdImpl();
  68. public ServerIdImpl getServerId() {
  69. return serverId;
  70. }
  71. public String getName() {
  72. return "name";
  73. }
  74. public String getUrl() {
  75. return "http://atlassian.com";
  76. }
  77. public boolean isEnabled() {
  78. return true;
  79. }
  80. public boolean isUseDefaultCredentials() {
  81. return false;
  82. }
  83. public String getUsername() {
  84. return "userName";
  85. }
  86. public String getPassword() {
  87. return "password";
  88. }
  89. public ServerType getServerType() {
  90. return ServerType.JIRA_SERVER;
  91. }
  92. public boolean isDontUseBasicAuth() {
  93. return false;
  94. }
  95. public UserCfg getBasicHttpUser() {
  96. return null;
  97. }
  98. public boolean isShared() {
  99. return false;
  100. }
  101. public void setShared(boolean global) {
  102. }
  103. };
  104. final JiraServerData.Builder builder = new JiraServerData.Builder(srv);
  105. builder.useBasicUser(false);
  106. builder.defaultUser(new UserCfg("userName", "password"));
  107. JIRARssClientPublic mockRssClient = new JIRARssClientPublic(builder.build(), new HttpSessionCallback() {
  108. public HttpClient getHttpClient(ConnectionCfg server) throws HttpProxySettingsException {
  109. return new HttpClient();
  110. }
  111. public void configureHttpMethod(AbstractHttpSession session, HttpMethod method) {
  112. }
  113. });
  114. List<JIRAQueryFragment> list = new ArrayList<JIRAQueryFragment>();
  115. try {
  116. mockRssClient.getIssues(list, "", "", 1, 100);
  117. fail();
  118. } catch (JIRAException e) {
  119. assertTrue(e.getMessage().startsWith("Connection error"));
  120. }
  121. }
  122. // for testing PL-863
  123. public void testBugPl863() throws Exception {
  124. JiraServerCfg serverCfg = new JiraServerCfg(true, "jira", "file://test", new ServerIdImpl(), true) {
  125. public ServerType getServerType() {
  126. return null;
  127. }
  128. public JiraServerCfg getClone() {
  129. return null;
  130. }
  131. };
  132. final JiraServerData server = new JiraServerData(serverCfg);
  133. JIRARssClient c = new JIRARssClient(server, new IntelliJHttpSessionCallbackImpl()) {
  134. @Override
  135. protected Document retrieveGetResponse(String urlString)
  136. throws IOException, JDOMException, RemoteApiSessionExpiredException {
  137. SAXBuilder builder = new SAXBuilder();
  138. InputStream is = JIRARssClientTest.class.getResourceAsStream("/jira/PL-863.xml");
  139. Document doc = builder.build(is);
  140. preprocessResult(doc);
  141. return doc;
  142. }
  143. };
  144. List<JIRAQueryFragment> l = new ArrayList<JIRAQueryFragment>();
  145. l.add(new JIRAProjectBean());
  146. try {
  147. c.getIssues(l, "ASC", "prio", 0, 1);
  148. } catch (JIRAException e) {
  149. // I think it should stay here like this, as this is really unsolved on client side!
  150. System.out.println("PL-863 not fixed: " + e.getMessage());
  151. }
  152. }
  153. public void testBugPl941() throws Exception {
  154. final JiraServerData server =
  155. new JiraServerData(new JiraServerCfg(true, "jira", "file://test", new ServerIdImpl(), true) {
  156. public ServerType getServerType() {
  157. return null;
  158. }
  159. public JiraServerCfg getClone() {
  160. return null;
  161. }
  162. });
  163. JIRARssClient c = new JIRARssClient(server, new IntelliJHttpSessionCallbackImpl()) {
  164. @Override
  165. protected Document retrieveGetResponse(String urlString)
  166. throws IOException, JDOMException, RemoteApiSessionExpiredException {
  167. SAXBuilder builder = new SAXBuilder();
  168. InputStream is = JIRARssClientTest.class.getResourceAsStream("/jira/PL-941.xml");
  169. Document doc = builder.build(is);
  170. preprocessResult(doc);
  171. return doc;
  172. }
  173. };
  174. try {
  175. //if something wron with xml structure getIssue throws an exception so code has to be aware of that
  176. c.getIssue("PL-941");
  177. fail("PL-941 not fixed");
  178. } catch (JIRAException e) {
  179. assertTrue(e.getMessage().startsWith("Cannot parse response from JIRA:"));
  180. }
  181. }
  182. // make a simple mock rss client that overrides URL loading with loading from a file
  183. private JIRARssClient getClasspathJIRARssClient(String url, String userName, String password, final String file)
  184. throws RemoteApiMalformedUrlException {
  185. final JiraServerCfg jiraCfg = new JiraServerCfg(true, "jira", url, new ServerIdImpl(), true) {
  186. public ServerType getServerType() {
  187. return null;
  188. }
  189. public JiraServerCfg getClone() {
  190. return null;
  191. }
  192. };
  193. final JiraServerData.Builder builder = new JiraServerData.Builder(jiraCfg);
  194. builder.useDefaultUser(false);
  195. builder.defaultUser(new UserCfg(userName, password));
  196. return new JIRARssClient(builder.build(), new IntelliJHttpSessionCallbackImpl()) {
  197. // protected so that we can easily write tests by simply returning XML from a file instead of a URL!
  198. protected InputStream getUrlAsStream(String url) throws IOException {
  199. mostRecentUrl = url;
  200. return JIRARssClientTest.class.getResourceAsStream(file);
  201. }
  202. };
  203. }
  204. private class JIRARssClientPublic extends JIRARssClient {
  205. public JIRARssClientPublic(final JiraServerData server, final HttpSessionCallback callback) throws RemoteApiMalformedUrlException {
  206. super(server, callback);
  207. }
  208. @Override
  209. public Document retrieveGetResponse(String urlString) throws IOException, JDOMException, RemoteApiSessionExpiredException {
  210. throw new AuthenticationException("ntlm authorization challenge expected, but not found: ntlm " +
  211. "authorization challenge expected, but not found");
  212. }
  213. }
  214. }