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