Skip to content

Commit

Permalink
TSK-1824: throw Exception in REST if Attachment has taskId
Browse files Browse the repository at this point in the history
  • Loading branch information
ryzheboka committed Mar 31, 2022
1 parent 52f102f commit c0128e0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import pro.taskana.task.api.TaskService;
import pro.taskana.task.api.TaskState;
import pro.taskana.task.api.exceptions.TaskAlreadyExistException;
import pro.taskana.task.api.models.Attachment;
import pro.taskana.task.api.models.AttachmentSummary;
import pro.taskana.task.api.models.ObjectReference;
import pro.taskana.task.api.models.Task;
Expand Down Expand Up @@ -730,21 +729,6 @@ void testCreateTaskWithWorkbasketMarkedForDeletion() throws Exception {
.isInstanceOf(WorkbasketNotFoundException.class);
}

// Rewritten
@WithAccessId(user = "user-1-1")
@Test
void should_ThrowException_When_CreatingTaskWithAttachmentClassificationNull() {
TaskImpl task = (TaskImpl) makeNewTask(taskService);
Attachment attachment = taskService.newAttachment();
attachment.setObjectReference(
createObjectReference("COMPANY_A", "SYSTEM_A", "INSTANCE_A", "VNR", "1234567"));
task.addAttachment(attachment);

assertThatThrownBy(() -> taskService.createTask(task))
.isInstanceOf(InvalidArgumentException.class)
.hasMessage("Classification of Attachment must not be null.");
}

private Task setTaskProperties(Task task) {
task.setClassificationKey("L12010");
task.setPrimaryObjRef(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import pro.taskana.task.api.models.TaskSummary;
import pro.taskana.task.rest.assembler.TaskRepresentationModelAssembler;
import pro.taskana.task.rest.assembler.TaskSummaryRepresentationModelAssembler;
import pro.taskana.task.rest.models.AttachmentRepresentationModel;
import pro.taskana.task.rest.models.TaskRepresentationModel;
import pro.taskana.task.rest.models.TaskSummaryCollectionRepresentationModel;
import pro.taskana.task.rest.models.TaskSummaryPagedRepresentationModel;
Expand Down Expand Up @@ -342,6 +343,7 @@ public ResponseEntity<TaskRepresentationModel> createTask(
throws WorkbasketNotFoundException, ClassificationNotFoundException, NotAuthorizedException,
TaskAlreadyExistException, InvalidArgumentException, AttachmentPersistenceException,
ObjectReferencePersistenceException {
verifyTaskIdOfAttachments(taskRepresentationModel.getAttachments(), null);
Task fromResource = taskRepresentationModelAssembler.toEntityModel(taskRepresentationModel);
Task createdTask = taskService.createTask(fromResource);

Expand Down Expand Up @@ -411,11 +413,23 @@ public ResponseEntity<TaskRepresentationModel> updateTask(
+ "object in the payload which should be updated. ID=('%s')",
taskId, taskRepresentationModel.getTaskId()));
}
verifyTaskIdOfAttachments(
taskRepresentationModel.getAttachments(), taskRepresentationModel.getTaskId());
Task task = taskRepresentationModelAssembler.toEntityModel(taskRepresentationModel);
task = taskService.updateTask(task);
return ResponseEntity.ok(taskRepresentationModelAssembler.toModel(task));
}

private void verifyTaskIdOfAttachments(
List<AttachmentRepresentationModel> attachments, String taskId)
throws InvalidArgumentException {
for (AttachmentRepresentationModel attachment : attachments) {
if (attachment.getTaskId() != null && !attachment.getTaskId().equals(taskId)) {
throw new InvalidArgumentException("TaskId of Attachment should be empty.");
}
}
}

public enum TaskQuerySortBy implements QuerySortBy<TaskQuery> {
CLASSIFICATION_KEY(TaskQuery::orderByClassificationKey),
CLASSIFICATION_NAME(TaskQuery::orderByClassificationName),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import pro.taskana.common.test.rest.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.ObjectReferenceRepresentationModel;
import pro.taskana.task.rest.models.TaskRepresentationModel;
import pro.taskana.task.rest.models.TaskRepresentationModel.CustomAttribute;
Expand Down Expand Up @@ -653,6 +654,29 @@ void should_NotGetEmptyObjectReferencesList_When_GettingTaskWithObjectReferences
assertThat(repModel.getSecondaryObjectReferences()).isNotEmpty();
}

@Test
void should_ThrowException_When_AttachmentAlreadyHasTaskId() {
TaskRepresentationModel taskRepresentationModel = getTaskResourceSample();
AttachmentRepresentationModel attachment = new AttachmentRepresentationModel();
attachment.setTaskId("TKI:1");
attachment.setClassificationSummary(taskRepresentationModel.getClassificationSummary());
attachment.setObjectReference(taskRepresentationModel.getPrimaryObjRef());
taskRepresentationModel.setAttachments(List.of(attachment));

HttpEntity<Object> auth =
new HttpEntity<>(taskRepresentationModel, RestHelper.generateHeadersForUser("admin"));
String url = restHelper.toUrl(RestEndpoints.URL_TASKS);

ThrowingCallable httpCall =
() -> TEMPLATE.exchange(url, HttpMethod.POST, auth, TASK_MODEL_TYPE);
assertThatThrownBy(httpCall)
.isInstanceOf(HttpStatusCodeException.class)
.hasMessageContaining("TaskId of Attachment should be empty")
.extracting(HttpStatusCodeException.class::cast)
.extracting(HttpStatusCodeException::getStatusCode)
.isEqualTo(HttpStatus.BAD_REQUEST);
}

@Test
void should_ReturnFilteredTasks_WhenGettingTasksBySecondaryObjectReferenceValue() {
String url = restHelper.toUrl(RestEndpoints.URL_TASKS) + "?sor-value=Value2";
Expand Down

0 comments on commit c0128e0

Please sign in to comment.