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.blogs.util.test;
016    
017    import com.liferay.portal.kernel.editor.EditorConstants;
018    import com.liferay.portal.kernel.test.util.RandomTestUtil;
019    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
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.blogs.model.BlogsEntry;
027    import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
028    
029    import java.io.Serializable;
030    
031    import java.util.Calendar;
032    import java.util.HashMap;
033    import java.util.Map;
034    
035    import org.junit.Assert;
036    
037    /**
038     * @author Zsolt Berentey
039     */
040    public class BlogsTestUtil {
041    
042            public static BlogsEntry addEntryWithWorkflow(
043                            long userId, String title, boolean approved,
044                            ServiceContext serviceContext)
045                    throws Exception {
046    
047                    boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
048    
049                    try {
050                            WorkflowThreadLocal.setEnabled(true);
051    
052                            Calendar displayCalendar = CalendarFactoryUtil.getCalendar(
053                                    2012, 1, 1);
054    
055                            serviceContext = (ServiceContext)serviceContext.clone();
056    
057                            serviceContext.setWorkflowAction(
058                                    WorkflowConstants.ACTION_SAVE_DRAFT);
059    
060                            BlogsEntry entry = BlogsEntryLocalServiceUtil.addEntry(
061                                    userId, title, RandomTestUtil.randomString(),
062                                    RandomTestUtil.randomString(), RandomTestUtil.randomString(),
063                                    displayCalendar.getTime(), true, true, new String[0],
064                                    StringPool.BLANK, null, null, serviceContext);
065    
066                            if (approved) {
067                                    return updateStatus(entry, serviceContext);
068                            }
069    
070                            return entry;
071                    }
072                    finally {
073                            WorkflowThreadLocal.setEnabled(workflowEnabled);
074                    }
075            }
076    
077            public static void assertEquals(
078                    BlogsEntry expectedEntry, BlogsEntry actualEntry) {
079    
080                    Assert.assertEquals(expectedEntry.getUserId(), actualEntry.getUserId());
081                    Assert.assertEquals(expectedEntry.getTitle(), actualEntry.getTitle());
082                    Assert.assertEquals(
083                            expectedEntry.getDescription(), actualEntry.getDescription());
084                    Assert.assertEquals(
085                            expectedEntry.getContent(), actualEntry.getContent());
086                    Assert.assertEquals(
087                            expectedEntry.getDisplayDate(), actualEntry.getDisplayDate());
088                    Assert.assertEquals(
089                            expectedEntry.isAllowPingbacks(), actualEntry.isAllowPingbacks());
090                    Assert.assertEquals(
091                            expectedEntry.isAllowTrackbacks(), actualEntry.isAllowTrackbacks());
092                    Assert.assertEquals(
093                            expectedEntry.isSmallImage(), actualEntry.isSmallImage());
094            }
095    
096            public static String getTempBlogsEntryAttachmentFileEntryImgTag(
097                    long dataImageId, String url) {
098    
099                    StringBundler sb = new StringBundler(7);
100    
101                    sb.append("<img ");
102                    sb.append(EditorConstants.ATTRIBUTE_DATA_IMAGE_ID);
103                    sb.append("=\"");
104                    sb.append(dataImageId);
105                    sb.append("\" src=\"");
106                    sb.append(url);
107                    sb.append("\"/>");
108    
109                    return sb.toString();
110            }
111    
112            public static void populateNotificationsServiceContext(
113                            ServiceContext serviceContext, String command)
114                    throws Exception {
115    
116                    serviceContext.setAttribute("entryURL", "http://localhost");
117    
118                    if (Validator.isNotNull(command)) {
119                            serviceContext.setCommand(command);
120                    }
121    
122                    serviceContext.setLayoutFullURL("http://localhost");
123            }
124    
125            protected static BlogsEntry updateStatus(
126                            BlogsEntry entry, ServiceContext serviceContext)
127                    throws Exception {
128    
129                    Map<String, Serializable> workflowContext = new HashMap<>();
130    
131                    workflowContext.put(WorkflowConstants.CONTEXT_URL, "http://localhost");
132                    workflowContext.put(
133                            WorkflowConstants.CONTEXT_USER_PORTRAIT_URL, "http://localhost");
134                    workflowContext.put(
135                            WorkflowConstants.CONTEXT_USER_URL, "http://localhost");
136    
137                    return BlogsEntryLocalServiceUtil.updateStatus(
138                            entry.getUserId(), entry.getEntryId(),
139                            WorkflowConstants.STATUS_APPROVED, serviceContext, workflowContext);
140            }
141    
142    }