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