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