diff --git a/lib/taskana-core/src/main/java/pro/taskana/task/internal/TaskServiceImpl.java b/lib/taskana-core/src/main/java/pro/taskana/task/internal/TaskServiceImpl.java index 3d76f8bfba..17955f7914 100644 --- a/lib/taskana-core/src/main/java/pro/taskana/task/internal/TaskServiceImpl.java +++ b/lib/taskana-core/src/main/java/pro/taskana/task/internal/TaskServiceImpl.java @@ -247,6 +247,13 @@ public Task createTask(Task taskToCreate) throw new InvalidArgumentException("taskId must be empty when creating a task"); } + if (Objects.nonNull(task.getAttachments()) && !task.getAttachments().isEmpty() + && task.getAttachments().stream() + .anyMatch(a -> Objects.nonNull(a.getTaskId()) && !a.getTaskId().isEmpty())) { + throw new InvalidArgumentException( + "attachments' taskId must be empty when creating a task"); + } + if (LOGGER.isDebugEnabled()) { LOGGER.debug("Task {} cannot be found, so it can be created.", task.getId()); } diff --git a/rest/taskana-rest-spring/src/test/java/pro/taskana/task/rest/TaskControllerIntTest.java b/rest/taskana-rest-spring/src/test/java/pro/taskana/task/rest/TaskControllerIntTest.java index 5147319217..b6dbc8c564 100644 --- a/rest/taskana-rest-spring/src/test/java/pro/taskana/task/rest/TaskControllerIntTest.java +++ b/rest/taskana-rest-spring/src/test/java/pro/taskana/task/rest/TaskControllerIntTest.java @@ -19,6 +19,7 @@ import java.util.stream.Stream; import javax.sql.DataSource; import org.assertj.core.api.ThrowableAssert.ThrowingCallable; +import org.assertj.core.util.Lists; import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @@ -42,6 +43,7 @@ import pro.taskana.rest.test.TaskanaSpringBootTest; import pro.taskana.sampledata.SampleDataGenerator; import pro.taskana.task.api.TaskState; +import pro.taskana.task.rest.models.AttachmentRepresentationModel; import pro.taskana.task.rest.models.IsReadRepresentationModel; import pro.taskana.task.rest.models.ObjectReferenceRepresentationModel; import pro.taskana.task.rest.models.TaskRepresentationModel; @@ -114,6 +116,34 @@ void should_UpdateTaskOwnerOfReadyForReviewTask() { assertThat(theUpdatedTaskRepresentationModel.getOwner()).isEqualTo("dummyUser"); } + private ObjectReferenceRepresentationModel getObjectReferenceResourceSample() { + ObjectReferenceRepresentationModel objectReference = new ObjectReferenceRepresentationModel(); + objectReference.setCompany("MyCompany1"); + objectReference.setSystem("MySystem1"); + objectReference.setSystemInstance("MyInstance1"); + objectReference.setType("MyType1"); + objectReference.setValue("00000001"); + return objectReference; + } + + private AttachmentRepresentationModel getAttachmentResourceSample() { + AttachmentRepresentationModel attachmentRepresentationModel = + new AttachmentRepresentationModel(); + attachmentRepresentationModel.setAttachmentId("A11010"); + + attachmentRepresentationModel.setObjectReference(getObjectReferenceResourceSample()); + + ClassificationSummaryRepresentationModel classificationSummaryRepresentationModel = + new ClassificationSummaryRepresentationModel(); + classificationSummaryRepresentationModel + .setClassificationId("CLI:100000000000000000000000000000000004"); + classificationSummaryRepresentationModel.setKey("L11010"); + + attachmentRepresentationModel + .setClassificationSummary(classificationSummaryRepresentationModel); + return attachmentRepresentationModel; + } + private TaskRepresentationModel getTaskResourceSample() { ClassificationSummaryRepresentationModel classificationResource = new ClassificationSummaryRepresentationModel(); @@ -1475,6 +1505,27 @@ void should_CreateAndDeleteTask() { assertThat(responseDeleted.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT); } + @Test + void should_CreateTaskWithError_When_SpecifyingAttachmentWrong() { + TaskRepresentationModel taskRepresentationModel = getTaskResourceSample(); + AttachmentRepresentationModel attachmentRepresentationModel = getAttachmentResourceSample(); + attachmentRepresentationModel.setTaskId(taskRepresentationModel.getTaskId() + "wrongId"); + taskRepresentationModel.setAttachments(Lists.newArrayList(attachmentRepresentationModel)); + + String url = restHelper.toUrl(RestEndpoints.URL_TASKS); + HttpEntity auth = + new HttpEntity<>( + taskRepresentationModel, RestHelper.generateHeadersForUser("teamlead-1")); + + ThrowingCallable httpCall = + () -> TEMPLATE.exchange(url, HttpMethod.POST, auth, TASK_MODEL_TYPE); + + assertThatThrownBy(httpCall) + .extracting(HttpStatusCodeException.class::cast) + .extracting(HttpStatusCodeException::getStatusCode) + .isEqualTo(HttpStatus.BAD_REQUEST); + } + @Test void should_CreateAndDeleteTaskWithSecondaryObjectReferences_When_SpecifyingObjectReferences() { TaskRepresentationModel taskRepresentationModel = getTaskResourceSample();