Search in sources :

Example 36 with Subscriber

use of org.olat.core.commons.services.notifications.Subscriber in project openolat by klemens.

the class NotificationsSubscribersTest method unsubscribe.

@Test
public void unsubscribe() throws IOException, URISyntaxException {
    Identity id1 = JunitTestHelper.createAndPersistIdentityAsRndUser("rest-sub-3");
    Identity id2 = JunitTestHelper.createAndPersistIdentityAsRndUser("rest-sub-4");
    Identity id3 = JunitTestHelper.createAndPersistIdentityAsRndUser("rest-sub-5");
    // deploy a course with forums
    URL courseWithForumsUrl = MyForumsTest.class.getResource("myCourseWS.zip");
    Assert.assertNotNull(courseWithForumsUrl);
    File courseWithForums = new File(courseWithForumsUrl.toURI());
    String softKey = UUID.randomUUID().toString().replace("-", "");
    RepositoryEntry courseEntry = CourseFactory.deployCourseFromZIP(courseWithForums, softKey, 4);
    Assert.assertNotNull(courseEntry);
    // load the course and found the first forum
    ICourse course = CourseFactory.loadCourse(courseEntry);
    // find the forum
    IdentityEnvironment ienv = new IdentityEnvironment(id1, new Roles(false, false, false, false, false, false, false));
    ForumVisitor forumVisitor = new ForumVisitor(course);
    new CourseTreeVisitor(course, ienv).visit(forumVisitor, new VisibleTreeFilter());
    FOCourseNode courseNode = forumVisitor.firstNode;
    Forum forum = forumVisitor.firstForum;
    Assert.assertNotNull(courseNode);
    Assert.assertNotNull(forum);
    // the 3 users subscribed to the forum
    PublisherData publisherData = new PublisherData("Forum", forum.getKey().toString(), "[RepositoryEntry:" + courseEntry.getKey() + "][CourseNode:" + courseNode.getIdent() + "]");
    SubscriptionContext subsContext = new SubscriptionContext("CourseModule", course.getResourceableId(), courseNode.getIdent());
    notificationsManager.subscribe(id1, subsContext, publisherData);
    notificationsManager.subscribe(id2, subsContext, publisherData);
    notificationsManager.subscribe(id3, subsContext, publisherData);
    dbInstance.commitAndCloseSession();
    // get the subscriber
    RestConnection conn = new RestConnection();
    Assert.assertTrue(conn.login("administrator", "openolat"));
    URI subscribersUri = UriBuilder.fromUri(getContextURI()).path("notifications").path("subscribers").path(subsContext.getResName()).path(subsContext.getResId().toString()).path(subsContext.getSubidentifier()).build();
    HttpGet getMethod = conn.createGet(subscribersUri, MediaType.APPLICATION_JSON, true);
    HttpResponse getResponse = conn.execute(getMethod);
    Assert.assertEquals(200, getResponse.getStatusLine().getStatusCode());
    List<SubscriberVO> subscriberVOes = parseGroupArray(getResponse.getEntity().getContent());
    Assert.assertNotNull(subscriberVOes);
    Assert.assertEquals(3, subscriberVOes.size());
    SubscriberVO subscriberId2VO = null;
    for (SubscriberVO subscriberVO : subscriberVOes) {
        if (subscriberVO.getIdentityKey().equals(id2.getKey())) {
            subscriberId2VO = subscriberVO;
        }
    }
    // delete id2
    URI deleteSubscriberUri = UriBuilder.fromUri(getContextURI()).path("notifications").path("subscribers").path(subscriberId2VO.getSubscriberKey().toString()).build();
    HttpDelete deleteMethod = conn.createDelete(deleteSubscriberUri, MediaType.APPLICATION_JSON);
    HttpResponse deleteResponse = conn.execute(deleteMethod);
    Assert.assertEquals(200, deleteResponse.getStatusLine().getStatusCode());
    // check
    Publisher publisher = notificationsManager.getPublisher(subsContext);
    List<Subscriber> survivingSubscribers = notificationsManager.getSubscribers(publisher);
    Assert.assertNotNull(survivingSubscribers);
    Assert.assertEquals(2, survivingSubscribers.size());
    for (Subscriber subscriber : survivingSubscribers) {
        Assert.assertNotEquals(id2, subscriber.getIdentity());
    }
}
Also used : HttpDelete(org.apache.http.client.methods.HttpDelete) VisibleTreeFilter(org.olat.course.run.userview.VisibleTreeFilter) HttpGet(org.apache.http.client.methods.HttpGet) ICourse(org.olat.course.ICourse) FOCourseNode(org.olat.course.nodes.FOCourseNode) RepositoryEntry(org.olat.repository.RepositoryEntry) PublisherData(org.olat.core.commons.services.notifications.PublisherData) URI(java.net.URI) URL(java.net.URL) Forum(org.olat.modules.fo.Forum) SubscriberVO(org.olat.core.commons.services.notifications.restapi.vo.SubscriberVO) Subscriber(org.olat.core.commons.services.notifications.Subscriber) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) Identity(org.olat.core.id.Identity) IdentityEnvironment(org.olat.core.id.IdentityEnvironment) CourseTreeVisitor(org.olat.course.run.userview.CourseTreeVisitor) HttpResponse(org.apache.http.HttpResponse) Roles(org.olat.core.id.Roles) Publisher(org.olat.core.commons.services.notifications.Publisher) File(java.io.File) Test(org.junit.Test)

