View Javadoc

1   /*
2    * Copyright (C) 2010 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  
17  package com.atlassian.jira.rest.client.internal.json;
18  
19  import com.atlassian.jira.rest.client.TestUtil;
20  import com.atlassian.jira.rest.client.api.OptionalIterable;
21  import com.atlassian.jira.rest.client.api.domain.BasicProjectRole;
22  import com.atlassian.jira.rest.client.api.domain.IssueType;
23  import com.atlassian.jira.rest.client.api.domain.Project;
24  import com.google.common.collect.Iterables;
25  import org.codehaus.jettison.json.JSONException;
26  import org.hamcrest.collection.IsEmptyIterable;
27  import org.hamcrest.collection.IsIterableContainingInAnyOrder;
28  import org.joda.time.DateMidnight;
29  import org.junit.Assert;
30  import org.junit.Test;
31  
32  import java.net.URISyntaxException;
33  
34  import static org.junit.Assert.assertEquals;
35  
36  // Ignore "May produce NPE" warnings, as we know what we are doing in tests
37  @SuppressWarnings("ConstantConditions")
38  public class ProjectJsonParserTest {
39      private final ProjectJsonParser parser = new ProjectJsonParser();
40  
41      @Test
42      public void testParse() throws Exception {
43          final Project project = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/project/valid.json"));
44          Assert.assertEquals(TestUtil.toUri("http://localhost:8090/jira/rest/api/latest/project/TST"), project.getSelf());
45          Assert.assertEquals("This is my description here.\r\nAnother line.", project.getDescription());
46          assertEquals(TestConstants.USER_ADMIN_BASIC_DEPRECATED, project.getLead());
47          Assert.assertEquals("http://example.com", project.getUri().toString());
48          Assert.assertEquals("TST", project.getKey());
49          Assert.assertEquals(Long.valueOf(10000), project.getId());
50          Assert.assertThat(project.getVersions(), IsIterableContainingInAnyOrder
51                  .containsInAnyOrder(TestConstants.VERSION_1, TestConstants.VERSION_1_1));
52          Assert.assertThat(project.getComponents(), IsIterableContainingInAnyOrder
53                  .containsInAnyOrder(TestConstants.BCOMPONENT_A, TestConstants.BCOMPONENT_B));
54          Assert.assertNull(project.getName());
55          final OptionalIterable<IssueType> issueTypes = project.getIssueTypes();
56          Assert.assertFalse(issueTypes.isSupported());
57          Assert.assertThat(issueTypes, IsEmptyIterable.<IssueType>emptyIterable());
58      }
59  
60      @Test
61      public void testParseWithoutId() throws Exception {
62          final Project project = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/project/valid-without-id.json"));
63          Assert.assertEquals("TST", project.getKey());
64          Assert.assertNull(project.getId());
65      }
66  
67      @Test
68      public void testParseProjectWithNoUrl() throws JSONException {
69          final Project project = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/project/project-no-url.json"));
70          Assert.assertEquals("MYT", project.getKey());
71          Assert.assertNull(project.getUri());
72          Assert.assertNull(project.getDescription());
73      }
74  
75      @Test
76      public void testParseProjectInJira5x0() throws JSONException, URISyntaxException {
77          final Project project = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/project/project-jira-5-0.json"));
78          Assert.assertEquals("TST", project.getKey());
79          Assert.assertEquals(new DateMidnight(2010, 8, 25).toInstant(), Iterables.getLast(project.getVersions()).getReleaseDate()
80                  .toInstant());
81          Assert.assertEquals("Test Project", project.getName());
82          final OptionalIterable<IssueType> issueTypes = project.getIssueTypes();
83          Assert.assertTrue(issueTypes.isSupported());
84          Assert.assertThat(issueTypes, IsIterableContainingInAnyOrder.containsInAnyOrder(
85                  new IssueType(TestUtil
86                          .toUri("http://localhost:2990/jira/rest/api/latest/issuetype/1"), 1L, "Bug", false, "A problem which impairs or prevents the functions of the product.", TestUtil
87                          .toUri("http://localhost:2990/jira/images/icons/bug.gif")),
88                  new IssueType(TestUtil
89                          .toUri("http://localhost:2990/jira/rest/api/latest/issuetype/2"), 2L, "New Feature", false, "A new feature of the product, which has yet to be developed.", TestUtil
90                          .toUri("http://localhost:2990/jira/images/icons/newfeature.gif")),
91                  new IssueType(TestUtil
92                          .toUri("http://localhost:2990/jira/rest/api/latest/issuetype/3"), 3L, "Task", false, "A task that needs to be done.", TestUtil
93                          .toUri("http://localhost:2990/jira/images/icons/task.gif")),
94                  new IssueType(TestUtil
95                          .toUri("http://localhost:2990/jira/rest/api/latest/issuetype/4"), 4L, "Improvement", false, "An improvement or enhancement to an existing feature or task.", TestUtil
96                          .toUri("http://localhost:2990/jira/images/icons/improvement.gif")),
97                  new IssueType(TestUtil
98                          .toUri("http://localhost:2990/jira/rest/api/latest/issuetype/5"), 5L, "Sub-task", true, "The sub-task of the issue", TestUtil
99                          .toUri("http://localhost:2990/jira/images/icons/issue_subtask.gif"))
100         ));
101     }
102 
103     @Test
104     public void testParseProjectWithBasicRoles() throws JSONException, URISyntaxException {
105         final Project project = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/project/project-jira-5-0.json"));
106         final Iterable<BasicProjectRole> projectRoles = project.getProjectRoles();
107         Assert.assertThat(projectRoles, IsIterableContainingInAnyOrder.containsInAnyOrder(
108                 new BasicProjectRole(TestUtil
109                         .toUri("http://localhost:2990/jira/rest/api/latest/project/TST/role/10000"), "Users"),
110                 new BasicProjectRole(TestUtil
111                         .toUri("http://localhost:2990/jira/rest/api/latest/project/TST/role/10001"), "Developers"),
112                 new BasicProjectRole(TestUtil
113                         .toUri("http://localhost:2990/jira/rest/api/latest/project/TST/role/10002"), "Administrators")
114         ));
115     }
116 }