001
014
015 package com.liferay.portlet.messageboards.util.test;
016
017 import com.liferay.portal.kernel.util.Constants;
018 import com.liferay.portal.kernel.util.ObjectValuePair;
019 import com.liferay.portal.kernel.util.StringPool;
020 import com.liferay.portal.kernel.util.Validator;
021 import com.liferay.portal.kernel.workflow.WorkflowConstants;
022 import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
023 import com.liferay.portal.model.User;
024 import com.liferay.portal.service.ServiceContext;
025 import com.liferay.portal.util.test.RandomTestUtil;
026 import com.liferay.portal.util.test.ServiceContextTestUtil;
027 import com.liferay.portal.util.test.TestPropsValues;
028 import com.liferay.portal.util.test.UserTestUtil;
029 import com.liferay.portlet.messageboards.model.MBBan;
030 import com.liferay.portlet.messageboards.model.MBCategory;
031 import com.liferay.portlet.messageboards.model.MBCategoryConstants;
032 import com.liferay.portlet.messageboards.model.MBMessage;
033 import com.liferay.portlet.messageboards.model.MBMessageConstants;
034 import com.liferay.portlet.messageboards.model.MBMessageDisplay;
035 import com.liferay.portlet.messageboards.model.MBThread;
036 import com.liferay.portlet.messageboards.model.MBThreadFlag;
037 import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
038 import com.liferay.portlet.messageboards.service.MBCategoryServiceUtil;
039 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
040 import com.liferay.portlet.messageboards.service.MBThreadFlagLocalServiceUtil;
041
042 import java.io.InputStream;
043 import java.io.Serializable;
044
045 import java.util.ArrayList;
046 import java.util.Collections;
047 import java.util.HashMap;
048 import java.util.List;
049 import java.util.Map;
050
051
055 public class MBTestUtil {
056
057 public static MBBan addBan(long groupId) throws Exception {
058 User user = UserTestUtil.addUser(
059 RandomTestUtil.randomString(), TestPropsValues.getGroupId());
060
061 return addBan(groupId, user.getUserId());
062 }
063
064 public static MBBan addBan(long groupId, long banUserId) throws Exception {
065 return addBan(TestPropsValues.getUserId(), groupId, banUserId);
066 }
067
068 public static MBBan addBan(long userId, long groupId, long banUserId)
069 throws Exception {
070
071 ServiceContext serviceContext =
072 ServiceContextTestUtil.getServiceContext(groupId);
073
074 return MBBanLocalServiceUtil.addBan(userId, banUserId, serviceContext);
075 }
076
077 public static MBCategory addCategory(long groupId) throws Exception {
078 return addCategory(
079 groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
080 }
081
082 public static MBCategory addCategory(long groupId, long parentCategoryId)
083 throws Exception {
084
085 ServiceContext serviceContext =
086 ServiceContextTestUtil.getServiceContext(groupId);
087
088 return addCategory(
089 RandomTestUtil.randomString(), parentCategoryId, serviceContext);
090 }
091
092 public static MBCategory addCategory(ServiceContext serviceContext)
093 throws Exception {
094
095 return MBCategoryServiceUtil.addCategory(
096 TestPropsValues.getUserId(),
097 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID,
098 RandomTestUtil.randomString(), StringPool.BLANK, serviceContext);
099 }
100
101 public static MBCategory addCategory(
102 String name, long parentCategoryId, ServiceContext serviceContext)
103 throws Exception {
104
105 return MBCategoryServiceUtil.addCategory(
106 TestPropsValues.getUserId(), parentCategoryId, name,
107 RandomTestUtil.randomString(), serviceContext);
108 }
109
110 public static MBMessage addDiscussionMessage(
111 long groupId, String className, long classPK)
112 throws Exception {
113
114 return addDiscussionMessage(
115 TestPropsValues.getUser(), groupId, className, classPK);
116 }
117
118 public static MBMessage addDiscussionMessage(
119 User user, long groupId, String className, long classPK)
120 throws Exception {
121
122 MBMessageDisplay messageDisplay =
123 MBMessageLocalServiceUtil.getDiscussionMessageDisplay(
124 user.getUserId(), groupId, className, classPK,
125 WorkflowConstants.STATUS_APPROVED);
126
127 MBThread thread = messageDisplay.getThread();
128
129 ServiceContext serviceContext =
130 ServiceContextTestUtil.getServiceContext(groupId);
131
132 serviceContext.setCommand(Constants.ADD);
133 serviceContext.setLayoutFullURL("http:
134
135 return MBMessageLocalServiceUtil.addDiscussionMessage(
136 user.getUserId(), user.getFullName(), groupId, className, classPK,
137 thread.getThreadId(), thread.getRootMessageId(),
138 RandomTestUtil.randomString(), RandomTestUtil.randomString(50),
139 serviceContext);
140 }
141
142 public static MBMessage addMessage(long groupId) throws Exception {
143 return addMessage(
144 groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
145 }
146
147 public static MBMessage addMessage(long groupId, long categoryId)
148 throws Exception {
149
150 return addMessage(groupId, categoryId, 0, 0);
151 }
152
153 public static MBMessage addMessage(
154 long groupId, long categoryId, boolean approved)
155 throws Exception {
156
157 ServiceContext serviceContext =
158 ServiceContextTestUtil.getServiceContext(groupId);
159
160 serviceContext.setCommand(Constants.ADD);
161 serviceContext.setLayoutFullURL("http:
162
163 return addMessage(
164 groupId, categoryId, StringPool.BLANK, approved, serviceContext);
165 }
166
167 public static MBMessage addMessage(
168 long groupId, long categoryId, long threadId, long parentMessageId)
169 throws Exception {
170
171 long userId = TestPropsValues.getUserId();
172 String userName = RandomTestUtil.randomString();
173 String subject = RandomTestUtil.randomString();
174 String body = RandomTestUtil.randomString();
175 String format = MBMessageConstants.DEFAULT_FORMAT;
176 List<ObjectValuePair<String, InputStream>> inputStreamOVPs =
177 Collections.emptyList();
178 boolean anonymous = false;
179 double priority = 0.0;
180 boolean allowPingbacks = false;
181
182 ServiceContext serviceContext =
183 ServiceContextTestUtil.getServiceContext(groupId);
184
185 return MBMessageLocalServiceUtil.addMessage(
186 userId, userName, groupId, categoryId, threadId, parentMessageId,
187 subject, body, format, inputStreamOVPs, anonymous, priority,
188 allowPingbacks, serviceContext);
189 }
190
191 public static MBMessage addMessage(
192 long groupId, long categoryId, ServiceContext serviceContext)
193 throws Exception {
194
195 return addMessage(
196 groupId, categoryId, StringPool.BLANK, false, serviceContext);
197 }
198
199 public static MBMessage addMessage(
200 long groupId, long categoryId, String keywords, boolean approved,
201 ServiceContext serviceContext)
202 throws Exception {
203
204 String subject = "subject";
205 String body = "body";
206
207 if (!Validator.isBlank(keywords)) {
208 subject = keywords;
209 body = keywords;
210 }
211
212 MBMessage message = MBMessageLocalServiceUtil.addMessage(
213 serviceContext.getUserId(), RandomTestUtil.randomString(), groupId,
214 categoryId, subject, body, serviceContext);
215
216 if (!approved) {
217 message = updateStatus(message, serviceContext);
218 }
219
220 return message;
221 }
222
223 public static MBMessage addMessageWithWorkflow(
224 long groupId, boolean approved)
225 throws Exception {
226
227 return addMessageWithWorkflow(
228 groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID, approved);
229 }
230
231 public static MBMessage addMessageWithWorkflow(
232 long groupId, long categoryId, boolean approved)
233 throws Exception {
234
235 return addMessageWithWorkflowAndAttachments(
236 groupId, categoryId, approved, null);
237 }
238
239 public static MBMessage addMessageWithWorkflowAndAttachments(
240 long groupId, long categoryId, boolean approved,
241 List<ObjectValuePair<String, InputStream>> inputStreamOVPs)
242 throws Exception {
243
244 ServiceContext serviceContext =
245 ServiceContextTestUtil.getServiceContext(groupId);
246
247 return addMessage(
248 groupId, categoryId, true, approved, inputStreamOVPs,
249 serviceContext);
250 }
251
252 public static MBThreadFlag addThreadFlag(long groupId, MBThread thread)
253 throws Exception {
254
255 ServiceContext serviceContext =
256 ServiceContextTestUtil.getServiceContext(groupId);
257
258 MBThreadFlagLocalServiceUtil.addThreadFlag(
259 TestPropsValues.getUserId(), thread, serviceContext);
260
261 return MBThreadFlagLocalServiceUtil.getThreadFlag(
262 TestPropsValues.getUserId(), thread);
263 }
264
265 public static List<ObjectValuePair<String, InputStream>> getInputStreamOVPs(
266 String fileName, Class<?> clazz, String keywords) {
267
268 List<ObjectValuePair<String, InputStream>> inputStreamOVPs =
269 new ArrayList<ObjectValuePair<String, InputStream>>(1);
270
271 InputStream inputStream = clazz.getResourceAsStream(
272 "dependencies/" + fileName);
273
274 ObjectValuePair<String, InputStream> inputStreamOVP = null;
275
276 if (Validator.isBlank(keywords)) {
277 inputStreamOVP = new ObjectValuePair<String, InputStream>(
278 fileName, inputStream);
279 }
280 else {
281 inputStreamOVP = new ObjectValuePair<String, InputStream>(
282 keywords, inputStream);
283 }
284
285 inputStreamOVPs.add(inputStreamOVP);
286
287 return inputStreamOVPs;
288 }
289
290 public static MBMessage updateDiscussionMessage(
291 long userId, long groupId, long messageId, String className,
292 long classPK)
293 throws Exception {
294
295 ServiceContext serviceContext =
296 ServiceContextTestUtil.getServiceContext(groupId);
297
298 serviceContext.setCommand(Constants.UPDATE);
299 serviceContext.setLayoutFullURL("http:
300
301 return MBMessageLocalServiceUtil.updateDiscussionMessage(
302 userId, messageId, className, classPK,
303 RandomTestUtil.randomString(), RandomTestUtil.randomString(50),
304 serviceContext);
305 }
306
307 public static MBMessage updateDiscussionMessage(
308 long groupId, long messageId, String className, long classPK)
309 throws Exception {
310
311 return updateDiscussionMessage(
312 TestPropsValues.getUserId(), groupId, messageId, className,
313 classPK);
314 }
315
316 public static MBMessage updateMessage(MBMessage message, boolean approved)
317 throws Exception {
318
319 return updateMessage(
320 message, RandomTestUtil.randomString(),
321 RandomTestUtil.randomString(50), approved);
322 }
323
324 public static MBMessage updateMessage(
325 MBMessage message, String subject, String body, boolean approved)
326 throws Exception {
327
328 boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
329
330 try {
331 WorkflowThreadLocal.setEnabled(true);
332
333 ServiceContext serviceContext =
334 ServiceContextTestUtil.getServiceContext(message.getGroupId());
335
336 serviceContext.setCommand(Constants.UPDATE);
337 serviceContext.setLayoutFullURL("http:
338 serviceContext.setWorkflowAction(
339 WorkflowConstants.ACTION_SAVE_DRAFT);
340
341 message = MBMessageLocalServiceUtil.updateMessage(
342 TestPropsValues.getUserId(), message.getMessageId(), subject,
343 body,
344 Collections.<ObjectValuePair<String, InputStream>>emptyList(),
345 Collections.<String>emptyList(), message.getPriority(),
346 message.isAllowPingbacks(), serviceContext);
347
348 if (approved) {
349 message = updateStatus(message, serviceContext);
350 }
351
352 return message;
353 }
354 finally {
355 WorkflowThreadLocal.setEnabled(workflowEnabled);
356 }
357 }
358
359 protected static MBMessage addMessage(
360 long groupId, long categoryId, boolean workflowEnabled,
361 boolean approved,
362 List<ObjectValuePair<String, InputStream>> inputStreamOVPs,
363 ServiceContext serviceContext)
364 throws Exception {
365
366 long userId = TestPropsValues.getUserId();
367 String userName = RandomTestUtil.randomString();
368 long threadId = 0;
369 long parentMessageId = 0;
370 String subject = RandomTestUtil.randomString();
371 String body = RandomTestUtil.randomString();
372 String format = MBMessageConstants.DEFAULT_FORMAT;
373 boolean anonymous = false;
374 double priority = 0.0;
375 boolean allowPingbacks = false;
376
377 if (inputStreamOVPs == null) {
378 inputStreamOVPs = Collections.emptyList();
379 }
380
381 if (workflowEnabled) {
382 if (approved) {
383 serviceContext.setWorkflowAction(
384 WorkflowConstants.ACTION_PUBLISH);
385 }
386 else {
387 serviceContext.setWorkflowAction(
388 WorkflowConstants.ACTION_SAVE_DRAFT);
389 }
390 }
391
392 MBMessage message = MBMessageLocalServiceUtil.addMessage(
393 userId, userName, groupId, categoryId, threadId, parentMessageId,
394 subject, body, format, inputStreamOVPs, anonymous, priority,
395 allowPingbacks, serviceContext);
396
397 return MBMessageLocalServiceUtil.getMessage(message.getMessageId());
398 }
399
400 protected static MBMessage updateStatus(
401 MBMessage message, ServiceContext serviceContext)
402 throws Exception {
403
404 Map<String, Serializable> workflowContext =
405 new HashMap<String, Serializable>();
406
407 workflowContext.put(WorkflowConstants.CONTEXT_URL, "http:
408
409 message = MBMessageLocalServiceUtil.updateStatus(
410 message.getUserId(), message.getMessageId(),
411 WorkflowConstants.STATUS_APPROVED, serviceContext, workflowContext);
412
413 return message;
414 }
415
416 }