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.unit.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.Field;
021    import com.liferay.portal.kernel.util.FastDateFormatFactory;
022    import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
023    import com.liferay.portal.kernel.util.Props;
024    import com.liferay.portal.kernel.util.PropsKeys;
025    import com.liferay.portal.kernel.util.PropsUtil;
026    
027    import java.text.SimpleDateFormat;
028    
029    import org.apache.commons.lang.math.RandomUtils;
030    
031    import org.mockito.Mockito;
032    
033    /**
034     * @author Miguel Angelo Caldas Gallindo
035     */
036    public class DocumentFixture {
037    
038            public static Document newDocument(
039                    long companyId, long groupId, String entryClassName) {
040    
041                    DocumentImpl document = new DocumentImpl();
042    
043                    document.addKeyword(Field.COMPANY_ID, companyId);
044                    document.addKeyword(Field.GROUP_ID, groupId);
045    
046                    long entryClassPK = RandomUtils.nextLong();
047    
048                    document.addUID(entryClassName, entryClassPK);
049    
050                    DocumentHelper documentHelper = new DocumentHelper(document);
051    
052                    documentHelper.setEntryKey(entryClassName, entryClassPK);
053    
054                    return document;
055            }
056    
057            public void setUp() {
058                    setUpFastDateFormatFactoryUtil();
059                    setUpPropsUtil();
060            }
061    
062            public void tearDown() {
063                    tearDownFastDateFormatFactoryUtil();
064                    tearDownPropsUtil();
065            }
066    
067            protected void mockProperty(String property, String value) {
068                    Mockito.when(
069                            props.get(property)
070                    ).thenReturn(
071                            value
072                    );
073            }
074    
075            protected void setUpFastDateFormatFactoryUtil() {
076                    _fastDateFormatFactory =
077                            FastDateFormatFactoryUtil.getFastDateFormatFactory();
078    
079                    FastDateFormatFactoryUtil fastDateFormatFactoryUtil =
080                            new FastDateFormatFactoryUtil();
081    
082                    FastDateFormatFactory fastDateFormatFactory = Mockito.mock(
083                            FastDateFormatFactory.class);
084    
085                    Mockito.when(
086                            fastDateFormatFactory.getSimpleDateFormat("yyyyMMddHHmmss")
087                    ).thenReturn(
088                            new SimpleDateFormat("yyyyMMddHHmmss")
089                    );
090    
091                    fastDateFormatFactoryUtil.setFastDateFormatFactory(
092                            fastDateFormatFactory);
093            }
094    
095            protected void setUpPropsUtil() {
096                    _props = PropsUtil.getProps();
097    
098                    props = Mockito.mock(Props.class);
099    
100                    mockProperty(PropsKeys.INDEX_DATE_FORMAT_PATTERN, "yyyyMMddHHmmss");
101                    mockProperty(
102                            PropsKeys.INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_ENABLED, "true");
103                    mockProperty(
104                            PropsKeys.INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_SCORES_THRESHOLD,
105                            "50");
106                    mockProperty(PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED, "true");
107                    mockProperty(PropsKeys.INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE, "80");
108                    mockProperty(
109                            PropsKeys.INDEX_SEARCH_HIGHLIGHT_REQUIRE_FIELD_MATCH, "true");
110                    mockProperty(PropsKeys.INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE, "3");
111                    mockProperty(PropsKeys.INDEX_SEARCH_QUERY_INDEXING_ENABLED, "true");
112                    mockProperty(PropsKeys.INDEX_SEARCH_QUERY_INDEXING_THRESHOLD, "50");
113                    mockProperty(PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_ENABLED, "true");
114                    mockProperty(
115                            PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_MAX, "yyyyMMddHHmmss");
116                    mockProperty(
117                            PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_SCORES_THRESHOLD, "0");
118                    mockProperty(PropsKeys.INDEX_SEARCH_SCORING_ENABLED, "true");
119    
120                    PropsUtil.setProps(props);
121            }
122    
123            protected void tearDownFastDateFormatFactoryUtil() {
124                    FastDateFormatFactoryUtil fastDateFormatFactoryUtil =
125                            new FastDateFormatFactoryUtil();
126    
127                    fastDateFormatFactoryUtil.setFastDateFormatFactory(
128                            _fastDateFormatFactory);
129    
130                    _fastDateFormatFactory = null;
131            }
132    
133            protected void tearDownPropsUtil() {
134                    PropsUtil.setProps(_props);
135    
136                    _props = null;
137    
138                    props = null;
139            }
140    
141            protected Props props;
142    
143            private FastDateFormatFactory _fastDateFormatFactory;
144            private Props _props;
145    
146    }