Example 37 with Subscriber

use of org.olat.core.commons.services.notifications.Subscriber in project openolat by klemens.

the class NotificationsManagerTest method testUnsubscribe_v2.

@Test
public void testUnsubscribe_v2() {
    Identity id = JunitTestHelper.createAndPersistIdentityAsUser("unsubs-" + UUID.randomUUID().toString());
    // create a publisher
    String identifier = UUID.randomUUID().toString().replace("-", "");
    SubscriptionContext context = new SubscriptionContext("All", new Long(123), identifier);
    PublisherData publisherData = new PublisherData("testUnsubscribe", "e.g. forumdata=keyofforum", null);
    Publisher publisher = notificationManager.getOrCreatePublisher(context, publisherData);
    dbInstance.commitAndCloseSession();
    Assert.assertNotNull(publisher);
    // subscribe
    notificationManager.subscribe(id, context, publisherData);
    dbInstance.commitAndCloseSession();
    // check
    Subscriber subscriber = notificationManager.getSubscriber(id, publisher);
    Assert.assertNotNull(subscriber);
    // unsubscribe
    notificationManager.unsubscribe(id, context);
    dbInstance.commitAndCloseSession();
    // check
    boolean subscribed = notificationManager.isSubscribed(id, context);
    Assert.assertFalse(subscribed);
}
Also used : Subscriber(org.olat.core.commons.services.notifications.Subscriber) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) Publisher(org.olat.core.commons.services.notifications.Publisher) Identity(org.olat.core.id.Identity) PublisherData(org.olat.core.commons.services.notifications.PublisherData) Test(org.junit.Test)

Example 38 with Subscriber

use of org.olat.core.commons.services.notifications.Subscriber in project openolat by klemens.

the class NotificationsManagerTest method testSubscribe.

@Test
public void testSubscribe() {
    Identity id = JunitTestHelper.createAndPersistIdentityAsUser("subs-" + UUID.randomUUID().toString());
    // create a publisher
    String identifier = UUID.randomUUID().toString().replace("-", "");
    SubscriptionContext context = new SubscriptionContext("All", new Long(123), identifier);
    PublisherData publisherData = new PublisherData("testAllPublishers", "e.g. forumdata=keyofforum", null);
    Publisher publisher = notificationManager.getOrCreatePublisher(context, publisherData);
    dbInstance.commitAndCloseSession();
    Assert.assertNotNull(publisher);
    // subscribe
    notificationManager.subscribe(id, context, publisherData);
    dbInstance.commitAndCloseSession();
    // check
    boolean subscribed = notificationManager.isSubscribed(id, context);
    Assert.assertTrue(subscribed);
    dbInstance.commitAndCloseSession();
    // double check
    Subscriber subscriber = notificationManager.getSubscriber(id, publisher);
    Assert.assertNotNull(subscriber);
    Assert.assertEquals(publisher, subscriber.getPublisher());
    dbInstance.commitAndCloseSession();
    // triple check
    Subscriber reloadedSubscriber = notificationManager.getSubscriber(subscriber.getKey());
    Assert.assertNotNull(reloadedSubscriber);
    Assert.assertEquals(subscriber, reloadedSubscriber);
}
Also used : Subscriber(org.olat.core.commons.services.notifications.Subscriber) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) Publisher(org.olat.core.commons.services.notifications.Publisher) Identity(org.olat.core.id.Identity) PublisherData(org.olat.core.commons.services.notifications.PublisherData) Test(org.junit.Test)

Example 39 with Subscriber

use of org.olat.core.commons.services.notifications.Subscriber in project openolat by klemens.

the class NotificationsManagerTest method testValidSubscribersOf.

