Skip to content
Snippets Groups Projects
Commit b13fc46b authored by srosse's avatar srosse
Browse files

OO-3988: implement a REST API to manipulate reminders

parent cc968c21
No related branches found
No related tags found
No related merge requests found
Showing
with 905 additions and 15 deletions
......@@ -97,7 +97,7 @@ public class LectureBlocksWebService {
* Create or update a lecture block.
* @response.representation.200.qname {http://www.example.com}lectureBlocksVO
* @response.representation.200.mediaType application/xml, application/json
* @response.representation.200.doc The updated configuration
* @response.representation.200.doc The updated lecture block
* @response.representation.200.example {@link org.olat.modules.lecture.restapi.Examples#SAMPLE_LECTUREBLOCKVO}
* @response.representation.401.doc The roles of the authenticated user are not sufficient
* @response.representation.404.doc The course not found
......
/**
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at the
* <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Initial code contributed and copyrighted by<br>
* frentix GmbH, http://www.frentix.com
* <p>
*/
package org.olat.modules.reminder.manager;
import java.io.InputStream;
import java.io.OutputStream;
import org.olat.core.util.xml.XStreamHelper;
import org.olat.modules.reminder.model.ImportExportReminders;
import org.olat.modules.reminder.model.ReminderRules;
import com.thoughtworks.xstream.XStream;
/**
*
* Initial date: 25 mars 2019<br>
* @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
*
*/
public class ReminderRulesXStream {
private static final XStream ruleXStream = XStreamHelper.createXStreamInstance();
static {
ruleXStream.alias("rule", org.olat.modules.reminder.model.ReminderRuleImpl.class);
ruleXStream.alias("rules", org.olat.modules.reminder.model.ReminderRules.class);
ruleXStream.alias("reminders", org.olat.modules.reminder.model.ImportExportReminders.class);
ruleXStream.alias("reminder", org.olat.modules.reminder.model.ImportExportReminder.class);
}
public static ReminderRules toRules(String rulesXml) {
return (ReminderRules)ruleXStream.fromXML(rulesXml);
}
public static String toXML(ReminderRules rules) {
return ruleXStream.toXML(rules);
}
public static void toXML(ImportExportReminders reminders, OutputStream out) {
ruleXStream.toXML(reminders, out);
}
public static ImportExportReminders fromXML(InputStream in) {
return (ImportExportReminders)ruleXStream.fromXML(in);
}
}
......@@ -50,7 +50,6 @@ import org.olat.core.util.mail.MailContextImpl;
import org.olat.core.util.mail.MailManager;
import org.olat.core.util.mail.MailTemplate;
import org.olat.core.util.mail.MailerResult;
import org.olat.core.util.xml.XStreamHelper;
import org.olat.modules.reminder.Reminder;
import org.olat.modules.reminder.ReminderRule;
import org.olat.modules.reminder.ReminderService;
......@@ -69,8 +68,6 @@ import org.olat.user.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.thoughtworks.xstream.XStream;
/**
*
* Initial date: 08.04.2015<br>
......@@ -81,13 +78,6 @@ import com.thoughtworks.xstream.XStream;
public class ReminderServiceImpl implements ReminderService {
private static final OLog log = Tracing.createLoggerFor(ReminderServiceImpl.class);
private static final XStream ruleXStream = XStreamHelper.createXStreamInstance();
static {
ruleXStream.alias("rule", org.olat.modules.reminder.model.ReminderRuleImpl.class);
ruleXStream.alias("rules", org.olat.modules.reminder.model.ReminderRules.class);
ruleXStream.alias("reminders", org.olat.modules.reminder.model.ImportExportReminders.class);
ruleXStream.alias("reminder", org.olat.modules.reminder.model.ImportExportReminder.class);
}
@Autowired
......@@ -174,12 +164,12 @@ public class ReminderServiceImpl implements ReminderService {
@Override
public String toXML(ReminderRules rules) {
return ruleXStream.toXML(rules);
return ReminderRulesXStream.toXML(rules);
}
@Override
public ReminderRules toRules(String rulesXml) {
return (ReminderRules)ruleXStream.fromXML(rulesXml);
return ReminderRulesXStream.toRules(rulesXml);
}
@Override
......@@ -192,7 +182,7 @@ public class ReminderServiceImpl implements ReminderService {
ImportExportReminder exportReminder = new ImportExportReminder(reminder);
exportReminders.getReminders().add(exportReminder);
}
ruleXStream.toXML(exportReminders, fOut);
ReminderRulesXStream.toXML(exportReminders, fOut);
} catch(Exception e) {
log.error("", e);
}
......@@ -205,7 +195,7 @@ public class ReminderServiceImpl implements ReminderService {
List<Reminder> reminders = new ArrayList<>();
if(reminderFile.exists()) {
try(InputStream in = new FileInputStream(reminderFile)) {
ImportExportReminders importReminders = (ImportExportReminders)ruleXStream.fromXML(in);
ImportExportReminders importReminders = ReminderRulesXStream.fromXML(in);
List<ImportExportReminder> importReminderList = importReminders.getReminders();
for(ImportExportReminder importReminder:importReminderList) {
Reminder reminder = reminderDao.createReminder(newEntry, creator);
......
/**
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at the
* <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Initial code contributed and copyrighted by<br>
* frentix GmbH, http://www.frentix.com
* <p>
*/
package org.olat.modules.reminder.restapi;
/**
*
* Initial date: 25 mars 2019<br>
* @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
*
*/
public class Examples {
public static final ReminderVO SAMPLE_REMINDERVO = new ReminderVO();
static {
SAMPLE_REMINDERVO.setKey(231l);
SAMPLE_REMINDERVO.setRepoEntryKey(264827834l);
SAMPLE_REMINDERVO.setDescription("A nice little description");
SAMPLE_REMINDERVO.setEmailSubject("The subject");
SAMPLE_REMINDERVO.setEmailBody("The body");
}
}
/**
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at the
* <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Initial code contributed and copyrighted by<br>
* frentix GmbH, http://www.frentix.com
* <p>
*/
package org.olat.modules.reminder.restapi;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import org.olat.modules.reminder.ReminderRule;
import org.olat.modules.reminder.model.ReminderRuleImpl;
/**
*
* Initial date: 25 mars 2019<br>
* @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "reminderRuleVO")
public class ReminderRuleVO {
private String type;
private String leftOperand;
private String operator;
private String rightOperand;
private String rightUnit;
public ReminderRuleVO() {
//
}
public static ReminderRuleVO valueOf(ReminderRule rule) {
ReminderRuleVO vo = new ReminderRuleVO();
vo.setType(rule.getType());
if(rule instanceof ReminderRuleImpl) {
ReminderRuleImpl ruleImpl = (ReminderRuleImpl)rule;
vo.setLeftOperand(ruleImpl.getLeftOperand());
vo.setOperator(ruleImpl.getOperator());
vo.setRightOperand(ruleImpl.getRightOperand());
vo.setRightUnit(ruleImpl.getRightUnit());
}
return vo;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getLeftOperand() {
return leftOperand;
}
public void setLeftOperand(String leftOperand) {
this.leftOperand = leftOperand;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public String getRightOperand() {
return rightOperand;
}
public void setRightOperand(String rightOperand) {
this.rightOperand = rightOperand;
}
public String getRightUnit() {
return rightUnit;
}
public void setRightUnit(String rightUnit) {
this.rightUnit = rightUnit;
}
}
/**
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at the
* <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Initial code contributed and copyrighted by<br>
* frentix GmbH, http://www.frentix.com
* <p>
*/
package org.olat.modules.reminder.restapi;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import org.olat.core.util.StringHelper;
import org.olat.modules.reminder.Reminder;
import org.olat.modules.reminder.ReminderRule;
import org.olat.modules.reminder.manager.ReminderRulesXStream;
import org.olat.modules.reminder.model.ReminderRules;
/**
*
* Initial date: 25 mars 2019<br>
* @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "reminderVO")
public class ReminderVO {
private Long key;
private Long repoEntryKey;
private String description;
private String emailSubject;
private String emailBody;
private List<ReminderRuleVO> rules = new ArrayList<>();
public static ReminderVO valueOf(Reminder reminder, Long repositoryEntryKey) {
ReminderVO vo = new ReminderVO();
vo.setKey(reminder.getKey());
vo.setRepoEntryKey(repositoryEntryKey);
vo.setDescription(reminder.getDescription());
vo.setEmailSubject(reminder.getEmailSubject());
vo.setEmailBody(reminder.getEmailBody());
String configuration = reminder.getConfiguration();
if(StringHelper.containsNonWhitespace(configuration)) {
ReminderRules reminderRules = ReminderRulesXStream.toRules(configuration);
List<ReminderRule> rules = reminderRules.getRules();
List<ReminderRuleVO> ruleVoes = new ArrayList<>();
for(ReminderRule rule:rules) {
ruleVoes.add(ReminderRuleVO.valueOf(rule));
}
vo.setRules(ruleVoes);
}
return vo;
}
public Long getKey() {
return key;
}
public void setKey(Long key) {
this.key = key;
}
public Long getRepoEntryKey() {
return repoEntryKey;
}
public void setRepoEntryKey(Long repoEntryKey) {
this.repoEntryKey = repoEntryKey;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getEmailSubject() {
return emailSubject;
}
public void setEmailSubject(String emailSubject) {
this.emailSubject = emailSubject;
}
public String getEmailBody() {
return emailBody;
}
public void setEmailBody(String emailBody) {
this.emailBody = emailBody;
}
public List<ReminderRuleVO> getRules() {
return rules;
}
public void setRules(List<ReminderRuleVO> rules) {
this.rules = rules;
}
}
/**
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at the
* <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Initial code contributed and copyrighted by<br>
* frentix GmbH, http://www.frentix.com
* <p>
*/
package org.olat.modules.reminder.restapi;
import static org.olat.restapi.security.RestSecurityHelper.getIdentity;
import static org.olat.restapi.security.RestSecurityHelper.getRoles;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.olat.core.id.Identity;
import org.olat.core.id.Roles;
import org.olat.modules.reminder.Reminder;
import org.olat.modules.reminder.ReminderService;
import org.olat.modules.reminder.manager.ReminderRulesXStream;
import org.olat.modules.reminder.model.ReminderRuleImpl;
import org.olat.modules.reminder.model.ReminderRules;
import org.olat.repository.RepositoryEntry;
import org.springframework.beans.factory.annotation.Autowired;
/**
*
* Initial date: 25 mars 2019<br>
* @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
*
*/
public class RemindersWebService {
private RepositoryEntry entry;
@Autowired
private ReminderService reminderService;
public RemindersWebService(RepositoryEntry entry) {
this.entry = entry;
}
/**
* Return the reminders of the specified course or repository entry.
* @response.representation.200.qname {http://www.example.com}reminderVO
* @response.representation.200.mediaType application/xml, application/json
* @response.representation.200.doc An array of lecture blocks
* @response.representation.200.example {@link org.olat.modules.reminder.restapi.Examples#SAMPLE_REMINDERVO}
* @response.representation.401.doc The roles of the authenticated user are not sufficient
* @response.representation.404.doc The resource not found
* @param httpRequest The HTTP request
* @return The reminders
*/
@GET
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getReminders(@Context HttpServletRequest httpRequest) {
Roles roles = getRoles(httpRequest);
if(!roles.isOLATAdmin()) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
List<Reminder> reminders = reminderService.getReminders(entry);
List<ReminderVO> voList = new ArrayList<>(reminders.size());
for(Reminder reminder:reminders) {
voList.add(ReminderVO.valueOf(reminder, entry.getKey()));
}
ReminderVO[] voes = voList.toArray(new ReminderVO[voList.size()]);
return Response.ok(voes).build();
}
/**
* Create or update a reminder.
*
* @response.representation.200.qname {http://www.example.com}reminderVO
* @response.representation.200.mediaType application/xml, application/json
* @response.representation.200.doc The updated reminder
* @response.representation.200.example {@link org.olat.modules.reminder.restapi.Examples#SAMPLE_REMINDERVO}
* @response.representation.401.doc The roles of the authenticated user are not sufficient
* @response.representation.404.doc The course or repository entry not found
* @param reminder The reminder
* @param httpRequest The HTTP request
* @return It returns the updated / created reminder.
*/
@PUT
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response putReminder(ReminderVO reminder, @Context HttpServletRequest httpRequest) {
Roles roles = getRoles(httpRequest);
if(!roles.isOLATAdmin()) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
Reminder updatedReminder = saveReminder(reminder, httpRequest);
return Response.ok(ReminderVO.valueOf(updatedReminder, entry.getKey())).build();
}
/**
* Create or update a reminder.
*
* @response.representation.200.qname {http://www.example.com}reminderVO
* @response.representation.200.mediaType application/xml, application/json
* @response.representation.200.doc The updated reminder
* @response.representation.200.example {@link org.olat.modules.lecture.restapi.Examples#SAMPLE_REMINDERVO}
* @response.representation.401.doc The roles of the authenticated user are not sufficient
* @response.representation.404.doc The course or repository entry not found
* @param reminder The reminder
* @param httpRequest The HTTP request
* @return It returns the updated / created reminder.
*/
@POST
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Consumes({MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response postReminder(ReminderVO reminder, @Context HttpServletRequest httpRequest) {
Roles roles = getRoles(httpRequest);
if(!roles.isOLATAdmin()) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
Reminder updatedReminder = saveReminder(reminder, httpRequest);
return Response.ok(ReminderVO.valueOf(updatedReminder, entry.getKey())).build();
}
private Reminder saveReminder(ReminderVO reminderVo, HttpServletRequest httpRequest) {
Identity creator = getIdentity(httpRequest);
Reminder reminder;
if(reminderVo.getKey() == null) {
reminder = reminderService.createReminder(entry, creator);
} else {
reminder = reminderService.loadByKey(reminderVo.getKey());
}
reminder.setDescription(reminderVo.getDescription());
reminder.setEmailSubject(reminderVo.getEmailSubject());
reminder.setEmailBody(reminderVo.getEmailBody());
if(reminderVo.getRules() != null && !reminderVo.getRules().isEmpty()) {
ReminderRules rules = new ReminderRules();
for(ReminderRuleVO ruleVo:reminderVo.getRules()) {
ReminderRuleImpl rule = new ReminderRuleImpl();
rule.setType(ruleVo.getType());
rule.setLeftOperand(ruleVo.getLeftOperand());
rule.setOperator(ruleVo.getOperator());
rule.setRightOperand(ruleVo.getRightOperand());
rule.setRightUnit(ruleVo.getRightUnit());
rules.getRules().add(rule);
}
String configuration = ReminderRulesXStream.toXML(rules);
reminder.setConfiguration(configuration);
}
return reminderService.save(reminder);
}
/**
* Delete a specific reminder.
* @response.representation.200.doc Reminder deleted
* @response.representation.401.doc The roles of the authenticated user are not sufficient
* @response.representation.404.doc The course or repository entry not found
* @param reminderKey The reminder primary key
* @param httpRequest The HTTP request
* @return Nothing
*/
@DELETE
@Path("{reminderKey}")
public Response deleteReminder(@PathParam("reminderKey") Long reminderKey, @Context HttpServletRequest httpRequest) {
Roles roles = getRoles(httpRequest);
if(!roles.isOLATAdmin()) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
Reminder reminder = reminderService.loadByKey(reminderKey);
if(reminder == null) {
return Response.serverError().status(Status.NOT_FOUND).build();
}
reminderService.delete(reminder);
return Response.ok().build();
}
}
......@@ -75,6 +75,7 @@ import org.olat.core.util.mail.MailPackage;
import org.olat.fileresource.FileResourceManager;
import org.olat.fileresource.types.ImsCPFileResource;
import org.olat.modules.lecture.restapi.LectureBlocksWebService;
import org.olat.modules.reminder.restapi.RemindersWebService;
import org.olat.repository.ErrorList;
import org.olat.repository.RepositoryEntry;
import org.olat.repository.RepositoryManager;
......@@ -176,6 +177,21 @@ public class RepositoryEntryResource {
CoreSpringFactory.autowireObject(service);
return service;
}
/**
* To get the web service for the reminders of a specific learning resource.
* @response.representation.200.doc A web service to manage the reminders
* @param repoEntryKey The primary key of the learning resource
* @return The web service for reminders.
*/
@Path("reminders")
public RemindersWebService getRemindersWebService(@PathParam("repoEntryKey")String repoEntryKey) {
RepositoryEntry re = lookupRepositoryEntry(repoEntryKey);
if(re == null) return null;
RemindersWebService service = new RemindersWebService(re);
CoreSpringFactory.autowireObject(service);
return service;
}
/**
* Returns the list of owners of the repository entry specified by the groupKey.
......
......@@ -82,6 +82,7 @@ import org.olat.course.run.userview.UserCourseEnvironment;
import org.olat.course.run.userview.UserCourseEnvironmentImpl;
import org.olat.modules.gotomeeting.restapi.GoToTrainingWebService;
import org.olat.modules.lecture.restapi.LectureBlocksWebService;
import org.olat.modules.reminder.restapi.RemindersWebService;
import org.olat.modules.vitero.restapi.ViteroBookingWebService;
import org.olat.repository.ErrorList;
import org.olat.repository.RepositoryEntry;
......@@ -198,6 +199,19 @@ public class CourseWebService {
CoreSpringFactory.autowireObject(service);
return service;
}
/**
* To get the web service for the reminders of a specific course.
*
* @return The web service for reminders.
*/
@Path("reminders")
public RemindersWebService getRemindersWebService() {
RepositoryEntry courseRe = course.getCourseEnvironment().getCourseGroupManager().getCourseEntry();
RemindersWebService service = new RemindersWebService(courseRe);
CoreSpringFactory.autowireObject(service);
return service;
}
/**
* Publish the course.
......
/**
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at the
* <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Initial code contributed and copyrighted by<br>
* frentix GmbH, http://www.frentix.com
* <p>
*/
package org.olat.restapi;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.junit.Assert;
import org.junit.Test;
import org.olat.core.commons.persistence.DB;
import org.olat.core.id.Identity;
import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing;
import org.olat.course.nodes.gta.rule.AssignTaskRuleSPI;
import org.olat.modules.reminder.Reminder;
import org.olat.modules.reminder.ReminderRule;
import org.olat.modules.reminder.ReminderService;
import org.olat.modules.reminder.manager.ReminderDAO;
import org.olat.modules.reminder.manager.ReminderRuleEngine;
import org.olat.modules.reminder.manager.ReminderRulesXStream;
import org.olat.modules.reminder.model.ReminderImpl;
import org.olat.modules.reminder.model.ReminderRuleImpl;
import org.olat.modules.reminder.model.ReminderRules;
import org.olat.modules.reminder.restapi.ReminderRuleVO;
import org.olat.modules.reminder.restapi.ReminderVO;
import org.olat.modules.reminder.rule.DateRuleSPI;
import org.olat.repository.RepositoryEntry;
import org.olat.test.JunitTestHelper;
import org.olat.test.OlatJerseyTestCase;
import org.springframework.beans.factory.annotation.Autowired;
/**
*
* Initial date: 25 mars 2019<br>
* @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
*
*/
public class RemindersWebServiceTest extends OlatJerseyTestCase {
private static final OLog log = Tracing.createLoggerFor(RemindersWebServiceTest.class);
@Autowired
private DB dbInstance;
@Autowired
private ReminderDAO reminderDao;
@Autowired
private DateRuleSPI dateRuleSpi;
@Autowired
private ReminderService reminderService;
@Test
public void getReminders_repo()
throws IOException, URISyntaxException {
RestConnection conn = new RestConnection();
Assert.assertTrue(conn.login("administrator", "openolat"));
Identity creator = JunitTestHelper.createAndPersistIdentityAsRndUser("rest-rem-1");
RepositoryEntry entry = JunitTestHelper.createAndPersistRepositoryEntry();
Reminder transientReminder = reminderService.createReminder(entry, creator);
transientReminder.setDescription("Hello");
Reminder reminder = reminderService.save(transientReminder);
dbInstance.commitAndCloseSession();
URI request = UriBuilder.fromUri(getContextURI()).path("repo").path("entries")
.path(entry.getKey().toString()).path("reminders").build();
HttpGet method = conn.createGet(request, MediaType.APPLICATION_JSON, true);
HttpResponse response = conn.execute(method);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
List<ReminderVO> reminderVoes = parseReminderArray(response.getEntity());
Assert.assertNotNull(reminderVoes);
Assert.assertEquals(1, reminderVoes.size());
ReminderVO reminderVo = reminderVoes.get(0);
Assert.assertNotNull(reminderVo);
Assert.assertEquals(reminder.getKey(), reminderVo.getKey());
}
@Test
public void getReminders_course()
throws IOException, URISyntaxException {
RestConnection conn = new RestConnection();
Assert.assertTrue(conn.login("administrator", "openolat"));
Identity creator = JunitTestHelper.createAndPersistIdentityAsRndUser("rest-rem-1");
RepositoryEntry courseEntry = JunitTestHelper.deployBasicCourse(creator);
Reminder transientReminder = reminderService.createReminder(courseEntry, creator);
transientReminder.setDescription("Hello");
Reminder reminder = reminderService.save(transientReminder);
dbInstance.commitAndCloseSession();
URI request = UriBuilder.fromUri(getContextURI()).path("repo").path("courses")
.path(courseEntry.getOlatResource().getResourceableId().toString()).path("reminders").build();
HttpGet method = conn.createGet(request, MediaType.APPLICATION_JSON, true);
HttpResponse response = conn.execute(method);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
List<ReminderVO> reminderVoes = parseReminderArray(response.getEntity());
Assert.assertNotNull(reminderVoes);
Assert.assertEquals(1, reminderVoes.size());
ReminderVO reminderVo = reminderVoes.get(0);
Assert.assertNotNull(reminderVo);
Assert.assertEquals(reminder.getKey(), reminderVo.getKey());
}
@Test
public void putNewReminder()
throws IOException, URISyntaxException {
Identity creator = JunitTestHelper.createAndPersistIdentityAsAdmin("rest-rem-1-" + UUID.randomUUID());
RepositoryEntry entry = JunitTestHelper.createAndPersistRepositoryEntry();
dbInstance.commitAndCloseSession();
RestConnection conn = new RestConnection();
Assert.assertTrue(conn.login(creator.getName(), JunitTestHelper.PWD));
ReminderVO reminderVo = new ReminderVO();
reminderVo.setDescription("Hello, I'm a reminder");
reminderVo.setEmailSubject("Remind me");
reminderVo.setEmailBody("<p>To remind you</p>");
List<ReminderRuleVO> rulesVo = new ArrayList<>();
reminderVo.setRules(rulesVo);
ReminderRuleVO ruleVo = new ReminderRuleVO();
ruleVo.setType("DateRuleSPI");
ruleVo.setOperator(">");
ruleVo.setRightOperand("2015-05-13T00:00:00");
rulesVo.add(ruleVo);
URI request = UriBuilder.fromUri(getContextURI()).path("repo").path("entries")
.path(entry.getKey().toString()).path("reminders").build();
HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true);
conn.addJsonEntity(method, reminderVo);
HttpResponse response = conn.execute(method);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
// check the return value
ReminderVO savedReminderVo = conn.parse(response, ReminderVO.class);
Assert.assertNotNull(savedReminderVo);
Assert.assertNotNull(savedReminderVo.getKey());
Assert.assertEquals(entry.getKey(), savedReminderVo.getRepoEntryKey());
Assert.assertEquals("Hello, I'm a reminder", savedReminderVo.getDescription());
Assert.assertEquals("Remind me", savedReminderVo.getEmailSubject());
Assert.assertEquals("<p>To remind you</p>", savedReminderVo.getEmailBody());
Assert.assertEquals(1, savedReminderVo.getRules().size());
// check the reminder on the database
List<Reminder> reminders = reminderService.getReminders(entry);
Assert.assertNotNull(reminders);
Assert.assertEquals(1, reminders.size());
Reminder reminder = reminders.get(0);
Assert.assertEquals(savedReminderVo.getKey(), reminder.getKey());
Assert.assertEquals("Hello, I'm a reminder", reminder.getDescription());
Assert.assertEquals("Remind me", reminder.getEmailSubject());
Assert.assertEquals("<p>To remind you</p>", reminder.getEmailBody());
Assert.assertEquals(creator, ((ReminderImpl)reminder).getCreator());
// check rule configuration
String configuration = reminder.getConfiguration();
ReminderRules rules = reminderService.toRules(configuration);
List<ReminderRule> ruleList = rules.getRules();
Assert.assertEquals(1, ruleList.size());
ReminderRule rule = ruleList.get(0);
Assert.assertEquals(ReminderRuleEngine.DATE_RULE_TYPE, rule.getType());
// check if the rule works
boolean ok = dateRuleSpi.evaluate(rule);
Assert.assertTrue(ok);
}
@Test
public void postReminder()
throws IOException, URISyntaxException {
Identity creator = JunitTestHelper.createAndPersistIdentityAsAdmin("rest-rem-3-" + UUID.randomUUID());
RepositoryEntry entry = JunitTestHelper.createAndPersistRepositoryEntry();
Reminder reminder = reminderService.createReminder(entry, creator);
reminder.setDescription("Hello, I'm a reminder");
reminder.setEmailSubject("Remind me");
reminder.setEmailBody("<p>To remind you</p>");
ReminderRules rules = new ReminderRules();
ReminderRuleImpl rule = new ReminderRuleImpl();
rule.setType("DateRuleSPI");
rule.setOperator("<");
rule.setRightOperand("2015-05-13T00:00:00");
rules.getRules().add(rule);
String configuration = ReminderRulesXStream.toXML(rules);
reminder.setConfiguration(configuration);
reminder = reminderService.save(reminder);
dbInstance.commitAndCloseSession();
RestConnection conn = new RestConnection();
Assert.assertTrue(conn.login(creator.getName(), JunitTestHelper.PWD));
ReminderVO reminderVo = new ReminderVO();
reminderVo.setKey(reminder.getKey());
reminderVo.setRepoEntryKey(entry.getKey());
reminderVo.setDescription("I forgot");
reminderVo.setEmailSubject("I forgot the subject");
reminderVo.setEmailBody("<p>I forgot the body</p>");
List<ReminderRuleVO> rulesVo = new ArrayList<>();
reminderVo.setRules(rulesVo);
ReminderRuleVO rule1Vo = new ReminderRuleVO();
rule1Vo.setType("AssignTaskRuleSPI");
rule1Vo.setLeftOperand("937539759");
rule1Vo.setOperator("<");
rule1Vo.setRightOperand("1");
rule1Vo.setRightUnit("day");
rulesVo.add(rule1Vo);
ReminderRuleVO rule2Vo = new ReminderRuleVO();
rule2Vo.setType("DateRuleSPI");
rule2Vo.setOperator(">");
rule2Vo.setRightOperand("2019-05-15T00:00:00");
rulesVo.add(rule2Vo);
reminderVo.setRules(rulesVo);
URI request = UriBuilder.fromUri(getContextURI()).path("repo").path("entries")
.path(entry.getKey().toString()).path("reminders").build();
HttpPost method = conn.createPost(request, MediaType.APPLICATION_JSON);
conn.addJsonEntity(method, reminderVo);
HttpResponse response = conn.execute(method);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
// check the return value
ReminderVO savedReminderVo = conn.parse(response, ReminderVO.class);
Assert.assertNotNull(savedReminderVo);
Assert.assertNotNull(savedReminderVo.getKey());
Assert.assertEquals(entry.getKey(), savedReminderVo.getRepoEntryKey());
Assert.assertEquals("I forgot", savedReminderVo.getDescription());
Assert.assertEquals("I forgot the subject", savedReminderVo.getEmailSubject());
Assert.assertEquals("<p>I forgot the body</p>", savedReminderVo.getEmailBody());
Assert.assertEquals(2, savedReminderVo.getRules().size());
// check the reminder on the database
List<Reminder> reminders = reminderService.getReminders(entry);
Assert.assertNotNull(reminders);
Assert.assertEquals(1, reminders.size());
Reminder updatedReminder = reminders.get(0);
Assert.assertEquals(savedReminderVo.getKey(), updatedReminder.getKey());
Assert.assertEquals("I forgot", updatedReminder.getDescription());
Assert.assertEquals("I forgot the subject", updatedReminder.getEmailSubject());
Assert.assertEquals("<p>I forgot the body</p>", updatedReminder.getEmailBody());
// check rule configuration
String updatedConfiguration = updatedReminder.getConfiguration();
ReminderRules updatedRules = reminderService.toRules(updatedConfiguration);
List<ReminderRule> updatedRuleList = updatedRules.getRules();
Assert.assertEquals(2, updatedRuleList.size());
ReminderRuleImpl updatedRule1 = (ReminderRuleImpl)updatedRuleList.get(0);
Assert.assertEquals(AssignTaskRuleSPI.class.getSimpleName(), updatedRule1.getType());
Assert.assertEquals("937539759", updatedRule1.getLeftOperand());
Assert.assertEquals("<", updatedRule1.getOperator());
Assert.assertEquals("1", updatedRule1.getRightOperand());
Assert.assertEquals("day", updatedRule1.getRightUnit());
ReminderRuleImpl updatedRule2 = (ReminderRuleImpl)updatedRuleList.get(1);
Assert.assertEquals(DateRuleSPI.class.getSimpleName(), updatedRule2.getType());
Assert.assertEquals(">", updatedRule2.getOperator());
Assert.assertEquals("2019-05-15T00:00:00", updatedRule2.getRightOperand());
}
@Test
public void deleteReminder()
throws IOException, URISyntaxException {
RestConnection conn = new RestConnection();
Assert.assertTrue(conn.login("administrator", "openolat"));
Identity creator = JunitTestHelper.createAndPersistIdentityAsRndUser("rest-rem-1");
Identity id = JunitTestHelper.createAndPersistIdentityAsRndUser("rest-rem-1");
RepositoryEntry courseEntry = JunitTestHelper.deployBasicCourse(creator);
Reminder transientReminder = reminderService.createReminder(courseEntry, creator);
transientReminder.setDescription("Hello");
Reminder reminder = reminderService.save(transientReminder);
//mark as sent
reminderDao.markAsSend(reminder, id, "ok");
dbInstance.commitAndCloseSession();
URI request = UriBuilder.fromUri(getContextURI()).path("repo").path("entries")
.path(courseEntry.getKey().toString()).path("reminders")
.path(reminder.getKey().toString()).build();
HttpDelete method = conn.createDelete(request, MediaType.APPLICATION_JSON);
HttpResponse response = conn.execute(method);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
Reminder deletedReminder = reminderService.loadByKey(reminder.getKey());
Assert.assertNull(deletedReminder);
}
private List<ReminderVO> parseReminderArray(HttpEntity entity) {
try(InputStream content=entity.getContent()) {
ObjectMapper mapper = new ObjectMapper(jsonFactory);
return mapper.readValue(content, new TypeReference<List<ReminderVO>>(){/* */});
} catch (Exception e) {
log.error("", e);
return null;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment