Skip to content

Commit

Permalink
[Gepardec/mega#753] add tests for comment service, absence service, d…
Browse files Browse the repository at this point in the history
…ate helper service and enterprise entry mapper
  • Loading branch information
gattrCh committed Jun 24, 2024
1 parent 5c49c04 commit d6cff32
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ void testGetNumberOfDaysAbsent_whenThreeDaysAbsent_thenReturnThree() {
}
}

@Test
void testGetNumberOfDaysAbsent_whenTwoDaysAreHoliday_thenReturnNumberOfDaysWithoutTheseDay() {
try (MockedStatic<OfficeCalendarUtil> 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<AbsenceTime> createAbsencesForDaysAbsent(){
LocalDate fromDate = LocalDate.now();
LocalDate toDate = LocalDate.now();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
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 {

@Inject
DateHelperService dateHelperService;


@InjectMock
MonthlyReportService monthlyReportService;

@Test
void testGetNumberOfFridaysInMonth_whenMonthIsNovember2024_thenReturnFour(){
int actual = dateHelperService.getNumberOfFridaysInMonth(LocalDate.of(2024, 11,1));
Expand All @@ -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<LocalDate> 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> dateUtils = mockStatic(DateUtils.class)) {
try (MockedStatic<YearMonth> 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<String, String> 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<LocalDate> 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> 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<String, String> 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<LocalDate> 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> 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<String, String> actual = dateHelperService.getCorrectDateForRequest(Employee.builder().userId("007-jbond").build(), null);
assertThat(actual.getLeft()).isEqualTo(expectedFrom);
}
}
}
}



Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit d6cff32

Please sign in to comment.