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.documentlibrary.util.test;
016    
017    import com.liferay.portal.kernel.test.util.RandomTestUtil;
018    import com.liferay.portal.kernel.test.util.ServiceContextTestUtil;
019    import com.liferay.portal.kernel.test.util.TestPropsValues;
020    import com.liferay.portal.kernel.util.ContentTypes;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portal.test.randomizerbumpers.TikaSafeRandomizerBumper;
024    import com.liferay.portlet.documentlibrary.exception.NoSuchFolderException;
025    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
026    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
027    import com.liferay.portlet.documentlibrary.model.DLFolder;
028    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
029    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
030    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
031    
032    import java.io.ByteArrayInputStream;
033    import java.io.InputStream;
034    
035    /**
036     * @author Adolfo P??rez
037     */
038    public class DLTestUtil {
039    
040            public static DLFileEntry addDLFileEntry(long dlFolderId) throws Exception {
041                    DLFolder dlFolder = DLFolderLocalServiceUtil.fetchDLFolder(dlFolderId);
042    
043                    byte[] bytes = RandomTestUtil.randomBytes(
044                            TikaSafeRandomizerBumper.INSTANCE);
045    
046                    InputStream is = new ByteArrayInputStream(bytes);
047    
048                    ServiceContext serviceContext =
049                            ServiceContextTestUtil.getServiceContext(dlFolder.getGroupId());
050    
051                    return DLFileEntryLocalServiceUtil.addFileEntry(
052                            TestPropsValues.getUserId(), dlFolder.getGroupId(),
053                            dlFolder.getRepositoryId(), dlFolder.getFolderId(),
054                            RandomTestUtil.randomString(), ContentTypes.TEXT_PLAIN,
055                            RandomTestUtil.randomString(), StringPool.BLANK, StringPool.BLANK,
056                            DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT, null,
057                            null, is, bytes.length, serviceContext);
058            }
059    
060            public static DLFolder addDLFolder(long groupId) throws Exception {
061                    ServiceContext serviceContext =
062                            ServiceContextTestUtil.getServiceContext(groupId);
063    
064                    return addDLFolder(groupId, serviceContext);
065            }
066    
067            public static DLFolder addDLFolder(
068                            long groupId, boolean deleteExisting, ServiceContext serviceContext)
069                    throws Exception {
070    
071                    return addDLFolder(
072                            groupId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, deleteExisting,
073                            serviceContext);
074            }
075    
076            public static DLFolder addDLFolder(
077                            long groupId, long parentFolderId, boolean deleteExisting,
078                            ServiceContext serviceContext)
079                    throws Exception {
080    
081                    String name = RandomTestUtil.randomString();
082    
083                    if (deleteExisting) {
084                            try {
085                                    DLFolder folder = DLFolderLocalServiceUtil.getFolder(
086                                            groupId, parentFolderId, name);
087    
088                                    DLFolderLocalServiceUtil.deleteFolder(folder.getFolderId());
089                            }
090                            catch (NoSuchFolderException nsfe) {
091                            }
092                    }
093    
094                    return DLFolderLocalServiceUtil.addFolder(
095                            TestPropsValues.getUserId(), groupId, groupId, false,
096                            parentFolderId, name, StringPool.BLANK, false, serviceContext);
097            }
098    
099            public static DLFolder addDLFolder(
100                            long groupId, ServiceContext serviceContext)
101                    throws Exception {
102    
103                    return addDLFolder(groupId, true, serviceContext);
104            }
105    
106    }