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.rest.client.BasicComponentNameExtractionFunction;
20  import com.atlassian.jira.rest.client.IntegrationTestUtil;
21  import com.atlassian.jira.rest.client.IterableMatcher;
22  import com.atlassian.jira.rest.client.TestUtil;
23  import com.atlassian.jira.rest.client.domain.AssigneeType;
24  import com.atlassian.jira.rest.client.domain.BasicComponent;
25  import com.atlassian.jira.rest.client.domain.Component;
26  import com.atlassian.jira.rest.client.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.base.Predicate;
30  import com.google.common.collect.Iterables;
31  import org.junit.Test;
32  
33  import javax.ws.rs.core.Response;
34  
35  import static org.junit.Assert.assertThat;
36  
37  public class JerseyComponentRestClientTest extends AbstractRestoringJiraStateJerseyRestClientTest {
38  
39  	@Test
40  	public void testGetComponent() throws Exception {
41  		final BasicComponent basicComponent = Iterables.find(client.getProjectClient().getProject("TST", pm).getComponents(),
42  				new Predicate<BasicComponent>() {
43  					@Override
44  					public boolean apply(BasicComponent input) {
45  						return "Component A".equals(input.getName());
46  					}
47  				});
48  		final Component component = client.getComponentClient().getComponent(basicComponent.getSelf(), pm);
49  		assertEquals("Component A", component.getName());
50  		assertEquals("this is some description of component A", component.getDescription());
51  		assertEquals(IntegrationTestUtil.USER_ADMIN, component.getLead());
52  	}
53  
54  	@Test
55  	public void testGetInvalidComponent() throws Exception {
56  		final BasicComponent basicComponent = Iterables.get(client.getProjectClient().getProject("TST", pm).getComponents(), 0);
57  		final String uriForUnexistingComponent = basicComponent.getSelf().toString() + "1234";
58  		TestUtil.assertErrorCode(Response.Status.NOT_FOUND,  "The component with id "
59  				+ TestUtil.getLastPathSegment(basicComponent.getSelf()) + "1234 does not exist.", new Runnable() {
60  			@Override
61  			public void run() {
62  				client.getComponentClient().getComponent(TestUtil.toUri(uriForUnexistingComponent), pm);
63  			}
64  		});
65  	}
66  
67  	@Test
68  	public void testGetComponentFromRestrictedProject() throws Exception {
69  		final BasicComponent basicComponent = Iterables.getOnlyElement(client.getProjectClient().getProject("RST", pm).getComponents());
70  		assertEquals("One Great Component", client.getComponentClient().getComponent(basicComponent.getSelf(), pm).getName());
71  
72  		// now as unauthorized user
73  		setClient(TestConstants.USER2_USERNAME, TestConstants.USER2_PASSWORD);
74  		TestUtil.assertErrorCode(Response.Status.NOT_FOUND, "The user user does not have permission to complete this operation.", new Runnable() {
75  			@Override
76  			public void run() {
77  				client.getComponentClient().getComponent(basicComponent.getSelf(), pm).getName();
78  			}
79  		});
80  
81  		setAnonymousMode();
82  		TestUtil.assertErrorCode(Response.Status.NOT_FOUND, "This user does not have permission to complete this operation.", new Runnable() {
83  			@Override
84  			public void run() {
85  				client.getComponentClient().getComponent(basicComponent.getSelf(), pm).getName();
86  			}
87  		});
88  	}
89  
90  	@Test
91  	public void testCreateAndRemoveComponent() {
92  		if (!isJira4x4OrNewer()) {
93  			return;
94  		}
95  		final Iterable<BasicComponent> components = client.getProjectClient().getProject("TST", pm).getComponents();
96  		assertEquals(2, Iterables.size(components));
97  		final BasicComponent basicComponent = Iterables.get(components, 0);
98  		final BasicComponent basicComponent2 = Iterables.get(components, 1);
99  		final String componentName = "my component";
100 		final ComponentInput componentInput = new ComponentInput(componentName, "a description", null, null);
101 		final Component component = client.getComponentClient().createComponent("TST", componentInput, pm);
102 		assertEquals(componentInput.getName(), component.getName());
103 		assertEquals(componentInput.getDescription(), component.getDescription());
104 		assertNull(component.getLead());
105 		assertProjectHasComponents(basicComponent.getName(), basicComponent2.getName(), componentName);
106 
107 		client.getComponentClient().removeComponent(basicComponent.getSelf(), null, pm);
108 		assertProjectHasComponents(basicComponent2.getName(), componentName);
109 		client.getComponentClient().removeComponent(basicComponent2.getSelf(), null, pm);
110 		assertProjectHasComponents(componentName);
111 		client.getComponentClient().removeComponent(component.getSelf(), null, pm);
112 		assertProjectHasComponents();
113 
114 	}
115 
116 	@Test
117 	public void testCreateAndRemoveComponentAsUnauthorizedUsers() {
118 		if (!isJira4x4OrNewer()) {
119 			return;
120 		}
121 		final Iterable<BasicComponent> components = client.getProjectClient().getProject("TST", pm).getComponents();
122 		assertEquals(2, Iterables.size(components));
123 		final BasicComponent basicComponent = Iterables.get(components, 0);
124 		final BasicComponent basicComponent2 = Iterables.get(components, 1);
125 
126 		final ComponentInput componentInput = new ComponentInput("my component", "a description", null, null);
127 		setUser1();
128 
129 		final Response.Status expectedForbiddenErrorCode = (doesJiraReturnCorrectErrorCodeForForbiddenOperation()) ? Response.Status.FORBIDDEN : Response.Status.UNAUTHORIZED;
130 		TestUtil.assertErrorCode(expectedForbiddenErrorCode, "The user wseliga does not have permission to complete this operation.", new Runnable() {
131 			@Override
132 			public void run() {
133 				client.getComponentClient().removeComponent(basicComponent.getSelf(), null, pm);
134 			}
135 		});
136 		TestUtil.assertErrorCode(expectedForbiddenErrorCode, "You cannot edit the configuration of this project.", new Runnable() {
137 			@Override
138 			public void run() {
139 				client.getComponentClient().createComponent("TST", componentInput, pm);
140 			}
141 		});
142 
143 		setAnonymousMode();
144 		TestUtil.assertErrorCode(Response.Status.NOT_FOUND, "This user does not have permission to complete this operation.", new Runnable() {
145 			@Override
146 			public void run() {
147 				client.getComponentClient().removeComponent(basicComponent.getSelf(), null, pm);
148 			}
149 		});
150 		// IMO for anonymous access still Response.Status.UNAUTHORIZED should be returned - JRADEV-7671
151 		TestUtil.assertErrorCode(expectedForbiddenErrorCode, "You cannot edit the configuration of this project.", new Runnable() {
152 			@Override
153 			public void run() {
154 				client.getComponentClient().createComponent("TST", componentInput, pm);
155 			}
156 		});
157 
158 		setAdmin();
159 		// now let's try to add a component with colliding name
160 		final ComponentInput dupeComponentInput = new ComponentInput(basicComponent.getName(), "a description", null, null);
161 		TestUtil.assertErrorCode(Response.Status.BAD_REQUEST, "A component with the name Component A already exists in this project.", new Runnable() {
162 			@Override
163 			public void run() {
164 				client.getComponentClient().createComponent("TST", dupeComponentInput, pm);
165 			}
166 		});
167 
168 		// now let's try to add a component for a non existing project
169 		TestUtil.assertErrorCode(Response.Status.NOT_FOUND, "No project could be found with key 'FAKE'.", new Runnable() {
170 			@Override
171 			public void run() {
172 				client.getComponentClient().createComponent("FAKE", componentInput, pm);
173 			}
174 		});
175 
176 	}
177 
178 
179 	@SuppressWarnings({"ConstantConditions"})
180 	@Test
181 	public void testCreateComponentWithLead() {
182 		if (!isJira4x4OrNewer()) {
183 			return;
184 		}
185 		final ComponentInput componentInput = new ComponentInput("my component name", "a description", "admin", AssigneeType.COMPONENT_LEAD);
186 		final Component component = client.getComponentClient().createComponent("TST", componentInput, pm);
187 		assertNotNull(component.getAssigneeInfo());
188 		assertEquals(IntegrationTestUtil.USER_ADMIN, component.getAssigneeInfo().getAssignee());
189 		assertEquals(AssigneeType.COMPONENT_LEAD, component.getAssigneeInfo().getAssigneeType());
190 		assertTrue(component.getAssigneeInfo().isAssigneeTypeValid());
191 		assertEquals(IntegrationTestUtil.USER_ADMIN, component.getAssigneeInfo().getRealAssignee());
192 		assertEquals(AssigneeType.COMPONENT_LEAD, component.getAssigneeInfo().getRealAssigneeType());
193 
194 		final ComponentInput componentInput2 = new ComponentInput("my component name2", "a description", IntegrationTestUtil.USER1.getName(), AssigneeType.UNASSIGNED);
195 		final Component component2 = client.getComponentClient().createComponent("TST", componentInput2, pm);
196 		assertNotNull(component2.getAssigneeInfo());
197 		assertNull(component2.getAssigneeInfo().getAssignee());
198 		assertEquals(AssigneeType.UNASSIGNED, component2.getAssigneeInfo().getAssigneeType());
199 		assertFalse(component2.getAssigneeInfo().isAssigneeTypeValid());
200 		assertEquals(IntegrationTestUtil.USER_ADMIN, component2.getAssigneeInfo().getRealAssignee());
201 		assertEquals(AssigneeType.PROJECT_DEFAULT, component2.getAssigneeInfo().getRealAssigneeType());
202 	}
203 
204 
205 	@Test
206 	public void testUpdateComponent() {
207 		if (!isJira4x4OrNewer()) {
208 			return;
209 		}
210 		final BasicComponent basicComponent = Iterables.get(client.getProjectClient().getProject("TST", pm).getComponents(), 0);
211 		final Component component = client.getComponentClient().getComponent(basicComponent.getSelf(), pm);
212 		final String newName = basicComponent.getName() + "updated";
213 		Component adjustedComponent = new Component(component.getSelf(), newName, component.getDescription(), component.getLead(), component.getAssigneeInfo());
214 
215 		Component updatedComponent = client.getComponentClient().updateComponent(basicComponent.getSelf(), new ComponentInput(newName, null, null, null), pm);
216 		assertEquals(adjustedComponent, updatedComponent);
217 		assertEquals(adjustedComponent, client.getComponentClient().getComponent(basicComponent.getSelf(), pm));
218 
219 		final String newDescription = "updated description";
220 		adjustedComponent = new Component(component.getSelf(), newName, newDescription, IntegrationTestUtil.USER1, component.getAssigneeInfo());
221 		updatedComponent = client.getComponentClient().updateComponent(basicComponent.getSelf(), new ComponentInput(null, newDescription, IntegrationTestUtil.USER1.getName(), null), pm);
222 		assertEquals(adjustedComponent, updatedComponent);
223 
224 		final Component.AssigneeInfo ai = component.getAssigneeInfo();
225 		adjustedComponent = new Component(component.getSelf(), newName, newDescription, IntegrationTestUtil.USER1,
226 				new Component.AssigneeInfo(IntegrationTestUtil.USER1, AssigneeType.COMPONENT_LEAD, IntegrationTestUtil.USER1, AssigneeType.COMPONENT_LEAD, true));
227 
228 		updatedComponent = client.getComponentClient().updateComponent(basicComponent.getSelf(), new ComponentInput(null, newDescription, IntegrationTestUtil.USER1.getName(), AssigneeType.COMPONENT_LEAD), pm);
229 		assertEquals(adjustedComponent, updatedComponent);
230 
231 
232 		// now with non-assignable assignee (thus we are inheriting assignee from project settings and component-level settings are ignored)
233 		adjustedComponent = new Component(component.getSelf(), newName, newDescription, IntegrationTestUtil.USER2,
234 				new Component.AssigneeInfo(IntegrationTestUtil.USER2, AssigneeType.COMPONENT_LEAD, IntegrationTestUtil.USER_ADMIN, AssigneeType.PROJECT_DEFAULT, false));
235 
236 		updatedComponent = client.getComponentClient().updateComponent(basicComponent.getSelf(), new ComponentInput(null, newDescription, IntegrationTestUtil.USER2.getName(), AssigneeType.COMPONENT_LEAD), pm);
237 		assertEquals(adjustedComponent, updatedComponent);
238 
239 	}
240 
241 
242 	@Test
243 	public void testGetComponentRelatedIssuesCount() {
244 		if (!isJira4x4OrNewer()) {
245 			return;
246 		}
247 		final BasicComponent bc = Iterables.find(client.getProjectClient().getProject("TST", pm).getComponents(), new Predicate<BasicComponent>() {
248 			@Override
249 			public boolean apply(BasicComponent input) {
250 				return "Component A".equals(input.getName());
251 			}
252 		});
253 		assertEquals(1, client.getComponentClient().getComponentRelatedIssuesCount(bc.getSelf(), pm));
254 		final ComponentInput componentInput = new ComponentInput("my component name", "a description", "admin", AssigneeType.COMPONENT_LEAD);
255 		final Component component = client.getComponentClient().createComponent("TST", componentInput, pm);
256 		assertEquals(0, client.getComponentClient().getComponentRelatedIssuesCount(component.getSelf(), pm));
257 
258 		client.getComponentClient().removeComponent(bc.getSelf(), component.getSelf(), pm);
259 		assertEquals(1, client.getComponentClient().getComponentRelatedIssuesCount(component.getSelf(), pm));
260 
261 		// smelly error code/message returned here - JRA-25062
262 		setAnonymousMode();
263 		TestUtil.assertErrorCode(Response.Status.NOT_FOUND, "This user does not have permission to complete this operation.", new Runnable() {
264 			@Override
265 			public void run() {
266 				client.getComponentClient().getComponentRelatedIssuesCount(component.getSelf(), pm);
267 			}
268 		});
269 
270 		setAdmin();
271 		final BasicComponent restrictedComponent = Iterables.getOnlyElement(client.getProjectClient().getProject("RST", pm).getComponents());
272 		setUser1();
273 		TestUtil.assertErrorCode(Response.Status.NOT_FOUND, "The user wseliga does not have permission to complete this operation.", new Runnable() {
274 			@Override
275 			public void run() {
276 				client.getComponentClient().getComponentRelatedIssuesCount(restrictedComponent.getSelf(), pm);
277 			}
278 		});
279 
280 		setAdmin();
281 		TestUtil.assertErrorCode(Response.Status.NOT_FOUND, "The component with id " + TestUtil.getLastPathSegment(restrictedComponent.getSelf())
282 				+ "999 does not exist.", new Runnable() {
283 			@Override
284 			public void run() {
285 				client.getComponentClient().getComponentRelatedIssuesCount(TestUtil.toUri(restrictedComponent.getSelf() + "999"), pm);
286 			}
287 		});
288 
289 	}
290 
291 
292 	boolean doesJiraReturnCorrectErrorCodeForForbiddenOperation() {
293 		return client.getMetadataClient().getServerInfo(pm).getBuildNumber() >= ServerVersionConstants.BN_JIRA_5;
294 	}
295 
296 
297 	private void assertProjectHasComponents(String ...names) {
298 		assertThat(Iterables.transform(client.getProjectClient().getProject("TST", pm).getComponents(),
299 				new BasicComponentNameExtractionFunction()), IterableMatcher.hasOnlyElements(names));
300 	}
301 
302 }