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