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