/modules/apps/journal/journal-test/src/testIntegration/java/com/liferay/journal/search/test/JournalFolderIndexerLocalizedTest.java

https://github.com/kiyoshilee/liferay-portal · Java · 206 lines · 137 code · 52 blank · 17 comment · 2 complexity · cdc43519d8b5454a6ea8d92dc6c7dd49 MD5 · raw file

  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.journal.search.test;
  15. import com.liferay.arquillian.extension.junit.bridge.junit.Arquillian;
  16. import com.liferay.journal.model.JournalFolder;
  17. import com.liferay.journal.model.JournalFolderConstants;
  18. import com.liferay.journal.test.util.JournalTestUtil;
  19. import com.liferay.petra.string.StringPool;
  20. import com.liferay.portal.kernel.model.Group;
  21. import com.liferay.portal.kernel.search.Document;
  22. import com.liferay.portal.kernel.search.Hits;
  23. import com.liferay.portal.kernel.search.Indexer;
  24. import com.liferay.portal.kernel.search.IndexerRegistry;
  25. import com.liferay.portal.kernel.search.QueryConfig;
  26. import com.liferay.portal.kernel.search.SearchContext;
  27. import com.liferay.portal.kernel.security.auth.CompanyThreadLocal;
  28. import com.liferay.portal.kernel.service.ServiceContext;
  29. import com.liferay.portal.kernel.test.rule.AggregateTestRule;
  30. import com.liferay.portal.kernel.test.rule.DeleteAfterTestRun;
  31. import com.liferay.portal.kernel.test.util.GroupTestUtil;
  32. import com.liferay.portal.kernel.test.util.SearchContextTestUtil;
  33. import com.liferay.portal.kernel.test.util.ServiceContextTestUtil;
  34. import com.liferay.portal.kernel.test.util.TestPropsValues;
  35. import com.liferay.portal.kernel.test.util.UserTestUtil;
  36. import com.liferay.portal.kernel.util.LocaleUtil;
  37. import com.liferay.portal.search.test.util.FieldValuesAssert;
  38. import com.liferay.portal.test.rule.Inject;
  39. import com.liferay.portal.test.rule.LiferayIntegrationTestRule;
  40. import java.util.Collections;
  41. import java.util.List;
  42. import java.util.Locale;
  43. import org.junit.Before;
  44. import org.junit.ClassRule;
  45. import org.junit.Rule;
  46. import org.junit.Test;
  47. import org.junit.runner.RunWith;
  48. /**
  49. * @author Yasuyuki Takeo
  50. * @author André de Oliveira
  51. */
  52. @RunWith(Arquillian.class)
  53. public class JournalFolderIndexerLocalizedTest {
  54. @ClassRule
  55. @Rule
  56. public static final AggregateTestRule aggregateTestRule =
  57. new LiferayIntegrationTestRule();
  58. @Before
  59. public void setUp() throws Exception {
  60. _group = GroupTestUtil.addGroup();
  61. _indexer = _indexerRegistry.getIndexer(JournalFolder.class);
  62. UserTestUtil.setUser(TestPropsValues.getUser());
  63. CompanyThreadLocal.setCompanyId(TestPropsValues.getCompanyId());
  64. List<Locale> availableLocales = Collections.singletonList(
  65. LocaleUtil.JAPAN);
  66. GroupTestUtil.updateDisplaySettings(
  67. _group.getGroupId(), availableLocales, LocaleUtil.JAPAN);
  68. }
  69. @Test
  70. public void testJapaneseDescription() throws Exception {
  71. ServiceContext serviceContext = _getServiceContext();
  72. String description = "諸行無常";
  73. String title = "平家物語";
  74. _addFolder(serviceContext, title, description);
  75. String searchTerm = "諸行";
  76. Document document = _search(searchTerm, LocaleUtil.JAPAN);
  77. FieldValuesAssert.assertFieldValues(
  78. Collections.singletonMap("description_ja_JP", "諸行無常"),
  79. "description", document, searchTerm);
  80. }
  81. @Test
  82. public void testJapaneseSearchWithSimilarTexts() throws Exception {
  83. ServiceContext serviceContext = _getServiceContext();
  84. String description1 = "渋谷";
  85. String title1 = "東京都";
  86. _addFolder(serviceContext, title1, description1);
  87. String description2 = "三年坂";
  88. String title2 = "京都";
  89. _addFolder(serviceContext, title2, description2);
  90. String searchTerm = "東京";
  91. Document document = _search(searchTerm, LocaleUtil.JAPAN);
  92. FieldValuesAssert.assertFieldValues(
  93. Collections.singletonMap("title_ja_JP", "東京都"), "title", document,
  94. searchTerm);
  95. }
  96. @Test
  97. public void testJapaneseTitle() throws Exception {
  98. ServiceContext serviceContext = _getServiceContext();
  99. String description = "諸行無常";
  100. String title = "平家物語";
  101. _addFolder(serviceContext, title, description);
  102. String searchTerm = "平家";
  103. Document document = _search(searchTerm, LocaleUtil.JAPAN);
  104. FieldValuesAssert.assertFieldValues(
  105. Collections.singletonMap("title_ja_JP", "平家物語"), "title", document,
  106. searchTerm);
  107. }
  108. private void _addFolder(
  109. ServiceContext serviceContext, String title, String description)
  110. throws Exception {
  111. JournalTestUtil.addFolder(
  112. JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID, title, description,
  113. serviceContext);
  114. }
  115. private SearchContext _getSearchContext(String searchTerm, Locale locale)
  116. throws Exception {
  117. SearchContext searchContext = SearchContextTestUtil.getSearchContext(
  118. _group.getGroupId());
  119. searchContext.setKeywords(searchTerm);
  120. searchContext.setLocale(locale);
  121. QueryConfig queryConfig = searchContext.getQueryConfig();
  122. queryConfig.setSelectedFieldNames(StringPool.STAR);
  123. return searchContext;
  124. }
  125. private ServiceContext _getServiceContext() throws Exception {
  126. return ServiceContextTestUtil.getServiceContext(
  127. _group.getGroupId(), TestPropsValues.getUserId());
  128. }
  129. private Document _getSingleDocument(String searchTerm, Hits hits) {
  130. List<Document> documents = hits.toList();
  131. if (documents.size() == 1) {
  132. return documents.get(0);
  133. }
  134. throw new AssertionError(searchTerm + "->" + documents);
  135. }
  136. private Document _search(String searchTerm, Locale locale) {
  137. try {
  138. SearchContext searchContext = _getSearchContext(searchTerm, locale);
  139. Hits hits = _indexer.search(searchContext);
  140. return _getSingleDocument(searchTerm, hits);
  141. }
  142. catch (RuntimeException re) {
  143. throw re;
  144. }
  145. catch (Exception e) {
  146. throw new RuntimeException(e);
  147. }
  148. }
  149. @Inject
  150. private static IndexerRegistry _indexerRegistry;
  151. @DeleteAfterTestRun
  152. private Group _group;
  153. private Indexer<JournalFolder> _indexer;
  154. }