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