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.portal.kernel.comment.Comment;
018    import com.liferay.portal.kernel.repository.model.FileEntry;
019    import com.liferay.portal.kernel.search.Document;
020    import com.liferay.portal.kernel.search.IndexerRegistry;
021    import com.liferay.portal.kernel.search.RelatedSearchResult;
022    import com.liferay.portal.kernel.search.SearchResult;
023    import com.liferay.portal.kernel.search.SearchResultManager;
024    import com.liferay.portal.kernel.search.result.SearchResultTranslator;
025    import com.liferay.portal.kernel.util.FastDateFormatFactory;
026    import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
027    import com.liferay.portal.kernel.util.Props;
028    import com.liferay.portal.kernel.util.PropsUtil;
029    import com.liferay.portal.util.Portal;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
032    import com.liferay.portlet.asset.model.AssetRenderer;
033    import com.liferay.portlet.asset.model.AssetRendererFactory;
034    import com.liferay.registry.BasicRegistryImpl;
035    import com.liferay.registry.Registry;
036    import com.liferay.registry.RegistryUtil;
037    import com.liferay.registry.collections.ServiceReferenceMapper;
038    import com.liferay.registry.collections.ServiceTrackerCollections;
039    import com.liferay.registry.collections.ServiceTrackerMap;
040    
041    import java.util.List;
042    
043    import org.junit.Assert;
044    import org.junit.Before;
045    
046    import org.mockito.Mock;
047    import org.mockito.Mockito;
048    import org.mockito.MockitoAnnotations;
049    
050    import org.powermock.api.mockito.PowerMockito;
051    
052    /**
053     * @author Andr?? de Oliveira
054     */
055    public abstract class BaseSearchResultUtilTestCase extends PowerMockito {
056    
057            @Before
058            public void setUp() throws Exception {
059                    MockitoAnnotations.initMocks(this);
060    
061                    setUpRegistryUtil();
062    
063                    setUpFastDateFormatFactoryUtil();
064                    setUpIndexerRegistry();
065                    setUpPortalUtil();
066                    setUpPropsUtil();
067                    setUpServiceTrackerMap();
068                    setUpSearchResultTranslator();
069            }
070    
071            protected void assertEmptyCommentRelatedSearchResults(
072                    SearchResult searchResult) {
073    
074                    List<RelatedSearchResult<Comment>> commentRelatedSearchResults =
075                            searchResult.getCommentRelatedSearchResults();
076    
077                    Assert.assertTrue(commentRelatedSearchResults.isEmpty());
078            }
079    
080            protected void assertEmptyFileEntryRelatedSearchResults(
081                    SearchResult searchResult) {
082    
083                    List<RelatedSearchResult<FileEntry>> fileEntryRelatedSearchResults =
084                            searchResult.getFileEntryRelatedSearchResults();
085    
086                    Assert.assertTrue(fileEntryRelatedSearchResults.isEmpty());
087            }
088    
089            protected void assertEmptyVersions(SearchResult searchResult) {
090                    List<String> versions = searchResult.getVersions();
091    
092                    Assert.assertTrue(versions.isEmpty());
093            }
094    
095            protected SearchResult assertOneSearchResult(Document document) {
096                    List<SearchResult> searchResults = SearchTestUtil.getSearchResults(
097                            searchResultTranslator, document);
098    
099                    Assert.assertEquals(1, searchResults.size());
100    
101                    return searchResults.get(0);
102            }
103    
104            protected abstract SearchResultTranslator createSearchResultTranslator();
105    
106            protected void setUpFastDateFormatFactoryUtil() {
107                    FastDateFormatFactoryUtil fastDateFormatFactoryUtil =
108                            new FastDateFormatFactoryUtil();
109    
110                    fastDateFormatFactoryUtil.setFastDateFormatFactory(
111                            mock(FastDateFormatFactory.class));
112            }
113    
114            protected void setUpIndexerRegistry() {
115                    Registry registry = RegistryUtil.getRegistry();
116    
117                    registry.registerService(
118                            IndexerRegistry.class, new TestIndexerRegistry());
119            }
120    
121            protected void setUpPortalUtil() {
122                    Portal portal = Mockito.mock(Portal.class);
123    
124                    when(
125                            portal.getClassName(SearchTestUtil.ATTACHMENT_OWNER_CLASS_NAME_ID)
126                    ).thenReturn(
127                            SearchTestUtil.ATTACHMENT_OWNER_CLASS_NAME
128                    );
129    
130                    PortalUtil portalUtil = new PortalUtil();
131    
132                    portalUtil.setPortal(portal);
133            }
134    
135            protected void setUpPropsUtil() {
136                    PropsUtil.setProps(Mockito.mock(Props.class));
137            }
138    
139            protected void setUpRegistryUtil() {
140                    RegistryUtil.setRegistry(new BasicRegistryImpl());
141    
142                    mockStatic(
143                            AssetRendererFactoryRegistryUtil.class, Mockito.CALLS_REAL_METHODS);
144            }
145    
146            protected void setUpSearchResultTranslator() {
147                    searchResultTranslator = createSearchResultTranslator();
148            }
149    
150            protected void setUpServiceTrackerMap() {
151                    stub(
152                            method(
153                                    ServiceTrackerCollections.class, "singleValueMap", Class.class,
154                                    String.class, ServiceReferenceMapper.class)
155                    ).toReturn(
156                            _serviceTrackerMap
157                    );
158    
159                    when(
160                            _serviceTrackerMap.getService(Mockito.anyString())
161                    ).thenReturn(
162                            null
163                    );
164            }
165    
166            @Mock
167            @SuppressWarnings("rawtypes")
168            protected AssetRenderer assetRenderer;
169    
170            @Mock
171            protected AssetRendererFactory<?> assetRendererFactory;
172    
173            protected SearchResultTranslator searchResultTranslator;
174    
175            @Mock
176            private ServiceTrackerMap<String, SearchResultManager> _serviceTrackerMap;
177    
178    }