fix: added null check in sharedService and updated test
This commit is contained in:
parent
6861d07dfc
commit
8ee06b93a6
@ -2,7 +2,8 @@ package enseirb.myinpulse.service;
|
||||
|
||||
import com.itextpdf.text.*;
|
||||
import com.itextpdf.text.pdf.PdfWriter;
|
||||
|
||||
import enseirb.myinpulse.controller.AdminApi;
|
||||
import enseirb.myinpulse.controller.EntrepreneurApi;
|
||||
import enseirb.myinpulse.model.*;
|
||||
import enseirb.myinpulse.service.database.*;
|
||||
|
||||
@ -30,6 +31,10 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
@Service
|
||||
public class SharedApiService {
|
||||
|
||||
private final AdminApi adminApi;
|
||||
|
||||
private final EntrepreneurApi entrepreneurApi;
|
||||
|
||||
protected static final Logger logger = LogManager.getLogger();
|
||||
|
||||
private final ProjectService projectService;
|
||||
@ -45,12 +50,14 @@ public class SharedApiService {
|
||||
EntrepreneurService entrepreneurService,
|
||||
SectionCellService sectionCellService,
|
||||
AppointmentService appointmentService,
|
||||
UtilsService utilsService) {
|
||||
UtilsService utilsService, EntrepreneurApi entrepreneurApi, AdminApi adminApi) {
|
||||
this.projectService = projectService;
|
||||
this.entrepreneurService = entrepreneurService;
|
||||
this.sectionCellService = sectionCellService;
|
||||
this.appointmentService = appointmentService;
|
||||
this.utilsService = utilsService;
|
||||
this.entrepreneurApi = entrepreneurApi;
|
||||
this.adminApi = adminApi;
|
||||
}
|
||||
|
||||
// TODO filter this with date
|
||||
@ -287,6 +294,13 @@ public class SharedApiService {
|
||||
sectionCell -> {
|
||||
sectionCell.updateAppointmentSectionCell(newAppointment);
|
||||
});
|
||||
newAppointment.getAppointmentReport().setAppointmentReport(newAppointment);
|
||||
|
||||
/*
|
||||
* On initial insertion, the resport value is null unless that report does already exist in the db somewhere
|
||||
* If a non null value is passed and it does not exist in db it will throw an exception
|
||||
*/
|
||||
if (newAppointment.getAppointmentReport() != null) {
|
||||
newAppointment.getAppointmentReport().setAppointmentReport(newAppointment);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -209,6 +209,16 @@ public class SharedApiServiceTest {
|
||||
return report;
|
||||
}
|
||||
|
||||
/*
|
||||
* _____ _ ____ _ _ ____ _ _
|
||||
* |_ _|__ ___| |_/ ___| ___ ___| |_(_) ___ _ __ / ___|___| | |
|
||||
* | |/ _ \/ __| __\___ \ / _ \/ __| __| |/ _ \| '_ \| | / _ \ | |
|
||||
* | | __/\__ \ |_ ___) | __/ (__| |_| | (_) | | | | |__| __/ | |
|
||||
* |_|\___||___/\__|____/ \___|\___|\__|_|\___/|_| |_|\____\___|_|_|
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Tests retrieving section cells for a specific project and section ID before a given date
|
||||
* when the user is authorized but no matching cells exist.
|
||||
@ -299,6 +309,19 @@ public class SharedApiServiceTest {
|
||||
assertEquals(HttpStatus.UNAUTHORIZED, exception.getStatusCode());
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* _____ _ ____ _ ____ _ _ ____
|
||||
* |_ _|__ ___| |_ / ___| ___| |_| _ \ _ __ ___ (_) ___ ___| |_| __ ) _ _
|
||||
* | |/ _ \/ __| __| | _ / _ \ __| |_) | '__/ _ \| |/ _ \/ __| __| _ \| | | |
|
||||
* | | __/\__ \ |_| |_| | __/ |_| __/| | | (_) | | __/ (__| |_| |_) | |_| |
|
||||
* _|_|\___||___/\__|\____|\___|\__|_| |_| \___// |\___|\___|\__|____/ \__, |
|
||||
* |_ _| _ \ |__/ |___/
|
||||
* | || | | |
|
||||
* | || |_| |
|
||||
* |___|____/
|
||||
*/
|
||||
|
||||
/*
|
||||
* Tests retrieving entrepreneurs linked to a project when the user is authorized
|
||||
* but no entrepreneurs are linked.
|
||||
@ -337,6 +360,39 @@ public class SharedApiServiceTest {
|
||||
assertEquals(HttpStatus.UNAUTHORIZED, exception.getStatusCode());
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* _____ _ ____ _ _ _ _ ____
|
||||
* |_ _|__ ___| |_ / ___| ___| |_ / \ __| |_ __ ___ (_)_ __ | __ ) _ _
|
||||
* | |/ _ \/ __| __| | _ / _ \ __| / _ \ / _` | '_ ` _ \| | '_ \| _ \| | | |
|
||||
* | | __/\__ \ |_| |_| | __/ |_ / ___ \ (_| | | | | | | | | | | |_) | |_| |
|
||||
* _|_|\___||___/\__|\____|\___|\__/_/ \_\__,_|_| |_| |_|_|_| |_|____/ \__, |
|
||||
* |_ _| _ \ |___/
|
||||
* | || | | |
|
||||
* | || |_| |
|
||||
* |___|____/
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Tests retrieving appointments linked to a project's section cells when the user is authorized
|
||||
* but no such appointments exist.
|
||||
* Verifies that an empty list is returned.
|
||||
*/
|
||||
@Test
|
||||
void testGetAppointmentsByProjectId_Authorized_NotFound() {
|
||||
// Arrange: staticAuthorizedProject has no linked section cells or appointments initially
|
||||
// Act
|
||||
Iterable<Appointment> result =
|
||||
sharedApiService.getAppointmentsByProjectId(
|
||||
staticAuthorizedProject.getIdProject(), staticAuthorizedMail);
|
||||
|
||||
List<Appointment> resultList = TestUtils.toList(result);
|
||||
|
||||
// Assert
|
||||
assertTrue(resultList.isEmpty());
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests retrieving the administrator linked to a project when the user is authorized
|
||||
* and an administrator is linked.
|
||||
@ -376,23 +432,17 @@ public class SharedApiServiceTest {
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests retrieving appointments linked to a project's section cells when the user is authorized
|
||||
* but no such appointments exist.
|
||||
* Verifies that an empty list is returned.
|
||||
* _____ _
|
||||
* |_ _|__ ___| |_
|
||||
* | |/ _ \/ __| __|
|
||||
* | | __/\__ \ |_
|
||||
* |_|\___||___/\__| _ _ _
|
||||
* / \ _ __ _ __ ___ (_)_ __ | |_ ___ _ __ ___ ___ _ __ | |_ ___
|
||||
* / _ \ | '_ \| '_ \ / _ \| | '_ \| __/ _ \ '_ ` _ \ / _ \ '_ \| __/ __|
|
||||
* / ___ \| |_) | |_) | (_) | | | | | || __/ | | | | | __/ | | | |_\__ \
|
||||
* /_/ \_\ .__/| .__/ \___/|_|_| |_|\__\___|_| |_| |_|\___|_| |_|\__|___/
|
||||
* |_| |_|
|
||||
*/
|
||||
@Test
|
||||
void testGetAppointmentsByProjectId_Authorized_NotFound() {
|
||||
// Arrange: staticAuthorizedProject has no linked section cells or appointments initially
|
||||
// Act
|
||||
Iterable<Appointment> result =
|
||||
sharedApiService.getAppointmentsByProjectId(
|
||||
staticAuthorizedProject.getIdProject(), staticAuthorizedMail);
|
||||
|
||||
List<Appointment> resultList = TestUtils.toList(result);
|
||||
|
||||
// Assert
|
||||
assertTrue(resultList.isEmpty());
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests retrieving appointments linked to a project's section cells when the user is not authorized.
|
||||
@ -413,6 +463,92 @@ public class SharedApiServiceTest {
|
||||
assertEquals(HttpStatus.UNAUTHORIZED, exception.getStatusCode());
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests creating a new appointment request when the user is authorized
|
||||
* for the project linked to the appointment's section cell.
|
||||
* Verifies that the appointment and its relationships are saved correctly in the database.
|
||||
*/
|
||||
// Tests createAppointmentRequest
|
||||
@Test
|
||||
// Commenting out failing test
|
||||
void testCreateAppointmentRequest_Authorized_Success() {
|
||||
// Arrange: Create transient appointment linked to a cell in the static authorized project
|
||||
LocalDate date = LocalDate.parse("2026-01-01");
|
||||
LocalTime time = LocalTime.parse("10:00:00");
|
||||
LocalTime duration = LocalTime.parse("00:30:00");
|
||||
String place = "Meeting Room Integrated";
|
||||
String subject = "Discuss Project Integrated";
|
||||
|
||||
SectionCell linkedCell =
|
||||
sectionCellService.addNewSectionCell(
|
||||
getTestSectionCell(
|
||||
staticAuthorizedProject,
|
||||
0L,
|
||||
"Related Section Content Integrated",
|
||||
LocalDateTime.now()));
|
||||
|
||||
Report newReport = null;// getTestReport(reportContent); // Uses no-arg constructor
|
||||
|
||||
Appointment newAppointment =
|
||||
getTestAppointment(
|
||||
date, time, duration, place, subject, List.of(linkedCell), newReport);
|
||||
|
||||
// mockUtilsService is configured in BeforeEach to allow staticAuthorizedMail for
|
||||
// staticAuthorizedProject
|
||||
|
||||
// Act
|
||||
// Allow the service method to call the actual appointmentService.addNewAppointment
|
||||
assertDoesNotThrow(
|
||||
() ->
|
||||
sharedApiService.createAppointmentRequest(
|
||||
newAppointment, staticAuthorizedMail));
|
||||
|
||||
// Assert: Retrieve the appointment from the DB and verify it and its relationships were
|
||||
// saved
|
||||
// We find it by looking for appointments linked to the authorized project's cells
|
||||
Iterable<SectionCell> projectCells =
|
||||
sectionCellService.getSectionCellsByProject(
|
||||
staticAuthorizedProject, linkedCell.getSectionId()); // Fetch relevant cells
|
||||
List<Appointment> projectAppointmentsList = new ArrayList<>();
|
||||
projectCells.forEach(
|
||||
cell ->
|
||||
projectAppointmentsList.addAll(
|
||||
sectionCellService.getAppointmentsBySectionCellId(
|
||||
cell.getIdSectionCell()))); // Get appointments for
|
||||
// those cells
|
||||
|
||||
Optional<Appointment> createdAppointmentOpt =
|
||||
projectAppointmentsList.stream()
|
||||
.filter(
|
||||
a ->
|
||||
a.getAppointmentDate().equals(date)
|
||||
&& a.getAppointmentTime().equals(time)
|
||||
&& a.getAppointmentPlace().equals(place)
|
||||
&& a.getAppointmentSubject().equals(subject))
|
||||
.findFirst();
|
||||
|
||||
assertTrue(createdAppointmentOpt.isPresent());
|
||||
Appointment createdAppointment = createdAppointmentOpt.get();
|
||||
|
||||
// FIX: Corrected bidirectional link check
|
||||
assertEquals(1, createdAppointment.getAppointmentListSectionCell().size());
|
||||
assertTrue(
|
||||
createdAppointment.getAppointmentListSectionCell().stream()
|
||||
.anyMatch(
|
||||
sc -> sc.getIdSectionCell().equals(linkedCell.getIdSectionCell())));
|
||||
|
||||
List<Appointment> appointmentsLinkedToCell =
|
||||
TestUtils.toList(
|
||||
sectionCellService.getAppointmentsBySectionCellId(
|
||||
linkedCell.getIdSectionCell()));
|
||||
assertTrue(
|
||||
appointmentsLinkedToCell.stream()
|
||||
.anyMatch(
|
||||
a ->
|
||||
a.getIdAppointment()
|
||||
.equals(createdAppointment.getIdAppointment())));
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests creating a new appointment request when the user is not authorized
|
||||
* for the project linked to the appointment's section cell.
|
||||
@ -574,99 +710,6 @@ public class SharedApiServiceTest {
|
||||
assertEquals(HttpStatus.UNAUTHORIZED, exception.getStatusCode());
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests creating a new appointment request when the user is authorized
|
||||
* for the project linked to the appointment's section cell.
|
||||
* Verifies that the appointment and its relationships are saved correctly in the database.
|
||||
*/
|
||||
// Tests createAppointmentRequest
|
||||
/*@Test*/
|
||||
// Commenting out failing test
|
||||
void testCreateAppointmentRequest_Authorized_Success() {
|
||||
// Arrange: Create transient appointment linked to a cell in the static authorized project
|
||||
LocalDate date = LocalDate.parse("2026-01-01");
|
||||
LocalTime time = LocalTime.parse("10:00:00");
|
||||
LocalTime duration = LocalTime.parse("00:30:00");
|
||||
String place = "Meeting Room Integrated";
|
||||
String subject = "Discuss Project Integrated";
|
||||
String reportContent = "Initial Report Integrated";
|
||||
|
||||
SectionCell linkedCell =
|
||||
sectionCellService.addNewSectionCell(
|
||||
getTestSectionCell(
|
||||
staticAuthorizedProject,
|
||||
1L,
|
||||
"Related Section Content Integrated",
|
||||
LocalDateTime.now()));
|
||||
|
||||
Report newReport = getTestReport(reportContent); // Uses no-arg constructor
|
||||
Appointment newAppointment =
|
||||
getTestAppointment(
|
||||
date, time, duration, place, subject, List.of(linkedCell), newReport);
|
||||
|
||||
// mockUtilsService is configured in BeforeEach to allow staticAuthorizedMail for
|
||||
// staticAuthorizedProject
|
||||
|
||||
// Act
|
||||
// Allow the service method to call the actual appointmentService.addNewAppointment
|
||||
assertDoesNotThrow(
|
||||
() ->
|
||||
sharedApiService.createAppointmentRequest(
|
||||
newAppointment, staticAuthorizedMail));
|
||||
|
||||
// Assert: Retrieve the appointment from the DB and verify it and its relationships were
|
||||
// saved
|
||||
// We find it by looking for appointments linked to the authorized project's cells
|
||||
Iterable<SectionCell> projectCells =
|
||||
sectionCellService.getSectionCellsByProject(
|
||||
staticAuthorizedProject, 1L); // Fetch relevant cells
|
||||
List<Appointment> projectAppointmentsList = new ArrayList<>();
|
||||
projectCells.forEach(
|
||||
cell ->
|
||||
projectAppointmentsList.addAll(
|
||||
sectionCellService.getAppointmentsBySectionCellId(
|
||||
cell.getIdSectionCell()))); // Get appointments for
|
||||
// those cells
|
||||
|
||||
Optional<Appointment> createdAppointmentOpt =
|
||||
projectAppointmentsList.stream()
|
||||
.filter(
|
||||
a ->
|
||||
a.getAppointmentDate().equals(date)
|
||||
&& a.getAppointmentTime().equals(time)
|
||||
&& a.getAppointmentPlace().equals(place)
|
||||
&& a.getAppointmentSubject().equals(subject))
|
||||
.findFirst();
|
||||
|
||||
assertTrue(createdAppointmentOpt.isPresent());
|
||||
Appointment createdAppointment = createdAppointmentOpt.get();
|
||||
|
||||
assertNotNull(createdAppointment.getAppointmentReport());
|
||||
assertEquals(reportContent, createdAppointment.getAppointmentReport().getReportContent());
|
||||
// FIX: Corrected bidirectional link check
|
||||
assertEquals(
|
||||
createdAppointment.getIdAppointment(),
|
||||
createdAppointment
|
||||
.getAppointmentReport()
|
||||
.getAppointmentReport()
|
||||
.getIdAppointment());
|
||||
assertEquals(1, createdAppointment.getAppointmentListSectionCell().size());
|
||||
assertTrue(
|
||||
createdAppointment.getAppointmentListSectionCell().stream()
|
||||
.anyMatch(
|
||||
sc -> sc.getIdSectionCell().equals(linkedCell.getIdSectionCell())));
|
||||
|
||||
List<Appointment> appointmentsLinkedToCell =
|
||||
TestUtils.toList(
|
||||
sectionCellService.getAppointmentsBySectionCellId(
|
||||
linkedCell.getIdSectionCell()));
|
||||
assertTrue(
|
||||
appointmentsLinkedToCell.stream()
|
||||
.anyMatch(
|
||||
a ->
|
||||
a.getIdAppointment()
|
||||
.equals(createdAppointment.getIdAppointment())));
|
||||
}
|
||||
|
||||
// --- Test Methods (Use static data from @BeforeAll where possible) ---
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user