001
014
015 package com.liferay.portlet.blogs.util.test;
016
017 import com.liferay.portal.kernel.repository.model.FileEntry;
018 import com.liferay.portal.kernel.servlet.taglib.ui.ImageSelector;
019 import com.liferay.portal.kernel.util.Constants;
020 import com.liferay.portal.kernel.util.MimeTypesUtil;
021 import com.liferay.portal.kernel.util.StringPool;
022 import com.liferay.portal.kernel.util.TempFileEntryUtil;
023 import com.liferay.portal.kernel.workflow.WorkflowConstants;
024 import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
025 import com.liferay.portal.model.Group;
026 import com.liferay.portal.service.GroupLocalServiceUtil;
027 import com.liferay.portal.service.ServiceContext;
028 import com.liferay.portal.util.test.RandomTestUtil;
029 import com.liferay.portal.util.test.ServiceContextTestUtil;
030 import com.liferay.portal.util.test.TestPropsValues;
031 import com.liferay.portlet.blogs.model.BlogsEntry;
032 import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
033
034 import java.io.InputStream;
035 import java.io.Serializable;
036
037 import java.util.HashMap;
038 import java.util.Map;
039
040 import org.junit.Assert;
041
042
045 public class BlogsTestUtil {
046
047 public static BlogsEntry addEntry(Group group, boolean approved)
048 throws Exception {
049
050 return addEntry(TestPropsValues.getUserId(), group, approved);
051 }
052
053 public static BlogsEntry addEntry(
054 long userId, Group group, boolean approved)
055 throws Exception {
056
057 return addEntry(userId, group, "Title", approved);
058 }
059
060 public static BlogsEntry addEntry(
061 long userId, Group group, boolean approved, boolean smallImage)
062 throws Exception {
063
064 return addEntry(userId, group, "Title", approved, smallImage);
065 }
066
067 public static BlogsEntry addEntry(
068 long userId, Group group, String title, boolean approved)
069 throws Exception {
070
071 ServiceContext serviceContext =
072 ServiceContextTestUtil.getServiceContext(group.getGroupId());
073
074 serviceContext.setCommand(Constants.ADD);
075 serviceContext.setLayoutFullURL("http:
076
077 return addEntry(userId, title, approved, serviceContext);
078 }
079
080 public static BlogsEntry addEntry(
081 long userId, Group group, String title, boolean approved,
082 boolean smallImage)
083 throws Exception {
084
085 ServiceContext serviceContext =
086 ServiceContextTestUtil.getServiceContext(group.getGroupId());
087
088 serviceContext.setCommand(Constants.ADD);
089 serviceContext.setLayoutFullURL("http:
090
091 return addEntry(userId, title, approved, smallImage, serviceContext);
092 }
093
094 public static BlogsEntry addEntry(
095 long userId, long groupId, String title, boolean approved)
096 throws Exception {
097
098 Group group = GroupLocalServiceUtil.getGroup(groupId);
099
100 return addEntry(userId, group, title, approved);
101 }
102
103 public static BlogsEntry addEntry(
104 long userId, long groupId, String title, boolean approved,
105 boolean smallImage)
106 throws Exception {
107
108 Group group = GroupLocalServiceUtil.getGroup(groupId);
109
110 return addEntry(userId, group, title, approved, smallImage);
111 }
112
113 public static BlogsEntry addEntry(
114 long userId, String title, boolean approved, boolean smallImage,
115 ServiceContext serviceContext)
116 throws Exception {
117
118 boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
119
120 try {
121 WorkflowThreadLocal.setEnabled(true);
122
123 String subtitle = StringPool.BLANK;
124 String description = "Description";
125 String content = "Content";
126 int displayDateMonth = 1;
127 int displayDateDay = 1;
128 int displayDateYear = 2012;
129 int displayDateHour = 12;
130 int displayDateMinute = 0;
131 boolean allowPingbacks = true;
132 boolean allowTrackbacks = true;
133 String[] trackbacks = new String[0];
134
135 ImageSelector coverImageSelector = null;
136 ImageSelector smallImageSelector = null;
137
138 if (smallImage) {
139 Class<?> clazz = BlogsTestUtil.class;
140
141 ClassLoader classLoader = clazz.getClassLoader();
142
143 InputStream inputStream = classLoader.getResourceAsStream(
144 "com/liferay/portal/util/dependencies/test.jpg");
145
146 FileEntry fileEntry = null;
147
148 try {
149 fileEntry = TempFileEntryUtil.getTempFileEntry(
150 serviceContext.getScopeGroupId(), userId,
151 BlogsEntry.class.getName(), "image.jpg");
152 }
153 catch (Exception e) {
154 fileEntry = TempFileEntryUtil.addTempFileEntry(
155 serviceContext.getScopeGroupId(), userId,
156 BlogsEntry.class.getName(), "image.jpg", inputStream,
157 MimeTypesUtil.getContentType("image.jpg"));
158 }
159
160 smallImageSelector = new ImageSelector(
161 fileEntry.getFileEntryId(), StringPool.BLANK, null);
162 }
163
164 serviceContext = (ServiceContext)serviceContext.clone();
165
166 serviceContext.setWorkflowAction(
167 WorkflowConstants.ACTION_SAVE_DRAFT);
168
169 BlogsEntry entry = BlogsEntryLocalServiceUtil.addEntry(
170 userId, title, subtitle, description, content, displayDateMonth,
171 displayDateDay, displayDateYear, displayDateHour,
172 displayDateMinute, allowPingbacks, allowTrackbacks, trackbacks,
173 coverImageSelector, smallImageSelector, serviceContext);
174
175 if (approved) {
176 return updateStatus(entry, serviceContext);
177 }
178
179 return entry;
180 }
181 finally {
182 WorkflowThreadLocal.setEnabled(workflowEnabled);
183 }
184 }
185
186 public static BlogsEntry addEntry(
187 long userId, String title, boolean approved,
188 ServiceContext serviceContext)
189 throws Exception {
190
191 return addEntry(userId, title, approved, false, serviceContext);
192 }
193
194 public static void assertEquals(
195 BlogsEntry expectedEntry, BlogsEntry actualEntry) {
196
197 Assert.assertEquals(expectedEntry.getUserId(), actualEntry.getUserId());
198 Assert.assertEquals(expectedEntry.getTitle(), actualEntry.getTitle());
199 Assert.assertEquals(
200 expectedEntry.getDescription(), actualEntry.getDescription());
201 Assert.assertEquals(
202 expectedEntry.getContent(), actualEntry.getContent());
203 Assert.assertEquals(
204 expectedEntry.getDisplayDate(), actualEntry.getDisplayDate());
205 Assert.assertEquals(
206 expectedEntry.isAllowPingbacks(), actualEntry.isAllowPingbacks());
207 Assert.assertEquals(
208 expectedEntry.isAllowTrackbacks(), actualEntry.isAllowTrackbacks());
209 Assert.assertEquals(
210 expectedEntry.isSmallImage(), actualEntry.isSmallImage());
211 }
212
213 public static BlogsEntry updateEntry(BlogsEntry entry, boolean approved)
214 throws Exception {
215
216 return updateEntry(entry, RandomTestUtil.randomString(), approved);
217 }
218
219 public static BlogsEntry updateEntry(
220 BlogsEntry entry, String title, boolean approved)
221 throws Exception {
222
223 ServiceContext serviceContext =
224 ServiceContextTestUtil.getServiceContext(entry.getGroupId());
225
226 return updateEntry(entry, title, approved, serviceContext);
227 }
228
229 public static BlogsEntry updateEntry(
230 BlogsEntry entry, String title, boolean approved,
231 ServiceContext serviceContext)
232 throws Exception {
233
234 boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
235
236 try {
237 WorkflowThreadLocal.setEnabled(true);
238
239 serviceContext = (ServiceContext)serviceContext.clone();
240
241 serviceContext.setCommand(Constants.UPDATE);
242 serviceContext.setLayoutFullURL("http:
243 serviceContext.setWorkflowAction(
244 WorkflowConstants.ACTION_SAVE_DRAFT);
245
246 entry = BlogsEntryLocalServiceUtil.updateEntry(
247 entry.getUserId(), entry.getEntryId(), title,
248 entry.getSubtitle(), entry.getDescription(), entry.getContent(),
249 1, 1, 2012, 12, 00, true, true, new String[0], null, null,
250 serviceContext);
251
252 if (approved) {
253 return updateStatus(entry, serviceContext);
254 }
255
256 return entry;
257 }
258 finally {
259 WorkflowThreadLocal.setEnabled(workflowEnabled);
260 }
261 }
262
263 protected static BlogsEntry updateStatus(
264 BlogsEntry entry, ServiceContext serviceContext)
265 throws Exception {
266
267 Map<String, Serializable> workflowContext =
268 new HashMap<String, Serializable>();
269
270 workflowContext.put(WorkflowConstants.CONTEXT_URL, "http:
271 workflowContext.put(
272 WorkflowConstants.CONTEXT_USER_PORTRAIT_URL, "http:
273 workflowContext.put(
274 WorkflowConstants.CONTEXT_USER_URL, "http:
275
276 return BlogsEntryLocalServiceUtil.updateStatus(
277 entry.getUserId(), entry.getEntryId(),
278 WorkflowConstants.STATUS_APPROVED, serviceContext, workflowContext);
279 }
280
281 }