Skip to content

Commit

Permalink
[Gepardec/mega#753] reformat code, remove test prefix and public modi…
Browse files Browse the repository at this point in the history
…fiers from test classes and methods and user AssertJ
  • Loading branch information
Ollitod committed Oct 4, 2024
1 parent 9d7ea36 commit 4ac78c3
Show file tree
Hide file tree
Showing 61 changed files with 603 additions and 675 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.gepardec.mega.db.entity.project.ProjectComment;
import com.gepardec.mega.db.repository.ProjectCommentRepository;
import com.gepardec.mega.db.repository.ProjectRepository;
import com.gepardec.mega.rest.mapper.ProjectCommentMapper;
import com.gepardec.mega.domain.utils.DateUtils;
import com.gepardec.mega.rest.mapper.ProjectCommentMapper;
import com.gepardec.mega.rest.model.ProjectCommentDto;
import com.gepardec.mega.service.api.ProjectCommentService;
import jakarta.enterprise.context.ApplicationScoped;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ public void syncEmployees() {
public List<EmployeeDto> syncUpdateEmployeesWithoutTimeBookingsAndAbsentWholeMonth() {
//to avoid having a look at external employees filter
List<Employee> activeAndInternalEmployees = employeeService.getAllActiveEmployees()
.stream()
.filter(e -> !e.getUserId().startsWith("e"))
.toList();
.stream()
.filter(e -> !e.getUserId().startsWith("e"))
.toList();
List<EmployeeDto> updatedEmployees = new ArrayList<>();
List<Employee> absentEmployees = new ArrayList<>();

Expand Down Expand Up @@ -129,7 +129,7 @@ public List<EmployeeDto> syncUpdateEmployeesWithoutTimeBookingsAndAbsentWholeMon
}

// only add employee who was absent the whole month
if(allAbsent){
if (allAbsent) {
absentEmployees.add(employee);
}
}
Expand All @@ -138,8 +138,8 @@ public List<EmployeeDto> syncUpdateEmployeesWithoutTimeBookingsAndAbsentWholeMon
absentEmployees.forEach(employee -> {
StepEntry entry = stepEntryService.findStepEntryForEmployeeAtStep(1L, employee.getEmail(), employee.getEmail(), DateUtils.formatDate(firstOfPreviousMonth));
// if IN_PROGRESS OR already DONE than do not update reason
if(entry.getState().equals(EmployeeState.OPEN)) {
stepEntryService.setOpenAndAssignedStepEntriesDone(employee, 1L, firstOfPreviousMonth, lastOfPreviousMonth);
if (entry.getState().equals(EmployeeState.OPEN)) {
stepEntryService.setOpenAndAssignedStepEntriesDone(employee, 1L, firstOfPreviousMonth, lastOfPreviousMonth);
stepEntryService.updateStepEntryReasonForStepWithStateDone(employee, 1L, firstOfPreviousMonth, lastOfPreviousMonth, "Aufgrund von Abwesenheiten wurde der Monat automatisch bestätigt.");
updatedEmployees.add(employeeMapper.mapToDto(zepService.getEmployee(employee.getUserId())));
}
Expand All @@ -150,10 +150,10 @@ public List<EmployeeDto> syncUpdateEmployeesWithoutTimeBookingsAndAbsentWholeMon
}

private boolean isAbsent(LocalDate day, List<AbsenceTime> absences) {
for(var absence : absences){
for (var absence : absences) {
LocalDate startDate = absence.fromDate();
LocalDate endDate = absence.toDate();
if(day.equals(startDate) ||
if (day.equals(startDate) ||
day.equals(endDate) ||
(day.isAfter(startDate) && day.isBefore(endDate))) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import com.gepardec.mega.application.exception.ForbiddenException;
import com.gepardec.mega.db.repository.UserRepository;
import com.gepardec.mega.domain.mapper.UserMapper;
import com.gepardec.mega.domain.model.Role;
import com.gepardec.mega.domain.model.User;
import com.gepardec.mega.service.api.UserService;
import com.gepardec.mega.domain.mapper.UserMapper;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
Expand Down
56 changes: 28 additions & 28 deletions src/main/java/com/gepardec/mega/zep/mapper/AbsenceTimeMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,36 @@
@ApplicationScoped
public class AbsenceTimeMapper {

private AbsenceTimeMapper() {
}

public static List<AbsenceTime> mapList(List<FehlzeitType> absenceTimes) {
if (absenceTimes == null) {
return null;
}

return absenceTimes.stream()
.map(AbsenceTimeMapper::map)
.filter(Objects::nonNull)
.sorted(Comparator.comparing(AbsenceTime::fromDate))
.toList();
private AbsenceTimeMapper() {
}

public static List<AbsenceTime> mapList(List<FehlzeitType> absenceTimes) {
if (absenceTimes == null) {
return null;
}

public static AbsenceTime map(FehlzeitType fehlzeitType) {
if (fehlzeitType == null) {
return null;
}

LocalDate startDate = MapperUtil.convertStringToDate(fehlzeitType.getStartdatum());
LocalDate endDate = MapperUtil.convertStringToDate(fehlzeitType.getEnddatum());

return AbsenceTime.builder()
.userId(fehlzeitType.getUserId())
.fromDate(startDate)
.toDate(endDate)
.reason(fehlzeitType.getFehlgrund())
.accepted(fehlzeitType.isGenehmigt())
.build();
return absenceTimes.stream()
.map(AbsenceTimeMapper::map)
.filter(Objects::nonNull)
.sorted(Comparator.comparing(AbsenceTime::fromDate))
.toList();
}

public static AbsenceTime map(FehlzeitType fehlzeitType) {
if (fehlzeitType == null) {
return null;
}

LocalDate startDate = MapperUtil.convertStringToDate(fehlzeitType.getStartdatum());
LocalDate endDate = MapperUtil.convertStringToDate(fehlzeitType.getEnddatum());

return AbsenceTime.builder()
.userId(fehlzeitType.getUserId())
.fromDate(startDate)
.toDate(endDate)
.reason(fehlzeitType.getFehlgrund())
.accepted(fehlzeitType.isGenehmigt())
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.gepardec.mega.domain.model.monthlyreport.Task;
import com.gepardec.mega.domain.model.monthlyreport.Vehicle;
import com.gepardec.mega.domain.model.monthlyreport.WorkingLocation;

import de.provantis.zep.ProjektzeitType;
import jakarta.enterprise.context.ApplicationScoped;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class AbsenceMapper implements Mapper<AbsenceTime, ZepAbsence> {
@Override
public AbsenceTime map(ZepAbsence zepAbsence) {

if (zepAbsence == null){
if (zepAbsence == null) {
logger.info("ZEP REST implementation -- While trying to map ZepAbsence to AbsenceTime, ZepAbsence was null");
return null;
}
Expand All @@ -36,7 +36,7 @@ public AbsenceTime map(ZepAbsence zepAbsence) {
logger.debug("Mapped ZepAbsence to AbsenceTime -- some values have been hardcoded and not fetched from the ZEP rest client");
return absenceTime;

} catch (Exception e){
} catch (Exception e) {
throw new ZepServiceException("While trying to map ZepAbsence to AbsenceTime, an error occurred", e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public com.gepardec.mega.domain.model.monthlyreport.ProjectEntry map(ZepAttendan
return null;
}

try{
try {

Task task = toTask(zepAttendance.activity());
LocalDateTime from = LocalDateTime.of(zepAttendance.date(), zepAttendance.from());
Expand Down Expand Up @@ -54,11 +54,9 @@ public com.gepardec.mega.domain.model.monthlyreport.ProjectEntry map(ZepAttendan
.process(process)
.build();
}
}catch (Exception e){
} catch (Exception e) {
throw new ZepServiceException("While trying to map ZepAttendance to ProjectEntry, an error occurred", e);
}


}

@Override
Expand Down Expand Up @@ -88,7 +86,7 @@ private Vehicle toVehicle(final String vehicle) {
}

private JourneyDirection toJourneyDirection(final ZepAttendanceDirectionOfTravel direction) {
if(direction == null) {
if (direction == null) {
return JourneyDirection.TO;
}
return JourneyDirection.fromString(direction.id())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ public Project.Builder map(ZepProject zepProject) {
.employees(employees)
.leads(leads)
.categories(List.of("CONS"));
}catch (Exception e){
} catch (Exception e) {
throw new ZepServiceException("While trying to map ZepProject to Project, an error occurred", e);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public class RegularWorkingHoursMapMapper implements Mapper<Map<DayOfWeek, Durat
@Override
public Map<DayOfWeek, Duration> map(ZepRegularWorkingTimes zepRegularWorkingTimes) {

try{

try {
Map<DayOfWeek, Duration> regularWorkingHours = new EnumMap<>(DayOfWeek.class);

if (zepRegularWorkingTimes != null) {
Expand All @@ -29,7 +28,7 @@ public Map<DayOfWeek, Duration> map(ZepRegularWorkingTimes zepRegularWorkingTime
regularWorkingHours.put(DayOfWeek.SUNDAY, Duration.ofHours(zepRegularWorkingTimes.sunday() == null ? 0 : zepRegularWorkingTimes.sunday().longValue()));
}
return regularWorkingHours;
}catch (Exception e){
} catch (Exception e) {
throw new ZepServiceException("While trying to map ZepRegularWorkingTimes to Map<DayOfWeek, Duration>, an error occurred", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public List<ZepAbsence> getZepAbsencesByEmployeeNameForDateRange(String employee
);

List<ZepAbsence> filteredAbsences = absences.stream()
.filter(absence -> datesInRange(absence.startDate(), absence.endDate(), start, end))
.toList();
.filter(absence -> datesInRange(absence.startDate(), absence.endDate(), start, end))
.toList();
return getFullZepAbsences(filteredAbsences);
} catch (ZepServiceException e) {
} catch (ZepServiceException e) {
logger.warn("Error retrieving employee + \"%s\" from ZEP: No /data field in response".formatted(employeeName),
e);
return List.of();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public List<ZepAttendance> getBillableAttendancesForUserAndMonth(String username
return List.of();

}

//Return the attendances for a user for a given month. The month in which the date is located determines the month to be queried.
public List<ZepAttendance> getAttendanceForUserAndMonth(String username, LocalDate date) {
String startDate = date.withDayOfMonth(1).toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public List<ZepProject> getProjectsForMonthYear(LocalDate date) {

try {
return responseParser.retrieveAll(
page -> zepProjectRestClient.getProjectByStartEnd(startDate,endDate, page),
page -> zepProjectRestClient.getProjectByStartEnd(startDate, endDate, page),
ZepProject.class
);
} catch (ZepServiceException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
package com.gepardec.mega.application.exception.mapper;

import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import org.hibernate.exception.ConstraintViolationException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Spy;
import org.slf4j.Logger;

import jakarta.inject.Inject;

import java.sql.SQLException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.contains;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@QuarkusTest
public class HibernateConstraintValidationExceptionMapperTest {
class HibernateConstraintValidationExceptionMapperTest {

@Inject

HibernateConstraintViolationExceptionMapper hibernateConstraintViolationExceptionMapper;

@Spy
Expand All @@ -37,7 +33,7 @@ void setup() {
}

@Test
public void when_ConstraintViolationExcpetion_THEN_LOG_WITH_STATUS_400(){
void when_ConstraintViolationExcpetion_THEN_LOG_WITH_STATUS_400() {
final Response response = hibernateConstraintViolationExceptionMapper.toResponse(new ConstraintViolationException(" ERROR: duplicate key value violates unique constraint \"uc_premature_employee_check_userid_and_formonth\"\n" +
" Detail: Key (user_id, for_month)=(18, 2023-11-01) already exists.", new SQLException(), "uc_premature_employee_check_userid_and_formonth"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import static org.assertj.core.api.Assertions.assertThat;

@QuarkusTest
public class CommentTest {
class CommentTest {

@Test
public void setMessage_whenSmallMessage_thenSetsSameMessage() {
void setMessage_whenSmallMessage_thenSetsSameMessage() {
// given
String input = "Hello";

Expand All @@ -22,7 +22,7 @@ public void setMessage_whenSmallMessage_thenSetsSameMessage() {
}

@Test
public void setMessage_whenMessageSizeIsLimit_thenSetsSameMessage() {
void setMessage_whenMessageSizeIsLimit_thenSetsSameMessage() {
// given
String inputMessage = "a".repeat(Comment.MAX_MESSAGE_LENGTH);

Expand All @@ -35,7 +35,7 @@ public void setMessage_whenMessageSizeIsLimit_thenSetsSameMessage() {
}

@Test
public void setMessage_whenLargeMessage_thenSetsShortenedMessage() {
void setMessage_whenLargeMessage_thenSetsShortenedMessage() {
// given
String inputMessage = "a".repeat(Comment.MAX_MESSAGE_LENGTH + 10);

Expand All @@ -49,7 +49,7 @@ public void setMessage_whenLargeMessage_thenSetsShortenedMessage() {
}

@Test
public void setMessage_whenNull_thenMessageNull() {
void setMessage_whenNull_thenMessageNull() {
Comment messageComment = new Comment();
messageComment.setMessage("becomes null");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

@QuarkusTest
@TestTransaction
public class PrematureEmployeeCheckRepositoryTest {
class PrematureEmployeeCheckRepositoryTest {

@Inject
PrematureEmployeeCheckRepository prematureEmployeeCheckRepository;
Expand All @@ -32,12 +32,12 @@ public class PrematureEmployeeCheckRepositoryTest {
private com.gepardec.mega.db.entity.employee.User user;

@BeforeEach
public void initDependentEntities() {
void initDependentEntities() {
user = initializeUserObject();
}

@Test
public void save_validEntry_returnDbId() {
void save_validEntry_returnDbId() {
// Given
persistUser();

Expand All @@ -49,7 +49,7 @@ public void save_validEntry_returnDbId() {
}

@Test
public void save_secondEntry_throwConstraintViolationException() {
void save_secondEntry_throwConstraintViolationException() {
// Given
persistUser();
PrematureEmployeeCheckEntity saved = prematureEmployeeCheckRepository.create(createDBPrematureEmployeeCheck(null));
Expand All @@ -68,7 +68,7 @@ public void save_secondEntry_throwConstraintViolationException() {
}

@Test
public void findByEmail_missingEntry_returnEmptyList() {
void findByEmail_missingEntry_returnEmptyList() {
// When
var byEmailAndMonth = prematureEmployeeCheckRepository.findByEmailAndMonth(EMAIL, DATE);

Expand All @@ -77,7 +77,7 @@ public void findByEmail_missingEntry_returnEmptyList() {
}

@Test
public void findByEmail_validEntries_returnList() {
void findByEmail_validEntries_returnList() {
// Given
persistUser();
persistPrematureEmployeeCheck();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.slf4j.Logger;

import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
Expand Down
Loading

0 comments on commit 4ac78c3

Please sign in to comment.