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.portlet.notifications.test;
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.kernel.test.rule.DeleteAfterTestRun;
021    import com.liferay.portal.kernel.test.util.GroupTestUtil;
022    import com.liferay.portal.kernel.test.util.UserTestUtil;
023    import com.liferay.portal.model.BaseModel;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.model.User;
026    import com.liferay.portal.model.UserNotificationDelivery;
027    import com.liferay.portal.model.UserNotificationDeliveryConstants;
028    import com.liferay.portal.model.UserNotificationEvent;
029    import com.liferay.portal.service.UserNotificationDeliveryLocalServiceUtil;
030    import com.liferay.portal.service.UserNotificationEventLocalServiceUtil;
031    import com.liferay.portal.util.test.MailServiceTestUtil;
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            }
296    
297            protected void deleteUserNotificationDeliveries() throws Exception {
298                    UserNotificationDeliveryLocalServiceUtil.
299                            deleteUserNotificationDeliveries(user.getUserId());
300            }
301    
302            protected void deleteUserNotificationEvents(long userId) throws Exception {
303                    List<UserNotificationEvent> userNotificationEvents =
304                            UserNotificationEventLocalServiceUtil.getUserNotificationEvents(
305                                    userId);
306    
307                    for (UserNotificationEvent userNotificationEvent :
308                                    userNotificationEvents) {
309    
310                            UserNotificationEventLocalServiceUtil.deleteUserNotificationEvent(
311                                    userNotificationEvent);
312                    }
313            }
314    
315            protected abstract String getPortletId();
316    
317            protected List<UserNotificationDelivery> getUserNotificationDeliveries(
318                            long userId)
319                    throws Exception {
320    
321                    List<UserNotificationDelivery> userNotificationDeliveries =
322                            new ArrayList<>();
323    
324                    userNotificationDeliveries.add(
325                            UserNotificationDeliveryLocalServiceUtil.
326                                    getUserNotificationDelivery(
327                                            userId, getPortletId(), 0,
328                                            UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY,
329                                            UserNotificationDeliveryConstants.TYPE_EMAIL, true));
330                    userNotificationDeliveries.add(
331                            UserNotificationDeliveryLocalServiceUtil.
332                                    getUserNotificationDelivery(
333                                            userId, getPortletId(), 0,
334                                            UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY,
335                                            UserNotificationDeliveryConstants.TYPE_WEBSITE, true));
336                    userNotificationDeliveries.add(
337                            UserNotificationDeliveryLocalServiceUtil.
338                                    getUserNotificationDelivery(
339                                            userId, getPortletId(), 0,
340                                            UserNotificationDefinition.NOTIFICATION_TYPE_UPDATE_ENTRY,
341                                            UserNotificationDeliveryConstants.TYPE_EMAIL, true));
342                    userNotificationDeliveries.add(
343                            UserNotificationDeliveryLocalServiceUtil.
344                                    getUserNotificationDelivery(
345                                            userId, getPortletId(), 0,
346                                            UserNotificationDefinition.NOTIFICATION_TYPE_UPDATE_ENTRY,
347                                            UserNotificationDeliveryConstants.TYPE_WEBSITE, true));
348    
349                    return userNotificationDeliveries;
350            }
351    
352            protected List<JSONObject> getUserNotificationEventsJSONObjects(
353                            long userId, long primaryKey)
354                    throws Exception {
355    
356                    List<UserNotificationEvent> userNotificationEvents =
357                            UserNotificationEventLocalServiceUtil.getUserNotificationEvents(
358                                    userId);
359    
360                    List<JSONObject> userNotificationEventJSONObjects = new ArrayList<>(
361                            userNotificationEvents.size());
362    
363                    for (UserNotificationEvent userNotificationEvent :
364                                    userNotificationEvents) {
365    
366                            JSONObject userNotificationEventJSONObject =
367                                    JSONFactoryUtil.createJSONObject(
368                                            userNotificationEvent.getPayload());
369    
370                            userNotificationEventJSONObjects.add(
371                                    userNotificationEventJSONObject);
372                    }
373    
374                    return userNotificationEventJSONObjects;
375            }
376    
377            protected boolean isValidUserNotificationEventObject(
378                            long primaryKey, JSONObject userNotificationEventJSONObject)
379                    throws Exception {
380    
381                    long classPK = userNotificationEventJSONObject.getLong("classPK");
382    
383                    if (classPK != primaryKey) {
384                            return false;
385                    }
386    
387                    return true;
388            }
389    
390            protected abstract void subscribeToContainer() throws Exception;
391    
392            protected abstract BaseModel<?> updateBaseModel(BaseModel<?> baseModel)
393                    throws Exception;
394    
395            protected void updateUserNotificationDelivery(
396                            int notificationType, int deliveryType, boolean deliver)
397                    throws Exception {
398    
399                    boolean exists = false;
400    
401                    for (UserNotificationDelivery userNotificationDelivery :
402                                    userNotificationDeliveries) {
403    
404                            if ((userNotificationDelivery.getNotificationType() !=
405                                            notificationType) ||
406                                    (userNotificationDelivery.getDeliveryType() != deliveryType)) {
407    
408                                    continue;
409                            }
410    
411                            UserNotificationDeliveryLocalServiceUtil.
412                                    updateUserNotificationDelivery(
413                                            userNotificationDelivery.getUserNotificationDeliveryId(),
414                                            deliver);
415    
416                            exists = true;
417    
418                            break;
419                    }
420    
421                    Assert.assertTrue("User notification does not exist", exists);
422            }
423    
424            protected void updateUserNotificationsDelivery(boolean deliver)
425                    throws Exception {
426    
427                    for (UserNotificationDelivery userNotificationDelivery :
428                                    userNotificationDeliveries) {
429    
430                            UserNotificationDeliveryLocalServiceUtil.
431                                    updateUserNotificationDelivery(
432                                            userNotificationDelivery.getUserNotificationDeliveryId(),
433                                            deliver);
434                    }
435            }
436    
437            @DeleteAfterTestRun
438            protected Group group;
439    
440            @DeleteAfterTestRun
441            protected User user;
442    
443            protected List<UserNotificationDelivery> userNotificationDeliveries =
444                    new ArrayList<>();
445    
446    }