Skip to content

Commit

Permalink
adds checking whether attachment has a proper taskId set when creatin…
Browse files Browse the repository at this point in the history
…g a task.

In other words, throw an InvalidArgumentException and therefor a BadRequest in the REST API

adds test for that isssue

Signed-off-by: Kálmán Képes <[email protected]>
  • Loading branch information
nyuuyn committed Aug 2, 2023
1 parent fbd388e commit 8ccdfb1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<TaskRepresentationModel> 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();
Expand Down

0 comments on commit 8ccdfb1

Please sign in to comment.