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.portal.util;
016    
017    import com.liferay.portal.kernel.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.notifications.UserNotificationDefinition;
020    import com.liferay.portal.model.BaseModel;
021    import com.liferay.portal.model.Group;
022    import com.liferay.portal.model.User;
023    import com.liferay.portal.model.UserNotificationDelivery;
024    import com.liferay.portal.model.UserNotificationDeliveryConstants;
025    import com.liferay.portal.model.UserNotificationEvent;
026    import com.liferay.portal.service.UserNotificationDeliveryLocalServiceUtil;
027    import com.liferay.portal.service.UserNotificationEventLocalServiceUtil;
028    import com.liferay.portal.test.DeleteAfterTestRun;
029    import com.liferay.portal.util.test.GroupTestUtil;
030    import com.liferay.portal.util.test.MailServiceTestUtil;
031    import com.liferay.portal.util.test.UserTestUtil;
032    
033    import java.util.ArrayList;
034    import java.util.List;
035    
036    import org.junit.After;
037    import org.junit.Assert;
038    import org.junit.Before;
039    import org.junit.Test;
040    
041    /**
042     * @author Roberto D??az
043     * @author Sergio Gonz??lez
044     */
045    public abstract class BaseUserNotificationTestCase {
046    
047            @Before
048            public void setUp() throws Exception {
049                    user = UserTestUtil.addOmniAdminUser();
050    
051                    group = GroupTestUtil.addGroup();
052    
053                    addContainerModel();
054    
055                    userNotificationDeliveries = getUserNotificationDeliveries(
056                            user.getUserId());
057            }
058    
059            @After
060            public void tearDown() throws Exception {
061                    deleteUserNotificationEvents(user.getUserId());
062    
063                    deleteUserNotificationDeliveries();
064            }
065    
066            @Test
067            public void testAddUserNotification() throws Exception {
068                    subscribeToContainer();
069    
070                    BaseModel<?> baseModel = addBaseModel();
071    
072                    Assert.assertEquals(1, MailServiceTestUtil.getInboxSize());
073    
074                    List<JSONObject> userNotificationEventsJSONObjects =
075                            getUserNotificationEventsJSONObjects(
076                                    user.getUserId(), (Long)baseModel.getPrimaryKeyObj());
077    
078                    Assert.assertEquals(1, userNotificationEventsJSONObjects.size());
079    
080                    for (JSONObject userNotificationEventsJSONObject :
081                                    userNotificationEventsJSONObjects) {
082    
083                            Assert.assertEquals(
084                                    UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY,
085                                    userNotificationEventsJSONObject.getInt("notificationType"));
086                    }
087            }
088    
089            @Test
090            public void testAddUserNotificationWhenEmailNotificationsDisabled()
091                    throws Exception {
092    
093                    subscribeToContainer();
094    
095                    updateUserNotificationDelivery(
096                            UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY,
097                            UserNotificationDeliveryConstants.TYPE_EMAIL, false);
098    
099                    BaseModel<?> baseModel = addBaseModel();
100    
101                    Assert.assertEquals(0, MailServiceTestUtil.getInboxSize());
102    
103                    List<JSONObject> userNotificationEventsJSONObjects =
104                            getUserNotificationEventsJSONObjects(
105                                    user.getUserId(), (Long)baseModel.getPrimaryKeyObj());
106    
107                    Assert.assertEquals(1, userNotificationEventsJSONObjects.size());
108    
109                    for (JSONObject userNotificationEventsJSONObject :
110                                    userNotificationEventsJSONObjects) {
111    
112                            Assert.assertEquals(
113                                    UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY,
114                                    userNotificationEventsJSONObject.getInt("notificationType"));
115                    }
116            }
117    
118            @Test
119            public void testAddUserNotificationWhenNotificationsDisabled()
120                    throws Exception {
121    
122                    subscribeToContainer();
123    
124                    updateUserNotificationsDelivery(false);
125    
126                    BaseModel<?> baseModel = addBaseModel();
127    
128                    Assert.assertEquals(0, MailServiceTestUtil.getInboxSize());
129    
130                    List<JSONObject> userNotificationEventsJSONObjects =
131                            getUserNotificationEventsJSONObjects(
132                                    user.getUserId(), (Long)baseModel.getPrimaryKeyObj());
133    
134                    Assert.assertEquals(0, userNotificationEventsJSONObjects.size());
135            }
136    
137            @Test
138            public void testAddUserNotificationWhenWebsiteNotificationsDisabled()
139                    throws Exception {
140    
141                    subscribeToContainer();
142    
143                    updateUserNotificationDelivery(
144                            UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY,
145                            UserNotificationDeliveryConstants.TYPE_WEBSITE, false);
146    
147                    BaseModel<?> baseModel = addBaseModel();
148    
149                    Assert.assertEquals(1, MailServiceTestUtil.getInboxSize());
150    
151                    List<JSONObject> userNotificationEventsJSONObjects =
152                            getUserNotificationEventsJSONObjects(
153                                    user.getUserId(), (Long)baseModel.getPrimaryKeyObj());
154    
155                    Assert.assertEquals(0, userNotificationEventsJSONObjects.size());
156            }
157    
158            @Test
159            public void testUpdateUserNotification() throws Exception {
160                    BaseModel<?> baseModel = addBaseModel();
161    
162                    subscribeToContainer();
163    
164                    BaseModel<?> updatedBasemodel = updateBaseModel(baseModel);
165    
166                    Assert.assertEquals(1, MailServiceTestUtil.getInboxSize());
167    
168                    List<JSONObject> userNotificationEventsJSONObjects =
169                            getUserNotificationEventsJSONObjects(
170                                    user.getUserId(), (Long)updatedBasemodel.getPrimaryKeyObj());
171    
172                    Assert.assertEquals(1, userNotificationEventsJSONObjects.size());
173    
174                    int notificationType = -1;
175    
176                    for (JSONObject userNotificationEventsJSONObject :
177                                    userNotificationEventsJSONObjects) {
178    
179                            notificationType = userNotificationEventsJSONObject.getInt(
180                                    "notificationType");
181                    }
182    
183                    Assert.assertEquals(
184                            notificationType,
185                            UserNotificationDefinition.NOTIFICATION_TYPE_UPDATE_ENTRY);
186            }
187    
188            @Test
189            public void testUpdateUserNotificationWhenEmailNotificationsDisabled()
190                    throws Exception {
191    
192                    updateUserNotificationDelivery(
193                            UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY,
194                            UserNotificationDeliveryConstants.TYPE_EMAIL, false);
195                    updateUserNotificationDelivery(
196                            UserNotificationDefinition.NOTIFICATION_TYPE_UPDATE_ENTRY,
197                            UserNotificationDeliveryConstants.TYPE_EMAIL, false);
198    
199                    BaseModel<?> baseModel = addBaseModel();
200    
201                    subscribeToContainer();
202    
203                    BaseModel<?> updatedBasemodel = updateBaseModel(baseModel);
204    
205                    Assert.assertEquals(0, MailServiceTestUtil.getInboxSize());
206    
207                    List<JSONObject> userNotificationEventsJSONObjects =
208                            getUserNotificationEventsJSONObjects(
209                                    user.getUserId(), (Long)updatedBasemodel.getPrimaryKeyObj());
210    
211                    Assert.assertEquals(1, userNotificationEventsJSONObjects.size());
212    
213                    int notificationType = -1;
214    
215                    for (JSONObject userNotificationEventsJSONObject :
216                                    userNotificationEventsJSONObjects) {
217    
218                            notificationType = userNotificationEventsJSONObject.getInt(
219                                    "notificationType");
220                    }
221    
222                    Assert.assertEquals(
223                            notificationType,
224                            UserNotificationDefinition.NOTIFICATION_TYPE_UPDATE_ENTRY);
225            }
226    
227            @Test
228            public void testUpdateUserNotificationWhenNotificationsDisabled()
229                    throws Exception {
230    
231                    updateUserNotificationsDelivery(false);
232    
233                    BaseModel<?> baseModel = addBaseModel();
234    
235                    subscribeToContainer();
236    
237                    BaseModel<?> updatedBasemodel = updateBaseModel(baseModel);
238    
239                    Assert.assertEquals(0, MailServiceTestUtil.getInboxSize());
240    
241                    List<JSONObject> userNotificationEventsJSONObjects =
242                            getUserNotificationEventsJSONObjects(
243                                    user.getUserId(), (Long)updatedBasemodel.getPrimaryKeyObj());
244    
245                    Assert.assertEquals(0, userNotificationEventsJSONObjects.size());
246            }
247    
248            @Test
249            public void testUpdateUserNotificationWhenWebsiteNotificationsDisabled()
250                    throws Exception {
251    
252                    updateUserNotificationDelivery(
253                            UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY,
254                            UserNotificationDeliveryConstants.TYPE_WEBSITE, false);
255                    updateUserNotificationDelivery(
256                            UserNotificationDefinition.NOTIFICATION_TYPE_UPDATE_ENTRY,
257                            UserNotificationDeliveryConstants.TYPE_WEBSITE, false);
258    
259                    BaseModel<?> baseModel = addBaseModel();
260    
261                    subscribeToContainer();
262    
263                    BaseModel<?> updatedBasemodel = updateBaseModel(baseModel);
264    
265                    Assert.assertEquals(1, MailServiceTestUtil.getInboxSize());
266    
267                    List<JSONObject> userNotificationEventsJSONObjects =
268                            getUserNotificationEventsJSONObjects(
269                                    user.getUserId(), (Long)updatedBasemodel.getPrimaryKeyObj());
270    
271                    Assert.assertEquals(0, userNotificationEventsJSONObjects.size());
272            }
273    
274            protected abstract BaseModel<?> addBaseModel() throws Exception;
275    
276            protected void addContainerModel() throws Exception {
277                    return;
278            }
279    
280            protected void deleteUserNotificationDeliveries() throws Exception {
281                    UserNotificationDeliveryLocalServiceUtil.
282                            deleteUserNotificationDeliveries(user.getUserId());
283            }
284    
285            protected void deleteUserNotificationEvents(long userId) throws Exception {
286                    List<UserNotificationEvent> userNotificationEvents =
287                            UserNotificationEventLocalServiceUtil.getUserNotificationEvents(
288                                    userId);
289    
290                    for (UserNotificationEvent userNotificationEvent :
291                                    userNotificationEvents) {
292    
293                            UserNotificationEventLocalServiceUtil.deleteUserNotificationEvent(
294                                    userNotificationEvent);
295                    }
296            }
297    
298            protected abstract String getPortletId();
299    
300            protected List<UserNotificationDelivery> getUserNotificationDeliveries(
301                            long userId)
302                    throws Exception {
303    
304                    List<UserNotificationDelivery> userNotificationDeliveries =
305                            new ArrayList<UserNotificationDelivery>();
306    
307                    userNotificationDeliveries.add(
308                            UserNotificationDeliveryLocalServiceUtil.
309                                    getUserNotificationDelivery(
310                                            userId, getPortletId(), 0,
311                                            UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY,
312                                            UserNotificationDeliveryConstants.TYPE_EMAIL, true));
313                    userNotificationDeliveries.add(
314                            UserNotificationDeliveryLocalServiceUtil.
315                                    getUserNotificationDelivery(
316                                            userId, getPortletId(), 0,
317                                            UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY,
318                                            UserNotificationDeliveryConstants.TYPE_WEBSITE, true));
319                    userNotificationDeliveries.add(
320                            UserNotificationDeliveryLocalServiceUtil.
321                                    getUserNotificationDelivery(
322                                            userId, getPortletId(), 0,
323                                            UserNotificationDefinition.NOTIFICATION_TYPE_UPDATE_ENTRY,
324                                            UserNotificationDeliveryConstants.TYPE_EMAIL, true));
325                    userNotificationDeliveries.add(
326                            UserNotificationDeliveryLocalServiceUtil.
327                                    getUserNotificationDelivery(
328                                            userId, getPortletId(), 0,
329                                            UserNotificationDefinition.NOTIFICATION_TYPE_UPDATE_ENTRY,
330                                            UserNotificationDeliveryConstants.TYPE_WEBSITE, true));
331    
332                    return userNotificationDeliveries;
333            }
334    
335            protected List<JSONObject> getUserNotificationEventsJSONObjects(
336                            long userId, long blogsEntryId)
337                    throws Exception {
338    
339                    List<UserNotificationEvent> userNotificationEvents =
340                            UserNotificationEventLocalServiceUtil.getUserNotificationEvents(
341                                    userId);
342    
343                    List<JSONObject> userNotificationEventJSONObjects =
344                            new ArrayList<JSONObject>(userNotificationEvents.size());
345    
346                    for (UserNotificationEvent userNotificationEvent :
347                                    userNotificationEvents) {
348    
349                            JSONObject userNotificationEventJSONObject =
350                                    JSONFactoryUtil.createJSONObject(
351                                            userNotificationEvent.getPayload());
352    
353                            long classPK = userNotificationEventJSONObject.getLong("classPK");
354    
355                            if (classPK != blogsEntryId) {
356                                    continue;
357                            }
358    
359                            userNotificationEventJSONObjects.add(
360                                    userNotificationEventJSONObject);
361                    }
362    
363                    return userNotificationEventJSONObjects;
364            }
365    
366            protected abstract void subscribeToContainer() throws Exception;
367    
368            protected abstract BaseModel<?> updateBaseModel(BaseModel<?> baseModel)
369                    throws Exception;
370    
371            protected void updateUserNotificationDelivery(
372                            int notificationType, int deliveryType, boolean deliver)
373                    throws Exception {
374    
375                    for (UserNotificationDelivery userNotificationDelivery :
376                                    userNotificationDeliveries) {
377    
378                            if ((userNotificationDelivery.getNotificationType() !=
379                                            notificationType) ||
380                                    (userNotificationDelivery.getDeliveryType() != deliveryType)) {
381    
382                                    continue;
383                            }
384    
385                            UserNotificationDeliveryLocalServiceUtil.
386                                    updateUserNotificationDelivery(
387                                            userNotificationDelivery.getUserNotificationDeliveryId(),
388                                            deliver);
389    
390                            return;
391                    }
392    
393                    Assert.fail("User notification does not exist");
394            }
395    
396            protected void updateUserNotificationsDelivery(boolean deliver)
397                    throws Exception {
398    
399                    for (UserNotificationDelivery userNotificationDelivery :
400                                    userNotificationDeliveries) {
401    
402                            UserNotificationDeliveryLocalServiceUtil.
403                                    updateUserNotificationDelivery(
404                                            userNotificationDelivery.getUserNotificationDeliveryId(),
405                                            deliver);
406                    }
407            }
408    
409            @DeleteAfterTestRun
410            protected Group group;
411    
412            @DeleteAfterTestRun
413            protected User user;
414    
415            protected List<UserNotificationDelivery> userNotificationDeliveries =
416                    new ArrayList<UserNotificationDelivery>();
417    
418    }