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.portlet.asset.search.test;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.search.Hits;
019    import com.liferay.portal.kernel.search.SearchContext;
020    import com.liferay.portal.kernel.security.RandomUtil;
021    import com.liferay.portal.kernel.test.IdempotentRetryAssert;
022    import com.liferay.portal.kernel.test.rule.AggregateTestRule;
023    import com.liferay.portal.kernel.test.rule.DeleteAfterTestRun;
024    import com.liferay.portal.kernel.test.rule.Sync;
025    import com.liferay.portal.kernel.test.util.GroupTestUtil;
026    import com.liferay.portal.kernel.test.util.RandomTestUtil;
027    import com.liferay.portal.kernel.test.util.SearchContextTestUtil;
028    import com.liferay.portal.kernel.test.util.ServiceContextTestUtil;
029    import com.liferay.portal.kernel.test.util.TestPropsValues;
030    import com.liferay.portal.kernel.test.util.UserTestUtil;
031    import com.liferay.portal.kernel.util.ArrayUtil;
032    import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
033    import com.liferay.portal.kernel.util.LocaleUtil;
034    import com.liferay.portal.kernel.util.Time;
035    import com.liferay.portal.model.BaseModel;
036    import com.liferay.portal.model.Group;
037    import com.liferay.portal.model.User;
038    import com.liferay.portal.service.ServiceContext;
039    import com.liferay.portal.test.rule.LiferayIntegrationTestRule;
040    import com.liferay.portal.test.rule.MainServletTestRule;
041    import com.liferay.portal.util.PortalUtil;
042    import com.liferay.portal.util.PropsValues;
043    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
044    import com.liferay.portlet.asset.model.AssetCategory;
045    import com.liferay.portlet.asset.model.AssetEntry;
046    import com.liferay.portlet.asset.model.AssetVocabulary;
047    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
048    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
049    import com.liferay.portlet.asset.service.AssetVocabularyLocalServiceUtil;
050    import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
051    import com.liferay.portlet.asset.service.persistence.test.AssetEntryQueryTestUtil;
052    import com.liferay.portlet.asset.util.AssetUtil;
053    
054    import java.text.DateFormat;
055    
056    import java.util.ArrayList;
057    import java.util.Arrays;
058    import java.util.Calendar;
059    import java.util.Date;
060    import java.util.GregorianCalendar;
061    import java.util.HashMap;
062    import java.util.List;
063    import java.util.Locale;
064    import java.util.Map;
065    import java.util.concurrent.Callable;
066    import java.util.concurrent.TimeUnit;
067    
068    import org.junit.Assert;
069    import org.junit.Before;
070    import org.junit.ClassRule;
071    import org.junit.Rule;
072    import org.junit.Test;
073    
074    /**
075     * @author Eudaldo Alonso
076     */
077    @Sync
078    public abstract class BaseAssetSearchTestCase {
079    
080            @ClassRule
081            @Rule
082            public static final AggregateTestRule aggregateTestRule =
083                    new AggregateTestRule(
084                            new LiferayIntegrationTestRule(), MainServletTestRule.INSTANCE);
085    
086            @Before
087            public void setUp() throws Exception {
088                    _group1 = GroupTestUtil.addGroup();
089    
090                    ServiceContext serviceContext =
091                            ServiceContextTestUtil.getServiceContext(_group1.getGroupId());
092    
093                    AssetVocabulary vocabulary =
094                            AssetVocabularyLocalServiceUtil.addVocabulary(
095                                    TestPropsValues.getUserId(), _group1.getGroupId(),
096                                    RandomTestUtil.randomString(), serviceContext);
097    
098                    _vocabularyId = vocabulary.getVocabularyId();
099    
100                    AssetCategory fashionCategory =
101                            AssetCategoryLocalServiceUtil.addCategory(
102                                    TestPropsValues.getUserId(), _group1.getGroupId(), "Fashion",
103                                    _vocabularyId, serviceContext);
104    
105                    _fashionCategoryId = fashionCategory.getCategoryId();
106    
107                    AssetCategory foodCategory = AssetCategoryLocalServiceUtil.addCategory(
108                            TestPropsValues.getUserId(), _group1.getGroupId(), "Food",
109                            _vocabularyId, serviceContext);
110    
111                    _foodCategoryId = foodCategory.getCategoryId();
112    
113                    AssetCategory healthCategory =
114                            AssetCategoryLocalServiceUtil.addCategory(
115                                    TestPropsValues.getUserId(), _group1.getGroupId(), "Health",
116                                    _vocabularyId, serviceContext);
117    
118                    _healthCategoryId = healthCategory.getCategoryId();
119    
120                    AssetCategory sportCategory = AssetCategoryLocalServiceUtil.addCategory(
121                            TestPropsValues.getUserId(), _group1.getGroupId(), "Sport",
122                            _vocabularyId, serviceContext);
123    
124                    _sportCategoryId = sportCategory.getCategoryId();
125    
126                    AssetCategory travelCategory =
127                            AssetCategoryLocalServiceUtil.addCategory(
128                                    TestPropsValues.getUserId(), _group1.getGroupId(), "Travel",
129                                    _vocabularyId, serviceContext);
130    
131                    _travelCategoryId = travelCategory.getCategoryId();
132    
133                    _assetCategoryIds1 =
134                            new long[] {_healthCategoryId, _sportCategoryId, _travelCategoryId};
135                    _assetCategoryIds2 = new long[] {
136                            _fashionCategoryId, _foodCategoryId, _healthCategoryId,
137                            _sportCategoryId
138                    };
139    
140                    _group2 = GroupTestUtil.addGroup();
141    
142                    long[] groupIds = new long[] {
143                            _group1.getGroupId(), _group2.getGroupId()
144                    };
145    
146                    for (long groupId : groupIds) {
147                            serviceContext = ServiceContextTestUtil.getServiceContext(groupId);
148    
149                            AssetTagLocalServiceUtil.addTag(
150                                    TestPropsValues.getUserId(), groupId, "liferay",
151                                    serviceContext);
152    
153                            AssetTagLocalServiceUtil.addTag(
154                                    TestPropsValues.getUserId(), groupId, "architecture",
155                                    serviceContext);
156    
157                            AssetTagLocalServiceUtil.addTag(
158                                    TestPropsValues.getUserId(), groupId, "modularity",
159                                    serviceContext);
160    
161                            AssetTagLocalServiceUtil.addTag(
162                                    TestPropsValues.getUserId(), groupId, "osgi", serviceContext);
163    
164                            AssetTagLocalServiceUtil.addTag(
165                                    TestPropsValues.getUserId(), groupId, "services",
166                                    serviceContext);
167                    }
168    
169                    _assetTagsNames1 =
170                            new String[] {"liferay", "architecture", "modularity", "osgi"};
171                    _assetTagsNames2 = new String[] {"liferay", "architecture", "services"};
172            }
173    
174            @Test
175            public void testAllAssetCategories1() throws Exception {
176                    long[] allCategoryIds = {_healthCategoryId};
177    
178                    AssetEntryQuery assetEntryQuery =
179                            AssetEntryQueryTestUtil.createAssetEntryQuery(
180                                    _group1.getGroupId(), getBaseModelClassName(), null, null,
181                                    allCategoryIds, null);
182    
183                    testAssetCategorization(assetEntryQuery, 2);
184            }
185    
186            @Test
187            public void testAllAssetCategories2() throws Exception {
188                    long[] allCategoryIds = {_healthCategoryId, _sportCategoryId};
189    
190                    AssetEntryQuery assetEntryQuery =
191                            AssetEntryQueryTestUtil.createAssetEntryQuery(
192                                    _group1.getGroupId(), getBaseModelClassName(), null, null,
193                                    allCategoryIds, null);
194    
195                    testAssetCategorization(assetEntryQuery, 2);
196            }
197    
198            @Test
199            public void testAllAssetCategories3() throws Exception {
200                    long[] allCategoryIds =
201                            {_healthCategoryId, _sportCategoryId, _foodCategoryId};
202    
203                    AssetEntryQuery assetEntryQuery =
204                            AssetEntryQueryTestUtil.createAssetEntryQuery(
205                                    _group1.getGroupId(), getBaseModelClassName(), null, null,
206                                    allCategoryIds, null);
207    
208                    testAssetCategorization(assetEntryQuery, 1);
209            }
210    
211            @Test
212            public void testAllAssetCategories4() throws Exception {
213                    long[] allCategoryIds = {
214                            _healthCategoryId, _sportCategoryId, _foodCategoryId,
215                            _travelCategoryId
216                    };
217    
218                    AssetEntryQuery assetEntryQuery =
219                            AssetEntryQueryTestUtil.createAssetEntryQuery(
220                                    _group1.getGroupId(), getBaseModelClassName(), null, null,
221                                    allCategoryIds, null);
222    
223                    testAssetCategorization(assetEntryQuery, 0);
224            }
225    
226            @Test
227            public void testAllAssetTags1() throws Exception {
228                    String[] allTagNames = {"liferay"};
229    
230                    AssetEntryQuery assetEntryQuery =
231                            AssetEntryQueryTestUtil.createAssetEntryQuery(
232                                    _group1.getGroupId(), getBaseModelClassName(), null, null,
233                                    allTagNames, null);
234    
235                    testAssetCategorization(assetEntryQuery, 2);
236            }
237    
238            @Test
239            public void testAllAssetTags2() throws Exception {
240                    String[] allTagNames = {"liferay", "architecture"};
241    
242                    AssetEntryQuery assetEntryQuery =
243                            AssetEntryQueryTestUtil.createAssetEntryQuery(
244                                    _group1.getGroupId(), getBaseModelClassName(), null, null,
245                                    allTagNames, null);
246    
247                    testAssetCategorization(assetEntryQuery, 2);
248            }
249    
250            @Test
251            public void testAllAssetTags3() throws Exception {
252                    String[] allTagNames = {"liferay", "architecture", "services"};
253    
254                    AssetEntryQuery assetEntryQuery =
255                            AssetEntryQueryTestUtil.createAssetEntryQuery(
256                                    _group1.getGroupId(), getBaseModelClassName(), null, null,
257                                    allTagNames, null);
258    
259                    testAssetCategorization(assetEntryQuery, 1);
260            }
261    
262            @Test
263            public void testAllAssetTags4() throws Exception {
264                    String[] allTagNames = {"liferay", "architecture", "services", "osgi"};
265    
266                    AssetEntryQuery assetEntryQuery =
267                            AssetEntryQueryTestUtil.createAssetEntryQuery(
268                                    _group1.getGroupId(), getBaseModelClassName(), null, null,
269                                    allTagNames, null);
270    
271                    testAssetCategorization(assetEntryQuery, 0);
272            }
273    
274            @Test
275            public void testAllAssetTagsMultipleGroups1() throws Exception {
276                    String[] allTagNames = {"liferay"};
277    
278                    AssetEntryQuery assetEntryQuery =
279                            AssetEntryQueryTestUtil.createAssetEntryQuery(
280                                    new long[] {_group1.getGroupId(), _group2.getGroupId()},
281                                    getBaseModelClassName(), null, null, allTagNames, null);
282    
283                    testAssetCategorization(
284                            new Group[] {_group1, _group2}, assetEntryQuery, 4);
285            }
286    
287            @Test
288            public void testAllAssetTagsMultipleGroups2() throws Exception {
289                    String[] allTagNames = {"liferay", "architecture"};
290    
291                    AssetEntryQuery assetEntryQuery =
292                            AssetEntryQueryTestUtil.createAssetEntryQuery(
293                                    new long[] {_group1.getGroupId(), _group2.getGroupId()},
294                                    getBaseModelClassName(), null, null, allTagNames, null);
295    
296                    testAssetCategorization(
297                            new Group[] {_group1, _group2}, assetEntryQuery, 4);
298            }
299    
300            @Test
301            public void testAllAssetTagsMultipleGroups3() throws Exception {
302                    String[] allTagNames = {"liferay", "architecture", "services"};
303    
304                    AssetEntryQuery assetEntryQuery =
305                            AssetEntryQueryTestUtil.createAssetEntryQuery(
306                                    new long[] {_group1.getGroupId(), _group2.getGroupId()},
307                                    getBaseModelClassName(), null, null, allTagNames, null);
308    
309                    testAssetCategorization(
310                            new Group[] {_group1, _group2}, assetEntryQuery, 2);
311            }
312    
313            @Test
314            public void testAllAssetTagsMultipleGroups4() throws Exception {
315                    String[] allTagNames = {"liferay", "architecture", "services", "osgi"};
316    
317                    AssetEntryQuery assetEntryQuery =
318                            AssetEntryQueryTestUtil.createAssetEntryQuery(
319                                    new long[] {_group1.getGroupId(), _group2.getGroupId()},
320                                    getBaseModelClassName(), null, null, allTagNames, null);
321    
322                    testAssetCategorization(
323                            new Group[] {_group1, _group2}, assetEntryQuery, 0);
324            }
325    
326            @Test
327            public void testAnyAssetCategories1() throws Exception {
328                    long[] anyCategoryIds = {_healthCategoryId};
329    
330                    AssetEntryQuery assetEntryQuery =
331                            AssetEntryQueryTestUtil.createAssetEntryQuery(
332                                    _group1.getGroupId(), getBaseModelClassName(), null, null, null,
333                                    anyCategoryIds);
334    
335                    testAssetCategorization(assetEntryQuery, 2);
336            }
337    
338            @Test
339            public void testAnyAssetCategories2() throws Exception {
340                    long[] anyCategoryIds = {_healthCategoryId, _sportCategoryId};
341    
342                    AssetEntryQuery assetEntryQuery =
343                            AssetEntryQueryTestUtil.createAssetEntryQuery(
344                                    _group1.getGroupId(), getBaseModelClassName(), null, null, null,
345                                    anyCategoryIds);
346    
347                    testAssetCategorization(assetEntryQuery, 2);
348            }
349    
350            @Test
351            public void testAnyAssetCategories3() throws Exception {
352                    long[] anyCategoryIds =
353                            {_healthCategoryId, _sportCategoryId, _foodCategoryId};
354    
355                    AssetEntryQuery assetEntryQuery =
356                            AssetEntryQueryTestUtil.createAssetEntryQuery(
357                                    _group1.getGroupId(), getBaseModelClassName(), null, null, null,
358                                    anyCategoryIds);
359    
360                    testAssetCategorization(assetEntryQuery, 2);
361            }
362    
363            @Test
364            public void testAnyAssetCategories4() throws Exception {
365                    long[] anyCategoryIds = {_fashionCategoryId, _foodCategoryId};
366    
367                    AssetEntryQuery assetEntryQuery =
368                            AssetEntryQueryTestUtil.createAssetEntryQuery(
369                                    _group1.getGroupId(), getBaseModelClassName(), null, null, null,
370                                    anyCategoryIds);
371    
372                    testAssetCategorization(assetEntryQuery, 1);
373            }
374    
375            @Test
376            public void testAnyAssetTags1() throws Exception {
377                    String[] anyTagNames = {"liferay"};
378    
379                    AssetEntryQuery assetEntryQuery =
380                            AssetEntryQueryTestUtil.createAssetEntryQuery(
381                                    _group1.getGroupId(), getBaseModelClassName(), null, null, null,
382                                    anyTagNames);
383    
384                    testAssetCategorization(assetEntryQuery, 2);
385            }
386    
387            @Test
388            public void testAnyAssetTags2() throws Exception {
389                    String[] anyTagNames = {"liferay", "architecture"};
390    
391                    AssetEntryQuery assetEntryQuery =
392                            AssetEntryQueryTestUtil.createAssetEntryQuery(
393                                    _group1.getGroupId(), getBaseModelClassName(), null, null, null,
394                                    anyTagNames);
395    
396                    testAssetCategorization(assetEntryQuery, 2);
397            }
398    
399            @Test
400            public void testAnyAssetTags3() throws Exception {
401                    String[] anyTagNames = {"liferay", "architecture", "services"};
402    
403                    AssetEntryQuery assetEntryQuery =
404                            AssetEntryQueryTestUtil.createAssetEntryQuery(
405                                    _group1.getGroupId(), getBaseModelClassName(), null, null, null,
406                                    anyTagNames);
407    
408                    testAssetCategorization(assetEntryQuery, 2);
409            }
410    
411            @Test
412            public void testAnyAssetTags4() throws Exception {
413                    String[] anyTagNames = {"modularity", "osgi"};
414    
415                    AssetEntryQuery assetEntryQuery =
416                            AssetEntryQueryTestUtil.createAssetEntryQuery(
417                                    _group1.getGroupId(), getBaseModelClassName(), null, null, null,
418                                    anyTagNames);
419    
420                    testAssetCategorization(assetEntryQuery, 1);
421            }
422    
423            @Test
424            public void testAssetCategoryAllAndAny() throws Exception {
425                    long[] allCategoryIds =
426                            {_healthCategoryId, _sportCategoryId, _travelCategoryId};
427                    long[] anyCategoryIds = {_healthCategoryId};
428    
429                    AssetEntryQuery assetEntryQuery =
430                            AssetEntryQueryTestUtil.createAssetEntryQuery(
431                                    _group1.getGroupId(), getBaseModelClassName(), null, null,
432                                    allCategoryIds, anyCategoryIds);
433    
434                    testAssetCategorization(assetEntryQuery, 1);
435            }
436    
437            @Test
438            public void testAssetCategoryNotAllAndAll() throws Exception {
439                    long[] notAllCategoryIds = {_fashionCategoryId, _foodCategoryId};
440                    long[] allCategoryIds =
441                            {_healthCategoryId, _sportCategoryId, _travelCategoryId};
442    
443                    AssetEntryQuery assetEntryQuery =
444                            AssetEntryQueryTestUtil.createAssetEntryQuery(
445                                    _group1.getGroupId(), getBaseModelClassName(),
446                                    notAllCategoryIds, null, allCategoryIds, null);
447    
448                    testAssetCategorization(assetEntryQuery, 1);
449            }
450    
451            @Test
452            public void testAssetCategoryNotAllAndAny() throws Exception {
453                    long[] notAllCategoryIds = {_fashionCategoryId};
454                    long[] anyCategoryIds = {_sportCategoryId, _travelCategoryId};
455    
456                    AssetEntryQuery assetEntryQuery =
457                            AssetEntryQueryTestUtil.createAssetEntryQuery(
458                                    _group1.getGroupId(), getBaseModelClassName(),
459                                    notAllCategoryIds, null, null, anyCategoryIds);
460    
461                    testAssetCategorization(assetEntryQuery, 1);
462            }
463    
464            @Test
465            public void testAssetCategoryNotAllAndNotAny() throws Exception {
466                    long[] notAllCategoryIds = {_fashionCategoryId, _foodCategoryId};
467                    long[] notAnyCategoryIds = {_travelCategoryId};
468    
469                    AssetEntryQuery assetEntryQuery =
470                            AssetEntryQueryTestUtil.createAssetEntryQuery(
471                                    _group1.getGroupId(), getBaseModelClassName(),
472                                    notAllCategoryIds, notAnyCategoryIds, null, null);
473    
474                    testAssetCategorization(assetEntryQuery, 0);
475            }
476    
477            @Test
478            public void testAssetCategoryNotAnyAndAll() throws Exception {
479                    long[] notAnyCategoryIds = {_fashionCategoryId};
480                    long[] allCategoryIds = {_healthCategoryId, _sportCategoryId};
481    
482                    AssetEntryQuery assetEntryQuery =
483                            AssetEntryQueryTestUtil.createAssetEntryQuery(
484                                    _group1.getGroupId(), getBaseModelClassName(), null,
485                                    notAnyCategoryIds, allCategoryIds, null);
486    
487                    testAssetCategorization(assetEntryQuery, 1);
488            }
489    
490            @Test
491            public void testAssetCategoryNotAnyAndAny() throws Exception {
492                    long[] notAnyCategoryIds = {_fashionCategoryId, _foodCategoryId};
493                    long[] anyCategoryIds =
494                            {_healthCategoryId, _sportCategoryId, _travelCategoryId};
495    
496                    AssetEntryQuery assetEntryQuery =
497                            AssetEntryQueryTestUtil.createAssetEntryQuery(
498                                    _group1.getGroupId(), getBaseModelClassName(), null,
499                                    notAnyCategoryIds, null, anyCategoryIds);
500    
501                    testAssetCategorization(assetEntryQuery, 1);
502            }
503    
504            @Test
505            public void testAssetTagsAllAndAny() throws Exception {
506                    String[] allTagNames = {"liferay", "architecture", "services"};
507                    String[] anyTagNames = {"liferay"};
508    
509                    AssetEntryQuery assetEntryQuery =
510                            AssetEntryQueryTestUtil.createAssetEntryQuery(
511                                    _group1.getGroupId(), getBaseModelClassName(), null, null,
512                                    allTagNames, anyTagNames);
513    
514                    testAssetCategorization(assetEntryQuery, 1);
515            }
516    
517            @Test
518            public void testAssetTagsNotAllAndAll() throws Exception {
519                    String[] notAllTagNames = {"osgi", "modularity"};
520                    String[] allTagNames = {"liferay", "architecture", "services"};
521    
522                    AssetEntryQuery assetEntryQuery =
523                            AssetEntryQueryTestUtil.createAssetEntryQuery(
524                                    _group1.getGroupId(), getBaseModelClassName(), notAllTagNames,
525                                    null, allTagNames, null);
526    
527                    testAssetCategorization(assetEntryQuery, 1);
528            }
529    
530            @Test
531            public void testAssetTagsNotAllAndAny() throws Exception {
532                    String[] notAllTagNames = {"services"};
533                    String[] anyTagNames = {"liferay", "architecture"};
534    
535                    AssetEntryQuery assetEntryQuery =
536                            AssetEntryQueryTestUtil.createAssetEntryQuery(
537                                    _group1.getGroupId(), getBaseModelClassName(), notAllTagNames,
538                                    null, null, anyTagNames);
539    
540                    testAssetCategorization(assetEntryQuery, 1);
541            }
542    
543            @Test
544            public void testAssetTagsNotAllAndNotAny() throws Exception {
545                    String[] notAllTagNames = {"osgi", "modularity"};
546                    String[] notAnyTagNames = {"services"};
547    
548                    AssetEntryQuery assetEntryQuery =
549                            AssetEntryQueryTestUtil.createAssetEntryQuery(
550                                    _group1.getGroupId(), getBaseModelClassName(), notAllTagNames,
551                                    notAnyTagNames, null, null);
552    
553                    testAssetCategorization(assetEntryQuery, 0);
554            }
555    
556            @Test
557            public void testAssetTagsNotAnyAndAll() throws Exception {
558                    String[] notAnyTagNames = {"modularity"};
559                    String[] allTagNames = {"liferay", "architecture"};
560    
561                    AssetEntryQuery assetEntryQuery =
562                            AssetEntryQueryTestUtil.createAssetEntryQuery(
563                                    _group1.getGroupId(), getBaseModelClassName(), null,
564                                    notAnyTagNames, allTagNames, null);
565    
566                    testAssetCategorization(assetEntryQuery, 1);
567            }
568    
569            @Test
570            public void testAssetTagsNotAnyAndAny() throws Exception {
571                    String[] notAnyTagNames = {"modularity", "osgi"};
572                    String[] anyTagNames = {"liferay", "architecture", "services"};
573    
574                    AssetEntryQuery assetEntryQuery =
575                            AssetEntryQueryTestUtil.createAssetEntryQuery(
576                                    _group1.getGroupId(), getBaseModelClassName(), null,
577                                    notAnyTagNames, null, anyTagNames);
578    
579                    testAssetCategorization(assetEntryQuery, 1);
580            }
581    
582            @Test
583            public void testClassName1() throws Exception {
584                    AssetEntryQuery assetEntryQuery =
585                            AssetEntryQueryTestUtil.createAssetEntryQuery(
586                                    _group1.getGroupId(), new String[] {getBaseModelClassName()});
587    
588                    testClassNames(assetEntryQuery, 1);
589            }
590    
591            @Test
592            public void testClassName2() throws Exception {
593                    long[] classNameIds = AssetRendererFactoryRegistryUtil.getClassNameIds(
594                            TestPropsValues.getCompanyId());
595    
596                    classNameIds = ArrayUtil.remove(
597                            classNameIds, PortalUtil.getClassNameId(getBaseModelClass()));
598    
599                    AssetEntryQuery assetEntryQuery =
600                            AssetEntryQueryTestUtil.createAssetEntryQuery(
601                                    _group1.getGroupId(), classNameIds);
602    
603                    testClassNames(assetEntryQuery, 0);
604            }
605    
606            @Test
607            public void testClassTypeIds1() throws Exception {
608                    AssetEntryQuery assetEntryQuery =
609                            AssetEntryQueryTestUtil.createAssetEntryQuery(
610                                    _group1.getGroupId(), new String[] {getBaseModelClassName()});
611    
612                    testClassTypeIds(assetEntryQuery, true);
613            }
614    
615            @Test
616            public void testClassTypeIds2() throws Exception {
617                    AssetEntryQuery assetEntryQuery =
618                            AssetEntryQueryTestUtil.createAssetEntryQuery(
619                                    _group1.getGroupId(), new String[] {getBaseModelClassName()});
620    
621                    testClassTypeIds(assetEntryQuery, false);
622            }
623    
624            @Test
625            public void testGroups() throws Exception {
626                    AssetEntryQuery assetEntryQuery = new AssetEntryQuery();
627    
628                    assetEntryQuery.setClassName(getBaseModelClassName());
629    
630                    Group group1 = GroupTestUtil.addGroup();
631                    Group group2 = GroupTestUtil.addGroup();
632    
633                    assetEntryQuery.setGroupIds(
634                            new long[] {group1.getGroupId(), group2.getGroupId()});
635    
636                    SearchContext searchContext = SearchContextTestUtil.getSearchContext();
637    
638                    searchContext.setGroupIds(assetEntryQuery.getGroupIds());
639    
640                    int initialEntries = 0;
641    
642                    assertCount(initialEntries, assetEntryQuery, searchContext);
643    
644                    ServiceContext serviceContext1 =
645                            ServiceContextTestUtil.getServiceContext(group1.getGroupId());
646    
647                    BaseModel<?> parentBaseModel1 = getParentBaseModel(
648                            group1, serviceContext1);
649    
650                    addBaseModel(parentBaseModel1, getSearchKeywords(), serviceContext1);
651    
652                    ServiceContext serviceContext2 =
653                            ServiceContextTestUtil.getServiceContext(group2.getGroupId());
654    
655                    BaseModel<?> parentBaseModel2 = getParentBaseModel(
656                            group1, serviceContext2);
657    
658                    addBaseModel(parentBaseModel2, getSearchKeywords(), serviceContext2);
659    
660                    assertCount(initialEntries + 2, assetEntryQuery, searchContext);
661            }
662    
663            @Test
664            public void testNotAllAssetCategories1() throws Exception {
665                    long[] notAllCategoryIds = {_healthCategoryId};
666    
667                    AssetEntryQuery assetEntryQuery =
668                            AssetEntryQueryTestUtil.createAssetEntryQuery(
669                                    _group1.getGroupId(), getBaseModelClassName(),
670                                    notAllCategoryIds, null, null, null);
671    
672                    testAssetCategorization(assetEntryQuery, 0);
673            }
674    
675            @Test
676            public void testNotAllAssetCategories2() throws Exception {
677                    long[] notAllCategoryIds = {_healthCategoryId, _sportCategoryId};
678    
679                    AssetEntryQuery assetEntryQuery =
680                            AssetEntryQueryTestUtil.createAssetEntryQuery(
681                                    _group1.getGroupId(), getBaseModelClassName(),
682                                    notAllCategoryIds, null, null, null);
683    
684                    testAssetCategorization(assetEntryQuery, 0);
685            }
686    
687            @Test
688            public void testNotAllAssetCategories3() throws Exception {
689                    long[] notAllCategoryIds = {_fashionCategoryId, _foodCategoryId};
690    
691                    AssetEntryQuery assetEntryQuery =
692                            AssetEntryQueryTestUtil.createAssetEntryQuery(
693                                    _group1.getGroupId(), getBaseModelClassName(),
694                                    notAllCategoryIds, null, null, null);
695    
696                    testAssetCategorization(assetEntryQuery, 1);
697            }
698    
699            @Test
700            public void testNotAllAssetCategories4() throws Exception {
701                    long[] notAllCategoryIds =
702                            {_fashionCategoryId, _foodCategoryId, _travelCategoryId};
703    
704                    AssetEntryQuery assetEntryQuery =
705                            AssetEntryQueryTestUtil.createAssetEntryQuery(
706                                    _group1.getGroupId(), getBaseModelClassName(),
707                                    notAllCategoryIds, null, null, null);
708    
709                    testAssetCategorization(assetEntryQuery, 2);
710            }
711    
712            @Test
713            public void testNotAllAssetTags1() throws Exception {
714                    String[] notAllTagNames = {"liferay"};
715    
716                    AssetEntryQuery assetEntryQuery =
717                            AssetEntryQueryTestUtil.createAssetEntryQuery(
718                                    _group1.getGroupId(), getBaseModelClassName(), notAllTagNames,
719                                    null, null, null);
720    
721                    testAssetCategorization(assetEntryQuery, 0);
722            }
723    
724            @Test
725            public void testNotAllAssetTags2() throws Exception {
726                    String[] notAllTagNames = {"liferay", "architecture"};
727    
728                    AssetEntryQuery assetEntryQuery =
729                            AssetEntryQueryTestUtil.createAssetEntryQuery(
730                                    _group1.getGroupId(), getBaseModelClassName(), notAllTagNames,
731                                    null, null, null);
732    
733                    testAssetCategorization(assetEntryQuery, 0);
734            }
735    
736            @Test
737            public void testNotAllAssetTags3() throws Exception {
738                    String[] notAllTagNames = {"liferay", "architecture", "services"};
739    
740                    AssetEntryQuery assetEntryQuery =
741                            AssetEntryQueryTestUtil.createAssetEntryQuery(
742                                    _group1.getGroupId(), getBaseModelClassName(), notAllTagNames,
743                                    null, null, null);
744    
745                    testAssetCategorization(assetEntryQuery, 1);
746            }
747    
748            @Test
749            public void testNotAllAssetTags4() throws Exception {
750                    String[] notAllTagNames =
751                            {"liferay", "architecture", "services", "osgi"};
752    
753                    AssetEntryQuery assetEntryQuery =
754                            AssetEntryQueryTestUtil.createAssetEntryQuery(
755                                    _group1.getGroupId(), getBaseModelClassName(), notAllTagNames,
756                                    null, null, null);
757    
758                    testAssetCategorization(assetEntryQuery, 2);
759            }
760    
761            @Test
762            public void testNotAllAssetTagsMultipleGroups1() throws Exception {
763                    String[] notAllTagNames = {"liferay"};
764    
765                    AssetEntryQuery assetEntryQuery =
766                            AssetEntryQueryTestUtil.createAssetEntryQuery(
767                                    new long[] {_group1.getGroupId(), _group2.getGroupId()},
768                                    getBaseModelClassName(), notAllTagNames, null, null, null);
769    
770                    testAssetCategorization(
771                            new Group[] {_group1, _group2}, assetEntryQuery, 0);
772            }
773    
774            @Test
775            public void testNotAllAssetTagsMultipleGroups2() throws Exception {
776                    String[] notAllTagNames = {"liferay", "architecture"};
777    
778                    AssetEntryQuery assetEntryQuery =
779                            AssetEntryQueryTestUtil.createAssetEntryQuery(
780                                    new long[] {_group1.getGroupId(), _group2.getGroupId()},
781                                    getBaseModelClassName(), notAllTagNames, null, null, null);
782    
783                    testAssetCategorization(
784                            new Group[] {_group1, _group2}, assetEntryQuery, 0);
785            }
786    
787            @Test
788            public void testNotAllAssetTagsMultipleGroups3() throws Exception {
789                    String[] notAllTagNames = {"liferay", "architecture", "services"};
790    
791                    AssetEntryQuery assetEntryQuery =
792                            AssetEntryQueryTestUtil.createAssetEntryQuery(
793                                    new long[] {_group1.getGroupId(), _group2.getGroupId()},
794                                    getBaseModelClassName(), notAllTagNames, null, null, null);
795    
796                    testAssetCategorization(
797                            new Group[] {_group1, _group2}, assetEntryQuery, 2);
798            }
799    
800            @Test
801            public void testNotAllAssetTagsMultipleGroups4() throws Exception {
802                    String[] notAllTagNames =
803                            {"liferay", "architecture", "services", "osgi"};
804    
805                    AssetEntryQuery assetEntryQuery =
806                            AssetEntryQueryTestUtil.createAssetEntryQuery(
807                                    new long[] {_group1.getGroupId(), _group2.getGroupId()},
808                                    getBaseModelClassName(), notAllTagNames, null, null, null);
809    
810                    testAssetCategorization(
811                            new Group[] {_group1, _group2}, assetEntryQuery, 4);
812            }
813    
814            @Test
815            public void testNotAnyAssetCategories1() throws Exception {
816                    long[] notAnyCategoryIds = {_healthCategoryId};
817    
818                    AssetEntryQuery assetEntryQuery =
819                            AssetEntryQueryTestUtil.createAssetEntryQuery(
820                                    _group1.getGroupId(), getBaseModelClassName(), null,
821                                    notAnyCategoryIds, null, null);
822    
823                    testAssetCategorization(assetEntryQuery, 0);
824            }
825    
826            @Test
827            public void testNotAnyAssetCategories2() throws Exception {
828                    long[] notAnyCategoryIds = {_healthCategoryId, _sportCategoryId};
829    
830                    AssetEntryQuery assetEntryQuery =
831                            AssetEntryQueryTestUtil.createAssetEntryQuery(
832                                    _group1.getGroupId(), getBaseModelClassName(), null,
833                                    notAnyCategoryIds, null, null);
834    
835                    testAssetCategorization(assetEntryQuery, 0);
836            }
837    
838            @Test
839            public void testNotAnyAssetCategories3() throws Exception {
840                    long[] notAnyCategoryIds =
841                            {_fashionCategoryId, _foodCategoryId, _travelCategoryId};
842    
843                    AssetEntryQuery assetEntryQuery =
844                            AssetEntryQueryTestUtil.createAssetEntryQuery(
845                                    _group1.getGroupId(), getBaseModelClassName(), null,
846                                    notAnyCategoryIds, null, null);
847    
848                    testAssetCategorization(assetEntryQuery, 0);
849            }
850    
851            @Test
852            public void testNotAnyAssetCategories4() throws Exception {
853                    long[] notAnyCategoryIds = {_fashionCategoryId, _foodCategoryId};
854    
855                    AssetEntryQuery assetEntryQuery =
856                            AssetEntryQueryTestUtil.createAssetEntryQuery(
857                                    _group1.getGroupId(), getBaseModelClassName(), null,
858                                    notAnyCategoryIds, null, null);
859    
860                    testAssetCategorization(assetEntryQuery, 1);
861            }
862    
863            @Test
864            public void testNotAnyAssetTags1() throws Exception {
865                    String[] notAnyTagNames = {"liferay"};
866    
867                    AssetEntryQuery assetEntryQuery =
868                            AssetEntryQueryTestUtil.createAssetEntryQuery(
869                                    _group1.getGroupId(), getBaseModelClassName(), null,
870                                    notAnyTagNames, null, null);
871    
872                    testAssetCategorization(assetEntryQuery, 0);
873            }
874    
875            @Test
876            public void testNotAnyAssetTags2() throws Exception {
877                    String[] notAnyTagNames = {"liferay", "architecture"};
878    
879                    AssetEntryQuery assetEntryQuery =
880                            AssetEntryQueryTestUtil.createAssetEntryQuery(
881                                    _group1.getGroupId(), getBaseModelClassName(), null,
882                                    notAnyTagNames, null, null);
883    
884                    testAssetCategorization(assetEntryQuery, 0);
885            }
886    
887            @Test
888            public void testNotAnyAssetTags3() throws Exception {
889                    String[] notAnyTagNames = {"liferay", "architecture", "services"};
890    
891                    AssetEntryQuery assetEntryQuery =
892                            AssetEntryQueryTestUtil.createAssetEntryQuery(
893                                    _group1.getGroupId(), getBaseModelClassName(), null,
894                                    notAnyTagNames, null, null);
895    
896                    testAssetCategorization(assetEntryQuery, 0);
897            }
898    
899            @Test
900            public void testNotAnyAssetTags4() throws Exception {
901                    String[] notAnyTagNames = {"modularity", "osgi"};
902    
903                    AssetEntryQuery assetEntryQuery =
904                            AssetEntryQueryTestUtil.createAssetEntryQuery(
905                                    _group1.getGroupId(), getBaseModelClassName(), null,
906                                    notAnyTagNames, null, null);
907    
908                    testAssetCategorization(assetEntryQuery, 1);
909            }
910    
911            @Test
912            public void testOrderByCreateDateAsc() throws Exception {
913                    AssetEntryQuery assetEntryQuery =
914                            AssetEntryQueryTestUtil.createAssetEntryQuery(
915                                    _group1.getGroupId(), new String[] {getBaseModelClassName()});
916    
917                    String[] titles = {
918                            "open", "liferay", "social", "osgi", "content", "life"
919                    };
920    
921                    testOrderByCreateDate(assetEntryQuery, "asc", titles, titles);
922            }
923    
924            @Test
925            public void testOrderByCreateDateDesc() throws Exception {
926                    AssetEntryQuery assetEntryQuery =
927                            AssetEntryQueryTestUtil.createAssetEntryQuery(
928                                    _group1.getGroupId(), new String[] {getBaseModelClassName()});
929    
930                    String[] titles = {
931                            "open", "liferay", "social", "osgi", "content", "life"
932                    };
933    
934                    String[] orderedTitles = {
935                            "life", "content", "osgi", "social", "liferay", "open"
936                    };
937    
938                    testOrderByCreateDate(assetEntryQuery, "desc", titles, orderedTitles);
939            }
940    
941            @Test
942            public void testOrderByExpirationDateAsc() throws Exception {
943                    AssetEntryQuery assetEntryQuery =
944                            AssetEntryQueryTestUtil.createAssetEntryQuery(
945                                    _group1.getGroupId(), new String[] {getBaseModelClassName()});
946    
947                    Date[] expirationDates = generateRandomDates(new Date(), 6);
948    
949                    testOrderByExpirationDate(assetEntryQuery, "asc", expirationDates);
950            }
951    
952            @Test
953            public void testOrderByExpirationDateDesc() throws Exception {
954                    AssetEntryQuery assetEntryQuery =
955                            AssetEntryQueryTestUtil.createAssetEntryQuery(
956                                    _group1.getGroupId(), new String[] {getBaseModelClassName()});
957    
958                    Date[] expirationDates = generateRandomDates(new Date(), 6);
959    
960                    testOrderByExpirationDate(assetEntryQuery, "desc", expirationDates);
961            }
962    
963            @Test
964            public void testOrderByTitleAsc() throws Exception {
965                    AssetEntryQuery assetEntryQuery =
966                            AssetEntryQueryTestUtil.createAssetEntryQuery(
967                                    _group1.getGroupId(), new String[] {getBaseModelClassName()});
968    
969                    String[] defaultTitles = {
970                            "open", "liferay", "content", "social", "osgi", "life"
971                    };
972    
973                    String[] frenchTitles = {
974                            "ouvert", "liferay", "content", "social", "osgi", "vie"
975                    };
976    
977                    List<Map<Locale, String>> titleMaps = new ArrayList<>();
978    
979                    for (int i = 0; i < defaultTitles.length; i++) {
980                            Map<Locale, String> titleMap = new HashMap<>();
981    
982                            titleMap.put(LocaleUtil.getDefault(), defaultTitles[i]);
983                            titleMap.put(LocaleUtil.FRANCE, frenchTitles[i]);
984    
985                            titleMaps.add(titleMap);
986                    }
987    
988                    String[] defaultOrderedTitles = {
989                            "content", "life", "liferay", "open", "osgi", "social"
990                    };
991    
992                    String[] frenchOrderedTitles = {
993                            "content", "liferay", "osgi", "ouvert", "social", "vie"
994                    };
995    
996                    List<Map<Locale, String>> orderedTitleMaps = new ArrayList<>();
997    
998                    for (int i = 0; i < defaultOrderedTitles.length; i++) {
999                            Map<Locale, String> titleMap = new HashMap<>();
1000    
1001                            titleMap.put(LocaleUtil.getDefault(), defaultOrderedTitles[i]);
1002    
1003                            String orderedTitle = frenchOrderedTitles[i];
1004    
1005                            if (!isLocalizableTitle()) {
1006                                    orderedTitle = defaultOrderedTitles[i];
1007                            }
1008    
1009                            titleMap.put(LocaleUtil.FRANCE, orderedTitle);
1010    
1011                            orderedTitleMaps.add(titleMap);
1012                    }
1013    
1014                    testOrderByTitle(
1015                            assetEntryQuery, "asc", titleMaps, orderedTitleMaps,
1016                            new Locale[] {LocaleUtil.getDefault(), LocaleUtil.FRANCE});
1017            }
1018    
1019            @Test
1020            public void testOrderByTitleDesc() throws Exception {
1021                    AssetEntryQuery assetEntryQuery =
1022                            AssetEntryQueryTestUtil.createAssetEntryQuery(
1023                                    _group1.getGroupId(), new String[] {getBaseModelClassName()});
1024    
1025                    String[] defaultTitles = {
1026                            "open", "liferay", "content", "social", "osgi", "life"
1027                    };
1028    
1029                    String[] frenchTitles = {
1030                            "ouvert", "liferay", "content", "social", "osgi", "vie"
1031                    };
1032    
1033                    List<Map<Locale, String>> titleMaps = new ArrayList<>();
1034    
1035                    for (int i = 0; i < defaultTitles.length; i++) {
1036                            Map<Locale, String> titleMap = new HashMap<>();
1037    
1038                            titleMap.put(LocaleUtil.getDefault(), defaultTitles[i]);
1039                            titleMap.put(LocaleUtil.FRANCE, frenchTitles[i]);
1040    
1041                            titleMaps.add(titleMap);
1042                    }
1043    
1044                    String[] defaultOrderedTitles = {
1045                            "social", "osgi", "open", "liferay", "life", "content"
1046                    };
1047    
1048                    String[] frenchOrderedTitles = {
1049                            "vie", "social", "ouvert", "osgi", "liferay", "content"
1050                    };
1051    
1052                    List<Map<Locale, String>> orderedTitleMaps = new ArrayList<>();
1053    
1054                    for (int i = 0; i < defaultOrderedTitles.length; i++) {
1055                            Map<Locale, String> titleMap = new HashMap<>();
1056    
1057                            titleMap.put(LocaleUtil.getDefault(), defaultOrderedTitles[i]);
1058    
1059                            String orderedTitle = frenchOrderedTitles[i];
1060    
1061                            if (!isLocalizableTitle()) {
1062                                    orderedTitle = defaultOrderedTitles[i];
1063                            }
1064    
1065                            titleMap.put(LocaleUtil.FRANCE, orderedTitle);
1066    
1067                            orderedTitleMaps.add(titleMap);
1068                    }
1069    
1070                    testOrderByTitle(
1071                            assetEntryQuery, "desc", titleMaps, orderedTitleMaps,
1072                            new Locale[] {LocaleUtil.getDefault(), LocaleUtil.FRANCE});
1073            }
1074    
1075            @Test
1076            public void testPaginationTypeNone() throws Exception {
1077                    AssetEntryQuery assetEntryQuery =
1078                            AssetEntryQueryTestUtil.createAssetEntryQuery(
1079                                    _group1.getGroupId(), new String[] {getBaseModelClassName()});
1080    
1081                    assetEntryQuery.setPaginationType("none");
1082    
1083                    testPaginationType(assetEntryQuery, 5);
1084            }
1085    
1086            @Test
1087            public void testPaginationTypeRegular() throws Exception {
1088                    AssetEntryQuery assetEntryQuery =
1089                            AssetEntryQueryTestUtil.createAssetEntryQuery(
1090                                    _group1.getGroupId(), new String[] {getBaseModelClassName()});
1091    
1092                    assetEntryQuery.setPaginationType("regular");
1093    
1094                    testPaginationType(assetEntryQuery, 5);
1095            }
1096    
1097            @Test
1098            public void testPaginationTypeSimple() throws Exception {
1099                    AssetEntryQuery assetEntryQuery =
1100                            AssetEntryQueryTestUtil.createAssetEntryQuery(
1101                                    _group1.getGroupId(), new String[] {getBaseModelClassName()});
1102    
1103                    assetEntryQuery.setPaginationType("simple");
1104    
1105                    testPaginationType(assetEntryQuery, 5);
1106            }
1107    
1108            protected abstract BaseModel<?> addBaseModel(
1109                            BaseModel<?> parentBaseModel, Map<Locale, String> titleMap,
1110                            ServiceContext serviceContext)
1111                    throws Exception;
1112    
1113            protected BaseModel<?> addBaseModel(
1114                            BaseModel<?> parentBaseModel, String keywords, Date expirationDate,
1115                            ServiceContext serviceContext)
1116                    throws Exception {
1117    
1118                    return addBaseModel(parentBaseModel, keywords, serviceContext);
1119            }
1120    
1121            protected abstract BaseModel<?> addBaseModel(
1122                            BaseModel<?> parentBaseModel, String keywords,
1123                            ServiceContext serviceContext)
1124                    throws Exception;
1125    
1126            protected List<BaseModel<?>> addBaseModels(
1127                            Group[] groups, String keywords, ServiceContext serviceContext)
1128                    throws Exception {
1129    
1130                    List<BaseModel<?>> baseModels = new ArrayList<>();
1131    
1132                    for (Group group : groups) {
1133                            User user = UserTestUtil.getAdminUser(group.getCompanyId());
1134    
1135                            serviceContext.setCompanyId(group.getCompanyId());
1136                            serviceContext.setScopeGroupId(group.getGroupId());
1137                            serviceContext.setUserId(user.getUserId());
1138    
1139                            BaseModel<?> parentBaseModel = getParentBaseModel(
1140                                    group, serviceContext);
1141    
1142                            baseModels.add(
1143                                    addBaseModel(parentBaseModel, keywords, serviceContext));
1144                    }
1145    
1146                    return baseModels;
1147            }
1148    
1149            protected BaseModel<?> addBaseModelWithClassType(
1150                            BaseModel<?> parentBaseModel, String keywords,
1151                            ServiceContext serviceContext)
1152                    throws Exception {
1153    
1154                    return addBaseModel(parentBaseModel, keywords, serviceContext);
1155            }
1156    
1157            protected BaseModel<?> addBaseModelWithWorkflow(
1158                            BaseModel<?> parentBaseModel, String keywords, boolean approved,
1159                            ServiceContext serviceContext)
1160                    throws Exception {
1161    
1162                    return addBaseModel(parentBaseModel, keywords, serviceContext);
1163            }
1164    
1165            protected void assertCount(
1166                            int expectedCount, AssetEntryQuery assetEntryQuery,
1167                            SearchContext searchContext)
1168                    throws Exception {
1169    
1170                    assertCount(
1171                            expectedCount, assetEntryQuery, searchContext, QueryUtil.ALL_POS,
1172                            QueryUtil.ALL_POS);
1173            }
1174    
1175            protected void assertCount(
1176                            final int expectedCount, final AssetEntryQuery assetEntryQuery,
1177                            final SearchContext searchContext, final int start, final int end)
1178                    throws Exception {
1179    
1180                    IdempotentRetryAssert.retryAssert(
1181                            3, TimeUnit.SECONDS,
1182                            new Callable<Void>() {
1183    
1184                                    @Override
1185                                    public Void call() throws Exception {
1186                                            int actualCount = searchCount(
1187                                                    assetEntryQuery, searchContext, start, end);
1188    
1189                                            Assert.assertEquals(expectedCount, actualCount);
1190    
1191                                            return null;
1192                                    }
1193    
1194                            });
1195            }
1196    
1197            protected Date[] generateRandomDates(Date startDate, int size) {
1198                    Date[] dates = new Date[size];
1199    
1200                    for (int i = 0; i < size; i++) {
1201                            Date date = new Date(
1202                                    startDate.getTime() + (RandomUtil.nextInt(365) + 1) * Time.DAY);
1203    
1204                            Calendar calendar = new GregorianCalendar();
1205    
1206                            calendar.setTime(date);
1207    
1208                            calendar.set(Calendar.SECOND, 0);
1209                            calendar.set(Calendar.MILLISECOND, 0);
1210    
1211                            dates[i] = calendar.getTime();
1212                    }
1213    
1214                    return dates;
1215            }
1216    
1217            protected abstract Class<?> getBaseModelClass();
1218    
1219            protected String getBaseModelClassName() {
1220                    Class<?> clazz = getBaseModelClass();
1221    
1222                    return clazz.getName();
1223            }
1224    
1225            protected long[] getClassTypeIds() {
1226                    return null;
1227            }
1228    
1229            protected BaseModel<?> getParentBaseModel(
1230                            Group group, ServiceContext serviceContext)
1231                    throws Exception {
1232    
1233                    return group;
1234            }
1235    
1236            protected abstract String getSearchKeywords();
1237    
1238            protected boolean isLocalizableTitle() {
1239                    return true;
1240            }
1241    
1242            protected AssetEntry[] search(
1243                            AssetEntryQuery assetEntryQuery, SearchContext searchContext)
1244                    throws Exception {
1245    
1246                    Hits results = AssetUtil.search(
1247                            searchContext, assetEntryQuery, QueryUtil.ALL_POS,
1248                            QueryUtil.ALL_POS);
1249    
1250                    List<AssetEntry> assetEntries = AssetUtil.getAssetEntries(results);
1251    
1252                    return assetEntries.toArray(new AssetEntry[assetEntries.size()]);
1253            }
1254    
1255            protected int searchCount(
1256                            AssetEntryQuery assetEntryQuery, SearchContext searchContext,
1257                            int start, int end)
1258                    throws Exception {
1259    
1260                    Hits results = AssetUtil.search(
1261                            searchContext, assetEntryQuery, start, end);
1262    
1263                    return results.getLength();
1264            }
1265    
1266            protected void testAssetCategorization(
1267                            AssetEntryQuery assetEntryQuery, int expectedResults)
1268                    throws Exception {
1269    
1270                    testAssetCategorization(
1271                            new Group[] {_group1}, assetEntryQuery, expectedResults);
1272            }
1273    
1274            protected void testAssetCategorization(
1275                            Group[] groups, AssetEntryQuery assetEntryQuery,
1276                            int expectedResults)
1277                    throws Exception {
1278    
1279                    SearchContext searchContext = SearchContextTestUtil.getSearchContext();
1280    
1281                    searchContext.setGroupIds(assetEntryQuery.getGroupIds());
1282    
1283                    int initialEntries = 0;
1284    
1285                    assertCount(initialEntries, assetEntryQuery, searchContext);
1286    
1287                    ServiceContext serviceContext =
1288                            ServiceContextTestUtil.getServiceContext(groups[0].getGroupId());
1289    
1290                    serviceContext.setAssetTagNames(_assetTagsNames1);
1291                    serviceContext.setAssetCategoryIds(_assetCategoryIds1);
1292    
1293                    addBaseModels(groups, getSearchKeywords(), serviceContext);
1294    
1295                    serviceContext.setAssetTagNames(_assetTagsNames2);
1296                    serviceContext.setAssetCategoryIds(_assetCategoryIds2);
1297    
1298                    addBaseModels(groups, getSearchKeywords(), serviceContext);
1299    
1300                    assertCount(
1301                            initialEntries + expectedResults, assetEntryQuery, searchContext);
1302            }
1303    
1304            protected void testClassNames(
1305                            AssetEntryQuery assetEntryQuery, int expectedResult)
1306                    throws Exception {
1307    
1308                    ServiceContext serviceContext =
1309                            ServiceContextTestUtil.getServiceContext(_group1.getGroupId());
1310    
1311                    BaseModel<?> parentBaseModel = getParentBaseModel(
1312                            _group1, serviceContext);
1313    
1314                    SearchContext searchContext = SearchContextTestUtil.getSearchContext();
1315    
1316                    searchContext.setGroupIds(assetEntryQuery.getGroupIds());
1317    
1318                    int initialEntries = 0;
1319    
1320                    assertCount(initialEntries, assetEntryQuery, searchContext);
1321    
1322                    addBaseModel(parentBaseModel, getSearchKeywords(), serviceContext);
1323    
1324                    assertCount(
1325                            initialEntries + expectedResult, assetEntryQuery, searchContext);
1326            }
1327    
1328            protected void testClassTypeIds(
1329                            AssetEntryQuery assetEntryQuery, boolean classType)
1330                    throws Exception {
1331    
1332                    ServiceContext serviceContext =
1333                            ServiceContextTestUtil.getServiceContext(_group1.getGroupId());
1334    
1335                    BaseModel<?> parentBaseModel = getParentBaseModel(
1336                            _group1, serviceContext);
1337    
1338                    SearchContext searchContext = SearchContextTestUtil.getSearchContext();
1339    
1340                    searchContext.setGroupIds(assetEntryQuery.getGroupIds());
1341    
1342                    int initialEntries = 0;
1343    
1344                    assertCount(initialEntries, assetEntryQuery, searchContext);
1345    
1346                    addBaseModelWithClassType(
1347                            parentBaseModel, getSearchKeywords(), serviceContext);
1348    
1349                    if (classType) {
1350                            assetEntryQuery.setClassTypeIds(getClassTypeIds());
1351    
1352                            assertCount(initialEntries + 1, assetEntryQuery, searchContext);
1353                    }
1354                    else {
1355                            assetEntryQuery.setClassTypeIds(new long[] {0});
1356    
1357                            assertCount(initialEntries, assetEntryQuery, searchContext);
1358                    }
1359            }
1360    
1361            protected void testOrderByCreateDate(
1362                            AssetEntryQuery assetEntryQuery, String orderByType,
1363                            String[] titles, String[] orderedTitles)
1364                    throws Exception {
1365    
1366                    ServiceContext serviceContext =
1367                            ServiceContextTestUtil.getServiceContext(_group1.getGroupId());
1368    
1369                    BaseModel<?> parentBaseModel = getParentBaseModel(
1370                            _group1, serviceContext);
1371    
1372                    SearchContext searchContext = SearchContextTestUtil.getSearchContext();
1373    
1374                    searchContext.setGroupIds(assetEntryQuery.getGroupIds());
1375    
1376                    BaseModel<?>[] baseModels = new BaseModel[titles.length];
1377    
1378                    for (int i = 0; i < titles.length; i++) {
1379                            String title = titles[i];
1380    
1381                            baseModels[i] = addBaseModel(
1382                                    parentBaseModel, title, serviceContext);
1383                    }
1384    
1385                    assetEntryQuery.setOrderByCol1("createDate");
1386                    assetEntryQuery.setOrderByType1(orderByType);
1387    
1388                    AssetEntry[] assetEntries = search(assetEntryQuery, searchContext);
1389    
1390                    for (int i = 0; i < assetEntries.length; i++) {
1391                            AssetEntry assetEntry = assetEntries[i];
1392    
1393                            String title = assetEntry.getTitle(LocaleUtil.getDefault());
1394    
1395                            Assert.assertEquals(title, orderedTitles[i]);
1396                    }
1397            }
1398    
1399            protected void testOrderByExpirationDate(
1400                            AssetEntryQuery assetEntryQuery, String orderByType,
1401                            Date[] expirationDates)
1402                    throws Exception {
1403    
1404                    ServiceContext serviceContext =
1405                            ServiceContextTestUtil.getServiceContext(_group1.getGroupId());
1406    
1407                    BaseModel<?> parentBaseModel = getParentBaseModel(
1408                            _group1, serviceContext);
1409    
1410                    SearchContext searchContext = SearchContextTestUtil.getSearchContext();
1411    
1412                    searchContext.setGroupIds(assetEntryQuery.getGroupIds());
1413    
1414                    for (Date expirationDate : expirationDates) {
1415                            addBaseModel(
1416                                    parentBaseModel, RandomTestUtil.randomString(), expirationDate,
1417                                    serviceContext);
1418                    }
1419    
1420                    assetEntryQuery.setOrderByCol1("expirationDate");
1421                    assetEntryQuery.setOrderByType1(orderByType);
1422    
1423                    Arrays.sort(expirationDates);
1424    
1425                    DateFormat dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
1426                            PropsValues.INDEX_DATE_FORMAT_PATTERN);
1427    
1428                    AssetEntry[] assetEntries = search(assetEntryQuery, searchContext);
1429    
1430                    for (int i = 0; i < assetEntries.length; i++) {
1431                            AssetEntry assetEntry = assetEntries[i];
1432    
1433                            String expirationDate = dateFormat.format(
1434                                    assetEntry.getExpirationDate());
1435    
1436                            int index = i;
1437    
1438                            if (orderByType.equals("desc")) {
1439                                    index = assetEntries.length - 1 - i;
1440                            }
1441    
1442                            Assert.assertEquals(
1443                                    expirationDate, dateFormat.format(expirationDates[index]));
1444                    }
1445            }
1446    
1447            protected void testOrderByTitle(
1448                            AssetEntryQuery assetEntryQuery, String orderByType,
1449                            List<Map<Locale, String>> titleMaps,
1450                            List<Map<Locale, String>> orderedTitleMaps, Locale[] locales)
1451                    throws Exception {
1452    
1453                    ServiceContext serviceContext =
1454                            ServiceContextTestUtil.getServiceContext(_group1.getGroupId());
1455    
1456                    BaseModel<?> parentBaseModel = getParentBaseModel(
1457                            _group1, serviceContext);
1458    
1459                    for (Map<Locale, String> titleMap : titleMaps) {
1460                            addBaseModel(parentBaseModel, titleMap, serviceContext);
1461                    }
1462    
1463                    assetEntryQuery.setOrderByCol1("title");
1464                    assetEntryQuery.setOrderByType1(orderByType);
1465    
1466                    SearchContext searchContext = SearchContextTestUtil.getSearchContext();
1467    
1468                    searchContext.setGroupIds(assetEntryQuery.getGroupIds());
1469    
1470                    for (Locale locale : locales) {
1471                            searchContext.setLocale(locale);
1472    
1473                            AssetEntry[] assetEntries = search(assetEntryQuery, searchContext);
1474    
1475                            for (int i = 0; i < assetEntries.length; i++) {
1476                                    AssetEntry assetEntry = assetEntries[i];
1477    
1478                                    String title = assetEntry.getTitle(locale);
1479    
1480                                    Map<Locale, String> orderedTitleMap = orderedTitleMaps.get(i);
1481    
1482                                    Assert.assertEquals(orderedTitleMap.get(locale), title);
1483                            }
1484                    }
1485            }
1486    
1487            protected void testPaginationType(AssetEntryQuery assetEntryQuery, int size)
1488                    throws Exception {
1489    
1490                    ServiceContext serviceContext =
1491                            ServiceContextTestUtil.getServiceContext(_group1.getGroupId());
1492    
1493                    BaseModel<?> parentBaseModel = getParentBaseModel(
1494                            _group1, serviceContext);
1495    
1496                    SearchContext searchContext = SearchContextTestUtil.getSearchContext();
1497    
1498                    searchContext.setGroupIds(assetEntryQuery.getGroupIds());
1499    
1500                    for (int i = 0; i < size; i++) {
1501                            addBaseModel(
1502                                    parentBaseModel, RandomTestUtil.randomString(), serviceContext);
1503                    }
1504    
1505                    assertCount(size, assetEntryQuery, searchContext, 0, 1);
1506            }
1507    
1508            private long[] _assetCategoryIds1;
1509            private long[] _assetCategoryIds2;
1510            private String[] _assetTagsNames1;
1511            private String[] _assetTagsNames2;
1512            private long _fashionCategoryId;
1513            private long _foodCategoryId;
1514    
1515            @DeleteAfterTestRun
1516            private Group _group1;
1517    
1518            @DeleteAfterTestRun
1519            private Group _group2;
1520    
1521            private long _healthCategoryId;
1522            private long _sportCategoryId;
1523            private long _travelCategoryId;
1524            private long _vocabularyId;
1525    
1526    }