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.util.LocaleUtil;
019    import com.liferay.portal.service.ServiceContext;
020    import com.liferay.portal.util.test.RandomTestUtil;
021    import com.liferay.portal.util.test.ServiceContextTestUtil;
022    import com.liferay.portal.util.test.TestPropsValues;
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<Locale, String>();
079    
080                    Locale locale = LocaleUtil.getSiteDefault();
081    
082                    titleMap.put(locale, RandomTestUtil.randomString());
083    
084                    Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
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(), 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                    String[] tagProperties = null;
103    
104                    ServiceContext serviceContext =
105                            ServiceContextTestUtil.getServiceContext(groupId, userId);
106    
107                    return AssetTagLocalServiceUtil.addTag(
108                            userId, RandomTestUtil.randomString(), tagProperties,
109                            serviceContext);
110            }
111    
112            public static AssetVocabulary addVocabulary(long groupId) throws Exception {
113                    long userId = TestPropsValues.getUserId();
114    
115                    ServiceContext serviceContext =
116                            ServiceContextTestUtil.getServiceContext(groupId, userId);
117    
118                    return AssetVocabularyLocalServiceUtil.addVocabulary(
119                            userId, RandomTestUtil.randomString(), serviceContext);
120            }
121    
122            public static AssetVocabulary addVocabulary(
123                            long groupId, long classNameId, long classTypePK, boolean required)
124                    throws Exception {
125    
126                    Map<Locale, String> titleMap = new HashMap<Locale, String>();
127    
128                    Locale locale = LocaleUtil.getSiteDefault();
129    
130                    titleMap.put(locale, RandomTestUtil.randomString());
131    
132                    Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
133    
134                    descriptionMap.put(locale, RandomTestUtil.randomString());
135    
136                    AssetVocabularySettingsHelper vocabularySettingsHelper =
137                            new AssetVocabularySettingsHelper();
138    
139                    vocabularySettingsHelper.setClassNameIdsAndClassTypePKs(
140                            new long[] {classNameId}, new long[] {classTypePK},
141                            new boolean[] {required});
142                    vocabularySettingsHelper.setMultiValued(true);
143    
144                    ServiceContext serviceContext =
145                            ServiceContextTestUtil.getServiceContext(
146                                    groupId, TestPropsValues.getUserId());
147    
148                    AssetVocabulary vocabulary = AssetVocabularyServiceUtil.addVocabulary(
149                            RandomTestUtil.randomString(), titleMap, descriptionMap,
150                            vocabularySettingsHelper.toString(), serviceContext);
151    
152                    return vocabulary;
153            }
154    
155    }