/src/test/java/it/IssueResourceTest.java
Java | 222 lines | 181 code | 41 blank | 0 comment | 0 complexity | 31acbdf955c2fa06019f9705d6419d34 MD5 | raw file
- package it;
- import com.atlassian.jconnect.rest.entities.IssueWithCommentsEntity;
- import com.atlassian.jira.pageobjects.JiraTestedProduct;
- import com.atlassian.jira.pageobjects.pages.admin.RestoreDataPage;
- import com.atlassian.jira.pageobjects.pages.viewissue.ViewIssuePage;
- import com.atlassian.jira.util.IOUtil;
- import com.atlassian.jira.util.json.JSONArray;
- import com.atlassian.jira.util.json.JSONException;
- import com.atlassian.jira.util.json.JSONObject;
- import com.atlassian.pageobjects.TestedProduct;
- import com.atlassian.pageobjects.TestedProductFactory;
- import com.atlassian.pageobjects.page.LoginPage;
- import com.atlassian.webdriver.AtlassianWebDriver;
- import com.atlassian.webdriver.pageobjects.WebDriverTester;
- import org.apache.commons.httpclient.HttpClient;
- import org.apache.commons.httpclient.HttpMethod;
- import org.apache.commons.httpclient.NameValuePair;
- import org.apache.commons.httpclient.methods.GetMethod;
- import org.apache.commons.httpclient.methods.PostMethod;
- import org.apache.commons.httpclient.methods.multipart.FilePart;
- import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
- import org.apache.commons.httpclient.methods.multipart.Part;
- import org.apache.commons.httpclient.methods.multipart.StringPart;
- import org.apache.commons.httpclient.params.HttpMethodParams;
- import org.apache.commons.io.IOUtils;
- import org.codehaus.jackson.map.ObjectMapper;
- import org.junit.Before;
- import org.junit.Test;
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.URISyntaxException;
- import java.net.URL;
- import java.util.HashMap;
- import java.util.Map;
- import static junit.framework.Assert.assertEquals;
- import static org.junit.Assert.*;
- public class IssueResourceTest
- {
- final TestedProduct<?> jira = TestedProductFactory.create(JiraTestedProduct.class);
- @Before
- public void setUp()
- {
- jira.visit(LoginPage.class).loginAsSysAdmin(RestoreDataPage.class);
- }
- @Test
- public void testCreateAndReplyToFeedback() throws URISyntaxException, IOException, JSONException {
- final IssueWithCommentsEntity response = _testCreateIssue();
-
- _testIssueComment(response.getKey());
-
- _testPollForUpdates(response.getKey());
- }
- private IssueWithCommentsEntity _testCreateIssue() throws IOException, URISyntaxException {
- String baseUrl = jira.getProductInstance().getBaseUrl();
- final PostMethod method = new PostMethod(baseUrl);
- configureMethod(method, "/rest/jconnect/latest/issue/create");
- final String issueJSON = loadJSONFromFile("/scripts/assets/issue1.json");
- final Part issue = new StringPart("issue", issueJSON, "UTF-8");
- final Part image = new FilePart("screenshot-1", "screenshot-1.png", getAssetNamed("pony-2.png"), "image/png", "UTF-8");
- final Part image2 = new FilePart("screenshot-2", "screenshot-2.png", getAssetNamed("pony-2.png"), "image/png", "UTF-8");
- final Part voice = new FilePart("voicerecording-1", "recording-1.aac", getAssetNamed("jiraconnect-recording.aac"), "audio/aac", "UTF-8");
- final Part voice2 = new FilePart("voicerecording-2","recording-2.aac", getAssetNamed("jiraconnect-recording.aac"), "audio/aac", "UTF-8");
- final Map<String, Object> fields = new HashMap<String, Object>();
- fields.put("jmctestfield", "1234");
- final JSONObject customFieldsJSON = new JSONObject(fields);
- final Part customfields = new StringPart("customfields", customFieldsJSON.toString());
- final HttpMethodParams methodParams = new HttpMethodParams();
- final MultipartRequestEntity entity = new MultipartRequestEntity(new Part[]{issue, image, image2, voice, voice2, customfields}, methodParams);
- method.setRequestEntity(entity);
- final HttpClient httpclient = new HttpClient();
- final int status = httpclient.executeMethod(method);
- assertEquals(200, status);
- final ObjectMapper mapper = new ObjectMapper();
- IssueWithCommentsEntity response = mapper.readValue(IOUtils.toString(method.getResponseBodyAsStream(), "UTF-8"), IssueWithCommentsEntity.class);
- assertEquals("Open", response.getStatus());
- assertEquals("Title - please fix my phone", response.getSummary());
- assertEquals("Description", response.getDescription());
- assertNotNull(response.getKey());
- ViewIssuePage page = jira.visit(ViewIssuePage.class, response.getKey());
- WebDriverTester tester = (WebDriverTester) jira.getTester();
- final AtlassianWebDriver driver = tester.getDriver();
- assertTrue(driver.elementExists(By.linkText("recording-1.aac")));
- assertTrue(driver.elementExists(By.linkText("recording-1.aac")));
- assertTrue(driver.elementExists(By.linkText("recording-2.aac")));
- assertTrue(driver.elementExists(By.linkText("screenshot-1.png")));
- assertTrue(driver.elementExists(By.linkText("screenshot-2.png")));
- assertTrue(driver.elementExists(By.linkText("Title - please fix my phone")));
- assertTrue(driver.elementExists(By.linkText("iOS")));
- assertTrue(driver.elementExists(By.linkText("java")));
- assertTrue(driver.elementExists(By.linkText("JIRA")));
- final String environment = driver.findElement(By.id("environment-val")).getText();
- assertTrue(environment, environment.contains("Model: iPhone"));
- assertTrue(environment, environment.contains("OS Version: 4.1"));
- assertTrue(environment, environment.contains("App Version: 1.0"));
- assertTrue(environment, environment.contains("Language: Espa")); // removed the ?ol since maven/web-driver not liking this. Works when test is run from IDEA however...
- final String version = driver.findElement(By.id("versions-field")).getText();
- assertEquals("1.0", version);
- final String description = driver.findElement(By.id("issue-description")).getText();
- assertEquals("Description", description);
- final String customfield = driver.findElement(By.id("customfield_10100-val")).getText();
- assertEquals("1,234", customfield);
- return response;
- }
- public void _testIssueComment(String issueKey) throws IOException, URISyntaxException {
- String baseUrl = this.jira.getProductInstance().getBaseUrl();
- final PostMethod method = new PostMethod(baseUrl);
- configureMethod(method, "/rest/jconnect/latest/issue/comment/" + issueKey);
- final Map<String, Object> reply = new HashMap<String, Object>();
- reply.put("description", "Testing a reply");
- reply.put("uuid", IssueResourceUtil.UUID);
- final Part issue = new StringPart("issue", new JSONObject(reply).toString());
- final Part image =
- new FilePart("screenshot-3", "screenshot-reply.png", getAssetNamed("pony-2.png"), "image/png", "UTF-8");
- final MultipartRequestEntity entity = new MultipartRequestEntity(new Part[]{issue, image}, new HttpMethodParams());
- method.setRequestEntity(entity);
- final HttpClient httpclient = new HttpClient();
- final int status = httpclient.executeMethod(method);
- assertEquals(200, status);
- ViewIssuePage page = jira.visit(ViewIssuePage.class, issueKey);
- WebDriverTester tester = (WebDriverTester) jira.getTester();
- assertTrue(tester.getDriver().elementExists(By.linkText("screenshot-reply.png")));
- assertTrue(tester.getDriver().getPageSource().contains("Testing a reply"));
- }
- public void _testPollForUpdates(String issueKey) throws IOException, JSONException {
- final long timeBeforeCommentAdded = System.currentTimeMillis();
- final String commentText = "Thanks for the feedback. We will be in touch shortly.";
- WebDriverTester tester = (WebDriverTester) this.jira.getTester();
- ViewIssuePage page = jira.visit(ViewIssuePage.class, issueKey);
- final WebDriver driver = tester.getDriver().getDriver();
- driver.findElement(By.id("footer-comment-button")).click();;
- final WebElement commentArea = driver.findElement(By.id("comment"));
- commentArea.sendKeys(commentText);
- commentArea.submit();
- GetMethod method = IssueResourceUtil.makeJMCPollRequest(jira, IssueResourceUtil.API_KEY, timeBeforeCommentAdded);
- final int status = new HttpClient().executeMethod(method);
- assertEquals(200, status);
- JSONObject responseObj = new JSONObject(method.getResponseBodyAsString());
- final JSONArray issuesWithComments = responseObj.getJSONArray("issuesWithComments");
- assertTrue(issuesWithComments.length() >= 1);
- final JSONObject issue = (JSONObject) issuesWithComments.get(0);
- assertEquals(issueKey, issue.get("key"));
- final JSONArray comments = issue.getJSONArray("comments");
- assertEquals(2, comments.length());
- final JSONObject comment = (JSONObject) comments.get(1);
- assertEquals(commentText, comment.get("text"));
- assertFalse(comment.getBoolean("systemUser"));
- _testBadUUIDSeesNothing(timeBeforeCommentAdded, method);
- }
- private void _testBadUUIDSeesNothing(long timeBeforeCommentAdded, GetMethod method) throws IOException, JSONException {
- final NameValuePair[] params2 = new NameValuePair[] {
- new NameValuePair("apikey", IssueResourceUtil.API_KEY),
- new NameValuePair("project", IssueResourceUtil.PROJECT_KEY),
- new NameValuePair("uuid", "INVALID-UUID-KEY-123"),
- new NameValuePair("sinceMillis", timeBeforeCommentAdded + "")
- };
- method.setQueryString(params2);
-
- final int badUUIDStatus = new HttpClient().executeMethod(method);
- assertEquals(200, badUUIDStatus);
- JSONObject responseJSON = new JSONObject(method.getResponseBodyAsString());
- assertEquals(0, responseJSON.getJSONArray("issuesWithComments").length());
- }
- private File getAssetNamed(String filename) throws URISyntaxException {
- final URL url = getClass().getResource("/scripts/assets/" + filename);
- return new File(url.toURI());
- }
- private HttpMethod configureMethod(HttpMethod method, String path) {
- method.setPath(jira.getProductInstance().getContextPath() + path);
- final NameValuePair[] params = new NameValuePair[] {
- new NameValuePair("apikey", IssueResourceUtil.API_KEY),
- new NameValuePair("project", IssueResourceUtil.PROJECT_KEY)
- };
- method.setQueryString(params);
- return method;
- }
- private String loadJSONFromFile(String filename) throws IOException {
- final InputStream in = getClass().getResourceAsStream(filename);
- return IOUtil.toString(in, "UTF-8");
- }
- }