diff --git a/src/main/java/com/gepardec/mega/service/impl/CommentServiceImpl.java b/src/main/java/com/gepardec/mega/service/impl/CommentServiceImpl.java index 75922733e..dddfc7618 100644 --- a/src/main/java/com/gepardec/mega/service/impl/CommentServiceImpl.java +++ b/src/main/java/com/gepardec/mega/service/impl/CommentServiceImpl.java @@ -23,7 +23,6 @@ import java.util.Locale; import java.util.Map; import java.util.Objects; -import java.util.stream.Collectors; @ApplicationScoped public class CommentServiceImpl implements CommentService { diff --git a/src/main/java/com/gepardec/mega/service/mapper/EmployeeMapper.java b/src/main/java/com/gepardec/mega/service/mapper/EmployeeMapper.java index 5d7362879..462906b20 100644 --- a/src/main/java/com/gepardec/mega/service/mapper/EmployeeMapper.java +++ b/src/main/java/com/gepardec/mega/service/mapper/EmployeeMapper.java @@ -21,7 +21,6 @@ import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.stream.Collectors; @ApplicationScoped public class EmployeeMapper { diff --git a/src/test/java/com/gepardec/mega/service/impl/absenceservice/AbsenceServiceImplTest.java b/src/test/java/com/gepardec/mega/service/impl/absenceservice/AbsenceServiceImplTest.java index f66e66a3c..409b1cfa8 100644 --- a/src/test/java/com/gepardec/mega/service/impl/absenceservice/AbsenceServiceImplTest.java +++ b/src/test/java/com/gepardec/mega/service/impl/absenceservice/AbsenceServiceImplTest.java @@ -68,6 +68,28 @@ void testGetNumberOfDaysAbsent_whenThreeDaysAbsent_thenReturnThree() { } } + @Test + void testGetNumberOfDaysAbsent_whenTwoDaysAreHoliday_thenReturnNumberOfDaysWithoutTheseDay() { + try (MockedStatic officeCalendarUtilMock = Mockito.mockStatic(OfficeCalendarUtil.class)) { + + officeCalendarUtilMock.when(() -> OfficeCalendarUtil.isWorkingDay(any(LocalDate.class))) + .thenReturn(true); + officeCalendarUtilMock.when(() -> OfficeCalendarUtil.getHolidaysForMonth(any(YearMonth.class))) + .thenReturn(Stream.of( + LocalDate.of(2024,5,6), + LocalDate.of(2024,5,10) + )); + + + int actual = absenceService.getNumberOfDaysAbsent(List.of( + createAbsenceTime(AbsenceType.VACATION_DAYS, LocalDate.of(2024,5,6), LocalDate.of(2024,5,6)), + createAbsenceTime(AbsenceType.VACATION_DAYS, LocalDate.of(2024,5,9), LocalDate.of(2024,5,12)) + ), LocalDate.of(2024, 4, 1)); + + assertThat(actual).isEqualTo(3); + } + } + private List createAbsencesForDaysAbsent(){ LocalDate fromDate = LocalDate.now(); LocalDate toDate = LocalDate.now(); diff --git a/src/test/java/com/gepardec/mega/service/impl/comment/CommentServiceImplTest.java b/src/test/java/com/gepardec/mega/service/impl/comment/CommentServiceImplTest.java index 8bf06deb7..8b95b90a1 100644 --- a/src/test/java/com/gepardec/mega/service/impl/comment/CommentServiceImplTest.java +++ b/src/test/java/com/gepardec/mega/service/impl/comment/CommentServiceImplTest.java @@ -236,7 +236,7 @@ void updateComment_whenValid_thenReturnUpdatedComment() { } @Test - void deleteComment_whenFound_thenDeleteAndSendMail() { + void deleteComment_whenSuccess_thenDeleteAndSendMail() { Long commentId = 1L; com.gepardec.mega.db.entity.employee.Comment commentEntity = createComment(commentId, EmployeeState.IN_PROGRESS); when(commentRepository.findById(commentId)).thenReturn(commentEntity); @@ -258,6 +258,20 @@ void deleteComment_whenFound_thenDeleteAndSendMail() { verify(commentRepository, times(1)).deleteComment(commentId); } + @Test + void deleteComment_whenNoSuccess_thenReturnFalse() { + Long commentId = 1L; + com.gepardec.mega.db.entity.employee.Comment commentEntity = createComment(commentId, EmployeeState.IN_PROGRESS); + when(commentRepository.findById(commentId)).thenReturn(commentEntity); + when(commentRepository.deleteComment(commentId)).thenReturn(false); + + boolean deleted = commentService.delete(commentId); + + assertThat(deleted).isFalse(); + verify(commentRepository, times(1)).findById(commentId); + verify(commentRepository, times(1)).deleteComment(commentId); + } + private com.gepardec.mega.db.entity.employee.Comment createComment(Long id, EmployeeState employeeState) { com.gepardec.mega.db.entity.employee.Comment comment = new com.gepardec.mega.db.entity.employee.Comment(); comment.setId(id); diff --git a/src/test/java/com/gepardec/mega/service/impl/datehelper/DateHelperServiceImplTest.java b/src/test/java/com/gepardec/mega/service/impl/datehelper/DateHelperServiceImplTest.java index 32e21f3e1..2abc804f2 100644 --- a/src/test/java/com/gepardec/mega/service/impl/datehelper/DateHelperServiceImplTest.java +++ b/src/test/java/com/gepardec/mega/service/impl/datehelper/DateHelperServiceImplTest.java @@ -1,12 +1,26 @@ package com.gepardec.mega.service.impl.datehelper; +import com.gepardec.mega.domain.model.Employee; +import com.gepardec.mega.domain.utils.DateUtils; import com.gepardec.mega.service.api.DateHelperService; +import com.gepardec.mega.service.api.MonthlyReportService; import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.mockito.InjectMock; import jakarta.inject.Inject; +import org.apache.commons.lang3.tuple.Pair; import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; + import java.time.LocalDate; +import java.time.YearMonth; + import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.when; @QuarkusTest class DateHelperServiceImplTest { @@ -14,6 +28,10 @@ class DateHelperServiceImplTest { @Inject DateHelperService dateHelperService; + + @InjectMock + MonthlyReportService monthlyReportService; + @Test void testGetNumberOfFridaysInMonth_whenMonthIsNovember2024_thenReturnFour(){ int actual = dateHelperService.getNumberOfFridaysInMonth(LocalDate.of(2024, 11,1)); @@ -31,4 +49,124 @@ void testGetNumberOfWorkingDaysForMonthWithoutHolidays_whenMonthIsApril2024_then int actual = dateHelperService.getNumberOfWorkingDaysForMonthWithoutHolidays(LocalDate.of(2024, 4, 1)); assertThat(actual).isEqualTo(21); } + + @Test + void getCorrectDateForRequest_whenYearMonthProvided_thenReturnCorrectDate(){ + LocalDate mockCurrentDate = LocalDate.of(2024,5,15); + LocalDate previousMonth = mockCurrentDate.minusMonths(1); + LocalDate firstOfPreviousMonth = previousMonth.withDayOfMonth(1); + LocalDate mockMidOfMonth = mockCurrentDate.withDayOfMonth(14); + + LocalDate firstOfCurrentMonth = LocalDate.of(2024, 5, 1); + LocalDate lastOfCurrentMonth = LocalDate.of(2024, 5, 31); + YearMonth yearMonthMock = YearMonth.of(2024, 5); + + String expectedFrom = "2024-05-01"; + String expectedTo = "2024-05-31"; + + + try (MockedStatic mockedStatic = mockStatic(LocalDate.class)) { + mockedStatic.when(LocalDate::now).thenReturn(mockCurrentDate); + mockedStatic.when(() -> LocalDate.now().minusMonths(1)).thenReturn(previousMonth); + mockedStatic.when(() -> LocalDate.now().withMonth(LocalDate.now().getMonth().minus(1).getValue()).withDayOfMonth(1)).thenReturn(firstOfPreviousMonth); + mockedStatic.when(() -> LocalDate.now().withDayOfMonth(14)).thenReturn(mockMidOfMonth); + + try(MockedStatic dateUtils = mockStatic(DateUtils.class)) { + try (MockedStatic yearMonth = mockStatic(YearMonth.class)) { + + yearMonth.when(() -> YearMonth.of(2024, 5)).thenReturn(yearMonthMock); + yearMonth.when(() -> yearMonthMock.atDay(1)).thenReturn(firstOfCurrentMonth); + } + + dateUtils.when(() -> DateUtils.getLastDayOfCurrentMonth(anyString())) + .thenReturn(lastOfCurrentMonth); + + dateUtils.when(() -> DateUtils.formatDate(firstOfCurrentMonth)) + .thenReturn(expectedFrom); + + dateUtils.when(() -> DateUtils.formatDate(lastOfCurrentMonth)) + .thenReturn(expectedTo); + } + } + + Pair actual = dateHelperService.getCorrectDateForRequest(Employee.builder().userId("007-jbond").build(), YearMonth.of(2024,5)); + + assertThat(actual).isNotNull(); + assertThat(actual.getLeft()).isEqualTo(expectedFrom); + assertThat(actual.getRight()).isEqualTo(expectedTo); + } + + @Test + void getCorrectDateForRequest_whenYearMonthIsNotProvidedAndNowIsAfterMidOfMonthAndConfirmed_thenReturnCorrectDate(){ + LocalDate mockCurrentDate = LocalDate.of(2024,5,15); + LocalDate previousMonth = mockCurrentDate.minusMonths(1); + LocalDate firstOfPreviousMonth = previousMonth.withDayOfMonth(1); + LocalDate mockMidOfMonth = mockCurrentDate.withDayOfMonth(14); + + String expectedFrom = "2024-05-01"; + String expectedTo = "2024-05-31"; + + try(MockedStatic mockedStatic = mockStatic(LocalDate.class, Mockito.CALLS_REAL_METHODS)) { + mockedStatic.when(LocalDate::now).thenReturn(mockCurrentDate).thenReturn(mockCurrentDate); + mockedStatic.when(() -> LocalDate.now().minusMonths(1)).thenReturn(previousMonth); + mockedStatic.when(() -> LocalDate.now().withMonth(LocalDate.now().getMonth().minus(1).getValue()).withDayOfMonth(1)).thenReturn(firstOfPreviousMonth); + mockedStatic.when(() -> LocalDate.now().withDayOfMonth(14)).thenReturn(mockMidOfMonth); + + try(MockedStatic dateUtils = mockStatic(DateUtils.class)) { + dateUtils.when(() -> DateUtils.getFirstDayOfCurrentMonth(any(LocalDate.class))) + .thenReturn(expectedFrom); + + dateUtils.when(() -> DateUtils.getLastDayOfCurrentMonth(any(LocalDate.class))) + .thenReturn(expectedTo); + + when(monthlyReportService.isMonthConfirmedFromEmployee(any(Employee.class), any(LocalDate.class))) + .thenReturn(true); + + Pair actual = dateHelperService.getCorrectDateForRequest(Employee.builder().userId("007-jbond").build(), null); + assertThat(actual.getLeft()).isEqualTo(expectedFrom); + } + } + } + + @Test + void getCorrectDateForRequest_whenYearMonthIsNotProvidedAndNowIsAfterMidOfMonthAndNotConfirmed_thenReturnCorrectDate(){ + LocalDate mockCurrentDate = LocalDate.of(2024,5,15); + LocalDate previousMonth = mockCurrentDate.minusMonths(1); + LocalDate firstOfPreviousMonth = previousMonth.withDayOfMonth(1); + LocalDate mockMidOfMonth = mockCurrentDate.withDayOfMonth(14); + + + LocalDate lastOfPreviousMonth = LocalDate.of(2024, 4, 30); + + String expectedFrom = "2024-04-01"; + String expectedTo = "2024-04-30"; + + try(MockedStatic mockedStatic = mockStatic(LocalDate.class, Mockito.CALLS_REAL_METHODS)) { + mockedStatic.when(LocalDate::now).thenReturn(mockCurrentDate).thenReturn(mockCurrentDate); + mockedStatic.when(() -> LocalDate.now().minusMonths(1)).thenReturn(previousMonth); + mockedStatic.when(() -> LocalDate.now().withMonth(LocalDate.now().getMonth().minus(1).getValue()).withDayOfMonth(1)).thenReturn(firstOfPreviousMonth); + mockedStatic.when(() -> LocalDate.now().withDayOfMonth(14)).thenReturn(mockMidOfMonth); + + try(MockedStatic dateUtils = mockStatic(DateUtils.class)) { + + dateUtils.when(() -> DateUtils.getLastDayOfCurrentMonth(any(LocalDate.class))) + .thenReturn(expectedTo); + + dateUtils.when(() -> DateUtils.formatDate(firstOfPreviousMonth)) + .thenReturn(expectedFrom); + + dateUtils.when(() -> DateUtils.formatDate(lastOfPreviousMonth)) + .thenReturn(expectedTo); + + when(monthlyReportService.isMonthConfirmedFromEmployee(any(Employee.class), any(LocalDate.class))) + .thenReturn(false); + + Pair actual = dateHelperService.getCorrectDateForRequest(Employee.builder().userId("007-jbond").build(), null); + assertThat(actual.getLeft()).isEqualTo(expectedFrom); + } + } + } } + + + diff --git a/src/test/java/com/gepardec/mega/service/mapper/EnterpriseEntryMapperTest.java b/src/test/java/com/gepardec/mega/service/mapper/EnterpriseEntryMapperTest.java new file mode 100644 index 000000000..3378e07ab --- /dev/null +++ b/src/test/java/com/gepardec/mega/service/mapper/EnterpriseEntryMapperTest.java @@ -0,0 +1,41 @@ +package com.gepardec.mega.service.mapper; + +import com.gepardec.mega.db.entity.common.State; +import com.gepardec.mega.db.entity.enterprise.EnterpriseEntry; +import com.gepardec.mega.rest.model.EnterpriseEntryDto; +import io.quarkus.test.junit.QuarkusTest; +import jakarta.inject.Inject; +import org.junit.jupiter.api.Test; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; + +@QuarkusTest +public class EnterpriseEntryMapperTest { + @Inject + EnterpriseEntryMapper mapper; + + @Test + void map_whenEntryIsEmpty_thenReturnNull() { + EnterpriseEntryDto actual = mapper.map(Optional.empty()); + assertThat(actual).isNull(); + } + + @Test + void map_whenEntryIsNotEmpty_thenReturnDto() { + EnterpriseEntry entry = new EnterpriseEntry(); + entry.setId(1L); + entry.setZepTimesReleased(State.OPEN); + entry.setChargeabilityExternalEmployeesRecorded(State.OPEN); + entry.setPayrollAccountingSent(State.DONE); + entry.setDate(LocalDate.of(2024,5,3)); + entry.setCreationDate(LocalDateTime.of(2024,4,20, 13, 20)); + + EnterpriseEntryDto actual = mapper.map(Optional.of(entry)); + assertThat(actual).isNotNull(); + assertThat(actual.getDate()).isEqualTo(entry.getDate()); + } +}