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.domain.BasicIssue;
20  import com.atlassian.jira.rest.client.domain.SearchResult;
21  import com.google.common.collect.Iterables;
22  import com.google.common.collect.Lists;
23  import org.codehaus.jettison.json.JSONException;
24  import org.junit.Rule;
25  import org.junit.Test;
26  import org.junit.rules.ExpectedException;
27  
28  import java.util.ArrayList;
29  
30  import static com.atlassian.jira.rest.client.TestUtil.toUri;
31  import static org.junit.Assert.assertEquals;
32  
33  public class SearchResultJsonParserTest {
34  
35  	@Rule
36  	public final ExpectedException exception = ExpectedException.none();
37  
38  	final SearchResultJsonParser parser = new SearchResultJsonParser();
39  
40  	@Test
41  	public void testParse() throws Exception {
42  		final SearchResult searchResult = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/search/issues1.json"));
43  		final ArrayList<BasicIssue> issues = Lists.newArrayList(new BasicIssue(toUri("http://localhost:8090/jira/rest/api/latest/issue/TST-7"), "TST-7"));
44  
45  		assertEquals(new SearchResult(0, 50, 1, issues), searchResult);
46  	}
47  
48  	@Test
49  	public void testParseMany() throws Exception {
50  		final SearchResult searchResult = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/search/many-issues.json"));
51  
52  		assertEquals(9, searchResult.getTotal());
53  		assertEquals(8, searchResult.getMaxResults());
54  		assertEquals(0, searchResult.getStartIndex());
55  		assertEquals(8, Iterables.size(searchResult.getIssues()));
56  		assertEquals(new BasicIssue(toUri("http://localhost:8090/jira/rest/api/latest/issue/RST-1"), "RST-1"), Iterables.getLast(searchResult.getIssues()));
57  	}
58  
59  	@Test
60  	public void testParseInvalidTotal() throws Exception {
61  		exception.expect(JSONException.class);
62  		exception.expectMessage("JSONObject[\"total\"] is not a number.");
63  
64  		parser.parse(ResourceUtil.getJsonObjectFromResource("/json/search/issues-invalid-total.json"));
65  	}
66  
67  }