PageRenderTime 46ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/apps/collaboration/document-library/document-library-repository-cmis-api/src/test/java/com/liferay/document/library/repository/cmis/search/BaseCmisSearchQueryBuilderTest.java

http://github.com/liferay/liferay-portal
Java | 502 lines | 338 code | 147 blank | 17 comment | 0 complexity | 9cd23e6d38bf1e7599654f13dfdce5b9 MD5 | raw file
Possible License(s): LGPL-2.0
  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.document.library.repository.cmis.search;
  15. import com.liferay.document.library.kernel.service.DLAppService;
  16. import com.liferay.document.library.repository.search.internal.LuceneRepositorySearchQueryTermBuilder;
  17. import com.liferay.document.library.repository.search.internal.RepositorySearchQueryBuilderImpl;
  18. import com.liferay.portal.kernel.model.RepositoryEntry;
  19. import com.liferay.portal.kernel.repository.search.RepositorySearchQueryBuilder;
  20. import com.liferay.portal.kernel.repository.search.RepositorySearchQueryTermBuilder;
  21. import com.liferay.portal.kernel.search.BooleanQuery;
  22. import com.liferay.portal.kernel.search.QueryConfig;
  23. import com.liferay.portal.kernel.search.SearchContext;
  24. import com.liferay.portal.kernel.search.SearchEngineHelper;
  25. import com.liferay.portal.kernel.service.RepositoryEntryLocalService;
  26. import com.liferay.portal.kernel.service.UserLocalService;
  27. import com.liferay.portal.kernel.test.util.RandomTestUtil;
  28. import com.liferay.portal.kernel.util.DateFormatFactory;
  29. import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
  30. import com.liferay.portal.kernel.util.Props;
  31. import com.liferay.portal.kernel.util.PropsKeys;
  32. import com.liferay.portal.kernel.util.PropsUtil;
  33. import java.text.SimpleDateFormat;
  34. import org.apache.chemistry.opencmis.commons.enums.CapabilityQuery;
  35. import org.junit.Assert;
  36. import org.junit.Before;
  37. import org.junit.Test;
  38. import org.mockito.Mock;
  39. import org.mockito.Mockito;
  40. import org.mockito.MockitoAnnotations;
  41. /**
  42. * @author Mika Koivisto
  43. * @author André de Oliveira
  44. */
  45. public class BaseCmisSearchQueryBuilderTest {
  46. @Before
  47. public void setUp() throws Exception {
  48. MockitoAnnotations.initMocks(this);
  49. setUpPropsUtil();
  50. setUpDateFormatFactoryUtil();
  51. _cmisSearchQueryBuilder = new BaseCmisSearchQueryBuilder(
  52. createRepositoryEntryLocalService(),
  53. Mockito.mock(UserLocalService.class));
  54. }
  55. @Test
  56. public void testBooleanQuery() throws Exception {
  57. SearchContext searchContext = getSearchContext();
  58. searchContext.setKeywords("+test* -test.doc");
  59. String cmisQuery = buildQuery(searchContext);
  60. assertQueryEquals(
  61. "((cmis:name LIKE 'test%' AND NOT(cmis:name = 'test.doc')) OR " +
  62. "(cmis:createdBy LIKE 'test%' AND NOT(cmis:createdBy = " +
  63. "'test.doc')))",
  64. cmisQuery);
  65. }
  66. @Test
  67. public void testContainsCombinedSupportedQuery() throws Exception {
  68. SearchContext searchContext = getSearchContext();
  69. searchContext.setKeywords("test");
  70. QueryConfig queryConfig = searchContext.getQueryConfig();
  71. queryConfig.setAttribute(
  72. "capabilityQuery", CapabilityQuery.BOTHCOMBINED.value());
  73. String cmisQuery = buildQuery(searchContext);
  74. assertQueryEquals(
  75. "((cmis:name = 'test' OR cmis:createdBy = 'test') OR " +
  76. "CONTAINS('test'))",
  77. cmisQuery);
  78. }
  79. @Test
  80. public void testContainsCombinedSupportedWildcardQuery() throws Exception {
  81. SearchContext searchContext = getSearchContext();
  82. searchContext.setKeywords("test*.jpg");
  83. QueryConfig queryConfig = searchContext.getQueryConfig();
  84. queryConfig.setAttribute(
  85. "capabilityQuery", CapabilityQuery.BOTHCOMBINED.value());
  86. String cmisQuery = buildQuery(searchContext);
  87. assertQueryEquals(
  88. "((cmis:name LIKE 'test%.jpg' OR cmis:createdBy LIKE " +
  89. "'test%.jpg') OR CONTAINS('(test AND .jpg)'))",
  90. cmisQuery);
  91. }
  92. @Test
  93. public void testContainsOnlySupportedQuery() throws Exception {
  94. SearchContext searchContext = getSearchContext();
  95. searchContext.setKeywords("test");
  96. QueryConfig queryConfig = searchContext.getQueryConfig();
  97. queryConfig.setAttribute(
  98. "capabilityQuery", CapabilityQuery.FULLTEXTONLY.value());
  99. String cmisQuery = buildQuery(searchContext);
  100. assertQueryEquals("CONTAINS('test')", cmisQuery);
  101. }
  102. @Test
  103. public void testContainsOnlySupportedQueryMultipleKeywords()
  104. throws Exception {
  105. SearchContext searchContext = getSearchContext();
  106. searchContext.setKeywords("test multiple");
  107. QueryConfig queryConfig = searchContext.getQueryConfig();
  108. queryConfig.setAttribute(
  109. "capabilityQuery", CapabilityQuery.FULLTEXTONLY.value());
  110. String cmisQuery = buildQuery(searchContext);
  111. assertQueryEquals("CONTAINS('(test OR multiple)')", cmisQuery);
  112. }
  113. @Test
  114. public void testContainsOnlySupportedQueryWithConjunction()
  115. throws Exception {
  116. SearchContext searchContext = getSearchContext();
  117. searchContext.setKeywords("+test +multiple");
  118. QueryConfig queryConfig = searchContext.getQueryConfig();
  119. queryConfig.setAttribute(
  120. "capabilityQuery", CapabilityQuery.FULLTEXTONLY.value());
  121. String cmisQuery = buildQuery(searchContext);
  122. assertQueryEquals("CONTAINS('(test multiple)')", cmisQuery);
  123. }
  124. @Test
  125. public void testContainsOnlySupportedQueryWithNegation() throws Exception {
  126. SearchContext searchContext = getSearchContext();
  127. searchContext.setKeywords("test -multiple");
  128. QueryConfig queryConfig = searchContext.getQueryConfig();
  129. queryConfig.setAttribute(
  130. "capabilityQuery", CapabilityQuery.FULLTEXTONLY.value());
  131. String cmisQuery = buildQuery(searchContext);
  132. assertQueryEquals("CONTAINS('(-multiple OR test)')", cmisQuery);
  133. }
  134. @Test
  135. public void testContainsOnlySupportedQueryWithNegationPhrase()
  136. throws Exception {
  137. SearchContext searchContext = getSearchContext();
  138. searchContext.setKeywords("test -\"multiple words\"");
  139. QueryConfig queryConfig = searchContext.getQueryConfig();
  140. queryConfig.setAttribute(
  141. "capabilityQuery", CapabilityQuery.FULLTEXTONLY.value());
  142. String cmisQuery = buildQuery(searchContext);
  143. assertQueryEquals(
  144. "CONTAINS('(-\\'multiple words\\' OR test)')", cmisQuery);
  145. }
  146. @Test
  147. public void testContainsOnlySupportedWithApostrophe() throws Exception {
  148. SearchContext searchContext = getSearchContext();
  149. searchContext.setKeywords("test's");
  150. QueryConfig queryConfig = searchContext.getQueryConfig();
  151. queryConfig.setAttribute(
  152. "capabilityQuery", CapabilityQuery.FULLTEXTONLY.value());
  153. String cmisQuery = buildQuery(searchContext);
  154. assertQueryEquals("CONTAINS('test\\'s')", cmisQuery);
  155. }
  156. @Test
  157. public void testExactFilenameQuery() throws Exception {
  158. SearchContext searchContext = getSearchContext();
  159. searchContext.setKeywords("test.jpg");
  160. String cmisQuery = buildQuery(searchContext);
  161. assertQueryEquals(
  162. "(cmis:name = 'test.jpg' OR cmis:createdBy = 'test.jpg')",
  163. cmisQuery);
  164. }
  165. @Test
  166. public void testFolderQuery() throws Exception {
  167. String folderQuery = buildFolderQuery(false);
  168. assertQueryEquals(
  169. "((IN_FOLDER('" + _MAPPED_ID + "') AND (cmis:name = 'test' OR " +
  170. "cmis:createdBy = 'test')) OR CONTAINS('test'))",
  171. folderQuery);
  172. }
  173. @Test
  174. public void testFuzzyQuery() throws Exception {
  175. SearchContext searchContext = getSearchContext();
  176. searchContext.setKeywords("test~");
  177. String cmisQuery = buildQuery(searchContext);
  178. assertQueryEquals(
  179. "(cmis:name LIKE 'test%' OR cmis:createdBy LIKE 'test%')",
  180. cmisQuery);
  181. }
  182. @Test
  183. public void testPhraseQuery() throws Exception {
  184. SearchContext searchContext = getSearchContext();
  185. searchContext.setKeywords("\"My test document.jpg\"");
  186. String cmisQuery = buildQuery(searchContext);
  187. assertQueryEquals(
  188. "(cmis:name = 'My test document.jpg' OR cmis:createdBy = 'My " +
  189. "test document.jpg')",
  190. cmisQuery);
  191. }
  192. @Test
  193. public void testPrefixQuery() throws Exception {
  194. SearchContext searchContext = getSearchContext();
  195. searchContext.setKeywords("Test*");
  196. String cmisQuery = buildQuery(searchContext);
  197. assertQueryEquals(
  198. "(cmis:name LIKE 'Test%' OR cmis:createdBy LIKE 'Test%')",
  199. cmisQuery);
  200. }
  201. @Test
  202. public void testProximityQuery() throws Exception {
  203. SearchContext searchContext = getSearchContext();
  204. searchContext.setKeywords("\"test document\"~10");
  205. String cmisQuery = buildQuery(searchContext);
  206. assertQueryEquals(
  207. "(cmis:name = 'test document' OR cmis:createdBy = 'test " +
  208. "document')",
  209. cmisQuery);
  210. }
  211. @Test
  212. public void testRangeQuery() throws Exception {
  213. SearchContext searchContext = getSearchContext();
  214. searchContext.setKeywords(
  215. "createDate:[20091011000000 TO 20091110235959]");
  216. String cmisQuery = buildQuery(searchContext);
  217. assertQueryEquals(
  218. "cmis:creationDate >= 2009-10-11T00:00:00.000Z AND " +
  219. "cmis:creationDate <= 2009-11-10T23:59:59.000Z",
  220. cmisQuery);
  221. }
  222. @Test
  223. public void testSubfolderQuery() throws Exception {
  224. String folderQuery = buildFolderQuery(true);
  225. assertQueryEquals(
  226. "((IN_TREE('" + _MAPPED_ID + "') AND (cmis:name = 'test' OR " +
  227. "cmis:createdBy = 'test')) OR CONTAINS('test'))",
  228. folderQuery);
  229. }
  230. @Test
  231. public void testWildcardFieldQuery() throws Exception {
  232. SearchContext searchContext = getSearchContext();
  233. searchContext.setKeywords("+title:test*.jpg +userName:bar*");
  234. String cmisQuery = buildQuery(searchContext);
  235. assertQueryEquals(
  236. "(cmis:name LIKE 'test%.jpg' AND cmis:createdBy LIKE 'bar%')",
  237. cmisQuery);
  238. }
  239. @Test
  240. public void testWildcardQuery() throws Exception {
  241. SearchContext searchContext = getSearchContext();
  242. searchContext.setKeywords("test*.jpg");
  243. String cmisQuery = buildQuery(searchContext);
  244. assertQueryEquals(
  245. "(cmis:name LIKE 'test%.jpg' OR cmis:createdBy LIKE 'test%.jpg')",
  246. cmisQuery);
  247. }
  248. protected void assertQueryEquals(String where, String query) {
  249. Assert.assertEquals(_QUERY_PREFIX + where + _QUERY_POSTFIX, query);
  250. }
  251. protected String buildFolderQuery(boolean searchSubfolders)
  252. throws Exception {
  253. SearchContext searchContext = getSearchContext();
  254. searchContext.setFolderIds(new long[] {_DL_FOLDER_ID});
  255. searchContext.setKeywords("test");
  256. QueryConfig queryConfig = searchContext.getQueryConfig();
  257. queryConfig.setAttribute(
  258. "capabilityQuery", CapabilityQuery.BOTHCOMBINED.value());
  259. queryConfig.setSearchSubfolders(searchSubfolders);
  260. return buildQuery(searchContext);
  261. }
  262. protected String buildQuery(SearchContext searchContext) throws Exception {
  263. return _cmisSearchQueryBuilder.buildQuery(
  264. searchContext, getFullQuery(searchContext));
  265. }
  266. protected DateFormatFactory createDateFormatFactory(String pattern) {
  267. DateFormatFactory dateFormatFactory = Mockito.mock(
  268. DateFormatFactory.class);
  269. setUpPattern(dateFormatFactory, pattern);
  270. setUpPattern(dateFormatFactory, "yyyy-MM-dd'T'HH:mm:ss.000'Z'");
  271. return dateFormatFactory;
  272. }
  273. protected RepositoryEntry createRepositoryEntry() {
  274. RepositoryEntry repositoryEntry = Mockito.mock(RepositoryEntry.class);
  275. Mockito.doReturn(
  276. _MAPPED_ID
  277. ).when(
  278. repositoryEntry
  279. ).getMappedId();
  280. return repositoryEntry;
  281. }
  282. protected RepositoryEntryLocalService createRepositoryEntryLocalService() {
  283. RepositoryEntryLocalService repositoryEntryLocalService = Mockito.mock(
  284. RepositoryEntryLocalService.class);
  285. Mockito.doReturn(
  286. createRepositoryEntry()
  287. ).when(
  288. repositoryEntryLocalService
  289. ).fetchRepositoryEntry(Mockito.anyLong());
  290. return repositoryEntryLocalService;
  291. }
  292. protected RepositorySearchQueryBuilder
  293. createRepositorySearchQueryBuilder() {
  294. return new RepositorySearchQueryBuilderImpl() {
  295. {
  296. setDLAppService(Mockito.mock(DLAppService.class));
  297. setRepositorySearchQueryTermBuilder(
  298. createRepositorySearchQueryTermBuilder());
  299. }
  300. };
  301. }
  302. protected RepositorySearchQueryTermBuilder
  303. createRepositorySearchQueryTermBuilder() {
  304. return new LuceneRepositorySearchQueryTermBuilder() {
  305. {
  306. activate(null);
  307. }
  308. };
  309. }
  310. protected BooleanQuery getFullQuery(SearchContext searchContext)
  311. throws Exception {
  312. RepositorySearchQueryBuilder repositorySearchQueryBuilder =
  313. createRepositorySearchQueryBuilder();
  314. return repositorySearchQueryBuilder.getFullQuery(searchContext);
  315. }
  316. protected SearchContext getSearchContext() {
  317. SearchContext searchContext = new SearchContext();
  318. searchContext.setSearchEngineId(SearchEngineHelper.GENERIC_ENGINE_ID);
  319. QueryConfig queryConfig = searchContext.getQueryConfig();
  320. queryConfig.setScoreEnabled(true);
  321. return searchContext;
  322. }
  323. protected void setUpDateFormatFactoryUtil() {
  324. String pattern = _INDEX_DATE_FORMAT_PATTERN;
  325. Mockito.doReturn(
  326. pattern
  327. ).when(
  328. _props
  329. ).get(PropsKeys.INDEX_DATE_FORMAT_PATTERN);
  330. DateFormatFactoryUtil dateFormatFactoryUtil =
  331. new DateFormatFactoryUtil();
  332. dateFormatFactoryUtil.setDateFormatFactory(
  333. createDateFormatFactory(pattern));
  334. }
  335. protected void setUpPattern(
  336. DateFormatFactory dateFormatFactory, String pattern) {
  337. Mockito.doReturn(
  338. new SimpleDateFormat(pattern)
  339. ).when(
  340. dateFormatFactory
  341. ).getSimpleDateFormat(pattern);
  342. }
  343. protected void setUpPropsUtil() {
  344. PropsUtil.setProps(_props);
  345. }
  346. private static final long _DL_FOLDER_ID = RandomTestUtil.randomLong();
  347. private static final String _INDEX_DATE_FORMAT_PATTERN = "yyyyMMddHHmmss";
  348. private static final String _MAPPED_ID = "1000";
  349. private static final String _QUERY_POSTFIX = " ORDER BY HITS DESC";
  350. private static final String _QUERY_PREFIX =
  351. "SELECT cmis:objectId, SCORE() AS HITS FROM cmis:document WHERE ";
  352. private CMISSearchQueryBuilder _cmisSearchQueryBuilder;
  353. @Mock
  354. private Props _props;
  355. }