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.IndexerRegistryUtil;
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.SearchResultManagerUtil;
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.model.ClassName;
030    import com.liferay.portal.search.SearchResultManagerImpl;
031    import com.liferay.portal.service.ClassNameLocalService;
032    import com.liferay.portal.util.Portal;
033    import com.liferay.portal.util.PortalUtil;
034    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
035    import com.liferay.portlet.asset.model.AssetRenderer;
036    import com.liferay.portlet.asset.model.AssetRendererFactory;
037    import com.liferay.portlet.documentlibrary.service.DLAppLocalService;
038    import com.liferay.portlet.messageboards.service.MBMessageLocalService;
039    import com.liferay.registry.BasicRegistryImpl;
040    import com.liferay.registry.RegistryUtil;
041    import com.liferay.registry.collections.ServiceReferenceMapper;
042    import com.liferay.registry.collections.ServiceTrackerCollections;
043    import com.liferay.registry.collections.ServiceTrackerMap;
044    
045    import java.util.List;
046    
047    import org.junit.Assert;
048    import org.junit.Before;
049    
050    import org.mockito.Mock;
051    import org.mockito.Mockito;
052    import org.mockito.MockitoAnnotations;
053    
054    import org.powermock.api.mockito.PowerMockito;
055    
056    /**
057     * @author Andr?? de Oliveira
058     */
059    public abstract class BaseSearchResultUtilTestCase extends PowerMockito {
060    
061            @Before
062            public void setUp() throws Exception {
063                    MockitoAnnotations.initMocks(this);
064    
065                    setUpClassNameLocalService();
066                    setUpFastDateFormatFactoryUtil();
067                    setUpPortalUtil();
068                    setUpPropsUtil();
069                    setUpRegistryUtil();
070                    setUpServiceTrackerMap();
071                    setUpSearchResultManagerUtil();
072            }
073    
074            protected void assertEmptyCommentRelatedSearchResults(
075                    SearchResult searchResult) {
076    
077                    List<RelatedSearchResult<Comment>> commentRelatedSearchResults =
078                            searchResult.getCommentRelatedSearchResults();
079    
080                    Assert.assertTrue(commentRelatedSearchResults.isEmpty());
081            }
082    
083            protected void assertEmptyFileEntryRelatedSearchResults(
084                    SearchResult searchResult) {
085    
086                    List<RelatedSearchResult<FileEntry>> fileEntryRelatedSearchResults =
087                            searchResult.getFileEntryRelatedSearchResults();
088    
089                    Assert.assertTrue(fileEntryRelatedSearchResults.isEmpty());
090            }
091    
092            protected void assertEmptyVersions(SearchResult searchResult) {
093                    List<String> versions = searchResult.getVersions();
094    
095                    Assert.assertTrue(versions.isEmpty());
096            }
097    
098            protected SearchResult assertOneSearchResult(Document document) {
099                    List<SearchResult> searchResults = SearchTestUtil.getSearchResults(
100                            document);
101    
102                    Assert.assertEquals(1, searchResults.size());
103    
104                    return searchResults.get(0);
105            }
106    
107            protected void setUpClassNameLocalService() throws Exception {
108                    when(
109                            classNameLocalService.getClassName(
110                                    SearchTestUtil.ATTACHMENT_OWNER_CLASS_NAME_ID)
111                    ).thenReturn(
112                            className
113                    );
114    
115                    when(
116                            className.getClassName()
117                    ).thenReturn(
118                            SearchTestUtil.ATTACHMENT_OWNER_CLASS_NAME
119                    );
120            }
121    
122            protected void setUpFastDateFormatFactoryUtil() {
123                    FastDateFormatFactoryUtil fastDateFormatFactoryUtil =
124                            new FastDateFormatFactoryUtil();
125    
126                    fastDateFormatFactoryUtil.setFastDateFormatFactory(
127                            mock(FastDateFormatFactory.class));
128            }
129    
130            protected void setUpPortalUtil() {
131                    PortalUtil portalUtil = new PortalUtil();
132    
133                    portalUtil.setPortal(portal);
134    
135                    when(
136                            portal.getClassName(SearchTestUtil.ATTACHMENT_OWNER_CLASS_NAME_ID)
137                    ).thenReturn(
138                            SearchTestUtil.ATTACHMENT_OWNER_CLASS_NAME
139                    );
140            }
141    
142            protected void setUpPropsUtil() {
143                    PropsUtil.setProps(props);
144            }
145    
146            protected void setUpRegistryUtil() {
147                    RegistryUtil.setRegistry(new BasicRegistryImpl());
148    
149                    mockStatic(
150                            AssetRendererFactoryRegistryUtil.class, Mockito.CALLS_REAL_METHODS);
151                    mockStatic(IndexerRegistryUtil.class, Mockito.CALLS_REAL_METHODS);
152            }
153    
154            protected void setUpSearchResultManagerUtil() {
155                    SearchResultManagerUtil searchResultManagerUtil =
156                            new SearchResultManagerUtil();
157    
158                    SearchResultManager searchResultManager = new SearchResultManagerImpl(
159                            classNameLocalService, dlAppLocalService, mbMessageLocalService);
160    
161                    searchResultManagerUtil.setSearchResultManager(searchResultManager);
162            }
163    
164            protected void setUpServiceTrackerMap() {
165                    stub(
166                            method(
167                                    ServiceTrackerCollections.class, "singleValueMap", Class.class,
168                                    String.class, ServiceReferenceMapper.class)
169                    ).toReturn(
170                            _serviceTrackerMap
171                    );
172    
173                    when(
174                            _serviceTrackerMap.getService(Mockito.anyString())
175                    ).thenReturn(
176                            null
177                    );
178            }
179    
180            @Mock
181            protected AssetRenderer assetRenderer;
182    
183            @Mock
184            protected AssetRendererFactory assetRendererFactory;
185    
186            @Mock
187            protected ClassName className;
188    
189            @Mock
190            protected ClassNameLocalService classNameLocalService;
191    
192            @Mock
193            protected DLAppLocalService dlAppLocalService;
194    
195            @Mock
196            protected MBMessageLocalService mbMessageLocalService;
197    
198            @Mock
199            protected Portal portal;
200    
201            @Mock
202            protected Props props;
203    
204            @Mock
205            private ServiceTrackerMap<String, SearchResultManager> _serviceTrackerMap;
206    
207    }