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

/src/test/java/org/agilos/zendesk_jira_plugin/integrationtest/fixtures/JIRAFixture.java

https://github.com/mikis/zendesk-jira-plugin
Java | 224 lines | 144 code | 33 blank | 47 comment | 6 complexity | efc3511b2d2644cb47b06e49c698e136 MD5 | raw file
  1. package org.agilos.zendesk_jira_plugin.integrationtest.fixtures;
  2. import it.org.agilos.zendesk_jira_plugin.integrationtest.JIRAClient;
  3. import it.org.agilos.zendesk_jira_plugin.integrationtest.atlassian.issuehandler.IssueHandler;
  4. import it.org.agilos.zendesk_jira_plugin.integrationtest.atlassian.issuehandler.IssueHandlerProvider;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.util.HashMap;
  8. import java.util.Iterator;
  9. import java.util.Map;
  10. import java.util.ResourceBundle;
  11. import junit.framework.AssertionFailedError;
  12. import net.sourceforge.jwebunit.WebTester;
  13. import org.agilos.jira.soapclient.RemoteException;
  14. import org.agilos.jira.soapclient.RemoteGroup;
  15. import org.agilos.jira.soapclient.RemotePermissionScheme;
  16. import org.agilos.jira.soapclient.RemoteProject;
  17. import org.agilos.jira.soapclient.RemoteUser;
  18. import org.apache.commons.io.FileUtils;
  19. import org.apache.log4j.Logger;
  20. import com.thoughtworks.selenium.DefaultSelenium;
  21. import com.thoughtworks.selenium.Selenium;
  22. /**
  23. * Parent fixture for access to a JIRA instance.
  24. */
  25. public class JIRAFixture {
  26. protected RemoteProject project;
  27. Map<String, RemoteUser> userMap = new HashMap<String, RemoteUser>();
  28. Map<String, RemoteProject> projectMap = new HashMap<String, RemoteProject>();
  29. public JIRAClient jiraClient;
  30. private Logger log = Logger.getLogger(JIRAFixture.class.getName());
  31. public static final String JIRA_ADMINISTRATORS = "jira-administrators";
  32. public static final String JIRA_DEVELOPERS = "jira-developers";
  33. public static final String JIRA_USERS = "jira-users";
  34. public static final ResourceBundle webText = ResourceBundle.getBundle("language.web-texts");
  35. private static final char FS = File.separatorChar;
  36. public WebTester tester;
  37. public static Selenium selenium;
  38. protected IssueHandler issueHandler = IssueHandlerProvider.getIssueHandler();
  39. public JIRAFixture() {
  40. jiraClient = JIRAClient.instance();
  41. tester = jiraClient.getFuncTestHelperFactory().getTester();
  42. selenium = JIRAClient.selenium;
  43. }
  44. /**
  45. * Connects to JIRA
  46. */
  47. public void connect() throws Exception {
  48. jiraClient.login();
  49. }
  50. public RemoteProject createProjectWithKeyAndNameAndLead(String key, String projectName, String ProjectLead) throws Exception {
  51. log.info("Creating project: "+key+" "+projectName);
  52. try {
  53. project = jiraClient.getService().createProject(jiraClient.getToken(), key, projectName, "", null, ProjectLead, new RemotePermissionScheme(null, new Long(0), null, null, null), null, null);
  54. } catch (RemoteException re) { // Let's add the user to the user map in case he already exists. This will cause the teardown to delete the user.
  55. log.error("Failed to create project "+key, re);
  56. if( re.getFaultReason().equals("com.atlassian.jira.rpc.exception.RemoteValidationException: A project with that project key already exists.")) {
  57. projectMap.put(key, null);
  58. }
  59. throw re;
  60. }
  61. projectMap.put(project.getKey(), project);
  62. return project;
  63. }
  64. public void removeProject(String key) throws Exception {
  65. log.info("Deleting project: "+key+" "+projectMap.get(key));
  66. jiraClient.getService().deleteProject(jiraClient.getToken(), key);
  67. }
  68. /**
  69. * Creates user and adds the user to the developers role (to enable editing permission)
  70. * @param name
  71. * @throws Exception
  72. */
  73. public void createUserWithUsername(String name) throws Exception {
  74. log.info("Creating user: "+name);
  75. RemoteUser user = null;
  76. try {
  77. user = jiraClient.getService().createUser(jiraClient.getToken(), name, name+"-pw", "Test User "+name, name+"@nowhere.test");
  78. } catch (RemoteException re) { // Let's add the user to the user map in case he already exists. This will cause the teardown to delete the user.
  79. log.error("Failed to create user "+name, re);
  80. if( re.getFaultReason().equals("com.atlassian.jira.rpc.exception.RemoteValidationException: user for this name already exists, please choose a different user name")) {
  81. userMap.put(name, null);
  82. }
  83. throw re;
  84. }
  85. userMap.put(name, user);
  86. log.debug("Adding "+name+" to "+JIRA_DEVELOPERS);
  87. addUserToGroup(name, JIRA_DEVELOPERS);
  88. }
  89. public void removeUser(String name) throws Exception {
  90. log.info("Removing user: "+name);
  91. jiraClient.getService().deleteUser(jiraClient.getToken(), name);
  92. }
  93. public void addUserToGroup(String userName, String groupName) throws Exception {
  94. log.info("Adding user: "+userName+" to group: "+groupName);
  95. RemoteGroup group = jiraClient.getService().getGroup(jiraClient.getToken(), groupName);
  96. RemoteUser user = jiraClient.getService().getUser(jiraClient.getToken(), userName);
  97. jiraClient.getService().addUserToGroup(jiraClient.getToken(), group, user);
  98. }
  99. public void removeUserFromGroup(String userName, String groupName) throws Exception {
  100. log.info("Removing user: "+userName+" from group: "+groupName);
  101. RemoteGroup group = jiraClient.getService().getGroup(jiraClient.getToken(), groupName);
  102. RemoteUser user = jiraClient.getService().getUser(jiraClient.getToken(), userName);
  103. jiraClient.getService().removeUserFromGroup(jiraClient.getToken(), group, user);
  104. }
  105. public void login(String username, String password) {
  106. selenium.open("login.jsp");
  107. selenium.type("login-form-username", username);
  108. selenium.type("login-form-password", password);
  109. selenium.click("login-form-submit");
  110. }
  111. //Needs to exterminate all data before each test to ensure a stable test environment
  112. public void cleanData() {
  113. Iterator<String> projectKeyIterator = projectMap.keySet().iterator();
  114. while (projectKeyIterator.hasNext()) {
  115. String projectKey = projectKeyIterator.next();
  116. try {
  117. removeProject(projectKey);
  118. } catch (Exception e) {
  119. log.error("Failed to clean project: "+projectKey);
  120. }
  121. }
  122. Iterator<String> userIDIterator = userMap.keySet().iterator();
  123. while (userIDIterator.hasNext()) {
  124. String userID = userIDIterator.next();
  125. try {
  126. removeUser(userID);
  127. } catch (Exception e) {
  128. log.error("Failed to clean user: "+userID);
  129. }
  130. }
  131. }
  132. public JIRAClient getJiraClient() {
  133. return jiraClient;
  134. }
  135. /**
  136. * Loads data from the indicated backup file into JIRA. The file should be located in the <code>jira.xml.data.location</code> directory indicated in the
  137. * localtest.properties file (Generated during the pre-integration-test phase and located in the test-classes folder ).
  138. *
  139. * Handles two cases, either a initial setup of JIRA, or a XML restore in a already configured JIRA.
  140. */
  141. public void loadData (String fileName) {
  142. String restoreSourceFile = jiraClient.getFuncTestHelperFactory().getEnvironmentData().getXMLDataLocation().getAbsolutePath() + FS + fileName;
  143. String JIRAHomeDir = System.getProperty("jiraserver.deploy.dir") + FS + "data" + FS ;
  144. // Copy
  145. File restorefile = new File(JIRAHomeDir + FS + "import" + FS + fileName);
  146. try {
  147. FileUtils.copyFile(new File(restoreSourceFile), restorefile);
  148. } catch (IOException e1) {
  149. throw new RuntimeException("Failed to restore data from "+restoreSourceFile, e1);
  150. }
  151. login("admin", "admin");
  152. log.info("Restoring data from '" + restoreSourceFile + "'");
  153. selenium.open("secure/admin/XmlRestore!default.jspa");
  154. selenium.waitForPageToLoad("1000");
  155. if (System.getProperty("jira.deploy.version", "4.2.2-b589").equals("4.2.2-b589")) {
  156. selenium.type("filename", fileName);
  157. } else {
  158. selenium.type("filename", restoreSourceFile);
  159. }
  160. selenium.click("restore_submit");
  161. selenium.waitForPageToLoad("1200000");
  162. // try {
  163. // log.info("Restoring data from '" + restoreSourceFile + "'");
  164. //
  165. // if (tester.getDialog().getResponsePageTitle().indexOf(webText.getString("jira_introduction_screen_title")) != -1) {
  166. // tester.gotoPage("secure/SetupImport!default.jspa");
  167. // tester.setWorkingForm("jiraform");
  168. // tester.setFormElement("filename", restoreSourceFile);
  169. // if (tester.getDialog().isTextInResponse("indexPath")) { //JIRA 4.1 and earlier
  170. // tester.setFormElement("indexPath", JIRAHomeDir + FS + "indexes");
  171. // }
  172. // tester.submit();
  173. // } else {
  174. // jiraClient.getFuncTestHelperFactory().getNavigation().login("admin", "admin", true);// GUI login with default user
  175. // tester.gotoPage("secure/Dashboard.jspa");
  176. // tester.gotoPage("secure/admin/XmlRestore!default.jspa");
  177. // tester.setWorkingForm("jiraform");
  178. // if (System.getProperty("jira.deploy.version", "4.2.2").equals("4.2.2")) {
  179. // tester.setFormElement("filename", fileName);
  180. // } else {
  181. // tester.setFormElement("filename", restoreSourceFile);
  182. // }
  183. // tester.submit();
  184. // jiraClient.getFuncTestHelperFactory().getNavigation().login("admin", "admin", true);// Relogin after import
  185. // }
  186. // } catch(AssertionFailedError e) {
  187. // if (log.isDebugEnabled()) log.debug("Received unexpected text: "+tester.getDialog().getResponseText());
  188. // throw new RuntimeException("Failed to restore data from "+restoreSourceFile, e);
  189. // }
  190. }
  191. }