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_LATEST, 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  		final String message = getCannotViewProjectErrorMessage("RST");
63  		TestUtil.assertErrorCode(Response.Status.NOT_FOUND, message, new Runnable() {
64  			@Override
65  			public void run() {
66  				client.getProjectClient().getProject("RST", pm);
67  			}
68  		});
69  	}
70  
71  	private String getCannotViewProjectErrorMessage(String key) {
72  		return isJira4x4OrNewer()
73  				? (isJira5xOrNewer() ? ("No project could be found with key '" + key + "'.") : "You cannot view this project.")
74  				: "You must have the browse project permission to view this project.";
75  	}
76  
77  	@Test
78  	public void testGetAnonymouslyProject() {
79  		// @todo when JRADEV-3519 - instead of NOT_FOUND, UNAUTHORIZED code should be returned by JIRA
80  		setAnonymousMode();
81  		TestUtil.assertErrorCode(Response.Status.NOT_FOUND, getCannotViewProjectErrorMessage("RST"), new Runnable() {
82  			@Override
83  			public void run() {
84  				client.getProjectClient().getProject("RST", pm);
85  			}
86  		});
87  
88  		TestUtil.assertErrorCode(Response.Status.NOT_FOUND, getCannotViewProjectErrorMessage("TST"), new Runnable() {
89  			@Override
90  			public void run() {
91  				client.getProjectClient().getProject("TST", pm);
92  			}
93  		});
94  
95  		final Project project = client.getProjectClient().getProject("ANNON", pm);
96  		assertEquals("ANNON", project.getKey());
97  
98  	}
99  
100 	@Test
101 	public void testGetAllProject() {
102 		if (!isGetAllProjectsSupported()) {
103 			return;
104 		}
105 
106 		final Iterable<BasicProject> projects = client.getProjectClient().getAllProjects(pm);
107 		assertEquals(3, Iterables.size(projects));
108 		final BasicProject tst = Iterables.find(projects, new Predicate<BasicProject>() {
109 			@Override
110 			public boolean apply(@Nullable BasicProject input) {
111 				return input.getKey().equals("TST");
112 			}
113 		});
114 		assertTrue(tst.getSelf().toString().contains(jiraRestRootUri.toString()));
115 
116 		setAnonymousMode();
117 		final Iterable<BasicProject> anonymouslyAccessibleProjects = client.getProjectClient().getAllProjects(pm);
118 		assertEquals(1, Iterables.size(anonymouslyAccessibleProjects));
119 		assertEquals("ANNON", Iterables.get(anonymouslyAccessibleProjects, 0).getKey());
120 
121 		setUser1();
122 		assertEquals(2, Iterables.size(client.getProjectClient().getAllProjects(pm)));
123 	}
124 
125 	private boolean isGetAllProjectsSupported() {
126 		return client.getMetadataClient().getServerInfo(pm).getBuildNumber() >= ServerVersionConstants.BN_JIRA_4_3;
127 	}
128 
129 
130 }