1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package it;
17
18 import com.atlassian.jira.nimblefunctests.annotation.JiraBuildNumberDependent;
19 import com.atlassian.jira.nimblefunctests.annotation.LongCondition;
20 import com.atlassian.jira.rest.client.IntegrationTestUtil;
21 import com.atlassian.jira.rest.client.api.RestClientException;
22 import com.atlassian.jira.rest.client.api.domain.EntityHelper;
23 import com.atlassian.jira.rest.client.api.domain.Project;
24 import com.atlassian.jira.rest.client.api.domain.ProjectRole;
25 import com.atlassian.jira.rest.client.api.domain.RoleActor;
26 import com.atlassian.jira.rest.client.internal.ServerVersionConstants;
27 import com.atlassian.jira.rest.client.internal.json.TestConstants;
28 import com.google.common.base.Function;
29 import com.google.common.collect.ImmutableList;
30 import com.google.common.collect.Iterables;
31 import com.google.common.collect.Lists;
32 import org.hamcrest.BaseMatcher;
33 import org.hamcrest.Description;
34 import org.hamcrest.Matcher;
35 import org.hamcrest.Matchers;
36 import org.junit.Assert;
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41
42 import java.net.URI;
43
44 import static com.atlassian.jira.rest.client.IntegrationTestUtil.buildUserAvatarUri;
45 import static com.atlassian.jira.rest.client.test.matchers.RestClientExceptionMatchers.rceWithSingleError;
46 import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
47 import static org.junit.Assert.assertEquals;
48 import static org.junit.Assert.assertNotNull;
49 import static org.junit.Assert.assertThat;
50
51 public class AsynchronousProjectRoleRestClientTest extends AbstractAsynchronousRestClientTest {
52
53 private static final String ANONYMOUS_PROJECT_KEY = "ANNON";
54 private static final String RESTRICTED_PROJECT_KEY = "RST";
55
56 private static boolean alreadyRestored;
57
58 @Rule
59 public final ExpectedException exception = ExpectedException.none();
60
61 @Before
62 public void setup() {
63 if (!alreadyRestored) {
64 IntegrationTestUtil.restoreAppropriateJiraData(TestConstants.DEFAULT_JIRA_DUMP_FILE, administration);
65 alreadyRestored = true;
66 }
67 }
68
69 @JiraBuildNumberDependent(ServerVersionConstants.BN_JIRA_4_4)
70 @Test
71 public void testGetProjectRoleWithRoleKeyFromAnonymousProject() {
72 final Project anonProject = client.getProjectClient().getProject(ANONYMOUS_PROJECT_KEY).claim();
73 final ProjectRole role = client.getProjectRolesRestClient().getRole(anonProject.getSelf(), 10000l).claim();
74 assertNotNull(role);
75 assertEquals("Users", role.getName());
76 assertEquals("A project role that represents users in a project", role.getDescription());
77 final RoleActor actor = Iterables.getOnlyElement(role.getActors());
78 assertEquals("jira-users", actor.getDisplayName());
79 assertEquals("atlassian-group-role-actor", actor.getType());
80 assertEquals("jira-users", actor.getName());
81 assertEquals(actor.getAvatarUri(), buildUserAvatarUri(null, 10083L, "16x16"));
82 }
83
84 @JiraBuildNumberDependent(ServerVersionConstants.BN_JIRA_4_4)
85 @Test
86 public void testGetProjectRoleWithRoleKeyFromRestrictedProject() {
87 final Project restrictedProject = client.getProjectClient().getProject(RESTRICTED_PROJECT_KEY).claim();
88 final ProjectRole role = client.getProjectRolesRestClient().getRole(restrictedProject.getSelf(), 10000l).claim();
89 assertNotNull(role);
90 assertEquals("Users", role.getName());
91 assertEquals("A project role that represents users in a project", role.getDescription());
92 final RoleActor actor = Iterables.getOnlyElement(role.getActors());
93 assertEquals("Administrator", actor.getDisplayName());
94 assertEquals("atlassian-user-role-actor", actor.getType());
95 assertEquals("admin", actor.getName());
96 assertEquals(actor.getAvatarUri(), buildUserAvatarUri("admin", 10054L, "16x16"));
97 }
98
99 @JiraBuildNumberDependent(ServerVersionConstants.BN_JIRA_4_4)
100 @Test
101 public void testGetProjectRoleWithRoleKeyFromRestrictedProjectWithoutPermission() {
102 final Project restrictedProject = client.getProjectClient().getProject(RESTRICTED_PROJECT_KEY).claim();
103 setAnonymousMode();
104 exception.expect(RestClientException.class);
105 if (isJira61xOrNewer()) {
106 final String expectedError = String.format("No project could be found with id '%s'.", restrictedProject.getId());
107 exception.expect(rceWithSingleError(404, expectedError));
108 } else {
109 exception.expectMessage(String.format("No project could be found with key '%s'", RESTRICTED_PROJECT_KEY));
110 }
111 client.getProjectRolesRestClient().getRole(restrictedProject.getSelf(), 10000l).claim();
112 }
113
114 @JiraBuildNumberDependent(ServerVersionConstants.BN_JIRA_4_4)
115 @Test
116 public void testGetProjectRoleWithFullURI() {
117 final Project anonProject = client.getProjectClient().getProject(ANONYMOUS_PROJECT_KEY).claim();
118 final URI roleURI = client.getProjectRolesRestClient().getRole(anonProject.getSelf(), 10000l).claim().getSelf();
119 final ProjectRole role = client.getProjectRolesRestClient().getRole(roleURI).claim();
120 assertNotNull(role);
121 assertEquals("Users", role.getName());
122 assertEquals("A project role that represents users in a project", role.getDescription());
123 final RoleActor actor = Iterables.getOnlyElement(role.getActors());
124 assertEquals("jira-users", actor.getDisplayName());
125 assertEquals("atlassian-group-role-actor", actor.getType());
126 assertEquals("jira-users", actor.getName());
127 assertEquals(actor.getAvatarUri(), buildUserAvatarUri(null, 10083L, "16x16"));
128 }
129
130 @JiraBuildNumberDependent(value = ServerVersionConstants.BN_JIRA_6_1, condition = LongCondition.LESS_THAN)
131 @Test
132 public void testGetAllRolesForProjectBefore6_1() {
133 testGetAllRolesForProject(ANONYMOUS_PROJECT_KEY);
134 }
135
136 @JiraBuildNumberDependent(value = ServerVersionConstants.BN_JIRA_6_1)
137 @Test
138 public void testGetAllRolesForProject() {
139 testGetAllRolesForProject("10020");
140 }
141
142 private void testGetAllRolesForProject(String projectIdOrKey) {
143 final Project anonymousProject = client.getProjectClient().getProject(ANONYMOUS_PROJECT_KEY).claim();
144 final Iterable<ProjectRole> projectRoles = client.getProjectRolesRestClient().getRoles(anonymousProject.getSelf())
145 .claim();
146 final Iterable<ProjectRole> projectRolesWithoutSelf = Iterables.transform(
147 projectRoles,
148 new Function<ProjectRole, ProjectRole>() {
149 @Override
150 public ProjectRole apply(final ProjectRole role) {
151 return new ProjectRole(role.getId(), null, role.getName(), role.getDescription(), Lists.newArrayList(role
152 .getActors()));
153 }
154 }
155 );
156 assertThat(projectRolesWithoutSelf, containsInAnyOrder(
157 new ProjectRole(10000l, null, "Users", "A project role that represents users in a project",
158 ImmutableList.<RoleActor>of(
159 new RoleActor(10062l, "jira-users", "atlassian-group-role-actor", "jira-users", buildUserAvatarUri(null, 10083L, "16x16"))
160 )),
161 new ProjectRole(10001l, null, "Developers", "A project role that represents developers in a project",
162 ImmutableList.<RoleActor>of(
163 new RoleActor(10061l, "jira-developers", "atlassian-group-role-actor", "jira-developers", buildUserAvatarUri(null, 10083L, "16x16")),
164 new RoleActor(10063l, "My Test User", "atlassian-user-role-actor", "user", buildUserAvatarUri(null, 10082L, "16x16"))
165 )),
166 new ProjectRole(10002l, null, "Administrators", "A project role that represents administrators in a project",
167 ImmutableList.<RoleActor>of(
168 new RoleActor(10060l, "jira-administrators", "atlassian-group-role-actor", "jira-administrators", buildUserAvatarUri(null, 10083L, "16x16"))
169 ))
170 ));
171
172
173 Assert.assertThat(projectRoles, Matchers.containsInAnyOrder(
174 addressEndsWith("project/" + projectIdOrKey + "/role/10000"),
175 addressEndsWith("project/" + projectIdOrKey + "/role/10001"),
176 addressEndsWith("project/" + projectIdOrKey + "/role/10002")));
177 }
178
179 private Matcher<ProjectRole> addressEndsWith(final String addressEnding) {
180 return new BaseMatcher<ProjectRole>() {
181 @Override
182 public boolean matches(Object o) {
183 return o instanceof ProjectRole && new EntityHelper.AddressEndsWithPredicate(addressEnding).apply((ProjectRole) o);
184 }
185
186 @Override
187 public void describeTo(Description description) {
188 description.appendText("ProjectRole with self address ending with " + addressEnding);
189 }
190 };
191 }
192
193 private void testGetProjectRoleWithRoleKeyErrorCode(final String description) {
194 final Project anonProject = client.getProjectClient().getProject(ANONYMOUS_PROJECT_KEY).claim();
195 exception.expect(RestClientException.class);
196 exception.expectMessage(description);
197 client.getProjectRolesRestClient().getRole(anonProject.getSelf(), -1l).claim();
198 }
199
200 @JiraBuildNumberDependent(ServerVersionConstants.BN_JIRA_7_2)
201 @Test
202 public void testGetProjectRoleWithRoleKeyErrorCode() {
203 testGetProjectRoleWithRoleKeyErrorCode("We don't seem to be able to find the role you're trying to use. Check it still exists and try again.");
204 }
205
206 @JiraBuildNumberDependent(condition = LongCondition.LESS_THAN, value = ServerVersionConstants.BN_JIRA_7_2)
207 @Test
208 public void testGetProjectRoleWithRoleKeyErrorCodeLegacy() {
209 testGetProjectRoleWithRoleKeyErrorCode("Can not retrieve a role actor for a null project role.");
210 }
211 }