Skip to content

Commit

Permalink
Merge pull request #464 from opensrp/web-824-check-before-task-genera…
Browse files Browse the repository at this point in the history
…tion

only add task if it does not exist check
  • Loading branch information
LZRS authored May 26, 2021
2 parents 9b9b4d8 + 044416f commit 3536936
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<artifactId>opensrp-server-core</artifactId>
<packaging>jar</packaging>
<version>2.11.2-SNAPSHOT</version>
<version>2.11.3-SNAPSHOT</version>
<name>opensrp-server-core</name>
<description>OpenSRP Server Core module</description>
<url>https://github.com/OpenSRP/opensrp-server-core</url>
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/org/opensrp/service/TaskService.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,27 @@ public void addOrUpdateTask(Task task) {
addTask(task);
}
}

public boolean taskExists(Task task){
String taskIdentifier = task.getIdentifier();
String entityId = task.getForEntity();
String jurisdiction = task.getGroupIdentifier();
String planIdentifier = task.getPlanIdentifier();
String taskCode = task.getCode();

return (StringUtils.isNotBlank(taskIdentifier) && taskRepository.checkIfTaskExists(entityId, jurisdiction, planIdentifier, taskCode));
}

public Task addTask(Task task) {
if (StringUtils.isBlank(task.getIdentifier()))
throw new IllegalArgumentException("Identifier not specified");
task.setAuthoredOn(new DateTime());
task.setLastModified(new DateTime());
taskRepository.add(task);
if (!taskExists(task)) {
task.setAuthoredOn(new DateTime());
task.setLastModified(new DateTime());
taskRepository.add(task);
}

return task;

}

public Task updateTask(Task task) {
Expand Down
48 changes: 44 additions & 4 deletions src/test/java/org/opensrp/service/TaskServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.opensrp.domain.TaskUpdate;
import org.opensrp.repository.TaskRepository;
import org.opensrp.search.TaskSearchBean;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.modules.junit4.PowerMockRunner;
import org.smartregister.domain.Task;
import org.smartregister.domain.Task.TaskPriority;
import org.smartregister.domain.Task.TaskStatus;
import org.opensrp.domain.TaskUpdate;
import org.opensrp.repository.TaskRepository;
import org.smartregister.utils.TaskDateTimeTypeConverter;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.modules.junit4.PowerMockRunner;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -77,13 +77,53 @@ public void testAddOrUpdateTaskWithoutIdentifier() {

}

@Test
public void testTaskExistsChecksRepo(){
Task task = initializeTask();
taskService.taskExists(task);
verify(taskRepository).checkIfTaskExists(eq(task.getForEntity()), eq(task.getGroupIdentifier()), eq(task.getPlanIdentifier()), eq(task.getCode()));
}

@Test
public void testTaskExistsReturnsExpected(){
Task task = initializeTask();
when(taskRepository.checkIfTaskExists(eq(task.getForEntity()), eq(task.getGroupIdentifier()), eq(task.getPlanIdentifier()), eq(task.getCode())))
.thenReturn(true);
assertTrue(taskService.taskExists(task));
}

@Test
public void testAddTaskChecksIfTaskExists(){
Task task = initializeTask();
taskService = spy(taskService);
taskService.addTask(task);
verify(taskService).taskExists(eq(task));
}

@Test
public void testAddTask() {
Task task = initializeTask();
taskService.addTask(task);
verify(taskRepository).add(task);
}

@Test
public void testAddTaskDoesNotDuplicate(){
taskService = spy(taskService);

doReturn(false, true, true, true, true)
.when(taskService)
.taskExists(any(Task.class));

Task task = initializeTask();
taskService.addTask(task);
verify(taskRepository).add(task);
taskService.addTask(task);
taskService.addTask(task);
taskService.addTask(task);
verify(taskRepository, times(1)).add(task);
}

@Test(expected = IllegalArgumentException.class)
public void testAddTaskWithoutIdentifier() {
Task task = new Task();
Expand Down

0 comments on commit 3536936

Please sign in to comment.