001
014
015 package com.liferay.portlet.messageboards.util.test;
016
017 import com.liferay.portal.kernel.test.util.RandomTestUtil;
018 import com.liferay.portal.kernel.util.ObjectValuePair;
019 import com.liferay.portal.kernel.util.Validator;
020 import com.liferay.portal.kernel.workflow.WorkflowConstants;
021 import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
022 import com.liferay.portal.service.ServiceContext;
023 import com.liferay.portlet.messageboards.model.MBMessage;
024 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
025
026 import java.io.InputStream;
027 import java.io.Serializable;
028
029 import java.util.ArrayList;
030 import java.util.HashMap;
031 import java.util.List;
032 import java.util.Map;
033
034
038 public class MBTestUtil {
039
040 public static MBMessage addMessageWithWorkflow(
041 long userId, long groupId, long categoryId, String subject,
042 String body, boolean approved, ServiceContext serviceContext)
043 throws Exception {
044
045 boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
046
047 try {
048 WorkflowThreadLocal.setEnabled(true);
049
050 serviceContext = (ServiceContext)serviceContext.clone();
051
052 serviceContext.setWorkflowAction(
053 WorkflowConstants.ACTION_SAVE_DRAFT);
054
055 MBMessage message = MBMessageLocalServiceUtil.addMessage(
056 serviceContext.getUserId(), RandomTestUtil.randomString(),
057 groupId, categoryId, subject, body, serviceContext);
058
059 if (approved) {
060 return updateStatus(message, serviceContext);
061 }
062
063 return message;
064 }
065 finally {
066 WorkflowThreadLocal.setEnabled(workflowEnabled);
067 }
068 }
069
070 public static List<ObjectValuePair<String, InputStream>> getInputStreamOVPs(
071 String fileName, Class<?> clazz, String keywords) {
072
073 List<ObjectValuePair<String, InputStream>> inputStreamOVPs =
074 new ArrayList<>(1);
075
076 InputStream inputStream = clazz.getResourceAsStream(
077 "dependencies/" + fileName);
078
079 ObjectValuePair<String, InputStream> inputStreamOVP = null;
080
081 if (Validator.isBlank(keywords)) {
082 inputStreamOVP = new ObjectValuePair<>(fileName, inputStream);
083 }
084 else {
085 inputStreamOVP = new ObjectValuePair<>(keywords, inputStream);
086 }
087
088 inputStreamOVPs.add(inputStreamOVP);
089
090 return inputStreamOVPs;
091 }
092
093 public static void populateNotificationsServiceContext(
094 ServiceContext serviceContext, String command)
095 throws Exception {
096
097 serviceContext.setAttribute("entryURL", "http:
098
099 if (Validator.isNotNull(command)) {
100 serviceContext.setCommand(command);
101 }
102
103 serviceContext.setLayoutFullURL("http:
104 }
105
106 protected static MBMessage updateStatus(
107 MBMessage message, ServiceContext serviceContext)
108 throws Exception {
109
110 Map<String, Serializable> workflowContext = new HashMap<>();
111
112 workflowContext.put(WorkflowConstants.CONTEXT_URL, "http:
113
114 message = MBMessageLocalServiceUtil.updateStatus(
115 message.getUserId(), message.getMessageId(),
116 WorkflowConstants.STATUS_APPROVED, serviceContext, workflowContext);
117
118 return message;
119 }
120
121 }