Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RESTWS-673] Scheduler REST Implementation #293

Merged
merged 1 commit into from
Aug 30, 2017

Conversation

suthagar23
Copy link
Member

Created TaskDefinitionResource1_8 for the task REST implementation

@dkayiwa
Copy link
Member

dkayiwa commented Aug 16, 2017

Can you add some tests?

@suthagar23
Copy link
Member Author

@dkayiwa Yah Sure @dkayiwa

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.3%) to 45.863% when pulling cbd8195 on suthagar23:RESTWS-673 into e12f8c7 on openmrs:master.

@suthagar23 suthagar23 changed the title [RESTWS-673] Initial commit for the TaskDefinition Resource [RESTWS-673] Scheduler REST Implementation Aug 21, 2017
@gayanW
Copy link
Member

gayanW commented Aug 24, 2017

I just ran the tests :) Tests in TaskActionController1_8Test.java fail.

@suthagar23 suthagar23 force-pushed the RESTWS-673 branch 2 times, most recently from 58e1557 to 7c082ef Compare August 24, 2017 19:56
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.05%) to 46.131% when pulling 7c082ef on suthagar23:RESTWS-673 into e12f8c7 on openmrs:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.05%) to 46.131% when pulling 7c082ef on suthagar23:RESTWS-673 into e12f8c7 on openmrs:master.

// @Override
// public DelegatingResourceDescription getUpdatableProperties() {
// DelegatingResourceDescription description = new DelegatingResourceDescription();
// description.addRequiredProperty("name");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed those Comments

*/
@Override
public TaskDefinition save(TaskDefinition taskDefinition) {
//Context.getSchedulerService().saveTaskDefinition(taskDefinition);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed that line

schedulerService.scheduleTask(task);
}
catch (SchedulerException e) {
throw new UnsupportedOperationException();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this really mean that the operation is not supported? The same applies elsewhere.

Copy link
Member Author

@suthagar23 suthagar23 Aug 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed unwanted Catch Exceptions

*/
package org.openmrs.module.webservices.helper;

import org.openmrs.scheduler.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored :-)

