View Javadoc

1   /*
2    * Copyright (C) 2012 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  package com.atlassian.jira.rest.client.internal.json;
17  
18  import com.atlassian.jira.rest.client.TestUtil;
19  import com.atlassian.jira.rest.client.api.domain.ProjectRole;
20  import com.atlassian.jira.rest.client.api.domain.RoleActor;
21  import com.google.common.collect.Iterables;
22  import org.codehaus.jettison.json.JSONException;
23  import org.hamcrest.collection.IsIterableContainingInAnyOrder;
24  import org.junit.Assert;
25  import org.junit.Rule;
26  import org.junit.Test;
27  import org.junit.rules.ExpectedException;
28  
29  import java.net.MalformedURLException;
30  import java.net.URI;
31  
32  import static com.atlassian.jira.rest.client.TestUtil.toUri;
33  
34  public class ProjectRoleJsonParserTest {
35  
36      private final URI baseJiraURI = TestUtil.toUri("http://localhost:2990");
37      private final ProjectRoleJsonParser parser = new ProjectRoleJsonParser(baseJiraURI);
38  
39      @Rule
40      public final ExpectedException exception = ExpectedException.none();
41  
42      @Test
43      public void testParseRoleDetail() throws Exception {
44          final ProjectRole role = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/role/valid-role-single-actor.json"));
45          Assert.assertEquals(TestUtil.toUri("http://www.example.com/jira/rest/api/2/project/MKY/role/10360"), role.getSelf());
46          Assert.assertEquals("Developers", role.getName());
47          Assert.assertEquals("A project role that represents developers in a project", role.getDescription());
48          Assert.assertNotNull(role.getActors());
49          final RoleActor actor = Iterables.getOnlyElement(role.getActors());
50          Assert.assertEquals("jira-developers", actor.getDisplayName());
51          Assert.assertEquals("atlassian-group-role-actor", actor.getType());
52          Assert.assertEquals("jira-developers", actor.getName());
53      }
54  
55      @Test
56      public void testParseRoleWithMultipleActors() throws Exception {
57          final ProjectRole role = parser.parse(ResourceUtil
58                  .getJsonObjectFromResource("/json/role/valid-role-multiple-actors.json"));
59          Assert.assertEquals(TestUtil.toUri("http://localhost:2990/jira/rest/api/2/project/TST/role/10000"), role.getSelf());
60          Assert.assertEquals("Users", role.getName());
61          Assert.assertEquals("A project role that represents users in a project", role.getDescription());
62          Assert.assertNotNull(role.getActors());
63          Assert.assertThat(role.getActors(),
64                  IsIterableContainingInAnyOrder
65                          .containsInAnyOrder(new RoleActor(10020l, "jira-users", "atlassian-group-role-actor", "jira-users",
66                                          toUri("http://localhost:2990/jira/secure/useravatar?size=small&avatarId=10083")
67                                  ),
68                                  new RoleActor(10030l, "jira-superuser", "atlassian-user-role-actor", "superuser", null)
69                          )
70          );
71      }
72  
73      @Test
74      public void testParseRoleWithNoActors() throws Exception {
75          final ProjectRole role = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/role/valid-role-no-actors.json"));
76          Assert.assertEquals(toUri("http://localhost:2990/jira/rest/api/2/project/TST/role/10000"), role.getSelf());
77          Assert.assertEquals("Users", role.getName());
78          Assert.assertEquals("A project role that represents users in a project", role.getDescription());
79          Assert.assertNotNull(role.getActors());
80      }
81  
82      @Test
83      public void testInvalidRole() throws Exception {
84          exception.expect(JSONException.class);
85          exception.expectMessage("JSONObject[\"self\"] not found.");
86          parser.parse(ResourceUtil.getJsonObjectFromResource("/json/role/invalid-role.json"));
87      }
88  
89      // This test checks the special "admin" case.
90      // Id field should not be optional, unfortunately it is not returned for an admin role actor.
91      @Test
92      public void testParseProjectRoleContainingActorWithoutIdField() throws JSONException, MalformedURLException {
93          final ProjectRole role = parser.parse(ResourceUtil
94                  .getJsonObjectFromResource("/json/role/valid-role-without-user-actor-id.json"));
95          Assert.assertNotNull(role);
96          Assert.assertEquals("Users", role.getName());
97          Assert.assertEquals(TestUtil.toUri("http://localhost:2990/jira/rest/api/2/project/TST/role/10000"), role.getSelf());
98          Assert.assertEquals(10000, role.getId().longValue());
99          Assert.assertEquals("A project role that represents users in a project", role.getDescription());
100         Assert.assertThat(
101                 role.getActors(),
102                 IsIterableContainingInAnyOrder.containsInAnyOrder(
103                         new RoleActor(null, "Administrator", "atlassian-user-role-actor", "admin",
104                                 baseJiraURI.resolve("/jira/secure/useravatar?size=small&ownerId=admin&avatarId=10054")
105                         ),
106                         new RoleActor(10020l, "jira-users", "atlassian-group-role-actor", "jira-users",
107                                 baseJiraURI.resolve("/jira/secure/useravatar?size=small&avatarId=10083")
108                         ),
109                         new RoleActor(10030l, "Wojciech Seliga", "atlassian-user-role-actor", "wseliga",
110                                 baseJiraURI.resolve("/jira/secure/useravatar?size=small&avatarId=10082"))
111                 )
112         );
113     }
114 
115 }