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.IntegrationTestUtil;
20  import com.atlassian.jira.rest.client.TestUtil;
21  import com.atlassian.jira.rest.client.domain.BasicComponent;
22  import com.atlassian.jira.rest.client.domain.Component;
23  import com.atlassian.jira.rest.client.internal.json.TestConstants;
24  import com.google.common.collect.Iterables;
25  
26  import javax.ws.rs.core.Response;
27  
28  public class JerseyComponentRestClientTest extends AbstractRestoringJiraStateJerseyRestClientTest {
29  	public void testGetComponent() throws Exception {
30  		final BasicComponent basicComponent = Iterables.get(client.getProjectClient().getProject("TST", pm).getComponents(), 0);
31  		final Component component = client.getComponentClient().getComponent(basicComponent.getSelf(), pm);
32  		assertEquals("Component A", component.getName());
33  		assertEquals("this is some description of component A", component.getDescription());
34  		assertEquals(IntegrationTestUtil.USER_ADMIN, component.getLead());
35  	}
36  
37  	public void testGetInvalidComponent() throws Exception {
38  		final BasicComponent basicComponent = Iterables.get(client.getProjectClient().getProject("TST", pm).getComponents(), 0);
39  		final String uriForUnexistingComponent = basicComponent.getSelf().toString() + "1234";
40  		TestUtil.assertErrorCode(Response.Status.NOT_FOUND,  "The component with id "
41  				+ TestUtil.getLastPathSegment(basicComponent.getSelf()) + "1234 does not exist.", new Runnable() {
42  			@Override
43  			public void run() {
44  				client.getComponentClient().getComponent(TestUtil.toUri(uriForUnexistingComponent), pm);
45  			}
46  		});
47  	}
48  
49  	public void testGetComponentFromRestrictedProject() throws Exception {
50  		final BasicComponent basicComponent = Iterables.get(client.getProjectClient().getProject("RST", pm).getComponents(), 0);
51  		assertEquals("One Great Component", client.getComponentClient().getComponent(basicComponent.getSelf(), pm).getName());
52  
53  		// now as unauthorized user
54  		setClient(TestConstants.USER2_USERNAME, TestConstants.USER2_PASSWORD);
55  		TestUtil.assertErrorCode(Response.Status.NOT_FOUND, "The user user does not have permission to complete this operation.", new Runnable() {
56  			@Override
57  			public void run() {
58  				client.getComponentClient().getComponent(basicComponent.getSelf(), pm).getName();
59  			}
60  		});
61  
62  		setAnonymousMode();
63  		TestUtil.assertErrorCode(Response.Status.NOT_FOUND, "This user does not have permission to complete this operation.", new Runnable() {
64  			@Override
65  			public void run() {
66  				client.getComponentClient().getComponent(basicComponent.getSelf(), pm).getName();
67  			}
68  		});
69  	}
70  }