// ensure they're not trying to modify the REST module
for (TaskDefinition taskDef : taskDefinitions) {
// if they specified a module that's not loaded, it will show up here as null
if (taskDef == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does taskDefinitions become a collection of nulls?

@Autowired
RestService restService;

private TaskDefinition TestTask = new TaskDefinition(1, "TestTask", "TestTask Description",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our convention is to use variable names that start with lower case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored


MockTaskFactoryWrapper mockTaskFactory = new MockTaskFactoryWrapper();

String TestTaskMockJson = "{\"id\" : 1,\"name\":\"TestTask\",\"description\":\"TestTask Description\","
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above for variable names.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored


@Test
public void shouldStopTestTask() throws Exception {
System.out.println("shouldStopTestTask");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed :-)

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.02%) to 46.158% when pulling 177dfba on suthagar23:RESTWS-673 into e12f8c7 on openmrs:master.

@suthagar23
Copy link
Member Author

@dkayiwa I have refactored the code about your comments

@dkayiwa
Copy link
Member

dkayiwa commented Aug 24, 2017

How about other comments that have nothing to do with refactoring?


ServletContext servletContext = getServletContext(context);

if (action.getAction().equals("SCHEDULETASK") || action.getAction().equals("RESCHEDULETASK")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you already have constants for SCHEDULETASK, RESCHEDULETASK, etc?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed with constants from TaskAction

taskFactoryWrapper.reScheduleTask(taskDef);
}
catch (SchedulerException e) {
throw new UnsupportedOperationException();
Copy link
Member

@dkayiwa dkayiwa Aug 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see so many UnsupportedOperationException. Does this mean if anything goes wrong during these actions, it means that the REST call is not supported?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't throw the SchedulerException , because method implemented from Creatable Interface doesn't allow to throw the SchedulerException. Else I wanted to change the Creatable Interface. So I decided to throw UnsupportedOperationException

Copy link
Member Author

@suthagar23 suthagar23 Aug 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't throw ResponseException instead of UnsupportedOperationException. Because I can't instantiate that abstract here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are not doing anything with the thrown exception, just do not catch it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I do not catch it, then want to throw it by methods.
If I throw it from here, then want to throw it from Create method.
But I can't throw it because It is a overridden method. So I caught and throw like this.
Are there any perfect methods for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dkayiwa Shall I throw like this,
throw new ResourceDoesNotSupportOperationException("SchedulerException");

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dkayiwa If I throw the exception from here, then I can catch this from the angular REST request to indicate the user as well

Is it good to use like,
throw ObjectNotFoundException("SchedulerException : Ca not execute the action")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do something along these lines:
throw new APIException("Errors occurred while rescheduling task", e);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed! Thanks @dkayiwa :-)

}
if (!contains) {
scheduledTasks.add(task.getName());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you intentionally decide to silently ignore a call for scheduling a task which does not exist?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same applies for others like task shutdown, reschedule, etc

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, I am doing same as ModuleAction Test. I using mock collections to maintain registered and scheduled tasks. When REST call arrives at Resource, that will call these MockTaskFactoryWrapper class methods. Here I am mocking that tasks and assigning to the collections to check the status from Test Class.

taskFactoryWrapper.unloadTask(taskDef);
}
catch (SchedulerException e) {
throw new APIException("Errors occurred while shutdowning all tasks", e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this exception message correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ow sorry, Missed :-(
Changed to "Errors occurred while deleting task".

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.01%) to 46.168% when pulling 447299e on suthagar23:RESTWS-673 into e12f8c7 on openmrs:master.

@suthagar23 suthagar23 force-pushed the RESTWS-673 branch 2 times, most recently from 4a068e8 to aaf0df0 Compare August 30, 2017 09:48
@coveralls
Copy link

Coverage Status

Coverage increased (+0.3%) to 40.328% when pulling aaf0df0 on suthagar23:RESTWS-673 into 4f5eb1a on openmrs:master.

@suthagar23
Copy link
Member Author

@dkayiwa Completed 😊 Can you please take a look to merge!

}
if (!action.isAllTasks()) {
for (TaskDefinition taskDef : taskDefinitions) {
if (taskDef == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can a collection contain a null value?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed this Condition, because the wrong TaskDefinition will be caught by the setConvertedProperties() and If there are any issues, then It will throw the error.

}

@Test
public void shouldScheduleTestTask() throws Exception {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to add "Test" to any of these test methods. For instance, shouldScheduleTask is enough instead of shouldScheduleTestTask

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored

}

@Test
public void shouldGetModuleByUuid() throws Exception {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should get what?


@Test
@Override
public void shouldGetDefaultByUuid() throws Exception {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you say shouldGet, add what exactly you are getting.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I have @OverRide the method from the super class. that will express the Default representation and I can't change the name here

/**
* It will used to retrieve the required data from the Scheduler Service
*/
public class TaskServiceWrapper {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What value does this class add?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrapping the required task information between related resource and SchedulerService

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wrapping is simply one method call hence rendering it useless.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yah, but in the flow I want this class to create another Mock class for testing.
Otherwise, I can't do the testing.
I am using TaskServiceWrapper for active service works
MockTaskServiceWrapper for mock testing works

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can always mock Context.getSchedulerService().getTask(), etc without writing new mock classes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I followed the same way of the ModuleActionResource and ModuleWrapper and achieve the target 😊
What do you mean by the mocking Services?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let us go with what you have. 😊

@coveralls
Copy link

Coverage Status

Coverage increased (+0.3%) to 40.311% when pulling e38193c on suthagar23:RESTWS-673 into 4f5eb1a on openmrs:master.

@suthagar23
Copy link
Member Author

@dkayiwa So Finally If you haven't any concerns or comments, Could we move to merge it 😊


@Override
public TaskAction getByUniqueId(String uniqueId) {
throw new UnsupportedOperationException("TaskAction cannot get by id");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we take this to be the internal id, if a number, or task name?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry. Just ignore the above. 😊

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here It is the integer number. Here I have overridden that method in TaskAction.
Anyway, TaskAction is not supported to that function.


@Override
@Test(expected = Exception.class)
public void shouldGetDefaultByUuid() throws Exception {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should get what?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overridden method. It is also like used to get Default Representation. But not supported to TaskAction.

* @throws SchedulerException - It will throw in case of any SchedulerService exceptions
*/
public void deleteTask(TaskDefinition task) throws SchedulerException {
if (Context.getSchedulerService().getTaskByName(task.getName()).getStarted()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the use of this logic? Isn't it taken care of by the service layer? Why don't you just Context.getSchedulerService().deleteTask?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, SchedulerService is allowed to delete only the shutdown tasks. Otherwise, It threw the error. (I think, Core doesn't care about the task status)
So I just check about the task status before delete that task

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even then, that is the business of the service layer. Your duty is to simply call Context.getSchedulerService().deleteTask

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay 😊

assertPropEquals("started", getObject().getStarted());
assertPropEquals("taskClass", getObject().getTaskClass());
assertPropEquals("lastExecutionTime", getObject().getLastExecutionTime());
assertPropPresent("startTimePattern");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the above also be assertPropEquals()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about the one below it? 😊

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That too changed 😊

MockHttpServletRequest req = request(RequestMethod.POST, getURI());
req.setContent(json.getBytes());
deserialize(handle(req));
Assert.assertEquals(mockTaskServiceWrapper.getTaskByName("MockTask").getName(), "MockTask");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How sure can i be that this task did not exist before saving?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add another test before to this to confirm that

}

@Test
public void shouldGetAllRegistered() throws Exception {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference between shouldGetAll and shouldGetAllRegistered?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defaultly GetAll method in the resource has been set to return the list of Scheduled tasks. (I can't change the name because it is an Overriden method)
So I am using another separate method to test about the Registered tasks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it more natural for GetAll to return all tasks regardless of whether they are scheduled or not?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I will change the way to retrieve all the registered for the getAll method

}

@Test
public void shouldDoNothingIfTaskAlreadyShutdown() throws Exception {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be: shutdown_shouldDoNothingIfTaskAlreadyShutdown

Copy link
Member Author

@suthagar23 suthagar23 Aug 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to shutdownTask_shouldDoNothingIfTaskAlreadyShutdown

}

@Test
public void shouldDoNothingIfTaskAlreadyScheduled() throws Exception {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scheduleTask _shouldDoNothingIfTaskAlreadyScheduled

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

mockTaskServiceWrapper.scheduledTasks.add(testTask);
deserialize(handle(newPostRequest(getURI(), "{\"action\": \"shutdowntask\", \"tasks\":[\"" + getTestTaskName()
+ "\"]}")));
assertThat(mockTaskServiceWrapper.scheduledTasks, not(hasItem(testTask)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How how are you that the task wasn't already shutdown even before the shutdown command?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I am checking the Scheduled list before making shout down the request. The scheduled list will contain this task If it is not shut down.
After the request, It will be removed from the Scheduled list (shutdown). Then I am checking for removal.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh which line number are you checking that the task is not already shutdown?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh, Sorry really missed that 😢
I will add it quickly

@suthagar23 suthagar23 force-pushed the RESTWS-673 branch 2 times, most recently from 5ff8ce8 to 15140a5 Compare August 30, 2017 12:51
update

TaskDefinition and TaskAction Implementation
@coveralls
Copy link

Coverage Status

Coverage increased (+0.3%) to 40.318% when pulling 1bb91de on suthagar23:RESTWS-673 into 4f5eb1a on openmrs:master.

@dkayiwa dkayiwa merged commit c165168 into openmrs:master Aug 30, 2017
@coveralls
Copy link

Coverage Status

Coverage increased (+0.3%) to 40.318% when pulling 1bb91de on suthagar23:RESTWS-673 into 4f5eb1a on openmrs:master.

@suthagar23
Copy link
Member Author

Thank you so much @dkayiwa for the review and merge 😊

@dkayiwa
Copy link
Member

dkayiwa commented Aug 30, 2017

Thank you too for responding to all my back and forth review comments! 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants