From d6da6f81240587e0409bfd4b0dcbc36389f9267b Mon Sep 17 00:00:00 2001 From: Norman Schmidt <60552466+norman-schmidt@users.noreply.github.com> Date: Thu, 22 Sep 2022 11:42:27 +0200 Subject: [PATCH] TSK-1957: migrate UserServiceAccTest to new test-api --- .../acceptance/user/UserServiceAccTest.java | 321 ++++++++++++++++++ .../test/java/acceptance/AbstractAccTest.java | 20 -- .../acceptance/user/UserServiceAccTest.java | 224 ------------ 3 files changed, 321 insertions(+), 244 deletions(-) create mode 100644 lib/taskana-core-test/src/test/java/acceptance/user/UserServiceAccTest.java delete mode 100644 lib/taskana-core/src/test/java/acceptance/user/UserServiceAccTest.java diff --git a/lib/taskana-core-test/src/test/java/acceptance/user/UserServiceAccTest.java b/lib/taskana-core-test/src/test/java/acceptance/user/UserServiceAccTest.java new file mode 100644 index 000000000..88c5dfd79 --- /dev/null +++ b/lib/taskana-core-test/src/test/java/acceptance/user/UserServiceAccTest.java @@ -0,0 +1,321 @@ +package acceptance.user; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.stream.Stream; +import org.assertj.core.api.ThrowableAssert.ThrowingCallable; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestFactory; +import org.junit.jupiter.api.function.ThrowingConsumer; + +import pro.taskana.common.api.TaskanaRole; +import pro.taskana.common.api.exceptions.InvalidArgumentException; +import pro.taskana.common.api.exceptions.MismatchedRoleException; +import pro.taskana.testapi.TaskanaInject; +import pro.taskana.testapi.TaskanaIntegrationTest; +import pro.taskana.testapi.security.WithAccessId; +import pro.taskana.user.api.UserService; +import pro.taskana.user.api.exceptions.UserAlreadyExistException; +import pro.taskana.user.api.exceptions.UserNotFoundException; +import pro.taskana.user.api.models.User; + +/** Acceptance test which tests the functionality of the UserService. */ +@TaskanaIntegrationTest +class UserServiceAccTest { + + @TaskanaInject UserService userService; + + protected User createExampleUser(String id) { + User user = userService.newUser(); + user.setId(id); + user.setFirstName("Hans"); + user.setLastName("Georg"); + user.setFullName("Georg, Hans"); + user.setLongName("Georg, Hans - (user-10-20)"); + user.setEmail("hans.georg@web.com"); + user.setPhone("1234"); + user.setMobilePhone("01574275632"); + user.setOrgLevel4("level4"); + user.setOrgLevel3("level3"); + user.setOrgLevel2("level2"); + user.setOrgLevel1("level1"); + user.setData("ab"); + + return user; + } + + @WithAccessId(user = "admin") + @BeforeAll + void setup() throws Exception { + User testuser1 = userService.newUser(); + testuser1.setId("testuser1"); + testuser1.setFirstName("Max"); + testuser1.setLastName("Mustermann"); + testuser1.setFullName("Max, Mustermann"); + testuser1.setLongName("Max, Mustermann - (testuser1)"); + testuser1.setEmail("max.mustermann@web.com"); + testuser1.setPhone("040-2951854"); + testuser1.setMobilePhone("015637683197"); + testuser1.setOrgLevel4("Novatec"); + testuser1.setOrgLevel3("BPM"); + testuser1.setOrgLevel2("Human Workflow"); + testuser1.setOrgLevel1("TASKANA"); + testuser1.setData(""); + userService.createUser(testuser1); + + User testuser2 = userService.newUser(); + testuser2.setId("testuser2"); + testuser2.setFirstName("Elena"); + testuser2.setLastName("Eifrig"); + testuser2.setFullName("Elena, Eifrig"); + testuser2.setLongName("Elena, Eifrig - (testuser2)"); + testuser2.setEmail("elena.eifrig@web.com"); + testuser2.setPhone("040-2951854"); + testuser2.setMobilePhone("015637683197"); + testuser2.setOrgLevel4("Novatec"); + testuser2.setOrgLevel3("BPM"); + testuser2.setOrgLevel2("Human Workflow"); + testuser2.setOrgLevel1("TASKANA"); + testuser2.setData(""); + userService.createUser(testuser2); + } + + @WithAccessId(user = "user-1-2") + @Test + void should_ReturnUserWithAllFields_When_IdExisting() throws Exception { + User userInDatabase = userService.getUser("testuser1"); + + User userToCompare = userService.newUser(); + userToCompare.setId("testuser1"); + userToCompare.setFirstName("Max"); + userToCompare.setLastName("Mustermann"); + userToCompare.setFullName("Max, Mustermann"); + userToCompare.setLongName("Max, Mustermann - (testuser1)"); + userToCompare.setEmail("max.mustermann@web.com"); + userToCompare.setPhone("040-2951854"); + userToCompare.setMobilePhone("015637683197"); + userToCompare.setOrgLevel4("Novatec"); + userToCompare.setOrgLevel3("BPM"); + userToCompare.setOrgLevel2("Human Workflow"); + userToCompare.setOrgLevel1("TASKANA"); + userToCompare.setData(""); + + assertThat(userInDatabase).hasNoNullFieldsOrProperties().isEqualTo(userToCompare); + } + + @WithAccessId(user = "user-1-2") + @Test + void should_ThrowUserNotFoundException_When_TryingToGetUserWithNonExistingId() { + ThrowingCallable callable = () -> userService.getUser("NOT_EXISTING"); + + assertThatThrownBy(callable) + .isInstanceOf(UserNotFoundException.class) + .hasFieldOrPropertyWithValue("userId", "NOT_EXISTING") + .hasMessage("User with id 'NOT_EXISTING' was not found."); + } + + @WithAccessId(user = "admin") + @Test + void should_InsertUserInDatabase_When_CreatingUser() throws Exception { + User userToCreate = createExampleUser("user-10-20"); + + userService.createUser(userToCreate); + + User userInDatabse = userService.getUser(userToCreate.getId()); + + assertThat(userToCreate) + .hasNoNullFieldsOrProperties() + .isNotSameAs(userInDatabse) + .isEqualTo(userInDatabse); + } + + @WithAccessId(user = "admin") + @Test + void should_SetTheLongAndFullNameAccordingToRules_When_CreatingUserWithThoseFieldsEmpty() + throws Exception { + User userToCreate = createExampleUser("user-10-21"); + userToCreate.setLongName(null); + userToCreate.setFullName(null); + + String fullName = userToCreate.getLastName() + ", " + userToCreate.getFirstName(); + String longName = + userToCreate.getLastName() + + ", " + + userToCreate.getFirstName() + + " - (" + + userToCreate.getId() + + ")"; + + User createdUser = userService.createUser(userToCreate); + + assertThat(createdUser.getLongName()).isEqualTo(longName); + assertThat(createdUser.getFullName()).isEqualTo(fullName); + } + + @WithAccessId(user = "admin") + @Test + void should_ThrowInvalidArgumentException_When_TryingToCreateUserWithFirstOrLastNameNull() + throws Exception { + User userToCreate = createExampleUser("user-10-20"); + userToCreate.setFirstName(null); + + ThrowingCallable callable = () -> userService.createUser(userToCreate); + + assertThatThrownBy(callable) + .isInstanceOf(InvalidArgumentException.class) + .hasMessage("First and last name of User must be set or empty."); + } + + @WithAccessId(user = "admin") + @Test + void should_ThrowInvalidArgumentException_When_TryingToCreateUserWithLastNameNull() { + User userToCreate = createExampleUser("user-10-20"); + userToCreate.setLastName(null); + + ThrowingCallable callable = () -> userService.createUser(userToCreate); + + assertThatThrownBy(callable) + .isInstanceOf(InvalidArgumentException.class) + .hasMessage("First and last name of User must be set or empty."); + } + + @WithAccessId(user = "admin") + @TestFactory + Stream should_ThrowInvalidArgumentException_When_TryingToCreateUserWithNotSetId() + throws Exception { + Iterator iterator = Arrays.asList("", null).iterator(); + + ThrowingConsumer test = + userId -> { + User userToCreate = createExampleUser("user-10-20"); + userToCreate.setId(userId); + + ThrowingCallable callable = () -> userService.createUser(userToCreate); + + assertThatThrownBy(callable) + .isInstanceOf(InvalidArgumentException.class) + .hasMessage("UserId must not be empty when creating User."); + }; + + return DynamicTest.stream(iterator, c -> "for " + c, test); + } + + @WithAccessId(user = "admin") + @Test + void should_ThrowUserAlreadyExistException_When_TryingToCreateUserWithExistingId() { + User userToCreate = createExampleUser("testuser1"); // existing userId + + ThrowingCallable callable = () -> userService.createUser(userToCreate); + + assertThatThrownBy(callable) + .isInstanceOf(UserAlreadyExistException.class) + .hasFieldOrPropertyWithValue("userId", "testuser1") + .hasMessage("User with id 'testuser1' already exists."); + } + + @WithAccessId(user = "user-1-2") + @Test + void should_ThrowNotAuthorizedException_When_TryingToCreateUserWithoutAdminRole() { + User userToCreate = createExampleUser("user-10-22"); + + ThrowingCallable callable = () -> userService.createUser(userToCreate); + + assertThatThrownBy(callable) + .isInstanceOf(MismatchedRoleException.class) + .hasFieldOrPropertyWithValue("currentUserId", "user-1-2") + .hasFieldOrPropertyWithValue( + "roles", new TaskanaRole[] {TaskanaRole.BUSINESS_ADMIN, TaskanaRole.ADMIN}) + .hasMessage( + "Not authorized. The current user 'user-1-2' is not member of role(s) " + + "'[BUSINESS_ADMIN, ADMIN]'."); + } + + @WithAccessId(user = "admin") + @Test + void should_UpdateUserInDatabase_When_IdExisting() throws Exception { + User userToUpdate = createExampleUser("testuser1"); // existing userId + + userService.updateUser(userToUpdate); + + User userInDatabase = userService.getUser("testuser1"); + + assertThat(userToUpdate).isNotSameAs(userInDatabase).isEqualTo(userInDatabase); + } + + @WithAccessId(user = "admin") + @Test + void should_ThrowUserNotFoundException_When_TryingToUpdateUserWithNonExistingId() { + User userToUpdate = createExampleUser("NOT_EXISTING"); + + ThrowingCallable callable = () -> userService.updateUser(userToUpdate); + + assertThatThrownBy(callable) + .isInstanceOf(UserNotFoundException.class) + .hasFieldOrPropertyWithValue("userId", "NOT_EXISTING") + .hasMessage("User with id 'NOT_EXISTING' was not found."); + } + + @WithAccessId(user = "user-1-2") + @Test + void should_ThrowNotAuthorizedException_When_TryingToUpdateUserWithNoAdminRole() { + User userToUpdate = createExampleUser("testuser1"); // existing userId + + ThrowingCallable callable = () -> userService.updateUser(userToUpdate); + + assertThatThrownBy(callable) + .isInstanceOf(MismatchedRoleException.class) + .hasFieldOrPropertyWithValue("currentUserId", "user-1-2") + .hasFieldOrPropertyWithValue( + "roles", new TaskanaRole[] {TaskanaRole.BUSINESS_ADMIN, TaskanaRole.ADMIN}) + .hasMessage( + "Not authorized. The current user 'user-1-2' is not member of role(s) " + + "'[BUSINESS_ADMIN, ADMIN]'."); + } + + @WithAccessId(user = "admin") + @Test + void should_DeleteUserFromDatabase_When_IdExisting() throws Exception { + String id = "testuser2"; + userService.getUser(id); // User existing + + userService.deleteUser(id); + + ThrowingCallable callable = () -> userService.getUser(id); // User deleted + + assertThatThrownBy(callable) + .isInstanceOf(UserNotFoundException.class) + .hasFieldOrPropertyWithValue("userId", "testuser2") + .hasMessage("User with id 'testuser2' was not found."); + } + + @WithAccessId(user = "admin") + @Test + void should_ThrowUserNotFoundException_When_TryingToDeleteUserWithNonExistingId() { + ThrowingCallable callable = () -> userService.deleteUser("NOT_EXISTING"); + + assertThatThrownBy(callable) + .isInstanceOf(UserNotFoundException.class) + .hasFieldOrPropertyWithValue("userId", "NOT_EXISTING") + .hasMessage("User with id 'NOT_EXISTING' was not found."); + } + + @WithAccessId(user = "user-1-2") + @Test + void should_ThrowNotAuthorizedException_When_TryingToDeleteUserWithNoAdminRole() { + ThrowingCallable callable = () -> userService.deleteUser("testuser1"); + + assertThatThrownBy(callable) + .isInstanceOf(MismatchedRoleException.class) + .hasFieldOrPropertyWithValue("currentUserId", "user-1-2") + .hasFieldOrPropertyWithValue( + "roles", new TaskanaRole[] {TaskanaRole.BUSINESS_ADMIN, TaskanaRole.ADMIN}) + .hasMessage( + "Not authorized. The current user 'user-1-2' is not member of role(s) " + + "'[BUSINESS_ADMIN, ADMIN]'."); + } +} diff --git a/lib/taskana-core/src/test/java/acceptance/AbstractAccTest.java b/lib/taskana-core/src/test/java/acceptance/AbstractAccTest.java index 64db961b7..784236425 100644 --- a/lib/taskana-core/src/test/java/acceptance/AbstractAccTest.java +++ b/lib/taskana-core/src/test/java/acceptance/AbstractAccTest.java @@ -28,7 +28,6 @@ import pro.taskana.task.api.models.Attachment; import pro.taskana.task.api.models.ObjectReference; import pro.taskana.task.internal.TaskServiceImpl; import pro.taskana.task.internal.models.ObjectReferenceImpl; -import pro.taskana.user.api.models.User; /** Base class for all acceptance tests. */ public abstract class AbstractAccTest { @@ -120,25 +119,6 @@ public abstract class AbstractAccTest { return attachment; } - protected User createExampleUser(String id) { - User user = taskanaEngine.getUserService().newUser(); - user.setId(id); - user.setFirstName("Hans"); - user.setLastName("Georg"); - user.setFullName("Georg, Hans"); - user.setLongName("Georg, Hans - (user-10-20)"); - user.setEmail("hans.georg@web.com"); - user.setPhone("1234"); - user.setMobilePhone("01574275632"); - user.setOrgLevel4("level4"); - user.setOrgLevel3("level3"); - user.setOrgLevel2("level2"); - user.setOrgLevel1("level1"); - user.setData("ab"); - - return user; - } - protected TimeInterval toDaysInterval() { Instant begin = ZonedDateTime.of(LocalDate.now(ZoneId.of("UTC")), LocalTime.MIN, ZoneId.of("UTC")) diff --git a/lib/taskana-core/src/test/java/acceptance/user/UserServiceAccTest.java b/lib/taskana-core/src/test/java/acceptance/user/UserServiceAccTest.java deleted file mode 100644 index a7df7a494..000000000 --- a/lib/taskana-core/src/test/java/acceptance/user/UserServiceAccTest.java +++ /dev/null @@ -1,224 +0,0 @@ -package acceptance.user; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import acceptance.AbstractAccTest; -import java.util.Arrays; -import java.util.Iterator; -import java.util.stream.Stream; -import org.assertj.core.api.ThrowableAssert.ThrowingCallable; -import org.junit.jupiter.api.DynamicTest; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestFactory; -import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.jupiter.api.function.ThrowingConsumer; - -import pro.taskana.common.api.exceptions.InvalidArgumentException; -import pro.taskana.common.api.exceptions.MismatchedRoleException; -import pro.taskana.common.test.security.JaasExtension; -import pro.taskana.common.test.security.WithAccessId; -import pro.taskana.user.api.UserService; -import pro.taskana.user.api.exceptions.UserAlreadyExistException; -import pro.taskana.user.api.exceptions.UserNotFoundException; -import pro.taskana.user.api.models.User; - -/** Acceptance test which tests the functionality of the UserService. */ -@ExtendWith(JaasExtension.class) -class UserServiceAccTest extends AbstractAccTest { - private static final UserService USER_SERVICE = taskanaEngine.getUserService(); - - @WithAccessId(user = "user-1-2") - @Test - void should_ReturnUserWithAllFields_When_IdExisting() throws Exception { - User user = USER_SERVICE.getUser("teamlead-1"); - - assertThat(user.getFirstName()).isEqualTo("Titus"); - assertThat(user.getLastName()).isEqualTo("Toll"); - assertThat(user.getFullName()).isEqualTo("Toll, Titus"); - assertThat(user.getLongName()).isEqualTo("Toll, Titus - (teamlead-1)"); - assertThat(user.getEmail()).isEqualTo("titus.toll@web.de"); - assertThat(user.getPhone()).isEqualTo("040-2951854"); - assertThat(user.getMobilePhone()).isEqualTo("015637683197"); - assertThat(user.getOrgLevel4()).isEqualTo("Novatec"); - assertThat(user.getOrgLevel3()).isEqualTo("BPM"); - assertThat(user.getOrgLevel2()).isEqualTo("Human Workflow"); - assertThat(user.getOrgLevel1()).isEqualTo("TASKANA"); - assertThat(user.getData()).isEqualTo("xy"); - } - - @WithAccessId(user = "user-1-2") - @Test - void should_ThrowUserNotFoundException_When_TryingToGetUserWithNonExistingId() { - ThrowingCallable callable = () -> USER_SERVICE.getUser("NOT_EXISTING"); - assertThatThrownBy(callable) - .isInstanceOf(UserNotFoundException.class) - .hasMessage("User with id 'NOT_EXISTING' was not found."); - } - - @WithAccessId(user = "admin") - @Test - void should_InsertUserInDatabase_When_CreatingUser() throws Exception { - User userToCreate = createExampleUser("user-10-20"); - - USER_SERVICE.createUser(userToCreate); - User userInDatabse = USER_SERVICE.getUser(userToCreate.getId()); - - assertThat(userToCreate) - .hasNoNullFieldsOrProperties() - .isNotSameAs(userInDatabse) - .isEqualTo(userInDatabse); - } - - @WithAccessId(user = "admin") - @Test - void should_SetTheLongAndFullNameAccordingToRules_When_CreatingUserWithThoseFieldsEmpty() - throws Exception { - User userToCreate = createExampleUser("user-10-21"); - userToCreate.setLongName(null); - userToCreate.setFullName(null); - - String fullName = userToCreate.getLastName() + ", " + userToCreate.getFirstName(); - String longName = - userToCreate.getLastName() - + ", " - + userToCreate.getFirstName() - + " - (" - + userToCreate.getId() - + ")"; - - User createdUser = USER_SERVICE.createUser(userToCreate); - assertThat(createdUser.getLongName()).isEqualTo(longName); - assertThat(createdUser.getFullName()).isEqualTo(fullName); - } - - @WithAccessId(user = "admin") - @Test - void should_ThrowInvalidArgumentException_When_TryingToCreateUserWithFirstOrLastNameNull() - throws Exception { - User userToCreate = createExampleUser("user-10-20"); - userToCreate.setFirstName(null); - - ThrowingCallable callable = () -> USER_SERVICE.createUser(userToCreate); - assertThatThrownBy(callable) - .isInstanceOf(InvalidArgumentException.class) - .hasMessage("First and last name of User must be set or empty."); - - userToCreate.setFirstName("xy"); - userToCreate.setLastName(null); - callable = () -> USER_SERVICE.createUser(userToCreate); - assertThatThrownBy(callable).isInstanceOf(InvalidArgumentException.class); - } - - @WithAccessId(user = "admin") - @TestFactory - Stream should_ThrowInvalidArgumentException_When_TryingToCreateUserWithNotSetId() - throws Exception { - Iterator iterator = Arrays.asList("", null).iterator(); - - ThrowingConsumer test = - userId -> { - User userToCreate = createExampleUser("user-10-20"); - userToCreate.setId(userId); - ThrowingCallable callable = () -> USER_SERVICE.createUser(userToCreate); - assertThatThrownBy(callable) - .isInstanceOf(InvalidArgumentException.class) - .hasMessage("UserId must not be empty when creating User."); - }; - - return DynamicTest.stream(iterator, c -> "for " + c, test); - } - - @WithAccessId(user = "admin") - @Test - void should_ThrowUserAlreadyExistException_When_TryingToCreateUserWithExistingId() { - User userToCreate = createExampleUser("teamlead-1"); // existing userId - - ThrowingCallable callable = () -> USER_SERVICE.createUser(userToCreate); - assertThatThrownBy(callable) - .isInstanceOf(UserAlreadyExistException.class) - .hasMessage("User with id 'teamlead-1' already exists."); - } - - @WithAccessId(user = "user-1-2") - @Test - void should_ThrowNotAuthorizedException_When_TryingToCreateUserWithoutAdminRole() { - User userToCreate = createExampleUser("user-10-22"); - - ThrowingCallable callable = () -> USER_SERVICE.createUser(userToCreate); - assertThatThrownBy(callable) - .isInstanceOf(MismatchedRoleException.class) - .hasMessage( - "Not authorized. The current user 'user-1-2' is not member of role(s) " - + "'[BUSINESS_ADMIN, ADMIN]'."); - } - - @WithAccessId(user = "admin") - @Test - void should_UpdateUserInDatabase_When_IdExisting() throws Exception { - User userToUpdate = createExampleUser("teamlead-1"); // existing userId - - USER_SERVICE.updateUser(userToUpdate); - User userInDatabase = USER_SERVICE.getUser("teamlead-1"); - - assertThat(userToUpdate) - .hasNoNullFieldsOrProperties() - .isNotSameAs(userInDatabase) - .isEqualTo(userInDatabase); - } - - @WithAccessId(user = "admin") - @Test - void should_ThrowUserNotFoundException_When_TryingToUpdateUserWithNonExistingId() { - User userToUpdate = createExampleUser("NOT_EXISTING"); - - ThrowingCallable callable = () -> USER_SERVICE.updateUser(userToUpdate); - assertThatThrownBy(callable) - .isInstanceOf(UserNotFoundException.class) - .hasMessage("User with id 'NOT_EXISTING' was not found."); - } - - @WithAccessId(user = "user-1-2") - @Test - void should_ThrowNotAuthorizedException_When_TryingToUpdateUserWithNoAdminRole() { - User userToUpdate = createExampleUser("teamlead-1"); // existing userId - - ThrowingCallable callable = () -> USER_SERVICE.updateUser(userToUpdate); - assertThatThrownBy(callable) - .isInstanceOf(MismatchedRoleException.class) - .hasMessage( - "Not authorized. The current user 'user-1-2' is not member of role(s) " - + "'[BUSINESS_ADMIN, ADMIN]'."); - } - - @WithAccessId(user = "admin") - @Test - void should_DeleteUserFromDatabase_When_IdExisting() throws Exception { - String id = "teamlead-1"; - USER_SERVICE.getUser(id); // User existing - - USER_SERVICE.deleteUser(id); - ThrowingCallable callable = () -> USER_SERVICE.getUser(id); // User deleted - assertThatThrownBy(callable).isInstanceOf(UserNotFoundException.class); - } - - @WithAccessId(user = "admin") - @Test - void should_ThrowUserNotFoundException_When_TryingToDeleteUserWithNonExistingId() { - ThrowingCallable callable = () -> USER_SERVICE.deleteUser("NOT_EXISTING"); - assertThatThrownBy(callable) - .isInstanceOf(UserNotFoundException.class) - .hasMessage("User with id 'NOT_EXISTING' was not found."); - } - - @WithAccessId(user = "user-1-2") - @Test - void should_ThrowNotAuthorizedException_When_TryingToDeleteUserWithNoAdminRole() { - ThrowingCallable callable = () -> USER_SERVICE.deleteUser("teamlead-1"); - assertThatThrownBy(callable) - .isInstanceOf(MismatchedRoleException.class) - .hasMessage( - "Not authorized. The current user 'user-1-2' is not member of role(s) " - + "'[BUSINESS_ADMIN, ADMIN]'."); - } -}