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 it;
18  
19  import com.atlassian.jira.rest.client.TestUtil;
20  import com.atlassian.jira.rest.client.domain.SearchResult;
21  import com.atlassian.jira.rest.client.internal.ServerVersionConstants;
22  import com.google.common.collect.Iterables;
23  import org.junit.Test;
24  
25  import javax.ws.rs.core.Response;
26  
27  public class JerseySearchRestClientTest extends AbstractRestoringJiraStateJerseyRestClientTest {
28  	@Test
29  	public void testJqlSearch() {
30  		if (!isJqlSupportedByRest()) {
31  			return;
32  		}
33  		final SearchResult searchResultForNull = client.getSearchClient().searchJql(null, pm);
34  		assertEquals(9, searchResultForNull.getTotal());
35  
36  		final SearchResult searchResultForReporterWseliga = client.getSearchClient().searchJql("reporter=wseliga", pm);
37  		assertEquals(1, searchResultForReporterWseliga.getTotal());
38  
39  		setAnonymousMode();
40  		final SearchResult searchResultAsAnonymous = client.getSearchClient().searchJql(null, pm);
41  		assertEquals(1, searchResultAsAnonymous.getTotal());
42  
43  		final SearchResult searchResultForReporterWseligaAsAnonymous = client.getSearchClient().searchJql("reporter=wseliga", pm);
44  		assertEquals(0, searchResultForReporterWseligaAsAnonymous.getTotal());
45  	}
46  
47  	@Test
48  	public void testJqlSearchWithPaging() {
49  		if (!isJqlSupportedByRest()) {
50  			return;
51  		}
52  		final SearchResult searchResultForNull = client.getSearchClient().searchJql(null, 3, 3, pm);
53  		assertEquals(9, searchResultForNull.getTotal());
54  		assertEquals(3, Iterables.size(searchResultForNull.getIssues()));
55  		assertEquals(3, searchResultForNull.getStartIndex());
56  		assertEquals(3, searchResultForNull.getMaxResults());
57  
58  		final SearchResult search2 = client.getSearchClient().searchJql("assignee is not EMPTY", 2, 1, pm);
59  		assertEquals(9, search2.getTotal());
60  		assertEquals(2, Iterables.size(search2.getIssues()));
61  		assertEquals(0, search2.getStartIndex());
62  		assertEquals(2, search2.getMaxResults());
63  
64  		setUser1();
65  		final SearchResult search3 = client.getSearchClient().searchJql("assignee is not EMPTY", 10, 5, pm);
66  		assertEquals(8, search3.getTotal());
67  		assertEquals(8, Iterables.size(search3.getIssues()));
68  		assertEquals(0, search3.getStartIndex());
69  		assertEquals(10, search3.getMaxResults());
70  	}
71  
72  	@Test
73  	public void testVeryLongJqlWhichWillBePost() {
74  		if (!isJqlSupportedByRest()) {
75  			return;
76  		}
77  		final String coreJql = "summary ~ fsdsfdfds";
78  		StringBuilder sb = new StringBuilder(coreJql);
79  		for (int i = 0; i < 500; i++) {
80  			sb.append(" and (reporter is not empty)"); // building very long JQL query
81  		}
82  		sb.append(" or summary is not empty"); // so that effectively all issues are returned;
83  		final SearchResult searchResultForNull = client.getSearchClient().searchJql(sb.toString(), 3, 6, pm);
84  		assertEquals(9, searchResultForNull.getTotal());
85  		assertEquals(3, Iterables.size(searchResultForNull.getIssues()));
86  		assertEquals(6, searchResultForNull.getStartIndex());
87  		assertEquals(3, searchResultForNull.getMaxResults());
88  	}
89  
90  
91  	@Test
92  	public void testJqlSearchUnescapedCharacter() {
93  		if (!isJqlSupportedByRest()) {
94  			return;
95  		}
96  		TestUtil.assertErrorCode(Response.Status.BAD_REQUEST,
97  				"Error in the JQL Query: The character '/' is a reserved JQL character. You must enclose it in a string or use the escape '\\u002f' instead. (line 1, character 11)", new Runnable() {
98  			@Override
99  			public void run() {
100 				client.getSearchClient().searchJql("reporter=a/user/with/slash", pm);
101 			}
102 		});
103 	}
104 
105 
106 	private boolean isJqlSupportedByRest() {
107 		return client.getMetadataClient().getServerInfo(pm).getBuildNumber() >= ServerVersionConstants.BN_JIRA_4_3;
108 	}
109 
110 
111 }