Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
Merged
4 changes: 1 addition & 3 deletions src/main/java/com/shiftsl/backend/Service/LeaveService.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ public String reject(Long leaveID) {
return "leave request rejected";
} catch(Exception e) {
logger.error("Error while trying to reject Leave Request for LeaveID={}", leaveID, e);
throw new LeaveNotSavedException(String.format(
"Failed to update leave status in database for leaveId=%d", leaveID
));
throw new LeaveNotSavedException(String.format("Failed to update leave status in database for leaveId=%d", leaveID));
}
}

Expand Down
14 changes: 8 additions & 6 deletions src/main/java/com/shiftsl/backend/Service/ShiftService.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public List<Shift> getAvailableShifts() {
// Doctor claims a shift from the shift pool
@Transactional
public void claimShift(Long doctorId, Long shiftId) {
String lockErrorMessage = null;
String errorMessage = null;
try {
logger.info("Claiming shift " + shiftId);
Shift shift = getShiftWithLock(shiftId);
Expand All @@ -82,7 +82,9 @@ public void claimShift(Long doctorId, Long shiftId) {
int sizeD = shift.getNoOfDoctors();

if (sizeD >= shift.getTotalDoctors()) {
throw new DoctorCountExceededException("Number of assigned doctors exceeds the allowed limit.");
logger.error("Number of doctors is greater than total number of doctors allowed");
errorMessage = "Number of assigned doctors exceeds the allowed limit.";
throw new DoctorCountExceededException(errorMessage);
}

Set<User> doctors = shift.getDoctors();
Expand All @@ -93,17 +95,17 @@ public void claimShift(Long doctorId, Long shiftId) {
shiftRepo.save(shift);
} catch (LockTimeoutException | PessimisticLockException e) {
logger.error("Too many threads are trying to claim shift.");
lockErrorMessage = "System is experiencing high load. Please try again later. " + e.getMessage();
throw new ShiftClaimFailedException(lockErrorMessage);
errorMessage = "System is experiencing high load. Please try again later. " + e.getMessage();
throw new ShiftClaimFailedException(errorMessage);
} catch (Exception e) {
logger.error("Error occurred while trying to store shift {} for doctor {} in database", shiftId, doctorId);

String fullMessage = String.format(
"Unable to claim shiftId: %d for doctorId: %d", shiftId, doctorId
);

if (lockErrorMessage != null) {
fullMessage += ". " + lockErrorMessage;
if (errorMessage != null) {
fullMessage += ". " + errorMessage;
} else {
fullMessage += ". " + e.getMessage();
}
Expand Down
35 changes: 15 additions & 20 deletions src/main/java/com/shiftsl/backend/Service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,23 @@ public class UserService {

@Transactional
public User registerUser(UserDTO userDTO) {
try {
logger.info("Checking if user is already registered with given phone number");
userRepo.findByPhoneNo(userDTO.phoneNo()).ifPresent(user -> {
throw new PhoneAlreadyInUseException("Phone Number already in use.");
});
logger.info("Checking if user is already registered with given phone number");
userRepo.findByPhoneNo(userDTO.phoneNo()).ifPresent(user -> {
throw new PhoneAlreadyInUseException("Phone Number already in use.");
});

logger.info("Creating User object for User {} {}, and number {}", userDTO.firstName(), userDTO.lastName(), userDTO.phoneNo());
User user = new User();
user.setFirstName(userDTO.firstName());
user.setLastName(userDTO.lastName());
user.setSlmcReg(userDTO.slmcReg());
user.setFirebaseUid(firebaseAuthService.createUser(userDTO.email()));
user.setEmail(userDTO.email());
user.setPhoneNo(userDTO.phoneNo());
user.setRole(userDTO.role());
logger.info("Creating User object for User {} {}, and number {}", userDTO.firstName(), userDTO.lastName(), userDTO.phoneNo());
User user = new User();
user.setFirstName(userDTO.firstName());
user.setLastName(userDTO.lastName());
user.setSlmcReg(userDTO.slmcReg());
user.setFirebaseUid(firebaseAuthService.createUser(userDTO.email()));
user.setEmail(userDTO.email());
user.setPhoneNo(userDTO.phoneNo());
user.setRole(userDTO.role());

logger.info("Registering User with phone number: {}", user.getPhoneNo());
return userRepo.save(user);
} catch (Exception e) {
logger.error("Unable to save user to database", e);
throw new AccountNotCreatedException("Unable to register the current user");
}
logger.info("Registering User with phone number: {}", user.getPhoneNo());
return userRepo.save(user);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.firebase.database.DatabaseException;
import com.shiftsl.backend.DTO.LeaveDTO;
import com.shiftsl.backend.Exceptions.LeaveNotFoundException;
import com.shiftsl.backend.Exceptions.LeaveNotSavedException;
import com.shiftsl.backend.Exceptions.LeaveRetrievalException;
import com.shiftsl.backend.Service.LeaveService;
Expand All @@ -12,6 +13,8 @@
import com.shiftsl.backend.unittests.Extensions.TimingExtension;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
Expand All @@ -28,6 +31,7 @@

@ExtendWith(TimingExtension.class)
@ExtendWith(MockitoExtension.class)
@Execution(ExecutionMode.CONCURRENT)
public class LeaveServiceTest {

@InjectMocks
Expand All @@ -39,13 +43,9 @@ public class LeaveServiceTest {
@Mock
private ShiftService shiftService;

@Mock
private User testDoctor;
@Mock
private Shift testShift;
@Mock
private Leave testLeave;
@Mock
private LeaveDTO testLeaveDTO;

private ArgumentCaptor<Leave> captor;
Expand Down Expand Up @@ -156,8 +156,16 @@ void requestLeaveExceptionTest() {
@Test
void getLeaveTest() {
when(leaveRepo.findById(ID)).thenReturn(Optional.ofNullable(testLeave));
underTest.getLeave(ID);
Leave result = underTest.getLeave(ID);
verify(leaveRepo).findById(ID);
assertEquals(testLeave, result);
}

@Test
void getLeaveExceptionTest() {
when(leaveRepo.findById(ID)).thenReturn(Optional.empty());
Exception ex = assertThrows(LeaveNotFoundException.class,() -> underTest.getLeave(ID));
assertEquals("User - (" + ID + ") not found.", ex.getMessage());
}

@Test
Expand All @@ -176,9 +184,10 @@ void rejectTest() {

@Test
void rejectExceptionTest() {
when(leaveRepo.findById(ID)).thenThrow(new DatabaseException("Unable to access the database"));
when(leaveRepo.findById(ID)).thenThrow(new RuntimeException());
Exception e = assertThrows(LeaveNotSavedException.class,() -> underTest.reject(ID));
verify(leaveRepo, never()).save(testLeave); // verify that the leave object was never saved to database
Exception e = assertThrows(LeaveNotSavedException.class,() -> underTest.approve(ID));
verify(leaveRepo).findById(ID);
assertEquals(String.format("Failed to update leave status in database for leaveId=%d", ID), e.getMessage());
}

Expand Down
Loading