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

/jira-project/jira-functional-tests/jira-func-tests/src/main/java/com/atlassian/jira/functest/framework/admin/ViewServices.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 218 lines | 147 code | 32 blank | 39 comment | 12 complexity | a8f15538f69237ec1b51f057ff0b6f34 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.functest.framework.admin;
  2. import com.atlassian.jira.functest.framework.FunctTestConstants;
  3. import com.atlassian.jira.functest.framework.Navigation;
  4. import com.atlassian.jira.functest.framework.admin.services.EditService;
  5. import com.atlassian.jira.functest.framework.locator.XPathLocator;
  6. import com.atlassian.jira.webtests.table.HtmlTable;
  7. import com.google.common.collect.ImmutableSet;
  8. import com.meterware.httpunit.WebTable;
  9. import net.sourceforge.jwebunit.WebTester;
  10. import org.apache.commons.lang.StringUtils;
  11. import org.apache.commons.lang.builder.EqualsBuilder;
  12. import org.apache.commons.lang.builder.HashCodeBuilder;
  13. import org.w3c.dom.Node;
  14. import javax.inject.Inject;
  15. import java.util.HashSet;
  16. import java.util.Set;
  17. /**
  18. * Gives operations that can be called on the ViewServices page.
  19. *
  20. * @since v4.0
  21. */
  22. public class ViewServices implements FunctTestConstants {
  23. private static final String SERVICES_TABLE_ID = "tbl_services";
  24. private final WebTester tester;
  25. private final Navigation navigation;
  26. @Inject
  27. public ViewServices(final WebTester tester, final Navigation navigation) {
  28. this.tester = tester;
  29. this.navigation = navigation;
  30. }
  31. /**
  32. * Navigates to the ViewServices page.
  33. *
  34. * @return this instance of the page.
  35. */
  36. public ViewServices goTo() {
  37. navigation.gotoAdminSection(Navigation.AdminSection.SERVICES);
  38. return this;
  39. }
  40. /**
  41. * Adds a service to JIRA with default configuration parameters.
  42. *
  43. * @param service The service to be added.
  44. * @param delayInMinutes the delay between runs of this service specified in minutes.
  45. * @return this instance of the view services page.
  46. * @throws UnableToAddServiceException This is thrown if it was not possible to add the service.
  47. */
  48. public ViewServices add(final Service service, final String delayInMinutes) throws UnableToAddServiceException {
  49. tester.setWorkingForm(JIRA_FORM_NAME);
  50. tester.setFormElement("name", service.getName());
  51. tester.setFormElement("clazz", service.getServiceClass());
  52. tester.setFormElement("service.schedule.cronString", String.format("0 0/%1s * * * ?", delayInMinutes));
  53. tester.submit();
  54. // submit the edit form as well
  55. if (tester.getDialog().hasSubmitButton("Update")) {
  56. tester.submit("Update");
  57. } else {
  58. throw new UnableToAddServiceException
  59. (
  60. String.format
  61. (
  62. "Unable to add a service with name: %s and class: %s",
  63. service.getName(), service.getServiceClass()
  64. )
  65. );
  66. }
  67. return this;
  68. }
  69. public EditService edit(Service service) {
  70. final XPathLocator xPathLocator = new XPathLocator(tester, "//*[@id='tbl_services']//tr[contains(@id,'service')]");
  71. for (Node node : xPathLocator.getNodes()) {
  72. final XPathLocator serviceNameLocator = new XPathLocator(node, ".//*[contains(@id,'service-name')]");
  73. if (serviceNameLocator.getText().equals(service.getName())) {
  74. String serviceId = StringUtils.difference("service-", node.getAttributes().getNamedItem("id").getNodeValue());
  75. tester.clickLink("edit_" + serviceId);
  76. return new EditService(this, tester);
  77. }
  78. }
  79. throw new AssertionError("No service could be found with the name: " + service.getName());
  80. }
  81. /**
  82. * Gets the set of service configurations displayed on the page.
  83. *
  84. * @return A set of service configurations.
  85. */
  86. public Set<Service> list() {
  87. final Set<Service> services = new HashSet<Service>();
  88. final XPathLocator xPathLocator = new XPathLocator(tester, "//*[@id='tbl_services']//tr[contains(@id,'service')]");
  89. for (Node node : xPathLocator.getNodes()) {
  90. final XPathLocator serviceNameLocator = new XPathLocator(node, ".//*[contains(@id,'service-name')]");
  91. final XPathLocator serviceClassLocator = new XPathLocator(node, ".//*[contains(@id,'service-class')]");
  92. services.add(new Service(serviceNameLocator.getText(), serviceClassLocator.getText()));
  93. }
  94. return ImmutableSet.copyOf(services);
  95. }
  96. /**
  97. * Gets the id of a named service
  98. */
  99. public long getIdForServiceName(String name) {
  100. final XPathLocator xPathLocator = new XPathLocator(tester, "//*[@id='tbl_services']//tr[contains(@id,'service')]");
  101. for (Node node : xPathLocator.getNodes()) {
  102. final XPathLocator serviceNameLocator = new XPathLocator(node, ".//*[contains(@id,'service-name')]");
  103. if (serviceNameLocator.getText().equals(name)) {
  104. String serviceId = StringUtils.difference("service-", node.getAttributes().getNamedItem("id").getNodeValue());
  105. return Long.parseLong(serviceId);
  106. }
  107. }
  108. throw new AssertionError("No service could be found with the name: " + name);
  109. }
  110. /**
  111. * Clicks the <strong>Edit</strong> link for the given service name.
  112. *
  113. * @param serviceName The service name.
  114. */
  115. public void clickEdit(final String serviceName) {
  116. clickLink(serviceName, "Edit");
  117. }
  118. /**
  119. * Clicks the <strong>Delete</strong> link for the given service name.
  120. *
  121. * @param serviceName The service name.
  122. */
  123. public void clickDelete(final String serviceName) {
  124. clickLink(serviceName, "Delete");
  125. }
  126. private void clickLink(final String serviceName, final String linkName) {
  127. // We will likely already be on the View Services page - try get the services table.
  128. WebTable table = tester.getDialog().getWebTableBySummaryOrId(SERVICES_TABLE_ID);
  129. if (table == null) {
  130. // We were not on the View Services page. Go there and try again.
  131. goTo();
  132. table = tester.getDialog().getWebTableBySummaryOrId(SERVICES_TABLE_ID);
  133. }
  134. HtmlTable htmlTable = new HtmlTable(table);
  135. HtmlTable.Row row = htmlTable.findRowWhereCellStartsWith(1, serviceName);
  136. navigation.clickLinkInTableCell(table, row.getRowIndex(), 4, linkName);
  137. }
  138. public static class Service {
  139. private final String name;
  140. private final String serviceClass;
  141. public Service(final String name, final String serviceClass) {
  142. this.name = name;
  143. this.serviceClass = serviceClass;
  144. }
  145. public String getName() {
  146. return name;
  147. }
  148. public String getServiceClass() {
  149. return serviceClass;
  150. }
  151. @Override
  152. public boolean equals(Object obj) {
  153. if (this == obj) {
  154. return true;
  155. }
  156. if (!(obj instanceof Service)) {
  157. return false;
  158. }
  159. Service rhs = (Service) obj;
  160. return new EqualsBuilder().
  161. append(name, rhs.name).
  162. append(serviceClass, rhs.getServiceClass()).
  163. isEquals();
  164. }
  165. @Override
  166. public int hashCode() {
  167. return new HashCodeBuilder(17, 31).
  168. append(name).
  169. append(serviceClass).
  170. toHashCode();
  171. }
  172. }
  173. public class UnableToAddServiceException extends Exception {
  174. public UnableToAddServiceException() {
  175. }
  176. public UnableToAddServiceException(String message) {
  177. super(message);
  178. }
  179. public UnableToAddServiceException(String message, Throwable cause) {
  180. super(message, cause);
  181. }
  182. public UnableToAddServiceException(Throwable cause) {
  183. super(cause);
  184. }
  185. }
  186. }