/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
- /**
- * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
- *
- * This library is free software; you can redistribute it and/or modify it under
- * the terms of the GNU Lesser General Public License as published by the Free
- * Software Foundation; either version 2.1 of the License, or (at your option)
- * any later version.
- *
- * This library is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
- * details.
- */
- package com.liferay.journal.search.test;
- import com.liferay.arquillian.extension.junit.bridge.junit.Arquillian;
- import com.liferay.journal.model.JournalFolder;
- import com.liferay.journal.model.JournalFolderConstants;
- import com.liferay.journal.test.util.JournalTestUtil;
- import com.liferay.petra.string.StringPool;
- import com.liferay.portal.kernel.model.Group;
- import com.liferay.portal.kernel.search.Document;
- import com.liferay.portal.kernel.search.Hits;
- import com.liferay.portal.kernel.search.Indexer;
- import com.liferay.portal.kernel.search.IndexerRegistry;
- import com.liferay.portal.kernel.search.QueryConfig;
- import com.liferay.portal.kernel.search.SearchContext;
- import com.liferay.portal.kernel.security.auth.CompanyThreadLocal;
- import com.liferay.portal.kernel.service.ServiceContext;
- import com.liferay.portal.kernel.test.rule.AggregateTestRule;
- import com.liferay.portal.kernel.test.rule.DeleteAfterTestRun;
- import com.liferay.portal.kernel.test.util.GroupTestUtil;
- import com.liferay.portal.kernel.test.util.SearchContextTestUtil;
- import com.liferay.portal.kernel.test.util.ServiceContextTestUtil;
- import com.liferay.portal.kernel.test.util.TestPropsValues;
- import com.liferay.portal.kernel.test.util.UserTestUtil;
- import com.liferay.portal.kernel.util.LocaleUtil;
- import com.liferay.portal.search.test.util.FieldValuesAssert;
- import com.liferay.portal.test.rule.Inject;
- import com.liferay.portal.test.rule.LiferayIntegrationTestRule;
- import java.util.Collections;
- import java.util.List;
- import java.util.Locale;
- import org.junit.Before;
- import org.junit.ClassRule;
- import org.junit.Rule;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- /**
- * @author Yasuyuki Takeo
- * @author André de Oliveira
- */
- @RunWith(Arquillian.class)
- public class JournalFolderIndexerLocalizedTest {
- @ClassRule
- @Rule
- public static final AggregateTestRule aggregateTestRule =
- new LiferayIntegrationTestRule();
- @Before
- public void setUp() throws Exception {
- _group = GroupTestUtil.addGroup();
- _indexer = _indexerRegistry.getIndexer(JournalFolder.class);
- UserTestUtil.setUser(TestPropsValues.getUser());
- CompanyThreadLocal.setCompanyId(TestPropsValues.getCompanyId());
- List<Locale> availableLocales = Collections.singletonList(
- LocaleUtil.JAPAN);
- GroupTestUtil.updateDisplaySettings(
- _group.getGroupId(), availableLocales, LocaleUtil.JAPAN);
- }
- @Test
- public void testJapaneseDescription() throws Exception {
- ServiceContext serviceContext = _getServiceContext();
- String description = "諸行無常";
- String title = "平家物語";
- _addFolder(serviceContext, title, description);
- String searchTerm = "諸行";
- Document document = _search(searchTerm, LocaleUtil.JAPAN);
- FieldValuesAssert.assertFieldValues(
- Collections.singletonMap("description_ja_JP", "諸行無常"),
- "description", document, searchTerm);
- }
- @Test
- public void testJapaneseSearchWithSimilarTexts() throws Exception {
- ServiceContext serviceContext = _getServiceContext();
- String description1 = "渋谷";
- String title1 = "東京都";
- _addFolder(serviceContext, title1, description1);
- String description2 = "三年坂";
- String title2 = "京都";
- _addFolder(serviceContext, title2, description2);
- String searchTerm = "東京";
- Document document = _search(searchTerm, LocaleUtil.JAPAN);
- FieldValuesAssert.assertFieldValues(
- Collections.singletonMap("title_ja_JP", "東京都"), "title", document,
- searchTerm);
- }
- @Test
- public void testJapaneseTitle() throws Exception {
- ServiceContext serviceContext = _getServiceContext();
- String description = "諸行無常";
- String title = "平家物語";
- _addFolder(serviceContext, title, description);
- String searchTerm = "平家";
- Document document = _search(searchTerm, LocaleUtil.JAPAN);
- FieldValuesAssert.assertFieldValues(
- Collections.singletonMap("title_ja_JP", "平家物語"), "title", document,
- searchTerm);
- }
- private void _addFolder(
- ServiceContext serviceContext, String title, String description)
- throws Exception {
- JournalTestUtil.addFolder(
- JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID, title, description,
- serviceContext);
- }
- private SearchContext _getSearchContext(String searchTerm, Locale locale)
- throws Exception {
- SearchContext searchContext = SearchContextTestUtil.getSearchContext(
- _group.getGroupId());
- searchContext.setKeywords(searchTerm);
- searchContext.setLocale(locale);
- QueryConfig queryConfig = searchContext.getQueryConfig();
- queryConfig.setSelectedFieldNames(StringPool.STAR);
- return searchContext;
- }
- private ServiceContext _getServiceContext() throws Exception {
- return ServiceContextTestUtil.getServiceContext(
- _group.getGroupId(), TestPropsValues.getUserId());
- }
- private Document _getSingleDocument(String searchTerm, Hits hits) {
- List<Document> documents = hits.toList();
- if (documents.size() == 1) {
- return documents.get(0);
- }
- throw new AssertionError(searchTerm + "->" + documents);
- }
- private Document _search(String searchTerm, Locale locale) {
- try {
- SearchContext searchContext = _getSearchContext(searchTerm, locale);
- Hits hits = _indexer.search(searchContext);
- return _getSingleDocument(searchTerm, hits);
- }
- catch (RuntimeException re) {
- throw re;
- }
- catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- @Inject
- private static IndexerRegistry _indexerRegistry;
- @DeleteAfterTestRun
- private Group _group;
- private Indexer<JournalFolder> _indexer;
- }