001
014
015 package com.liferay.portlet.documentlibrary.util.test;
016
017 import com.liferay.portal.kernel.repository.model.FileEntry;
018 import com.liferay.portal.kernel.test.util.RandomTestUtil;
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.kernel.util.Validator;
023 import com.liferay.portal.kernel.workflow.WorkflowConstants;
024 import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
025 import com.liferay.portal.service.ServiceContext;
026 import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
027 import com.liferay.portlet.documentlibrary.model.DLSyncConstants;
028 import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
029 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
030
031 import java.io.Serializable;
032
033 import java.util.HashMap;
034 import java.util.Map;
035
036
039 public abstract class DLAppTestUtil {
040
041 public static FileEntry addFileEntryWithWorkflow(
042 long userId, long groupId, long folderId, String sourceFileName,
043 String title, boolean approved, ServiceContext serviceContext)
044 throws Exception {
045
046 boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
047
048 try {
049 WorkflowThreadLocal.setEnabled(true);
050
051 serviceContext = (ServiceContext)serviceContext.clone();
052
053 serviceContext.setWorkflowAction(
054 WorkflowConstants.ACTION_SAVE_DRAFT);
055
056 FileEntry fileEntry = DLAppLocalServiceUtil.addFileEntry(
057 userId, groupId, folderId, sourceFileName,
058 ContentTypes.TEXT_PLAIN, title, StringPool.BLANK,
059 StringPool.BLANK, RandomTestUtil.randomBytes(), serviceContext);
060
061 if (approved) {
062 return updateStatus(fileEntry, serviceContext);
063 }
064
065 return fileEntry;
066 }
067 finally {
068 WorkflowThreadLocal.setEnabled(workflowEnabled);
069 }
070 }
071
072 public static void populateNotificationsServiceContext(
073 ServiceContext serviceContext, String command)
074 throws Exception {
075
076 serviceContext.setAttribute("entryURL", "http:
077
078 if (Validator.isNotNull(command)) {
079 serviceContext.setCommand(command);
080 }
081
082 serviceContext.setLayoutFullURL("http:
083 }
084
085 public static void populateServiceContext(
086 ServiceContext serviceContext, long fileEntryTypeId)
087 throws Exception {
088
089 if (fileEntryTypeId !=
090 DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL) {
091
092 serviceContext.setAttribute("fileEntryTypeId", fileEntryTypeId);
093 }
094
095 serviceContext.setLayoutFullURL("http:
096 }
097
098 protected static FileEntry updateStatus(
099 FileEntry fileEntry, ServiceContext serviceContext)
100 throws Exception {
101
102 Map<String, Serializable> workflowContext = new HashMap<>();
103
104 workflowContext.put(WorkflowConstants.CONTEXT_URL, "http:
105 workflowContext.put("event", DLSyncConstants.EVENT_ADD);
106
107 DLFileEntryLocalServiceUtil.updateStatus(
108 TestPropsValues.getUserId(),
109 fileEntry.getFileVersion().getFileVersionId(),
110 WorkflowConstants.STATUS_APPROVED, serviceContext, workflowContext);
111
112 return DLAppLocalServiceUtil.getFileEntry(fileEntry.getFileEntryId());
113 }
114
115 }