Skip to content

Commit

Permalink
#931: UserPersistenceService test for delete user by id
Browse files Browse the repository at this point in the history
  • Loading branch information
clean-coder committed Jul 30, 2024
1 parent c36b2db commit f55e7bb
Showing 1 changed file with 41 additions and 0 deletions.
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;

Expand Down Expand Up @@ -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());
}

}

0 comments on commit f55e7bb

Please sign in to comment.