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.assertTrue(
084                                    isValidUserNotificationEventObject(
085                                            (Long)baseModel.getPrimaryKeyObj(),
086                                            userNotificationEventsJSONObject));
087                            Assert.assertEquals(
088                                    UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY,
089                                    userNotificationEventsJSONObject.getInt("notificationType"));
090                    }
091            }
092    
093            @Test
094            public void testAddUserNotificationWhenEmailNotificationsDisabled()
095                    throws Exception {
096    
097                    subscribeToContainer();
098    
099                    updateUserNotificationDelivery(
100                            UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY,
101                            UserNotificationDeliveryConstants.TYPE_EMAIL, false);
102    
103                    BaseModel<?> baseModel = addBaseModel();
104    
105                    Assert.assertEquals(0, MailServiceTestUtil.getInboxSize());
106    
107                    List<JSONObject> userNotificationEventsJSONObjects =
108                            getUserNotificationEventsJSONObjects(
109                                    user.getUserId(), (Long)baseModel.getPrimaryKeyObj());
110    
111                    Assert.assertEquals(1, userNotificationEventsJSONObjects.size());
112    
113                    for (JSONObject userNotificationEventsJSONObject :
114                                    userNotificationEventsJSONObjects) {
115    
116                            Assert.assertTrue(
117                                    isValidUserNotificationEventObject(
118                                            (Long)baseModel.getPrimaryKeyObj(),
119                                            userNotificationEventsJSONObject));
120                            Assert.assertEquals(
121                                    UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY,
122                                    userNotificationEventsJSONObject.getInt("notificationType"));
123                    }
124            }
125    
126            @Test
127            public void testAddUserNotificationWhenNotificationsDisabled()
128                    throws Exception {
129    
130                    subscribeToContainer();
131    
132                    updateUserNotificationsDelivery(false);
133    
134                    BaseModel<?> baseModel = addBaseModel();
135    
136                    Assert.assertEquals(0, MailServiceTestUtil.getInboxSize());
137    
138                    List<JSONObject> userNotificationEventsJSONObjects =
139                            getUserNotificationEventsJSONObjects(
140                                    user.getUserId(), (Long)baseModel.getPrimaryKeyObj());
141    
142                    Assert.assertEquals(0, userNotificationEventsJSONObjects.size());
143            }
144    
145            @Test
146            public void testAddUserNotificationWhenWebsiteNotificationsDisabled()
147                    throws Exception {
148    
149                    subscribeToContainer();
150    
151                    updateUserNotificationDelivery(
152                            UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY,
153                            UserNotificationDeliveryConstants.TYPE_WEBSITE, false);
154    
155                    BaseModel<?> baseModel = addBaseModel();
156    
157                    Assert.assertEquals(1, MailServiceTestUtil.getInboxSize());
158    
159                    List<JSONObject> userNotificationEventsJSONObjects =
160                            getUserNotificationEventsJSONObjects(
161                                    user.getUserId(), (Long)baseModel.getPrimaryKeyObj());
162    
163                    Assert.assertEquals(0, userNotificationEventsJSONObjects.size());
164            }
165    
166            @Test
167            public void testUpdateUserNotification() throws Exception {
168                    BaseModel<?> baseModel = addBaseModel();
169    
170                    subscribeToContainer();
171    
172                    BaseModel<?> updatedBasemodel = updateBaseModel(baseModel);
173    
174                    Assert.assertEquals(1, MailServiceTestUtil.getInboxSize());
175    
176                    List<JSONObject> userNotificationEventsJSONObjects =
177                            getUserNotificationEventsJSONObjects(
178                                    user.getUserId(), (Long)updatedBasemodel.getPrimaryKeyObj());
179    
180                    Assert.assertEquals(1, userNotificationEventsJSONObjects.size());
181    
182                    int notificationType = -1;
183    
184                    for (JSONObject userNotificationEventsJSONObject :
185                                    userNotificationEventsJSONObjects) {
186    
187                            Assert.assertTrue(
188                                    isValidUserNotificationEventObject(
189                                            (Long)updatedBasemodel.getPrimaryKeyObj(),
190                                            userNotificationEventsJSONObject));
191    
192                            notificationType = userNotificationEventsJSONObject.getInt(
193                                    "notificationType");
194    
195                            Assert.assertEquals(
196                                    notificationType,
197                                    UserNotificationDefinition.NOTIFICATION_TYPE_UPDATE_ENTRY);
198                    }
199            }
200    
201            @Test
202            public void testUpdateUserNotificationWhenEmailNotificationsDisabled()
203                    throws Exception {
204    
205                    updateUserNotificationDelivery(
206                            UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY,
207                            UserNotificationDeliveryConstants.TYPE_EMAIL, false);
208                    updateUserNotificationDelivery(
209                            UserNotificationDefinition.NOTIFICATION_TYPE_UPDATE_ENTRY,
210                            UserNotificationDeliveryConstants.TYPE_EMAIL, false);
211    
212                    BaseModel<?> baseModel = addBaseModel();
213    
214                    subscribeToContainer();
215    
216                    BaseModel<?> updatedBasemodel = updateBaseModel(baseModel);
217    
218                    Assert.assertEquals(0, MailServiceTestUtil.getInboxSize());
219    
220                    List<JSONObject> userNotificationEventsJSONObjects =
221                            getUserNotificationEventsJSONObjects(
222                                    user.getUserId(), (Long)updatedBasemodel.getPrimaryKeyObj());
223    
224                    Assert.assertEquals(1, userNotificationEventsJSONObjects.size());
225    
226                    int notificationType = -1;
227    
228                    for (JSONObject userNotificationEventsJSONObject :
229                                    userNotificationEventsJSONObjects) {
230    
231                            Assert.assertTrue(
232                                    isValidUserNotificationEventObject(
233                                            (Long)updatedBasemodel.getPrimaryKeyObj(),
234                                            userNotificationEventsJSONObject));
235    
236                            notificationType = userNotificationEventsJSONObject.getInt(
237                                    "notificationType");
238    
239                            Assert.assertEquals(
240                                    notificationType,
241                                    UserNotificationDefinition.NOTIFICATION_TYPE_UPDATE_ENTRY);
242                    }
243            }
244    
245            @Test
246            public void testUpdateUserNotificationWhenNotificationsDisabled()
247                    throws Exception {
248    
249                    updateUserNotificationsDelivery(false);
250    
251                    BaseModel<?> baseModel = addBaseModel();
252    
253                    subscribeToContainer();
254    
255                    BaseModel<?> updatedBasemodel = updateBaseModel(baseModel);
256    
257                    Assert.assertEquals(0, MailServiceTestUtil.getInboxSize());
258    
259                    List<JSONObject> userNotificationEventsJSONObjects =
260                            getUserNotificationEventsJSONObjects(
261                                    user.getUserId(), (Long)updatedBasemodel.getPrimaryKeyObj());
262    
263                    Assert.assertEquals(0, userNotificationEventsJSONObjects.size());
264            }
265    
266            @Test
267            public void testUpdateUserNotificationWhenWebsiteNotificationsDisabled()
268                    throws Exception {
269    
270                    updateUserNotificationDelivery(
271                            UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY,
272                            UserNotificationDeliveryConstants.TYPE_WEBSITE, false);
273                    updateUserNotificationDelivery(
274                            UserNotificationDefinition.NOTIFICATION_TYPE_UPDATE_ENTRY,
275                            UserNotificationDeliveryConstants.TYPE_WEBSITE, false);
276    
277                    BaseModel<?> baseModel = addBaseModel();
278    
279                    subscribeToContainer();
280    
281                    BaseModel<?> updatedBasemodel = updateBaseModel(baseModel);
282    
283                    Assert.assertEquals(1, MailServiceTestUtil.getInboxSize());
284    
285                    List<JSONObject> userNotificationEventsJSONObjects =
286                            getUserNotificationEventsJSONObjects(
287                                    user.getUserId(), (Long)updatedBasemodel.getPrimaryKeyObj());
288    
289                    Assert.assertEquals(0, userNotificationEventsJSONObjects.size());
290            }
291    
292            protected abstract BaseModel<?> addBaseModel() throws Exception;
293    
294            protected void addContainerModel() throws Exception {
295                    return;
296            }
297    
298            protected void deleteUserNotificationDeliveries() throws Exception {
299                    UserNotificationDeliveryLocalServiceUtil.
300                            deleteUserNotificationDeliveries(user.getUserId());
301            }
302    
303            protected void deleteUserNotificationEvents(long userId) throws Exception {
304                    List<UserNotificationEvent> userNotificationEvents =
305                            UserNotificationEventLocalServiceUtil.getUserNotificationEvents(
306                                    userId);
307    
308                    for (UserNotificationEvent userNotificationEvent :
309                                    userNotificationEvents) {
310    
311                            UserNotificationEventLocalServiceUtil.deleteUserNotificationEvent(
312                                    userNotificationEvent);
313                    }
314            }
315    
316            protected abstract String getPortletId();
317    
318            protected List<UserNotificationDelivery> getUserNotificationDeliveries(
319                            long userId)
320                    throws Exception {
321    
322                    List<UserNotificationDelivery> userNotificationDeliveries =
323                            new ArrayList<UserNotificationDelivery>();
324    
325                    userNotificationDeliveries.add(
326                            UserNotificationDeliveryLocalServiceUtil.
327                                    getUserNotificationDelivery(
328                                            userId, getPortletId(), 0,
329                                            UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY,
330                                            UserNotificationDeliveryConstants.TYPE_EMAIL, true));
331                    userNotificationDeliveries.add(
332                            UserNotificationDeliveryLocalServiceUtil.
333                                    getUserNotificationDelivery(
334                                            userId, getPortletId(), 0,
335                                            UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY,
336                                            UserNotificationDeliveryConstants.TYPE_WEBSITE, true));
337                    userNotificationDeliveries.add(
338                            UserNotificationDeliveryLocalServiceUtil.
339                                    getUserNotificationDelivery(
340                                            userId, getPortletId(), 0,
341                                            UserNotificationDefinition.NOTIFICATION_TYPE_UPDATE_ENTRY,
342                                            UserNotificationDeliveryConstants.TYPE_EMAIL, true));
343                    userNotificationDeliveries.add(
344                            UserNotificationDeliveryLocalServiceUtil.
345                                    getUserNotificationDelivery(
346                                            userId, getPortletId(), 0,
347                                            UserNotificationDefinition.NOTIFICATION_TYPE_UPDATE_ENTRY,
348                                            UserNotificationDeliveryConstants.TYPE_WEBSITE, true));
349    
350                    return userNotificationDeliveries;
351            }
352    
353            protected List<JSONObject> getUserNotificationEventsJSONObjects(
354                            long userId, long primaryKey)
355                    throws Exception {
356    
357                    List<UserNotificationEvent> userNotificationEvents =
358                            UserNotificationEventLocalServiceUtil.getUserNotificationEvents(
359                                    userId);
360    
361                    List<JSONObject> userNotificationEventJSONObjects =
362                            new ArrayList<JSONObject>(userNotificationEvents.size());
363    
364                    for (UserNotificationEvent userNotificationEvent :
365                                    userNotificationEvents) {
366    
367                            JSONObject userNotificationEventJSONObject =
368                                    JSONFactoryUtil.createJSONObject(
369                                            userNotificationEvent.getPayload());
370    
371                            userNotificationEventJSONObjects.add(
372                                    userNotificationEventJSONObject);
373                    }
374    
375                    return userNotificationEventJSONObjects;
376            }
377    
378            protected boolean isValidUserNotificationEventObject(
379                            long primaryKey, JSONObject userNotificationEventJSONObject)
380                    throws Exception {
381    
382                    long classPK = userNotificationEventJSONObject.getLong("classPK");
383    
384                    if (classPK != primaryKey) {
385                            return false;
386                    }
387    
388                    return true;
389            }
390    
391            protected abstract void subscribeToContainer() throws Exception;
392    
393            protected abstract BaseModel<?> updateBaseModel(BaseModel<?> baseModel)
394                    throws Exception;
395    
396            protected void updateUserNotificationDelivery(
397                            int notificationType, int deliveryType, boolean deliver)
398                    throws Exception {
399    
400                    for (UserNotificationDelivery userNotificationDelivery :
401                                    userNotificationDeliveries) {
402    
403                            if ((userNotificationDelivery.getNotificationType() !=
404                                            notificationType) ||
405                                    (userNotificationDelivery.getDeliveryType() != deliveryType)) {
406    
407                                    continue;
408                            }
409    
410                            UserNotificationDeliveryLocalServiceUtil.
411                                    updateUserNotificationDelivery(
412                                            userNotificationDelivery.getUserNotificationDeliveryId(),
413                                            deliver);
414    
415                            return;
416                    }
417    
418                    Assert.fail("User notification does not exist");
419            }
420    
421            protected void updateUserNotificationsDelivery(boolean deliver)
422                    throws Exception {
423    
424                    for (UserNotificationDelivery userNotificationDelivery :
425                                    userNotificationDeliveries) {
426    
427                            UserNotificationDeliveryLocalServiceUtil.
428                                    updateUserNotificationDelivery(
429                                            userNotificationDelivery.getUserNotificationDeliveryId(),
430                                            deliver);
431                    }
432            }
433    
434            @DeleteAfterTestRun
435            protected Group group;
436    
437            @DeleteAfterTestRun
438            protected User user;
439    
440            protected List<UserNotificationDelivery> userNotificationDeliveries =
441                    new ArrayList<UserNotificationDelivery>();
442    
443    }