diff --git a/src/test/java/com/example/usersapi/controller/UserControllerTest.java b/src/test/java/com/example/usersapi/controller/UserControllerTest.java index f282f5e..edff88e 100644 --- a/src/test/java/com/example/usersapi/controller/UserControllerTest.java +++ b/src/test/java/com/example/usersapi/controller/UserControllerTest.java @@ -1,5 +1,12 @@ package com.example.usersapi.controller; +import static com.example.usersapi.util.TestUtil.REQUEST_DTO; +import static com.example.usersapi.util.TestUtil.RESPONSE_DTO1; +import static com.example.usersapi.util.TestUtil.RESPONSE_DTO2; +import static com.example.usersapi.util.TestUtil.RESPONSE_DTO3; +import static com.example.usersapi.util.TestUtil.RESPONSE_DTO4; +import static com.example.usersapi.util.TestUtil.RESPONSE_DTO5; +import static com.example.usersapi.util.TestUtil.USER1; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; @@ -8,17 +15,13 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import com.example.usersapi.dto.CreateUserRequestDto; import com.example.usersapi.dto.UserPatchRequestDto; import com.example.usersapi.dto.UserResponseDto; import com.example.usersapi.dto.UserResponseDtoWrapper; -import com.example.usersapi.model.User; import com.example.usersapi.service.impl.UserServiceImpl; import com.example.usersapi.util.TestUtil; import com.fasterxml.jackson.databind.ObjectMapper; -import java.time.LocalDate; import java.util.List; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; @@ -33,8 +36,6 @@ @RunWith(SpringRunner.class) @WebMvcTest(UserController.class) class UserControllerTest { - private CreateUserRequestDto requestDto; - private UserResponseDto responseDto; @Autowired private MockMvc mvc; @Autowired @@ -42,36 +43,22 @@ class UserControllerTest { @MockBean private UserServiceImpl userService; - @BeforeEach - void init() { - requestDto = new CreateUserRequestDto( - "user@gmail.com", - "Bob", - "Jackson", - "01/02/1990", - "414 Union Ave, Brooklyn, NY 11211", - "(111) 111-1111" - ); - User user = TestUtil.getUserFromCreateUserDto(1L, requestDto); - responseDto = TestUtil.getResponseDtoFromUser(user); - } - @Test @DisplayName("Add a new user") void addUser_ValidRequest_ShouldAddUser() throws Exception { - Mockito.when(userService.createUser(requestDto)) - .thenReturn(new UserResponseDtoWrapper<>(responseDto)); - String jasonObject = objectMapper.writeValueAsString(requestDto); + Mockito.when(userService.createUser(REQUEST_DTO)) + .thenReturn(new UserResponseDtoWrapper<>(RESPONSE_DTO1)); + String jasonObject = objectMapper.writeValueAsString(REQUEST_DTO); mvc.perform(post("/api/users") .content(jasonObject) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isCreated()) - .andExpect(jsonPath("$.data.email").value(requestDto.email())) - .andExpect(jsonPath("$.data.address").value(requestDto.address())) - .andExpect(jsonPath("$.data.lastName").value(requestDto.lastName())) - .andExpect(jsonPath("$.data.birthDate").value(requestDto.birthDate())) - .andExpect(jsonPath("$.data.firstName").value(requestDto.firstName())) - .andExpect(jsonPath("$.data.phoneNumber").value(requestDto.phoneNumber())); + .andExpect(jsonPath("$.data.email").value(REQUEST_DTO.email())) + .andExpect(jsonPath("$.data.address").value(REQUEST_DTO.address())) + .andExpect(jsonPath("$.data.lastName").value(REQUEST_DTO.lastName())) + .andExpect(jsonPath("$.data.birthDate").value(REQUEST_DTO.birthDate())) + .andExpect(jsonPath("$.data.firstName").value(REQUEST_DTO.firstName())) + .andExpect(jsonPath("$.data.phoneNumber").value(REQUEST_DTO.phoneNumber())); } @Test @@ -79,6 +66,7 @@ void addUser_ValidRequest_ShouldAddUser() throws Exception { void patchUser_ValidRequest_ShouldPatchUserInfo() throws Exception { UserPatchRequestDto patchRequestDto = new UserPatchRequestDto(null, "Adam", null, null, null, "(111) 111-2222"); + UserResponseDto responseDto = TestUtil.getResponseDtoFromUser(USER1); responseDto.setFirstName(patchRequestDto.firstName()); responseDto.setPhoneNumber(patchRequestDto.phoneNumber()); String jasonObject = objectMapper.writeValueAsString(patchRequestDto); @@ -98,19 +86,19 @@ void patchUser_ValidRequest_ShouldPatchUserInfo() throws Exception { @DisplayName("Update user") void updateUser_ValidRequest_ShouldUpdateUserInfo() throws Exception { Long userId = 1L; - Mockito.when(userService.updateUser(userId, requestDto)) - .thenReturn(new UserResponseDtoWrapper<>(responseDto)); - String jasonObject = objectMapper.writeValueAsString(requestDto); + Mockito.when(userService.updateUser(userId, REQUEST_DTO)) + .thenReturn(new UserResponseDtoWrapper<>(RESPONSE_DTO1)); + String jasonObject = objectMapper.writeValueAsString(REQUEST_DTO); mvc.perform(put("/api/users/" + userId) .content(jasonObject) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) - .andExpect(jsonPath("$.data.email").value(requestDto.email())) - .andExpect(jsonPath("$.data.address").value(requestDto.address())) - .andExpect(jsonPath("$.data.lastName").value(requestDto.lastName())) - .andExpect(jsonPath("$.data.birthDate").value(requestDto.birthDate())) - .andExpect(jsonPath("$.data.firstName").value(requestDto.firstName())) - .andExpect(jsonPath("$.data.phoneNumber").value(requestDto.phoneNumber())); + .andExpect(jsonPath("$.data.email").value(REQUEST_DTO.email())) + .andExpect(jsonPath("$.data.address").value(REQUEST_DTO.address())) + .andExpect(jsonPath("$.data.lastName").value(REQUEST_DTO.lastName())) + .andExpect(jsonPath("$.data.birthDate").value(REQUEST_DTO.birthDate())) + .andExpect(jsonPath("$.data.firstName").value(REQUEST_DTO.firstName())) + .andExpect(jsonPath("$.data.phoneNumber").value(REQUEST_DTO.phoneNumber())); } @Test @@ -122,94 +110,78 @@ void deleteUserById_ValidRequest_ShouldDeleteUser() throws Exception { } @Test - @DisplayName("Search for users in birth dates range") - void searchByBirthDates_ValidRequest_ShouldReturnListOfUsersInDatesRange() throws Exception { - User user2 = new User(); - user2.setId(2L); - user2.setFirstName("Kassandra"); - user2.setLastName("Hernandez"); - user2.setEmail("Kassandra@example.com"); - user2.setBirthDate(LocalDate.of(1985, 5, 15)); - - User user3 = new User(); - user3.setId(3L); - user3.setFirstName("William"); - user3.setLastName("Williamson"); - user3.setEmail("William@example.com"); - user3.setBirthDate(LocalDate.of(1988, 9, 20)); - - User user4 = new User(); - user4.setId(4L); - user4.setFirstName("Kevin"); - user4.setLastName("Thompson"); - user4.setEmail("Kevin@example.com"); - user4.setBirthDate(LocalDate.of(1995, 3, 10)); - - User user5 = new User(); - user5.setId(5L); - user5.setFirstName("Matt"); - user5.setLastName("Wallace"); - user5.setEmail("Matt@example.com"); - user5.setBirthDate(LocalDate.of(1992, 11, 28)); - - UserResponseDto responseDto3 = TestUtil.getResponseDtoFromUser(user3); - UserResponseDto responseDto4 = TestUtil.getResponseDtoFromUser(user4); - + @DisplayName("Search for users in birth dates range from 02/03/1986 to 10/03/1995") + void searchByBirthDates_DatesRangeFrom1986To1995_ShouldReturnListOfUsersInDatesRange() + throws Exception { String from = "02/03/1986"; String to = "10/03/1995"; Mockito.when(userService.searchByBirthDates(from, to)).thenReturn( - new UserResponseDtoWrapper<>(List.of(responseDto, responseDto3, responseDto4))); + new UserResponseDtoWrapper<>(List.of(RESPONSE_DTO1, RESPONSE_DTO3, RESPONSE_DTO4))); String getUrl = "/api/users/search?fromDate=%s&toDate=%s"; mvc.perform(get(String.format(getUrl, from, to))) .andExpect(status().isOk()) .andExpect(jsonPath("$.data.size()").value(3)) - .andExpect(jsonPath("$.data[0].firstName").value(responseDto.getFirstName())) - .andExpect(jsonPath("$.data[0].birthDate").value(responseDto.getBirthDate())) - .andExpect(jsonPath("$.data[0].email").value(responseDto.getEmail())) - .andExpect(jsonPath("$.data[1].firstName").value(responseDto3.getFirstName())) - .andExpect(jsonPath("$.data[1].birthDate").value(responseDto3.getBirthDate())) - .andExpect(jsonPath("$.data[1].email").value(responseDto3.getEmail())) - .andExpect(jsonPath("$.data[2].firstName").value(responseDto4.getFirstName())) - .andExpect(jsonPath("$.data[2].birthDate").value(responseDto4.getBirthDate())) - .andExpect(jsonPath("$.data[2].email").value(responseDto4.getEmail())); + .andExpect(jsonPath("$.data[0].firstName").value(RESPONSE_DTO1.getFirstName())) + .andExpect(jsonPath("$.data[0].birthDate").value(RESPONSE_DTO1.getBirthDate())) + .andExpect(jsonPath("$.data[0].email").value(RESPONSE_DTO1.getEmail())) + .andExpect(jsonPath("$.data[1].firstName").value(RESPONSE_DTO3.getFirstName())) + .andExpect(jsonPath("$.data[1].birthDate").value(RESPONSE_DTO3.getBirthDate())) + .andExpect(jsonPath("$.data[1].email").value(RESPONSE_DTO3.getEmail())) + .andExpect(jsonPath("$.data[2].firstName").value(RESPONSE_DTO4.getFirstName())) + .andExpect(jsonPath("$.data[2].birthDate").value(RESPONSE_DTO4.getBirthDate())) + .andExpect(jsonPath("$.data[2].email").value(RESPONSE_DTO4.getEmail())); + } - UserResponseDto responseDto2 = TestUtil.getResponseDtoFromUser(user2); - UserResponseDto responseDto5 = TestUtil.getResponseDtoFromUser(user5); + @Test + @DisplayName("Search for users in birth dates range from 15/04/1985 to 10/03/2000") + void searchByBirthDates_DatesRangeFrom1985To2000_ShouldReturnListOfUsersInDatesRange() + throws Exception { + String from = "15/04/1985"; + String to = "10/03/2000"; - from = "15/04/1985"; - to = "10/03/2000"; Mockito.when(userService.searchByBirthDates(from, to)) .thenReturn(new UserResponseDtoWrapper<>(List.of( - responseDto, responseDto2, responseDto3, responseDto4, responseDto5 + RESPONSE_DTO1, + RESPONSE_DTO2, + RESPONSE_DTO3, + RESPONSE_DTO4, + RESPONSE_DTO5 ))); + String getUrl = "/api/users/search?fromDate=%s&toDate=%s"; mvc.perform(get(String.format(getUrl, from, to))) .andExpect(status().isOk()) .andExpect(jsonPath("$.data.size()").value(5)) - .andExpect(jsonPath("$.data[0].firstName").value(responseDto.getFirstName())) - .andExpect(jsonPath("$.data[0].birthDate").value(responseDto.getBirthDate())) - .andExpect(jsonPath("$.data[0].email").value(responseDto.getEmail())) - .andExpect(jsonPath("$.data[1].firstName").value(responseDto2.getFirstName())) - .andExpect(jsonPath("$.data[1].birthDate").value(responseDto2.getBirthDate())) - .andExpect(jsonPath("$.data[1].email").value(responseDto2.getEmail())) - .andExpect(jsonPath("$.data[2].firstName").value(responseDto3.getFirstName())) - .andExpect(jsonPath("$.data[2].birthDate").value(responseDto3.getBirthDate())) - .andExpect(jsonPath("$.data[2].email").value(responseDto3.getEmail())) - .andExpect(jsonPath("$.data[3].firstName").value(responseDto4.getFirstName())) - .andExpect(jsonPath("$.data[3].birthDate").value(responseDto4.getBirthDate())) - .andExpect(jsonPath("$.data[3].email").value(responseDto4.getEmail())) - .andExpect(jsonPath("$.data[4].firstName").value(responseDto5.getFirstName())) - .andExpect(jsonPath("$.data[4].birthDate").value(responseDto5.getBirthDate())) - .andExpect(jsonPath("$.data[4].email").value(responseDto5.getEmail())); + .andExpect(jsonPath("$.data[0].firstName").value(RESPONSE_DTO1.getFirstName())) + .andExpect(jsonPath("$.data[0].birthDate").value(RESPONSE_DTO1.getBirthDate())) + .andExpect(jsonPath("$.data[0].email").value(RESPONSE_DTO1.getEmail())) + .andExpect(jsonPath("$.data[1].firstName").value(RESPONSE_DTO2.getFirstName())) + .andExpect(jsonPath("$.data[1].birthDate").value(RESPONSE_DTO2.getBirthDate())) + .andExpect(jsonPath("$.data[1].email").value(RESPONSE_DTO2.getEmail())) + .andExpect(jsonPath("$.data[2].firstName").value(RESPONSE_DTO3.getFirstName())) + .andExpect(jsonPath("$.data[2].birthDate").value(RESPONSE_DTO3.getBirthDate())) + .andExpect(jsonPath("$.data[2].email").value(RESPONSE_DTO3.getEmail())) + .andExpect(jsonPath("$.data[3].firstName").value(RESPONSE_DTO4.getFirstName())) + .andExpect(jsonPath("$.data[3].birthDate").value(RESPONSE_DTO4.getBirthDate())) + .andExpect(jsonPath("$.data[3].email").value(RESPONSE_DTO4.getEmail())) + .andExpect(jsonPath("$.data[4].firstName").value(RESPONSE_DTO5.getFirstName())) + .andExpect(jsonPath("$.data[4].birthDate").value(RESPONSE_DTO5.getBirthDate())) + .andExpect(jsonPath("$.data[4].email").value(RESPONSE_DTO5.getEmail())); + } - from = "15/04/1990"; - to = "10/03/2003"; + @Test + @DisplayName("Search for users in birth dates range from 15/04/1990 to 10/03/2003") + void searchByBirthDates_DatesRangeFrom1990To2003_ShouldReturnListOfUsersInDatesRange() + throws Exception { + String from = "15/04/1990"; + String to = "10/03/2003"; Mockito.when(userService.searchByBirthDates(from, to)) - .thenReturn(new UserResponseDtoWrapper<>(List.of(responseDto5))); + .thenReturn(new UserResponseDtoWrapper<>(List.of(RESPONSE_DTO5))); + String getUrl = "/api/users/search?fromDate=%s&toDate=%s"; mvc.perform(get(String.format(getUrl, from, to))) .andExpect(status().isOk()) .andExpect(jsonPath("$.data.size()").value(1)) - .andExpect(jsonPath("$.data[0].firstName").value(responseDto5.getFirstName())) - .andExpect(jsonPath("$.data[0].birthDate").value(responseDto5.getBirthDate())) - .andExpect(jsonPath("$.data[0].email").value(responseDto5.getEmail())); + .andExpect(jsonPath("$.data[0].firstName").value(RESPONSE_DTO5.getFirstName())) + .andExpect(jsonPath("$.data[0].birthDate").value(RESPONSE_DTO5.getBirthDate())) + .andExpect(jsonPath("$.data[0].email").value(RESPONSE_DTO5.getEmail())); } } diff --git a/src/test/java/com/example/usersapi/repository/UserRepositoryTest.java b/src/test/java/com/example/usersapi/repository/UserRepositoryTest.java deleted file mode 100644 index 92b563e..0000000 --- a/src/test/java/com/example/usersapi/repository/UserRepositoryTest.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.example.usersapi.repository; - -import static org.assertj.core.api.Assertions.assertThat; - -import com.example.usersapi.model.User; -import java.time.LocalDate; -import java.util.List; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; -import org.springframework.test.context.jdbc.Sql; - -@DataJpaTest -class UserRepositoryTest { - @Autowired - private UserRepository userRepository; - - @Test - @DisplayName("Get users in birth dates range") - @Sql(scripts = "classpath:database/add-five-users.sql", - executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) - @Sql(scripts = "classpath:database/clear-db.sql", - executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) - void findByBirthDateBetween_ValidRequest_ShouldReturnListOfUsers() { - LocalDate from = LocalDate.of(1985, 2, 2); - LocalDate to = LocalDate.of(1991, 1, 1); - List actual = userRepository.findByBirthDateBetween(from, to); - Assertions.assertEquals(3, actual.size()); - for (User user : actual) { - assertThat(user.getBirthDate().getYear()).isIn(1985, 1988, 1990); - } - - to = LocalDate.of(2000, 1, 1); - actual = userRepository.findByBirthDateBetween(from, to); - Assertions.assertEquals(5, actual.size()); - for (User user : actual) { - assertThat(user.getBirthDate().getYear()).isIn(1985, 1988, 1990, 1992, 1995); - } - - from = LocalDate.of(1992, 11, 28); - to = LocalDate.of(1995, 3, 10); - actual = userRepository.findByBirthDateBetween(from, to); - Assertions.assertEquals(2, actual.size()); - for (User user : actual) { - assertThat(user.getBirthDate().getYear()).isIn(1992, 1995); - } - } -} diff --git a/src/test/java/com/example/usersapi/service/UserServiceTest.java b/src/test/java/com/example/usersapi/service/UserServiceTest.java index 29bea85..9134012 100644 --- a/src/test/java/com/example/usersapi/service/UserServiceTest.java +++ b/src/test/java/com/example/usersapi/service/UserServiceTest.java @@ -1,5 +1,17 @@ package com.example.usersapi.service; +import static com.example.usersapi.util.TestUtil.FORMATTER; +import static com.example.usersapi.util.TestUtil.RESPONSE_DTO1; +import static com.example.usersapi.util.TestUtil.RESPONSE_DTO2; +import static com.example.usersapi.util.TestUtil.RESPONSE_DTO3; +import static com.example.usersapi.util.TestUtil.RESPONSE_DTO4; +import static com.example.usersapi.util.TestUtil.RESPONSE_DTO5; +import static com.example.usersapi.util.TestUtil.USER1; +import static com.example.usersapi.util.TestUtil.USER2; +import static com.example.usersapi.util.TestUtil.USER3; +import static com.example.usersapi.util.TestUtil.USER4; +import static com.example.usersapi.util.TestUtil.USER5; + import com.example.usersapi.dto.CreateUserRequestDto; import com.example.usersapi.dto.UserPatchRequestDto; import com.example.usersapi.dto.UserResponseDto; @@ -26,9 +38,6 @@ @ExtendWith(MockitoExtension.class) class UserServiceTest { - private CreateUserRequestDto requestDto; - private User user; - private UserResponseDto responseDto; @Mock private UserRepository userRepository; @Mock @@ -38,17 +47,6 @@ class UserServiceTest { @BeforeEach void init() throws Exception { - requestDto = new CreateUserRequestDto( - "user@gmail.com", - "Bob", - "Jackson", - "01/02/1990", - "414 Union Ave, Brooklyn, NY 11211", - "(111) 111-1111" - ); - user = TestUtil.getUserFromCreateUserDto(1L, requestDto); - responseDto = TestUtil.getResponseDtoFromUser(user); - Field field = userService.getClass().getDeclaredField("minAge"); field.setAccessible(true); field.setInt(userService, 18); @@ -57,16 +55,16 @@ void init() throws Exception { @Test @DisplayName("Add a new used to db") void createUser_ValidRequest_ShouldAddUser() { - Mockito.when(userMapper.toModel(requestDto)).thenReturn(user); - Mockito.when(userRepository.save(user)).thenReturn(user); - Mockito.when(userMapper.toDto(user)).thenReturn(responseDto); + Mockito.when(userMapper.toModel(TestUtil.REQUEST_DTO)).thenReturn(USER1); + Mockito.when(userRepository.save(USER1)).thenReturn(USER1); + Mockito.when(userMapper.toDto(USER1)).thenReturn(RESPONSE_DTO1); - UserResponseDto actual = userService.createUser(requestDto).getData(); + UserResponseDto actual = userService.createUser(TestUtil.REQUEST_DTO).getData(); - Assertions.assertEquals(responseDto.getId(), actual.getId()); - Assertions.assertEquals(responseDto.getBirthDate(), actual.getBirthDate()); - Assertions.assertEquals(responseDto.getFirstName(), actual.getFirstName()); - Assertions.assertEquals(responseDto.getAddress(), actual.getAddress()); + Assertions.assertEquals(RESPONSE_DTO1.getId(), actual.getId()); + Assertions.assertEquals(RESPONSE_DTO1.getBirthDate(), actual.getBirthDate()); + Assertions.assertEquals(RESPONSE_DTO1.getFirstName(), actual.getFirstName()); + Assertions.assertEquals(RESPONSE_DTO1.getAddress(), actual.getAddress()); } @Test @@ -95,17 +93,17 @@ void patchUser_ValidRequest_ShouldUpdateUserField() { Long userId = 1L; User updatedUser = new User(); updatedUser.setId(userId); - updatedUser.setEmail(user.getEmail()); + updatedUser.setEmail(USER1.getEmail()); updatedUser.setLastName(newLastName); - updatedUser.setFirstName(user.getFirstName()); - updatedUser.setAddress(user.getAddress()); - updatedUser.setPhoneNumber(user.getPhoneNumber()); - updatedUser.setBirthDate(user.getBirthDate()); + updatedUser.setFirstName(USER1.getFirstName()); + updatedUser.setAddress(USER1.getAddress()); + updatedUser.setPhoneNumber(USER1.getPhoneNumber()); + updatedUser.setBirthDate(USER1.getBirthDate()); UserResponseDto updatedResponseDto = TestUtil.getResponseDtoFromUser(updatedUser); - Mockito.when(userRepository.findById(userId)).thenReturn(Optional.of(user)); - Mockito.when(userRepository.save(user)).thenReturn(user); + Mockito.when(userRepository.findById(userId)).thenReturn(Optional.of(USER1)); + Mockito.when(userRepository.save(USER1)).thenReturn(USER1); Mockito.when(userMapper.toDto(updatedUser)).thenReturn(updatedResponseDto); UserPatchRequestDto userPatchRequestDto = new UserPatchRequestDto( @@ -165,77 +163,65 @@ void updateUser_ValidRequest_ShouldUpdateAllUserInfo() { } @Test - @DisplayName("Search for users in birth dates range") - void searchByBirthDates_ValidRequest_ShouldReturnListOfUsersInDatesRange() { - User user2 = new User(); - user2.setId(2L); - user2.setFirstName("Jane"); - user2.setLastName("Smith"); - user2.setEmail("user2@example.com"); - user2.setBirthDate(LocalDate.of(1985, 5, 15)); - - User user3 = new User(); - user3.setId(3L); - user3.setFirstName("Alice"); - user3.setLastName("Johnson"); - user3.setEmail("user3@example.com"); - user3.setBirthDate(LocalDate.of(1988, 9, 20)); - - User user4 = new User(); - user4.setId(4L); - user4.setFirstName("Bob"); - user4.setLastName("Brown"); - user4.setEmail("user4@example.com"); - user4.setBirthDate(LocalDate.of(1995, 3, 10)); - - User user5 = new User(); - user5.setId(5L); - user5.setFirstName("Emily"); - user5.setLastName("Davis"); - user5.setEmail("user5@example.com"); - user5.setBirthDate(LocalDate.of(1992, 11, 28)); - - UserResponseDto responseDto2 = TestUtil.getResponseDtoFromUser(user2); - UserResponseDto responseDto3 = TestUtil.getResponseDtoFromUser(user3); - UserResponseDto responseDto4 = TestUtil.getResponseDtoFromUser(user4); - UserResponseDto responseDto5 = TestUtil.getResponseDtoFromUser(user5); - - Mockito.when(userMapper.toDto(user)).thenReturn(responseDto); - Mockito.when(userMapper.toDto(user2)).thenReturn(responseDto2); - Mockito.when(userMapper.toDto(user3)).thenReturn(responseDto3); - Mockito.when(userMapper.toDto(user4)).thenReturn(responseDto4); - Mockito.when(userMapper.toDto(user5)).thenReturn(responseDto5); + @DisplayName("Search for users in birth dates range from 02/03/1986 to 10/03/1995") + void searchByBirthDates_DatesRangeFrom1986To1995_ShouldReturnListOfUsersInDatesRange() { + Mockito.when(userMapper.toDto(USER1)).thenReturn(RESPONSE_DTO1); + Mockito.when(userMapper.toDto(USER3)).thenReturn(RESPONSE_DTO3); + Mockito.when(userMapper.toDto(USER4)).thenReturn(RESPONSE_DTO4); String from = "02/03/1986"; String to = "10/03/1995"; Mockito.when(userRepository.findByBirthDateBetween( - LocalDate.parse(from, TestUtil.FORMATTER), - LocalDate.parse(to, TestUtil.FORMATTER)) - ).thenReturn(List.of(user, user3, user4)); - List expected = List.of(responseDto, responseDto3, responseDto4); + LocalDate.parse(from, FORMATTER), + LocalDate.parse(to, FORMATTER)) + ).thenReturn(List.of(USER1, USER3, USER4)); + List expected = + List.of(RESPONSE_DTO1, RESPONSE_DTO3, RESPONSE_DTO4); List actual = userService.searchByBirthDates(from, to).getData(); Assertions.assertEquals(expected.size(), actual.size()); Assertions.assertTrue(actual.containsAll(expected)); + } - from = "15/04/1985"; - to = "10/03/2000"; + @Test + @DisplayName("Search for users in birth dates range from 15/04/1985 to 10/03/2000") + void searchByBirthDates_DatesRangeFrom1985To2000_ShouldReturnListOfUsersInDatesRange() { + Mockito.when(userMapper.toDto(USER1)).thenReturn(RESPONSE_DTO1); + Mockito.when(userMapper.toDto(USER2)).thenReturn(RESPONSE_DTO2); + Mockito.when(userMapper.toDto(USER3)).thenReturn(RESPONSE_DTO3); + Mockito.when(userMapper.toDto(USER4)).thenReturn(RESPONSE_DTO4); + Mockito.when(userMapper.toDto(USER5)).thenReturn(RESPONSE_DTO5); + + String from = "15/04/1985"; + String to = "10/03/2000"; Mockito.when(userRepository.findByBirthDateBetween( - LocalDate.parse(from, TestUtil.FORMATTER), - LocalDate.parse(to, TestUtil.FORMATTER)) - ).thenReturn(List.of(user, user2, user3, user4, user5)); - expected = List.of(responseDto, responseDto2, responseDto3, responseDto4, responseDto5); - actual = userService.searchByBirthDates(from, to).getData(); + LocalDate.parse(from, FORMATTER), + LocalDate.parse(to, FORMATTER)) + ).thenReturn(List.of(USER1, USER2, USER3, USER4, USER5)); + List expected = List.of( + RESPONSE_DTO1, + RESPONSE_DTO2, + RESPONSE_DTO3, + RESPONSE_DTO4, + RESPONSE_DTO5 + ); + List actual = userService.searchByBirthDates(from, to).getData(); Assertions.assertEquals(expected.size(), actual.size()); Assertions.assertTrue(actual.containsAll(expected)); + } - from = "15/04/1990"; - to = "10/03/2003"; + @Test + @DisplayName("Search for users in birth dates range from 15/04/1990 to 10/03/2003") + void searchByBirthDates_DatesRangeFrom1990To2003_ShouldReturnListOfUsersInDatesRange() { + Mockito.when(userMapper.toDto(USER5)).thenReturn(RESPONSE_DTO5); + + String from = "15/04/1990"; + String to = "10/03/2003"; Mockito.when(userRepository.findByBirthDateBetween( - LocalDate.parse(from, TestUtil.FORMATTER), - LocalDate.parse(to, TestUtil.FORMATTER)) - ).thenReturn(List.of(user5)); - expected = List.of(responseDto5); - actual = userService.searchByBirthDates(from, to).getData(); + LocalDate.parse(from, FORMATTER), + LocalDate.parse(to, FORMATTER)) + ).thenReturn(List.of(USER5)); + List expected = List.of(RESPONSE_DTO5); + List actual = userService.searchByBirthDates(from, to).getData(); Assertions.assertEquals(expected.size(), actual.size()); Assertions.assertTrue(actual.containsAll(expected)); } diff --git a/src/test/java/com/example/usersapi/util/TestUtil.java b/src/test/java/com/example/usersapi/util/TestUtil.java index 9861b62..d5c534e 100644 --- a/src/test/java/com/example/usersapi/util/TestUtil.java +++ b/src/test/java/com/example/usersapi/util/TestUtil.java @@ -8,6 +8,63 @@ public class TestUtil { public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("dd/MM/yyyy"); + public static final CreateUserRequestDto REQUEST_DTO; + public static final User USER1; + public static final User USER2; + public static final User USER3; + public static final User USER4; + public static final User USER5; + public static final UserResponseDto RESPONSE_DTO1; + public static final UserResponseDto RESPONSE_DTO2; + public static final UserResponseDto RESPONSE_DTO3; + public static final UserResponseDto RESPONSE_DTO4; + public static final UserResponseDto RESPONSE_DTO5; + + static { + REQUEST_DTO = new CreateUserRequestDto( + "user@gmail.com", + "Bob", + "Jackson", + "01/02/1990", + "414 Union Ave, Brooklyn, NY 11211", + "(111) 111-1111" + ); + USER1 = getUserFromCreateUserDto(1L, REQUEST_DTO); + + USER2 = new User(); + USER2.setId(2L); + USER2.setFirstName("Jane"); + USER2.setLastName("Smith"); + USER2.setEmail("user2@example.com"); + USER2.setBirthDate(LocalDate.of(1985, 5, 15)); + + USER3 = new User(); + USER3.setId(3L); + USER3.setFirstName("Alice"); + USER3.setLastName("Johnson"); + USER3.setEmail("user3@example.com"); + USER3.setBirthDate(LocalDate.of(1988, 9, 20)); + + USER4 = new User(); + USER4.setId(4L); + USER4.setFirstName("Bob"); + USER4.setLastName("Brown"); + USER4.setEmail("user4@example.com"); + USER4.setBirthDate(LocalDate.of(1995, 3, 10)); + + USER5 = new User(); + USER5.setId(5L); + USER5.setFirstName("Emily"); + USER5.setLastName("Davis"); + USER5.setEmail("user5@example.com"); + USER5.setBirthDate(LocalDate.of(1992, 11, 28)); + + RESPONSE_DTO1 = getResponseDtoFromUser(USER1); + RESPONSE_DTO2 = getResponseDtoFromUser(USER2); + RESPONSE_DTO3 = getResponseDtoFromUser(USER3); + RESPONSE_DTO4 = getResponseDtoFromUser(USER4); + RESPONSE_DTO5 = getResponseDtoFromUser(USER5); + } public static User getUserFromCreateUserDto(Long userId, CreateUserRequestDto requestDto) { User resulUser = new User(); diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties deleted file mode 100644 index a74ba80..0000000 --- a/src/test/resources/application.properties +++ /dev/null @@ -1,10 +0,0 @@ -spring.datasource.url=jdbc:h2:mem:testdb -spring.datasource.driverClassName=org.h2.Driver -spring.datasource.username=sa -spring.datasource.password=password -spring.jpa.database-platform=org.hibernate.dialect.H2Dialect - -spring.jpa.hibernate.ddl-auto=create-drop -spring.h2.console.enabled=true - -user.min.age=18 diff --git a/src/test/resources/database/add-five-users.sql b/src/test/resources/database/add-five-users.sql deleted file mode 100644 index c779fe1..0000000 --- a/src/test/resources/database/add-five-users.sql +++ /dev/null @@ -1,6 +0,0 @@ -INSERT INTO users (email, first_name, last_name, birth_date, address, phone_number) -VALUES ('user1@example.com', 'John', 'Doe', '1990-01-01', '123 Main St', '123-456-7890'), - ('user2@example.com', 'Jane', 'Smith', '1985-05-15', '456 Elm St', '456-789-0123'), - ('user3@example.com', 'Alice', 'Johnson', '1988-09-20', '789 Oak St', '789-012-3456'), - ('user4@example.com', 'Bob', 'Brown', '1995-03-10', '321 Pine St', '012-345-6789'), - ('user5@example.com', 'Emily', 'Davis', '1992-11-28', '654 Birch St', '987-654-3210'); diff --git a/src/test/resources/database/clear-db.sql b/src/test/resources/database/clear-db.sql deleted file mode 100644 index d69513f..0000000 --- a/src/test/resources/database/clear-db.sql +++ /dev/null @@ -1 +0,0 @@ -DELETE FROM users; \ No newline at end of file