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.stats;
016    
017    import com.liferay.portal.kernel.search.Document;
018    import com.liferay.portal.kernel.search.Field;
019    import com.liferay.portal.kernel.search.Hits;
020    import com.liferay.portal.kernel.search.SearchContext;
021    import com.liferay.portal.kernel.search.Stats;
022    import com.liferay.portal.kernel.search.StatsResults;
023    import com.liferay.portal.kernel.test.IdempotentRetryAssert;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.search.unit.test.BaseIndexingTestCase;
026    
027    import java.util.Map;
028    import java.util.concurrent.Callable;
029    import java.util.concurrent.TimeUnit;
030    
031    import org.junit.Assert;
032    
033    /**
034     * @author Miguel Angelo Caldas Gallindo
035     */
036    public abstract class BaseStatisticsTestCase extends BaseIndexingTestCase {
037    
038            protected static String toString(StatsResults statsResults) {
039                    StringBundler sb = new StringBundler(19);
040    
041                    sb.append("{count=");
042                    sb.append(statsResults.getCount());
043                    sb.append(", field=");
044                    sb.append(statsResults.getField());
045                    sb.append(", max=");
046                    sb.append(statsResults.getMax());
047                    sb.append(", mean=");
048                    sb.append(statsResults.getMean());
049                    sb.append(", min=");
050                    sb.append(statsResults.getMin());
051                    sb.append(", missing=");
052                    sb.append(statsResults.getMissing());
053                    sb.append(", standardDeviation=");
054                    sb.append(statsResults.getStandardDeviation());
055                    sb.append(", sum=");
056                    sb.append(statsResults.getSum());
057                    sb.append(", sumOfSquares=");
058                    sb.append(statsResults.getSumOfSquares());
059                    sb.append("}");
060    
061                    return sb.toString();
062            }
063    
064            protected void addDocuments(int count) throws Exception {
065                    final String field = STAT_FIELD;
066    
067                    for (int i = 1; i <= count; i++) {
068                            final int value = i;
069    
070                            addDocument(
071                                    new DocumentCreationHelper() {
072    
073                                            @Override
074                                            public void populate(Document document) {
075                                                    document.addNumberSortable(field, value);
076                                            }
077    
078                                    });
079                    }
080            }
081    
082            protected void assertStats() throws Exception {
083                    String field = STAT_SORTABLE_FIELD;
084    
085                    SearchContext searchContext = createSearchContext();
086    
087                    Stats stats = new Stats();
088    
089                    stats.setCount(true);
090                    stats.setField(field);
091                    stats.setMax(true);
092                    stats.setMean(true);
093                    stats.setMin(true);
094                    stats.setSum(true);
095                    stats.setSumOfSquares(true);
096    
097                    searchContext.addStats(stats);
098    
099                    Hits hits = search(searchContext);
100    
101                    Map<String, StatsResults> statsResultsMap = hits.getStatsResults();
102    
103                    Assert.assertNotNull(statsResultsMap);
104    
105                    StatsResults statsResults = statsResultsMap.get(field);
106    
107                    Assert.assertNotNull(statsResults);
108    
109                    StatsResults expectedStatsResults = new StatsResults(field);
110    
111                    expectedStatsResults.setCount(31);
112                    expectedStatsResults.setMax(31);
113                    expectedStatsResults.setMean(16);
114                    expectedStatsResults.setMin(1);
115                    expectedStatsResults.setSum(496);
116                    expectedStatsResults.setSumOfSquares(10416);
117    
118                    Assert.assertEquals(
119                            toString(expectedStatsResults), toString(statsResults));
120            }
121    
122            protected void testGetStats() throws Exception {
123                    addDocuments(31);
124    
125                    IdempotentRetryAssert.retryAssert(
126                            3, TimeUnit.SECONDS,
127                            new Callable<Void>() {
128    
129                                    @Override
130                                    public Void call() throws Exception {
131                                            assertStats();
132    
133                                            return null;
134                                    }
135    
136                            });
137            }
138    
139            protected static final String STAT_FIELD = Field.PRIORITY;
140    
141            protected static final String STAT_SORTABLE_FIELD =
142                    STAT_FIELD + "_Number_sortable";
143    
144    }