From 1bb91dee635e2314d685072d4c1c3c143b885dda Mon Sep 17 00:00:00 2001 From: suthagar23 Date: Wed, 30 Aug 2017 18:20:32 +0530 Subject: [PATCH] TaskDefinition and TaskAction REST Implementation update TaskDefinition and TaskAction Implementation --- .../openmrs1_8/TaskActionResource1_8.java | 197 ++++++++++++++++++ .../openmrs1_8/TaskDefinitionResource1_8.java | 165 +++++++++++++++ .../rest/web/MockTaskServiceWrapper.java | 144 +++++++++++++ .../rest/web/RestTestConstants1_8.java | 1 + .../TaskActionController1_8Test.java | 146 +++++++++++++ .../TaskDefinitionController1_8Test.java | 176 ++++++++++++++++ .../TaskDefinitionResource1_8Test.java | 73 +++++++ .../module/webservices/helper/TaskAction.java | 61 ++++++ .../helper/TaskServiceWrapper.java | 99 +++++++++ 9 files changed, 1062 insertions(+) create mode 100644 omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/TaskActionResource1_8.java create mode 100644 omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/TaskDefinitionResource1_8.java create mode 100644 omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/MockTaskServiceWrapper.java create mode 100644 omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/TaskActionController1_8Test.java create mode 100644 omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/TaskDefinitionController1_8Test.java create mode 100644 omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/TaskDefinitionResource1_8Test.java create mode 100644 omod-common/src/main/java/org/openmrs/module/webservices/helper/TaskAction.java create mode 100644 omod-common/src/main/java/org/openmrs/module/webservices/helper/TaskServiceWrapper.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 new file mode 100644 index 000000000..3a6bfae34 --- /dev/null +++ b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/TaskActionResource1_8.java @@ -0,0 +1,197 @@ +/** + * 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.openmrs1_8; + +import io.swagger.models.Model; +import org.openmrs.api.APIException; +import org.openmrs.module.webservices.helper.TaskAction; +import org.openmrs.module.webservices.helper.TaskServiceWrapper; +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 java.util.ArrayList; +import java.util.Collection; + +@Resource(name = RestConstants.VERSION_1 + "/taskaction", supportedClass = TaskAction.class, supportedOpenmrsVersions = { + "1.8.*", "1.9.*", "1.10.*", "1.11.*", "1.12.*", "2.0.*", "2.1.*" }) +public class TaskActionResource1_8 extends BaseDelegatingResource implements Creatable { + + private TaskServiceWrapper taskServiceWrapper = new TaskServiceWrapper(); + + public void setTaskServiceWrapper(TaskServiceWrapper 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; + } + 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); + } + } + } + + @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) { + return null; + } + + /** + * 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-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 new file mode 100644 index 000000000..9a4dec485 --- /dev/null +++ b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/TaskDefinitionResource1_8.java @@ -0,0 +1,165 @@ +/** + * 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.openmrs1_8; + +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.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.Representation; +import org.openmrs.module.webservices.rest.web.resource.impl.MetadataDelegatingCrudResource; +import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription; +import org.openmrs.module.webservices.rest.web.response.ResponseException; +import java.util.ArrayList; +import java.util.List; + +/** + * 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.*" }) +public class TaskDefinitionResource1_8 extends MetadataDelegatingCrudResource { + + private TaskServiceWrapper taskServiceWrapper = new TaskServiceWrapper(); + + public void setTaskServiceWrapper(TaskServiceWrapper 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.getTaskById(Integer.parseInt(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-1.8/src/test/java/org/openmrs/module/webservices/rest/web/MockTaskServiceWrapper.java b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/MockTaskServiceWrapper.java new file mode 100644 index 000000000..38ac2fdb5 --- /dev/null +++ b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/MockTaskServiceWrapper.java @@ -0,0 +1,144 @@ +/** + * 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 org.openmrs.module.webservices.helper.TaskServiceWrapper; +import org.openmrs.scheduler.SchedulerException; +import org.openmrs.scheduler.TaskDefinition; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** + * This is the Mock TaskServiceWrapper class which will act as a TaskService for the test cases + */ +public class MockTaskServiceWrapper extends TaskServiceWrapper { + + 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 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-1.8/src/test/java/org/openmrs/module/webservices/rest/web/RestTestConstants1_8.java b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/RestTestConstants1_8.java index 39eba5207..ef9335a0c 100644 --- a/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/RestTestConstants1_8.java +++ b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/RestTestConstants1_8.java @@ -99,4 +99,5 @@ public class RestTestConstants1_8 { public static final String PATIENT_STATE_UUID = "ea89deaa-23cc-4840-92fe-63d199c37e4c"; + public static final String TASK_DEFINITION_UUID = "266a2ff1-f0b1-45f4-ad04-2ce952a27920"; } diff --git a/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/TaskActionController1_8Test.java b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/TaskActionController1_8Test.java new file mode 100644 index 000000000..48ef43c36 --- /dev/null +++ b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/TaskActionController1_8Test.java @@ -0,0 +1,146 @@ +/** + * 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.openmrs1_8; + +import org.junit.Before; +import org.junit.Test; +import org.openmrs.module.webservices.helper.TaskAction; +import org.openmrs.module.webservices.rest.web.MockTaskServiceWrapper; +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.openmrs1_8.TaskActionResource1_8; +import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.TaskDefinitionResource1_8; +import org.openmrs.scheduler.TaskDefinition; +import org.springframework.beans.factory.annotation.Autowired; +import java.util.Arrays; +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.IsNot.not; + +public class TaskActionController1_8Test extends MainResourceControllerTest { + + @Autowired + RestService restService; + + private TaskDefinition testTask = new TaskDefinition(1, "TestTask", "TestTask Description", + "org.openmrs.scheduler.tasks.TestTask"); + + private TaskDefinition tempTask = new TaskDefinition(3, "TempTask", "TempTask Description", + "org.openmrs.scheduler.tasks.TestTask"); + + private MockTaskServiceWrapper mockTaskServiceWrapper = new MockTaskServiceWrapper(); + + @Before + public void setUp() throws Exception { + mockTaskServiceWrapper.registeredTasks.addAll(Arrays.asList(testTask, tempTask)); + + TaskActionResource1_8 taskActionResource = (TaskActionResource1_8) restService + .getResourceBySupportedClass(TaskAction.class); + taskActionResource.setTaskServiceWrapper(mockTaskServiceWrapper); + + TaskDefinitionResource1_8 taskResource = (TaskDefinitionResource1_8) 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))); + } + + @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"; + } + + @Override + public long getAllCount() { + return 0; + } +} diff --git a/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/TaskDefinitionController1_8Test.java b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/TaskDefinitionController1_8Test.java new file mode 100644 index 000000000..bef53844b --- /dev/null +++ b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/TaskDefinitionController1_8Test.java @@ -0,0 +1,176 @@ +/** + * 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.openmrs1_8; + +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.MockTaskServiceWrapper; +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.openmrs1_8.TaskDefinitionResource1_8; +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; +import java.util.List; + +public class TaskDefinitionController1_8Test extends MainResourceControllerTest { + + @Autowired + RestService restService; + + private TaskDefinition testTask = new TaskDefinition(1, "TestTask", "TestTask Description", + "org.openmrs.scheduler.tasks.TestTask"); + + private MockTaskServiceWrapper mockTaskServiceWrapper = new MockTaskServiceWrapper(); + + @Before + public void setUp() throws Exception { + testTask.setRepeatInterval(10L); + testTask.setStartOnStartup(true); + testTask.setProperty("propertyKey", "propertyValue"); + + mockTaskServiceWrapper.registeredTasks.add(testTask); + + TaskDefinitionResource1_8 taskResource = (TaskDefinitionResource1_8) 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-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/TaskDefinitionResource1_8Test.java b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/TaskDefinitionResource1_8Test.java new file mode 100644 index 000000000..06b450563 --- /dev/null +++ b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/TaskDefinitionResource1_8Test.java @@ -0,0 +1,73 @@ +/** + * 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.openmrs1_8; + +import org.openmrs.module.webservices.rest.web.RestTestConstants1_8; +import org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResourceTest; +import org.openmrs.scheduler.TaskDefinition; + +import java.util.Date; + +public class TaskDefinitionResource1_8Test 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()); + } +} diff --git a/omod-common/src/main/java/org/openmrs/module/webservices/helper/TaskAction.java b/omod-common/src/main/java/org/openmrs/module/webservices/helper/TaskAction.java new file mode 100644 index 000000000..9b2e0e8b2 --- /dev/null +++ b/omod-common/src/main/java/org/openmrs/module/webservices/helper/TaskAction.java @@ -0,0 +1,61 @@ +/** + * 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.scheduler.TaskDefinition; + +import java.util.List; + +public class TaskAction { + + public enum Action { + SCHEDULETASK, SHUTDOWNTASK, RESCHEDULETASK, RESCHEDULEALLTASKS, DELETE; + } + + public TaskAction() { + } + + public TaskAction(Action action, List tasks, boolean startAll) { + this.tasks = tasks; + this.action = action; + this.allTasks = startAll; + } + + private List tasks; + + private Action action; + + private boolean allTasks; + + public void setTasks(List tasks) { + this.tasks = tasks; + } + + public List getTasks() { + return tasks; + } + + public void setAction(Action type) { + this.action = type; + } + + public Action getAction() { + return action; + } + + public boolean isAllTasks() { + return allTasks; + } + + public void setAllModules(boolean allTasks) { + this.allTasks = allTasks; + } + +} diff --git a/omod-common/src/main/java/org/openmrs/module/webservices/helper/TaskServiceWrapper.java b/omod-common/src/main/java/org/openmrs/module/webservices/helper/TaskServiceWrapper.java new file mode 100644 index 000000000..ea140d500 --- /dev/null +++ b/omod-common/src/main/java/org/openmrs/module/webservices/helper/TaskServiceWrapper.java @@ -0,0 +1,99 @@ +/** + * 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.scheduler.TaskDefinition; +import org.openmrs.scheduler.SchedulerException; +import org.openmrs.api.context.Context; + +import java.util.Collection; + +/** + * Wrapping the required task information between related resource and SchedulerService + */ +public class TaskServiceWrapper { + + public TaskDefinition getTaskById(Integer id) { + return Context.getSchedulerService().getTask(id); + } + + public TaskDefinition getTaskByName(String taskName) { + return Context.getSchedulerService().getTaskByName(taskName); + } + + public Collection getScheduledTasks() { + return Context.getSchedulerService().getScheduledTasks(); + } + + public Collection getRegisteredTasks() { + return Context.getSchedulerService().getRegisteredTasks(); + } + + /** + * Save the new task in the service + * + * @param task will contain the taskDefinition to be saved + */ + public void saveTaskDefinition(TaskDefinition task) { + Context.getSchedulerService().saveTaskDefinition(task); + } + + /** + * It will delete the task from the service + * + * @param task will contain the taskDefinition to be deleted + * @throws SchedulerException - It will throw in case of any SchedulerService exceptions + */ + public void deleteTask(TaskDefinition task) throws SchedulerException { + Context.getSchedulerService().deleteTask(task.getId()); + } + + /** + * It will schedule a task which is registered in the service + * + * @param task contains the taskDefinition to be scheduled + * @throws SchedulerException - It will throw in case of any SchedulerService exceptions + */ + public void scheduleTask(TaskDefinition task) throws SchedulerException { + Context.getSchedulerService().scheduleTask(task); + + } + + /** + * It will shutdown a task which is scheduled in the service + * + * @param task contains the taskDefinition to be shutdown + * @throws SchedulerException - It will throw in case of any SchedulerService exceptions + */ + public void shutDownTask(TaskDefinition task) throws SchedulerException { + Context.getSchedulerService().shutdownTask(task); + } + + /** + * It will re-schedule a task which is registered in the service + * + * @param task contains the taskDefinition to be re-scheduled + * @throws SchedulerException - It will throw in case of any SchedulerService exceptions + */ + public void reScheduleTask(TaskDefinition task) throws SchedulerException { + Context.getSchedulerService().rescheduleTask(task); + } + + /** + * It will re-schedule all the tasks which are registered in the service + * + * @throws SchedulerException - It will throw in case of any SchedulerService exceptions + */ + public void reScheduleAllTasks() throws SchedulerException { + Context.getSchedulerService().rescheduleAllTasks(); + } + +}