PageRenderTime 23ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/test/src/test/java/it/AsynchronousProjectRestClientReadOnlyTest.java

https://bitbucket.org/trevmex/jira-rest-java-client
Java | 221 lines | 173 code | 27 blank | 21 comment | 5 complexity | 8cae1033ffb81d263375af4a48f7f2e6 MD5 | raw file
  1. /*
  2. * Copyright (C) 2010 Atlassian
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package it;
  17. import com.atlassian.jira.nimblefunctests.annotation.JiraBuildNumberDependent;
  18. import com.atlassian.jira.nimblefunctests.annotation.RestoreOnce;
  19. import com.atlassian.jira.rest.client.IntegrationTestUtil;
  20. import com.atlassian.jira.rest.client.api.AddressableEntity;
  21. import com.atlassian.jira.rest.client.api.OptionalIterable;
  22. import com.atlassian.jira.rest.client.TestUtil;
  23. import com.atlassian.jira.rest.client.api.domain.BasicProject;
  24. import com.atlassian.jira.rest.client.api.domain.IssueType;
  25. import com.atlassian.jira.rest.client.api.domain.Priority;
  26. import com.atlassian.jira.rest.client.api.domain.Project;
  27. import com.atlassian.jira.rest.client.api.domain.Resolution;
  28. import com.atlassian.jira.rest.client.internal.ServerVersionConstants;
  29. import com.atlassian.jira.rest.client.internal.json.TestConstants;
  30. import com.google.common.base.Function;
  31. import com.google.common.base.Predicate;
  32. import com.google.common.collect.Iterables;
  33. import org.junit.Assert;
  34. import org.junit.Test;
  35. import javax.annotation.Nullable;
  36. import javax.ws.rs.core.Response;
  37. import java.net.URISyntaxException;
  38. import java.util.Iterator;
  39. import static com.atlassian.jira.rest.client.internal.ServerVersionConstants.BN_JIRA_5;
  40. import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
  41. import static org.junit.Assert.*;
  42. /**
  43. * Those tests mustn't change anything on server side, as jira is restored only once
  44. */
  45. // Ignore "May produce NPE" warnings, as we know what we are doing in tests
  46. @SuppressWarnings("ConstantConditions")
  47. @RestoreOnce(TestConstants.DEFAULT_JIRA_DUMP_FILE)
  48. public class AsynchronousProjectRestClientReadOnlyTest extends AbstractAsynchronousRestClientTest {
  49. @Test
  50. public void testGetNonExistingProject() throws Exception {
  51. final String nonExistingProjectKey = "NONEXISTINGPROJECTKEY";
  52. TestUtil.assertErrorCode(Response.Status.NOT_FOUND, "No project could be found with key '" +
  53. nonExistingProjectKey + "'.", new Runnable() {
  54. @Override
  55. public void run() {
  56. client.getProjectClient().getProject(nonExistingProjectKey).claim();
  57. }
  58. });
  59. }
  60. @Test
  61. public void testGetProject() throws URISyntaxException {
  62. final Project project = client.getProjectClient().getProject("TST").claim();
  63. assertEquals("TST", project.getKey());
  64. assertEquals(IntegrationTestUtil.USER_ADMIN_60, project.getLead());
  65. assertEquals(2, Iterables.size(project.getVersions()));
  66. assertEquals(2, Iterables.size(project.getComponents()));
  67. final OptionalIterable<IssueType> issueTypes = project.getIssueTypes();
  68. if (isJira4x4OrNewer()) {
  69. assertTrue(issueTypes.isSupported());
  70. final Iterator<IssueType> issueTypesIterator = issueTypes.iterator();
  71. assertTrue(issueTypesIterator.hasNext());
  72. final IssueType it = issueTypesIterator.next();
  73. if (isJira5xOrNewer()) {
  74. assertEquals(Long.valueOf(1), it.getId());
  75. } else {
  76. assertNull(it.getId());
  77. }
  78. assertEquals(it.getName(), "Bug");
  79. } else {
  80. assertFalse(issueTypes.isSupported());
  81. }
  82. }
  83. @Test
  84. public void testGetRestrictedProject() {
  85. final Project project = client.getProjectClient().getProject("RST").claim();
  86. assertEquals("RST", project.getKey());
  87. setClient(TestConstants.USER1_USERNAME, TestConstants.USER1_PASSWORD);
  88. client.getProjectClient().getProject("TST").claim();
  89. // @todo when JRADEV-3519 - instead of NOT_FOUND, FORBIDDEN code should be returned by JIRA
  90. final String message = getCannotViewProjectErrorMessage("RST");
  91. TestUtil.assertErrorCode(Response.Status.NOT_FOUND, message, new Runnable() {
  92. @Override
  93. public void run() {
  94. client.getProjectClient().getProject("RST").claim();
  95. }
  96. });
  97. }
  98. private String getCannotViewProjectErrorMessage(String key) {
  99. return isJira4x4OrNewer()
  100. ? (isJira5xOrNewer() ? ("No project could be found with key '" + key + "'.") : "You cannot view this project.")
  101. : "You must have the browse project permission to view this project.";
  102. }
  103. @Test
  104. public void testGetAnonymouslyProject() {
  105. // @todo when JRADEV-3519 - instead of NOT_FOUND, UNAUTHORIZED code should be returned by JIRA
  106. setAnonymousMode();
  107. TestUtil.assertErrorCode(Response.Status.NOT_FOUND, getCannotViewProjectErrorMessage("RST"), new Runnable() {
  108. @Override
  109. public void run() {
  110. client.getProjectClient().getProject("RST").claim();
  111. }
  112. });
  113. TestUtil.assertErrorCode(Response.Status.NOT_FOUND, getCannotViewProjectErrorMessage("TST"), new Runnable() {
  114. @Override
  115. public void run() {
  116. client.getProjectClient().getProject("TST").claim();
  117. }
  118. });
  119. final Project project = client.getProjectClient().getProject("ANNON").claim();
  120. assertEquals("ANNON", project.getKey());
  121. }
  122. @Test
  123. public void testGetAllProject() {
  124. if (!isGetAllProjectsSupported()) {
  125. return;
  126. }
  127. final Iterable<BasicProject> projects = client.getProjectClient().getAllProjects().claim();
  128. assertEquals(4, Iterables.size(projects));
  129. final BasicProject tst = Iterables.find(projects, new Predicate<BasicProject>() {
  130. @Override
  131. public boolean apply(@Nullable BasicProject input) {
  132. return input.getKey().equals("TST");
  133. }
  134. });
  135. assertTrue(tst.getSelf().toString().contains(jiraRestRootUri.toString()));
  136. setAnonymousMode();
  137. final Iterable<BasicProject> anonymouslyAccessibleProjects = client.getProjectClient().getAllProjects().claim();
  138. assertEquals(2, Iterables.size(anonymouslyAccessibleProjects));
  139. final Iterable<String> projectsKeys = Iterables
  140. .transform(anonymouslyAccessibleProjects, new Function<BasicProject, String>() {
  141. @Override
  142. public String apply(BasicProject project) {
  143. return project.getKey();
  144. }
  145. });
  146. Assert.assertThat(projectsKeys, containsInAnyOrder("ANNON", "ANONEDIT"));
  147. setUser1();
  148. assertEquals(3, Iterables.size(client.getProjectClient().getAllProjects().claim()));
  149. }
  150. private boolean isGetAllProjectsSupported() {
  151. return client.getMetadataClient().getServerInfo().claim().getBuildNumber() >= ServerVersionConstants.BN_JIRA_4_3;
  152. }
  153. @Test
  154. @JiraBuildNumberDependent(BN_JIRA_5)
  155. public void testGetPriorities() {
  156. final Iterable<Priority> priorities = client.getMetadataClient().getPriorities().claim();
  157. assertEquals(5, Iterables.size(priorities));
  158. final Priority priority = findEntityBySelfAddressSuffix(priorities, "/1");
  159. assertEquals(Long.valueOf(1), priority.getId());
  160. assertEquals("Blocker", priority.getName());
  161. assertEquals("Blocks development and/or testing work, production could not run.", priority.getDescription());
  162. assertNotNull(priority.getSelf());
  163. }
  164. @Test
  165. @JiraBuildNumberDependent(BN_JIRA_5)
  166. public void testGetIssueTypes() {
  167. final Iterable<IssueType> issueTypes = client.getMetadataClient().getIssueTypes().claim();
  168. assertEquals(5, Iterables.size(issueTypes));
  169. final IssueType issueType = findEntityBySelfAddressSuffix(issueTypes, "/5");
  170. assertEquals("Sub-task", issueType.getName());
  171. assertEquals("The sub-task of the issue", issueType.getDescription());
  172. assertEquals(Long.valueOf(5), issueType.getId());
  173. assertTrue(issueType.isSubtask());
  174. assertNotNull(issueType.getSelf());
  175. }
  176. @Test
  177. @JiraBuildNumberDependent(BN_JIRA_5)
  178. public void testGetResolutions() {
  179. final Iterable<Resolution> resolutions = client.getMetadataClient().getResolutions().claim();
  180. assertEquals(5, Iterables.size(resolutions));
  181. final Resolution resolution = findEntityBySelfAddressSuffix(resolutions, "/1");
  182. assertEquals("Fixed", resolution.getName());
  183. assertEquals("A fix for this issue is checked into the tree and tested.", resolution.getDescription());
  184. assertNotNull(resolution.getSelf());
  185. }
  186. private <T extends AddressableEntity> T findEntityBySelfAddressSuffix(final Iterable<T> entities, final String suffix) {
  187. return Iterables.find(entities, new Predicate<T>() {
  188. @Override
  189. public boolean apply(T input) {
  190. return input.getSelf().toString().endsWith(suffix);
  191. }
  192. });
  193. }
  194. }