use of org.olat.core.commons.services.notifications.SubscriptionContext in project openolat by klemens.
the class NotificationsSubscribersTest method subscribe.
@Test
public void subscribe() throws IOException, URISyntaxException {
Identity id1 = JunitTestHelper.createAndPersistIdentityAsRndUser("rest-sub-1");
Identity id2 = JunitTestHelper.createAndPersistIdentityAsRndUser("rest-sub-2");
// 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);
// put subscribers
RestConnection conn = new RestConnection();
Assert.assertTrue(conn.login("administrator", "openolat"));
PublisherVO subscribersVO = new PublisherVO();
// publisher data
subscribersVO.setType("Forum");
subscribersVO.setData(forum.getKey().toString());
subscribersVO.setBusinessPath("[RepositoryEntry:" + courseEntry.getKey() + "][CourseNode:" + courseNode.getIdent() + "]");
// context
subscribersVO.setResName("CourseModule");
subscribersVO.setResId(course.getResourceableId());
subscribersVO.setSubidentifier(courseNode.getIdent());
subscribersVO.getUsers().add(UserVOFactory.get(id1));
subscribersVO.getUsers().add(UserVOFactory.get(id2));
// create the subscribers
URI subscribersUri = UriBuilder.fromUri(getContextURI()).path("notifications").path("subscribers").build();
HttpPut putMethod = conn.createPut(subscribersUri, MediaType.APPLICATION_JSON, true);
conn.addJsonEntity(putMethod, subscribersVO);
HttpResponse response = conn.execute(putMethod);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
EntityUtils.consume(response.getEntity());
// get publisher
SubscriptionContext subsContext = new SubscriptionContext("CourseModule", course.getResourceableId(), courseNode.getIdent());
Publisher publisher = notificationsManager.getPublisher(subsContext);
Assert.assertNotNull(publisher);
// get subscribers
List<Subscriber> subscribers = notificationsManager.getSubscribers(publisher);
Assert.assertNotNull(subscribers);
Assert.assertEquals(2, subscribers.size());
conn.shutdown();
}
use of org.olat.core.commons.services.notifications.SubscriptionContext 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());
}
}
use of org.olat.core.commons.services.notifications.SubscriptionContext in project openolat by klemens.
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());
}
use of org.olat.core.commons.services.notifications.SubscriptionContext in project openolat by klemens.
the class NotificationsTest method testGetCourseForumNotifications.
@Test
public void testGetCourseForumNotifications() throws IOException, URISyntaxException {
// create a course with a forum
Identity id = JunitTestHelper.createAndPersistIdentityAsAuthor("rest-not-6-" + UUID.randomUUID().toString());
ICourse course = CoursesWebService.createEmptyCourse(id, "Course forum not", "Course forum with notification", null);
dbInstance.intermediateCommit();
// create the forum
CourseNodeConfiguration newNodeConfig = CourseNodeFactory.getInstance().getCourseNodeConfiguration("fo");
FOCourseNode forumNode = (FOCourseNode) newNodeConfig.getInstance();
forumNode.setShortTitle("Forum");
forumNode.setLearningObjectives("forum objectives");
forumNode.setNoAccessExplanation("You don't have access");
Forum courseForum = forumNode.loadOrCreateForum(course.getCourseEnvironment());
course.getEditorTreeModel().addCourseNode(forumNode, course.getRunStructure().getRootNode());
CourseFactory.publishCourse(course, RepositoryEntry.ACC_USERS, false, id, Locale.ENGLISH);
dbInstance.intermediateCommit();
// add message and publisher
RepositoryEntry re = repositoryManager.lookupRepositoryEntry(course.getCourseEnvironment().getCourseGroupManager().getCourseResource(), true);
String businessPath = "[RepositoryEntry:" + re.getKey() + "][CourseNode:" + forumNode.getIdent() + "]";
SubscriptionContext forumSubContext = new SubscriptionContext("CourseModule", course.getResourceableId(), forumNode.getIdent());
PublisherData forumPdata = new PublisherData(OresHelper.calculateTypeName(Forum.class), courseForum.getKey().toString(), businessPath);
notificationManager.subscribe(id, forumSubContext, forumPdata);
Message message = createMessage(id, courseForum);
notificationManager.markPublisherNews(forumSubContext, null, true);
dbInstance.commitAndCloseSession();
// get the notification
RestConnection conn = new RestConnection();
assertTrue(conn.login(id.getName(), "A6B7C8"));
UriBuilder request = UriBuilder.fromUri(getContextURI()).path("notifications");
HttpGet method = conn.createGet(request.build(), MediaType.APPLICATION_JSON, true);
HttpResponse response = conn.execute(method);
assertEquals(200, response.getStatusLine().getStatusCode());
List<SubscriptionInfoVO> infos = parseUserArray(response.getEntity().getContent());
Assert.assertNotNull(infos);
Assert.assertEquals(1, infos.size());
SubscriptionInfoVO infoVO = infos.get(0);
Assert.assertNotNull(infoVO.getItems());
Assert.assertEquals(1, infoVO.getItems().size());
SubscriptionListItemVO itemVO = infoVO.getItems().get(0);
Assert.assertNotNull(itemVO);
Assert.assertEquals(course.getResourceableId(), itemVO.getCourseKey());
Assert.assertEquals(forumNode.getIdent(), itemVO.getCourseNodeId());
Assert.assertEquals(message.getKey(), itemVO.getMessageKey());
}
use of org.olat.core.commons.services.notifications.SubscriptionContext in project openolat by klemens.
the class NotificationsTest method testGetCourseFolderNotifications.
@Test
public void testGetCourseFolderNotifications() throws IOException, URISyntaxException {
// create a course with a forum
Identity id = JunitTestHelper.createAndPersistIdentityAsAuthor("rest-not-7-" + UUID.randomUUID().toString());
ICourse course = CoursesWebService.createEmptyCourse(id, "Course folder not", "Course with folder and notification", null);
dbInstance.intermediateCommit();
// create the folder
CourseNodeConfiguration newNodeConfig = CourseNodeFactory.getInstance().getCourseNodeConfiguration("bc");
BCCourseNode folderNode = (BCCourseNode) newNodeConfig.getInstance();
folderNode.setShortTitle("Folder");
folderNode.setLearningObjectives("folder objectives");
folderNode.setNoAccessExplanation("You don't have access");
String relPath = BCCourseNode.getFoldernodePathRelToFolderBase(course.getCourseEnvironment(), folderNode);
VFSContainer folder = BCCourseNode.getNodeFolderContainer(folderNode, course.getCourseEnvironment());
course.getEditorTreeModel().addCourseNode(folderNode, course.getRunStructure().getRootNode());
CourseFactory.publishCourse(course, RepositoryEntry.ACC_USERS, false, id, Locale.ENGLISH);
dbInstance.intermediateCommit();
// add message and publisher
RepositoryEntry re = repositoryManager.lookupRepositoryEntry(course.getCourseEnvironment().getCourseGroupManager().getCourseResource(), true);
String businessPath = "[RepositoryEntry:" + re.getKey() + "][CourseNode:" + folderNode.getIdent() + "]";
SubscriptionContext folderSubContext = new SubscriptionContext("CourseModule", course.getResourceableId(), folderNode.getIdent());
PublisherData folderPdata = new PublisherData("FolderModule", relPath, businessPath);
notificationManager.subscribe(id, folderSubContext, folderPdata);
String filename = addFile(folder);
notificationManager.markPublisherNews(folderSubContext, null, true);
dbInstance.commitAndCloseSession();
// get the notification
RestConnection conn = new RestConnection();
assertTrue(conn.login(id.getName(), "A6B7C8"));
UriBuilder request = UriBuilder.fromUri(getContextURI()).path("notifications");
HttpGet method = conn.createGet(request.build(), MediaType.APPLICATION_JSON, true);
HttpResponse response = conn.execute(method);
assertEquals(200, response.getStatusLine().getStatusCode());
List<SubscriptionInfoVO> infos = parseUserArray(response.getEntity().getContent());
Assert.assertNotNull(infos);
Assert.assertEquals(1, infos.size());
SubscriptionInfoVO infoVO = infos.get(0);
Assert.assertNotNull(infoVO.getItems());
Assert.assertEquals(1, infoVO.getItems().size());
SubscriptionListItemVO itemVO = infoVO.getItems().get(0);
Assert.assertNotNull(itemVO);
Assert.assertEquals(course.getResourceableId(), itemVO.getCourseKey());
Assert.assertEquals(folderNode.getIdent(), itemVO.getCourseNodeId());
Assert.assertEquals("/" + filename, itemVO.getPath());
}
Aggregations