PageRenderTime 35ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/it/IssueResourceTest.java

https://bitbucket.org/atlassian/jiraconnect-jiraplugin/
Java | 222 lines | 181 code | 41 blank | 0 comment | 0 complexity | 31acbdf955c2fa06019f9705d6419d34 MD5 | raw file
  1. package it;
  2. import com.atlassian.jconnect.rest.entities.IssueWithCommentsEntity;
  3. import com.atlassian.jira.pageobjects.JiraTestedProduct;
  4. import com.atlassian.jira.pageobjects.pages.admin.RestoreDataPage;
  5. import com.atlassian.jira.pageobjects.pages.viewissue.ViewIssuePage;
  6. import com.atlassian.jira.util.IOUtil;
  7. import com.atlassian.jira.util.json.JSONArray;
  8. import com.atlassian.jira.util.json.JSONException;
  9. import com.atlassian.jira.util.json.JSONObject;
  10. import com.atlassian.pageobjects.TestedProduct;
  11. import com.atlassian.pageobjects.TestedProductFactory;
  12. import com.atlassian.pageobjects.page.LoginPage;
  13. import com.atlassian.webdriver.AtlassianWebDriver;
  14. import com.atlassian.webdriver.pageobjects.WebDriverTester;
  15. import org.apache.commons.httpclient.HttpClient;
  16. import org.apache.commons.httpclient.HttpMethod;
  17. import org.apache.commons.httpclient.NameValuePair;
  18. import org.apache.commons.httpclient.methods.GetMethod;
  19. import org.apache.commons.httpclient.methods.PostMethod;
  20. import org.apache.commons.httpclient.methods.multipart.FilePart;
  21. import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
  22. import org.apache.commons.httpclient.methods.multipart.Part;
  23. import org.apache.commons.httpclient.methods.multipart.StringPart;
  24. import org.apache.commons.httpclient.params.HttpMethodParams;
  25. import org.apache.commons.io.IOUtils;
  26. import org.codehaus.jackson.map.ObjectMapper;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. import org.openqa.selenium.By;
  30. import org.openqa.selenium.WebDriver;
  31. import org.openqa.selenium.WebElement;
  32. import java.io.File;
  33. import java.io.IOException;
  34. import java.io.InputStream;
  35. import java.net.URISyntaxException;
  36. import java.net.URL;
  37. import java.util.HashMap;
  38. import java.util.Map;
  39. import static junit.framework.Assert.assertEquals;
  40. import static org.junit.Assert.*;
  41. public class IssueResourceTest
  42. {
  43. final TestedProduct<?> jira = TestedProductFactory.create(JiraTestedProduct.class);
  44. @Before
  45. public void setUp()
  46. {
  47. jira.visit(LoginPage.class).loginAsSysAdmin(RestoreDataPage.class);
  48. }
  49. @Test
  50. public void testCreateAndReplyToFeedback() throws URISyntaxException, IOException, JSONException {
  51. final IssueWithCommentsEntity response = _testCreateIssue();
  52. _testIssueComment(response.getKey());
  53. _testPollForUpdates(response.getKey());
  54. }
  55. private IssueWithCommentsEntity _testCreateIssue() throws IOException, URISyntaxException {
  56. String baseUrl = jira.getProductInstance().getBaseUrl();
  57. final PostMethod method = new PostMethod(baseUrl);
  58. configureMethod(method, "/rest/jconnect/latest/issue/create");
  59. final String issueJSON = loadJSONFromFile("/scripts/assets/issue1.json");
  60. final Part issue = new StringPart("issue", issueJSON, "UTF-8");
  61. final Part image = new FilePart("screenshot-1", "screenshot-1.png", getAssetNamed("pony-2.png"), "image/png", "UTF-8");
  62. final Part image2 = new FilePart("screenshot-2", "screenshot-2.png", getAssetNamed("pony-2.png"), "image/png", "UTF-8");
  63. final Part voice = new FilePart("voicerecording-1", "recording-1.aac", getAssetNamed("jiraconnect-recording.aac"), "audio/aac", "UTF-8");
  64. final Part voice2 = new FilePart("voicerecording-2","recording-2.aac", getAssetNamed("jiraconnect-recording.aac"), "audio/aac", "UTF-8");
  65. final Map<String, Object> fields = new HashMap<String, Object>();
  66. fields.put("jmctestfield", "1234");
  67. final JSONObject customFieldsJSON = new JSONObject(fields);
  68. final Part customfields = new StringPart("customfields", customFieldsJSON.toString());
  69. final HttpMethodParams methodParams = new HttpMethodParams();
  70. final MultipartRequestEntity entity = new MultipartRequestEntity(new Part[]{issue, image, image2, voice, voice2, customfields}, methodParams);
  71. method.setRequestEntity(entity);
  72. final HttpClient httpclient = new HttpClient();
  73. final int status = httpclient.executeMethod(method);
  74. assertEquals(200, status);
  75. final ObjectMapper mapper = new ObjectMapper();
  76. IssueWithCommentsEntity response = mapper.readValue(IOUtils.toString(method.getResponseBodyAsStream(), "UTF-8"), IssueWithCommentsEntity.class);
  77. assertEquals("Open", response.getStatus());
  78. assertEquals("Title - please fix my phone", response.getSummary());
  79. assertEquals("Description", response.getDescription());
  80. assertNotNull(response.getKey());
  81. ViewIssuePage page = jira.visit(ViewIssuePage.class, response.getKey());
  82. WebDriverTester tester = (WebDriverTester) jira.getTester();
  83. final AtlassianWebDriver driver = tester.getDriver();
  84. assertTrue(driver.elementExists(By.linkText("recording-1.aac")));
  85. assertTrue(driver.elementExists(By.linkText("recording-1.aac")));
  86. assertTrue(driver.elementExists(By.linkText("recording-2.aac")));
  87. assertTrue(driver.elementExists(By.linkText("screenshot-1.png")));
  88. assertTrue(driver.elementExists(By.linkText("screenshot-2.png")));
  89. assertTrue(driver.elementExists(By.linkText("Title - please fix my phone")));
  90. assertTrue(driver.elementExists(By.linkText("iOS")));
  91. assertTrue(driver.elementExists(By.linkText("java")));
  92. assertTrue(driver.elementExists(By.linkText("JIRA")));
  93. final String environment = driver.findElement(By.id("environment-val")).getText();
  94. assertTrue(environment, environment.contains("Model: iPhone"));
  95. assertTrue(environment, environment.contains("OS Version: 4.1"));
  96. assertTrue(environment, environment.contains("App Version: 1.0"));
  97. assertTrue(environment, environment.contains("Language: Espa")); // removed the ?ol since maven/web-driver not liking this. Works when test is run from IDEA however...
  98. final String version = driver.findElement(By.id("versions-field")).getText();
  99. assertEquals("1.0", version);
  100. final String description = driver.findElement(By.id("issue-description")).getText();
  101. assertEquals("Description", description);
  102. final String customfield = driver.findElement(By.id("customfield_10100-val")).getText();
  103. assertEquals("1,234", customfield);
  104. return response;
  105. }
  106. public void _testIssueComment(String issueKey) throws IOException, URISyntaxException {
  107. String baseUrl = this.jira.getProductInstance().getBaseUrl();
  108. final PostMethod method = new PostMethod(baseUrl);
  109. configureMethod(method, "/rest/jconnect/latest/issue/comment/" + issueKey);
  110. final Map<String, Object> reply = new HashMap<String, Object>();
  111. reply.put("description", "Testing a reply");
  112. reply.put("uuid", IssueResourceUtil.UUID);
  113. final Part issue = new StringPart("issue", new JSONObject(reply).toString());
  114. final Part image =
  115. new FilePart("screenshot-3", "screenshot-reply.png", getAssetNamed("pony-2.png"), "image/png", "UTF-8");
  116. final MultipartRequestEntity entity = new MultipartRequestEntity(new Part[]{issue, image}, new HttpMethodParams());
  117. method.setRequestEntity(entity);
  118. final HttpClient httpclient = new HttpClient();
  119. final int status = httpclient.executeMethod(method);
  120. assertEquals(200, status);
  121. ViewIssuePage page = jira.visit(ViewIssuePage.class, issueKey);
  122. WebDriverTester tester = (WebDriverTester) jira.getTester();
  123. assertTrue(tester.getDriver().elementExists(By.linkText("screenshot-reply.png")));
  124. assertTrue(tester.getDriver().getPageSource().contains("Testing a reply"));
  125. }
  126. public void _testPollForUpdates(String issueKey) throws IOException, JSONException {
  127. final long timeBeforeCommentAdded = System.currentTimeMillis();
  128. final String commentText = "Thanks for the feedback. We will be in touch shortly.";
  129. WebDriverTester tester = (WebDriverTester) this.jira.getTester();
  130. ViewIssuePage page = jira.visit(ViewIssuePage.class, issueKey);
  131. final WebDriver driver = tester.getDriver().getDriver();
  132. driver.findElement(By.id("footer-comment-button")).click();;
  133. final WebElement commentArea = driver.findElement(By.id("comment"));
  134. commentArea.sendKeys(commentText);
  135. commentArea.submit();
  136. GetMethod method = IssueResourceUtil.makeJMCPollRequest(jira, IssueResourceUtil.API_KEY, timeBeforeCommentAdded);
  137. final int status = new HttpClient().executeMethod(method);
  138. assertEquals(200, status);
  139. JSONObject responseObj = new JSONObject(method.getResponseBodyAsString());
  140. final JSONArray issuesWithComments = responseObj.getJSONArray("issuesWithComments");
  141. assertTrue(issuesWithComments.length() >= 1);
  142. final JSONObject issue = (JSONObject) issuesWithComments.get(0);
  143. assertEquals(issueKey, issue.get("key"));
  144. final JSONArray comments = issue.getJSONArray("comments");
  145. assertEquals(2, comments.length());
  146. final JSONObject comment = (JSONObject) comments.get(1);
  147. assertEquals(commentText, comment.get("text"));
  148. assertFalse(comment.getBoolean("systemUser"));
  149. _testBadUUIDSeesNothing(timeBeforeCommentAdded, method);
  150. }
  151. private void _testBadUUIDSeesNothing(long timeBeforeCommentAdded, GetMethod method) throws IOException, JSONException {
  152. final NameValuePair[] params2 = new NameValuePair[] {
  153. new NameValuePair("apikey", IssueResourceUtil.API_KEY),
  154. new NameValuePair("project", IssueResourceUtil.PROJECT_KEY),
  155. new NameValuePair("uuid", "INVALID-UUID-KEY-123"),
  156. new NameValuePair("sinceMillis", timeBeforeCommentAdded + "")
  157. };
  158. method.setQueryString(params2);
  159. final int badUUIDStatus = new HttpClient().executeMethod(method);
  160. assertEquals(200, badUUIDStatus);
  161. JSONObject responseJSON = new JSONObject(method.getResponseBodyAsString());
  162. assertEquals(0, responseJSON.getJSONArray("issuesWithComments").length());
  163. }
  164. private File getAssetNamed(String filename) throws URISyntaxException {
  165. final URL url = getClass().getResource("/scripts/assets/" + filename);
  166. return new File(url.toURI());
  167. }
  168. private HttpMethod configureMethod(HttpMethod method, String path) {
  169. method.setPath(jira.getProductInstance().getContextPath() + path);
  170. final NameValuePair[] params = new NameValuePair[] {
  171. new NameValuePair("apikey", IssueResourceUtil.API_KEY),
  172. new NameValuePair("project", IssueResourceUtil.PROJECT_KEY)
  173. };
  174. method.setQueryString(params);
  175. return method;
  176. }
  177. private String loadJSONFromFile(String filename) throws IOException {
  178. final InputStream in = getClass().getResourceAsStream(filename);
  179. return IOUtil.toString(in, "UTF-8");
  180. }
  181. }