PageRenderTime 34ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/jira-project/jira-functional-tests/jira-func-tests-legacy/src/main/java/com/atlassian/jira/functest/framework/rules/RestRule.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 191 lines | 142 code | 36 blank | 13 comment | 4 complexity | df424944a4ae77141cb3c36ffab4ec72 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.functest.framework.rules;
  2. import com.atlassian.jira.functest.framework.BaseJiraFuncTest;
  3. import com.atlassian.jira.functest.framework.FuncTestCase;
  4. import com.atlassian.jira.util.Supplier;
  5. import com.atlassian.jira.util.collect.CollectionBuilder;
  6. import com.atlassian.jira.util.json.JSONException;
  7. import com.atlassian.jira.util.json.JSONObject;
  8. import com.atlassian.jira.webtests.util.JIRAEnvironmentData;
  9. import com.google.common.base.Function;
  10. import com.google.common.collect.Iterables;
  11. import com.google.common.collect.Lists;
  12. import com.meterware.httpunit.GetMethodWebRequest;
  13. import com.meterware.httpunit.HeaderOnlyWebRequest;
  14. import com.meterware.httpunit.HttpUnitOptions;
  15. import com.meterware.httpunit.PostMethodWebRequest;
  16. import com.meterware.httpunit.PutMethodWebRequest;
  17. import com.meterware.httpunit.WebResponse;
  18. import net.sourceforge.jwebunit.WebTester;
  19. import org.apache.commons.lang.StringUtils;
  20. import org.junit.rules.ExternalResource;
  21. import org.xml.sax.SAXException;
  22. import java.io.ByteArrayInputStream;
  23. import java.io.IOException;
  24. import java.net.HttpURLConnection;
  25. import java.net.URI;
  26. import java.net.URISyntaxException;
  27. import java.net.URLConnection;
  28. import java.util.Arrays;
  29. import java.util.Collections;
  30. import java.util.List;
  31. import java.util.Map;
  32. import static java.util.Arrays.asList;
  33. /**
  34. * Rule for using REST.
  35. *
  36. * @since v4.2
  37. * @deprecated inject {@link com.atlassian.jira.functest.framework.FuncTestRestClient} instead.
  38. */
  39. //DEBT-170: this should be moven to jira-func-tests-legacy in a long run
  40. public class RestRule extends ExternalResource {
  41. private final Supplier<WebTester> testerSupplier;
  42. private final Supplier<JIRAEnvironmentData> environmentDataSupplier;
  43. /**
  44. * The base URL used during func tests.
  45. */
  46. private String baseUrl;
  47. public RestRule(FuncTestCase testCase) {
  48. this.testerSupplier = testCase::getTester;
  49. this.environmentDataSupplier = testCase::getEnvironmentData;
  50. }
  51. public RestRule(final BaseJiraFuncTest baseJiraFuncTest) {
  52. this.testerSupplier = baseJiraFuncTest::getTester;
  53. this.environmentDataSupplier = baseJiraFuncTest::getEnvironmentData;
  54. }
  55. public String getBaseUrl() {
  56. return baseUrl;
  57. }
  58. public String getBaseUrlPlus(String... paths) {
  59. return getBaseUrlPlus(Arrays.asList(paths));
  60. }
  61. public String getBaseUrlPlus(Iterable<String> paths) {
  62. Iterable<String> pathsNoLeadingSlashes = Iterables.transform(paths, new Function<String, String>() {
  63. @Override
  64. public String apply(String path) {
  65. // remove leading slashes so we don't end up with duplicates
  66. return path.startsWith("/") ? path.substring(1) : path;
  67. }
  68. });
  69. String path = pathsNoLeadingSlashes != null ? StringUtils.join(Lists.newArrayList(pathsNoLeadingSlashes), '/') : "";
  70. return String.format("%s/%s", getBaseUrl(), path);
  71. }
  72. public URI getBaseUriPlus(Iterable<String> paths) {
  73. try {
  74. return new URI(getBaseUrlPlus(paths));
  75. } catch (URISyntaxException e) {
  76. throw new RuntimeException(e);
  77. }
  78. }
  79. public URI getBaseUriPlus(String... paths) {
  80. return getBaseUriPlus(Arrays.asList(paths));
  81. }
  82. public String getRestApiUrl(String... paths) {
  83. List<String> list = CollectionBuilder.<String>newBuilder("rest", "api", "2").addAll(paths).asList();
  84. return getBaseUrlPlus(list);
  85. }
  86. public URI getRestApiUri(String... paths) {
  87. return getRestApiUri(asList(paths));
  88. }
  89. public URI getRestApiUri(Iterable<String> paths) {
  90. List<String> all = Lists.newArrayList("rest", "api", "2");
  91. all.addAll(Lists.newArrayList(paths));
  92. return getBaseUriPlus(all);
  93. }
  94. public JSONObject getJSON(final String url, String... expand) throws JSONException {
  95. String queryString = (expand != null && expand.length > 0) ? ("?expand=" + StringUtils.join(expand, ',')) : "";
  96. testerSupplier.get().gotoPage(url + queryString);
  97. return new JSONObject(testerSupplier.get().getDialog().getResponseText());
  98. }
  99. public WebResponse GET(final String url) throws IOException, SAXException {
  100. return GET(url, Collections.<String, String>emptyMap());
  101. }
  102. public WebResponse GET(final String url, Map<String, String> headers) throws IOException, SAXException {
  103. testerSupplier.get().getDialog().getWebClient().setExceptionsThrownOnErrorStatus(false);
  104. HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
  105. for (Map.Entry<String, String> headerField : headers.entrySet()) {
  106. testerSupplier.get().getDialog().getWebClient().setHeaderField(headerField.getKey(), headerField.getValue());
  107. }
  108. final GetMethodWebRequest request = new GetMethodWebRequest(getBaseUrlPlus(url));
  109. return testerSupplier.get().getDialog().getWebClient().sendRequest(request);
  110. }
  111. public WebResponse DELETE(final String url) throws IOException, SAXException {
  112. testerSupplier.get().getDialog().getWebClient().setExceptionsThrownOnErrorStatus(false);
  113. HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
  114. final HeaderOnlyWebRequest delete = new HeaderOnlyWebRequest(getBaseUrlPlus(url)) {
  115. @Override
  116. public String getMethod() {
  117. return "DELETE";
  118. }
  119. // If you don't override this then the above getMethod() never gets called and the request goes through
  120. // as a GET. Thanks httpunit.
  121. @Override
  122. protected void completeRequest(final URLConnection connection) throws IOException {
  123. ((HttpURLConnection) connection).setRequestMethod(getMethod());
  124. super.completeRequest(connection);
  125. }
  126. };
  127. return testerSupplier.get().getDialog().getWebClient().sendRequest(delete);
  128. }
  129. public WebResponse POST(final String url, final JSONObject json) throws IOException, SAXException {
  130. return POST(url, json.toString());
  131. }
  132. public WebResponse POST(final String url, final String postBody) throws IOException, SAXException {
  133. testerSupplier.get().getDialog().getWebClient().setExceptionsThrownOnErrorStatus(false);
  134. HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
  135. final PostMethodWebRequest request = new PostMethodWebRequest(getBaseUrlPlus(url), new ByteArrayInputStream(postBody.getBytes()), "application/json");
  136. return testerSupplier.get().getDialog().getWebClient().sendRequest(request);
  137. }
  138. public WebResponse PUT(final String url, final JSONObject json) throws IOException, SAXException {
  139. return PUT(url, json.toString());
  140. }
  141. public WebResponse PUT(final String url, final String postBody) throws IOException, SAXException {
  142. testerSupplier.get().getDialog().getWebClient().setExceptionsThrownOnErrorStatus(false);
  143. HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
  144. final PutMethodWebRequest request = new PutMethodWebRequest(getBaseUrlPlus(url), new ByteArrayInputStream(postBody.getBytes()), "application/json");
  145. return testerSupplier.get().getDialog().getWebClient().sendRequest(request);
  146. }
  147. @Override
  148. public void before() {
  149. baseUrl = environmentDataSupplier.get().getBaseUrl().toExternalForm();
  150. }
  151. @Override
  152. public void after() {
  153. HttpUnitOptions.resetDefaultCharacterSet();
  154. }
  155. }