View Javadoc

1   /*
2    * Copyright (C) 2011 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.api.domain.BasicPriority;
20  import com.atlassian.jira.rest.client.api.domain.BasicProject;
21  import com.atlassian.jira.rest.client.api.domain.BasicVotes;
22  import com.atlassian.jira.rest.client.api.domain.BasicWatchers;
23  import com.atlassian.jira.rest.client.api.domain.Issue;
24  import com.atlassian.jira.rest.client.api.domain.IssueType;
25  import com.atlassian.jira.rest.client.api.domain.SearchResult;
26  import com.atlassian.jira.rest.client.api.domain.Status;
27  import com.google.common.collect.Iterables;
28  import org.codehaus.jettison.json.JSONException;
29  import org.junit.Rule;
30  import org.junit.Test;
31  import org.junit.rules.ExpectedException;
32  
33  import static com.atlassian.jira.rest.client.TestUtil.assertEmptyIterable;
34  import static com.atlassian.jira.rest.client.TestUtil.toDateTime;
35  import static com.atlassian.jira.rest.client.TestUtil.toUri;
36  import static com.atlassian.jira.rest.client.api.domain.EntityHelper.findEntityById;
37  import static com.atlassian.jira.rest.client.internal.json.ResourceUtil.getJsonObjectFromResource;
38  import static com.atlassian.jira.rest.client.test.matchers.IssueMatchers.issuesWithKeys;
39  import static com.atlassian.jira.rest.client.test.matchers.SearchResultMatchers.searchResultWithParamsAndIssueCount;
40  import static org.junit.Assert.assertEquals;
41  import static org.junit.Assert.assertNull;
42  import static org.junit.Assert.assertThat;
43  
44  public class SearchResultJsonParserTest {
45  
46      @Rule
47      public final ExpectedException exception = ExpectedException.none();
48  
49      final SearchResultJsonParser parser = new SearchResultJsonParser();
50  
51      @Test
52      public void testParse() throws Exception {
53          final SearchResult searchResult = parser.parse(getJsonObjectFromResource("/json/search/issues1.json"));
54  
55          assertThat(searchResult, searchResultWithParamsAndIssueCount(0, 50, 1, 1));
56  
57          final Issue foundIssue = Iterables.getLast(searchResult.getIssues());
58          assertIssueIsTST7(foundIssue);
59      }
60  
61      @Test
62      public void testParseMany() throws Exception {
63          final SearchResult searchResult = parser.parse(getJsonObjectFromResource("/json/search/many-issues.json"));
64  
65          assertThat(searchResult, searchResultWithParamsAndIssueCount(0, 8, 15, 8));
66  
67          final Issue issue = findEntityById(searchResult.getIssues(), 10040L);
68          assertIssueIsTST7(issue);
69  
70          final String[] expectedIssuesKeys = {"TST-13", "TST-12", "TST-11", "TST-10", "TST-9", "TST-8", "TST-7", "TST-6"};
71          assertThat(searchResult.getIssues(), issuesWithKeys(expectedIssuesKeys));
72      }
73  
74      @Test
75      public void testParseInvalidTotal() throws Exception {
76          exception.expect(JSONException.class);
77          exception.expectMessage("JSONObject[\"total\"] is not a number.");
78  
79          parser.parse(getJsonObjectFromResource("/json/search/issues-invalid-total.json"));
80      }
81  
82      private void assertIssueIsTST7(Issue issue) {
83          assertEquals("TST-7", issue.getKey());
84          assertEquals(Long.valueOf(10040), issue.getId());
85          assertEquals(toUri("http://localhost:8090/jira/rest/api/latest/issue/10040"), issue.getSelf());
86          assertEquals("A task where someone will vote", issue.getSummary());
87          assertNull(issue.getDescription()); // by default search doesn't retrieve description
88  
89          final BasicPriority expectedPriority = new BasicPriority(toUri("http://localhost:8090/jira/rest/api/2/priority/3"), 3L, "Major");
90          assertEquals(expectedPriority, issue.getPriority());
91  
92          final Status expectedStatus = new Status(toUri("http://localhost:8090/jira/rest/api/2/status/1"), 1L, "Open", "The issue is open and ready for the assignee to start work on it.", toUri("http://localhost:8090/jira/images/icons/status_open.gif"));
93          assertEquals(expectedStatus, issue.getStatus());
94  
95          assertEmptyIterable(issue.getComments());
96          assertEmptyIterable(issue.getComments());
97          assertEmptyIterable(issue.getComponents());
98          assertEmptyIterable(issue.getWorklogs());
99          assertEmptyIterable(issue.getSubtasks());
100         assertEmptyIterable(issue.getIssueLinks());
101         assertEmptyIterable(issue.getFixVersions());
102         assertEmptyIterable(issue.getAffectedVersions());
103         assertEmptyIterable(issue.getLabels());
104         assertNull(issue.getDueDate());
105         assertNull(issue.getTimeTracking());
106         assertNull(issue.getResolution());
107         assertNull(issue.getChangelog());
108         assertNull(issue.getAttachments());
109         assertEquals(toDateTime("2010-09-22T18:06:32.000+02:00"), issue.getUpdateDate());
110         assertEquals(toDateTime("2010-09-22T18:06:32.000+02:00"), issue.getCreationDate());
111         assertEquals(TestConstants.USER1, issue.getReporter());
112         assertEquals(TestConstants.USER_ADMIN, issue.getAssignee());
113 
114         final BasicProject expectedProject = new BasicProject(toUri("http://localhost:8090/jira/rest/api/2/project/TST"), "TST", 10000L, "Test Project");
115         assertEquals(expectedProject, issue.getProject());
116 
117         final BasicVotes expectedVotes = new BasicVotes(toUri("http://localhost:8090/jira/rest/api/2/issue/TST-7/votes"), 0, false);
118         assertEquals(expectedVotes, issue.getVotes());
119 
120         final BasicWatchers expectedWatchers = new BasicWatchers(toUri("http://localhost:8090/jira/rest/api/2/issue/TST-7/watchers"), false, 0);
121         assertEquals(expectedWatchers, issue.getWatchers());
122 
123         final IssueType expectedIssueType = new IssueType(toUri("http://localhost:8090/jira/rest/api/2/issuetype/3"), 3L, "Task", false, "A task that needs to be done.", toUri("http://localhost:8090/jira/images/icons/task.gif"));
124         assertEquals(expectedIssueType, issue.getIssueType());
125     }
126 
127 }