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.groupby;
016    
017    import com.liferay.portal.kernel.search.Document;
018    import com.liferay.portal.kernel.search.Field;
019    import com.liferay.portal.kernel.search.GroupBy;
020    import com.liferay.portal.kernel.search.Hits;
021    import com.liferay.portal.kernel.search.SearchContext;
022    import com.liferay.portal.kernel.test.IdempotentRetryAssert;
023    import com.liferay.portal.search.unit.test.BaseIndexingTestCase;
024    
025    import java.util.Map;
026    import java.util.concurrent.Callable;
027    import java.util.concurrent.TimeUnit;
028    
029    import org.junit.Assert;
030    
031    /**
032     * @author Miguel Angelo Caldas Gallindo
033     */
034    public abstract class BaseGroupByTestCase extends BaseIndexingTestCase {
035    
036            protected void addDocuments(final String name, int count) throws Exception {
037                    final String field = GROUP_FIELD;
038    
039                    for (int i = 1; i <= count; i++) {
040                            addDocument(
041                                    new DocumentCreationHelper() {
042    
043                                            @Override
044                                            public void populate(Document document) {
045                                                    document.addKeyword(field, name);
046                                            }
047    
048                                    });
049                    }
050            }
051    
052            protected void assertGroup(
053                    String key, int hitsCount, int docsCount,
054                    Map<String, Hits> groupedHitsMap) {
055    
056                    Hits hits = groupedHitsMap.get(key);
057    
058                    Assert.assertNotNull(hits);
059                    Assert.assertEquals(hitsCount, hits.getLength());
060    
061                    Document[] docs = hits.getDocs();
062    
063                    Assert.assertEquals(docsCount, docs.length);
064            }
065    
066            protected void assertGroup(
067                    String key, int count, Map<String, Hits> groupedHitsMap) {
068    
069                    assertGroup(key, count, count, groupedHitsMap);
070            }
071    
072            protected Map<String, Hits> searchGroups(SearchContext searchContext)
073                    throws Exception {
074    
075                    Hits hits = search(searchContext);
076    
077                    Map<String, Hits> groupedHitsMap = hits.getGroupedHits();
078    
079                    Assert.assertNotNull(groupedHitsMap);
080    
081                    return groupedHitsMap;
082            }
083    
084            protected void testGroupBy() throws Exception {
085                    addDocuments("sixteen", 16);
086                    addDocuments("three", 3);
087                    addDocuments("two", 2);
088    
089                    IdempotentRetryAssert.retryAssert(
090                            3, TimeUnit.SECONDS,
091                            new Callable<Void>() {
092    
093                                    @Override
094                                    public Void call() throws Exception {
095                                            SearchContext searchContext = createSearchContext();
096    
097                                            searchContext.setGroupBy(new GroupBy(GROUP_FIELD));
098    
099                                            Map<String, Hits> groupedHitsMap = searchGroups(
100                                                    searchContext);
101    
102                                            Assert.assertEquals(3, groupedHitsMap.size());
103    
104                                            assertGroup("sixteen", 16, groupedHitsMap);
105                                            assertGroup("three", 3, groupedHitsMap);
106                                            assertGroup("two", 2, groupedHitsMap);
107    
108                                            return null;
109                                    }
110    
111                            });
112            }
113    
114            protected void testStartAndEnd() throws Exception {
115                    addDocuments("sixteen", 16);
116    
117                    IdempotentRetryAssert.retryAssert(
118                            3, TimeUnit.SECONDS,
119                            new Callable<Void>() {
120    
121                                    @Override
122                                    public Void call() throws Exception {
123                                            SearchContext searchContext = createSearchContext();
124    
125                                            searchContext.setEnd(9);
126                                            searchContext.setGroupBy(new GroupBy(GROUP_FIELD));
127                                            searchContext.setStart(4);
128    
129                                            Map<String, Hits> groupedHitsMap = searchGroups(
130                                                    searchContext);
131    
132                                            assertGroup("sixteen", 16, 6, groupedHitsMap);
133    
134                                            return null;
135                                    }
136    
137                            });
138            }
139    
140            protected void testStartAndSize() throws Exception {
141                    addDocuments("sixteen", 16);
142    
143                    IdempotentRetryAssert.retryAssert(
144                            3, TimeUnit.SECONDS,
145                            new Callable<Void>() {
146    
147                                    @Override
148                                    public Void call() throws Exception {
149                                            SearchContext searchContext = createSearchContext();
150    
151                                            GroupBy groupBy = new GroupBy(GROUP_FIELD);
152    
153                                            groupBy.setSize(3);
154                                            groupBy.setStart(8);
155    
156                                            searchContext.setGroupBy(groupBy);
157    
158                                            Map<String, Hits> groupedHitsMap = searchGroups(
159                                                    searchContext);
160    
161                                            assertGroup("sixteen", 16, 3, groupedHitsMap);
162    
163                                            return null;
164                                    }
165    
166                            });
167            }
168    
169            protected static final String GROUP_FIELD = Field.USER_NAME;
170    
171    }