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