View Javadoc

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  
17  package it;
18  
19  import com.atlassian.jira.nimblefunctests.annotation.JiraBuildNumberDependent;
20  import com.atlassian.jira.rest.client.IntegrationTestUtil;
21  import com.atlassian.jira.rest.client.TestUtil;
22  import com.atlassian.jira.rest.client.api.AddressableEntity;
23  import com.atlassian.jira.rest.client.api.OptionalIterable;
24  import com.atlassian.jira.rest.client.api.domain.BasicProject;
25  import com.atlassian.jira.rest.client.api.domain.IssueType;
26  import com.atlassian.jira.rest.client.api.domain.Priority;
27  import com.atlassian.jira.rest.client.api.domain.Project;
28  import com.atlassian.jira.rest.client.api.domain.Resolution;
29  import com.atlassian.jira.rest.client.internal.ServerVersionConstants;
30  import com.atlassian.jira.rest.client.internal.json.TestConstants;
31  import com.google.common.base.Function;
32  import com.google.common.base.Predicate;
33  import com.google.common.collect.Iterables;
34  import org.junit.Assert;
35  import org.junit.Before;
36  import org.junit.Test;
37  
38  import javax.annotation.Nullable;
39  import javax.ws.rs.core.Response;
40  import java.net.URISyntaxException;
41  import java.util.Iterator;
42  
43  import static com.atlassian.jira.rest.client.internal.ServerVersionConstants.BN_JIRA_5;
44  import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.assertFalse;
47  import static org.junit.Assert.assertNotNull;
48  import static org.junit.Assert.assertNull;
49  import static org.junit.Assert.assertTrue;
50  
51  /**
52   * Those tests mustn't change anything on server side, as jira is restored only once
53   */
54  // Ignore "May produce NPE" warnings, as we know what we are doing in tests
55  @SuppressWarnings("ConstantConditions")
56  public class AsynchronousProjectRestClientReadOnlyTest extends AbstractAsynchronousRestClientTest {
57  
58      private static boolean alreadyRestored;
59  
60      @Before
61      public void setup() {
62          if (!alreadyRestored) {
63              IntegrationTestUtil.restoreAppropriateJiraData(TestConstants.DEFAULT_JIRA_DUMP_FILE, administration);
64              alreadyRestored = true;
65          }
66      }
67  
68      @Test
69      public void testGetNonExistingProject() throws Exception {
70          final String nonExistingProjectKey = "NONEXISTINGPROJECTKEY";
71          TestUtil.assertErrorCode(Response.Status.NOT_FOUND, "No project could be found with key '" +
72                  nonExistingProjectKey + "'.", new Runnable() {
73              @Override
74              public void run() {
75                  client.getProjectClient().getProject(nonExistingProjectKey).claim();
76              }
77          });
78      }
79  
80      @Test
81      public void testGetProject() throws URISyntaxException {
82          final Project project = client.getProjectClient().getProject("TST").claim();
83          assertEquals("TST", project.getKey());
84          assertEquals(Long.valueOf(10000), project.getId());
85          assertEquals(IntegrationTestUtil.USER_ADMIN_60, project.getLead());
86          assertEquals(2, Iterables.size(project.getVersions()));
87          assertEquals(2, Iterables.size(project.getComponents()));
88          final OptionalIterable<IssueType> issueTypes = project.getIssueTypes();
89          if (isJira4x4OrNewer()) {
90              assertTrue(issueTypes.isSupported());
91              final Iterator<IssueType> issueTypesIterator = issueTypes.iterator();
92              assertTrue(issueTypesIterator.hasNext());
93              final IssueType it = issueTypesIterator.next();
94              if (isJira5xOrNewer()) {
95                  assertEquals(Long.valueOf(1), it.getId());
96              } else {
97                  assertNull(it.getId());
98              }
99              assertEquals(it.getName(), "Bug");
100         } else {
101             assertFalse(issueTypes.isSupported());
102         }
103     }
104 
105     @Test
106     public void testGetRestrictedProject() {
107         final Project project = client.getProjectClient().getProject("RST").claim();
108         assertEquals("RST", project.getKey());
109 
110         setClient(TestConstants.USER1_USERNAME, TestConstants.USER1_PASSWORD);
111         client.getProjectClient().getProject("TST").claim();
112         // @todo when JRADEV-3519 - instead of NOT_FOUND, FORBIDDEN code should be returned by JIRA
113         final String message = getCannotViewProjectErrorMessage("RST");
114         TestUtil.assertErrorCode(Response.Status.NOT_FOUND, message, new Runnable() {
115             @Override
116             public void run() {
117                 client.getProjectClient().getProject("RST").claim();
118             }
119         });
120     }
121 
122     private String getCannotViewProjectErrorMessage(String key) {
123         return isJira4x4OrNewer()
124                 ? (isJira5xOrNewer() ? ("No project could be found with key '" + key + "'.") : "You cannot view this project.")
125                 : "You must have the browse project permission to view this project.";
126     }
127 
128     @Test
129     public void testGetAnonymouslyProject() {
130         // @todo when JRADEV-3519 - instead of NOT_FOUND, UNAUTHORIZED code should be returned by JIRA
131         setAnonymousMode();
132         TestUtil.assertErrorCode(Response.Status.NOT_FOUND, getCannotViewProjectErrorMessage("RST"), new Runnable() {
133             @Override
134             public void run() {
135                 client.getProjectClient().getProject("RST").claim();
136             }
137         });
138 
139         TestUtil.assertErrorCode(Response.Status.NOT_FOUND, getCannotViewProjectErrorMessage("TST"), new Runnable() {
140             @Override
141             public void run() {
142                 client.getProjectClient().getProject("TST").claim();
143             }
144         });
145 
146         final Project project = client.getProjectClient().getProject("ANNON").claim();
147         assertEquals("ANNON", project.getKey());
148 
149     }
150 
151     @Test
152     public void testGetAllProject() {
153         if (!isGetAllProjectsSupported()) {
154             return;
155         }
156 
157         final Iterable<BasicProject> projects = client.getProjectClient().getAllProjects().claim();
158         assertEquals(4, Iterables.size(projects));
159         final BasicProject tst = Iterables.find(projects, new Predicate<BasicProject>() {
160             @Override
161             public boolean apply(@Nullable BasicProject input) {
162                 return input.getKey().equals("TST");
163             }
164         });
165         assertTrue(tst.getSelf().toString().contains(jiraRestRootUri.toString()));
166 
167         setAnonymousMode();
168         final Iterable<BasicProject> anonymouslyAccessibleProjects = client.getProjectClient().getAllProjects().claim();
169         assertEquals(2, Iterables.size(anonymouslyAccessibleProjects));
170 
171         final Iterable<String> projectsKeys = Iterables
172                 .transform(anonymouslyAccessibleProjects, new Function<BasicProject, String>() {
173                     @Override
174                     public String apply(BasicProject project) {
175                         return project.getKey();
176                     }
177                 });
178         Assert.assertThat(projectsKeys, containsInAnyOrder("ANNON", "ANONEDIT"));
179 
180         setUser1();
181         assertEquals(3, Iterables.size(client.getProjectClient().getAllProjects().claim()));
182     }
183 
184     private boolean isGetAllProjectsSupported() {
185         return client.getMetadataClient().getServerInfo().claim().getBuildNumber() >= ServerVersionConstants.BN_JIRA_4_3;
186     }
187 
188     @Test
189     @JiraBuildNumberDependent(BN_JIRA_5)
190     public void testGetPriorities() {
191         final Iterable<Priority> priorities = client.getMetadataClient().getPriorities().claim();
192         assertEquals(5, Iterables.size(priorities));
193 
194         final Priority priority = findEntityBySelfAddressSuffix(priorities, "/1");
195         assertEquals(Long.valueOf(1), priority.getId());
196         assertEquals("Blocker", priority.getName());
197         assertEquals("Blocks development and/or testing work, production could not run.", priority.getDescription());
198         assertNotNull(priority.getSelf());
199     }
200 
201     @Test
202     @JiraBuildNumberDependent(BN_JIRA_5)
203     public void testGetIssueTypes() {
204         final Iterable<IssueType> issueTypes = client.getMetadataClient().getIssueTypes().claim();
205         assertEquals(5, Iterables.size(issueTypes));
206 
207         final IssueType issueType = findEntityBySelfAddressSuffix(issueTypes, "/5");
208         assertEquals("Sub-task", issueType.getName());
209         assertEquals("The sub-task of the issue", issueType.getDescription());
210         assertEquals(Long.valueOf(5), issueType.getId());
211         assertTrue(issueType.isSubtask());
212         assertNotNull(issueType.getSelf());
213     }
214 
215     @Test
216     @JiraBuildNumberDependent(BN_JIRA_5)
217     public void testGetResolutions() {
218         final Iterable<Resolution> resolutions = client.getMetadataClient().getResolutions().claim();
219         assertEquals(5, Iterables.size(resolutions));
220         final Resolution resolution = findEntityBySelfAddressSuffix(resolutions, "/1");
221         assertEquals("Fixed", resolution.getName());
222         assertEquals("A fix for this issue is checked into the tree and tested.", resolution.getDescription());
223         assertNotNull(resolution.getSelf());
224     }
225 
226     private <T extends AddressableEntity> T findEntityBySelfAddressSuffix(final Iterable<T> entities, final String suffix) {
227         return Iterables.find(entities, new Predicate<T>() {
228             @Override
229             public boolean apply(T input) {
230                 return input.getSelf().toString().endsWith(suffix);
231             }
232         });
233     }
234 
235 }