@Test
public void testValidSubscribersOf() {
    Identity id1 = JunitTestHelper.createAndPersistIdentityAsUser("valid1b-" + UUID.randomUUID().toString());
    Identity id2 = JunitTestHelper.createAndPersistIdentityAsUser("valid1b-" + UUID.randomUUID().toString());
    // create a publisher
    String identifier = UUID.randomUUID().toString().replace("-", "");
    SubscriptionContext context = new SubscriptionContext("Validb", new Long(123), identifier);
    PublisherData publisherData = new PublisherData("testValidSubscribers", "e.g. forumdata=keyofforum", null);
    Publisher publisher = notificationManager.getOrCreatePublisher(context, publisherData);
    dbInstance.commitAndCloseSession();
    Assert.assertNotNull(publisher);
    // add subscribers
    notificationManager.subscribe(id1, context, publisherData);
    notificationManager.subscribe(id2, context, publisherData);
    dbInstance.commitAndCloseSession();
    // get all subscribers of the publisher
    List<Subscriber> subscribers = notificationManager.getValidSubscribersOf(publisher);
    Assert.assertNotNull(subscribers);
    Assert.assertEquals(2, subscribers.size());
    Assert.assertEquals(publisher, subscribers.get(0).getPublisher());
    Assert.assertEquals(publisher, subscribers.get(1).getPublisher());
}
Also used : Subscriber(org.olat.core.commons.services.notifications.Subscriber) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) Publisher(org.olat.core.commons.services.notifications.Publisher) Identity(org.olat.core.id.Identity) PublisherData(org.olat.core.commons.services.notifications.PublisherData) Test(org.junit.Test)

Example 40 with Subscriber

use of org.olat.core.commons.services.notifications.Subscriber in project openolat by klemens.

the class NotificationsManagerTest method testValidSubscribers.

@Test
public void testValidSubscribers() {
    Identity id1 = JunitTestHelper.createAndPersistIdentityAsUser("valid1-" + UUID.randomUUID().toString());
    Identity id2 = JunitTestHelper.createAndPersistIdentityAsUser("valid1-" + UUID.randomUUID().toString());
    // create a publisher
    String identifier = UUID.randomUUID().toString().replace("-", "");
    SubscriptionContext context = new SubscriptionContext("Valid", new Long(123), identifier);
    PublisherData publisherData = new PublisherData("testValidSubscribers", "e.g. forumdata=keyofforum", null);
    Publisher publisher = notificationManager.getOrCreatePublisher(context, publisherData);
    dbInstance.commitAndCloseSession();
    Assert.assertNotNull(publisher);
    // add subscribers
    notificationManager.subscribe(id1, context, publisherData);
    notificationManager.subscribe(id2, context, publisherData);
    dbInstance.commitAndCloseSession();
    // get valid subscribers
    List<Subscriber> subscribers = notificationManager.getValidSubscribers(id1);
    Assert.assertNotNull(subscribers);
    Assert.assertEquals(1, subscribers.size());
    Assert.assertEquals(publisher, subscribers.get(0).getPublisher());
    Assert.assertEquals(id1, subscribers.get(0).getIdentity());
}
Also used : Subscriber(org.olat.core.commons.services.notifications.Subscriber) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) Publisher(org.olat.core.commons.services.notifications.Publisher) Identity(org.olat.core.id.Identity) PublisherData(org.olat.core.commons.services.notifications.PublisherData) Test(org.junit.Test)

Aggregations

Subscriber (org.olat.core.commons.services.notifications.Subscriber)82 Publisher (org.olat.core.commons.services.notifications.Publisher)42 Identity (org.olat.core.id.Identity)30 NotificationsManager (org.olat.core.commons.services.notifications.NotificationsManager)26 ArrayList (java.util.ArrayList)24 SubscriptionContext (org.olat.core.commons.services.notifications.SubscriptionContext)22 HashSet (java.util.HashSet)18 Test (org.junit.Test)18 PublisherData (org.olat.core.commons.services.notifications.PublisherData)18 Date (java.util.Date)16 GET (javax.ws.rs.GET)16 Produces (javax.ws.rs.Produces)16 ICourse (org.olat.course.ICourse)16 CourseTreeVisitor (org.olat.course.run.userview.CourseTreeVisitor)16 VisibleTreeFilter (org.olat.course.run.userview.VisibleTreeFilter)16 SubscriptionInfo (org.olat.core.commons.services.notifications.SubscriptionInfo)10 Roles (org.olat.core.id.Roles)10 FOCourseNode (org.olat.course.nodes.FOCourseNode)10 List (java.util.List)8 Locale (java.util.Locale)8