Skip to content

Commit

Permalink
Merge pull request #32 from VishwajeetVT/trunk
Browse files Browse the repository at this point in the history
New Tests  and code implementation
  • Loading branch information
Vishwajeet-29-pro authored Jan 21, 2025
2 parents 1afa836 + 6dc037c commit 17bb1de
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,19 @@ public EmployeeResponse assignParkingLot(Long employeeId, String spotNumber) {

@Override
public EmployeeResponse removeParkingLot(Long employeeId) {
return null;
Employee employee = employeeRepository.findById(employeeId).orElseThrow(
() -> new EmployeeNotFoundException("Employee with id: "+employeeId+" not found")
);
employee.setParkingSpot(null);
Employee removedParkingSpot = employeeRepository.save(employee);
return EmployeeResponse.toEmployeeResponse(removedParkingSpot);
}

@Override
public Optional<EmployeeResponse> findEmployeeWithParkingSpot(Long employeeId) {
return Optional.empty();
Employee employee = employeeRepository.findById(employeeId).orElseThrow(
() -> new EmployeeNotFoundException("Employee with id: "+employeeId+" not found")
);
return Optional.of(EmployeeResponse.toEmployeeResponse(employee));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,28 @@ void test_assign_parking_spot_to_employee_should_employee_response() {
verify(parkingSpotRepository, times(1)).findBySpotNumber("A1");
verify(employeeRepository, times(1)).save(any(Employee.class));
}

@Test
void test_remove_parking_spot_assign_to_employee() {
when(employeeRepository.findById(1L)).thenReturn(Optional.of(employee));
when(employeeRepository.save(any(Employee.class))).thenReturn(employee);

EmployeeResponse employeeResponse = employeeService.removeParkingLot(1L);
assertNotNull(employeeResponse);
assertNull(employeeResponse.getSpotNumber());
}

@Test
void test_find_employee_with_parking_spot() {
parkingSpot.setEmployee(employee);
parkingSpot.setAssigned(true);
employee.setParkingSpot(parkingSpot);
when(employeeRepository.findById(1L)).thenReturn(Optional.of(employee));
when(parkingSpotRepository.findBySpotNumber("A1")).thenReturn(Optional.of(parkingSpot));

Optional<EmployeeResponse> employeeResponse = employeeService.findEmployeeWithParkingSpot(1L);
assertNotNull(employeeResponse);
assertEquals(employee.getEmployeeName(), employeeResponse.get().getEmployeeName());
assertEquals(employee.getParkingSpot().getSpotNumber(), employeeResponse.get().getSpotNumber());
}
}

0 comments on commit 17bb1de

Please # to comment.