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.util.test;
016    
017    import com.liferay.counter.service.CounterLocalServiceUtil;
018    import com.liferay.portal.kernel.test.util.RandomTestUtil;
019    import com.liferay.portal.kernel.test.util.ServiceContextTestUtil;
020    import com.liferay.portal.kernel.test.util.TestPropsValues;
021    import com.liferay.portal.kernel.util.LocaleUtil;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portlet.asset.model.AssetCategory;
024    import com.liferay.portlet.asset.model.AssetCategoryConstants;
025    import com.liferay.portlet.asset.model.AssetEntry;
026    import com.liferay.portlet.asset.model.AssetTag;
027    import com.liferay.portlet.asset.model.AssetVocabulary;
028    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
029    import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
030    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
031    import com.liferay.portlet.asset.service.AssetVocabularyLocalServiceUtil;
032    import com.liferay.portlet.asset.service.AssetVocabularyServiceUtil;
033    import com.liferay.portlet.asset.util.AssetVocabularySettingsHelper;
034    
035    import java.util.Date;
036    import java.util.HashMap;
037    import java.util.Locale;
038    import java.util.Map;
039    
040    /**
041     * @author Mate Thurzo
042     */
043    public class AssetTestUtil {
044    
045            public static AssetEntry addAssetEntry(long groupId) throws Exception {
046                    return addAssetEntry(groupId, null);
047            }
048    
049            public static AssetEntry addAssetEntry(long groupId, Date publishDate)
050                    throws Exception {
051    
052                    long assetEntryId = CounterLocalServiceUtil.increment();
053    
054                    AssetEntry assetEntry = AssetEntryLocalServiceUtil.createAssetEntry(
055                            assetEntryId);
056    
057                    assetEntry.setClassName(RandomTestUtil.randomString());
058                    assetEntry.setClassPK(RandomTestUtil.randomLong());
059                    assetEntry.setGroupId(groupId);
060                    assetEntry.setPublishDate(publishDate);
061                    assetEntry.setVisible(true);
062    
063                    return AssetEntryLocalServiceUtil.updateAssetEntry(assetEntry);
064            }
065    
066            public static AssetCategory addCategory(long groupId, long vocabularyId)
067                    throws Exception {
068    
069                    return addCategory(
070                            groupId, vocabularyId,
071                            AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
072            }
073    
074            public static AssetCategory addCategory(
075                            long groupId, long vocabularyId, long parentCategoryId)
076                    throws Exception {
077    
078                    Map<Locale, String> titleMap = new HashMap<>();
079    
080                    Locale locale = LocaleUtil.getSiteDefault();
081    
082                    titleMap.put(locale, RandomTestUtil.randomString());
083    
084                    Map<Locale, String> descriptionMap = new HashMap<>();
085    
086                    descriptionMap.put(locale, RandomTestUtil.randomString());
087    
088                    String[] categoryProperties = null;
089    
090                    ServiceContext serviceContext =
091                            ServiceContextTestUtil.getServiceContext(
092                                    groupId, TestPropsValues.getUserId());
093    
094                    return AssetCategoryLocalServiceUtil.addCategory(
095                            TestPropsValues.getUserId(), groupId, parentCategoryId, titleMap,
096                            descriptionMap, vocabularyId, categoryProperties, serviceContext);
097            }
098    
099            public static AssetTag addTag(long groupId) throws Exception {
100                    long userId = TestPropsValues.getUserId();
101    
102                    ServiceContext serviceContext =
103                            ServiceContextTestUtil.getServiceContext(groupId, userId);
104    
105                    return AssetTagLocalServiceUtil.addTag(
106                            userId, groupId, RandomTestUtil.randomString(), serviceContext);
107            }
108    
109            public static AssetVocabulary addVocabulary(long groupId) throws Exception {
110                    long userId = TestPropsValues.getUserId();
111    
112                    ServiceContext serviceContext =
113                            ServiceContextTestUtil.getServiceContext(groupId, userId);
114    
115                    return AssetVocabularyLocalServiceUtil.addVocabulary(
116                            userId, groupId, RandomTestUtil.randomString(), serviceContext);
117            }
118    
119            public static AssetVocabulary addVocabulary(
120                            long groupId, long classNameId, long classTypePK, boolean required)
121                    throws Exception {
122    
123                    Map<Locale, String> titleMap = new HashMap<>();
124    
125                    Locale locale = LocaleUtil.getSiteDefault();
126    
127                    titleMap.put(locale, RandomTestUtil.randomString());
128    
129                    Map<Locale, String> descriptionMap = new HashMap<>();
130    
131                    descriptionMap.put(locale, RandomTestUtil.randomString());
132    
133                    AssetVocabularySettingsHelper vocabularySettingsHelper =
134                            new AssetVocabularySettingsHelper();
135    
136                    vocabularySettingsHelper.setClassNameIdsAndClassTypePKs(
137                            new long[] {classNameId}, new long[] {classTypePK},
138                            new boolean[] {required});
139                    vocabularySettingsHelper.setMultiValued(true);
140    
141                    ServiceContext serviceContext =
142                            ServiceContextTestUtil.getServiceContext(
143                                    groupId, TestPropsValues.getUserId());
144    
145                    AssetVocabulary vocabulary = AssetVocabularyServiceUtil.addVocabulary(
146                            groupId, RandomTestUtil.randomString(), titleMap, descriptionMap,
147                            vocabularySettingsHelper.toString(), serviceContext);
148    
149                    return vocabulary;
150            }
151    
152    }