Search in sources :

Example 31 with PublisherData

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

the class RepositoryEntryMembershipProcessorTest method testRemoveParticipant.

@Test
public void testRemoveParticipant() {
    RepositoryEntry re = JunitTestHelper.createAndPersistRepositoryEntry();
    // create a group with members
    Identity owner = JunitTestHelper.createAndPersistIdentityAsRndUser("remp-proc-1");
    Identity member = JunitTestHelper.createAndPersistIdentityAsRndUser("remp-proc-2");
    Identity participant = JunitTestHelper.createAndPersistIdentityAsRndUser("mbr-proc-3");
    repositoryEntryRelationDao.addRole(owner, re, GroupRoles.owner.name());
    repositoryEntryRelationDao.addRole(member, re, GroupRoles.coach.name());
    repositoryEntryRelationDao.addRole(member, re, GroupRoles.participant.name());
    repositoryEntryRelationDao.addRole(participant, re, GroupRoles.participant.name());
    // create a publisher
    SubscriptionContext context = new SubscriptionContext(re.getOlatResource(), "");
    PublisherData publisherData = new PublisherData("testGroupPublishers", "e.g. something", null);
    Publisher publisher = notificationManager.getOrCreatePublisher(context, publisherData);
    Assert.assertNotNull(publisher);
    dbInstance.commitAndCloseSession();
    // subscribe
    notificationManager.subscribe(owner, context, publisherData);
    notificationManager.subscribe(member, context, publisherData);
    notificationManager.subscribe(participant, context, publisherData);
    dbInstance.commitAndCloseSession();
    // remove member and participant as participant of the repo entry
    List<Identity> removeIdentities = new ArrayList<>(2);
    removeIdentities.add(member);
    removeIdentities.add(participant);
    MailPackage mailing = new MailPackage(false);
    repositoryManager.removeParticipants(owner, removeIdentities, re, mailing, false);
    // wait for the remove of subscription
    waitForCondition(new CheckUnsubscription(participant, context, dbInstance, notificationManager), 5000);
    sleep(1000);
    // check that subscription of id1 was deleted but not the ones of id2 and coach
    boolean subscribedPart = notificationManager.isSubscribed(participant, context);
    Assert.assertFalse(subscribedPart);
    boolean subscribedMember = notificationManager.isSubscribed(member, context);
    Assert.assertTrue(subscribedMember);
    boolean subscribedOwner = notificationManager.isSubscribed(owner, context);
    Assert.assertTrue(subscribedOwner);
}
Also used : MailPackage(org.olat.core.util.mail.MailPackage) ArrayList(java.util.ArrayList) RepositoryEntry(org.olat.repository.RepositoryEntry) 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 32 with PublisherData

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

the class RepositoryEntryMembershipProcessorTest method testRemoveCoach_withBusinessGroups.

@Test
public void testRemoveCoach_withBusinessGroups() {
    RepositoryEntry re = JunitTestHelper.createAndPersistRepositoryEntry();
    // create a group with members
    Identity owner = JunitTestHelper.createAndPersistIdentityAsRndUser("remp-proc-1");
    Identity member = JunitTestHelper.createAndPersistIdentityAsRndUser("remp-proc-2");
    Identity coach = JunitTestHelper.createAndPersistIdentityAsRndUser("mbr-proc-3");
    repositoryEntryRelationDao.addRole(owner, re, GroupRoles.owner.name());
    repositoryEntryRelationDao.addRole(member, re, GroupRoles.coach.name());
    repositoryEntryRelationDao.addRole(coach, re, GroupRoles.coach.name());
    BusinessGroup businessGroup = businessGroupDao.createAndPersist(coach, "mbr-proc-1", "mbr-proc-desc", -1, -1, false, false, false, false, false);
    businessGroupRelationDao.addRelationToResource(businessGroup, re);
    // create a publisher
    SubscriptionContext context = new SubscriptionContext(re.getOlatResource(), "");
    PublisherData publisherData = new PublisherData("testGroupPublishers", "e.g. something", null);
    Publisher publisher = notificationManager.getOrCreatePublisher(context, publisherData);
    Assert.assertNotNull(publisher);
    dbInstance.commitAndCloseSession();
    // subscribe
    notificationManager.subscribe(owner, context, publisherData);
    notificationManager.subscribe(member, context, publisherData);
    notificationManager.subscribe(coach, context, publisherData);
    dbInstance.commitAndCloseSession();
    // remove member and coach as coach of the repo entry
    List<Identity> removeIdentities = new ArrayList<>(2);
    removeIdentities.add(member);
    removeIdentities.add(coach);
    repositoryManager.removeTutors(owner, removeIdentities, re, new MailPackage(false));
    // wait for the remove of subscription
    waitForCondition(new CheckUnsubscription(member, context, dbInstance, notificationManager), 5000);
    sleep(1000);
    // check that subscription of id1 was deleted but not the ones of id2 and coach
    boolean subscribedMember = notificationManager.isSubscribed(member, context);
    Assert.assertFalse(subscribedMember);
    boolean subscribedCoach = notificationManager.isSubscribed(coach, context);
    Assert.assertTrue(subscribedCoach);
    boolean subscribedOwner = notificationManager.isSubscribed(owner, context);
    Assert.assertTrue(subscribedOwner);
}
Also used : MailPackage(org.olat.core.util.mail.MailPackage) BusinessGroup(org.olat.group.BusinessGroup) ArrayList(java.util.ArrayList) RepositoryEntry(org.olat.repository.RepositoryEntry) 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 33 with PublisherData

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

