From 0b2c507f300091d1ae9d7e64972c9aec8d8cc658 Mon Sep 17 00:00:00 2001 From: BVier <26220150+BVier@users.noreply.github.com> Date: Wed, 6 Dec 2017 09:55:00 +0100 Subject: [PATCH 1/2] TSK-64: Refactor createManualTask to createTask --- .../java/pro/taskana/ExampleBootstrap.java | 15 +- .../java/pro/taskana/TaskanaProducers.java | 27 ++- .../src/test/java/pro/taskana/TaskanaEjb.java | 25 ++- .../java/pro/taskana/TaskanaRestTest.java | 32 ++-- .../main/java/pro/taskana/TaskService.java | 22 +-- .../impl/ClassificationServiceImpl.java | 8 +- .../pro/taskana/impl/TaskServiceImpl.java | 173 +++++++++--------- .../impl/ClassificationServiceImplTest.java | 17 ++ .../pro/taskana/impl/TaskServiceImplTest.java | 109 +++++++---- .../SummaryServiceImplIntAutoCommitTest.java | 2 +- .../TaskServiceImplIntAutocommitTest.java | 85 +++++---- .../TaskServiceImplIntExplicitTest.java | 151 ++++++++------- .../java/pro/taskana/ExampleBootstrap.java | 9 +- .../java/pro/taskana/TaskanaComponent.java | 6 +- .../java/pro/taskana/rest/TaskController.java | 2 +- 15 files changed, 405 insertions(+), 278 deletions(-) diff --git a/lib/taskana-cdi-example/src/main/java/pro/taskana/ExampleBootstrap.java b/lib/taskana-cdi-example/src/main/java/pro/taskana/ExampleBootstrap.java index 00dea72ab..291b13918 100644 --- a/lib/taskana-cdi-example/src/main/java/pro/taskana/ExampleBootstrap.java +++ b/lib/taskana-cdi-example/src/main/java/pro/taskana/ExampleBootstrap.java @@ -1,16 +1,17 @@ package pro.taskana; +import pro.taskana.exceptions.ClassificationNotFoundException; +import pro.taskana.exceptions.NotAuthorizedException; +import pro.taskana.exceptions.TaskNotFoundException; +import pro.taskana.exceptions.WorkbasketNotFoundException; +import pro.taskana.model.Task; + import javax.annotation.PostConstruct; import javax.ejb.EJB; import javax.enterprise.context.ApplicationScoped; import javax.enterprise.context.Initialized; import javax.enterprise.event.Observes; -import pro.taskana.exceptions.NotAuthorizedException; -import pro.taskana.exceptions.TaskNotFoundException; -import pro.taskana.exceptions.WorkbasketNotFoundException; -import pro.taskana.model.Task; - @ApplicationScoped public class ExampleBootstrap { @@ -18,9 +19,9 @@ public class ExampleBootstrap { private TaskanaEjb taskanaEjb; @PostConstruct - public void init(@Observes @Initialized(ApplicationScoped.class) Object init) throws TaskNotFoundException, NotAuthorizedException, WorkbasketNotFoundException { + public void init(@Observes @Initialized(ApplicationScoped.class) Object init) throws TaskNotFoundException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { System.out.println("---------------------------> Start App"); - Task task = taskanaEjb.getTaskService().create(new Task()); + Task task = taskanaEjb.getTaskService().createTask(new Task()); System.out.println("---------------------------> Task started: " + task.getId()); taskanaEjb.getTaskService().claim(task.getId(), "John Doe"); System.out.println( diff --git a/lib/taskana-cdi/src/main/java/pro/taskana/TaskanaProducers.java b/lib/taskana-cdi/src/main/java/pro/taskana/TaskanaProducers.java index 4b33a890c..b4881a130 100644 --- a/lib/taskana-cdi/src/main/java/pro/taskana/TaskanaProducers.java +++ b/lib/taskana-cdi/src/main/java/pro/taskana/TaskanaProducers.java @@ -1,9 +1,8 @@ package pro.taskana; -import java.io.IOException; -import java.io.InputStream; -import java.sql.SQLException; -import java.util.Properties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pro.taskana.configuration.TaskanaEngineConfiguration; import javax.annotation.PostConstruct; import javax.enterprise.context.ApplicationScoped; @@ -13,10 +12,10 @@ import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import pro.taskana.configuration.TaskanaEngineConfiguration; +import java.io.IOException; +import java.io.InputStream; +import java.sql.SQLException; +import java.util.Properties; @ApplicationScoped public class TaskanaProducers { @@ -61,4 +60,16 @@ public class TaskanaProducers { return taskanaEngine.getTaskService(); } + @ApplicationScoped + @Produces + public ClassificationService generateClassificationService() { + return taskanaEngine.getClassificationService(); + } + + @ApplicationScoped + @Produces + public WorkbasketService generateWorkbasketService() { + return taskanaEngine.getWorkbasketService(); + } + } diff --git a/lib/taskana-cdi/src/test/java/pro/taskana/TaskanaEjb.java b/lib/taskana-cdi/src/test/java/pro/taskana/TaskanaEjb.java index 99fd0a9dd..33303a279 100644 --- a/lib/taskana-cdi/src/test/java/pro/taskana/TaskanaEjb.java +++ b/lib/taskana-cdi/src/test/java/pro/taskana/TaskanaEjb.java @@ -1,24 +1,39 @@ package pro.taskana; -import javax.ejb.Stateless; -import javax.inject.Inject; - +import pro.taskana.exceptions.ClassificationNotFoundException; import pro.taskana.exceptions.NotAuthorizedException; import pro.taskana.exceptions.WorkbasketNotFoundException; import pro.taskana.model.Task; +import javax.ejb.Stateless; +import javax.inject.Inject; + @Stateless public class TaskanaEjb { @Inject private TaskService taskService; + @Inject + private ClassificationService classificationService; + + @Inject + private WorkbasketService workbasketService; + public TaskService getTaskService() { return taskService; } - public void triggerRollback() throws NotAuthorizedException, WorkbasketNotFoundException { - Task t = taskService.create(new Task()); + public ClassificationService getClassificationService() { + return classificationService; + } + + public WorkbasketService getWorkbasketService() { + return workbasketService; + } + + public void triggerRollback() throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { + Task t = taskService.createTask(new Task()); System.out.println("---------------->" + t.getId()); throw new RuntimeException(); } diff --git a/lib/taskana-cdi/src/test/java/pro/taskana/TaskanaRestTest.java b/lib/taskana-cdi/src/test/java/pro/taskana/TaskanaRestTest.java index 3f2bc232d..8a23bed7e 100644 --- a/lib/taskana-cdi/src/test/java/pro/taskana/TaskanaRestTest.java +++ b/lib/taskana-cdi/src/test/java/pro/taskana/TaskanaRestTest.java @@ -1,19 +1,18 @@ package pro.taskana; -import javax.ejb.EJB; -import javax.ws.rs.DELETE; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.core.Response; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import pro.taskana.exceptions.ClassificationNotFoundException; import pro.taskana.exceptions.NotAuthorizedException; import pro.taskana.exceptions.TaskNotFoundException; import pro.taskana.exceptions.WorkbasketNotFoundException; +import pro.taskana.model.Classification; import pro.taskana.model.Task; +import pro.taskana.model.Workbasket; + +import javax.ejb.EJB; +import javax.ws.rs.*; +import javax.ws.rs.core.Response; @Path("/test") public class TaskanaRestTest { @@ -24,14 +23,25 @@ public class TaskanaRestTest { private TaskanaEjb taskanaEjb; @GET - public Response startTask() throws NotAuthorizedException, WorkbasketNotFoundException { - Task result = taskanaEjb.getTaskService().create(new Task()); + public Response startTask() throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { + Workbasket workbasket = new Workbasket(); + workbasket.setName("wb"); + taskanaEjb.getWorkbasketService().createWorkbasket(workbasket); + Classification classification = new Classification(); + taskanaEjb.getClassificationService().addClassification(classification); + + Task task = new Task(); + task.setClassification(classification); + task.setWorkbasketId(workbasket.getId()); + + Task result = taskanaEjb.getTaskService().createTask(task); + logger.info(result.getId() + ":" + result.getOwner()); return Response.status(200).entity(result.getId()).build(); } @POST - public Response rollbackTask() throws NotAuthorizedException, WorkbasketNotFoundException { + public Response rollbackTask() throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { taskanaEjb.triggerRollback(); return Response.status(204).build(); } diff --git a/lib/taskana-core/src/main/java/pro/taskana/TaskService.java b/lib/taskana-core/src/main/java/pro/taskana/TaskService.java index 2bda62b31..8f1839b31 100644 --- a/lib/taskana-core/src/main/java/pro/taskana/TaskService.java +++ b/lib/taskana-core/src/main/java/pro/taskana/TaskService.java @@ -4,11 +4,12 @@ import pro.taskana.exceptions.ClassificationNotFoundException; import pro.taskana.exceptions.NotAuthorizedException; import pro.taskana.exceptions.TaskNotFoundException; import pro.taskana.exceptions.WorkbasketNotFoundException; -import pro.taskana.model.*; +import pro.taskana.model.DueWorkbasketCounter; +import pro.taskana.model.Task; +import pro.taskana.model.TaskState; +import pro.taskana.model.TaskStateCounter; -import java.sql.Timestamp; import java.util.List; -import java.util.Map; /** * The Task Service manages all operations on tasks. @@ -41,21 +42,8 @@ public interface TaskService { * @return the created task * @throws NotAuthorizedException */ - Task create(Task task) throws NotAuthorizedException, WorkbasketNotFoundException; + Task createTask(Task task) throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException; - /** - * Create a task manually by filling the fields. - * @param workbasketId not null - * @param classificationId not null - * @param domain - * @param planned - * @param name - * @param description - * @param primaryObjectReference - * @param customAttributes - * @return - */ - Task createManualTask(String workbasketId, String classificationId, String domain, Timestamp planned, String name, String description, ObjectReference primaryObjectReference, Map customAttributes) throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException; /** * Get the details of a task. * @param taskId diff --git a/lib/taskana-core/src/main/java/pro/taskana/impl/ClassificationServiceImpl.java b/lib/taskana-core/src/main/java/pro/taskana/impl/ClassificationServiceImpl.java index 5cf3290c4..6b76a6ed1 100644 --- a/lib/taskana-core/src/main/java/pro/taskana/impl/ClassificationServiceImpl.java +++ b/lib/taskana-core/src/main/java/pro/taskana/impl/ClassificationServiceImpl.java @@ -1,5 +1,7 @@ package pro.taskana.impl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import pro.taskana.ClassificationQuery; import pro.taskana.ClassificationService; import pro.taskana.TaskanaEngine; @@ -16,9 +18,6 @@ import java.time.LocalDate; import java.util.ArrayList; import java.util.List; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** * This is the implementation of ClassificationService. */ @@ -170,6 +169,9 @@ public class ClassificationServiceImpl implements ClassificationService { @Override public Classification getClassification(String id, String domain) throws ClassificationNotFoundException { + if (id == null) { + throw new ClassificationNotFoundException(null); + } LOGGER.debug("entry to getClassification(id = {}, domain = {})", id, domain); Classification result = null; try { diff --git a/lib/taskana-core/src/main/java/pro/taskana/impl/TaskServiceImpl.java b/lib/taskana-core/src/main/java/pro/taskana/impl/TaskServiceImpl.java index dc854f784..b5d7f2901 100644 --- a/lib/taskana-core/src/main/java/pro/taskana/impl/TaskServiceImpl.java +++ b/lib/taskana-core/src/main/java/pro/taskana/impl/TaskServiceImpl.java @@ -1,17 +1,7 @@ package pro.taskana.impl; -import java.sql.Date; -import java.sql.Timestamp; -import java.time.Duration; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import pro.taskana.TaskQuery; import pro.taskana.TaskService; import pro.taskana.TaskanaEngine; @@ -21,16 +11,18 @@ import pro.taskana.exceptions.TaskNotFoundException; import pro.taskana.exceptions.WorkbasketNotFoundException; import pro.taskana.impl.util.IdGenerator; import pro.taskana.impl.util.LoggerUtils; -import pro.taskana.model.Classification; -import pro.taskana.model.DueWorkbasketCounter; -import pro.taskana.model.ObjectReference; -import pro.taskana.model.Task; -import pro.taskana.model.TaskState; -import pro.taskana.model.TaskStateCounter; -import pro.taskana.model.WorkbasketAuthorization; +import pro.taskana.model.*; import pro.taskana.model.mappings.ObjectReferenceMapper; import pro.taskana.model.mappings.TaskMapper; +import java.sql.Date; +import java.sql.Timestamp; +import java.time.Duration; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + /** * This is the implementation of TaskService. */ @@ -109,66 +101,31 @@ public class TaskServiceImpl implements TaskService { } @Override - public Task create(Task task) throws NotAuthorizedException, WorkbasketNotFoundException { - LOGGER.debug("entry to create(task = {})", task); + public Task createTask(Task task) throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { + LOGGER.debug("entry to createTask(task = {})", task); try { taskanaEngineImpl.openConnection(); taskanaEngine.getWorkbasketService().checkAuthorization(task.getWorkbasketId(), WorkbasketAuthorization.APPEND); + Workbasket wb = taskanaEngine.getWorkbasketService().getWorkbasket(task.getWorkbasketId()); + + Classification classification = task.getClassification(); + if (classification == null) { + throw new ClassificationNotFoundException(null); + } + taskanaEngine.getClassificationService().getClassification(classification.getId(), ""); standardSettings(task); this.taskMapper.insert(task); - LOGGER.debug("Method create() created Task '{}'.", task.getId()); + LOGGER.debug("Method createTask() created Task '{}'.", task.getId()); return task; } finally { taskanaEngineImpl.returnConnection(); - LOGGER.debug("exit from create(task = {})"); + LOGGER.debug("exit from createTask(task = {})"); } } - @Override - public Task createManualTask(String workbasketId, String classificationId, String domain, Timestamp planned, String name, - String description, ObjectReference primaryObjectReference, Map customAttributes) throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("entry to createManualTask(workbasketId = {}, classificationId = {}, domain = {}, planned = {}, name = {}," - + " description = {}, primaryObjectReference = {}, customAttributes = {})", workbasketId, classificationId, domain, planned, name, - description, primaryObjectReference, LoggerUtils.mapToString(customAttributes)); - } - try { - taskanaEngineImpl.openConnection(); - taskanaEngine.getWorkbasketService().checkAuthorization(workbasketId, WorkbasketAuthorization.APPEND); - - taskanaEngine.getWorkbasketService().getWorkbasket(workbasketId); - Classification classification = taskanaEngine.getClassificationService().getClassification(classificationId, domain); - - if (!TYPE_MANUAL.equals(classification.getCategory())) { - throw new NotAuthorizedException("You're not allowed to add a task manually to a '" + classification.getCategory() + "'- Classification!"); - } - - Task task = new Task(); - - task.setWorkbasketId(workbasketId); - task.setClassification(classification); - task.setPlanned(planned); - task.setPrimaryObjRef(primaryObjectReference); - task.setCustomAttributes(customAttributes); - task.setName(name); - task.setDescription(description); - - this.standardSettings(task); - this.setCustomAttributes(task); - - this.taskMapper.insert(task); - - LOGGER.debug("Method create() created Task '{}'.", task.getId()); - return task; - } finally { - taskanaEngineImpl.returnConnection(); - LOGGER.debug("exit from create()"); - } - } - @Override public Task getTaskById(String id) throws TaskNotFoundException { LOGGER.debug("entry to getTaskById(id = {})", id); @@ -381,33 +338,77 @@ public class TaskServiceImpl implements TaskService { } task.setPrimaryObjRef(objectReference); } + + //guarantees redundancy of customers and customAttributes + this.checkCustomAttributes(task); } - private void setCustomAttributes(Task task) { + private void checkCustomAttributes(Task task) { + List customList = new ArrayList<>(); + if (task.getCustom1() != null) { + customList.add(task.getCustom1()); + } + if (task.getCustom2() != null) { + customList.add(task.getCustom2()); + } + if (task.getCustom3() != null) { + customList.add(task.getCustom3()); + } + if (task.getCustom4() != null) { + customList.add(task.getCustom4()); + } + if (task.getCustom5() != null) { + customList.add(task.getCustom5()); + } + if (task.getCustom6() != null) { + customList.add(task.getCustom6()); + } + if (task.getCustom7() != null) { + customList.add(task.getCustom7()); + } + if (task.getCustom8() != null) { + customList.add(task.getCustom8()); + } + if (task.getCustom9() != null) { + customList.add(task.getCustom9()); + } + if (task.getCustom10() != null) { + customList.add(task.getCustom10()); + } + if (task.getCustomAttributes() != null) { for (String custom : task.getCustomAttributes().keySet()) { - if (task.getCustom1() == null) { - task.setCustom1(custom); - } else if (task.getCustom2() == null) { - task.setCustom2(custom); - } else if (task.getCustom3() == null) { - task.setCustom3(custom); - } else if (task.getCustom4() == null) { - task.setCustom4(custom); - } else if (task.getCustom5() == null) { - task.setCustom5(custom); - } else if (task.getCustom6() == null) { - task.setCustom6(custom); - } else if (task.getCustom7() == null) { - task.setCustom7(custom); - } else if (task.getCustom8() == null) { - task.setCustom8(custom); - } else if (task.getCustom9() == null) { - task.setCustom9(custom); - } else if (task.getCustom10() == null) { - task.setCustom10(custom); - } else { - break; + if (!customList.isEmpty() && customList.contains(custom)) { + if (task.getCustom1() == null) { + task.setCustom1(custom); + } else if (task.getCustom2() == null) { + task.setCustom2(custom); + } else if (task.getCustom3() == null) { + task.setCustom3(custom); + } else if (task.getCustom4() == null) { + task.setCustom4(custom); + } else if (task.getCustom5() == null) { + task.setCustom5(custom); + } else if (task.getCustom6() == null) { + task.setCustom6(custom); + } else if (task.getCustom7() == null) { + task.setCustom7(custom); + } else if (task.getCustom8() == null) { + task.setCustom8(custom); + } else if (task.getCustom9() == null) { + task.setCustom9(custom); + } else if (task.getCustom10() == null) { + task.setCustom10(custom); + } else { + break; + } + } + } + } + if (!customList.isEmpty()) { + for (String custom : customList) { + if (!task.getCustomAttributes().containsKey(custom)) { + task.getCustomAttributes().put(custom, null); } } } diff --git a/lib/taskana-core/src/test/java/pro/taskana/impl/ClassificationServiceImplTest.java b/lib/taskana-core/src/test/java/pro/taskana/impl/ClassificationServiceImplTest.java index 5377b82a4..213e95d58 100644 --- a/lib/taskana-core/src/test/java/pro/taskana/impl/ClassificationServiceImplTest.java +++ b/lib/taskana-core/src/test/java/pro/taskana/impl/ClassificationServiceImplTest.java @@ -13,6 +13,7 @@ import pro.taskana.model.Classification; import pro.taskana.model.mappings.ClassificationMapper; import java.sql.Date; +import java.sql.SQLException; import java.time.LocalDate; import java.util.ArrayList; import java.util.List; @@ -87,6 +88,22 @@ public class ClassificationServiceImplTest { Assert.assertEquals(classification2.getValidUntil(), classification3.getValidUntil()); } + @Test + public void testGetClassification() throws ClassificationNotFoundException, SQLException { + doNothing().when(classificationMapper).insert(any()); + doNothing().when(taskanaEngineImpl).openConnection(); + doNothing().when(taskanaEngineImpl).returnConnection(); + + Classification classification = new Classification(); + classification.setDomain("domain"); + classificationService.addClassification(classification); + + doReturn(classification).when(classificationMapper).findByIdAndDomain(any(), any(), any()); + + Classification test = classificationService.getClassification(classification.getId(), "domain"); + Assert.assertEquals("domain", test.getDomain()); + } + @Test public void testFindAllClassifications() throws NotAuthorizedException { doNothing().when(classificationMapper).insert(any()); diff --git a/lib/taskana-core/src/test/java/pro/taskana/impl/TaskServiceImplTest.java b/lib/taskana-core/src/test/java/pro/taskana/impl/TaskServiceImplTest.java index 9a2f8c7b1..c87ae0447 100644 --- a/lib/taskana-core/src/test/java/pro/taskana/impl/TaskServiceImplTest.java +++ b/lib/taskana-core/src/test/java/pro/taskana/impl/TaskServiceImplTest.java @@ -80,19 +80,25 @@ public class TaskServiceImplTest { } @Test - public void testCreateSimpleTask() throws NotAuthorizedException, WorkbasketNotFoundException { + public void testCreateSimpleTask() throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { Mockito.doNothing().when(taskMapperMock).insert(any()); Task expectedTask = createUnitTestTask("1", "DUMMYTASK", "1"); - Task actualTask = cut.create(expectedTask); + Task actualTask = cut.createTask(expectedTask); verify(taskanaEngineImpl, times(1)).openConnection(); - verify(taskanaEngineMock, times(1)).getWorkbasketService(); + verify(taskanaEngineMock, times(2)).getWorkbasketService(); + verify(taskanaEngineMock, times(1)).getClassificationService(); verify(workbasketServiceMock, times(1)).checkAuthorization(any(), any()); + verify(workbasketServiceMock, times(1)).getWorkbasket(any()); verify(taskMapperMock, times(1)).insert(expectedTask); verify(taskanaEngineImpl, times(1)).returnConnection(); + verify(classificationServiceMock, times(1)).addClassification(any()); + verify(classificationServiceMock, times(1)).getClassification(any(), any()); verifyNoMoreInteractions(taskanaEngineConfigurationMock, taskanaEngineMock, taskanaEngineImpl, - taskMapperMock, objectReferenceMapperMock, workbasketServiceMock); + taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, + classificationServiceMock); + assertNull(actualTask.getOwner()); assertNotNull(actualTask.getCreated()); assertNotNull(actualTask.getModified()); @@ -103,7 +109,7 @@ public class TaskServiceImplTest { } @Test - public void testCreateSimpleTaskWithObjectReference() throws NotAuthorizedException, WorkbasketNotFoundException { + public void testCreateSimpleTaskWithObjectReference() throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { ObjectReference expectedObjectReference = new ObjectReference(); expectedObjectReference.setId("1"); expectedObjectReference.setType("DUMMY"); @@ -114,16 +120,22 @@ public class TaskServiceImplTest { Mockito.doNothing().when(taskMapperMock).insert(any()); Mockito.doReturn(expectedObjectReference).when(objectReferenceMapperMock).findByObjectReference(any()); - Task actualTask = cut.create(expectedTask); + Task actualTask = cut.createTask(expectedTask); verify(taskanaEngineImpl, times(1)).openConnection(); - verify(taskanaEngineMock, times(1)).getWorkbasketService(); + verify(taskanaEngineMock, times(2)).getWorkbasketService(); + verify(taskanaEngineMock, times(1)).getClassificationService(); verify(workbasketServiceMock, times(1)).checkAuthorization(any(), any()); + verify(workbasketServiceMock, times(1)).getWorkbasket(any()); verify(objectReferenceMapperMock, times(1)).findByObjectReference(any()); verify(taskMapperMock, times(1)).insert(expectedTask); verify(taskanaEngineImpl, times(1)).returnConnection(); + verify(classificationServiceMock, times(1)).addClassification(any()); + verify(classificationServiceMock, times(1)).getClassification(any(), + any()); verifyNoMoreInteractions(taskanaEngineConfigurationMock, taskanaEngineMock, taskanaEngineImpl, - taskMapperMock, objectReferenceMapperMock, workbasketServiceMock); + taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, + classificationServiceMock); assertNull(actualTask.getOwner()); assertNotNull(actualTask.getCreated()); @@ -136,7 +148,7 @@ public class TaskServiceImplTest { } @Test - public void testCreateSimpleTaskWithObjectReferenceIsNull() throws NotAuthorizedException, WorkbasketNotFoundException { + public void testCreateSimpleTaskWithObjectReferenceIsNull() throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { ObjectReference expectedObjectReference = new ObjectReference(); expectedObjectReference.setId("1"); expectedObjectReference.setType("DUMMY"); @@ -148,18 +160,23 @@ public class TaskServiceImplTest { Mockito.doNothing().when(objectReferenceMapperMock).insert(any()); Mockito.doReturn(null).when(objectReferenceMapperMock).findByObjectReference(any()); - Task actualTask = cut.create(expectedTask); + Task actualTask = cut.createTask(expectedTask); expectedTask.getPrimaryObjRef().setId(actualTask.getPrimaryObjRef().getId()); // get only new ID verify(taskanaEngineImpl, times(1)).openConnection(); - verify(taskanaEngineMock, times(1)).getWorkbasketService(); + verify(taskanaEngineMock, times(2)).getWorkbasketService(); + verify(taskanaEngineMock, times(1)).getClassificationService(); verify(workbasketServiceMock, times(1)).checkAuthorization(any(), any()); + verify(workbasketServiceMock, times(1)).getWorkbasket(any()); + verify(classificationServiceMock, times(1)).addClassification(any()); + verify(classificationServiceMock, times(1)).getClassification(any(), any()); verify(objectReferenceMapperMock, times(1)).findByObjectReference(any()); verify(objectReferenceMapperMock, times(1)).insert(any()); verify(taskMapperMock, times(1)).insert(expectedTask); verify(taskanaEngineImpl, times(1)).returnConnection(); verifyNoMoreInteractions(taskanaEngineConfigurationMock, taskanaEngineMock, taskanaEngineImpl, - taskMapperMock, objectReferenceMapperMock, workbasketServiceMock); + taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, + classificationServiceMock); assertNull(actualTask.getOwner()); assertNotNull(actualTask.getCreated()); @@ -172,7 +189,7 @@ public class TaskServiceImplTest { } @Test - public void testCreateManualTask() throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { + public void testCreateTaskWithPlanned() throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { ObjectReference expectedObjectReference = new ObjectReference(); expectedObjectReference.setId("1"); expectedObjectReference.setType("DUMMY"); @@ -185,40 +202,55 @@ public class TaskServiceImplTest { Mockito.doNothing().when(taskMapperMock).insert(any()); Mockito.doNothing().when(objectReferenceMapperMock).insert(any()); - Task manualTask = cut.createManualTask("workbasketId", "classification", "domain", null, null, "simply awesome task", expectedObjectReference, null); + Task test = new Task(); + test.setWorkbasketId("workbasketId"); + test.setClassification(classification); + test.setPrimaryObjRef(expectedObjectReference); + test.setDescription("simply awesome task"); - Task manualTask2 = cut.createManualTask("workbasketId", "classification", "domain", Timestamp.valueOf(LocalDateTime.now().minusHours(1)), "Task2", "simply awesome task", expectedObjectReference, null); + cut.createTask(test); + + Task test2 = new Task(); + test2.setWorkbasketId("workbasketId"); + test2.setClassification(classification); + test2.setPrimaryObjRef(expectedObjectReference); + test2.setPlanned(Timestamp.valueOf(LocalDateTime.now().minusHours(1))); + test2.setName("Task2"); + + cut.createTask(test2); verify(taskanaEngineImpl, times(2)).openConnection(); verify(taskanaEngineMock, times(2 + 2)).getWorkbasketService(); verify(taskanaEngineMock, times(2)).getClassificationService(); verify(workbasketServiceMock, times(2)).checkAuthorization(any(), any()); verify(workbasketServiceMock, times(2)).getWorkbasket(any()); + verify(classificationServiceMock, times(2)).getClassification(any(), any()); verify(objectReferenceMapperMock, times(2)).findByObjectReference(any()); verify(objectReferenceMapperMock, times(2)).insert(any()); - verify(taskMapperMock, times(1)).insert(manualTask); - verify(taskMapperMock, times(1)).insert(manualTask2); + verify(taskMapperMock, times(1)).insert(test); + verify(taskMapperMock, times(1)).insert(test2); verify(taskanaEngineImpl, times(2)).returnConnection(); verifyNoMoreInteractions(taskanaEngineConfigurationMock, taskanaEngineMock, taskanaEngineImpl, - taskMapperMock, objectReferenceMapperMock, workbasketServiceMock); + taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, + classificationServiceMock); - assertNull(manualTask.getOwner()); - assertNotNull(manualTask.getCreated()); - assertNotNull(manualTask.getModified()); - assertNull(manualTask.getCompleted()); - assertNull(manualTask.getDue()); - assertThat(manualTask.getWorkbasketId(), equalTo(manualTask2.getWorkbasketId())); - assertThat(manualTask.getName(), equalTo(classification.getName())); - assertThat(manualTask.getState(), equalTo(TaskState.READY)); - assertThat(manualTask.getPrimaryObjRef(), equalTo(expectedObjectReference)); - assertThat(manualTask.getName(), not(manualTask2.getName())); - assertThat(manualTask.getPlanned(), not(manualTask2.getPlanned())); - assertThat(manualTask2.getPlanned(), not(manualTask2.getCreated())); + assertNull(test.getOwner()); + assertNotNull(test.getCreated()); + assertNotNull(test.getModified()); + assertNull(test.getCompleted()); + assertNull(test.getDue()); + assertThat(test.getWorkbasketId(), equalTo(test2.getWorkbasketId())); + assertThat(test.getName(), equalTo(classification.getName())); + assertThat(test.getState(), equalTo(TaskState.READY)); + assertThat(test.getPrimaryObjRef(), equalTo(expectedObjectReference)); + assertThat(test.getName(), not(test2.getName())); + assertThat(test.getPlanned(), not(test2.getPlanned())); + assertThat(test2.getPlanned(), not(test2.getCreated())); } @Test(expected = NotAuthorizedException.class) - public void testCreateThrowingAuthorizedOnWorkbasket() throws NotAuthorizedException, WorkbasketNotFoundException { + public void testCreateThrowingAuthorizedOnWorkbasket() throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { try { Mockito.doThrow(NotAuthorizedException.class).when(workbasketServiceMock).checkAuthorization(any(), any()); Task task = new Task(); @@ -226,33 +258,35 @@ public class TaskServiceImplTest { task.setBusinessProcessId("BPI1"); task.setParentBusinessProcessId("PBPI1"); - cut.create(task); + cut.createTask(task); } catch (Exception e) { verify(taskanaEngineImpl, times(1)).openConnection(); verify(taskanaEngineMock, times(1)).getWorkbasketService(); verify(workbasketServiceMock, times(1)).checkAuthorization(any(), any()); verify(taskanaEngineImpl, times(1)).returnConnection(); verifyNoMoreInteractions(taskanaEngineConfigurationMock, taskanaEngineMock, taskanaEngineImpl, - taskMapperMock, objectReferenceMapperMock, workbasketServiceMock); + taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, + classificationServiceMock); throw e; } } @Test(expected = WorkbasketNotFoundException.class) - public void testCreateThrowsWorkbasketNotFoundException() throws NotAuthorizedException, WorkbasketNotFoundException { + public void testCreateThrowsWorkbasketNotFoundException() throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { try { Mockito.doThrow(WorkbasketNotFoundException.class).when(workbasketServiceMock).checkAuthorization(any(), any()); Task task = new Task(); task.setWorkbasketId("1"); - cut.create(task); + cut.createTask(task); } catch (Exception e) { verify(taskanaEngineImpl, times(1)).openConnection(); verify(taskanaEngineMock, times(1)).getWorkbasketService(); verify(workbasketServiceMock, times(1)).checkAuthorization(any(), any()); verify(taskanaEngineImpl, times(1)).returnConnection(); verifyNoMoreInteractions(taskanaEngineConfigurationMock, taskanaEngineMock, taskanaEngineImpl, - taskMapperMock, objectReferenceMapperMock, workbasketServiceMock); + taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, + classificationServiceMock); throw e; } } @@ -614,6 +648,9 @@ public class TaskServiceImplTest { Timestamp now = new Timestamp(System.currentTimeMillis()); task.setCreated(now); task.setModified(now); + Classification classification = new Classification(); + classificationServiceMock.addClassification(classification); + task.setClassification(classification); return task; } diff --git a/lib/taskana-core/src/test/java/pro/taskana/impl/integration/SummaryServiceImplIntAutoCommitTest.java b/lib/taskana-core/src/test/java/pro/taskana/impl/integration/SummaryServiceImplIntAutoCommitTest.java index bb18f2211..d5b037946 100644 --- a/lib/taskana-core/src/test/java/pro/taskana/impl/integration/SummaryServiceImplIntAutoCommitTest.java +++ b/lib/taskana-core/src/test/java/pro/taskana/impl/integration/SummaryServiceImplIntAutoCommitTest.java @@ -127,7 +127,7 @@ public class SummaryServiceImplIntAutoCommitTest { dummyTask.setName("Dummy-Task"); dummyTask.setClassification(dummyClassification); dummyTask.setWorkbasketId(dummyWorkbasket.getId()); - dummyTask = taskServiceImpl.create(dummyTask); + dummyTask = taskServiceImpl.createTask(dummyTask); } @AfterClass diff --git a/lib/taskana-core/src/test/java/pro/taskana/impl/integration/TaskServiceImplIntAutocommitTest.java b/lib/taskana-core/src/test/java/pro/taskana/impl/integration/TaskServiceImplIntAutocommitTest.java index b3508e4aa..f4af654ea 100644 --- a/lib/taskana-core/src/test/java/pro/taskana/impl/integration/TaskServiceImplIntAutocommitTest.java +++ b/lib/taskana-core/src/test/java/pro/taskana/impl/integration/TaskServiceImplIntAutocommitTest.java @@ -1,24 +1,13 @@ package pro.taskana.impl.integration; -import java.io.FileNotFoundException; -import java.sql.SQLException; -import java.util.List; - -import javax.security.auth.login.LoginException; -import javax.sql.DataSource; - import org.h2.store.fs.FileUtils; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - +import org.junit.*; import pro.taskana.ClassificationQuery; import pro.taskana.ObjectReferenceQuery; import pro.taskana.TaskanaEngine; import pro.taskana.TaskanaEngine.ConnectionManagementMode; import pro.taskana.configuration.TaskanaEngineConfiguration; +import pro.taskana.exceptions.ClassificationNotFoundException; import pro.taskana.exceptions.NotAuthorizedException; import pro.taskana.exceptions.TaskNotFoundException; import pro.taskana.exceptions.WorkbasketNotFoundException; @@ -28,9 +17,16 @@ import pro.taskana.impl.TaskServiceImpl; import pro.taskana.impl.TaskanaEngineImpl; import pro.taskana.impl.configuration.DBCleaner; import pro.taskana.impl.configuration.TaskanaEngineConfigurationTest; -import pro.taskana.impl.util.IdGenerator; +import pro.taskana.model.Classification; import pro.taskana.model.Task; import pro.taskana.model.TaskState; +import pro.taskana.model.Workbasket; + +import javax.security.auth.login.LoginException; +import javax.sql.DataSource; +import java.io.FileNotFoundException; +import java.sql.SQLException; +import java.util.List; /** * Integration Test for TaskServiceImpl transactions with connection management mode AUTOCOMMIT. @@ -66,12 +62,19 @@ public class TaskServiceImplIntAutocommitTest { @Test public void testStart() throws FileNotFoundException, SQLException, TaskNotFoundException, - WorkbasketNotFoundException, NotAuthorizedException { + WorkbasketNotFoundException, NotAuthorizedException, ClassificationNotFoundException { + Workbasket wb = new Workbasket(); + wb.setName("workbasket"); + taskanaEngine.getWorkbasketService().createWorkbasket(wb); + Classification classification = new Classification(); + taskanaEngine.getClassificationService().addClassification(classification); + Task task = new Task(); task.setName("Unit Test Task"); - String id1 = IdGenerator.generateWithPrefix("TWB"); - task.setWorkbasketId(id1); - task = taskServiceImpl.create(task); + task.setWorkbasketId(wb.getId()); + task.setClassification(classification); + + task = taskServiceImpl.createTask(task); //skanaEngineImpl.getSqlSession().commit(); // needed so that the change is visible in the other session TaskanaEngine te2 = taskanaEngineConfiguration.buildTaskanaEngine(); @@ -82,43 +85,61 @@ public class TaskServiceImplIntAutocommitTest { @Test(expected = TaskNotFoundException.class) public void testStartTransactionFail() - throws FileNotFoundException, SQLException, TaskNotFoundException, NotAuthorizedException, WorkbasketNotFoundException { + throws FileNotFoundException, SQLException, TaskNotFoundException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { + Workbasket wb = new Workbasket(); + wb.setName("sdf"); + taskanaEngine.getWorkbasketService().createWorkbasket(wb); + Classification classification = new Classification(); + taskanaEngine.getClassificationService().addClassification(classification); + Task task = new Task(); task.setName("Unit Test Task"); - String id1 = IdGenerator.generateWithPrefix("TWB"); - task.setWorkbasketId("id1"); - task = taskServiceImpl.create(task); - taskServiceImpl.getTaskById(id1); + task.setWorkbasketId(wb.getId()); + task.setClassification(classification); + taskServiceImpl.createTask(task); + taskServiceImpl.getTaskById(task.getId()); TaskanaEngineImpl te2 = (TaskanaEngineImpl) taskanaEngineConfiguration.buildTaskanaEngine(); TaskServiceImpl taskServiceImpl2 = (TaskServiceImpl) te2.getTaskService(); - taskServiceImpl2.getTaskById(id1); + taskServiceImpl2.getTaskById(wb.getId()); } @Test public void testCreateTaskInTaskanaWithDefaultDb() - throws FileNotFoundException, SQLException, TaskNotFoundException, NotAuthorizedException, WorkbasketNotFoundException { + throws FileNotFoundException, SQLException, TaskNotFoundException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { TaskanaEngineConfiguration taskanaEngineConfiguration = new TaskanaEngineConfiguration(null, false, false); TaskanaEngine te = taskanaEngineConfiguration.buildTaskanaEngine(); TaskServiceImpl taskServiceImpl = (TaskServiceImpl) te.getTaskService(); + Workbasket wb = new Workbasket(); + wb.setName("workbasket"); + taskanaEngine.getWorkbasketService().createWorkbasket(wb); + Classification classification = new Classification(); + taskanaEngine.getClassificationService().addClassification(classification); + Task task = new Task(); task.setName("Unit Test Task"); - String id1 = IdGenerator.generateWithPrefix("TWB"); - task.setWorkbasketId(id1); - task = taskServiceImpl.create(task); + task.setWorkbasketId(wb.getId()); + task.setClassification(classification); + task = taskServiceImpl.createTask(task); Assert.assertNotNull(task); Assert.assertNotNull(task.getId()); } @Test - public void should_ReturnList_when_BuilderIsUsed() throws SQLException, NotAuthorizedException, WorkbasketNotFoundException { + public void should_ReturnList_when_BuilderIsUsed() throws SQLException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { + Workbasket wb = new Workbasket(); + wb.setName("workbasket"); + taskanaEngine.getWorkbasketService().createWorkbasket(wb); + Classification classification = new Classification(); + taskanaEngine.getClassificationService().addClassification(classification); + Task task = new Task(); task.setName("Unit Test Task"); - String id1 = IdGenerator.generateWithPrefix("TWB"); - task.setWorkbasketId(id1); - task = taskServiceImpl.create(task); + task.setWorkbasketId(wb.getId()); + task.setClassification(classification); + taskServiceImpl.createTask(task); TaskanaEngineImpl taskanaEngineImpl = (TaskanaEngineImpl) taskanaEngine; ClassificationQuery classificationQuery = new ClassificationQueryImpl(taskanaEngineImpl) diff --git a/lib/taskana-core/src/test/java/pro/taskana/impl/integration/TaskServiceImplIntExplicitTest.java b/lib/taskana-core/src/test/java/pro/taskana/impl/integration/TaskServiceImplIntExplicitTest.java index 2826c09c0..c263638b8 100644 --- a/lib/taskana-core/src/test/java/pro/taskana/impl/integration/TaskServiceImplIntExplicitTest.java +++ b/lib/taskana-core/src/test/java/pro/taskana/impl/integration/TaskServiceImplIntExplicitTest.java @@ -11,13 +11,9 @@ import pro.taskana.exceptions.ClassificationNotFoundException; import pro.taskana.exceptions.NotAuthorizedException; import pro.taskana.exceptions.TaskNotFoundException; import pro.taskana.exceptions.WorkbasketNotFoundException; -import pro.taskana.impl.ClassificationQueryImpl; -import pro.taskana.impl.ObjectReferenceQueryImpl; -import pro.taskana.impl.TaskServiceImpl; -import pro.taskana.impl.TaskanaEngineImpl; +import pro.taskana.impl.*; import pro.taskana.impl.configuration.DBCleaner; import pro.taskana.impl.configuration.TaskanaEngineConfigurationTest; -import pro.taskana.impl.util.IdGenerator; import pro.taskana.model.*; import javax.security.auth.login.LoginException; @@ -63,14 +59,12 @@ public class TaskServiceImplIntExplicitTest { } @Test - public void testStart() throws FileNotFoundException, SQLException, TaskNotFoundException, NotAuthorizedException, WorkbasketNotFoundException { + public void testStart() throws FileNotFoundException, SQLException, TaskNotFoundException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { Connection connection = dataSource.getConnection(); taskanaEngineImpl.setConnection(connection); - Task task = new Task(); - task.setName("Unit Test Task"); - String id1 = IdGenerator.generateWithPrefix("TWB"); - task.setWorkbasketId(id1); - task = taskServiceImpl.create(task); + + Task task = this.generateDummyTask(); + task = taskServiceImpl.createTask(task); connection.commit(); // needed so that the change is visible in the other session TaskanaEngine te2 = taskanaEngineConfiguration.buildTaskanaEngine(); @@ -82,40 +76,54 @@ public class TaskServiceImplIntExplicitTest { @Test(expected = TaskNotFoundException.class) public void testStartTransactionFail() - throws FileNotFoundException, SQLException, TaskNotFoundException, NotAuthorizedException, WorkbasketNotFoundException { + throws FileNotFoundException, SQLException, TaskNotFoundException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { Connection connection = dataSource.getConnection(); taskanaEngineImpl.setConnection(connection); // taskServiceImpl = (TaskServiceImpl) taskanaEngine.getTaskService(); + Workbasket workbasket = new Workbasket(); + workbasket.setName("workbasket"); + Classification classification = new Classification(); + taskanaEngine.getWorkbasketService().createWorkbasket(workbasket); + taskanaEngine.getClassificationService().addClassification(classification); + Task task = new Task(); task.setName("Unit Test Task"); - String id1 = IdGenerator.generateWithPrefix("TWB"); - task.setWorkbasketId("id1"); - task = taskServiceImpl.create(task); + task.setWorkbasketId(workbasket.getId()); + task.setClassification(classification); + task = taskServiceImpl.createTask(task); connection.commit(); - taskServiceImpl.getTaskById(id1); + taskServiceImpl.getTaskById(workbasket.getId()); TaskanaEngineImpl te2 = (TaskanaEngineImpl) taskanaEngineConfiguration.buildTaskanaEngine(); TaskServiceImpl taskServiceImpl2 = (TaskServiceImpl) te2.getTaskService(); - taskServiceImpl2.getTaskById(id1); + taskServiceImpl2.getTaskById(workbasket.getId()); connection.commit(); } @Test public void testCreateTaskInTaskanaWithDefaultDb() - throws FileNotFoundException, SQLException, TaskNotFoundException, NotAuthorizedException, WorkbasketNotFoundException { + throws FileNotFoundException, SQLException, TaskNotFoundException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { DataSource ds = TaskanaEngineConfiguration.createDefaultDataSource(); TaskanaEngineConfiguration taskanaEngineConfiguration = new TaskanaEngineConfiguration(ds, false, false); TaskanaEngine te = taskanaEngineConfiguration.buildTaskanaEngine(); Connection connection = ds.getConnection(); te.setConnection(connection); TaskServiceImpl taskServiceImpl = (TaskServiceImpl) te.getTaskService(); + WorkbasketServiceImpl workbasketServiceImpl = (WorkbasketServiceImpl) te.getWorkbasketService(); + ClassificationServiceImpl classificationServiceImpl = (ClassificationServiceImpl) te.getClassificationService(); + + Workbasket workbasket = new Workbasket(); + workbasket.setName("workbasket"); + Classification classification = new Classification(); + workbasketServiceImpl.createWorkbasket(workbasket); + classificationServiceImpl.addClassification(classification); Task task = new Task(); task.setName("Unit Test Task"); - String id1 = IdGenerator.generateWithPrefix("TWB"); - task.setWorkbasketId(id1); - task = taskServiceImpl.create(task); + task.setWorkbasketId(workbasket.getId()); + task.setClassification(classification); + task = taskServiceImpl.createTask(task); Assert.assertNotNull(task); Assert.assertNotNull(task.getId()); @@ -124,16 +132,11 @@ public class TaskServiceImplIntExplicitTest { } @Test - public void testCreateManualTask() throws SQLException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { + public void testCreateTaskWithCustomsAndPlanned() throws SQLException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { Connection connection = dataSource.getConnection(); taskanaEngineImpl.setConnection(connection); - Workbasket workbasket = new Workbasket(); - workbasket.setName("workbasket1"); - taskanaEngine.getWorkbasketService().createWorkbasket(workbasket); - Classification classification = new Classification(); - classification.setDomain("domain"); classification.setCategory("MANUAL"); classification.setName("classification name"); classification.setServiceLevel("P1D"); @@ -146,32 +149,49 @@ public class TaskServiceImplIntExplicitTest { objectReference.setValue("4444"); objectReference.setType("type"); - Task test = taskServiceImpl.createManualTask(workbasket.getId(), classification.getId(), "domain", null, "Name", null, objectReference, null); + Timestamp tomorrow = Timestamp.valueOf(LocalDateTime.now().plusDays(1)); - Assert.assertEquals(test.getPlanned(), test.getCreated()); + Task test = this.generateDummyTask(); + test.setClassification(classification); + test.setName("Name"); + test.setPrimaryObjRef(objectReference); + test.setPlanned(tomorrow); + test = taskServiceImpl.createTask(test); + + Assert.assertNotEquals(test.getPlanned(), test.getCreated()); Assert.assertNotNull(test.getDue()); - Timestamp tomorrow = Timestamp.valueOf(LocalDateTime.now().plusDays(1)); Map customs = new HashMap(); customs.put("Daimler", "Tons of money. And cars. And gold."); customs.put("Audi", 2); - Task test2 = taskServiceImpl.createManualTask(workbasket.getId(), classification.getId(), "domain", tomorrow, "Name2", "desc", objectReference, customs); + Task test2 = new Task(); + test2.setWorkbasketId(test.getWorkbasketId()); + test2.setClassification(classification); + test2.setPrimaryObjRef(objectReference); + test2.setName("Name2"); + test2.setDescription("desc"); + test2.setCustomAttributes(customs); + test2.setCustom1("Daimler"); + test2.setCustom5("BMW"); + taskServiceImpl.createTask(test2); + + Assert.assertEquals(test2.getPlanned(), test2.getCreated()); + + Assert.assertEquals(2 + 1, test2.getCustomAttributes().size()); Assert.assertEquals(test.getClassification().getId(), test2.getClassification().getId()); - Assert.assertTrue(test.getDue().before(test2.getDue())); + Assert.assertTrue(test.getDue().after(test2.getPlanned())); } @Test(expected = WorkbasketNotFoundException.class) - public void createManualTaskShouldThrowWorkbasketNotFoundException() throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException, SQLException { + public void createTaskShouldThrowWorkbasketNotFoundException() throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException, SQLException { Connection connection = dataSource.getConnection(); taskanaEngineImpl.setConnection(connection); - Workbasket workbasket = new Workbasket(); - workbasket.setName("wb"); - taskanaEngine.getWorkbasketService().createWorkbasket(workbasket); - - taskServiceImpl.createManualTask("1", "classification", "domain", null, null, null, null, null); + Task test = this.generateDummyTask(); + test.setWorkbasketId("1"); + taskServiceImpl.createTask(test); } @Test(expected = ClassificationNotFoundException.class) @@ -179,41 +199,28 @@ public class TaskServiceImplIntExplicitTest { Connection connection = dataSource.getConnection(); taskanaEngineImpl.setConnection(connection); - Workbasket workbasket = new Workbasket(); - workbasket.setName("wb"); - taskanaEngine.getWorkbasketService().createWorkbasket(workbasket); - - Classification classification = new Classification(); - taskanaEngine.getClassificationService().addClassification(classification); - - taskServiceImpl.createManualTask(workbasket.getId(), "classification", "domain", null, null, null, null, null); - } - - @Test(expected = NotAuthorizedException.class) - public void createManualTaskShouldThrowNotAuthorizedException() throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException, SQLException { - Connection connection = dataSource.getConnection(); - taskanaEngineImpl.setConnection(connection); - - Workbasket workbasket = new Workbasket(); - workbasket.setName("wb"); - taskanaEngine.getWorkbasketService().createWorkbasket(workbasket); - - Classification classification = new Classification(); - taskanaEngine.getClassificationService().addClassification(classification); - - taskServiceImpl.createManualTask(workbasket.getId(), classification.getId(), "domain", null, null, null, null, null); + Task test = this.generateDummyTask(); + test.setClassification(new Classification()); + taskServiceImpl.createTask(test); } @Test - public void should_ReturnList_when_BuilderIsUsed() throws SQLException, NotAuthorizedException, WorkbasketNotFoundException { + public void should_ReturnList_when_BuilderIsUsed() throws SQLException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { Connection connection = dataSource.getConnection(); taskanaEngineImpl.setConnection(connection); + + Workbasket workbasket = new Workbasket(); + workbasket.setName("workbasket"); + Classification classification = new Classification(); + taskanaEngine.getWorkbasketService().createWorkbasket(workbasket); + taskanaEngine.getClassificationService().addClassification(classification); + Task task = new Task(); task.setName("Unit Test Task"); - String id1 = IdGenerator.generateWithPrefix("TWB"); - task.setWorkbasketId(id1); - task = taskServiceImpl.create(task); + task.setWorkbasketId(workbasket.getId()); + task.setClassification(classification); + task = taskServiceImpl.createTask(task); TaskanaEngineImpl taskanaEngineImpl = (TaskanaEngineImpl) taskanaEngine; ClassificationQuery classificationQuery = new ClassificationQueryImpl(taskanaEngineImpl) @@ -233,6 +240,20 @@ public class TaskServiceImplIntExplicitTest { connection.commit(); } + private Task generateDummyTask() { + Workbasket workbasket = new Workbasket(); + workbasket.setName("wb"); + taskanaEngine.getWorkbasketService().createWorkbasket(workbasket); + + Classification classification = new Classification(); + taskanaEngine.getClassificationService().addClassification(classification); + + Task task = new Task(); + task.setWorkbasketId(workbasket.getId()); + task.setClassification(classification); + return task; + } + @After public void cleanUp() { taskanaEngineImpl.setConnection(null); diff --git a/lib/taskana-spring-example/src/main/java/pro/taskana/ExampleBootstrap.java b/lib/taskana-spring-example/src/main/java/pro/taskana/ExampleBootstrap.java index 5da6d8c54..57b36646f 100644 --- a/lib/taskana-spring-example/src/main/java/pro/taskana/ExampleBootstrap.java +++ b/lib/taskana-spring-example/src/main/java/pro/taskana/ExampleBootstrap.java @@ -1,15 +1,16 @@ package pro.taskana; -import javax.annotation.PostConstruct; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; +import pro.taskana.exceptions.ClassificationNotFoundException; import pro.taskana.exceptions.NotAuthorizedException; import pro.taskana.exceptions.TaskNotFoundException; import pro.taskana.exceptions.WorkbasketNotFoundException; import pro.taskana.model.Task; +import javax.annotation.PostConstruct; + @Component @Transactional public class ExampleBootstrap { @@ -18,12 +19,12 @@ public class ExampleBootstrap { private TaskService taskService; @PostConstruct - public void test() throws TaskNotFoundException, NotAuthorizedException, WorkbasketNotFoundException { + public void test() throws TaskNotFoundException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { System.out.println("---------------------------> Start App"); Task task = new Task(); task.setName("Spring example task"); task.setWorkbasketId("1"); - task = taskService.create(task); + task = taskService.createTask(task); System.out.println("---------------------------> Task started: " + task.getId()); taskService.claim(task.getId(), "John Doe"); System.out.println( diff --git a/lib/taskana-spring/src/test/java/pro/taskana/TaskanaComponent.java b/lib/taskana-spring/src/test/java/pro/taskana/TaskanaComponent.java index c996899d2..d87a3bd39 100644 --- a/lib/taskana-spring/src/test/java/pro/taskana/TaskanaComponent.java +++ b/lib/taskana-spring/src/test/java/pro/taskana/TaskanaComponent.java @@ -3,8 +3,10 @@ package pro.taskana; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; +import pro.taskana.exceptions.ClassificationNotFoundException; import pro.taskana.exceptions.NotAuthorizedException; import pro.taskana.exceptions.WorkbasketNotFoundException; +import pro.taskana.model.Classification; import pro.taskana.model.Task; @Component @@ -18,11 +20,11 @@ public class TaskanaComponent { return taskService; } - public void triggerRollback() throws NotAuthorizedException, WorkbasketNotFoundException { + public void triggerRollback() throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { Task task = new Task(); task.setName("Unit Test Task"); task.setWorkbasketId("1"); - task = taskService.create(task); + task = taskService.createTask(task); throw new RuntimeException(); } } diff --git a/rest/src/main/java/pro/taskana/rest/TaskController.java b/rest/src/main/java/pro/taskana/rest/TaskController.java index ab10ad7a8..348ba6593 100644 --- a/rest/src/main/java/pro/taskana/rest/TaskController.java +++ b/rest/src/main/java/pro/taskana/rest/TaskController.java @@ -106,7 +106,7 @@ public class TaskController { @RequestMapping(method = RequestMethod.POST) public ResponseEntity createTask(@RequestBody Task task) { try { - Task createdTask = taskService.create(task); + Task createdTask = taskService.createTask(task); return ResponseEntity.status(HttpStatus.CREATED).body(createdTask); } catch (Exception e) { logger.error("Something went wrong: ", e); From 0a21f0d96fd2755e54845f96e00e6865fd8e8cfe Mon Sep 17 00:00:00 2001 From: BVier <26220150+BVier@users.noreply.github.com> Date: Thu, 7 Dec 2017 14:14:38 +0100 Subject: [PATCH 2/2] Remove custom synchronization in Task and change order of wb-invocations --- .../pro/taskana/impl/TaskServiceImpl.java | 76 +------------------ .../pro/taskana/impl/TaskServiceImplTest.java | 7 +- .../TaskServiceImplIntExplicitTest.java | 18 +---- 3 files changed, 9 insertions(+), 92 deletions(-) diff --git a/lib/taskana-core/src/main/java/pro/taskana/impl/TaskServiceImpl.java b/lib/taskana-core/src/main/java/pro/taskana/impl/TaskServiceImpl.java index b5d7f2901..302c2538d 100644 --- a/lib/taskana-core/src/main/java/pro/taskana/impl/TaskServiceImpl.java +++ b/lib/taskana-core/src/main/java/pro/taskana/impl/TaskServiceImpl.java @@ -105,8 +105,8 @@ public class TaskServiceImpl implements TaskService { LOGGER.debug("entry to createTask(task = {})", task); try { taskanaEngineImpl.openConnection(); - taskanaEngine.getWorkbasketService().checkAuthorization(task.getWorkbasketId(), WorkbasketAuthorization.APPEND); Workbasket wb = taskanaEngine.getWorkbasketService().getWorkbasket(task.getWorkbasketId()); + taskanaEngine.getWorkbasketService().checkAuthorization(task.getWorkbasketId(), WorkbasketAuthorization.APPEND); Classification classification = task.getClassification(); if (classification == null) { @@ -338,79 +338,5 @@ public class TaskServiceImpl implements TaskService { } task.setPrimaryObjRef(objectReference); } - - //guarantees redundancy of customers and customAttributes - this.checkCustomAttributes(task); - } - - private void checkCustomAttributes(Task task) { - List customList = new ArrayList<>(); - if (task.getCustom1() != null) { - customList.add(task.getCustom1()); - } - if (task.getCustom2() != null) { - customList.add(task.getCustom2()); - } - if (task.getCustom3() != null) { - customList.add(task.getCustom3()); - } - if (task.getCustom4() != null) { - customList.add(task.getCustom4()); - } - if (task.getCustom5() != null) { - customList.add(task.getCustom5()); - } - if (task.getCustom6() != null) { - customList.add(task.getCustom6()); - } - if (task.getCustom7() != null) { - customList.add(task.getCustom7()); - } - if (task.getCustom8() != null) { - customList.add(task.getCustom8()); - } - if (task.getCustom9() != null) { - customList.add(task.getCustom9()); - } - if (task.getCustom10() != null) { - customList.add(task.getCustom10()); - } - - if (task.getCustomAttributes() != null) { - for (String custom : task.getCustomAttributes().keySet()) { - if (!customList.isEmpty() && customList.contains(custom)) { - if (task.getCustom1() == null) { - task.setCustom1(custom); - } else if (task.getCustom2() == null) { - task.setCustom2(custom); - } else if (task.getCustom3() == null) { - task.setCustom3(custom); - } else if (task.getCustom4() == null) { - task.setCustom4(custom); - } else if (task.getCustom5() == null) { - task.setCustom5(custom); - } else if (task.getCustom6() == null) { - task.setCustom6(custom); - } else if (task.getCustom7() == null) { - task.setCustom7(custom); - } else if (task.getCustom8() == null) { - task.setCustom8(custom); - } else if (task.getCustom9() == null) { - task.setCustom9(custom); - } else if (task.getCustom10() == null) { - task.setCustom10(custom); - } else { - break; - } - } - } - } - if (!customList.isEmpty()) { - for (String custom : customList) { - if (!task.getCustomAttributes().containsKey(custom)) { - task.getCustomAttributes().put(custom, null); - } - } - } } } diff --git a/lib/taskana-core/src/test/java/pro/taskana/impl/TaskServiceImplTest.java b/lib/taskana-core/src/test/java/pro/taskana/impl/TaskServiceImplTest.java index c87ae0447..36a54e1a9 100644 --- a/lib/taskana-core/src/test/java/pro/taskana/impl/TaskServiceImplTest.java +++ b/lib/taskana-core/src/test/java/pro/taskana/impl/TaskServiceImplTest.java @@ -261,7 +261,8 @@ public class TaskServiceImplTest { cut.createTask(task); } catch (Exception e) { verify(taskanaEngineImpl, times(1)).openConnection(); - verify(taskanaEngineMock, times(1)).getWorkbasketService(); + verify(taskanaEngineMock, times(2)).getWorkbasketService(); + verify(workbasketServiceMock, times(1)).getWorkbasket(any()); verify(workbasketServiceMock, times(1)).checkAuthorization(any(), any()); verify(taskanaEngineImpl, times(1)).returnConnection(); verifyNoMoreInteractions(taskanaEngineConfigurationMock, taskanaEngineMock, taskanaEngineImpl, @@ -274,7 +275,7 @@ public class TaskServiceImplTest { @Test(expected = WorkbasketNotFoundException.class) public void testCreateThrowsWorkbasketNotFoundException() throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { try { - Mockito.doThrow(WorkbasketNotFoundException.class).when(workbasketServiceMock).checkAuthorization(any(), any()); + Mockito.doThrow(WorkbasketNotFoundException.class).when(workbasketServiceMock).getWorkbasket(any()); Task task = new Task(); task.setWorkbasketId("1"); @@ -282,7 +283,7 @@ public class TaskServiceImplTest { } catch (Exception e) { verify(taskanaEngineImpl, times(1)).openConnection(); verify(taskanaEngineMock, times(1)).getWorkbasketService(); - verify(workbasketServiceMock, times(1)).checkAuthorization(any(), any()); + verify(workbasketServiceMock, times(1)).getWorkbasket(any()); verify(taskanaEngineImpl, times(1)).returnConnection(); verifyNoMoreInteractions(taskanaEngineConfigurationMock, taskanaEngineMock, taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, diff --git a/lib/taskana-core/src/test/java/pro/taskana/impl/integration/TaskServiceImplIntExplicitTest.java b/lib/taskana-core/src/test/java/pro/taskana/impl/integration/TaskServiceImplIntExplicitTest.java index c263638b8..e10d2571a 100644 --- a/lib/taskana-core/src/test/java/pro/taskana/impl/integration/TaskServiceImplIntExplicitTest.java +++ b/lib/taskana-core/src/test/java/pro/taskana/impl/integration/TaskServiceImplIntExplicitTest.java @@ -23,9 +23,7 @@ import java.sql.Connection; import java.sql.SQLException; import java.sql.Timestamp; import java.time.LocalDateTime; -import java.util.HashMap; import java.util.List; -import java.util.Map; /** * Integration Test for TaskServiceImpl transactions with connection management mode EXPLICIT. @@ -132,7 +130,7 @@ public class TaskServiceImplIntExplicitTest { } @Test - public void testCreateTaskWithCustomsAndPlanned() throws SQLException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { + public void testCreateTaskWithPlannedAndName() throws SQLException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException { Connection connection = dataSource.getConnection(); taskanaEngineImpl.setConnection(connection); @@ -161,27 +159,19 @@ public class TaskServiceImplIntExplicitTest { Assert.assertNotEquals(test.getPlanned(), test.getCreated()); Assert.assertNotNull(test.getDue()); - Map customs = new HashMap(); - customs.put("Daimler", "Tons of money. And cars. And gold."); - customs.put("Audi", 2); - - Task test2 = new Task(); test2.setWorkbasketId(test.getWorkbasketId()); test2.setClassification(classification); test2.setPrimaryObjRef(objectReference); - test2.setName("Name2"); test2.setDescription("desc"); - test2.setCustomAttributes(customs); - test2.setCustom1("Daimler"); - test2.setCustom5("BMW"); taskServiceImpl.createTask(test2); Assert.assertEquals(test2.getPlanned(), test2.getCreated()); + Assert.assertTrue(test2.getName().equals(classification.getName())); - Assert.assertEquals(2 + 1, test2.getCustomAttributes().size()); Assert.assertEquals(test.getClassification().getId(), test2.getClassification().getId()); - Assert.assertTrue(test.getDue().after(test2.getPlanned())); + Assert.assertTrue(test.getDue().after(test2.getDue())); + Assert.assertFalse(test.getName().equals(test2.getName())); } @Test(expected = WorkbasketNotFoundException.class)