diff --git a/src/main/java/de/bps/course/nodes/den/DENCourseNodeNotificationHandler.java b/src/main/java/de/bps/course/nodes/den/DENCourseNodeNotificationHandler.java index 828c452754eb0710aaa9d1dcb4c9833640730236..3d712530dd6974b65949471352f8446b52f29000 100644 --- a/src/main/java/de/bps/course/nodes/den/DENCourseNodeNotificationHandler.java +++ b/src/main/java/de/bps/course/nodes/den/DENCourseNodeNotificationHandler.java @@ -69,7 +69,7 @@ public class DENCourseNodeNotificationHandler implements NotificationsHandler { final Translator trans = Util.createPackageTranslator(DENCourseNodeNotificationHandler.class, locale); String cssClass = new DENCourseNodeConfiguration().getIconCSSClass(); - si = new SubscriptionInfo(new TitleItem(trans.translate("notifications.header", new String[]{course.getCourseTitle()}), cssClass), null); + si = new SubscriptionInfo(subscriber.getKey(), p.getType(), new TitleItem(trans.translate("notifications.header", new String[]{course.getCourseTitle()}), cssClass), null); SubscriptionListItem subListItem; for (DENCourseNode denNode : denNodes) { diff --git a/src/main/java/org/olat/commons/calendar/CalendarNotificationHandler.java b/src/main/java/org/olat/commons/calendar/CalendarNotificationHandler.java index a326eb5e3ffd4ce52f4e35c52a37d6c8ed8389e3..68b7ce1bf677d5a0112b623bd39378a5906ad705 100644 --- a/src/main/java/org/olat/commons/calendar/CalendarNotificationHandler.java +++ b/src/main/java/org/olat/commons/calendar/CalendarNotificationHandler.java @@ -96,7 +96,7 @@ public class CalendarNotificationHandler extends LogDelegator implements Notific if (calType != null) { Formatter form = Formatter.getInstance(locale); - si = new SubscriptionInfo(new TitleItem(title, CSS_CLASS_CALENDAR_ICON), null); + si = new SubscriptionInfo(subscriber.getKey(), p.getType(), new TitleItem(title, CSS_CLASS_CALENDAR_ICON), null); String bPath; if(StringHelper.containsNonWhitespace(p.getBusinessPath())) { diff --git a/src/main/java/org/olat/commons/info/notification/InfoMessageNotificationHandler.java b/src/main/java/org/olat/commons/info/notification/InfoMessageNotificationHandler.java index 2a1119254e58634933a3cae17e570d6b545bb0cc..4818dde78e91631bf08b6f0824d28836eab53347 100644 --- a/src/main/java/org/olat/commons/info/notification/InfoMessageNotificationHandler.java +++ b/src/main/java/org/olat/commons/info/notification/InfoMessageNotificationHandler.java @@ -70,7 +70,7 @@ public class InfoMessageNotificationHandler extends LogDelegator implements Noti String resSubPath = subscriber.getPublisher().getSubidentifier(); String businessPath = subscriber.getPublisher().getBusinessPath(); String title = RepositoryManager.getInstance().lookupDisplayNameByOLATResourceableId(resId); - si = new SubscriptionInfo(new TitleItem(title, CSS_CLASS_ICON), null); + si = new SubscriptionInfo(subscriber.getKey(), p.getType(), new TitleItem(title, CSS_CLASS_ICON), null); OLATResourceable ores = new OLATResourceable() { @Override diff --git a/src/main/java/org/olat/core/util/notifications/NotificationHelper.java b/src/main/java/org/olat/core/util/notifications/NotificationHelper.java index e7fb956b31ba7abd7a763598b7f0490fffb44686..b21b11a4d277230f164159babe8cff1ab0e23745 100644 --- a/src/main/java/org/olat/core/util/notifications/NotificationHelper.java +++ b/src/main/java/org/olat/core/util/notifications/NotificationHelper.java @@ -19,6 +19,7 @@ */ package org.olat.core.util.notifications; +import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.Iterator; @@ -55,10 +56,18 @@ public class NotificationHelper { private static final OLog log = Tracing.createLoggerFor(NotificationHelper.class); public static Map<Subscriber, SubscriptionInfo> getSubscriptionMap(Identity identity, Locale locale, boolean showWithNewsOnly, Date compareDate) { + return getSubscriptionMap(identity, locale, showWithNewsOnly, compareDate, Collections.<String>emptyList()); + } + + public static Map<Subscriber, SubscriptionInfo> getSubscriptionMap(Identity identity, Locale locale, boolean showWithNewsOnly, Date compareDate, List<String> types) { + NotificationsManager man = NotificationsManager.getInstance(); + List<Subscriber> subs = man.getSubscribers(identity, types); + return getSubscriptionMap(locale, showWithNewsOnly, compareDate, subs); + } + + public static Map<Subscriber, SubscriptionInfo> getSubscriptionMap(Locale locale, boolean showWithNewsOnly, Date compareDate, List<Subscriber> subs) { NotificationsManager man = NotificationsManager.getInstance(); - List<Subscriber> subs = man.getSubscribers(identity); Map<Subscriber, SubscriptionInfo> subToSubInfo = new HashMap<Subscriber, SubscriptionInfo>(); - // calc subscriptioninfo for all subscriptions and, if only those with news // are to be shown, remove the other ones for (Iterator<Subscriber> it_subs = subs.iterator(); it_subs.hasNext();) { diff --git a/src/main/java/org/olat/core/util/notifications/NotificationsManager.java b/src/main/java/org/olat/core/util/notifications/NotificationsManager.java index a48d18732da01711cbfa31552fcafc1c529590cf..377a7b624aab911b66bb451b659683ce4937b57d 100644 --- a/src/main/java/org/olat/core/util/notifications/NotificationsManager.java +++ b/src/main/java/org/olat/core/util/notifications/NotificationsManager.java @@ -250,6 +250,15 @@ public abstract class NotificationsManager extends BasicManager { */ public abstract List<Subscriber> getSubscribers(Identity identity); + /** + * subscribers for ONE person (e.g. subscribed to 5 forums -> 5 subscribers + * belonging to this person) restricted to the list of specified types + * @param identity + * @param types + * @return + */ + public abstract List<Subscriber> getSubscribers(Identity identity, List<String> types); + /** * @param identity * @return a list of all subscribers which belong to the identity and which diff --git a/src/main/java/org/olat/core/util/notifications/SubscriptionInfo.java b/src/main/java/org/olat/core/util/notifications/SubscriptionInfo.java index be60f61fe98ce327f31d1afca67ccb6e77108b2e..69a4fc149879eed6a8364bc58ba1d75d68d5c42a 100644 --- a/src/main/java/org/olat/core/util/notifications/SubscriptionInfo.java +++ b/src/main/java/org/olat/core/util/notifications/SubscriptionInfo.java @@ -43,11 +43,16 @@ public class SubscriptionInfo { public static final String MIME_HTML = "text/html"; public static final String MIME_PLAIN = "text/plain"; - private TitleItem title; - private List<SubscriptionListItem> subsList; + private final Long key; + private final String type; + + private final TitleItem title; + private final List<SubscriptionListItem> subsList; private String customUrl = null; - public SubscriptionInfo(TitleItem title, List<SubscriptionListItem> subsList){ + public SubscriptionInfo(Long key, String type, TitleItem title, List<SubscriptionListItem> subsList){ + this.key = key; + this.type = type; this.title = title; if (subsList == null){ subsList = new ArrayList<SubscriptionListItem>(); @@ -88,6 +93,14 @@ public class SubscriptionInfo { return subsList != null && subsList.size() > 0; } + public Long getKey() { + return key; + } + + public String getType() { + return type; + } + /** * @param mimeType the mimetype of the desired output; supported are currently text/plain and text/html * @return the title of a notification entry. e.g. "5 new posts" for a forum subscription, or "10 new uploads" for a folder subscription diff --git a/src/main/java/org/olat/course/assessment/AssessmentNotificationsHandler.java b/src/main/java/org/olat/course/assessment/AssessmentNotificationsHandler.java index ba00855e4e6e49904649d1611caefa10c420b196..220f1087eca23ce25e252f3ea8432ec2b52278db 100644 --- a/src/main/java/org/olat/course/assessment/AssessmentNotificationsHandler.java +++ b/src/main/java/org/olat/course/assessment/AssessmentNotificationsHandler.java @@ -374,7 +374,7 @@ public class AssessmentNotificationsHandler implements NotificationsHandler { if(si == null) { String title = translator.translate("notifications.header", new String[]{course.getCourseTitle()}); String css = CourseNodeFactory.getInstance().getCourseNodeConfigurationEvenForDisabledBB(test.getType()).getIconCSSClass(); - si = new SubscriptionInfo(new TitleItem(title, css), null); + si = new SubscriptionInfo(subscriber.getKey(), p.getType(), new TitleItem(title, css), null); } si.addSubscriptionListItem(subListItem); } diff --git a/src/main/java/org/olat/course/nodes/ta/AbstractTaskNotificationHandler.java b/src/main/java/org/olat/course/nodes/ta/AbstractTaskNotificationHandler.java index 8d4e4c0ad96d0a6c4861ac693511371e4ba941a6..45fc1ab8572fe04314b8ee4d2b36eb592928f361 100644 --- a/src/main/java/org/olat/course/nodes/ta/AbstractTaskNotificationHandler.java +++ b/src/main/java/org/olat/course/nodes/ta/AbstractTaskNotificationHandler.java @@ -84,7 +84,7 @@ public abstract class AbstractTaskNotificationHandler extends LogDelegator { } } - si = new SubscriptionInfo(new TitleItem(translator.translate(getNotificationHeaderKey(), new String[]{displayName}), getCssClassIcon() ), null); + si = new SubscriptionInfo(subscriber.getKey(), p.getType(), new TitleItem(translator.translate(getNotificationHeaderKey(), new String[]{displayName}), getCssClassIcon() ), null); SubscriptionListItem subListItem; for (Iterator<FileInfo> it_infos = fInfos.iterator(); it_infos.hasNext();) { FileInfo fi = it_infos.next(); diff --git a/src/main/java/org/olat/modules/dialog/FileUploadNotificationHandler.java b/src/main/java/org/olat/modules/dialog/FileUploadNotificationHandler.java index 2cac6df3a70d196b23099bc161fcb09cd85f20ff..c5def9dacde43f3d148b5f009a1a56163a5da449 100644 --- a/src/main/java/org/olat/modules/dialog/FileUploadNotificationHandler.java +++ b/src/main/java/org/olat/modules/dialog/FileUploadNotificationHandler.java @@ -83,7 +83,7 @@ public class FileUploadNotificationHandler implements NotificationsHandler { final List<DialogElement> dialogElements = elements.getDialogPropertyElements(); final Translator translator = Util.createPackageTranslator(FileUploadNotificationHandler.class, locale); - si = new SubscriptionInfo(new TitleItem(translator.translate("notifications.header", new String[]{displayname}), CSSS_CLASS_UPLOAD_ICON), null); + si = new SubscriptionInfo(subscriber.getKey(), p.getType(), new TitleItem(translator.translate("notifications.header", new String[]{displayname}), CSSS_CLASS_UPLOAD_ICON), null); SubscriptionListItem subListItem; for (DialogElement element : dialogElements) { // do only show entries newer then the ones already seen diff --git a/src/main/java/org/olat/modules/fo/ForumNotificationsHandler.java b/src/main/java/org/olat/modules/fo/ForumNotificationsHandler.java index 867067a3981e08fc187dac8c0f384facb19143f4..c7758037d6bdf4c73cf4051f94073b253432fa89 100644 --- a/src/main/java/org/olat/modules/fo/ForumNotificationsHandler.java +++ b/src/main/java/org/olat/modules/fo/ForumNotificationsHandler.java @@ -82,7 +82,7 @@ public class ForumNotificationsHandler extends LogDelegator implements Notificat businessControlString = p.getBusinessPath() + "[Message:"; - si = new SubscriptionInfo(getTitleItem(p, translator), null); + si = new SubscriptionInfo(subscriber.getKey(), p.getType(), getTitleItem(p, translator), null); for (Message mInfo : mInfos) { String title = mInfo.getTitle(); Identity creator = mInfo.getCreator(); diff --git a/src/main/java/org/olat/modules/wiki/WikiPageChangeOrCreateNotificationHandler.java b/src/main/java/org/olat/modules/wiki/WikiPageChangeOrCreateNotificationHandler.java index e1840c4ef5d5deb1d91d9fd5ab1969c28cb2a177..6992cb87c80b8c554378615ce931def7b080e78b 100644 --- a/src/main/java/org/olat/modules/wiki/WikiPageChangeOrCreateNotificationHandler.java +++ b/src/main/java/org/olat/modules/wiki/WikiPageChangeOrCreateNotificationHandler.java @@ -120,7 +120,7 @@ public class WikiPageChangeOrCreateNotificationHandler extends LogDelegator impl Translator translator = Util.createPackageTranslator(WikiPageChangeOrCreateNotificationHandler.class, locale); TitleItem title = getTitleItem(p, translator); - si = new SubscriptionInfo(title, null); + si = new SubscriptionInfo(subscriber.getKey(), p.getType(), title, null); SubscriptionListItem subListItem; for (Iterator<WikiPage> it = pages.listIterator(); it.hasNext();) { WikiPage element = it.next(); diff --git a/src/main/java/org/olat/notifications/NoSubscriptionInfo.java b/src/main/java/org/olat/notifications/NoSubscriptionInfo.java index ad661b5be77e384bccb9488b4f3a38c11d9c3f63..0b17715920febd8ae1b4e6619eddd1d82e814a9a 100644 --- a/src/main/java/org/olat/notifications/NoSubscriptionInfo.java +++ b/src/main/java/org/olat/notifications/NoSubscriptionInfo.java @@ -38,7 +38,7 @@ public class NoSubscriptionInfo extends SubscriptionInfo { * */ NoSubscriptionInfo() { - super(null, null); + super(null, null, null, null); } /** diff --git a/src/main/java/org/olat/notifications/NotificationNewsController.java b/src/main/java/org/olat/notifications/NotificationNewsController.java index 08dd6805f58f4b3e054172b09c4b4d27b8bf8e9f..1ce4b04195b85842d5c677e7ea98d45d0daf6394 100644 --- a/src/main/java/org/olat/notifications/NotificationNewsController.java +++ b/src/main/java/org/olat/notifications/NotificationNewsController.java @@ -106,18 +106,16 @@ class NotificationNewsController extends BasicController implements Activateable * Update the new data model and refresh the GUI */ List<Subscriber> updateNewsDataModel() { - NotificationsManager man = NotificationsManager.getInstance(); - List<Subscriber> subs = man.getSubscribers(subscriberIdentity); + List<String> notiTypes = new ArrayList<String>(); if(StringHelper.containsNonWhitespace(newsType)) { - for(Iterator<Subscriber> it=subs.iterator(); it.hasNext(); ) { - if(!newsType.equals(it.next().getPublisher().getType())) { - it.remove(); - } - } + notiTypes.add(newsType); } + + NotificationsManager man = NotificationsManager.getInstance(); + List<Subscriber> subs = man.getSubscribers(subscriberIdentity, notiTypes); newsVC.contextPut("subs", subs); - subsInfoMap = NotificationHelper.getSubscriptionMap(getIdentity(), getLocale(), true, compareDate); + subsInfoMap = NotificationHelper.getSubscriptionMap(getLocale(), true, compareDate, subs); NotificationSubscriptionAndNewsFormatter subsFormatter = new NotificationSubscriptionAndNewsFormatter(compareDate, getTranslator(), subsInfoMap); newsVC.contextPut("subsFormatter", subsFormatter); return subs; diff --git a/src/main/java/org/olat/notifications/NotificationsManagerImpl.java b/src/main/java/org/olat/notifications/NotificationsManagerImpl.java index 406d42339589852b6d1783802bf8829eab743bd1..863e0831b37ddee065d50ef1466817fd91300861 100644 --- a/src/main/java/org/olat/notifications/NotificationsManagerImpl.java +++ b/src/main/java/org/olat/notifications/NotificationsManagerImpl.java @@ -150,13 +150,32 @@ public class NotificationsManagerImpl extends NotificationsManager implements Us * @param identity * @return List of Subscriber Objects which belong to the identity */ - @SuppressWarnings("unchecked") public List<Subscriber> getSubscribers(Identity identity) { - DB db = DBFactory.getInstance(); - String q = "select sub from org.olat.notifications.SubscriberImpl sub" - + " inner join fetch sub.publisher where sub.identity = :anIdentity"; - DBQuery query = db.createQuery(q); + return getSubscribers(identity, Collections.<String>emptyList()); + } + + /** + * subscribers for ONE person (e.g. subscribed to 5 forums -> 5 subscribers + * belonging to this person) restricted to the specified types + * + * @param identity + * @return List of Subscriber Objects which belong to the identity + */ + @Override + public List<Subscriber> getSubscribers(Identity identity, List<String> types) { + StringBuilder sb = new StringBuilder(); + sb.append("select sub from ").append(SubscriberImpl.class.getName()).append(" as sub ") + .append("inner join fetch sub.publisher as publisher ") + .append("where sub.identity = :anIdentity"); + if(types != null && !types.isEmpty()) { + sb.append(" and publisher.type in (:types)"); + } + DBQuery query = DBFactory.getInstance().createQuery(sb.toString()); query.setEntity("anIdentity", identity); + if(types != null && !types.isEmpty()) { + query.setParameterList("types", types); + } + @SuppressWarnings("unchecked") List<Subscriber> res = query.list(); return res; } diff --git a/src/main/java/org/olat/notifications/bc/FolderNotificationsHandler.java b/src/main/java/org/olat/notifications/bc/FolderNotificationsHandler.java index 404a0b34c6f9b3cccb9da1a0235909de02f76373..272f014da922bc0481dcfeb75cde3628725c9b33 100644 --- a/src/main/java/org/olat/notifications/bc/FolderNotificationsHandler.java +++ b/src/main/java/org/olat/notifications/bc/FolderNotificationsHandler.java @@ -85,7 +85,7 @@ public class FolderNotificationsHandler implements NotificationsHandler { final List<FileInfo> fInfos = FolderManager.getFileInfos(folderRoot, compareDate); final Translator translator = Util.createPackageTranslator(FolderNotificationsHandler.class, locale); - si = new SubscriptionInfo(getTitleItem(p, translator), null); + si = new SubscriptionInfo(subscriber.getKey(), p.getType(), getTitleItem(p, translator), null); SubscriptionListItem subListItem; for (Iterator<FileInfo> it_infos = fInfos.iterator(); it_infos.hasNext();) { FileInfo fi = it_infos.next(); diff --git a/src/main/java/org/olat/notifications/restapi/NotificationsWebService.java b/src/main/java/org/olat/notifications/restapi/NotificationsWebService.java index b42e3e940badcb4b826ee75f547111a550792886..ab3e31c975b7045b90152161a18611398a41e484 100644 --- a/src/main/java/org/olat/notifications/restapi/NotificationsWebService.java +++ b/src/main/java/org/olat/notifications/restapi/NotificationsWebService.java @@ -73,7 +73,8 @@ public class NotificationsWebService { */ @GET @Produces({MediaType.APPLICATION_XML ,MediaType.APPLICATION_JSON}) - public Response getNotifications(@QueryParam("date") String date, @Context HttpServletRequest httpRequest) { + public Response getNotifications(@QueryParam("date") String date, + @QueryParam("type") String type, @Context HttpServletRequest httpRequest) { Identity identity = RestSecurityHelper.getIdentity(httpRequest); Locale locale = RestSecurityHelper.getLocale(httpRequest); @@ -85,7 +86,12 @@ public class NotificationsWebService { compareDate = man.getCompareDateFromInterval(man.getUserIntervalOrDefault(identity)); } - Map<Subscriber,SubscriptionInfo> subsInfoMap = NotificationHelper.getSubscriptionMap(identity, locale, true, compareDate); + List<String> types = new ArrayList<String>(1); + if(StringHelper.containsNonWhitespace(type)) { + types.add(type); + } + + Map<Subscriber,SubscriptionInfo> subsInfoMap = NotificationHelper.getSubscriptionMap(identity, locale, true, compareDate, types); List<SubscriptionInfoVO> voes = new ArrayList<SubscriptionInfoVO>(); for(Map.Entry<Subscriber, SubscriptionInfo> entry: subsInfoMap.entrySet()) { SubscriptionInfo info = entry.getValue(); diff --git a/src/main/java/org/olat/notifications/restapi/vo/SubscriptionInfoVO.java b/src/main/java/org/olat/notifications/restapi/vo/SubscriptionInfoVO.java index ef3e36f6af0f67b8d7f8931f3b6b6be9f8fa9619..6489501372005d683739bf0939545c321c5a4d5b 100644 --- a/src/main/java/org/olat/notifications/restapi/vo/SubscriptionInfoVO.java +++ b/src/main/java/org/olat/notifications/restapi/vo/SubscriptionInfoVO.java @@ -33,10 +33,21 @@ import javax.xml.bind.annotation.XmlRootElement; import org.olat.core.util.notifications.SubscriptionInfo; import org.olat.core.util.notifications.items.SubscriptionListItem; + +/** + * + * Description:<br> + * + * <P> + * Initial Date: 27 oct. 2011 <br> + * + * @author srosse, stephane.rosseªfrentix.com, http://www.frentix.com + */ @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement(name = "subscriptionInfoVO") public class SubscriptionInfoVO { - + private Long key; + private String type; private String title; @XmlElementWrapper(name="items") @@ -48,6 +59,8 @@ public class SubscriptionInfoVO { } public SubscriptionInfoVO(SubscriptionInfo info) { + key = info.getKey(); + type = info.getType(); title = info.getTitle(SubscriptionInfo.MIME_PLAIN); if(info.getSubscriptionListItems() != null) { for(SubscriptionListItem item:info.getSubscriptionListItems()) { @@ -56,6 +69,22 @@ public class SubscriptionInfoVO { } } + public Long getKey() { + return key; + } + + public void setKey(Long key) { + this.key = key; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + public String getTitle() { return title; } diff --git a/src/main/java/org/olat/user/notification/NewUsersNotificationHandler.java b/src/main/java/org/olat/user/notification/NewUsersNotificationHandler.java index 49317c9d13d421f833e9c2d88dd93bc85673bb4a..f37071f4b02b392ae1180929b19e9f73c8129a2d 100644 --- a/src/main/java/org/olat/user/notification/NewUsersNotificationHandler.java +++ b/src/main/java/org/olat/user/notification/NewUsersNotificationHandler.java @@ -67,7 +67,7 @@ public class NewUsersNotificationHandler implements NotificationsHandler { si = NotificationsManager.getInstance().getNoSubscriptionInfo(); } else { translator = Util.createPackageTranslator(this.getClass(), locale); - si = new SubscriptionInfo(new TitleItem(getItemTitle(translator), CSSHelper.CSS_CLASS_GROUP), null); + si = new SubscriptionInfo(subscriber.getKey(), p.getType(), new TitleItem(getItemTitle(translator), CSSHelper.CSS_CLASS_GROUP), null); SubscriptionListItem subListItem; for (Identity newUser : identities) { String desc = translator.translate("notifications.entry", new String[] { NotificationHelper.getFormatedName(newUser) }); diff --git a/src/test/java/org/olat/restapi/NotificationsTest.java b/src/test/java/org/olat/restapi/NotificationsTest.java index c519201d372fb3813917c099e150aff37909cb8a..5f1f7a048f41f6d91dc965f0f7af3e6780ec9bde 100644 --- a/src/test/java/org/olat/restapi/NotificationsTest.java +++ b/src/test/java/org/olat/restapi/NotificationsTest.java @@ -22,16 +22,37 @@ package org.olat.restapi; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import java.io.IOException; +import java.io.InputStream; +import java.util.List; +import java.util.UUID; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.UriBuilder; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.methods.GetMethod; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.type.TypeReference; +import org.junit.Before; import org.junit.Test; +import org.olat.core.id.Identity; +import org.olat.core.util.notifications.NotificationsManager; +import org.olat.core.util.notifications.PublisherData; +import org.olat.core.util.notifications.SubscriptionContext; +import org.olat.core.util.resource.OresHelper; +import org.olat.modules.fo.Forum; +import org.olat.modules.fo.ForumManager; +import org.olat.modules.fo.Message; +import org.olat.notifications.restapi.vo.SubscriptionInfoVO; +import org.olat.test.JunitTestHelper; import org.olat.test.OlatJerseyTestCase; +import org.olat.user.notification.UsersSubscriptionManager; /** * @@ -43,17 +64,155 @@ import org.olat.test.OlatJerseyTestCase; */ public class NotificationsTest extends OlatJerseyTestCase { - public NotificationsTest() { - super(); - } + private static Identity userSubscriberId; + private static Identity userAndForumSubscriberId; + + @Before + public void setUp() throws Exception { + super.setUp(); + + userSubscriberId = JunitTestHelper.createAndPersistIdentityAsUser("rest-notifications-test-1"); + userAndForumSubscriberId = JunitTestHelper.createAndPersistIdentityAsUser("rest-notifications-test-2"); + JunitTestHelper.createAndPersistIdentityAsUser("rest-notifications-test-3"); + + SubscriptionContext subContext = UsersSubscriptionManager.getInstance().getNewUsersSubscriptionContext(); + PublisherData publisherData = UsersSubscriptionManager.getInstance().getNewUsersPublisherData(); + NotificationsManager notifManager = NotificationsManager.getInstance(); + if(!notifManager.isSubscribed(userSubscriberId, subContext)) { + notifManager.subscribe(userSubscriberId, subContext, publisherData); + } + if(!notifManager.isSubscribed(userAndForumSubscriberId, subContext)) { + notifManager.subscribe(userAndForumSubscriberId, subContext, publisherData); + } + + //create a forum + ForumManager fm = ForumManager.getInstance(); + Forum forum = ForumManager.getInstance().addAForum(); + Message m1 = fm.createMessage(); + m1.setTitle("Thread-1"); + m1.setBody("Body of Thread-1"); + fm.addTopMessage(userSubscriberId, forum, m1); + + //subscribe + SubscriptionContext forumSubContext = new SubscriptionContext("NotificationRestCourse", new Long(12356), "676"); + PublisherData forumPdata = new PublisherData(OresHelper.calculateTypeName(Forum.class), forum.getKey().toString(), ""); + if(!notifManager.isSubscribed(userAndForumSubscriberId, forumSubContext)) { + notifManager.subscribe(userAndForumSubscriberId, forumSubContext, forumPdata); + } + notifManager.markPublisherNews(forumSubContext, userSubscriberId); + + //generate one notification + String randomLogin = UUID.randomUUID().toString().replace("-", ""); + JunitTestHelper.createAndPersistIdentityAsUser(randomLogin); + } + @Test - public void testExecuteService() throws HttpException, IOException { - HttpClient c = loginWithCookie("administrator", "olat"); + public void testGetNotifications() throws HttpException, IOException { + HttpClient c = loginWithCookie("rest-notifications-test-1", "A6B7C8"); String request = "/notifications"; GetMethod method = createGet(request, MediaType.APPLICATION_JSON, true); int code = c.executeMethod(method); assertEquals(code, 200); - } + InputStream body = method.getResponseBodyAsStream(); + List<SubscriptionInfoVO> infos = parseUserArray(body); + method.releaseConnection(); + assertNotNull(infos); + assertFalse(infos.isEmpty()); + + SubscriptionInfoVO infoVO = infos.get(0); + assertNotNull(infoVO); + assertNotNull(infoVO.getKey()); + assertNotNull("User", infoVO.getType()); + assertNotNull(infoVO.getTitle()); + assertNotNull(infoVO.getItems()); + assertFalse(infoVO.getItems().isEmpty()); + } + + @Test + public void testGetUserNotifications() throws HttpException, IOException { + HttpClient c = loginWithCookie("rest-notifications-test-1", "A6B7C8"); + + UriBuilder request = UriBuilder.fromUri(getContextURI()).path("notifications").queryParam("type", "User"); + GetMethod method = createGet(request.build(), MediaType.APPLICATION_JSON, true); + int code = c.executeMethod(method); + assertEquals(code, 200); + InputStream body = method.getResponseBodyAsStream(); + List<SubscriptionInfoVO> infos = parseUserArray(body); + method.releaseConnection(); + assertNotNull(infos); + assertFalse(infos.isEmpty()); + + SubscriptionInfoVO infoVO = infos.get(0); + assertNotNull(infoVO); + assertNotNull(infoVO.getKey()); + assertNotNull("User", infoVO.getType()); + assertNotNull(infoVO.getTitle()); + assertNotNull(infoVO.getItems()); + assertFalse(infoVO.getItems().isEmpty()); + } + + @Test + public void testGetUserForumNotifications() throws HttpException, IOException { + HttpClient c = loginWithCookie("rest-notifications-test-2", "A6B7C8"); + + UriBuilder request = UriBuilder.fromUri(getContextURI()).path("notifications"); + GetMethod method = createGet(request.build(), MediaType.APPLICATION_JSON, true); + int code = c.executeMethod(method); + assertEquals(code, 200); + InputStream body = method.getResponseBodyAsStream(); + List<SubscriptionInfoVO> infos = parseUserArray(body); + method.releaseConnection(); + assertNotNull(infos); + assertEquals(2, infos.size()); + } + + @Test + public void testGetUserForumNotificationsByType() throws HttpException, IOException { + HttpClient c = loginWithCookie("rest-notifications-test-2", "A6B7C8"); + + UriBuilder request = UriBuilder.fromUri(getContextURI()).path("notifications").queryParam("type", "Forum"); + GetMethod method = createGet(request.build(), MediaType.APPLICATION_JSON, true); + int code = c.executeMethod(method); + assertEquals(code, 200); + InputStream body = method.getResponseBodyAsStream(); + List<SubscriptionInfoVO> infos = parseUserArray(body); + method.releaseConnection(); + assertNotNull(infos); + assertEquals(1, infos.size()); + + SubscriptionInfoVO infoVO = infos.get(0); + assertNotNull(infoVO); + assertNotNull(infoVO.getKey()); + assertNotNull("Forum", infoVO.getType()); + assertNotNull(infoVO.getTitle()); + assertNotNull(infoVO.getItems()); + assertFalse(infoVO.getItems().isEmpty()); + } + + @Test + public void testGetNoNotifications() throws HttpException, IOException { + HttpClient c = loginWithCookie("rest-notifications-test-3", "A6B7C8"); + + String request = "/notifications"; + GetMethod method = createGet(request, MediaType.APPLICATION_JSON, true); + int code = c.executeMethod(method); + assertEquals(code, 200); + InputStream body = method.getResponseBodyAsStream(); + List<SubscriptionInfoVO> infos = parseUserArray(body); + method.releaseConnection(); + assertNotNull(infos); + assertTrue(infos.isEmpty()); + } + + protected List<SubscriptionInfoVO> parseUserArray(InputStream body) { + try { + ObjectMapper mapper = new ObjectMapper(jsonFactory); + return mapper.readValue(body, new TypeReference<List<SubscriptionInfoVO>>(){/* */}); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } }