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.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    /**
035     * @author Eudaldo Alonso
036     * @author Daniel Kocsis
037     */
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://localhost");
098    
099                    if (Validator.isNotNull(command)) {
100                            serviceContext.setCommand(command);
101                    }
102    
103                    serviceContext.setLayoutFullURL("http://localhost");
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://localhost");
113    
114                    message = MBMessageLocalServiceUtil.updateStatus(
115                            message.getUserId(), message.getMessageId(),
116                            WorkflowConstants.STATUS_APPROVED, serviceContext, workflowContext);
117    
118                    return message;
119            }
120    
121    }