Search in sources :

Example 61 with PublisherData

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

the class InfoMessageFrontendManagerTest method getInfoSubscribers.

@Test
public void getInfoSubscribers() {
    Identity id1 = JunitTestHelper.createAndPersistIdentityAsRndUser("info-1");
    Identity id2 = JunitTestHelper.createAndPersistIdentityAsRndUser("info-2");
    RepositoryEntry resource = JunitTestHelper.createAndPersistRepositoryEntry();
    BusinessGroup businessGroup = groupService.createBusinessGroup(null, "gdao", "gdao-desc", -1, -1, false, false, resource);
    final OLATResourceable ores = new OLATResourceable() {

        @Override
        public String getResourceableTypeName() {
            return businessGroup.getResourceableTypeName();
        }

        @Override
        public Long getResourceableId() {
            return businessGroup.getResourceableId();
        }
    };
    // create publisher data
    String identifier = InfoMessageFrontendManager.businessGroupResSubPath;
    SubscriptionContext context = new SubscriptionContext(businessGroup.getResourceableTypeName(), businessGroup.getResourceableId(), identifier);
    PublisherData publisherData = new PublisherData("testGetSubscriberIdentities", "e.g. data=infomessage", null);
    dbInstance.commitAndCloseSession();
    // add subscribers
    notificationManager.subscribe(id1, context, publisherData);
    notificationManager.subscribe(id2, context, publisherData);
    dbInstance.commitAndCloseSession();
    // get identities
    List<Identity> identities = infoManager.getInfoSubscribers(ores, identifier);
    Assert.assertNotNull(identities);
    Assert.assertEquals(2, identities.size());
    Assert.assertTrue(identities.contains(id1));
    Assert.assertTrue(identities.contains(id2));
}
Also used : BusinessGroup(org.olat.group.BusinessGroup) OLATResourceable(org.olat.core.id.OLATResourceable) RepositoryEntry(org.olat.repository.RepositoryEntry) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) Identity(org.olat.core.id.Identity) PublisherData(org.olat.core.commons.services.notifications.PublisherData) Test(org.junit.Test)

Example 62 with PublisherData

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

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 63 with PublisherData

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

the class NotificationsTest method testGetPublisher.

@Test
public void testGetPublisher() throws IOException, URISyntaxException {
    // create a business group with forum notifications
    Identity id = JunitTestHelper.createAndPersistIdentityAsRndUser("rest-not-9");
    BusinessGroup group = businessGroupService.createBusinessGroup(id, "Notifications 1", "REST forum notifications for group", null, null, false, false, null);
    CollaborationTools tools = CollaborationToolsFactory.getInstance().getOrCreateCollaborationTools(group);
    tools.setToolEnabled(CollaborationTools.TOOL_FORUM, true);
    Forum groupForum = tools.getForum();
    dbInstance.commitAndCloseSession();
    // publish
    String businessPath = "[BusinessGroup:" + group.getKey() + "][toolforum:0]";
    SubscriptionContext forumSubContext = new SubscriptionContext("BusinessGroup", group.getKey(), "toolforum");
    PublisherData forumPdata = new PublisherData(OresHelper.calculateTypeName(Forum.class), groupForum.getKey().toString(), businessPath);
    notificationManager.subscribe(id, forumSubContext, forumPdata);
    dbInstance.commitAndCloseSession();
    // GET publisher
    RestConnection conn = new RestConnection();
    assertTrue(conn.login("administrator", "openolat"));
    UriBuilder request = UriBuilder.fromUri(getContextURI()).path("notifications/publisher/BusinessGroup/" + group.getKey() + "/toolforum");
    HttpGet method = conn.createGet(request.build(), MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    PublisherVO publisher = conn.parse(response, PublisherVO.class);
    Assert.assertNotNull(publisher);
    Assert.assertEquals("BusinessGroup", publisher.getResName());
    Assert.assertEquals(group.getKey(), publisher.getResId());
    Assert.assertEquals("toolforum", publisher.getSubidentifier());
    Assert.assertEquals("Forum", publisher.getType());
    Assert.assertEquals(groupForum.getKey().toString(), publisher.getData());
}
Also used : BusinessGroup(org.olat.group.BusinessGroup) HttpGet(org.apache.http.client.methods.HttpGet) CollaborationTools(org.olat.collaboration.CollaborationTools) HttpResponse(org.apache.http.HttpResponse) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) PublisherVO(org.olat.core.commons.services.notifications.restapi.vo.PublisherVO) Identity(org.olat.core.id.Identity) UriBuilder(javax.ws.rs.core.UriBuilder) PublisherData(org.olat.core.commons.services.notifications.PublisherData) Forum(org.olat.modules.fo.Forum) Test(org.junit.Test)

Example 64 with PublisherData

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

the class NotificationsManagerTest method testCreatePublisher.

@Test
public void testCreatePublisher() {
    String identifier = UUID.randomUUID().toString().replace("-", "");
    SubscriptionContext context = new SubscriptionContext("PS", new Long(123), identifier);
    PublisherData publisherData = new PublisherData("testPublisherSubscriber", "e.g. forumdata=keyofforum", null);
    Publisher publisher = notificationManager.getOrCreatePublisher(context, publisherData);
    dbInstance.commitAndCloseSession();
    // check values
    Assert.assertNotNull(publisher);
    Assert.assertNotNull(publisher.getKey());
    Assert.assertNotNull(publisher.getCreationDate());
    Assert.assertNotNull(publisher.getLatestNewsDate());
    Assert.assertEquals("PS", publisher.getResName());
    Assert.assertEquals(new Long(123), publisher.getResId());
    Assert.assertEquals(identifier, publisher.getSubidentifier());
    // check if exists
    Publisher reloadedPublisher = notificationManager.getPublisher(context);
    Assert.assertNotNull(reloadedPublisher);
    Assert.assertEquals(publisher, reloadedPublisher);
}
Also used : SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) Publisher(org.olat.core.commons.services.notifications.Publisher) PublisherData(org.olat.core.commons.services.notifications.PublisherData) Test(org.junit.Test)

Example 65 with PublisherData

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

the class NotificationsManagerTest method testUnsubscribe_v1.

@Test
public void testUnsubscribe_v1() {
    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(subscriber);
    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)

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