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