the class UserFoldersTest method myFolders.

/**
 * Test retrieve the folder which the user subscribe in a course.
 * @throws IOException
 * @throws URISyntaxException
 */
@Test
public void myFolders() throws IOException, URISyntaxException {
    final Identity id = JunitTestHelper.createAndPersistIdentityAsUser("my-" + UUID.randomUUID().toString());
    dbInstance.commitAndCloseSession();
    // load my forums
    RestConnection conn = new RestConnection();
    assertTrue(conn.login(id.getName(), "A6B7C8"));
    // subscribed to nothing
    URI uri = UriBuilder.fromUri(getContextURI()).path("users").path(id.getKey().toString()).path("folders").build();
    HttpGet method = conn.createGet(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    InputStream body = response.getEntity().getContent();
    FolderVOes folders = conn.parse(body, FolderVOes.class);
    Assert.assertNotNull(folders);
    Assert.assertNotNull(folders.getFolders());
    Assert.assertEquals(0, folders.getFolders().length);
    // subscribe to the forum
    IdentityEnvironment ienv = new IdentityEnvironment(id, new Roles(false, false, false, false, false, false, false));
    new CourseTreeVisitor(myCourse, ienv).visit(new Visitor() {

        @Override
        public void visit(INode node) {
            if (node instanceof BCCourseNode) {
                BCCourseNode folderNode = (BCCourseNode) node;
                String relPath = BCCourseNode.getFoldernodePathRelToFolderBase(myCourse.getCourseEnvironment(), folderNode);
                String businessPath = "[RepositoryEntry:" + myCourseRe.getKey() + "][CourseNode:" + folderNode.getIdent() + "]";
                SubscriptionContext folderSubContext = new SubscriptionContext("CourseModule", myCourse.getResourceableId(), folderNode.getIdent());
                PublisherData folderPdata = new PublisherData("FolderModule", relPath, businessPath);
                NotificationsManager.getInstance().subscribe(id, folderSubContext, folderPdata);
            }
        }
    }, new VisibleTreeFilter());
    dbInstance.commitAndCloseSession();
    // retrieve my folders
    HttpGet method2 = conn.createGet(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response2 = conn.execute(method2);
    assertEquals(200, response2.getStatusLine().getStatusCode());
    InputStream body2 = response2.getEntity().getContent();
    FolderVOes folders2 = conn.parse(body2, FolderVOes.class);
    Assert.assertNotNull(folders2);
    Assert.assertNotNull(folders2.getFolders());
    Assert.assertEquals(1, folders2.getFolders().length);
}
Also used : FolderVOes(org.olat.restapi.support.vo.FolderVOes) INode(org.olat.core.util.nodes.INode) Visitor(org.olat.core.util.tree.Visitor) CourseTreeVisitor(org.olat.course.run.userview.CourseTreeVisitor) InputStream(java.io.InputStream) VisibleTreeFilter(org.olat.course.run.userview.VisibleTreeFilter) HttpGet(org.apache.http.client.methods.HttpGet) CourseTreeVisitor(org.olat.course.run.userview.CourseTreeVisitor) HttpResponse(org.apache.http.HttpResponse) Roles(org.olat.core.id.Roles) URI(java.net.URI) PublisherData(org.olat.core.commons.services.notifications.PublisherData) BCCourseNode(org.olat.course.nodes.BCCourseNode) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) Identity(org.olat.core.id.Identity) IdentityEnvironment(org.olat.core.id.IdentityEnvironment) Test(org.junit.Test)

Example 34 with PublisherData

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

the class MyForumsTest method myForums.

/**
 * Test retrieve the forum which the user subscribe in a course.
 * @throws IOException
 * @throws URISyntaxException
 */
@Test
public void myForums() throws IOException, URISyntaxException {
    URL courseWithForumsUrl = MyForumsTest.class.getResource("myCourseWS.zip");
    Assert.assertNotNull(courseWithForumsUrl);
    File courseWithForums = new File(courseWithForumsUrl.toURI());
    String softKey = UUID.randomUUID().toString().replace("_", "");
    RepositoryEntry myCourseRe = CourseFactory.deployCourseFromZIP(courseWithForums, softKey, 4);
    Assert.assertNotNull(myCourseRe);
    ICourse myCourse = CourseFactory.loadCourse(myCourseRe);
    final Identity id = JunitTestHelper.createAndPersistIdentityAsUser("my-" + UUID.randomUUID().toString());
    dbInstance.commitAndCloseSession();
    // load my forums
    RestConnection conn = new RestConnection();
    assertTrue(conn.login(id.getName(), "A6B7C8"));
    // subscribed to nothing
    URI uri = UriBuilder.fromUri(getContextURI()).path("users").path(id.getKey().toString()).path("forums").build();
    HttpGet method = conn.createGet(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    InputStream body = response.getEntity().getContent();
    ForumVOes forums = conn.parse(body, ForumVOes.class);
    Assert.assertNotNull(forums);
    Assert.assertNotNull(forums.getForums());
    Assert.assertEquals(0, forums.getForums().length);
    // subscribe to the forum
    IdentityEnvironment ienv = new IdentityEnvironment(id, new Roles(false, false, false, false, false, false, false));
    new CourseTreeVisitor(myCourse, ienv).visit(new Visitor() {

        @Override
        public void visit(INode node) {
            if (node instanceof FOCourseNode) {
                FOCourseNode forumNode = (FOCourseNode) node;
                Forum forum = forumNode.loadOrCreateForum(myCourse.getCourseEnvironment());
                String businessPath = "[RepositoryEntry:" + myCourseRe.getKey() + "][CourseNode:" + forumNode.getIdent() + "]";
                SubscriptionContext forumSubContext = new SubscriptionContext("CourseModule", myCourse.getResourceableId(), forumNode.getIdent());
                PublisherData forumPdata = new PublisherData(OresHelper.calculateTypeName(Forum.class), forum.getKey().toString(), businessPath);
                NotificationsManager.getInstance().subscribe(id, forumSubContext, forumPdata);
            }
        }
    }, new VisibleTreeFilter());
    dbInstance.commitAndCloseSession();
    // retrieve my forums
    HttpGet method2 = conn.createGet(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response2 = conn.execute(method2);
    assertEquals(200, response2.getStatusLine().getStatusCode());
    InputStream body2 = response2.getEntity().getContent();
    ForumVOes forums2 = conn.parse(body2, ForumVOes.class);
    Assert.assertNotNull(forums2);
    Assert.assertNotNull(forums2.getForums());
    Assert.assertEquals(1, forums2.getForums().length);
}
Also used : INode(org.olat.core.util.nodes.INode) Visitor(org.olat.core.util.tree.Visitor) CourseTreeVisitor(org.olat.course.run.userview.CourseTreeVisitor) InputStream(java.io.InputStream) ForumVOes(org.olat.modules.fo.restapi.ForumVOes) VisibleTreeFilter(org.olat.course.run.userview.VisibleTreeFilter) HttpGet(org.apache.http.client.methods.HttpGet) CourseTreeVisitor(org.olat.course.run.userview.CourseTreeVisitor) HttpResponse(org.apache.http.HttpResponse) ICourse(org.olat.course.ICourse) Roles(org.olat.core.id.Roles) FOCourseNode(org.olat.course.nodes.FOCourseNode) RepositoryEntry(org.olat.repository.RepositoryEntry) URI(java.net.URI) PublisherData(org.olat.core.commons.services.notifications.PublisherData) URL(java.net.URL) Forum(org.olat.modules.fo.Forum) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) Identity(org.olat.core.id.Identity) File(java.io.File) IdentityEnvironment(org.olat.core.id.IdentityEnvironment) Test(org.junit.Test)

Example 35 with PublisherData

use of org.olat.core.commons.services.notifications.PublisherData 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)

Aggregations

PublisherData (org.olat.core.commons.services.notifications.PublisherData)100 SubscriptionContext (org.olat.core.commons.services.notifications.SubscriptionContext)84 Test (org.junit.Test)64 Identity (org.olat.core.id.Identity)60 Publisher (org.olat.core.commons.services.notifications.Publisher)42 RepositoryEntry (org.olat.repository.RepositoryEntry)22 BusinessGroup (org.olat.group.BusinessGroup)20 HttpResponse (org.apache.http.HttpResponse)16 HttpGet (org.apache.http.client.methods.HttpGet)16 Subscriber (org.olat.core.commons.services.notifications.Subscriber)16 ArrayList (java.util.ArrayList)14 ContextualSubscriptionController (org.olat.core.commons.services.notifications.ui.ContextualSubscriptionController)12 Forum (org.olat.modules.fo.Forum)12 UriBuilder (javax.ws.rs.core.UriBuilder)10 SubscriptionInfoVO (org.olat.core.commons.services.notifications.restapi.vo.SubscriptionInfoVO)8 SubscriptionListItemVO (org.olat.core.commons.services.notifications.restapi.vo.SubscriptionListItemVO)8 DBRuntimeException (org.olat.core.logging.DBRuntimeException)8 ICourse (org.olat.course.ICourse)8 File (java.io.File)6 URI (java.net.URI)6