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