From 55dd12f8b12b3311b6906c0fd3bc4918479cf1cf Mon Sep 17 00:00:00 2001 From: Kizerov Dmytro Date: Wed, 18 Dec 2024 14:34:16 +0200 Subject: [PATCH 1/4] added new feature to add chat link to user --- .../greencity/configuration/SecurityConfig.java | 3 ++- .../greencity/controller/AdminUbsController.java | 15 +++++++++++++++ .../controller/AdminUbsControllerTest.java | 13 +++++++++++++ dao/src/main/java/greencity/entity/user/User.java | 3 +++ .../db/changelog/db.changelog-master.xml | 1 + ...12-18-add-new-column-to-user-table-Kizerov.xml | 14 ++++++++++++++ .../dto/order/UserWithSomeOrderDetailDto.java | 1 + .../main/java/greencity/dto/user/ChatLinkDto.java | 12 ++++++++++++ .../service/ubs/OrdersAdminsPageService.java | 9 +++++++++ .../service/ubs/OrdersAdminsPageServiceImpl.java | 13 +++++++++++++ .../ubs/ValuesForUserTableServiceImpl.java | 4 ++++ .../ubs/OrdersAdminsPageServiceImplTest.java | 12 ++++++++++++ 12 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 dao/src/main/resources/db/changelog/logs/2024-12-18-add-new-column-to-user-table-Kizerov.xml create mode 100644 service-api/src/main/java/greencity/dto/user/ChatLinkDto.java diff --git a/core/src/main/java/greencity/configuration/SecurityConfig.java b/core/src/main/java/greencity/configuration/SecurityConfig.java index 9ed2077d4b..1157a92ace 100644 --- a/core/src/main/java/greencity/configuration/SecurityConfig.java +++ b/core/src/main/java/greencity/configuration/SecurityConfig.java @@ -197,7 +197,8 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti .hasAnyRole(ADMIN, UBS_EMPLOYEE) .requestMatchers(HttpMethod.PATCH, SUPER_ADMIN_LINK + "/deactivateCourier/{id}", - SUPER_ADMIN_LINK + "/switchTariffStatus/{tariffId}") + SUPER_ADMIN_LINK + "/switchTariffStatus/{tariffId}", + UBS_MANAG_LINK + "/addChatLink") .hasAnyRole(ADMIN, UBS_EMPLOYEE) .requestMatchers(HttpMethod.PATCH, UBS_MANAG_LINK + "/update-order-page-admin-info/{id}", diff --git a/core/src/main/java/greencity/controller/AdminUbsController.java b/core/src/main/java/greencity/controller/AdminUbsController.java index 91fefcaf9a..72c3966b41 100644 --- a/core/src/main/java/greencity/controller/AdminUbsController.java +++ b/core/src/main/java/greencity/controller/AdminUbsController.java @@ -8,6 +8,7 @@ import greencity.dto.pageble.PageableDto; import greencity.dto.table.ColumnWidthDto; import greencity.dto.table.TableParamsDto; +import greencity.dto.user.ChatLinkDto; import greencity.dto.violation.UserViolationsWithUserName; import greencity.enums.SortingOrder; import greencity.filters.CustomerPage; @@ -270,4 +271,18 @@ public ResponseEntity saveIsFreeze(@CurrentUserUuid String uuid, bigOrderTableServiceView.changeIsFreezeStatus(uuid, value); return ResponseEntity.status(HttpStatus.OK).body(HttpStatus.OK); } + + @Operation(summary = "Add chat link to user") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = HttpStatuses.OK), + @ApiResponse(responseCode = "401", description = HttpStatuses.UNAUTHORIZED, content = @Content), + @ApiResponse(responseCode = "400", description = HttpStatuses.BAD_REQUEST, content = @Content), + @ApiResponse(responseCode = "403", description = HttpStatuses.FORBIDDEN, content = @Content) + }) + @PreAuthorize("@preAuthorizer.hasAuthority('SEE_CLIENTS_PAGE', authentication)") + @PatchMapping("/addChatLink") + public ResponseEntity addChatLink(@RequestBody @Valid ChatLinkDto chatLink) { + ordersAdminsPageService.addChatLinkToUser(chatLink); + return ResponseEntity.status(HttpStatus.OK).build(); + } } diff --git a/core/src/test/java/greencity/controller/AdminUbsControllerTest.java b/core/src/test/java/greencity/controller/AdminUbsControllerTest.java index 18e57437f4..d2012ca111 100644 --- a/core/src/test/java/greencity/controller/AdminUbsControllerTest.java +++ b/core/src/test/java/greencity/controller/AdminUbsControllerTest.java @@ -8,6 +8,7 @@ import greencity.dto.order.ChangeOrderResponseDTO; import greencity.dto.order.RequestToChangeOrdersDataDto; import greencity.dto.table.ColumnWidthDto; +import greencity.dto.user.ChatLinkDto; import greencity.service.ubs.OrdersAdminsPageService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -25,6 +26,7 @@ import static greencity.ModelUtils.getUuid; import static org.mockito.Mockito.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; @@ -154,4 +156,15 @@ void getAllLocations() throws Exception { .principal(principal)) .andExpect(status().isOk()); } + + @Test + void addChatLinkTest() throws Exception { + ObjectMapper objectMapper = new ObjectMapper(); + mockMvc.perform(patch(management + "/addChatLink") + .principal(principal) + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper + .writeValueAsString(new ChatLinkDto(1L, "https://my.binotel.ua/f/chat/#/visitor/21269249.12893974")))) + .andExpect(status().isOk()); + } } diff --git a/dao/src/main/java/greencity/entity/user/User.java b/dao/src/main/java/greencity/entity/user/User.java index cf687d63fc..a825e588bd 100644 --- a/dao/src/main/java/greencity/entity/user/User.java +++ b/dao/src/main/java/greencity/entity/user/User.java @@ -82,4 +82,7 @@ public class User { @Column(name = "date_of_registration") private LocalDate dateOfRegistration; + + @Column(name = "chat_link") + private String chatLink; } diff --git a/dao/src/main/resources/db/changelog/db.changelog-master.xml b/dao/src/main/resources/db/changelog/db.changelog-master.xml index d7f83a4059..017011340e 100644 --- a/dao/src/main/resources/db/changelog/db.changelog-master.xml +++ b/dao/src/main/resources/db/changelog/db.changelog-master.xml @@ -261,4 +261,5 @@ + \ No newline at end of file diff --git a/dao/src/main/resources/db/changelog/logs/2024-12-18-add-new-column-to-user-table-Kizerov.xml b/dao/src/main/resources/db/changelog/logs/2024-12-18-add-new-column-to-user-table-Kizerov.xml new file mode 100644 index 0000000000..68d731a05f --- /dev/null +++ b/dao/src/main/resources/db/changelog/logs/2024-12-18-add-new-column-to-user-table-Kizerov.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/service-api/src/main/java/greencity/dto/order/UserWithSomeOrderDetailDto.java b/service-api/src/main/java/greencity/dto/order/UserWithSomeOrderDetailDto.java index 6169dc36be..186ad6a38a 100644 --- a/service-api/src/main/java/greencity/dto/order/UserWithSomeOrderDetailDto.java +++ b/service-api/src/main/java/greencity/dto/order/UserWithSomeOrderDetailDto.java @@ -39,4 +39,5 @@ public class UserWithSomeOrderDetailDto { private int violation; @JsonProperty("currentPoints") private String userBonuses; + private String chatLink; } diff --git a/service-api/src/main/java/greencity/dto/user/ChatLinkDto.java b/service-api/src/main/java/greencity/dto/user/ChatLinkDto.java new file mode 100644 index 0000000000..c6c98a9427 --- /dev/null +++ b/service-api/src/main/java/greencity/dto/user/ChatLinkDto.java @@ -0,0 +1,12 @@ +package greencity.dto.user; + +import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Pattern; +import org.hibernate.validator.constraints.Length; + +public record ChatLinkDto( + Long userId, + @JsonProperty("link") @Length(max = 255) @NotBlank @Pattern(regexp = "^https://my\\.binotel\\.ua.*", + message = "Link must start with 'https://my.binotel.ua'") String link) { +} diff --git a/service-api/src/main/java/greencity/service/ubs/OrdersAdminsPageService.java b/service-api/src/main/java/greencity/service/ubs/OrdersAdminsPageService.java index 7aa9f05a7a..97b92a6375 100644 --- a/service-api/src/main/java/greencity/service/ubs/OrdersAdminsPageService.java +++ b/service-api/src/main/java/greencity/service/ubs/OrdersAdminsPageService.java @@ -6,6 +6,7 @@ import greencity.dto.order.RequestToChangeOrdersDataDto; import greencity.dto.table.ColumnWidthDto; import greencity.dto.table.TableParamsDto; +import greencity.dto.user.ChatLinkDto; import greencity.entity.user.employee.Employee; import java.util.List; @@ -136,4 +137,12 @@ ChangeOrderResponseDTO chooseOrdersDataSwitcher(String email, * regions, cities, and districts in the system */ List getAllLocationsInfo(); + + /** + * Adds a chat link to a user. + * + * @param chatLinkDto the chat link to add + * @author Kizerov Dmytro + */ + void addChatLinkToUser(ChatLinkDto chatLinkDto); } diff --git a/service/src/main/java/greencity/service/ubs/OrdersAdminsPageServiceImpl.java b/service/src/main/java/greencity/service/ubs/OrdersAdminsPageServiceImpl.java index 4fce8da5bc..2c65ff7e27 100644 --- a/service/src/main/java/greencity/service/ubs/OrdersAdminsPageServiceImpl.java +++ b/service/src/main/java/greencity/service/ubs/OrdersAdminsPageServiceImpl.java @@ -14,6 +14,7 @@ import greencity.dto.table.ColumnDTO; import greencity.dto.table.ColumnWidthDto; import greencity.dto.table.TableParamsDto; +import greencity.dto.user.ChatLinkDto; import greencity.entity.table.TableColumnWidthForEmployee; import greencity.entity.user.Region; import greencity.entity.user.employee.Employee; @@ -89,6 +90,7 @@ import static greencity.constant.ErrorMessage.ORDER_STATUS_NOT_FOUND; import static greencity.constant.ErrorMessage.ORDER_WITH_CURRENT_ID_DOES_NOT_EXIST; import static greencity.constant.ErrorMessage.POSITION_NOT_FOUND_BY_ID; +import static greencity.constant.ErrorMessage.USER_WITH_CURRENT_ID_DOES_NOT_EXIST; import static greencity.constant.ErrorMessage.USER_WITH_CURRENT_UUID_DOES_NOT_EXIST; import static greencity.constant.OrderHistory.UBS_ADMIN; import static java.util.Objects.isNull; @@ -447,6 +449,17 @@ public List getAllLocationsInfo() { .toList(); } + /** + * {@inheritDoc} + */ + @Override + public void addChatLinkToUser(ChatLinkDto chatLinkDto) { + User user = userRepository.findById(chatLinkDto.userId()) + .orElseThrow(() -> new NotFoundException(USER_WITH_CURRENT_ID_DOES_NOT_EXIST)); + user.setChatLink(chatLinkDto.link()); + userRepository.save(user); + } + private RegionInfoDto toRegionInfoDto(Region region) { return RegionInfoDto.builder() .id(region.getId()) diff --git a/service/src/main/java/greencity/service/ubs/ValuesForUserTableServiceImpl.java b/service/src/main/java/greencity/service/ubs/ValuesForUserTableServiceImpl.java index 976f5f1c9e..b8116ac976 100644 --- a/service/src/main/java/greencity/service/ubs/ValuesForUserTableServiceImpl.java +++ b/service/src/main/java/greencity/service/ubs/ValuesForUserTableServiceImpl.java @@ -10,6 +10,7 @@ import greencity.repository.EmployeeRepository; import greencity.repository.UserRepository; import greencity.repository.UserTableRepo; +import java.util.Objects; import lombok.AllArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.stereotype.Service; @@ -89,6 +90,9 @@ private UserWithSomeOrderDetailDto mapToDto(User u) { .setLastOrderDate(optional .get().getOrderDate().toLocalDate().format(DateTimeFormatter.ofPattern(DATE_FORMAT))); } + if (Objects.nonNull(u.getChatLink())) { + allFieldsFromTableDto.setChatLink(u.getChatLink()); + } return allFieldsFromTableDto; } } diff --git a/service/src/test/java/greencity/service/ubs/OrdersAdminsPageServiceImplTest.java b/service/src/test/java/greencity/service/ubs/OrdersAdminsPageServiceImplTest.java index 1a55ab59a8..307fb63384 100644 --- a/service/src/test/java/greencity/service/ubs/OrdersAdminsPageServiceImplTest.java +++ b/service/src/test/java/greencity/service/ubs/OrdersAdminsPageServiceImplTest.java @@ -9,6 +9,7 @@ import greencity.dto.order.ChangeOrderResponseDTO; import greencity.dto.order.RequestToChangeOrdersDataDto; import greencity.dto.table.ColumnWidthDto; +import greencity.dto.user.ChatLinkDto; import greencity.entity.order.Event; import greencity.entity.table.TableColumnWidthForEmployee; import greencity.entity.user.ubs.OrderAddress; @@ -1355,4 +1356,15 @@ void addNewCommentWithNotExistingOrderTest() { verify(employeeRepository).findByEmail(anyString()); verify(orderRepository).findById(anyLong()); } + + @Test + void addChatLinkToUserTest() { + when(userRepository.findById(anyLong())).thenReturn(Optional.ofNullable(ModelUtils.getUser())); + + ordersAdminsPageService + .addChatLinkToUser(new ChatLinkDto(1L, "https://my.binotel.ua/f/chat/#/visitor/21269249.12893974")); + + verify(userRepository).findById(anyLong()); + verify(userRepository).save(any(User.class)); + } } From 5aa4f2b9e6a9b5cbbb17c210edf0ffeadb653d3f Mon Sep 17 00:00:00 2001 From: Kizerov Dmytro Date: Wed, 18 Dec 2024 14:41:24 +0200 Subject: [PATCH 2/4] added ChatLinkDto to sonar exclusions --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 49e6de684a..743e14ace7 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -45,7 +45,7 @@ jobs: SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} run: | git fetch --unshallow - mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install sonar:sonar -Dsonar.projectKey=ita-social-projects-green-ubs -Dsonar.organization=ita-social-projects -Dsonar.host.url=https://sonarcloud.io -Dsonar.binaries=target/classes -Dsonar.dynamicAnalysis=reuseReports -Dsonar.exclusions=**/EncryptionUtil.java -Dsonar.coverage.exclusions=**/configuration/*,**/entity/*,**/exceptions/*,**/exceptions/**,**/enums/NotificationType.java,**/BigOrderTableRepository.java + mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install sonar:sonar -Dsonar.projectKey=ita-social-projects-green-ubs -Dsonar.organization=ita-social-projects -Dsonar.host.url=https://sonarcloud.io -Dsonar.binaries=target/classes -Dsonar.dynamicAnalysis=reuseReports -Dsonar.exclusions=**/EncryptionUtil.java -Dsonar.coverage.exclusions=**/configuration/*,**/entity/*,**/exceptions/*,**/exceptions/**,**/enums/NotificationType.java,**/BigOrderTableRepository.java,**/ChatLinkDto.java - name: Test Reporter uses: dorny/test-reporter@v1.5.0 From d9b6f4cb7d4d99ed36a39c7c7cbe8bd6bef9f4c1 Mon Sep 17 00:00:00 2001 From: Kizerov Dmytro Date: Fri, 20 Dec 2024 10:43:31 +0200 Subject: [PATCH 3/4] added tests --- .../ValuesForUserTableServiceImplTest.java | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 service/src/test/java/greencity/service/ubs/ValuesForUserTableServiceImplTest.java diff --git a/service/src/test/java/greencity/service/ubs/ValuesForUserTableServiceImplTest.java b/service/src/test/java/greencity/service/ubs/ValuesForUserTableServiceImplTest.java new file mode 100644 index 0000000000..2b32e36f69 --- /dev/null +++ b/service/src/test/java/greencity/service/ubs/ValuesForUserTableServiceImplTest.java @@ -0,0 +1,135 @@ +package greencity.service.ubs; + +import greencity.dto.order.UserWithSomeOrderDetailDto; +import greencity.dto.pageble.PageableDto; +import greencity.entity.order.Order; +import greencity.entity.user.User; +import greencity.entity.user.employee.Employee; +import greencity.enums.SortingOrder; +import greencity.filters.CustomerPage; +import greencity.filters.UserFilterCriteria; +import greencity.repository.EmployeeRepository; +import greencity.repository.UserRepository; +import greencity.repository.UserTableRepo; +import jakarta.persistence.EntityNotFoundException; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.List; +import java.util.Optional; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +@ExtendWith(MockitoExtension.class) +class ValuesForUserTableServiceImplTest { + @Mock + private UserRepository userRepository; + + @Mock + private UserTableRepo userTableRepo; + + @Mock + private EmployeeRepository employeeRepository; + + @InjectMocks + private ValuesForUserTableServiceImpl service; + + private static final String TEST_EMAIL = "test@example.com"; + + @Test + void getAllFieldsShouldReturnPageableDtoWhenDataIsValid() { + Long employeeId = 1L; + List tariffsInfoIds = List.of(2L, 3L); + List userIds = List.of(4L, 5L, 4L, 5L); + User user1 = createTestUser(4L, "John", "Doe", "john.doe@example.com", "+380123456789"); + User user2 = createTestUser(5L, "Jane", "Smith", "jane.smith@example.com", "+380987654321"); + Page mockPage = new PageImpl<>(List.of(user1, user2), PageRequest.of(0, 2), 2); + + CustomerPage page = new CustomerPage(0, 2); + String columnName = "name"; + SortingOrder sortingOrder = SortingOrder.ASC; + UserFilterCriteria filterCriteria = new UserFilterCriteria(); + + Mockito.when(employeeRepository.findByEmail(TEST_EMAIL)) + .thenReturn(Optional.of(createTestEmployee(employeeId))); + Mockito.when(employeeRepository.findTariffsInfoForEmployee(employeeId)) + .thenReturn(tariffsInfoIds); + Mockito.when(userRepository.getAllUsersByTariffsInfoId(Mockito.anyLong())) + .thenReturn(List.of(4L, 5L)); + Mockito.when(userTableRepo.findAll( + Mockito.eq(filterCriteria), + Mockito.eq(columnName), + Mockito.eq(sortingOrder), + Mockito.eq(page), + Mockito.anyList())) + .thenReturn(mockPage); + + PageableDto result = + service.getAllFields(page, columnName, sortingOrder, filterCriteria, TEST_EMAIL); + + assertThat(result).isNotNull(); + assertThat(result.getPage()).hasSize(2); + assertThat(result.getPage()) + .extracting("clientName") + .containsExactly("John Doe", "Jane Smith"); + Mockito.verify(employeeRepository).findByEmail(TEST_EMAIL); + Mockito.verify(employeeRepository).findTariffsInfoForEmployee(employeeId); + Mockito.verify(userRepository, Mockito.times(tariffsInfoIds.size())) + .getAllUsersByTariffsInfoId(Mockito.anyLong()); + Mockito.verify(userTableRepo).findAll(filterCriteria, columnName, sortingOrder, page, userIds); + } + + @Test + void getAllFieldsShouldThrowExceptionWhenEmployeeNotFound() { + Mockito.when(employeeRepository.findByEmail(TEST_EMAIL)) + .thenReturn(Optional.empty()); + + CustomerPage page = new CustomerPage(0, 2); + String columnName = "name"; + SortingOrder sortingOrder = SortingOrder.ASC; + UserFilterCriteria filterCriteria = new UserFilterCriteria(); + + assertThatThrownBy(() -> service.getAllFields(page, columnName, sortingOrder, filterCriteria, TEST_EMAIL)) + .isInstanceOf(EntityNotFoundException.class) + .hasMessage("Employee with current id doesn't exist: "); + + Mockito.verify(employeeRepository).findByEmail(TEST_EMAIL); + Mockito.verifyNoInteractions(userRepository, userTableRepo); + } + + private User createTestUser(Long id, String firstName, String lastName, String email, String phone) { + User user = new User(); + user.setId(id); + user.setRecipientName(firstName); + user.setRecipientSurname(lastName); + user.setRecipientEmail(email); + user.setRecipientPhone(phone); + user.setDateOfRegistration(LocalDate.now()); + user.setOrders(List.of(createTestOrder())); + user.setCurrentPoints(100); + user.setViolations(1); + return user; + } + + private Order createTestOrder() { + Order order = new Order(); + order.setOrderDate(LocalDateTime.now().minusDays(1)); + return order; + } + + private Employee createTestEmployee(Long id) { + Employee employee = new Employee(); + employee.setId(id); + employee.setEmail(TEST_EMAIL); + return employee; + } +} From 5a0b48b159ca5fb38b554ac88590ebe337135c20 Mon Sep 17 00:00:00 2001 From: Kizerov Dmytro Date: Fri, 20 Dec 2024 11:09:02 +0200 Subject: [PATCH 4/4] refactored tests --- .../src/test/java/greencity/ModelUtils.java | 27 ++++++++++++ .../ValuesForUserTableServiceImplTest.java | 42 +++---------------- 2 files changed, 33 insertions(+), 36 deletions(-) diff --git a/service/src/test/java/greencity/ModelUtils.java b/service/src/test/java/greencity/ModelUtils.java index 7b77b197f4..684cf12f63 100644 --- a/service/src/test/java/greencity/ModelUtils.java +++ b/service/src/test/java/greencity/ModelUtils.java @@ -5675,4 +5675,31 @@ public static RequestToChangeOrdersDataDto getChangeRequest(String columnName) { .newValue("Test comment") .build(); } + + public static User createTestUser(Long id, String firstName, String lastName, String email, String phone) { + User user = new User(); + user.setId(id); + user.setRecipientName(firstName); + user.setRecipientSurname(lastName); + user.setRecipientEmail(email); + user.setRecipientPhone(phone); + user.setDateOfRegistration(LocalDate.now()); + user.setOrders(List.of(createTestOrder())); + user.setCurrentPoints(100); + user.setViolations(1); + return user; + } + + public static Order createTestOrder() { + Order order = new Order(); + order.setOrderDate(LocalDateTime.now().minusDays(1)); + return order; + } + + public static Employee createTestEmployee(Long id) { + Employee employee = new Employee(); + employee.setId(id); + employee.setEmail("test@example.com"); + return employee; + } } diff --git a/service/src/test/java/greencity/service/ubs/ValuesForUserTableServiceImplTest.java b/service/src/test/java/greencity/service/ubs/ValuesForUserTableServiceImplTest.java index 2b32e36f69..b09e157003 100644 --- a/service/src/test/java/greencity/service/ubs/ValuesForUserTableServiceImplTest.java +++ b/service/src/test/java/greencity/service/ubs/ValuesForUserTableServiceImplTest.java @@ -1,10 +1,9 @@ package greencity.service.ubs; +import greencity.ModelUtils; import greencity.dto.order.UserWithSomeOrderDetailDto; import greencity.dto.pageble.PageableDto; -import greencity.entity.order.Order; import greencity.entity.user.User; -import greencity.entity.user.employee.Employee; import greencity.enums.SortingOrder; import greencity.filters.CustomerPage; import greencity.filters.UserFilterCriteria; @@ -12,8 +11,6 @@ import greencity.repository.UserRepository; import greencity.repository.UserTableRepo; import jakarta.persistence.EntityNotFoundException; -import java.time.LocalDate; -import java.time.LocalDateTime; import java.util.List; import java.util.Optional; import org.junit.jupiter.api.Test; @@ -46,12 +43,12 @@ class ValuesForUserTableServiceImplTest { private static final String TEST_EMAIL = "test@example.com"; @Test - void getAllFieldsShouldReturnPageableDtoWhenDataIsValid() { + void getAllFieldsShouldReturnPageableDtoWhenDataIsValidTest() { Long employeeId = 1L; List tariffsInfoIds = List.of(2L, 3L); List userIds = List.of(4L, 5L, 4L, 5L); - User user1 = createTestUser(4L, "John", "Doe", "john.doe@example.com", "+380123456789"); - User user2 = createTestUser(5L, "Jane", "Smith", "jane.smith@example.com", "+380987654321"); + User user1 = ModelUtils.createTestUser(4L, "John", "Doe", "john.doe@example.com", "+380123456789"); + User user2 = ModelUtils.createTestUser(5L, "Jane", "Smith", "jane.smith@example.com", "+380987654321"); Page mockPage = new PageImpl<>(List.of(user1, user2), PageRequest.of(0, 2), 2); CustomerPage page = new CustomerPage(0, 2); @@ -60,7 +57,7 @@ void getAllFieldsShouldReturnPageableDtoWhenDataIsValid() { UserFilterCriteria filterCriteria = new UserFilterCriteria(); Mockito.when(employeeRepository.findByEmail(TEST_EMAIL)) - .thenReturn(Optional.of(createTestEmployee(employeeId))); + .thenReturn(Optional.of(ModelUtils.createTestEmployee(employeeId))); Mockito.when(employeeRepository.findTariffsInfoForEmployee(employeeId)) .thenReturn(tariffsInfoIds); Mockito.when(userRepository.getAllUsersByTariffsInfoId(Mockito.anyLong())) @@ -89,7 +86,7 @@ void getAllFieldsShouldReturnPageableDtoWhenDataIsValid() { } @Test - void getAllFieldsShouldThrowExceptionWhenEmployeeNotFound() { + void getAllFieldsShouldThrowExceptionWhenEmployeeNotFoundTest() { Mockito.when(employeeRepository.findByEmail(TEST_EMAIL)) .thenReturn(Optional.empty()); @@ -105,31 +102,4 @@ void getAllFieldsShouldThrowExceptionWhenEmployeeNotFound() { Mockito.verify(employeeRepository).findByEmail(TEST_EMAIL); Mockito.verifyNoInteractions(userRepository, userTableRepo); } - - private User createTestUser(Long id, String firstName, String lastName, String email, String phone) { - User user = new User(); - user.setId(id); - user.setRecipientName(firstName); - user.setRecipientSurname(lastName); - user.setRecipientEmail(email); - user.setRecipientPhone(phone); - user.setDateOfRegistration(LocalDate.now()); - user.setOrders(List.of(createTestOrder())); - user.setCurrentPoints(100); - user.setViolations(1); - return user; - } - - private Order createTestOrder() { - Order order = new Order(); - order.setOrderDate(LocalDateTime.now().minusDays(1)); - return order; - } - - private Employee createTestEmployee(Long id) { - Employee employee = new Employee(); - employee.setId(id); - employee.setEmail(TEST_EMAIL); - return employee; - } }