1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.testtools.selenium;
17
18 import org.apache.commons.io.FileUtils;
19 import org.junit.After;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.openqa.selenium.By;
23 import org.openqa.selenium.Keys;
24 import org.openqa.selenium.NoSuchWindowException;
25 import org.openqa.selenium.WebDriver;
26 import org.openqa.selenium.WebElement;
27 import org.openqa.selenium.firefox.FirefoxDriver;
28 import org.openqa.selenium.firefox.FirefoxProfile;
29 import org.openqa.selenium.remote.DesiredCapabilities;
30
31 import java.io.File;
32 import java.io.FilenameFilter;
33 import java.io.IOException;
34 import java.util.HashMap;
35 import java.util.LinkedList;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Set;
39 import java.util.StringTokenizer;
40 import java.util.concurrent.TimeUnit;
41
42
43
44
45
46
47 public class JiraIssueCreation {
48
49 WebDriver driver;
50 String jiraBase;
51 List<File> jiraDataDirs = new LinkedList();
52 boolean passed = false;
53 Map<String, String> jiraMap = new HashMap<String, String>();
54 Map<String, Map<String, String>> jiraMaps = new HashMap<String, Map<String, String>>();
55
56 @Before
57 public void setUp() throws IOException, InterruptedException {
58 jiraMap.put("project-field", System.getProperty("jira.project", "Kuali Rice Development"));
59 jiraMap.put("jira.versions", System.getProperty("jira.versions", "2.5").replaceAll(",", " "));
60 jiraMap.put("jira.fixVersions", System.getProperty("jira.fixVersions", "").replaceAll(",", " "));
61 jiraMap.put("issuetype-field", System.getProperty("jira.issuetype", "Bug Fix"));
62 jiraMap.put("jira.component", System.getProperty("jira.component", "Regression,Development").replaceAll(",", " "));
63 jiraMap.put("jira.priority", System.getProperty("jira.priority", "Critical"));
64
65 jiraBase = System.getProperty("jira.base.url", "https://jira.kuali.org");
66 File inputDir = new File(System.getProperty("jira.input.dir", "."));
67 File listDir[] = inputDir.listFiles();
68 for (int i = 0; i < listDir.length; i++) {
69 if (listDir[i].isDirectory()) {
70 jiraDataDirs.add(listDir[i]);
71 }
72 }
73
74 createJiraMaps();
75 dumpJiraMaps();
76 login();
77
78 passed = true;
79 }
80
81 private void dumpJiraMaps() {
82 for (Map<String, String> jiraMapped : jiraMaps.values()) {
83 for (String key : jiraMapped.keySet()) {
84 System.out.println(key + " = " + jiraMapped.get(key));
85 }
86 System.out.println("\n\n\n\n\n\n\n\n");
87 }
88 }
89
90 private void login() throws InterruptedException {DesiredCapabilities capabilities = new DesiredCapabilities();
91 FirefoxProfile profile = new FirefoxProfile();
92 profile.setEnableNativeEvents(false);
93 capabilities.setCapability(FirefoxDriver.PROFILE, profile);
94
95 driver = new FirefoxDriver(capabilities);
96 driver.manage().timeouts().implicitlyWait(WebDriverUtils.configuredImplicityWait(), TimeUnit.SECONDS);
97 driver.get(jiraBase + "/secure/Dashboard.jspa");
98
99 WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(), By.className("login-link"),
100 this.getClass().toString()).click();
101
102
103 WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(), By.id("username"),
104 this.getClass().toString());
105 driver.findElement(By.id("username")).sendKeys(System.getProperty("cas.username"));
106 driver.findElement(By.id("password")).sendKeys(System.getProperty("cas.password"));
107 driver.findElement(By.name("submit")).click();
108 }
109
110 @After
111 public void tearDown() {
112
113 }
114
115 public void createJiraMaps() throws InterruptedException, IOException {
116 List<JiraData> jiraDatas = new LinkedList();
117 String summary;
118
119 for (File dir: jiraDataDirs) {
120
121 File[] inputFiles = dir.listFiles(new FilenameFilter() {
122 @Override
123 public boolean accept(File dir, String name) {
124 return name.endsWith(".jira");
125 }
126 });
127
128 for (int i = 0, s = inputFiles.length; i < s; i++) {
129 jiraDatas.add(parseJiraData(inputFiles[i]));
130 }
131
132 String testClass = jiraDatas.get(0).fullTestName.substring(0, jiraDatas.get(0).fullTestName.lastIndexOf("."));
133 testClass = testClass.replace("org.kuali.rice.", "");
134 testClass = testClass.replace("edu.sampleu.", "");
135 summary = System.getProperty("jira.summary.start", "").replaceAll("_", " ") + " " + testClass;
136
137 if (jiraDatas.size() == 1) {
138 summary += " " + jiraDatas.get(0).testDetails;
139 }
140
141 summary = summary.replace("java.lang.AssertionError: ", "");
142 summary = summary.replace("org.eclipse.persistence.exceptions.DatabaseException: \nInternal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: ", "");
143 if (summary.indexOf("\n") > -1) {
144 summary = summary.substring(0, summary.indexOf("\n")).trim();
145 } else if (summary.indexOf("\t") > -1) {
146 summary = summary.substring(0, summary.indexOf("\t")).trim();
147 }
148
149 if (summary.length() > 180) {
150 summary = summary.substring(0, 179);
151 }
152
153 jiraMap.put("jira.summary", summary);
154
155 StringBuilder description = new StringBuilder(summary).append(" ").append(System.getProperty("jira.description.start", "").replaceAll("_", " ")).append("\n");
156
157 for (JiraData jiraData : jiraDatas) {
158 if (!"".equals(jiraData.aftSteps)) {
159 description.append("\n").append(jiraData.aftSteps);
160 }
161 description.append("\n").append(jiraData.fullTestName).append(" ( ").append(jiraData.shortTestName).append(" ) - ");
162 description.append(jiraData.testUrl).append("\n");
163 description.append("\n{code}\n\n").append(jiraDatas.get(0).testDetails).append("\n\n{code}\n");
164 }
165
166 jiraMap.put("jira.description", description.toString());
167 jiraMaps.put(dir.getName(), new HashMap<String, String>(jiraMap));
168 }
169 }
170
171 @Test
172 public void testCreateJira() throws InterruptedException, IOException {
173 for (Map<String, String> jiraMap : jiraMaps.values()) {
174
175
176 WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(), By.id("create_link"), this.getClass().toString());
177 driver.get(jiraBase + "/secure/CreateIssue!default.jspa");
178
179
180 WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(), By.id("project-field"),
181 this.getClass().toString()).sendKeys(jiraMap.get("jira.project"));
182
183
184 WebElement issue = WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(), By.id(
185 "issuetype-field"), this.getClass().toString());
186 issue.click();
187 issue.sendKeys(Keys.BACK_SPACE);
188 issue.sendKeys(jiraMap.get("jira.issuetype"));
189
190 issue.sendKeys(Keys.TAB);
191
192 WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(), By.id("issue-create-submit"),
193 this.getClass().toString()).click();
194
195
196
197 WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(), By.id("summary"),
198 this.getClass().toString()).sendKeys(jiraMap.get("jira.summary"));
199
200
201 WebElement component = WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(), By.id(
202 "components-textarea"), this.getClass().toString());
203 String components = jiraMap.get("jira.component");
204 StringTokenizer tokens = new StringTokenizer(components);
205 while (tokens.hasMoreElements()) {
206 component.click();
207 component.sendKeys(tokens.nextToken());
208
209 component.sendKeys(Keys.TAB);
210 }
211
212
213 WebElement descriptionElement = WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(), By.id("description"),
214 this.getClass().toString());
215 descriptionElement.click();
216 descriptionElement.sendKeys(jiraMap.get("jira.description"));
217
218
219 WebElement priority = WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(), By.id("priority-field"),
220 this.getClass().toString());
221 priority.click();
222 priority.sendKeys(Keys.BACK_SPACE);
223 priority.sendKeys(jiraMap.get("jira.priority"));
224
225 priority.sendKeys(Keys.TAB);
226
227
228 WebElement version = WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(), By.id("versions-textarea"),
229 this.getClass().toString());
230 version.click();
231 version.sendKeys(jiraMap.get("jira.versions"));
232
233 version.sendKeys(Keys.TAB);
234
235
236 WebElement fixVersion = WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(), By.id("fixVersions-textarea"),
237 this.getClass().toString());
238 fixVersion.click();
239 fixVersion.sendKeys(jiraMap.get("jira.fixVersions"));
240
241 fixVersion.sendKeys(Keys.TAB);
242
243
244 WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(), By.id("customfield_11621-1"),
245 this.getClass().toString()).click();
246
247 WebDriverUtils.waitFor(driver, 8, By.id("issue-create-submit"), this.getClass().toString());
248
249
250 }
251 }
252
253 protected JiraData parseJiraData(File inputFile) throws IOException {
254 String rawData = FileUtils.readFileToString(inputFile, (String)null);
255 JiraData jiraData = new JiraData();
256 try {
257 jiraData.aftSteps = rawData.substring(rawData.indexOf("AFT Step:"), rawData.indexOf("Abbreviated test name: "));
258 } catch (IndexOutOfBoundsException ioobe) {
259
260 }
261 jiraData.shortTestName = rawData.substring(rawData.indexOf("Abbreviated test name: ") + 23, rawData.indexOf("Full test name: ")).trim();
262 jiraData.fullTestName = rawData.substring(rawData.indexOf("Full test name: ") + 16, rawData.indexOf("Test results url: ")).trim();
263 jiraData.testUrl = rawData.substring(rawData.indexOf("Test results url: ") + 18, rawData.indexOf("Error Message: ")).trim();
264 jiraData.errorMessage = rawData.substring(rawData.indexOf("Error Message: ") + 15, rawData.indexOf("Test Details: ")).trim();
265 jiraData.testDetails = rawData.substring(rawData.indexOf("Test Details: ") + 14, rawData.length()).trim().replace("\t", " ");
266 return jiraData;
267 }
268
269 private void closeAndQuitWebDriver() {
270 if (driver != null) {
271 if (WebDriverUtils.dontTearDownPropertyNotSet() && WebDriverUtils.dontTearDownOnFailure(passed)) {
272 try {
273 driver.close();
274 } catch (NoSuchWindowException nswe) {
275 System.out.println("NoSuchWindowException closing WebDriver " + nswe.getMessage());
276 } finally {
277 if (driver != null) {
278 driver.quit();
279 }
280 }
281 }
282 } else {
283 System.out.println("WebDriver is null for " + this.getClass().toString());
284 }
285 }
286
287 class JiraData {
288 String aftSteps = "";
289 String shortTestName;
290 String fullTestName;
291 String testUrl;
292 String errorMessage;
293 String testDetails;
294 }
295 }