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 imageSelector = null;
136
137 if (smallImage) {
138 Class<?> clazz = BlogsTestUtil.class;
139
140 ClassLoader classLoader = clazz.getClassLoader();
141
142 InputStream inputStream = classLoader.getResourceAsStream(
143 "com/liferay/portal/util/dependencies/test.jpg");
144
145 FileEntry fileEntry = null;
146
147 try {
148 fileEntry = TempFileEntryUtil.getTempFileEntry(
149 serviceContext.getScopeGroupId(), userId,
150 BlogsEntry.class.getName(), "image.jpg");
151 }
152 catch (Exception e) {
153 fileEntry = TempFileEntryUtil.addTempFileEntry(
154 serviceContext.getScopeGroupId(), userId,
155 BlogsEntry.class.getName(), "image.jpg", inputStream,
156 MimeTypesUtil.getContentType("image.jpg"));
157 }
158
159 imageSelector = new ImageSelector(
160 fileEntry.getFileEntryId(), StringPool.BLANK);
161 }
162
163 serviceContext = (ServiceContext)serviceContext.clone();
164
165 serviceContext.setWorkflowAction(
166 WorkflowConstants.ACTION_SAVE_DRAFT);
167
168 BlogsEntry entry = BlogsEntryLocalServiceUtil.addEntry(
169 userId, title, subtitle, description, content, displayDateMonth,
170 displayDateDay, displayDateYear, displayDateHour,
171 displayDateMinute, allowPingbacks, allowTrackbacks, trackbacks,
172 imageSelector, serviceContext);
173
174 if (approved) {
175 return updateStatus(entry, serviceContext);
176 }
177
178 return entry;
179 }
180 finally {
181 WorkflowThreadLocal.setEnabled(workflowEnabled);
182 }
183 }
184
185 public static BlogsEntry addEntry(
186 long userId, String title, boolean approved,
187 ServiceContext serviceContext)
188 throws Exception {
189
190 return addEntry(userId, title, approved, false, serviceContext);
191 }
192
193 public static void assertEquals(
194 BlogsEntry expectedEntry, BlogsEntry actualEntry) {
195
196 Assert.assertEquals(expectedEntry.getUserId(), actualEntry.getUserId());
197 Assert.assertEquals(expectedEntry.getTitle(), actualEntry.getTitle());
198 Assert.assertEquals(
199 expectedEntry.getDescription(), actualEntry.getDescription());
200 Assert.assertEquals(
201 expectedEntry.getContent(), actualEntry.getContent());
202 Assert.assertEquals(
203 expectedEntry.getDisplayDate(), actualEntry.getDisplayDate());
204 Assert.assertEquals(
205 expectedEntry.isAllowPingbacks(), actualEntry.isAllowPingbacks());
206 Assert.assertEquals(
207 expectedEntry.isAllowTrackbacks(), actualEntry.isAllowTrackbacks());
208 Assert.assertEquals(
209 expectedEntry.isSmallImage(), actualEntry.isSmallImage());
210 }
211
212 public static BlogsEntry updateEntry(BlogsEntry entry, boolean approved)
213 throws Exception {
214
215 return updateEntry(entry, RandomTestUtil.randomString(), approved);
216 }
217
218 public static BlogsEntry updateEntry(
219 BlogsEntry entry, String title, boolean approved)
220 throws Exception {
221
222 ServiceContext serviceContext =
223 ServiceContextTestUtil.getServiceContext(entry.getGroupId());
224
225 return updateEntry(entry, title, approved, serviceContext);
226 }
227
228 public static BlogsEntry updateEntry(
229 BlogsEntry entry, String title, boolean approved,
230 ServiceContext serviceContext)
231 throws Exception {
232
233 boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
234
235 try {
236 WorkflowThreadLocal.setEnabled(true);
237
238 serviceContext = (ServiceContext)serviceContext.clone();
239
240 serviceContext.setCommand(Constants.UPDATE);
241 serviceContext.setLayoutFullURL("http:
242 serviceContext.setWorkflowAction(
243 WorkflowConstants.ACTION_SAVE_DRAFT);
244
245 entry = BlogsEntryLocalServiceUtil.updateEntry(
246 entry.getUserId(), entry.getEntryId(), title,
247 entry.getSubtitle(), entry.getDescription(), entry.getContent(),
248 1, 1, 2012, 12, 00, true, true, new String[0], null,
249 serviceContext);
250
251 if (approved) {
252 return updateStatus(entry, serviceContext);
253 }
254
255 return entry;
256 }
257 finally {
258 WorkflowThreadLocal.setEnabled(workflowEnabled);
259 }
260 }
261
262 protected static BlogsEntry updateStatus(
263 BlogsEntry entry, ServiceContext serviceContext)
264 throws Exception {
265
266 Map<String, Serializable> workflowContext =
267 new HashMap<String, Serializable>();
268
269 workflowContext.put(WorkflowConstants.CONTEXT_URL, "http:
270 workflowContext.put(
271 WorkflowConstants.CONTEXT_USER_PORTRAIT_URL, "http:
272 workflowContext.put(
273 WorkflowConstants.CONTEXT_USER_URL, "http:
274
275 return BlogsEntryLocalServiceUtil.updateStatus(
276 entry.getUserId(), entry.getEntryId(),
277 WorkflowConstants.STATUS_APPROVED, serviceContext, workflowContext);
278 }
279
280 }