From f3ceb8850f2705a5b0a906c255548fa1bbb5c3fd Mon Sep 17 00:00:00 2001 From: gita cliff Date: Sun, 17 May 2020 20:23:21 +0300 Subject: [PATCH] Refactor Task action resource to take in task definition uuid in addition to name --- .../openmrs1_8/TaskActionResource1_8.java | 17 +- .../openmrs1_8/TaskDefinitionResource1_8.java | 19 +- .../helper/TaskServiceWrapper2_4.java | 22 ++ .../openmrs2_4/TaskActionResource2_4.java | 217 ++++++++++++++++++ .../openmrs2_4/TaskDefinitionResource2_4.java | 164 +++++++++++++ .../rest/web/MockTaskServiceWrapper2_4.java | 164 +++++++++++++ .../TaskActionController2_4Test.java | 204 ++++++++++++++++ .../TaskDefinitionController2_4Test.java | 177 ++++++++++++++ .../TaskDefinitionResource2_4Test.java | 76 ++++++ 9 files changed, 1043 insertions(+), 17 deletions(-) create mode 100644 omod-2.4/src/main/java/org/openmrs/module/webservices/helper/TaskServiceWrapper2_4.java create mode 100644 omod-2.4/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_4/TaskActionResource2_4.java create mode 100644 omod-2.4/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_4/TaskDefinitionResource2_4.java create mode 100644 omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/MockTaskServiceWrapper2_4.java create mode 100644 omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_4/TaskActionController2_4Test.java create mode 100644 omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_4/TaskDefinitionController2_4Test.java create mode 100644 omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_4/TaskDefinitionResource2_4Test.java diff --git a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/TaskActionResource1_8.java b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/TaskActionResource1_8.java index 2050802f8..78ffc35b9 100644 --- a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/TaskActionResource1_8.java +++ b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/TaskActionResource1_8.java @@ -9,11 +9,9 @@ */ package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8; -import io.swagger.models.Model; -import io.swagger.models.ModelImpl; -import io.swagger.models.properties.ArrayProperty; -import io.swagger.models.properties.BooleanProperty; -import io.swagger.models.properties.StringProperty; +import java.util.ArrayList; +import java.util.Collection; + import org.openmrs.api.APIException; import org.openmrs.module.webservices.docs.swagger.core.property.EnumProperty; import org.openmrs.module.webservices.helper.TaskAction; @@ -34,11 +32,14 @@ import org.openmrs.scheduler.SchedulerException; import org.openmrs.scheduler.TaskDefinition; -import java.util.ArrayList; -import java.util.Collection; +import io.swagger.models.Model; +import io.swagger.models.ModelImpl; +import io.swagger.models.properties.ArrayProperty; +import io.swagger.models.properties.BooleanProperty; +import io.swagger.models.properties.StringProperty; @Resource(name = RestConstants.VERSION_1 + "/taskaction", supportedClass = TaskAction.class, supportedOpenmrsVersions = { - "1.8.*", "1.9.*", "1.10.*", "1.11.*", "1.12.*", "2.0.*", "2.1.*", "2.2.*", "2.3.*", "2.4.*" }) + "1.8.*", "1.9.*", "1.10.*", "1.11.*", "1.12.*", "2.0.*", "2.1.*", "2.2.*", "2.3.*" }) public class TaskActionResource1_8 extends BaseDelegatingResource implements Creatable { private TaskServiceWrapper taskServiceWrapper = new TaskServiceWrapper(); diff --git a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/TaskDefinitionResource1_8.java b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/TaskDefinitionResource1_8.java index 58abc0e43..6512175b9 100644 --- a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/TaskDefinitionResource1_8.java +++ b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/TaskDefinitionResource1_8.java @@ -9,29 +9,30 @@ */ package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8; +import java.util.ArrayList; +import java.util.List; + import org.openmrs.module.webservices.helper.TaskServiceWrapper; -import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter; -import org.openmrs.module.webservices.rest.web.representation.RefRepresentation; -import org.openmrs.module.webservices.rest.web.resource.api.PageableResult; -import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging; -import org.openmrs.scheduler.TaskDefinition; import org.openmrs.module.webservices.rest.web.RequestContext; import org.openmrs.module.webservices.rest.web.RestConstants; +import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter; import org.openmrs.module.webservices.rest.web.annotation.Resource; import org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation; import org.openmrs.module.webservices.rest.web.representation.FullRepresentation; +import org.openmrs.module.webservices.rest.web.representation.RefRepresentation; import org.openmrs.module.webservices.rest.web.representation.Representation; -import org.openmrs.module.webservices.rest.web.resource.impl.MetadataDelegatingCrudResource; +import org.openmrs.module.webservices.rest.web.resource.api.PageableResult; import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription; +import org.openmrs.module.webservices.rest.web.resource.impl.MetadataDelegatingCrudResource; +import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging; import org.openmrs.module.webservices.rest.web.response.ResponseException; -import java.util.ArrayList; -import java.util.List; +import org.openmrs.scheduler.TaskDefinition; /** * Resource for Task Definitions, supporting standard CRUD operations */ @Resource(name = RestConstants.VERSION_1 + "/taskdefinition", supportedClass = TaskDefinition.class, supportedOpenmrsVersions = { - "1.8.*", "1.9.*", "1.10.*", "1.11.*", "1.12.*", "2.0.*", "2.1.*", "2.2.*", "2.3.*", "2.4.*" }) + "1.8.*", "1.9.*", "1.10.*", "1.11.*", "1.12.*", "2.0.*", "2.1.*", "2.2.*", "2.3.*" }) public class TaskDefinitionResource1_8 extends MetadataDelegatingCrudResource { private TaskServiceWrapper taskServiceWrapper = new TaskServiceWrapper(); diff --git a/omod-2.4/src/main/java/org/openmrs/module/webservices/helper/TaskServiceWrapper2_4.java b/omod-2.4/src/main/java/org/openmrs/module/webservices/helper/TaskServiceWrapper2_4.java new file mode 100644 index 000000000..84cdc9a26 --- /dev/null +++ b/omod-2.4/src/main/java/org/openmrs/module/webservices/helper/TaskServiceWrapper2_4.java @@ -0,0 +1,22 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ + +package org.openmrs.module.webservices.helper; + +import org.openmrs.api.context.Context; +import org.openmrs.scheduler.TaskDefinition; + +public class TaskServiceWrapper2_4 extends TaskServiceWrapper { + + public TaskDefinition getTaskByUuid(String uuid) { + return Context.getSchedulerService().getTaskByUuid(uuid); + } + +} diff --git a/omod-2.4/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_4/TaskActionResource2_4.java b/omod-2.4/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_4/TaskActionResource2_4.java new file mode 100644 index 000000000..49248ec4b --- /dev/null +++ b/omod-2.4/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_4/TaskActionResource2_4.java @@ -0,0 +1,217 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ + +package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_4; + +import java.util.ArrayList; +import java.util.Collection; + +import org.openmrs.api.APIException; +import org.openmrs.module.webservices.docs.swagger.core.property.EnumProperty; +import org.openmrs.module.webservices.helper.TaskAction; +import org.openmrs.module.webservices.helper.TaskServiceWrapper2_4; +import org.openmrs.module.webservices.rest.SimpleObject; +import org.openmrs.module.webservices.rest.web.ConversionUtil; +import org.openmrs.module.webservices.rest.web.RequestContext; +import org.openmrs.module.webservices.rest.web.RestConstants; +import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter; +import org.openmrs.module.webservices.rest.web.annotation.Resource; +import org.openmrs.module.webservices.rest.web.representation.Representation; +import org.openmrs.module.webservices.rest.web.resource.api.Creatable; +import org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource; +import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription; +import org.openmrs.module.webservices.rest.web.response.IllegalRequestException; +import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException; +import org.openmrs.module.webservices.rest.web.response.ResponseException; +import org.openmrs.scheduler.SchedulerException; +import org.openmrs.scheduler.TaskDefinition; + +import io.swagger.models.Model; +import io.swagger.models.ModelImpl; +import io.swagger.models.properties.ArrayProperty; +import io.swagger.models.properties.BooleanProperty; +import io.swagger.models.properties.StringProperty; + +@Resource(name = RestConstants.VERSION_1 + + "/taskaction", supportedClass = TaskAction.class, supportedOpenmrsVersions = { "2.4.*" }) +public class TaskActionResource2_4 extends BaseDelegatingResource implements Creatable { + + private TaskServiceWrapper2_4 taskServiceWrapper = new TaskServiceWrapper2_4(); + + public void setTaskServiceWrapper(TaskServiceWrapper2_4 taskServiceWrapper) { + this.taskServiceWrapper = taskServiceWrapper; + } + + @Override + public Object create(SimpleObject post, RequestContext context) throws ResponseException { + TaskAction action = newDelegate(); + setConvertedProperties(action, post, getCreatableProperties(), true); + + Collection taskDefinitions; + if (action.isAllTasks()) { + taskDefinitions = taskServiceWrapper.getRegisteredTasks(); + action.setTasks(new ArrayList(taskDefinitions)); + } else { + taskDefinitions = action.getTasks(); + } + + TaskAction.Action actionType = action.getAction(); + if (actionType != TaskAction.Action.RESCHEDULEALLTASKS) { + if (taskDefinitions == null || taskDefinitions.isEmpty()) { + throw new IllegalRequestException( + "Cannot execute action " + actionType + " on empty set of task definitions."); + } + } + + switch (action.getAction()) { + case SCHEDULETASK: + scheduleTasks(taskDefinitions); + break; + case RESCHEDULETASK: + reScheduleTasks(taskDefinitions); + break; + case DELETE: + deleteTasks(taskDefinitions); + break; + case SHUTDOWNTASK: + shutDownTasks(taskDefinitions); + break; + case RESCHEDULEALLTASKS: + reScheduleAllTasks(); + break; + case RUNTASK: + runTasks(taskDefinitions); + break; + } + return ConversionUtil.convertToRepresentation(action, Representation.DEFAULT); + } + + private void scheduleTasks(Collection taskDefs) { + for (TaskDefinition taskDef : taskDefs) { + try { + taskServiceWrapper.scheduleTask(taskDef); + } catch (SchedulerException e) { + throw new APIException("Errors occurred while scheduling task", e); + } + } + } + + private void shutDownTasks(Collection taskDefs) { + for (TaskDefinition taskDef : taskDefs) { + try { + taskServiceWrapper.shutDownTask(taskDef); + } catch (SchedulerException e) { + throw new APIException("Errors occurred while shutdowning task", e); + } + } + } + + // Stop and start a set of scheduled tasks. + private void reScheduleTasks(Collection taskDefs) { + for (TaskDefinition taskDef : taskDefs) { + try { + taskServiceWrapper.reScheduleTask(taskDef); + } catch (SchedulerException e) { + throw new APIException("Errors occurred while rescheduling task", e); + } + } + } + + // Stop and start all the tasks. + private void reScheduleAllTasks() { + try { + taskServiceWrapper.reScheduleAllTasks(); + } catch (SchedulerException e) { + throw new APIException("Errors occurred while rescheduling all tasks", e); + } + + } + + private void deleteTasks(Collection taskDefs) { + for (TaskDefinition taskDef : taskDefs) { + try { + taskServiceWrapper.deleteTask(taskDef); + } catch (SchedulerException e) { + throw new APIException("Errors occurred while deleting task", e); + } + } + } + + private void runTasks(Collection taskDefs) { + for (TaskDefinition taskDef : taskDefs) { + try { + taskServiceWrapper.runTask(taskDef); + } catch (SchedulerException e) { + throw new APIException("Errors occurred while running task", e); + } + } + } + + @Override + public TaskAction newDelegate() { + return new TaskAction(); + } + + @Override + public TaskAction save(TaskAction delegate) { + throw new UnsupportedOperationException("TaskAction cannot be saved"); + } + + @Override + public DelegatingResourceDescription getRepresentationDescription(Representation rep) { + DelegatingResourceDescription description = new DelegatingResourceDescription(); + description.addProperty("tasks", Representation.REF); + description.addProperty("action", "action"); + return description; + } + + @Override + public TaskAction getByUniqueId(String uniqueId) { + throw new UnsupportedOperationException("TaskAction can not get by id"); + } + + @Override + protected void delete(TaskAction delegate, String reason, RequestContext context) throws ResponseException { + throw new UnsupportedOperationException("TaskAction can not be deleted"); + } + + @Override + public void purge(TaskAction delegate, RequestContext context) throws ResponseException { + throw new UnsupportedOperationException("TaskAction can not be purged"); + } + + @Override + public DelegatingResourceDescription getCreatableProperties() throws ResourceDoesNotSupportOperationException { + DelegatingResourceDescription description = new DelegatingResourceDescription(); + description.addProperty("tasks"); + description.addProperty("allTasks"); + description.addRequiredProperty("action", "action"); + return description; + } + + @Override + public Model getCREATEModel(Representation rep) { + ModelImpl model = new ModelImpl(); + model.property("tasks", new ArrayProperty(new StringProperty())); + model.property("allTasks", new BooleanProperty()); + model.property("action", new EnumProperty(TaskAction.Action.class)); + model.required("action"); + return model; + } + + /** + * Converter does not handle getters starting with 'is' instead of 'get' + */ + @PropertyGetter("allModules") + public Boolean isAllModules(TaskAction action) { + return action.isAllTasks(); + } + +} diff --git a/omod-2.4/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_4/TaskDefinitionResource2_4.java b/omod-2.4/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_4/TaskDefinitionResource2_4.java new file mode 100644 index 000000000..ea34e2ccb --- /dev/null +++ b/omod-2.4/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_4/TaskDefinitionResource2_4.java @@ -0,0 +1,164 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ + +package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_4; + +import java.util.ArrayList; +import java.util.List; + +import org.openmrs.module.webservices.helper.TaskServiceWrapper2_4; +import org.openmrs.module.webservices.rest.web.RequestContext; +import org.openmrs.module.webservices.rest.web.RestConstants; +import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter; +import org.openmrs.module.webservices.rest.web.annotation.Resource; +import org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation; +import org.openmrs.module.webservices.rest.web.representation.FullRepresentation; +import org.openmrs.module.webservices.rest.web.representation.RefRepresentation; +import org.openmrs.module.webservices.rest.web.representation.Representation; +import org.openmrs.module.webservices.rest.web.resource.api.PageableResult; +import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription; +import org.openmrs.module.webservices.rest.web.resource.impl.MetadataDelegatingCrudResource; +import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging; +import org.openmrs.module.webservices.rest.web.response.ResponseException; +import org.openmrs.scheduler.TaskDefinition; + +@Resource(name = RestConstants.VERSION_1 + + "/taskdefinition", supportedClass = TaskDefinition.class, supportedOpenmrsVersions = { "2.4.*" }) +public class TaskDefinitionResource2_4 extends MetadataDelegatingCrudResource { + + private TaskServiceWrapper2_4 taskServiceWrapper = new TaskServiceWrapper2_4(); + + public void setTaskServiceWrapper(TaskServiceWrapper2_4 taskServiceWrapper) { + this.taskServiceWrapper = taskServiceWrapper; + } + + @Override + public TaskDefinition newDelegate() { + return new TaskDefinition(); + } + + @Override + public DelegatingResourceDescription getRepresentationDescription(Representation rep) { + if (rep instanceof DefaultRepresentation) { + DelegatingResourceDescription description = new DelegatingResourceDescription(); + description.addProperty("uuid"); + description.addProperty("name"); + description.addProperty("description"); + description.addProperty("taskClass"); + description.addProperty("startTime"); + description.addProperty("started"); + description.addLink("ref", ".?v=" + RestConstants.REPRESENTATION_REF); + description.addLink("full", ".?v=" + RestConstants.REPRESENTATION_FULL); + description.addSelfLink(); + return description; + } else if (rep instanceof FullRepresentation) { + DelegatingResourceDescription description = new DelegatingResourceDescription(); + description.addProperty("uuid"); + description.addProperty("name"); + description.addProperty("description"); + description.addProperty("taskClass"); + description.addProperty("startTime"); + description.addProperty("lastExecutionTime"); + description.addProperty("repeatInterval"); + description.addProperty("startOnStartup"); + description.addProperty("startTimePattern"); + description.addProperty("started"); + description.addProperty("properties"); + description.addSelfLink(); + description.addLink("ref", ".?v=" + RestConstants.REPRESENTATION_REF); + return description; + } else if (rep instanceof RefRepresentation) { + DelegatingResourceDescription description = new DelegatingResourceDescription(); + description.addProperty("uuid"); + description.addProperty("name"); + description.addProperty("description"); + description.addProperty("taskClass"); + description.addSelfLink(); + return description; + } + return null; + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getCreatableProperties() + */ + @Override + public DelegatingResourceDescription getCreatableProperties() { + DelegatingResourceDescription description = new DelegatingResourceDescription(); + description.addRequiredProperty("name"); + description.addRequiredProperty("description"); + description.addRequiredProperty("taskClass"); + description.addRequiredProperty("startTime"); + description.addRequiredProperty("repeatInterval"); + description.addRequiredProperty("startOnStartup"); + description.addRequiredProperty("properties"); + return description; + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#save(java.lang.Object) + */ + @Override + public TaskDefinition save(TaskDefinition taskDefinition) { + taskServiceWrapper.saveTaskDefinition(taskDefinition); + return taskDefinition; + } + + @Override + public TaskDefinition getByUniqueId(String uniqueId) { + TaskDefinition taskDefinition = taskServiceWrapper.getTaskByName(uniqueId); + if (taskDefinition == null) { + taskDefinition = taskServiceWrapper.getTaskByUuid(uniqueId); + } + return taskDefinition; + } + + @Override + public void purge(TaskDefinition delegate, RequestContext context) throws ResponseException { + throw new UnsupportedOperationException("TaskAction cannot be purged"); + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#doGetAll(org.openmrs.module.webservices.rest.web.RequestContext) + */ + @Override + public NeedsPaging doGetAll(RequestContext context) throws ResponseException { + return new NeedsPaging(new ArrayList(taskServiceWrapper.getRegisteredTasks()), + context); + } + + @PropertyGetter("uuid") + public static String getUuid(TaskDefinition instance) { + return instance.getUuid(); + } + + @PropertyGetter("display") + public static String getDisplay(TaskDefinition instance) { + return instance.getName(); + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#doSearch(org.openmrs.module.webservices.rest.web.RequestContext) + * It will return only the scheduled tasks, if query contains string as + * scheduled otherwise it will return the registered tasks + */ + @Override + protected PageableResult doSearch(RequestContext context) { + String query = context.getParameter("q"); + List taskDefinitions = null; + if (query == "registered") { + taskDefinitions = (ArrayList) taskServiceWrapper.getRegisteredTasks(); + } else { + taskDefinitions = (ArrayList) taskServiceWrapper.getScheduledTasks(); + } + return new NeedsPaging(taskDefinitions, context); + } + +} diff --git a/omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/MockTaskServiceWrapper2_4.java b/omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/MockTaskServiceWrapper2_4.java new file mode 100644 index 000000000..934f56ce5 --- /dev/null +++ b/omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/MockTaskServiceWrapper2_4.java @@ -0,0 +1,164 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ + +package org.openmrs.module.webservices.rest.web; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.openmrs.module.webservices.helper.TaskServiceWrapper2_4; +import org.openmrs.scheduler.SchedulerException; +import org.openmrs.scheduler.TaskDefinition; + +public class MockTaskServiceWrapper2_4 extends TaskServiceWrapper2_4{ + +public List registeredTasks = new ArrayList(); + + public List scheduledTasks = new ArrayList(); + + /** + * It will find the respected taskDefinition for the given task Id + * + * @param id - used to find the taskDefinition from the registeredTasks + * @return - return the taskDefinition if it found, else return the null + */ + @Override + public TaskDefinition getTaskById(Integer id) { + TaskDefinition taskFound = null; + for (TaskDefinition taskDef : registeredTasks) { + if (id == taskDef.getId()) { + taskFound = taskDef; + break; + } + } + return taskFound; + } + + + /** + * It will find the respected taskDefinition for the given task uuid + * + * @param uuid - used to find the taskDefinition from the registeredTasks + * @return - return the taskDefinition if it found, else return the null + */ + + @Override + public TaskDefinition getTaskByUuid(String uuid) { + TaskDefinition taskFound = null; + for (TaskDefinition taskDef : registeredTasks) { + if (uuid == taskDef.getUuid()) { + taskFound = taskDef; + break; + } + } + return taskFound; + } + + /** + * It will find the respected taskDefinition for the given task Name + * + * @param taskName - used to find the taskDefinition from the registeredTasks + * @return - return the taskDefinition if it found, else return the null + */ + @Override + public TaskDefinition getTaskByName(String taskName) { + TaskDefinition taskFound = null; + for (TaskDefinition taskDef : registeredTasks) { + if (taskName.equals(taskDef.getName())) { + taskFound = taskDef; + break; + } + } + return taskFound; + } + + @Override + public Collection getRegisteredTasks() { + return registeredTasks; + } + + @Override + public Collection getScheduledTasks() { + return scheduledTasks; + } + + /** + * Mock Function : It will add the taskDefinition to the registeredTasks List + * + * @param task will contain the taskDefinition to be saved + */ + @Override + public void saveTaskDefinition(TaskDefinition task) { + if (!registeredTasks.contains(task)) { + registeredTasks.add(task); + } + } + + /** + * Mock Function : It will remove the taskDefinition from the registeredTasks List and Scheduled + * List + * + * @param task will contain the taskDefinition to be deleted + * @throws SchedulerException + */ + @Override + public void deleteTask(TaskDefinition task) throws SchedulerException { + if (scheduledTasks.contains(task)) { + scheduledTasks.remove(task); + } + if (registeredTasks.contains(task)) { + registeredTasks.remove(task); + } + } + + /** + * Mock Function : It will add the taskDefinition to the scheduledTasks List + * + * @param task contains the taskDefinition to be scheduled + * @throws SchedulerException + */ + @Override + public void scheduleTask(TaskDefinition task) throws SchedulerException { + if (!scheduledTasks.contains(task)) { + scheduledTasks.add(task); + } + } + + /** + * Mock Function : It will remove the taskDefinition from the scheduledTasks List + * + * @param task contains the taskDefinition to be shutdown + * @throws SchedulerException + */ + @Override + public void shutDownTask(TaskDefinition task) throws SchedulerException { + if (scheduledTasks.contains(task)) { + scheduledTasks.remove(task); + } + + } + + /** + * Mock Function : it will remove the taskDefinition from the scheduledTasks and add it again + * + * @param task contains the taskDefinition to be re-scheduled + * @throws SchedulerException + */ + @Override + public void reScheduleTask(TaskDefinition task) throws SchedulerException { + if (scheduledTasks.contains(task)) { + scheduledTasks.remove(task); + } + scheduledTasks.add(task); + } + + +} diff --git a/omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_4/TaskActionController2_4Test.java b/omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_4/TaskActionController2_4Test.java new file mode 100644 index 000000000..5e19ccc20 --- /dev/null +++ b/omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_4/TaskActionController2_4Test.java @@ -0,0 +1,204 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ + +package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs2_4; + +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.IsNot.not; + +import java.util.Arrays; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.openmrs.module.webservices.helper.TaskAction; +import org.openmrs.module.webservices.rest.web.MockTaskServiceWrapper2_4; +import org.openmrs.module.webservices.rest.web.api.RestService; +import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest; +import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_4.TaskActionResource2_4; +import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_4.TaskDefinitionResource2_4; +import org.openmrs.scheduler.Task; +import org.openmrs.scheduler.TaskDefinition; +import org.springframework.beans.factory.annotation.Autowired; + +public class TaskActionController2_4Test extends MainResourceControllerTest { + + @Autowired + RestService restService; + + static int count = 1; + + private TaskDefinition testTask = new TaskDefinition(1, "TestTask", "TestTask Description", + "org.openmrs.scheduler.tasks.TestTask"); + + private TaskDefinition testDummyTask = new TaskDefinition(5, "TestDummy", "TestTask Description", + DummyTask.class.getName()); + + private TaskDefinition tempTask = new TaskDefinition(3, "TempTask", "TempTask Description", + "org.openmrs.scheduler.tasks.TestTask"); + + private MockTaskServiceWrapper2_4 mockTaskServiceWrapper = new MockTaskServiceWrapper2_4(); + + @Before + public void setUp() throws Exception { + mockTaskServiceWrapper.registeredTasks.addAll(Arrays.asList(testTask, tempTask, testDummyTask)); + + TaskActionResource2_4 taskActionResource = (TaskActionResource2_4) restService + .getResourceBySupportedClass(TaskAction.class); + taskActionResource.setTaskServiceWrapper(mockTaskServiceWrapper); + + TaskDefinitionResource2_4 taskResource = (TaskDefinitionResource2_4) restService + .getResourceBySupportedClass(TaskDefinition.class); + taskResource.setTaskServiceWrapper(mockTaskServiceWrapper); + } + + @Test + public void shouldScheduleTask() throws Exception { + // sanity check + assertThat(mockTaskServiceWrapper.scheduledTasks, not(hasItem(testTask))); + deserialize(handle( + newPostRequest(getURI(), "{\"action\": \"scheduletask\", \"tasks\":[\"" + getTestTaskName() + "\"]}"))); + assertThat(mockTaskServiceWrapper.scheduledTasks, hasItem(testTask)); + } + + @Test + public void shouldShutdownTask() throws Exception { + mockTaskServiceWrapper.scheduledTasks.add(testTask); + assertThat(mockTaskServiceWrapper.scheduledTasks, hasItem(testTask)); + deserialize(handle( + newPostRequest(getURI(), "{\"action\": \"shutdowntask\", \"tasks\":[\"" + getTestTaskName() + "\"]}"))); + assertThat(mockTaskServiceWrapper.scheduledTasks, not(hasItem(testTask))); + } + + @Test + public void scheduleTask_shouldDoNothingIfTaskAlreadyScheduled() throws Exception { + mockTaskServiceWrapper.scheduledTasks.add(testTask); + // sanity check + assertThat(mockTaskServiceWrapper.scheduledTasks, hasItem(testTask)); + assertThat(mockTaskServiceWrapper.registeredTasks, hasItem(testTask)); + deserialize(handle( + newPostRequest(getURI(), "{\"action\": \"scheduletask\", \"tasks\":[\"" + getTestTaskName() + "\"]}"))); + // check if state preserved + assertThat(mockTaskServiceWrapper.scheduledTasks, hasItem(testTask)); + assertThat(mockTaskServiceWrapper.registeredTasks, hasItem(testTask)); + } + + @Test + public void shutdownTask_shouldDoNothingIfTaskAlreadyShutdown() throws Exception { + // sanity check + assertThat(mockTaskServiceWrapper.scheduledTasks, not(hasItem(testTask))); + assertThat(mockTaskServiceWrapper.registeredTasks, hasItem(testTask)); + deserialize(handle( + newPostRequest(getURI(), "{\"action\": \"shutdowntask\", \"tasks\":[\"" + getTestTaskName() + "\"]}"))); + // check if state preserved + assertThat(mockTaskServiceWrapper.scheduledTasks, not(hasItem(testTask))); + assertThat(mockTaskServiceWrapper.registeredTasks, hasItem(testTask)); + } + + @Test + public void shouldDeleteTask() throws Exception { + // sanity check + assertThat(mockTaskServiceWrapper.registeredTasks, hasItem(testTask)); + deserialize(handle( + newPostRequest(getURI(), "{\"action\": \"delete\", \"tasks\":[\"" + getTestTaskName() + "\"]}"))); + assertThat(mockTaskServiceWrapper.registeredTasks, not(hasItem(testTask))); + } + + @Test + public void shouldRunTask() throws Exception { + // sanity check + assertThat(mockTaskServiceWrapper.registeredTasks, hasItem(testDummyTask)); + Assert.assertEquals(mockTaskServiceWrapper.getRegisteredTasks().size(), 3); + // count before manual execution + int countBefore = count; + deserialize(handle( + newPostRequest(getURI(), "{\"action\": \"runtask\", \"tasks\":[\"" + getTestDummyTaskName() + "\"]}"))); + assertThat(mockTaskServiceWrapper.registeredTasks, hasItem(testDummyTask)); + Assert.assertEquals(mockTaskServiceWrapper.getRegisteredTasks().size(), 3); + // count after manual execution + int countAfter = count; + Assert.assertEquals(++countBefore, countAfter); + } + + @Override + @Test(expected = Exception.class) + public void shouldGetDefaultByUuid() throws Exception { + super.shouldGetDefaultByUuid(); + } + + @Override + @Test(expected = Exception.class) + public void shouldGetRefByUuid() throws Exception { + super.shouldGetRefByUuid(); + } + + @Override + @Test(expected = Exception.class) + public void shouldGetFullByUuid() throws Exception { + super.shouldGetFullByUuid(); + } + + @Override + @Test(expected = Exception.class) + public void shouldGetAll() throws Exception { + super.shouldGetAll(); + } + + @Override + public String getURI() { + return "taskaction"; + } + + @Override + public String getUuid() { + return null; + } + + public String getTestTaskName() { + return "TestTask"; + } + + public String getTestDummyTaskName() { + return "TestDummy"; + } + + @Override + public long getAllCount() { + return 0; + } + + public static class DummyTask implements Task { + + @Override + public void execute() { + count = count + 1; + } + + @Override + public TaskDefinition getTaskDefinition() { + return null; + } + + @Override + public void initialize(TaskDefinition definition) { + } + + @Override + public boolean isExecuting() { + return false; + } + + @Override + public void shutdown() { + } + } + +} diff --git a/omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_4/TaskDefinitionController2_4Test.java b/omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_4/TaskDefinitionController2_4Test.java new file mode 100644 index 000000000..5e86bee0c --- /dev/null +++ b/omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_4/TaskDefinitionController2_4Test.java @@ -0,0 +1,177 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ + +package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs2_4; + +import java.util.List; + +import org.apache.commons.beanutils.PropertyUtils; +import org.codehaus.jackson.map.ObjectMapper; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.openmrs.module.webservices.rest.SimpleObject; +import org.openmrs.module.webservices.rest.test.Util; +import org.openmrs.module.webservices.rest.web.MockTaskServiceWrapper2_4; +import org.openmrs.module.webservices.rest.web.api.RestService; +import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest; +import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_4.TaskDefinitionResource2_4; +import org.openmrs.scheduler.TaskDefinition; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.web.bind.annotation.RequestMethod; + +public class TaskDefinitionController2_4Test extends MainResourceControllerTest { + + @Autowired + RestService restService; + + private TaskDefinition testTask = new TaskDefinition(1, "TestTask", "TestTask Description", + "org.openmrs.scheduler.tasks.TestTask"); + + private MockTaskServiceWrapper2_4 mockTaskServiceWrapper = new MockTaskServiceWrapper2_4(); + + @Before + public void setUp() throws Exception { + testTask.setRepeatInterval(10L); + testTask.setStartOnStartup(true); + testTask.setProperty("propertyKey", "propertyValue"); + + mockTaskServiceWrapper.registeredTasks.add(testTask); + + TaskDefinitionResource2_4 taskResource = (TaskDefinitionResource2_4) restService + .getResourceBySupportedClass(TaskDefinition.class); + taskResource.setTaskServiceWrapper(mockTaskServiceWrapper); + } + + /** + * Get all registered tasks - Used to test for registered tasks. + * + * @throws Exception + */ + @Test + @Override + public void shouldGetAll() throws Exception { + MockHttpServletRequest req = request(RequestMethod.GET, getURI()); + SimpleObject resultTasks = deserialize(handle(req)); + List results = Util.getResultsList(resultTasks); + Assert.assertNotNull(resultTasks); + Assert.assertEquals(results.size(), getAllCount()); + } + + /** + * Get all scheduled tasks - Used to test for scheduled tasks. + * + * @throws Exception + */ + @Test + public void shouldGetAllScheduled() throws Exception { + MockHttpServletRequest req = request(RequestMethod.GET, getURI()); + req.addParameter("q", "scheduled"); + SimpleObject resultTasks = deserialize(handle(req)); + List results = Util.getResultsList(resultTasks); + Assert.assertNotNull(resultTasks); + Assert.assertEquals(results.size(), getAllScheduledCount()); + } + + @Test + public void shouldGetTaskByUuid() throws Exception { + MockHttpServletRequest req = request(RequestMethod.GET, getURI() + "/" + getUuid()); + SimpleObject result = deserialize(handle(req)); + Assert.assertNotNull(result.get("name")); + Assert.assertNotNull(result.get("description")); + Assert.assertNotNull(result.get("taskClass")); + } + + @Test + @Override + public void shouldGetFullByUuid() throws Exception { + MockHttpServletRequest req = request(RequestMethod.GET, getURI() + "/" + getUuid()); + req.addParameter("v", "full"); + + SimpleObject result = deserialize(handle(req)); + Assert.assertNotNull(PropertyUtils.getProperty(result, "name")); + Assert.assertNotNull(PropertyUtils.getProperty(result, "description")); + Assert.assertNotNull(PropertyUtils.getProperty(result, "taskClass")); + Assert.assertNotNull(PropertyUtils.getProperty(result, "repeatInterval")); + Assert.assertNotNull(PropertyUtils.getProperty(result, "startOnStartup")); + Assert.assertNotNull(PropertyUtils.getProperty(result, "started")); + Assert.assertNotNull(PropertyUtils.getProperty(result, "properties")); + } + + @Test + @Override + public void shouldGetDefaultByUuid() throws Exception { + MockHttpServletRequest req = request(RequestMethod.GET, getURI() + "/" + getUuid()); + SimpleObject result = deserialize(handle(req)); + Assert.assertNotNull(PropertyUtils.getProperty(result, "name")); + Assert.assertNotNull(PropertyUtils.getProperty(result, "description")); + Assert.assertNotNull(PropertyUtils.getProperty(result, "taskClass")); + Assert.assertNotNull(PropertyUtils.getProperty(result, "startTime")); + Assert.assertNotNull(PropertyUtils.getProperty(result, "started")); + } + + @Test + @Override + public void shouldGetRefByUuid() throws Exception { + MockHttpServletRequest req = request(RequestMethod.GET, getURI() + "/" + getUuid()); + req.addParameter("v", "ref"); + SimpleObject result = deserialize(handle(req)); + Assert.assertNotNull(PropertyUtils.getProperty(result, "name")); + Assert.assertNotNull(PropertyUtils.getProperty(result, "description")); + Assert.assertNotNull(PropertyUtils.getProperty(result, "taskClass")); + } + + @Test + public void shouldSaveTaskDefinition() throws Exception { + SimpleObject mockTask = new SimpleObject(); + mockTask.add("name", "MockTask"); + mockTask.add("description", "MockTask Description"); + mockTask.add("taskClass", "org.openmrs.scheduler.tasks.TestTask"); + mockTask.add("startTime", "2017-08-28T23:59:59.000+0530"); + mockTask.add("repeatInterval", "10"); + mockTask.add("startOnStartup", false); + mockTask.add("properties", null); + String json = new ObjectMapper().writeValueAsString(mockTask); + + Assert.assertNull(mockTaskServiceWrapper.getTaskByName("MockTask")); + MockHttpServletRequest req = request(RequestMethod.POST, getURI()); + req.setContent(json.getBytes()); + deserialize(handle(req)); + Assert.assertEquals(mockTaskServiceWrapper.getTaskByName("MockTask").getName(), "MockTask"); + } + + @Override + public String getURI() { + return "taskdefinition"; + } + + @Override + public String getUuid() { + return getTestTaskName(); + } + + private String getTestTaskName() { + return "TestTask"; + } + + @Override + public long getAllCount() { + return getAllRegisteredCount(); + } + + public long getAllRegisteredCount() { + return mockTaskServiceWrapper.registeredTasks.size(); + } + + public long getAllScheduledCount() { + return mockTaskServiceWrapper.scheduledTasks.size(); + } +} diff --git a/omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_4/TaskDefinitionResource2_4Test.java b/omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_4/TaskDefinitionResource2_4Test.java new file mode 100644 index 000000000..c37759fd3 --- /dev/null +++ b/omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_4/TaskDefinitionResource2_4Test.java @@ -0,0 +1,76 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ + + +package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_4; + +import java.util.Date; + +import org.openmrs.module.webservices.rest.web.RestTestConstants1_8; +import org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResourceTest; +import org.openmrs.scheduler.TaskDefinition; + +public class TaskDefinitionResource2_4Test extends BaseDelegatingResourceTest{ + + @Override + public TaskDefinition newObject() { + //Create test Task Definition + TaskDefinition taskDefinition = new TaskDefinition(); + taskDefinition.setId(1); + taskDefinition.setName("TestTask"); + taskDefinition.setStarted(false); + taskDefinition.setRepeatInterval(10L); + taskDefinition.setStartTime(new Date()); + taskDefinition.setTaskClass("org.openmrs.scheduler.tasks.TestTask"); + return taskDefinition; + } + + @Override + public String getDisplayProperty() { + return "TestTask"; + } + + @Override + public String getUuidProperty() { + return RestTestConstants1_8.TASK_DEFINITION_UUID; + } + + @Override + public void validateDefaultRepresentation() throws Exception { + assertPropEquals("uuid", getObject().getUuid()); + assertPropEquals("name", getObject().getName()); + assertPropEquals("description", getObject().getDescription()); + assertPropEquals("startTime", getObject().getStartTime()); + assertPropEquals("started", getObject().getStarted()); + assertPropEquals("taskClass", getObject().getTaskClass()); + } + + @Override + public void validateFullRepresentation() throws Exception { + assertPropEquals("uuid", getObject().getUuid()); + assertPropEquals("name", getObject().getName()); + assertPropEquals("description", getObject().getDescription()); + assertPropEquals("startTime", getObject().getStartTime()); + assertPropEquals("started", getObject().getStarted()); + assertPropEquals("taskClass", getObject().getTaskClass()); + assertPropEquals("lastExecutionTime", getObject().getLastExecutionTime()); + assertPropEquals("startTimePattern", getObject().getStartTimePattern()); + assertPropEquals("properties", getObject().getProperties()); + } + + @Override + public void validateRefRepresentation() throws Exception { + assertPropEquals("uuid", getObject().getUuid()); + assertPropEquals("name", getObject().getName()); + assertPropEquals("description", getObject().getDescription()); + assertPropEquals("taskClass", getObject().getTaskClass()); + } + +}