From 88862e84073e5dc9e324a92457dd883cddd8bf75 Mon Sep 17 00:00:00 2001 From: arolfes Date: Mon, 30 Jan 2023 15:57:51 +0100 Subject: [PATCH] Closes #1984 - Refactor TaskCommentAccTests to use Test-API --- .../delete/DeleteTaskCommentAccTest.java | 174 +++++++ .../query/QueryTaskCommentAccTest.java | 488 ++++++++++++++++++ .../delete/DeleteTaskCommentAccTest.java | 128 ----- .../query/QueryTaskCommentAccTest.java | 343 ------------ 4 files changed, 662 insertions(+), 471 deletions(-) create mode 100644 lib/taskana-core-test/src/test/java/acceptance/taskcomment/delete/DeleteTaskCommentAccTest.java create mode 100644 lib/taskana-core-test/src/test/java/acceptance/taskcomment/query/QueryTaskCommentAccTest.java delete mode 100644 lib/taskana-core/src/test/java/acceptance/taskcomment/delete/DeleteTaskCommentAccTest.java delete mode 100644 lib/taskana-core/src/test/java/acceptance/taskcomment/query/QueryTaskCommentAccTest.java diff --git a/lib/taskana-core-test/src/test/java/acceptance/taskcomment/delete/DeleteTaskCommentAccTest.java b/lib/taskana-core-test/src/test/java/acceptance/taskcomment/delete/DeleteTaskCommentAccTest.java new file mode 100644 index 0000000000..80c27e5808 --- /dev/null +++ b/lib/taskana-core-test/src/test/java/acceptance/taskcomment/delete/DeleteTaskCommentAccTest.java @@ -0,0 +1,174 @@ +package acceptance.taskcomment.delete; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.catchThrowableOfType; + +import java.time.Instant; +import java.util.List; +import org.assertj.core.api.ThrowableAssert.ThrowingCallable; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestTemplate; +import pro.taskana.classification.api.ClassificationService; +import pro.taskana.classification.api.models.Classification; +import pro.taskana.common.api.TaskanaEngine; +import pro.taskana.common.api.exceptions.InvalidArgumentException; +import pro.taskana.common.api.exceptions.SystemException; +import pro.taskana.task.api.TaskService; +import pro.taskana.task.api.exceptions.NotAuthorizedOnTaskCommentException; +import pro.taskana.task.api.exceptions.TaskCommentNotFoundException; +import pro.taskana.task.api.exceptions.TaskNotFoundException; +import pro.taskana.task.api.models.Task; +import pro.taskana.task.api.models.TaskComment; +import pro.taskana.testapi.DefaultTestEntities; +import pro.taskana.testapi.TaskanaInject; +import pro.taskana.testapi.TaskanaIntegrationTest; +import pro.taskana.testapi.builder.TaskBuilder; +import pro.taskana.testapi.builder.TaskCommentBuilder; +import pro.taskana.testapi.builder.WorkbasketAccessItemBuilder; +import pro.taskana.testapi.security.WithAccessId; +import pro.taskana.workbasket.api.WorkbasketPermission; +import pro.taskana.workbasket.api.WorkbasketService; +import pro.taskana.workbasket.api.exceptions.NotAuthorizedOnWorkbasketException; +import pro.taskana.workbasket.api.models.Workbasket; + +@TaskanaIntegrationTest +class DeleteTaskCommentAccTest { + + @TaskanaInject TaskService taskService; + @TaskanaInject ClassificationService classificationService; + @TaskanaInject WorkbasketService workbasketService; + @TaskanaInject TaskanaEngine taskanaEngine; + + Classification defaultClassification; + Workbasket defaultWorkbasket; + Task task1; + TaskComment comment1; + + @WithAccessId(user = "admin") + @BeforeAll + void setup() throws Exception { + defaultClassification = + DefaultTestEntities.defaultTestClassification().buildAndStore(classificationService); + defaultWorkbasket = + DefaultTestEntities.defaultTestWorkbasket().buildAndStore(workbasketService); + WorkbasketAccessItemBuilder.newWorkbasketAccessItem() + .workbasketId(defaultWorkbasket.getId()) + .accessId("user-1-1") + .permission(WorkbasketPermission.OPEN) + .permission(WorkbasketPermission.READ) + .permission(WorkbasketPermission.READTASKS) + .permission(WorkbasketPermission.APPEND) + .buildAndStore(workbasketService); + task1 = + TaskBuilder.newTask() + .classificationSummary(defaultClassification.asSummary()) + .workbasketSummary(defaultWorkbasket.asSummary()) + .primaryObjRef(DefaultTestEntities.defaultTestObjectReference().build()) + .buildAndStore(taskService); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_DeleteTaskComment_For_TaskCommentId() throws Exception { + comment1 = + TaskCommentBuilder.newTaskComment() + .taskId(task1.getId()) + .textField("Text1") + .created(Instant.now()) + .modified(Instant.now()) + .buildAndStore(taskService); + + taskService.deleteTaskComment(comment1.getId()); + + List taskCommentsAfterDeletion = taskService.getTaskComments(task1.getId()); + assertThat(taskCommentsAfterDeletion).hasSize(0); + } + + @WithAccessId(user = "user-1-2", groups = "user-1-1") + @Test + void should_FailToDeleteTaskComment_When_UserHasNoAuthorization() throws Exception { + comment1 = + TaskCommentBuilder.newTaskComment() + .taskId(task1.getId()) + .textField("Text1") + .created(Instant.now()) + .modified(Instant.now()) + .buildAndStore(taskService, "user-1-1"); + ThrowingCallable call = () -> taskService.deleteTaskComment(comment1.getId()); + NotAuthorizedOnTaskCommentException e = + catchThrowableOfType(call, NotAuthorizedOnTaskCommentException.class); + assertThat(e.getTaskCommentId()).isEqualTo(comment1.getId()); + assertThat(e.getCurrentUserId()).isEqualTo(taskanaEngine.getCurrentUserContext().getUserid()); + + List taskCommentsAfterDeletion = taskService.getTaskComments(task1.getId()); + assertThat(taskCommentsAfterDeletion).hasSize(1); + + taskanaEngine.runAsAdmin( + () -> { + try { + taskService.deleteTaskComment(comment1.getId()); + } catch (TaskCommentNotFoundException ex) { + throw new SystemException("Cannot find task comment with id " + ex.getTaskCommentId()); + } catch (TaskNotFoundException ex) { + throw new SystemException("Cannot find task comment with id " + ex.getTaskId()); + } catch (NotAuthorizedOnTaskCommentException ex) { + throw new SystemException( + "User " + + ex.getCurrentUserId() + + " not authorized on task comment " + + ex.getTaskCommentId()); + } catch (NotAuthorizedOnWorkbasketException ex) { + throw new SystemException( + "User " + + ex.getCurrentUserId() + + " not authorized on workbasket " + + ex.getWorkbasketId()); + } + }); + List taskCommentsAfterDeletionWithAdmin = + taskService.getTaskComments(task1.getId()); + assertThat(taskCommentsAfterDeletionWithAdmin).hasSize(0); + } + + @WithAccessId(user = "admin") + @WithAccessId(user = "taskadmin") + @TestTemplate + void should_DeleteTaskComment_When_UserIsInAdministrativeRole() throws Exception { + comment1 = + TaskCommentBuilder.newTaskComment() + .taskId(task1.getId()) + .textField("Text1") + .created(Instant.now()) + .modified(Instant.now()) + .buildAndStore(taskService); + taskService.deleteTaskComment(comment1.getId()); + + List taskCommentsAfterDeletion = taskService.getTaskComments(task1.getId()); + assertThat(taskCommentsAfterDeletion).hasSize(0); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_FailToDeleteTaskComment_When_TaskCommentIdIsInvalid() throws Exception { + assertThatThrownBy(() -> taskService.deleteTaskComment("")) + .isInstanceOf(InvalidArgumentException.class); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_FailToDeleteTaskComment_When_TaskCommentIdIsNull() throws Exception { + assertThatThrownBy(() -> taskService.deleteTaskComment(null)) + .isInstanceOf(InvalidArgumentException.class); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_FailToDeleteTaskComment_When_CommentIdDoesNotExist() throws Exception { + + ThrowingCallable call = () -> taskService.deleteTaskComment("non existing task comment id"); + TaskCommentNotFoundException e = catchThrowableOfType(call, TaskCommentNotFoundException.class); + assertThat(e.getTaskCommentId()).isEqualTo("non existing task comment id"); + } +} diff --git a/lib/taskana-core-test/src/test/java/acceptance/taskcomment/query/QueryTaskCommentAccTest.java b/lib/taskana-core-test/src/test/java/acceptance/taskcomment/query/QueryTaskCommentAccTest.java new file mode 100644 index 0000000000..e37254097a --- /dev/null +++ b/lib/taskana-core-test/src/test/java/acceptance/taskcomment/query/QueryTaskCommentAccTest.java @@ -0,0 +1,488 @@ +package acceptance.taskcomment.query; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.time.Instant; +import java.util.List; +import org.apache.ibatis.exceptions.TooManyResultsException; +import org.assertj.core.api.ThrowableAssert.ThrowingCallable; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; +import pro.taskana.TaskanaConfiguration; +import pro.taskana.classification.api.ClassificationService; +import pro.taskana.classification.api.models.Classification; +import pro.taskana.common.api.TimeInterval; +import pro.taskana.task.api.TaskCommentQuery; +import pro.taskana.task.api.TaskCommentQueryColumnName; +import pro.taskana.task.api.TaskService; +import pro.taskana.task.api.models.Task; +import pro.taskana.task.api.models.TaskComment; +import pro.taskana.testapi.DefaultTestEntities; +import pro.taskana.testapi.TaskanaConfigurationModifier; +import pro.taskana.testapi.TaskanaInject; +import pro.taskana.testapi.TaskanaIntegrationTest; +import pro.taskana.testapi.builder.TaskBuilder; +import pro.taskana.testapi.builder.TaskCommentBuilder; +import pro.taskana.testapi.builder.WorkbasketAccessItemBuilder; +import pro.taskana.testapi.security.WithAccessId; +import pro.taskana.user.api.UserService; +import pro.taskana.user.api.models.User; +import pro.taskana.workbasket.api.WorkbasketPermission; +import pro.taskana.workbasket.api.WorkbasketService; +import pro.taskana.workbasket.api.exceptions.NotAuthorizedToQueryWorkbasketException; +import pro.taskana.workbasket.api.models.Workbasket; + +@TaskanaIntegrationTest +class QueryTaskCommentAccTest { + + @TaskanaInject TaskService taskService; + @TaskanaInject ClassificationService classificationService; + @TaskanaInject WorkbasketService workbasketService; + @TaskanaInject UserService userService; + + Classification defaultClassification; + Workbasket defaultWorkbasket; + Task task1; + TaskComment comment1; + TaskComment comment2; + TaskComment comment3; + + @WithAccessId(user = "admin") + @BeforeAll + void setup() throws Exception { + + defaultClassification = + DefaultTestEntities.defaultTestClassification().buildAndStore(classificationService); + defaultWorkbasket = + DefaultTestEntities.defaultTestWorkbasket().buildAndStore(workbasketService); + WorkbasketAccessItemBuilder.newWorkbasketAccessItem() + .workbasketId(defaultWorkbasket.getId()) + .accessId("user-1-1") + .permission(WorkbasketPermission.OPEN) + .permission(WorkbasketPermission.READ) + .permission(WorkbasketPermission.READTASKS) + .permission(WorkbasketPermission.APPEND) + .buildAndStore(workbasketService); + + task1 = + TaskBuilder.newTask() + .classificationSummary(defaultClassification.asSummary()) + .workbasketSummary(defaultWorkbasket.asSummary()) + .primaryObjRef(DefaultTestEntities.defaultTestObjectReference().build()) + .buildAndStore(taskService); + + comment1 = + TaskCommentBuilder.newTaskComment() + .taskId(task1.getId()) + .textField("Text1") + .created(Instant.now()) + .modified(Instant.now()) + .buildAndStore(taskService, "user-1-1"); + comment2 = + TaskCommentBuilder.newTaskComment() + .taskId(task1.getId()) + .textField("Text2") + .created(Instant.now()) + .modified(Instant.now()) + .buildAndStore(taskService, "user-1-1"); + comment3 = + TaskCommentBuilder.newTaskComment() + .taskId(task1.getId()) + .textField("Text3") + .created(Instant.now()) + .modified(Instant.now()) + .buildAndStore(taskService, "admin"); + List taskComments = taskService.getTaskComments(task1.getId()); + assertThat(taskComments).hasSize(3); + } + + @Nested + @TestInstance(Lifecycle.PER_CLASS) + class FilterTaskComments { + + @WithAccessId(user = "user-1-1") + @Test + void should_FilterTaskComments_For_IdIn() { + List comments = + taskService.createTaskCommentQuery().idIn(comment1.getId(), comment2.getId()).list(); + assertThat(comments).hasSize(2); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_FilterTaskComments_For_TaskIdIn() { + List comments = + taskService.createTaskCommentQuery().taskIdIn(task1.getId()).list(); + assertThat(comments).hasSize(3); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_FilterTaskComments_For_CreatorIn() { + List comments = + taskService.createTaskCommentQuery().creatorIn("user-1-1").list(); + assertThat(comments).hasSize(2); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_FilterTaskComments_For_CreatorNotIn() { + List comments = + taskService.createTaskCommentQuery().creatorNotIn("user-1-2").list(); + assertThat(comments).hasSize(3); + } + + @WithAccessId(user = "user-1-2") + @Test + void should_ThrowException_When_NotAuthorizedToReadTaskComments() { + + ThrowingCallable call = + () -> taskService.createTaskCommentQuery().taskIdIn(task1.getId()).list(); + + assertThatThrownBy(call).isInstanceOf(NotAuthorizedToQueryWorkbasketException.class); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_FilterTaskComments_For_CreatedWithin() { + TimeInterval timeInterval = new TimeInterval(Instant.now().minusSeconds(5), Instant.now()); + List comments = + taskService.createTaskCommentQuery().createdWithin(timeInterval).list(); + assertThat(comments).hasSize(3); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_FilterTaskComments_For_CreatedNotWithin() { + TimeInterval timeInterval = new TimeInterval(Instant.now().minusSeconds(10), Instant.now()); + List comments = + taskService.createTaskCommentQuery().createdNotWithin(timeInterval).list(); + assertThat(comments).isEmpty(); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_FilterTaskComments_For_ModifiedWithin() { + TimeInterval timeInterval = new TimeInterval(Instant.now().minusSeconds(10), Instant.now()); + List comments = + taskService.createTaskCommentQuery().modifiedWithin(timeInterval).list(); + assertThat(comments).hasSize(3); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_FilterTaskComments_For_ModifiedNotWithin() { + TimeInterval timeInterval = new TimeInterval(Instant.now().minusSeconds(10), Instant.now()); + List comments = + taskService.createTaskCommentQuery().modifiedNotWithin(timeInterval).list(); + assertThat(comments).isEmpty(); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_FilterTaskComments_For_IdLike() { + String searchId = "%" + comment1.getId().substring(1, comment1.getId().length() - 2) + "%"; + + List comments = taskService.createTaskCommentQuery().idLike(searchId).list(); + assertThat(comments).hasSize(1); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_FilterTaskComments_For_IdNotLike() { + List comments = + taskService.createTaskCommentQuery().idNotLike("%ABC-123456%").list(); + assertThat(comments).hasSize(3); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_FilterTaskComments_For_TextFieldLike() { + List comments = + taskService.createTaskCommentQuery().textFieldLike("%ext%").list(); + assertThat(comments).hasSize(3); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_FilterTaskComments_For_TextFieldNotLike() { + List comments = + taskService.createTaskCommentQuery().textFieldNotLike("%other%").list(); + assertThat(comments).hasSize(3); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_FilterTaskComments_For_CreatorLike() { + List comments = taskService.createTaskCommentQuery().creatorLike("%-1-%").list(); + assertThat(comments).hasSize(2); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_FilterTaskComments_For_CreatorNotLike() { + List comments = + taskService.createTaskCommentQuery().creatorNotLike("%dmi%").list(); + assertThat(comments).hasSize(2); + } + } + + @Nested + @TestInstance(Lifecycle.PER_CLASS) + class CountTaskComments { + @WithAccessId(user = "user-1-1") + @Test + void should_ReturnCountOfEvents_When_UsingCountMethod() { + long count = taskService.createTaskCommentQuery().count(); + assertThat(count).isEqualTo(3); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_ReturnCountOfEvents_When_UsingCountMethodAndCreatorIn() { + long count = taskService.createTaskCommentQuery().creatorIn("admin").count(); + assertThat(count).isEqualTo(1); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_ReturnCountOfEvents_When_UsingCountMethodAndCreatorNotIn() { + long count = taskService.createTaskCommentQuery().creatorNotIn("admin").count(); + assertThat(count).isEqualTo(2); + } + } + + @Nested + @TestInstance(Lifecycle.PER_CLASS) + class QueryColumnTaskComments { + @WithAccessId(user = "admin") + @BeforeAll + void setup() throws Exception { + User userWithName = userService.newUser(); + userWithName.setId("user-1-1"); + userWithName.setFirstName("Max"); + userWithName.setLastName("Mustermann"); + userWithName.setFullName("Max Mustermann"); + userService.createUser(userWithName); + } + + @WithAccessId(user = "admin") + @AfterAll + void reset() throws Exception { + userService.deleteUser("user-1-1"); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_ReturnListedValues_For_QueryColumnId() { + List listedValues = + taskService.createTaskCommentQuery().listValues(TaskCommentQueryColumnName.ID, null); + assertThat(listedValues) + .hasSize(3) + .containsExactlyInAnyOrder(comment1.getId(), comment2.getId(), comment3.getId()); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_ReturnListedValues_For_QueryColumnTaskId() { + List listedValues = + taskService.createTaskCommentQuery().listValues(TaskCommentQueryColumnName.TASK_ID, null); + assertThat(listedValues).hasSize(1).containsExactly(comment1.getTaskId()); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_ReturnListedValues_For_QueryColumnTextField() { + List listedValues = + taskService + .createTaskCommentQuery() + .listValues(TaskCommentQueryColumnName.TEXT_FIELD, null); + assertThat(listedValues) + .hasSize(3) + .containsExactlyInAnyOrder( + comment1.getTextField(), comment2.getTextField(), comment3.getTextField()); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_ReturnListedValues_For_QueryColumnCreator() { + List listedValues = + taskService.createTaskCommentQuery().listValues(TaskCommentQueryColumnName.CREATOR, null); + assertThat(listedValues).hasSize(2).containsExactlyInAnyOrder("user-1-1", "admin"); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_ReturnListedValues_For_QueryColumnCreatorLongName() { + List listedValues = + taskService + .createTaskCommentQuery() + .listValues(TaskCommentQueryColumnName.CREATOR_FULL_NAME, null); + assertThat(listedValues).hasSize(2); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_ReturnListedValues_For_QueryColumnCreated() { + List listedValues = + taskService.createTaskCommentQuery().listValues(TaskCommentQueryColumnName.CREATED, null); + assertThat(listedValues).hasSize(3); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_ReturnListedValues_For_QueryColumnModified() { + List listedValues = + taskService + .createTaskCommentQuery() + .listValues(TaskCommentQueryColumnName.MODIFIED, null); + assertThat(listedValues).hasSize(3); + } + } + + @Nested + @TestInstance(Lifecycle.PER_CLASS) + class OffsetConstraintsTaskComments { + @WithAccessId(user = "user-1-1") + @Test + void should_ConfirmQueryListOffset_When_ProvidingOffsetAndLimit() { + List offsetAndLimitResult = taskService.createTaskCommentQuery().list(1, 2); + List regularResult = taskService.createTaskCommentQuery().list(); + + assertThat(offsetAndLimitResult).hasSize(2); + assertThat(offsetAndLimitResult.get(0)) + .isNotEqualTo(regularResult.get(0)) + .isEqualTo(regularResult.get(1)); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_ReturnEmptyList_When_ProvidingWrongConstraints() { + List result = taskService.createTaskCommentQuery().list(0, 1000); + assertThat(result).hasSize(3); + + result = taskService.createTaskCommentQuery().list(100, 1000); + assertThat(result).isEmpty(); + } + } + + @Nested + @TestInstance(Lifecycle.PER_CLASS) + class SingleResultTaskComments { + + @WithAccessId(user = "user-1-1") + @Test + void should_ReturnSingleTaskComment_When_UsingSingleMethod() { + TaskComment single = + taskService + .createTaskCommentQuery() + .idIn(comment1.getId()) + .creatorIn("user-1-1") + .single(); + + assertThat(single.getId()).isEqualTo(comment1.getId()); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_ThrowException_When_SingleMethodRetrievesMoreThanOneEventFromDatabase() { + TaskCommentQuery query = taskService.createTaskCommentQuery().creatorIn("user-1-1"); + assertThatThrownBy(query::single).isInstanceOf(TooManyResultsException.class); + } + } + + @Nested + @TestInstance(Lifecycle.PER_CLASS) + class CreatorFullNameWhenAdditionalUserInfoIsTrue implements TaskanaConfigurationModifier { + + @TaskanaInject TaskService taskService; + @TaskanaInject UserService userService; + + @Override + public TaskanaConfiguration.Builder modify(TaskanaConfiguration.Builder builder) { + return builder.addAdditionalUserInfo(true); + } + + @WithAccessId(user = "admin") + @BeforeAll + void setup() throws Exception { + User userWithName = userService.newUser(); + userWithName.setId("user-1-1"); + userWithName.setFirstName("Max"); + userWithName.setLastName("Mustermann"); + userWithName.setFullName("Max Mustermann"); + userService.createUser(userWithName); + } + + @WithAccessId(user = "admin") + @AfterAll + void reset() throws Exception { + userService.deleteUser("user-1-1"); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_SetCreatorFullName_When_PropertyEnabled() throws Exception { + List taskComments = + taskService.createTaskCommentQuery().idIn(comment1.getId()).list(); + + String creatorFullName = userService.getUser(taskComments.get(0).getCreator()).getFullName(); + assertThat(creatorFullName).isNotNull(); + assertThat(taskComments.get(0)) + .extracting(TaskComment::getCreatorFullName) + .isEqualTo(creatorFullName); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_SetCreatorFullNameToNull_When_NotExistingAsUserInDatabase() { + List taskComments = + taskService.createTaskCommentQuery().idIn(comment3.getId()).list(); + + assertThat(taskComments).hasSize(1); + assertThat(taskComments.get(0)).extracting(TaskComment::getCreatorFullName).isNull(); + } + } + + @Nested + @TestInstance(Lifecycle.PER_CLASS) + class CreatorFullNameWhenAdditionalUserInfoIsFalse implements TaskanaConfigurationModifier { + @TaskanaInject TaskService taskService; + + @Override + public TaskanaConfiguration.Builder modify(TaskanaConfiguration.Builder builder) { + return builder.addAdditionalUserInfo(false); + } + + @WithAccessId(user = "admin") + @BeforeAll + void setup() throws Exception { + User userWithName = userService.newUser(); + userWithName.setId("user-1-1"); + userWithName.setFirstName("Max"); + userWithName.setLastName("Mustermann"); + userWithName.setFullName("Max Mustermann"); + userService.createUser(userWithName); + } + + @WithAccessId(user = "admin") + @AfterAll + void reset() throws Exception { + userService.deleteUser("user-1-1"); + } + + @WithAccessId(user = "user-1-1") + @Test + void should_NotSetCreatorFullName_When_PropertyDisabled() throws Exception { + List taskComments = + taskService.createTaskCommentQuery().idIn(comment1.getId()).list(); + + assertThat(taskComments.get(0)).extracting(TaskComment::getCreatorFullName).isNull(); + } + } +} diff --git a/lib/taskana-core/src/test/java/acceptance/taskcomment/delete/DeleteTaskCommentAccTest.java b/lib/taskana-core/src/test/java/acceptance/taskcomment/delete/DeleteTaskCommentAccTest.java deleted file mode 100644 index 688bec990d..0000000000 --- a/lib/taskana-core/src/test/java/acceptance/taskcomment/delete/DeleteTaskCommentAccTest.java +++ /dev/null @@ -1,128 +0,0 @@ -package acceptance.taskcomment.delete; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import acceptance.AbstractAccTest; -import java.util.List; -import org.assertj.core.api.ThrowableAssert.ThrowingCallable; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestTemplate; -import org.junit.jupiter.api.extension.ExtendWith; -import pro.taskana.common.api.exceptions.InvalidArgumentException; -import pro.taskana.common.test.security.JaasExtension; -import pro.taskana.common.test.security.WithAccessId; -import pro.taskana.task.api.TaskService; -import pro.taskana.task.api.exceptions.NotAuthorizedOnTaskCommentException; -import pro.taskana.task.api.exceptions.TaskCommentNotFoundException; -import pro.taskana.task.api.models.TaskComment; - -@ExtendWith(JaasExtension.class) -class DeleteTaskCommentAccTest extends AbstractAccTest { - - DeleteTaskCommentAccTest() { - super(); - } - - @WithAccessId(user = "user-1-1") - @Test - void should_DeleteTaskComment_For_TaskCommentId() throws Exception { - - TaskService taskService = taskanaEngine.getTaskService(); - - List taskComments = - taskService.getTaskComments("TKI:000000000000000000000000000000000001"); - assertThat(taskComments).hasSize(2); - - taskService.deleteTaskComment("TCI:000000000000000000000000000000000004"); - - // make sure the task comment was deleted - List taskCommentsAfterDeletion = - taskService.getTaskComments("TKI:000000000000000000000000000000000001"); - assertThat(taskCommentsAfterDeletion).hasSize(1); - } - - @WithAccessId(user = "user-1-2", groups = "user-1-1") // to read comments - @Test - void should_FailToDeleteTaskComment_When_UserHasNoAuthorization() throws Exception { - - TaskService taskService = taskanaEngine.getTaskService(); - - List taskComments = - taskService.getTaskComments("TKI:000000000000000000000000000000000000"); - assertThat(taskComments).hasSize(3); - - ThrowingCallable lambda = - () -> taskService.deleteTaskComment("TCI:000000000000000000000000000000000000"); - - assertThatThrownBy(lambda).isInstanceOf(NotAuthorizedOnTaskCommentException.class); - - // make sure the task comment was not deleted - List taskCommentsAfterDeletion = - taskService.getTaskComments("TKI:000000000000000000000000000000000000"); - assertThat(taskCommentsAfterDeletion).hasSize(3); - } - - @WithAccessId(user = "admin") - @WithAccessId(user = "taskadmin") - @TestTemplate - void should_DeleteTaskComment_When_NoExplicitPermissionsButUserIsInAdministrativeRole() - throws Exception { - - resetDb(false); - - TaskService taskService = taskanaEngine.getTaskService(); - - List taskComments = - taskService.getTaskComments("TKI:000000000000000000000000000000000002"); - assertThat(taskComments).hasSize(2); - - taskService.deleteTaskComment("TCI:000000000000000000000000000000000006"); - - // make sure the task comment was deleted - List taskCommentsAfterDeletion = - taskService.getTaskComments("TKI:000000000000000000000000000000000002"); - assertThat(taskCommentsAfterDeletion).hasSize(1); - } - - @WithAccessId(user = "user-1-1") - @Test - void should_FailToDeleteTaskComment_When_TaskCommentIdIsInvalid() throws Exception { - - TaskService taskService = taskanaEngine.getTaskService(); - - List taskComments = - taskService.getTaskComments("TKI:000000000000000000000000000000000000"); - assertThat(taskComments).hasSize(3); - - assertThatThrownBy(() -> taskService.deleteTaskComment("")) - .isInstanceOf(InvalidArgumentException.class); - - assertThatThrownBy(() -> taskService.deleteTaskComment(null)) - .isInstanceOf(InvalidArgumentException.class); - - // make sure that no task comment was deleted - List taskCommentsAfterDeletion = - taskService.getTaskComments("TKI:000000000000000000000000000000000000"); - assertThat(taskCommentsAfterDeletion).hasSize(3); - } - - @WithAccessId(user = "user-1-1") - @Test - void should_FailToDeleteTaskComment_When_TaskCommentIsNotExisting() throws Exception { - - TaskService taskService = taskanaEngine.getTaskService(); - - List taskComments = - taskService.getTaskComments("TKI:000000000000000000000000000000000000"); - assertThat(taskComments).hasSize(3); - - ThrowingCallable lambda = () -> taskService.deleteTaskComment("non existing task comment id"); - assertThatThrownBy(lambda).isInstanceOf(TaskCommentNotFoundException.class); - - // make sure the task comment was not deleted - List taskCommentsAfterDeletion = - taskService.getTaskComments("TKI:000000000000000000000000000000000000"); - assertThat(taskCommentsAfterDeletion).hasSize(3); - } -} diff --git a/lib/taskana-core/src/test/java/acceptance/taskcomment/query/QueryTaskCommentAccTest.java b/lib/taskana-core/src/test/java/acceptance/taskcomment/query/QueryTaskCommentAccTest.java deleted file mode 100644 index 85f2ddd060..0000000000 --- a/lib/taskana-core/src/test/java/acceptance/taskcomment/query/QueryTaskCommentAccTest.java +++ /dev/null @@ -1,343 +0,0 @@ -package acceptance.taskcomment.query; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import acceptance.AbstractAccTest; -import java.time.Instant; -import java.util.List; -import org.apache.ibatis.exceptions.TooManyResultsException; -import org.assertj.core.api.ThrowableAssert.ThrowingCallable; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import pro.taskana.TaskanaConfiguration; -import pro.taskana.common.api.TaskanaEngine; -import pro.taskana.common.api.TimeInterval; -import pro.taskana.common.test.security.JaasExtension; -import pro.taskana.common.test.security.WithAccessId; -import pro.taskana.task.api.TaskCommentQuery; -import pro.taskana.task.api.TaskCommentQueryColumnName; -import pro.taskana.task.api.models.TaskComment; -import pro.taskana.workbasket.api.exceptions.NotAuthorizedToQueryWorkbasketException; - -/** Test for TaskComment queries. */ -@ExtendWith(JaasExtension.class) -class QueryTaskCommentAccTest extends AbstractAccTest { - - @WithAccessId(user = "admin") - @Test - void should_FilterTaskComments_For_IdIn() { - List comments = - taskService - .createTaskCommentQuery() - .idIn( - "TCI:000000000000000000000000000000000000", - "TCI:000000000000000000000000000000000002") - .list(); - assertThat(comments).hasSize(2); - } - - @WithAccessId(user = "admin") - @Test - void should_FilterTaskComments_For_TaskIdIn() { - List comments = - taskService - .createTaskCommentQuery() - .taskIdIn("TKI:000000000000000000000000000000000000") - .list(); - assertThat(comments).hasSize(3); - } - - @WithAccessId(user = "user-1-1") - @Test - void should_ThrowException_When_NotAuthorizedToReadTaskComments() { - - ThrowingCallable call = - () -> - taskService - .createTaskCommentQuery() - .taskIdIn("TKI:000000000000000000000000000000000020") - .list(); - - assertThatThrownBy(call).isInstanceOf(NotAuthorizedToQueryWorkbasketException.class); - } - - @WithAccessId(user = "admin") - @Test - void should_FilterTaskComments_For_CreatorIn() { - List comments = taskService.createTaskCommentQuery().creatorIn("user-1-2").list(); - assertThat(comments).hasSize(2); - } - - @WithAccessId(user = "admin") - @Test - void should_FilterTaskComments_For_CreatedWithin() { - TimeInterval timeInterval = new TimeInterval(Instant.now().minusSeconds(10), Instant.now()); - List comments = - taskService.createTaskCommentQuery().createdWithin(timeInterval).list(); - assertThat(comments).isEmpty(); - } - - @WithAccessId(user = "admin") - @Test - void should_FilterTaskComments_For_ModifiedWithin() { - TimeInterval timeInterval = new TimeInterval(Instant.now().minusSeconds(10), Instant.now()); - List comments = - taskService.createTaskCommentQuery().modifiedWithin(timeInterval).list(); - assertThat(comments).isEmpty(); - } - - @WithAccessId(user = "admin") - @Test - void should_FilterTaskComments_For_CreatorNotIn() { - List comments = - taskService.createTaskCommentQuery().creatorNotIn("user-1-2").list(); - assertThat(comments).hasSize(11); - } - - @WithAccessId(user = "admin") - @Test - void should_FilterTaskComments_For_CreatedNotWithin() { - TimeInterval timeInterval = new TimeInterval(Instant.now().minusSeconds(10), Instant.now()); - List comments = - taskService.createTaskCommentQuery().createdNotWithin(timeInterval).list(); - assertThat(comments).hasSize(13); - } - - @WithAccessId(user = "admin") - @Test - void should_FilterTaskComments_For_ModiefiedNotWithin() { - TimeInterval timeInterval = new TimeInterval(Instant.now().minusSeconds(10), Instant.now()); - List comments = - taskService.createTaskCommentQuery().modifiedNotWithin(timeInterval).list(); - assertThat(comments).hasSize(13); - } - - @WithAccessId(user = "admin") - @Test - void should_FilterTaskComments_For_IdLike() { - List comments = taskService.createTaskCommentQuery().idLike("%000001%").list(); - assertThat(comments).hasSize(4); - } - - @WithAccessId(user = "admin") - @Test - void should_FilterTaskComments_For_TextFieldLike() { - List comments = - taskService.createTaskCommentQuery().textFieldLike("%other%").list(); - assertThat(comments).hasSize(6); - } - - @WithAccessId(user = "admin") - @Test - void should_FilterTaskComments_For_CreatorLike() { - List comments = taskService.createTaskCommentQuery().creatorLike("%1-1%").list(); - assertThat(comments).hasSize(10); - } - - @WithAccessId(user = "admin") - @Test - void should_FilterTaskComments_For_IdNotLike() { - List comments = taskService.createTaskCommentQuery().idNotLike("%000001%").list(); - assertThat(comments).hasSize(9); - } - - @WithAccessId(user = "admin") - @Test - void should_FilterTaskComments_For_TextFieldNotLike() { - List comments = - taskService.createTaskCommentQuery().textFieldNotLike("%other%").list(); - assertThat(comments).hasSize(7); - } - - @WithAccessId(user = "admin") - @Test - void should_FilterTaskComments_For_CreatorNotLike() { - List comments = - taskService.createTaskCommentQuery().creatorNotLike("%1-1%").list(); - assertThat(comments).hasSize(3); - } - - @WithAccessId(user = "admin") - @Test - void should_ReturnCountOfEvents_When_UsingCountMethod() { - long count = taskService.createTaskCommentQuery().creatorIn("user-1-1").count(); - assertThat(count).isEqualTo(10); - - count = taskService.createTaskCommentQuery().creatorNotIn("user-1-1").count(); - assertThat(count).isEqualTo(3); - - count = taskService.createTaskCommentQuery().count(); - assertThat(count).isEqualTo(13); - } - - @WithAccessId(user = "admin") - @Test - void should_ReturnListedValues_For_QueryColumnId() { - List listedValues = - taskService.createTaskCommentQuery().listValues(TaskCommentQueryColumnName.ID, null); - assertThat(listedValues).hasSize(13); - } - - @WithAccessId(user = "admin") - @Test - void should_ReturnListedValues_For_QueryColumnTaskId() { - List listedValues = - taskService.createTaskCommentQuery().listValues(TaskCommentQueryColumnName.TASK_ID, null); - assertThat(listedValues).hasSize(7); - } - - @WithAccessId(user = "admin") - @Test - void should_ReturnListedValues_For_QueryColumnTextField() { - List listedValues = - taskService - .createTaskCommentQuery() - .listValues(TaskCommentQueryColumnName.TEXT_FIELD, null); - assertThat(listedValues).hasSize(2); - } - - @WithAccessId(user = "admin") - @Test - void should_ReturnListedValues_For_QueryColumnCreator() { - List listedValues = - taskService.createTaskCommentQuery().listValues(TaskCommentQueryColumnName.CREATOR, null); - assertThat(listedValues).hasSize(3); - } - - @WithAccessId(user = "admin") - @Test - void should_ReturnListedValues_For_QueryColumnCreatorLongName() throws Exception { - TaskanaConfiguration taskanaConfiguration = - new TaskanaConfiguration.Builder(AbstractAccTest.taskanaConfiguration) - .addAdditionalUserInfo(false) - .build(); - TaskanaEngine taskanaEngine = TaskanaEngine.buildTaskanaEngine(taskanaConfiguration); - List listedValues = - taskanaEngine - .getTaskService() - .createTaskCommentQuery() - .listValues(TaskCommentQueryColumnName.CREATOR_FULL_NAME, null); - assertThat(listedValues).hasSize(3); - } - - @WithAccessId(user = "admin") - @Test - void should_ReturnListedValues_For_QueryColumnCreated() { - List listedValues = - taskService.createTaskCommentQuery().listValues(TaskCommentQueryColumnName.CREATED, null); - assertThat(listedValues).hasSize(5); - } - - @WithAccessId(user = "admin") - @Test - void should_ReturnListedValues_For_QueryColumnModified() { - List listedValues = - taskService.createTaskCommentQuery().listValues(TaskCommentQueryColumnName.MODIFIED, null); - assertThat(listedValues).hasSize(7); - } - - @WithAccessId(user = "admin") - @Test - void should_ConfirmQueryListOffset_When_ProvidingOffsetAndLimit() { - List offsetAndLimitResult = taskService.createTaskCommentQuery().list(1, 2); - List regularResult = taskService.createTaskCommentQuery().list(); - - assertThat(offsetAndLimitResult).hasSize(2); - assertThat(offsetAndLimitResult.get(0)) - .isNotEqualTo(regularResult.get(0)) - .isEqualTo(regularResult.get(1)); - } - - @WithAccessId(user = "admin") - @Test - void should_ReturnEmptyList_When_ProvidingWrongConstraints() { - List result = taskService.createTaskCommentQuery().list(1, 1000); - assertThat(result).hasSize(12); - - result = taskService.createTaskCommentQuery().list(100, 1000); - assertThat(result).isEmpty(); - } - - @WithAccessId(user = "admin") - @Test - void should_ReturnSingleTaskComment_When_UsingSingleMethod() { - TaskComment single = - taskService - .createTaskCommentQuery() - .taskIdIn("TKI:000000000000000000000000000000000000") - .creatorIn("user-1-2") - .single(); - - assertThat(single.getId()).isEqualTo("TCI:000000000000000000000000000000000001"); - } - - @WithAccessId(user = "admin") - @Test - void should_ThrowException_When_SingleMethodRetrievesMoreThanOneEventFromDatabase() { - TaskCommentQuery query = taskService.createTaskCommentQuery().creatorIn("user-1-1"); - assertThatThrownBy(query::single).isInstanceOf(TooManyResultsException.class); - } - - @WithAccessId(user = "admin") - @Test - void should_SetTaskCreatorFullNameOfTaskComment_When_PropertyEnabled() throws Exception { - TaskanaConfiguration taskanaConfiguration = - new TaskanaConfiguration.Builder(AbstractAccTest.taskanaConfiguration) - .addAdditionalUserInfo(true) - .build(); - TaskanaEngine taskanaEngine = TaskanaEngine.buildTaskanaEngine(taskanaConfiguration); - List taskComments = - taskanaEngine - .getTaskService() - .createTaskCommentQuery() - .idIn("TCI:000000000000000000000000000000000000") - .list(); - - assertThat(taskComments).hasSize(1); - String creatorFullName = - taskanaEngine.getUserService().getUser(taskComments.get(0).getCreator()).getFullName(); - assertThat(taskComments.get(0)) - .extracting(TaskComment::getCreatorFullName) - .isEqualTo(creatorFullName); - } - - @WithAccessId(user = "user-1-1") - @Test - void should_NotSetTaskCreatorFullNameOfTaskComment_When_PropertyDisabled() throws Exception { - TaskanaConfiguration taskanaConfiguration = - new TaskanaConfiguration.Builder(AbstractAccTest.taskanaConfiguration) - .addAdditionalUserInfo(false) - .build(); - TaskanaEngine taskanaEngine = TaskanaEngine.buildTaskanaEngine(taskanaConfiguration); - List taskComments = - taskanaEngine - .getTaskService() - .createTaskCommentQuery() - .idIn("TCI:000000000000000000000000000000000000") - .list(); - - assertThat(taskComments).hasSize(1); - assertThat(taskComments.get(0)).extracting(TaskComment::getCreatorFullName).isNull(); - } - - @WithAccessId(user = "admin") - @Test - void should_SetTaskCreatorFullNameOfTaskCommentToNull_When_NotExistingAsUserInDatabase() - throws Exception { - TaskanaConfiguration taskanaConfiguration = - new TaskanaConfiguration.Builder(AbstractAccTest.taskanaConfiguration) - .addAdditionalUserInfo(true) - .build(); - TaskanaEngine taskanaEngine = TaskanaEngine.buildTaskanaEngine(taskanaConfiguration); - List taskComments = - taskanaEngine - .getTaskService() - .createTaskCommentQuery() - .idIn("TCI:000000000000000000000000000000000008") - .list(); - - assertThat(taskComments).hasSize(1); - assertThat(taskComments.get(0)).extracting(TaskComment::getCreatorFullName).isNull(); - } -}