diff --git a/backend/src/test/java/ch/puzzle/okr/service/persistence/UserPersistenceServiceIT.java b/backend/src/test/java/ch/puzzle/okr/service/persistence/UserPersistenceServiceIT.java index ca1dc10624..4238062717 100644 --- a/backend/src/test/java/ch/puzzle/okr/service/persistence/UserPersistenceServiceIT.java +++ b/backend/src/test/java/ch/puzzle/okr/service/persistence/UserPersistenceServiceIT.java @@ -1,13 +1,16 @@ package ch.puzzle.okr.service.persistence; +import ch.puzzle.okr.exception.OkrResponseStatusException; import ch.puzzle.okr.models.User; import ch.puzzle.okr.multitenancy.TenantContext; import ch.puzzle.okr.test.SpringIntegrationTest; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.http.HttpStatus; import org.springframework.web.server.ResponseStatusException; @@ -98,4 +101,42 @@ void getOrCreateUserShouldReturnSavedUserWhenUserNotFound() { assertEquals("lastname", createdUser.getLastname()); assertEquals("lastname@puzzle.ch", createdUser.getEmail()); } + + @DisplayName("deleteById() should delete user when user found") + @Test + void deleteByIdShouldDeleteUserWhenUserFound() { + // arrange + User user = createUser(); + + // act + userPersistenceService.deleteById(user.getId()); + + // assert + OkrResponseStatusException exception = assertThrows(OkrResponseStatusException.class, // + () -> userPersistenceService.findById(createdUser.getId())); + + assertEquals(HttpStatus.NOT_FOUND, exception.getStatusCode()); + } + + private User createUser() { + User newUser = User.Builder.builder() // + .withId(null) // + .withFirstname("firstname") // + .withLastname("lastname") // + .withEmail("lastname@puzzle.ch") // + .build(); + createdUser = userPersistenceService.getOrCreateUser(newUser); + assertNotNull(createdUser.getId()); + return createdUser; + } + + @DisplayName("deleteById() should throw exception when Id is null") + @Test + void deleteByIdShouldThrowExceptionWhenIdIsNull() { + InvalidDataAccessApiUsageException exception = assertThrows(InvalidDataAccessApiUsageException.class, // + () -> userPersistenceService.deleteById(null)); + + assertEquals("The given id must not be null", exception.getMessage()); + } + } \ No newline at end of file