1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.domain.AssigneeType;
23 import com.atlassian.jira.rest.client.api.domain.BasicComponent;
24 import com.atlassian.jira.rest.client.api.domain.Component;
25 import com.atlassian.jira.rest.client.api.domain.EntityHelper;
26 import com.atlassian.jira.rest.client.api.domain.input.ComponentInput;
27 import com.atlassian.jira.rest.client.internal.ServerVersionConstants;
28 import com.atlassian.jira.rest.client.internal.json.TestConstants;
29 import com.google.common.collect.Iterables;
30 import org.junit.Before;
31 import org.junit.Test;
32
33 import javax.ws.rs.core.Response;
34
35 import static com.atlassian.jira.rest.client.api.domain.EntityHelper.findEntityByName;
36 import static com.atlassian.jira.rest.client.internal.ServerVersionConstants.BN_JIRA_4_4;
37 import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
38 import static org.junit.Assert.assertEquals;
39 import static org.junit.Assert.assertFalse;
40 import static org.junit.Assert.assertNotNull;
41 import static org.junit.Assert.assertNull;
42 import static org.junit.Assert.assertThat;
43 import static org.junit.Assert.assertTrue;
44
45 public class AsynchronousComponentRestClientTest extends AbstractAsynchronousRestClientTest {
46
47 @Before
48 public void setup() {
49 IntegrationTestUtil.restoreAppropriateJiraData(TestConstants.DEFAULT_JIRA_DUMP_FILE, administration);
50 }
51
52 @Test
53 public void testGetComponent() throws Exception {
54 final BasicComponent basicComponent = findEntityByName(client.getProjectClient().getProject("TST").claim()
55 .getComponents(), "Component A");
56 final Component component = client.getComponentClient().getComponent(basicComponent.getSelf()).claim();
57 assertEquals("Component A", component.getName());
58 assertEquals("this is some description of component A", component.getDescription());
59 assertEquals(IntegrationTestUtil.USER_ADMIN_60, component.getLead());
60 }
61
62 @Test
63 @JiraBuildNumberDependent(BN_JIRA_4_4)
64 public void testGetComponentOnJira4xOrNewerShouldContainNotNullId() throws Exception {
65 final BasicComponent basicComponent = findEntityByName(client.getProjectClient().getProject("TST").claim()
66 .getComponents(), "Component A");
67 final Component component = client.getComponentClient().getComponent(basicComponent.getSelf()).claim();
68 assertEquals("Component A", component.getName());
69 assertEquals("this is some description of component A", component.getDescription());
70 assertEquals(Long.valueOf(10000), component.getId());
71 assertEquals(IntegrationTestUtil.USER_ADMIN_60, component.getLead());
72 }
73
74 @Test
75 public void testGetInvalidComponent() throws Exception {
76 final BasicComponent basicComponent = Iterables.get(client.getProjectClient().getProject("TST").claim()
77 .getComponents(), 0);
78 final String uriForUnexistingComponent = basicComponent.getSelf().toString() + "1234";
79 TestUtil.assertErrorCode(Response.Status.NOT_FOUND, "The component with id "
80 + TestUtil.getLastPathSegment(basicComponent.getSelf()) + "1234 does not exist.", new Runnable() {
81 @Override
82 public void run() {
83 client.getComponentClient().getComponent(TestUtil.toUri(uriForUnexistingComponent)).claim();
84 }
85 });
86 }
87
88 @Test
89 public void testGetComponentFromRestrictedProject() throws Exception {
90 final BasicComponent basicComponent = Iterables.getOnlyElement(client.getProjectClient().getProject("RST").claim()
91 .getComponents());
92 assertEquals("One Great Component", client.getComponentClient().getComponent(basicComponent.getSelf()).claim().getName());
93
94
95 setClient(TestConstants.USER2_USERNAME, TestConstants.USER2_PASSWORD);
96 TestUtil.assertErrorCode(Response.Status.NOT_FOUND, IntegrationTestUtil.TESTING_JIRA_5_OR_NEWER ?
97 "The component with id 10010 does not exist."
98 : "The user user does not have permission to complete this operation.", new Runnable() {
99 @Override
100 public void run() {
101 client.getComponentClient().getComponent(basicComponent.getSelf()).claim().getName();
102 }
103 });
104
105 setAnonymousMode();
106 TestUtil.assertErrorCode(Response.Status.NOT_FOUND, IntegrationTestUtil.TESTING_JIRA_5_OR_NEWER ?
107 "The component with id 10010 does not exist."
108 : "This user does not have permission to complete this operation.", new Runnable() {
109 @Override
110 public void run() {
111 client.getComponentClient().getComponent(basicComponent.getSelf()).claim().getName();
112 }
113 });
114 }
115
116 @Test
117 @JiraBuildNumberDependent(BN_JIRA_4_4)
118 public void testCreateAndRemoveComponent() {
119 final Iterable<BasicComponent> components = client.getProjectClient().getProject("TST").claim().getComponents();
120 assertEquals(2, Iterables.size(components));
121 final BasicComponent basicComponent = Iterables.get(components, 0);
122 final BasicComponent basicComponent2 = Iterables.get(components, 1);
123 final String componentName = "my component";
124 final ComponentInput componentInput = new ComponentInput(componentName, "a description", null, null);
125 final Component component = client.getComponentClient().createComponent("TST", componentInput).claim();
126 assertEquals(componentInput.getName(), component.getName());
127 assertEquals(componentInput.getDescription(), component.getDescription());
128 assertNull(component.getLead());
129 assertProjectHasComponents(basicComponent.getName(), basicComponent2.getName(), componentName);
130
131 client.getComponentClient().removeComponent(basicComponent.getSelf(), null).claim();
132 assertProjectHasComponents(basicComponent2.getName(), componentName);
133 client.getComponentClient().removeComponent(basicComponent2.getSelf(), null).claim();
134 assertProjectHasComponents(componentName);
135 client.getComponentClient().removeComponent(component.getSelf(), null).claim();
136 assertProjectHasComponents();
137
138 }
139
140 @Test
141 @JiraBuildNumberDependent(BN_JIRA_4_4)
142 public void testCreateAndRemoveComponentAsUnauthorizedUsers() {
143 final Iterable<BasicComponent> components = client.getProjectClient().getProject("TST").claim().getComponents();
144 assertEquals(2, Iterables.size(components));
145 final BasicComponent basicComponent = Iterables.get(components, 0);
146
147 final ComponentInput componentInput = new ComponentInput("my component", "a description", null, null);
148 setUser1();
149
150 final Response.Status expectedForbiddenErrorCode =
151 (doesJiraReturnCorrectErrorCodeForForbiddenOperation()) ? Response.Status.FORBIDDEN
152 : Response.Status.UNAUTHORIZED;
153 TestUtil.assertErrorCode(expectedForbiddenErrorCode, "The user wseliga does not have permission to complete this operation.", new Runnable() {
154 @Override
155 public void run() {
156 client.getComponentClient().removeComponent(basicComponent.getSelf(), null).claim();
157 }
158 });
159 TestUtil.assertErrorCode(expectedForbiddenErrorCode, "You cannot edit the configuration of this project.", new Runnable() {
160 @Override
161 public void run() {
162 client.getComponentClient().createComponent("TST", componentInput).claim();
163 }
164 });
165
166 setAnonymousMode();
167 TestUtil.assertErrorCode(Response.Status.NOT_FOUND, IntegrationTestUtil.TESTING_JIRA_5_OR_NEWER ?
168 "The component with id 10000 does not exist."
169 : "This user does not have permission to complete this operation.", new Runnable() {
170 @Override
171 public void run() {
172 client.getComponentClient().removeComponent(basicComponent.getSelf(), null).claim();
173 }
174 });
175
176 if (IntegrationTestUtil.TESTING_JIRA_5_OR_NEWER) {
177 TestUtil.assertErrorCode(Response.Status.NOT_FOUND, "No project could be found with key 'TST'.", new Runnable() {
178 @Override
179 public void run() {
180 client.getComponentClient().createComponent("TST", componentInput).claim();
181 }
182 });
183 } else {
184
185 TestUtil.assertErrorCode(expectedForbiddenErrorCode, "You cannot edit the configuration of this project.", new Runnable() {
186 @Override
187 public void run() {
188 client.getComponentClient().createComponent("TST", componentInput).claim();
189 }
190 });
191 }
192
193
194 setAdmin();
195
196 final ComponentInput dupeComponentInput = new ComponentInput(basicComponent.getName(), "a description", null, null);
197 TestUtil.assertErrorCode(Response.Status.BAD_REQUEST, "A component with the name Component A already exists in this project.", new Runnable() {
198 @Override
199 public void run() {
200 client.getComponentClient().createComponent("TST", dupeComponentInput).claim();
201 }
202 });
203
204
205 TestUtil.assertErrorCode(Response.Status.NOT_FOUND, "No project could be found with key 'FAKE'.", new Runnable() {
206 @Override
207 public void run() {
208 client.getComponentClient().createComponent("FAKE", componentInput).claim();
209 }
210 });
211
212 }
213
214
215 @SuppressWarnings({"ConstantConditions"})
216 @Test
217 @JiraBuildNumberDependent(BN_JIRA_4_4)
218 public void testCreateComponentWithLead() {
219 final ComponentInput componentInput = new ComponentInput("my component name", "a description", "admin", AssigneeType.COMPONENT_LEAD);
220 final Component component = client.getComponentClient().createComponent("TST", componentInput).claim();
221 assertNotNull(component.getAssigneeInfo());
222 assertEquals(IntegrationTestUtil.USER_ADMIN_60, component.getAssigneeInfo().getAssignee());
223 assertEquals(AssigneeType.COMPONENT_LEAD, component.getAssigneeInfo().getAssigneeType());
224 assertTrue(component.getAssigneeInfo().isAssigneeTypeValid());
225 assertEquals(IntegrationTestUtil.USER_ADMIN_60, component.getAssigneeInfo().getRealAssignee());
226 assertEquals(AssigneeType.COMPONENT_LEAD, component.getAssigneeInfo().getRealAssigneeType());
227
228 final ComponentInput componentInput2 = new ComponentInput("my component name2", "a description", IntegrationTestUtil.USER1
229 .getName(), AssigneeType.UNASSIGNED);
230 final Component component2 = client.getComponentClient().createComponent("TST", componentInput2).claim();
231 assertNotNull(component2.getAssigneeInfo());
232 assertNull(component2.getAssigneeInfo().getAssignee());
233 assertEquals(AssigneeType.UNASSIGNED, component2.getAssigneeInfo().getAssigneeType());
234 assertFalse(component2.getAssigneeInfo().isAssigneeTypeValid());
235 assertEquals(IntegrationTestUtil.USER_ADMIN_60, component2.getAssigneeInfo().getRealAssignee());
236 assertEquals(AssigneeType.PROJECT_DEFAULT, component2.getAssigneeInfo().getRealAssigneeType());
237 }
238
239
240 @Test
241 @JiraBuildNumberDependent(BN_JIRA_4_4)
242 public void testUpdateComponent() {
243 final BasicComponent basicComponent = Iterables.get(client.getProjectClient().getProject("TST").claim()
244 .getComponents(), 0);
245 final Component component = client.getComponentClient().getComponent(basicComponent.getSelf()).claim();
246 final String newName = basicComponent.getName() + "updated";
247 Component adjustedComponent = new Component(component.getSelf(), component.getId(), newName, component
248 .getDescription(), component.getLead(), component.getAssigneeInfo());
249
250 Component updatedComponent = client.getComponentClient().updateComponent(basicComponent
251 .getSelf(), new ComponentInput(newName, null, null, null)).claim();
252 assertEquals(adjustedComponent, updatedComponent);
253 assertEquals(adjustedComponent, client.getComponentClient().getComponent(basicComponent.getSelf()).claim());
254
255 final String newDescription = "updated description";
256 adjustedComponent = new Component(component.getSelf(), component
257 .getId(), newName, newDescription, IntegrationTestUtil.USER1_60, component.getAssigneeInfo());
258 updatedComponent = client.getComponentClient().updateComponent(basicComponent
259 .getSelf(), new ComponentInput(null, newDescription, IntegrationTestUtil.USER1_60.getName(), null)).claim();
260 assertEquals(adjustedComponent, updatedComponent);
261
262 adjustedComponent = new Component(component.getSelf(), component
263 .getId(), newName, newDescription, IntegrationTestUtil.USER1_60,
264 new Component.AssigneeInfo(IntegrationTestUtil.USER1_60, AssigneeType.COMPONENT_LEAD, IntegrationTestUtil.USER1_60, AssigneeType.COMPONENT_LEAD, true));
265
266 updatedComponent = client.getComponentClient().updateComponent(basicComponent
267 .getSelf(), new ComponentInput(null, newDescription, IntegrationTestUtil.USER1
268 .getName(), AssigneeType.COMPONENT_LEAD)).claim();
269 assertEquals(adjustedComponent, updatedComponent);
270
271
272
273 adjustedComponent = new Component(component.getSelf(), component
274 .getId(), newName, newDescription, IntegrationTestUtil.USER2_60,
275 new Component.AssigneeInfo(IntegrationTestUtil.USER2_60, AssigneeType.COMPONENT_LEAD, IntegrationTestUtil.USER_ADMIN_60, AssigneeType.PROJECT_DEFAULT, false));
276
277 updatedComponent = client.getComponentClient().updateComponent(basicComponent
278 .getSelf(), new ComponentInput(null, newDescription, IntegrationTestUtil.USER2
279 .getName(), AssigneeType.COMPONENT_LEAD)).claim();
280 assertEquals(adjustedComponent, updatedComponent);
281
282 }
283
284
285 @Test
286 @JiraBuildNumberDependent(BN_JIRA_4_4)
287 public void testGetComponentRelatedIssuesCount() {
288 final BasicComponent bc = findEntityByName(client.getProjectClient().getProject("TST").claim()
289 .getComponents(), "Component A");
290 assertEquals(1, client.getComponentClient().getComponentRelatedIssuesCount(bc.getSelf()).claim().intValue());
291 final ComponentInput componentInput = new ComponentInput("my component name", "a description", "admin", AssigneeType.COMPONENT_LEAD);
292 final Component component = client.getComponentClient().createComponent("TST", componentInput).claim();
293 assertEquals(0, client.getComponentClient().getComponentRelatedIssuesCount(component.getSelf()).claim().intValue());
294
295 client.getComponentClient().removeComponent(bc.getSelf(), component.getSelf()).claim();
296 assertEquals(1, client.getComponentClient().getComponentRelatedIssuesCount(component.getSelf()).claim().intValue());
297
298
299 setAnonymousMode();
300 TestUtil.assertErrorCode(Response.Status.NOT_FOUND, IntegrationTestUtil.TESTING_JIRA_5_OR_NEWER ?
301 "The component with id 10000 does not exist."
302 : "This user does not have permission to complete this operation.", new Runnable() {
303 @Override
304 public void run() {
305 client.getComponentClient().getComponentRelatedIssuesCount(component.getSelf()).claim();
306 }
307 });
308
309 setAdmin();
310 final BasicComponent restrictedComponent = Iterables.getOnlyElement(client.getProjectClient().getProject("RST").claim()
311 .getComponents());
312 setUser1();
313 TestUtil.assertErrorCode(Response.Status.NOT_FOUND, IntegrationTestUtil.TESTING_JIRA_5_OR_NEWER ?
314 "The component with id 10010 does not exist."
315 : "The user wseliga does not have permission to complete this operation.", new Runnable() {
316 @Override
317 public void run() {
318 client.getComponentClient().getComponentRelatedIssuesCount(restrictedComponent.getSelf()).claim();
319 }
320 });
321
322 setAdmin();
323 TestUtil.assertErrorCode(Response.Status.NOT_FOUND,
324 "The component with id " + TestUtil.getLastPathSegment(restrictedComponent.getSelf())
325 + "999 does not exist.", new Runnable() {
326 @Override
327 public void run() {
328 client.getComponentClient().getComponentRelatedIssuesCount(TestUtil.toUri(restrictedComponent.getSelf() + "999"))
329 .claim();
330 }
331 });
332
333 }
334
335
336 private boolean doesJiraReturnCorrectErrorCodeForForbiddenOperation() {
337 return client.getMetadataClient().getServerInfo().claim().getBuildNumber() >= ServerVersionConstants.BN_JIRA_5;
338 }
339
340
341 private void assertProjectHasComponents(String... names) {
342 assertThat(Iterables.transform(client.getProjectClient().getProject("TST").claim().getComponents(),
343 EntityHelper.GET_ENTITY_NAME_FUNCTION), containsInAnyOrder(names));
344 }
345
346 }