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.domain.RoleActor;
19  import org.codehaus.jettison.json.JSONException;
20  import org.junit.Rule;
21  import org.junit.Test;
22  import org.junit.rules.ExpectedException;
23  
24  import java.net.URI;
25  
26  import static com.atlassian.jira.rest.client.TestUtil.toUri;
27  import static com.atlassian.jira.rest.client.internal.json.ResourceUtil.getJsonObjectFromResource;
28  import static junit.framework.Assert.assertEquals;
29  import static junit.framework.Assert.assertNull;
30  
31  public class RoleActorJsonParserTest {
32  
33  	private URI baseJiraURI = toUri("http://localhost:2990");
34  	private final RoleActorJsonParser roleActorJsonParser = new RoleActorJsonParser(baseJiraURI);
35  
36  	@Rule
37  	public final ExpectedException exception = ExpectedException.none();
38  
39  	@Test
40  	public void testParseValidActorWithOptionalParam() throws Exception {
41  		final RoleActor actor = roleActorJsonParser.parse(getJsonObjectFromResource("/json/actor/valid-actor.json"));
42  		assertEquals(10020l, actor.getId().longValue());
43  		assertEquals("jira-users", actor.getName());
44  		assertEquals("jira-users", actor.getDisplayName());
45  		assertEquals("atlassian-group-role-actor", actor.getType());
46  		assertEquals(toUri(baseJiraURI.toString() + "/jira/secure/useravatar?size=small&avatarId=10083"), actor.getAvatarUri());
47  	}
48  
49  	@Test
50  	public void testParseValidActorWithoutOptionalParams() throws JSONException {
51  		final RoleActor actor = roleActorJsonParser.parse(getJsonObjectFromResource("/json/actor/valid-actor-without-avatar.json"));
52  		assertEquals(10020l, actor.getId().longValue());
53  		assertEquals("jira-users", actor.getName());
54  		assertEquals("jira-users", actor.getDisplayName());
55  		assertEquals("atlassian-group-role-actor", actor.getType());
56  		assertNull(actor.getAvatarUri());
57  	}
58  
59  	@Test
60  	public void testParseInvalidActor() throws Exception {
61  		exception.expect(JSONException.class);
62  		exception.expectMessage("JSONObject[\"type\"] not found.");
63  		roleActorJsonParser.parse(getJsonObjectFromResource("/json/actor/invalid-actor-without-required-fields.json"));
64  	}
65  }