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.kernel.search.test;
016    
017    import com.liferay.portal.kernel.search.Document;
018    import com.liferay.portal.kernel.search.DocumentHelper;
019    import com.liferay.portal.kernel.search.DocumentImpl;
020    import com.liferay.portal.kernel.search.Hits;
021    import com.liferay.portal.kernel.search.HitsImpl;
022    import com.liferay.portal.kernel.search.SearchResult;
023    import com.liferay.portal.kernel.search.SearchResultUtil;
024    import com.liferay.portal.util.test.RandomTestUtil;
025    
026    import java.util.List;
027    
028    import javax.portlet.PortletURL;
029    
030    /**
031     * @author Manuel de la Pe??a
032     */
033    public class SearchTestUtil {
034    
035            public static final String ATTACHMENT_OWNER_CLASS_NAME =
036                    RandomTestUtil.randomString();
037    
038            public static final long ATTACHMENT_OWNER_CLASS_NAME_ID =
039                    RandomTestUtil.randomLong();
040    
041            public static final long ATTACHMENT_OWNER_CLASS_PK =
042                    RandomTestUtil.randomLong();
043    
044            public static final long ENTRY_CLASS_PK = RandomTestUtil.randomLong();
045    
046            public static final String SUMMARY_CONTENT = RandomTestUtil.randomString();
047    
048            public static final String SUMMARY_TITLE = RandomTestUtil.randomString();
049    
050            public static Document createAttachmentDocument(String entryClassName) {
051                    return createAttachmentDocument(entryClassName, ENTRY_CLASS_PK);
052            }
053    
054            public static Document createAttachmentDocument(
055                    String entryClassName, long entryClassPK) {
056    
057                    Document document = createDocument(entryClassName, entryClassPK);
058    
059                    DocumentHelper documentHelper = new DocumentHelper(document);
060    
061                    documentHelper.setAttachmentOwnerKey(
062                            ATTACHMENT_OWNER_CLASS_NAME_ID, ATTACHMENT_OWNER_CLASS_PK);
063    
064                    return document;
065            }
066    
067            public static Document createDocument(String entryClassName) {
068                    return createDocument(entryClassName, ENTRY_CLASS_PK);
069            }
070    
071            public static Document createDocument(
072                    String entryClassName, long entryClassPK) {
073    
074                    Document document = new DocumentImpl();
075    
076                    DocumentHelper documentHelper = new DocumentHelper(document);
077    
078                    documentHelper.setEntryKey(entryClassName, entryClassPK);
079    
080                    return document;
081            }
082    
083            public static List<SearchResult> getSearchResults(
084                    PortletURL portletURL, Document... documents) {
085    
086                    Hits hits = new HitsImpl();
087    
088                    hits.setDocs(documents);
089    
090                    return SearchResultUtil.getSearchResults(hits, null, portletURL);
091            }
092    
093    }