001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.search.test;
016    
017    import com.liferay.asset.kernel.AssetRendererFactoryRegistryUtil;
018    import com.liferay.asset.kernel.model.AssetRenderer;
019    import com.liferay.asset.kernel.model.AssetRendererFactory;
020    import com.liferay.portal.kernel.comment.Comment;
021    import com.liferay.portal.kernel.model.ClassName;
022    import com.liferay.portal.kernel.repository.model.FileEntry;
023    import com.liferay.portal.kernel.search.Document;
024    import com.liferay.portal.kernel.search.IndexerRegistry;
025    import com.liferay.portal.kernel.search.RelatedSearchResult;
026    import com.liferay.portal.kernel.search.SearchResult;
027    import com.liferay.portal.kernel.search.result.SearchResultTranslator;
028    import com.liferay.portal.kernel.service.ClassNameLocalService;
029    import com.liferay.portal.kernel.util.FastDateFormatFactory;
030    import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
031    import com.liferay.portal.kernel.util.Props;
032    import com.liferay.portal.kernel.util.PropsUtil;
033    import com.liferay.registry.BasicRegistryImpl;
034    import com.liferay.registry.Registry;
035    import com.liferay.registry.RegistryUtil;
036    
037    import java.util.List;
038    
039    import org.junit.Assert;
040    import org.junit.Before;
041    
042    import org.mockito.Mock;
043    import org.mockito.Mockito;
044    import org.mockito.MockitoAnnotations;
045    
046    import org.powermock.api.mockito.PowerMockito;
047    
048    /**
049     * @author Andr?? de Oliveira
050     */
051    public abstract class BaseSearchResultUtilTestCase extends PowerMockito {
052    
053            @Before
054            public void setUp() throws Exception {
055                    MockitoAnnotations.initMocks(this);
056    
057                    setUpRegistryUtil();
058    
059                    setUpClassNameLocalService();
060                    setUpFastDateFormatFactoryUtil();
061                    setUpIndexerRegistry();
062                    setUpPropsUtil();
063                    setUpSearchResultTranslator();
064            }
065    
066            protected void assertEmptyCommentRelatedSearchResults(
067                    SearchResult searchResult) {
068    
069                    List<RelatedSearchResult<Comment>> commentRelatedSearchResults =
070                            searchResult.getCommentRelatedSearchResults();
071    
072                    Assert.assertTrue(commentRelatedSearchResults.isEmpty());
073            }
074    
075            protected void assertEmptyFileEntryRelatedSearchResults(
076                    SearchResult searchResult) {
077    
078                    List<RelatedSearchResult<FileEntry>> fileEntryRelatedSearchResults =
079                            searchResult.getFileEntryRelatedSearchResults();
080    
081                    Assert.assertTrue(fileEntryRelatedSearchResults.isEmpty());
082            }
083    
084            protected void assertEmptyVersions(SearchResult searchResult) {
085                    List<String> versions = searchResult.getVersions();
086    
087                    Assert.assertTrue(versions.isEmpty());
088            }
089    
090            protected SearchResult assertOneSearchResult(Document document) {
091                    List<SearchResult> searchResults = SearchTestUtil.getSearchResults(
092                            searchResultTranslator, document);
093    
094                    Assert.assertEquals(1, searchResults.size());
095    
096                    return searchResults.get(0);
097            }
098    
099            protected abstract SearchResultTranslator createSearchResultTranslator();
100    
101            protected void setUpClassNameLocalService() throws Exception {
102                    ClassName className = Mockito.mock(ClassName.class);
103    
104                    when(
105                            classNameLocalService.getClassName(
106                                    SearchTestUtil.ATTACHMENT_OWNER_CLASS_NAME_ID)
107                    ).thenReturn(
108                            className
109                    );
110    
111                    when(
112                            className.getClassName()
113                    ).thenReturn(
114                            SearchTestUtil.ATTACHMENT_OWNER_CLASS_NAME
115                    );
116            }
117    
118            protected void setUpFastDateFormatFactoryUtil() {
119                    FastDateFormatFactoryUtil fastDateFormatFactoryUtil =
120                            new FastDateFormatFactoryUtil();
121    
122                    fastDateFormatFactoryUtil.setFastDateFormatFactory(
123                            mock(FastDateFormatFactory.class));
124            }
125    
126            protected void setUpIndexerRegistry() {
127                    Registry registry = RegistryUtil.getRegistry();
128    
129                    registry.registerService(
130                            IndexerRegistry.class, new TestIndexerRegistry());
131            }
132    
133            protected void setUpPropsUtil() {
134                    PropsUtil.setProps(Mockito.mock(Props.class));
135            }
136    
137            protected void setUpRegistryUtil() {
138                    RegistryUtil.setRegistry(new BasicRegistryImpl());
139    
140                    mockStatic(
141                            AssetRendererFactoryRegistryUtil.class, Mockito.CALLS_REAL_METHODS);
142            }
143    
144            protected void setUpSearchResultTranslator() {
145                    searchResultTranslator = createSearchResultTranslator();
146            }
147    
148            @Mock
149            @SuppressWarnings("rawtypes")
150            protected AssetRenderer assetRenderer;
151    
152            @Mock
153            protected AssetRendererFactory<?> assetRendererFactory;
154    
155            @Mock
156            protected ClassNameLocalService classNameLocalService;
157    
158            protected SearchResultTranslator searchResultTranslator;
159    
160    }