diff --git a/pom.xml b/pom.xml index 532e846d1..fad0c2382 100755 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ opensrp-server-core jar - 2.11.2-SNAPSHOT + 2.11.3-SNAPSHOT opensrp-server-core OpenSRP Server Core module https://github.com/OpenSRP/opensrp-server-core diff --git a/src/main/java/org/opensrp/service/TaskService.java b/src/main/java/org/opensrp/service/TaskService.java index b46d29de3..fe3167cfd 100644 --- a/src/main/java/org/opensrp/service/TaskService.java +++ b/src/main/java/org/opensrp/service/TaskService.java @@ -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) { diff --git a/src/test/java/org/opensrp/service/TaskServiceTest.java b/src/test/java/org/opensrp/service/TaskServiceTest.java index 5a4e47304..4b116f972 100644 --- a/src/test/java/org/opensrp/service/TaskServiceTest.java +++ b/src/test/java/org/opensrp/service/TaskServiceTest.java @@ -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; @@ -77,6 +77,29 @@ 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(); @@ -84,6 +107,23 @@ public void testAddTask() { 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();