-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#931: UserPersistenceService test for delete user by id
- Loading branch information
1 parent
c36b2db
commit f55e7bb
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("[email protected]", 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("[email protected]") // | ||
.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()); | ||
} | ||
|
||
} |