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.BasicProject;
22  import com.atlassian.jira.rest.client.domain.Project;
23  import com.atlassian.jira.rest.client.internal.ServerVersionConstants;
24  import com.atlassian.jira.rest.client.internal.json.TestConstants;
25  import com.google.common.base.Predicate;
26  import com.google.common.collect.Iterables;
27  import org.junit.Test;
28  
29  import javax.annotation.Nullable;
30  import javax.ws.rs.core.Response;
31  
32  public class JerseyProjectRestClientTest extends AbstractRestoringJiraStateJerseyRestClientTest {
33  	@Test
34  	public void testGetNonExistingProject() throws Exception {
35  		final String nonExistingProjectKey = "NONEXISTINGPROJECTKEY";
36  		TestUtil.assertErrorCode(Response.Status.NOT_FOUND, "No project could be found with key '" +
37  				nonExistingProjectKey + "'.", new Runnable() {
38  			@Override
39  			public void run() {
40  				client.getProjectClient().getProject(nonExistingProjectKey, pm);
41  			}
42  		});
43  	}
44  
45  	@Test
46  	public void testGetProject() {
47  		final Project project = client.getProjectClient().getProject("TST", pm);
48  		assertEquals("TST", project.getKey());
49  		assertEquals(IntegrationTestUtil.USER_ADMIN, project.getLead());
50  		assertEquals(2, Iterables.size(project.getVersions()));
51  		assertEquals(2, Iterables.size(project.getComponents()));
52  	}
53  
54  	@Test
55  	public void testGetRestrictedProject() {
56  		final Project project = client.getProjectClient().getProject("RST", pm);
57  		assertEquals("RST", project.getKey());
58  
59  		setClient(TestConstants.USER1_USERNAME, TestConstants.USER1_PASSWORD);
60  		client.getProjectClient().getProject("TST", pm);
61  		// @todo when JRADEV-3519 - instead of NOT_FOUND, FORBIDDEN code should be returned by JIRA
62  		TestUtil.assertErrorCode(Response.Status.NOT_FOUND, "You must have the browse project permission to view this project.", new Runnable() {
63  			@Override
64  			public void run() {
65  				client.getProjectClient().getProject("RST", pm);
66  			}
67  		});
68  	}
69  
70  	@Test
71  	public void testGetAnonymouslyProject() {
72  		// @todo when JRADEV-3519 - instead of NOT_FOUND, UNAUTHORIZED code should be returned by JIRA
73  		setAnonymousMode();
74  		TestUtil.assertErrorCode(Response.Status.NOT_FOUND, "You must have the browse project permission to view this project.", new Runnable() {
75  			@Override
76  			public void run() {
77  				client.getProjectClient().getProject("RST", pm);
78  			}
79  		});
80  
81  		TestUtil.assertErrorCode(Response.Status.NOT_FOUND, "You must have the browse project permission to view this project.", new Runnable() {
82  			@Override
83  			public void run() {
84  				client.getProjectClient().getProject("TST", pm);
85  			}
86  		});
87  
88  		final Project project = client.getProjectClient().getProject("ANNON", pm);
89  		assertEquals("ANNON", project.getKey());
90  
91  	}
92  
93  	@Test
94  	public void testGetAllProject() {
95  		if (!isGetAllProjectsSupported()) {
96  			return;
97  		}
98  
99  		final Iterable<BasicProject> projects = client.getProjectClient().getAllProjects(pm);
100 		assertEquals(3, Iterables.size(projects));
101 		final BasicProject tst = Iterables.find(projects, new Predicate<BasicProject>() {
102 			@Override
103 			public boolean apply(@Nullable BasicProject input) {
104 				return input.getKey().equals("TST");
105 			}
106 		});
107 		assertTrue(tst.getSelf().toString().contains(jiraRestRootUri.toString()));
108 
109 		setAnonymousMode();
110 		final Iterable<BasicProject> anonymouslyAccessibleProjects = client.getProjectClient().getAllProjects(pm);
111 		assertEquals(1, Iterables.size(anonymouslyAccessibleProjects));
112 		assertEquals("ANNON", Iterables.get(anonymouslyAccessibleProjects, 0).getKey());
113 
114 		setUser1();
115 		assertEquals(2, Iterables.size(client.getProjectClient().getAllProjects(pm)));
116 	}
117 
118 	private boolean isGetAllProjectsSupported() {
119 		return client.getMetadataClient().getServerInfo(pm).getBuildNumber() >= ServerVersionConstants.BN_JIRA_4_3_OR_NEWER;
120 	}
121 
122 }