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.nimblefunctests.annotation.JiraBuildNumberDependent;
20  import com.atlassian.jira.nimblefunctests.annotation.RestoreOnce;
21  import com.atlassian.jira.rest.client.AddressableEntity;
22  import com.atlassian.jira.rest.client.IntegrationTestUtil;
23  import com.atlassian.jira.rest.client.OptionalIterable;
24  import com.atlassian.jira.rest.client.TestUtil;
25  import com.atlassian.jira.rest.client.domain.BasicProject;
26  import com.atlassian.jira.rest.client.domain.IssueType;
27  import com.atlassian.jira.rest.client.domain.Priority;
28  import com.atlassian.jira.rest.client.domain.Project;
29  import com.atlassian.jira.rest.client.domain.Resolution;
30  import com.atlassian.jira.rest.client.internal.ServerVersionConstants;
31  import com.atlassian.jira.rest.client.internal.json.TestConstants;
32  import com.google.common.base.Function;
33  import com.google.common.base.Predicate;
34  import com.google.common.collect.Iterables;
35  import org.junit.Assert;
36  import org.junit.Test;
37  
38  import javax.annotation.Nullable;
39  import javax.ws.rs.core.Response;
40  import java.net.URISyntaxException;
41  import java.util.Iterator;
42  
43  import static com.atlassian.jira.rest.client.internal.ServerVersionConstants.BN_JIRA_5;
44  import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
45  import static org.junit.Assert.*;
46  
47  /**
48   * Those tests mustn't change anything on server side, as jira is restored only once
49   */
50  // Ignore "May produce NPE" warnings, as we know what we are doing in tests
51  @SuppressWarnings("ConstantConditions")
52  @RestoreOnce(TestConstants.DEFAULT_JIRA_DUMP_FILE)
53  public class JerseyProjectRestClientReadOnlyTest extends AbstractJerseyRestClientTest {
54  
55  	@Test
56  	public void testGetNonExistingProject() throws Exception {
57  		final String nonExistingProjectKey = "NONEXISTINGPROJECTKEY";
58  		TestUtil.assertErrorCode(Response.Status.NOT_FOUND, "No project could be found with key '" +
59  				nonExistingProjectKey + "'.", new Runnable() {
60  			@Override
61  			public void run() {
62  				client.getProjectClient().getProject(nonExistingProjectKey, pm);
63  			}
64  		});
65  	}
66  
67  	@Test
68  	public void testGetProject() throws URISyntaxException {
69  		final Project project = client.getProjectClient().getProject("TST", pm);
70  		assertEquals("TST", project.getKey());
71  		assertEquals(IntegrationTestUtil.USER_ADMIN_LATEST, project.getLead());
72  		assertEquals(2, Iterables.size(project.getVersions()));
73  		assertEquals(2, Iterables.size(project.getComponents()));
74  		final OptionalIterable<IssueType> issueTypes = project.getIssueTypes();
75  		if (isJira4x4OrNewer()) {
76  			assertTrue(issueTypes.isSupported());
77  			final Iterator<IssueType> issueTypesIterator = issueTypes.iterator();
78  			assertTrue(issueTypesIterator.hasNext());
79  			final IssueType it = issueTypesIterator.next();
80  			if (isJira5xOrNewer()) {
81  				assertEquals(Long.valueOf(1), it.getId());
82  			}
83  			else {
84  				assertNull(it.getId());
85  			}
86  			assertEquals(it.getName(), "Bug");
87  		}
88  		else {
89  			assertFalse(issueTypes.isSupported());
90  		}
91  	}
92  
93  	@Test
94  	public void testGetRestrictedProject() {
95  		final Project project = client.getProjectClient().getProject("RST", pm);
96  		assertEquals("RST", project.getKey());
97  
98  		setClient(TestConstants.USER1_USERNAME, TestConstants.USER1_PASSWORD);
99  		client.getProjectClient().getProject("TST", pm);
100 		// @todo when JRADEV-3519 - instead of NOT_FOUND, FORBIDDEN code should be returned by JIRA
101 		final String message = getCannotViewProjectErrorMessage("RST");
102 		TestUtil.assertErrorCode(Response.Status.NOT_FOUND, message, new Runnable() {
103 			@Override
104 			public void run() {
105 				client.getProjectClient().getProject("RST", pm);
106 			}
107 		});
108 	}
109 
110 	private String getCannotViewProjectErrorMessage(String key) {
111 		return isJira4x4OrNewer()
112 				? (isJira5xOrNewer() ? ("No project could be found with key '" + key + "'.") : "You cannot view this project.")
113 				: "You must have the browse project permission to view this project.";
114 	}
115 
116 	@Test
117 	public void testGetAnonymouslyProject() {
118 		// @todo when JRADEV-3519 - instead of NOT_FOUND, UNAUTHORIZED code should be returned by JIRA
119 		setAnonymousMode();
120 		TestUtil.assertErrorCode(Response.Status.NOT_FOUND, getCannotViewProjectErrorMessage("RST"), new Runnable() {
121 			@Override
122 			public void run() {
123 				client.getProjectClient().getProject("RST", pm);
124 			}
125 		});
126 
127 		TestUtil.assertErrorCode(Response.Status.NOT_FOUND, getCannotViewProjectErrorMessage("TST"), new Runnable() {
128 			@Override
129 			public void run() {
130 				client.getProjectClient().getProject("TST", pm);
131 			}
132 		});
133 
134 		final Project project = client.getProjectClient().getProject("ANNON", pm);
135 		assertEquals("ANNON", project.getKey());
136 
137 	}
138 
139 	@Test
140 	public void testGetAllProject() {
141 		if (!isGetAllProjectsSupported()) {
142 			return;
143 		}
144 
145 		final Iterable<BasicProject> projects = client.getProjectClient().getAllProjects(pm);
146 		assertEquals(4, Iterables.size(projects));
147 		final BasicProject tst = Iterables.find(projects, new Predicate<BasicProject>() {
148 			@Override
149 			public boolean apply(@Nullable BasicProject input) {
150 				return input.getKey().equals("TST");
151 			}
152 		});
153 		assertTrue(tst.getSelf().toString().contains(jiraRestRootUri.toString()));
154 
155 		setAnonymousMode();
156 		final Iterable<BasicProject> anonymouslyAccessibleProjects = client.getProjectClient().getAllProjects(pm);
157 		assertEquals(2, Iterables.size(anonymouslyAccessibleProjects));
158 
159 		final Iterable<String> projectsKeys = Iterables.transform(anonymouslyAccessibleProjects, new Function<BasicProject, String>() {
160 			@Override
161 			public String apply(BasicProject project) {
162 				return project.getKey();
163 			}
164 		});
165 		Assert.assertThat(projectsKeys, containsInAnyOrder("ANNON", "ANONEDIT"));
166 
167 		setUser1();
168 		assertEquals(3, Iterables.size(client.getProjectClient().getAllProjects(pm)));
169 	}
170 
171 	private boolean isGetAllProjectsSupported() {
172 		return client.getMetadataClient().getServerInfo(pm).getBuildNumber() >= ServerVersionConstants.BN_JIRA_4_3;
173 	}
174 
175 	@Test
176 	@JiraBuildNumberDependent(BN_JIRA_5)
177 	public void testGetPriorities() {
178 		final Iterable<Priority> priorities = client.getMetadataClient().getPriorities(pm);
179 		assertEquals(5, Iterables.size(priorities));
180 
181 		final Priority priority = findEntityBySelfAddressSuffix(priorities, "/1");
182 		assertEquals(Long.valueOf(1), priority.getId());
183 		assertEquals("Blocker", priority.getName());
184 		assertEquals("Blocks development and/or testing work, production could not run.", priority.getDescription());
185 		assertNotNull(priority.getSelf());
186 	}
187 
188 	@Test
189 	@JiraBuildNumberDependent(BN_JIRA_5)
190 	public void testGetIssueTypes() {
191 		final Iterable<IssueType> issueTypes = client.getMetadataClient().getIssueTypes(pm);
192 		assertEquals(5, Iterables.size(issueTypes));
193 
194 		final IssueType issueType = findEntityBySelfAddressSuffix(issueTypes, "/5");
195 		assertEquals("Sub-task", issueType.getName());
196 		assertEquals("The sub-task of the issue", issueType.getDescription());
197 		assertEquals(Long.valueOf(5), issueType.getId());
198 		assertTrue(issueType.isSubtask());
199 		assertNotNull(issueType.getSelf());
200 	}
201 
202 	@Test
203 	@JiraBuildNumberDependent(BN_JIRA_5)
204 	public void testGetResolutions() {
205 		final Iterable<Resolution> resolutions = client.getMetadataClient().getResolutions(pm);
206 		assertEquals(5, Iterables.size(resolutions));
207 		final Resolution resolution = findEntityBySelfAddressSuffix(resolutions, "/1");
208 		assertEquals("Fixed", resolution.getName());
209 		assertEquals("A fix for this issue is checked into the tree and tested.", resolution.getDescription());
210 		assertNotNull(resolution.getSelf());
211 	}
212 	
213 	private <T extends AddressableEntity> T findEntityBySelfAddressSuffix(final Iterable<T> entities, final String suffix) {
214 		return Iterables.find(entities, new Predicate<T>() {
215 			@Override
216 			public boolean apply(T input) {
217 				return input.getSelf().toString().endsWith(suffix);
218 			}
219 		});
220 	}
221 
222 }