From 5edcf9ffc80854f9b206ac5ecc1237fc0ae654c2 Mon Sep 17 00:00:00 2001 From: MAILLAL Anas Date: Tue, 22 Apr 2025 09:46:07 +0200 Subject: [PATCH] fix: fixed formatting to be compatible with workflow --- .../myinpulse/SharedApiServiceTest.java | 719 ++++++++++++------ 1 file changed, 493 insertions(+), 226 deletions(-) diff --git a/MyINPulse-back/src/test/java/enseirb/myinpulse/SharedApiServiceTest.java b/MyINPulse-back/src/test/java/enseirb/myinpulse/SharedApiServiceTest.java index 04a9312..17c14df 100644 --- a/MyINPulse-back/src/test/java/enseirb/myinpulse/SharedApiServiceTest.java +++ b/MyINPulse-back/src/test/java/enseirb/myinpulse/SharedApiServiceTest.java @@ -34,7 +34,7 @@ import java.util.Optional; // Helper to easily convert Iterable to List class TestUtils { - static List toList(Iterable iterable) { + static List toList(Iterable iterable) { List list = new ArrayList<>(); if (iterable != null) { iterable.forEach(list::add); @@ -43,7 +43,6 @@ class TestUtils { } } - @SpringBootTest @Transactional // Each @Test method runs in a transaction that is rolled back public class SharedApiServiceTest { @@ -68,7 +67,6 @@ public class SharedApiServiceTest { private static Project staticUnauthorizedProject; private static String staticUnauthorizedMail; - // --- Static Setup (Runs once before all tests) --- // Use @BeforeAll static method with injected services @BeforeAll @@ -78,54 +76,90 @@ public class SharedApiServiceTest { @Autowired EntrepreneurService entrepreneurService) { // Create and Save core test data here using injected services - staticAuthorizedAdmin = administratorService.addAdministrator(getTestAdmin("static_authorized_admin")); + staticAuthorizedAdmin = + administratorService.addAdministrator(getTestAdmin("static_authorized_admin")); staticAuthorizedMail = staticAuthorizedAdmin.getPrimaryMail(); - staticUnauthorizedProject = projectService.addNewProject(getTestProject("static_unauthorized_project", administratorService.addAdministrator(getTestAdmin("static_unauthorized_admin")))); - staticUnauthorizedMail = administratorService.addAdministrator(getTestAdmin("static_unauthorized_user")).getPrimaryMail(); // User who is NOT admin of the unauthorized project + staticUnauthorizedProject = + projectService.addNewProject( + getTestProject( + "static_unauthorized_project", + administratorService.addAdministrator( + getTestAdmin("static_unauthorized_admin")))); + staticUnauthorizedMail = + administratorService + .addAdministrator(getTestAdmin("static_unauthorized_user")) + .getPrimaryMail(); // User who is NOT admin of the unauthorized project - staticAuthorizedProject = projectService.addNewProject(getTestProject("static_authorized_project", staticAuthorizedAdmin)); + staticAuthorizedProject = + projectService.addNewProject( + getTestProject("static_authorized_project", staticAuthorizedAdmin)); - // Link a static entrepreneur to the authorized project if needed for some tests - // Entrepreneur staticLinkedEntrepreneur = entrepreneurService.addEntrepreneur(getTestEntrepreneur("static_linked_entrepreneur")); - // staticAuthorizedProject.updateListEntrepreneurParticipation(staticLinkedEntrepreneur); - // projectService.addNewProject(staticAuthorizedProject); // Re-save the project after updating lists + // Link a static entrepreneur to the authorized project if needed for some tests + // Entrepreneur staticLinkedEntrepreneur = + // entrepreneurService.addEntrepreneur(getTestEntrepreneur("static_linked_entrepreneur")); + // staticAuthorizedProject.updateListEntrepreneurParticipation(staticLinkedEntrepreneur); + // projectService.addNewProject(staticAuthorizedProject); // Re-save the project after + // updating lists } // --- Per-Test Setup (Runs before each test method) --- @BeforeEach void setupForEach() { // Reset mock expectations before each test - MockitoAnnotations.openMocks(this); // Needed for mocks if not using @ExtendWith(MockitoExtension.class) + MockitoAnnotations.openMocks( + this); // Needed for mocks if not using @ExtendWith(MockitoExtension.class) // --- Configure the mock UtilsService based on the actual authorization rules --- // Rule: Any admin can check any project. // Assuming staticAuthorizedMail is an admin: - when(mockUtilsService.isAllowedToCheckProject(eq(staticAuthorizedMail), anyLong())).thenReturn(true); // Admin allowed for ANY project ID + when(mockUtilsService.isAllowedToCheckProject(eq(staticAuthorizedMail), anyLong())) + .thenReturn(true); // Admin allowed for ANY project ID // Rule: An entrepreneur can only check their own stuff. - // Assuming staticUnauthorizedMail is an entrepreneur NOT linked to staticAuthorizedProject or staticUnauthorizedProject: - when(mockUtilsService.isAllowedToCheckProject(eq(staticUnauthorizedMail), anyLong())).thenReturn(false); // Unauthorized entrepreneur NOT allowed for ANY project ID by default + // Assuming staticUnauthorizedMail is an entrepreneur NOT linked to staticAuthorizedProject + // or staticUnauthorizedProject: + when(mockUtilsService.isAllowedToCheckProject(eq(staticUnauthorizedMail), anyLong())) + .thenReturn( + false); // Unauthorized entrepreneur NOT allowed for ANY project ID by + // default // Add more specific mock setups here if needed for entrepreneur tests // E.g., If you have a test specifically for an entrepreneur accessing THEIR project: - // Entrepreneur testEntrepreneur = entrepreneurService.addEntrepreneur(getTestEntrepreneur("specific_linked_entrepreneur")); - // Project linkedProject = projectService.addNewProject(getTestProject("specific_linked_project", staticAuthorizedAdmin)); + // Entrepreneur testEntrepreneur = + // entrepreneurService.addEntrepreneur(getTestEntrepreneur("specific_linked_entrepreneur")); + // Project linkedProject = + // projectService.addNewProject(getTestProject("specific_linked_project", + // staticAuthorizedAdmin)); // // Link testEntrepreneur to linkedProject in the database setup... - // when(mockUtilsService.isAllowedToCheckProject(eq(testEntrepreneur.getPrimaryMail()), eq(linkedProject.getIdProject()))).thenReturn(true); - // when(mockUtilsService.isAllowedToCheckProject(eq(testEntrepreneur.getPrimaryMail()), anyLong())).thenReturn(false); // Deny for other projects + // when(mockUtilsService.isAllowedToCheckProject(eq(testEntrepreneur.getPrimaryMail()), + // eq(linkedProject.getIdProject()))).thenReturn(true); + // when(mockUtilsService.isAllowedToCheckProject(eq(testEntrepreneur.getPrimaryMail()), + // anyLong())).thenReturn(false); // Deny for other projects } - // --- Helper Methods (Can remain non-static or static as needed) --- private static Administrator getTestAdmin(String name) { - return new Administrator(name + "_surname", name, name + "@example.com", "secondary_" + name + "@example.com", "0123456789"); + return new Administrator( + name + "_surname", + name, + name + "@example.com", + "secondary_" + name + "@example.com", + "0123456789"); } private static Entrepreneur getTestEntrepreneur(String name) { - return new Entrepreneur(name + "_surname", name, name + "@example.com", "secondary_" + name + "@example.com", "0123456789", "Test School", "Test Course", false); + return new Entrepreneur( + name + "_surname", + name, + name + "@example.com", + "secondary_" + name + "@example.com", + "0123456789", + "Test School", + "Test Course", + false); } private static Project getTestProject(String name, Administrator admin) { @@ -133,7 +167,8 @@ public class SharedApiServiceTest { return project; } - private static SectionCell getTestSectionCell(Project project, Long sectionId, String content, LocalDateTime date) { + private static SectionCell getTestSectionCell( + Project project, Long sectionId, String content, LocalDateTime date) { SectionCell sectionCell = new SectionCell(); sectionCell.setProjectSectionCell(project); sectionCell.setSectionId(sectionId); @@ -142,7 +177,14 @@ public class SharedApiServiceTest { return sectionCell; } - private static Appointment getTestAppointment(LocalDate date, LocalTime time, LocalTime duration, String place, String subject, List sectionCells, Report report) { + private static Appointment getTestAppointment( + LocalDate date, + LocalTime time, + LocalTime duration, + String place, + String subject, + List sectionCells, + Report report) { Appointment appointment = new Appointment(); appointment.setAppointmentDate(date); appointment.setAppointmentTime(time); @@ -150,11 +192,11 @@ public class SharedApiServiceTest { appointment.setAppointmentPlace(place); appointment.setAppointmentSubject(subject); - if(sectionCells != null) { + if (sectionCells != null) { sectionCells.forEach(appointment::updateListSectionCell); } - if(report != null) { + if (report != null) { appointment.setAppointmentReport(report); report.setAppointmentReport(appointment); } @@ -168,8 +210,6 @@ 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. @@ -182,11 +222,12 @@ public class SharedApiServiceTest { LocalDateTime dateFilter = LocalDateTime.now().plusDays(1); // Act - Iterable result = sharedApiService.getSectionCells( - staticAuthorizedProject.getIdProject(), - targetSectionId, - dateFilter.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")), - staticAuthorizedMail); + Iterable result = + sharedApiService.getSectionCells( + staticAuthorizedProject.getIdProject(), + targetSectionId, + dateFilter.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")), + staticAuthorizedMail); List resultList = TestUtils.toList(result); @@ -202,19 +243,25 @@ public class SharedApiServiceTest { void testGetSectionCells_Unauthorized() { // Arrange: mockUtilsService configured in BeforeEach // Act & Assert - ResponseStatusException exception = assertThrows(ResponseStatusException.class, () -> { - sharedApiService.getSectionCells( - staticAuthorizedProject.getIdProject(), // Project static user is not authorized for - 1L, - LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")), - staticUnauthorizedMail); // Static unauthorized user mail - }); + ResponseStatusException exception = + assertThrows( + ResponseStatusException.class, + () -> { + sharedApiService.getSectionCells( + staticAuthorizedProject + .getIdProject(), // Project static user is not + // authorized for + 1L, + LocalDateTime.now() + .format( + DateTimeFormatter.ofPattern( + "yyyy-MM-dd HH:mm")), + staticUnauthorizedMail); // Static unauthorized user mail + }); assertEquals(HttpStatus.UNAUTHORIZED, exception.getStatusCode()); } - - /* * Tests retrieving all section cells for a project when the user is authorized * but the project has no section cells. @@ -224,9 +271,9 @@ public class SharedApiServiceTest { void testGetAllSectionCells_Authorized_NoCells() { // Arrange: staticAuthorizedProject has no section cells initially in BeforeAll // Act - Iterable result = sharedApiService.getAllSectionCells( - staticAuthorizedProject.getIdProject(), - staticAuthorizedMail); + Iterable result = + sharedApiService.getAllSectionCells( + staticAuthorizedProject.getIdProject(), staticAuthorizedMail); List resultList = TestUtils.toList(result); @@ -242,18 +289,17 @@ public class SharedApiServiceTest { void testGetAllSectionCells_Unauthorized() { // Arrange: mockUtilsService configured in BeforeEach // Act & Assert - ResponseStatusException exception = assertThrows(ResponseStatusException.class, () -> { - sharedApiService.getAllSectionCells( - staticAuthorizedProject.getIdProject(), - staticUnauthorizedMail); - }); + ResponseStatusException exception = + assertThrows( + ResponseStatusException.class, + () -> { + sharedApiService.getAllSectionCells( + staticAuthorizedProject.getIdProject(), staticUnauthorizedMail); + }); assertEquals(HttpStatus.UNAUTHORIZED, exception.getStatusCode()); } - - - /* * Tests retrieving entrepreneurs linked to a project when the user is authorized * but no entrepreneurs are linked. @@ -263,9 +309,9 @@ public class SharedApiServiceTest { void testGetEntrepreneursByProjectId_Authorized_NotFound() { // Arrange: staticAuthorizedProject has no entrepreneurs linked initially in BeforeAll // Act - Iterable result = sharedApiService.getEntrepreneursByProjectId( - staticAuthorizedProject.getIdProject(), - staticAuthorizedMail); + Iterable result = + sharedApiService.getEntrepreneursByProjectId( + staticAuthorizedProject.getIdProject(), staticAuthorizedMail); List resultList = TestUtils.toList(result); @@ -281,11 +327,13 @@ public class SharedApiServiceTest { void testGetEntrepreneursByProjectId_Unauthorized() { // Arrange: mockUtilsService configured in BeforeEach // Act & Assert - ResponseStatusException exception = assertThrows(ResponseStatusException.class, () -> { - sharedApiService.getEntrepreneursByProjectId( - staticAuthorizedProject.getIdProject(), - staticUnauthorizedMail); - }); + ResponseStatusException exception = + assertThrows( + ResponseStatusException.class, + () -> { + sharedApiService.getEntrepreneursByProjectId( + staticAuthorizedProject.getIdProject(), staticUnauthorizedMail); + }); assertEquals(HttpStatus.UNAUTHORIZED, exception.getStatusCode()); } @@ -300,9 +348,9 @@ public class SharedApiServiceTest { void testGetAdminByProjectId_Authorized_Found() { // Arrange: staticAuthorizedProject is created with staticAuthorizedAdmin in BeforeAll // Act - Administrator result = sharedApiService.getAdminByProjectId( - staticAuthorizedProject.getIdProject(), - staticAuthorizedMail); + Administrator result = + sharedApiService.getAdminByProjectId( + staticAuthorizedProject.getIdProject(), staticAuthorizedMail); // Assert assertNotNull(result); @@ -315,32 +363,31 @@ public class SharedApiServiceTest { */ @Test void testGetAdminByProjectId_Unauthorized() { - // Arrange: mockUtilsService configured in BeforeEach - // Act & Assert - ResponseStatusException exception = assertThrows(ResponseStatusException.class, () -> { - sharedApiService.getAdminByProjectId( - staticAuthorizedProject.getIdProject(), - staticUnauthorizedMail); - }); + // Arrange: mockUtilsService configured in BeforeEach + // Act & Assert + ResponseStatusException exception = + assertThrows( + ResponseStatusException.class, + () -> { + sharedApiService.getAdminByProjectId( + staticAuthorizedProject.getIdProject(), staticUnauthorizedMail); + }); - assertEquals(HttpStatus.UNAUTHORIZED, exception.getStatusCode()); - } - - - + 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 + @Test void testGetAppointmentsByProjectId_Authorized_NotFound() { // Arrange: staticAuthorizedProject has no linked section cells or appointments initially // Act - Iterable result = sharedApiService.getAppointmentsByProjectId( - staticAuthorizedProject.getIdProject(), - staticAuthorizedMail); + Iterable result = + sharedApiService.getAppointmentsByProjectId( + staticAuthorizedProject.getIdProject(), staticAuthorizedMail); List resultList = TestUtils.toList(result); @@ -354,16 +401,18 @@ public class SharedApiServiceTest { */ @Test void testGetAppointmentsByProjectId_Unauthorized() { - // Arrange: mockUtilsService configured in BeforeEach - // Act & Assert - ResponseStatusException exception = assertThrows(ResponseStatusException.class, () -> { - sharedApiService.getAppointmentsByProjectId( - staticAuthorizedProject.getIdProject(), - staticUnauthorizedMail); - }); + // Arrange: mockUtilsService configured in BeforeEach + // Act & Assert + ResponseStatusException exception = + assertThrows( + ResponseStatusException.class, + () -> { + sharedApiService.getAppointmentsByProjectId( + staticAuthorizedProject.getIdProject(), staticUnauthorizedMail); + }); - assertEquals(HttpStatus.UNAUTHORIZED, exception.getStatusCode()); - } + assertEquals(HttpStatus.UNAUTHORIZED, exception.getStatusCode()); + } /* * Tests creating a new appointment request when the user is not authorized @@ -372,54 +421,66 @@ public class SharedApiServiceTest { */ @Test void testCreateAppointmentRequest_Unauthorized() { - // Arrange: Create transient appointment linked to a cell in the static *unauthorized* 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"; - String subject = "Discuss Project"; - String reportContent = "Initial Report"; + // Arrange: Create transient appointment linked to a cell in the static *unauthorized* + // 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"; + String subject = "Discuss Project"; + String reportContent = "Initial Report"; - SectionCell linkedCell = sectionCellService.addNewSectionCell(getTestSectionCell(staticUnauthorizedProject, 1L, "Related Section Content", LocalDateTime.now())); + SectionCell linkedCell = + sectionCellService.addNewSectionCell( + getTestSectionCell( + staticUnauthorizedProject, + 1L, + "Related Section Content", + LocalDateTime.now())); - Report newReport = getTestReport(reportContent); - Appointment newAppointment = getTestAppointment(date, time, duration, place, subject, List.of(linkedCell), newReport); + Report newReport = getTestReport(reportContent); + Appointment newAppointment = + getTestAppointment( + date, time, duration, place, subject, List.of(linkedCell), newReport); - // mockUtilsService is configured in BeforeEach to deny staticUnauthorizedMail for staticUnauthorizedProject + // mockUtilsService is configured in BeforeEach to deny staticUnauthorizedMail for + // staticUnauthorizedProject - // Act & Assert - ResponseStatusException exception = assertThrows(ResponseStatusException.class, () -> { - sharedApiService.createAppointmentRequest(newAppointment, staticUnauthorizedMail); // Unauthorized user mail - }); + // Act & Assert + ResponseStatusException exception = + assertThrows( + ResponseStatusException.class, + () -> { + sharedApiService.createAppointmentRequest( + newAppointment, + staticUnauthorizedMail); // Unauthorized user mail + }); - assertEquals(HttpStatus.UNAUTHORIZED, exception.getStatusCode()); - } - - + assertEquals(HttpStatus.UNAUTHORIZED, exception.getStatusCode()); + } /* - _____ _ _ _ - | ___|_ _(_) | ___ __| | - | |_ / _` | | |/ _ \/ _` | - | _| (_| | | | __/ (_| | - |_| \__,_|_|_|\___|\__,_| - _____ _____ ____ _____ - |_ _| ____/ ___|_ _| - | | | _| \___ \ | | - | | | |___ ___) || | - |_| |_____|____/ |_| + _____ _ _ _ + | ___|_ _(_) | ___ __| | + | |_ / _` | | |/ _ \/ _` | + | _| (_| | | | __/ (_| | + |_| \__,_|_|_|\___|\__,_| + _____ _____ ____ _____ + |_ _| ____/ ___|_ _| + | | | _| \___ \ | | + | | | |___ ___) || | + |_| |_____|____/ |_| - */ - + */ - /* these tests fail because of the use of mockito's eq(), - * and since thee instances are technically not the same as - * as the classes used to turn them into persistant data + /* these tests fail because of the use of mockito's eq(), + * and since thee instances are technically not the same as + * as the classes used to turn them into persistant data * (for e.g id are set by DB) so I have to add some equal functions * probably and look at peer tests to see what they have done but for now * I pushed this half-humain code. - */ + */ /* * Tests generating a PDF report for an appointment when the user is authorized @@ -427,21 +488,41 @@ public class SharedApiServiceTest { * Verifies that no authorization exception is thrown. (Note: File I/O is mocked). */ // Tests getPDFReport (Focus on authorization and data retrieval flow) - /*@Test*/ // Commenting out failing test + /*@Test*/ + // Commenting out failing test void testGetPDFReport_Authorized() throws DocumentException, URISyntaxException, IOException { // Arrange: Create a specific appointment linked to the static authorized project - SectionCell cell = sectionCellService.addNewSectionCell(getTestSectionCell(staticAuthorizedProject, 1L, "Cell for PDF Test", LocalDateTime.now())); - Report report = new Report(null, "PDF Report Content // Point 2 PDF Content"); // ID set by DB - Appointment appointment = getTestAppointment(LocalDate.now().plusDays(20), LocalTime.of(14,0), LocalTime.of(0, 45), "Salle PDF", "PDF Subject", List.of(cell), report); + SectionCell cell = + sectionCellService.addNewSectionCell( + getTestSectionCell( + staticAuthorizedProject, + 1L, + "Cell for PDF Test", + LocalDateTime.now())); + Report report = + new Report(null, "PDF Report Content // Point 2 PDF Content"); // ID set by DB + Appointment appointment = + getTestAppointment( + LocalDate.now().plusDays(20), + LocalTime.of(14, 0), + LocalTime.of(0, 45), + "Salle PDF", + "PDF Subject", + List.of(cell), + report); Appointment savedAppointment = appointmentService.addNewAppointment(appointment); - // Mock getAppointmentById to return the saved appointment for the service to use - when(appointmentService.getAppointmentById(eq(savedAppointment.getIdAppointment()))).thenReturn(savedAppointment); - // mockUtilsService is configured in BeforeEach to allow staticAuthorizedMail for staticAuthorizedProject + when(appointmentService.getAppointmentById(eq(savedAppointment.getIdAppointment()))) + .thenReturn(savedAppointment); + // mockUtilsService is configured in BeforeEach to allow staticAuthorizedMail for + // staticAuthorizedProject // Act & Assert (Just assert no authorization exception is thrown) - assertDoesNotThrow(() -> sharedApiService.getPDFReport(savedAppointment.getIdAppointment(), staticAuthorizedMail)); + assertDoesNotThrow( + () -> + sharedApiService.getPDFReport( + savedAppointment.getIdAppointment(), staticAuthorizedMail)); // Note: Actual PDF generation and file operations are not tested here, // as that requires mocking external libraries and file system operations. @@ -452,25 +533,47 @@ public class SharedApiServiceTest { * for the project linked to the appointment's section cell. * Verifies that an Unauthorized ResponseStatusException is thrown. */ - /*@Test*/ // Commenting out failing test + /*@Test*/ + // Commenting out failing test void testGetPDFReport_Unauthorized() { - // Arrange: Create a specific appointment linked to the static *unauthorized* project - SectionCell cell = sectionCellService.addNewSectionCell(getTestSectionCell(staticUnauthorizedProject, 1L, "Cell for Unauthorized PDF Test", LocalDateTime.now())); - Report report = new Report(null, "Unauthorized PDF Report Content"); - Appointment appointment = getTestAppointment(LocalDate.now().plusDays(21), LocalTime.of(15,0), LocalTime.of(0, 30), "Salle Unauthorized PDF", "Unauthorized PDF Subject", List.of(cell), report); - Appointment savedAppointment = appointmentService.addNewAppointment(appointment); + // Arrange: Create a specific appointment linked to the static *unauthorized* project + SectionCell cell = + sectionCellService.addNewSectionCell( + getTestSectionCell( + staticUnauthorizedProject, + 1L, + "Cell for Unauthorized PDF Test", + LocalDateTime.now())); + Report report = new Report(null, "Unauthorized PDF Report Content"); + Appointment appointment = + getTestAppointment( + LocalDate.now().plusDays(21), + LocalTime.of(15, 0), + LocalTime.of(0, 30), + "Salle Unauthorized PDF", + "Unauthorized PDF Subject", + List.of(cell), + report); + Appointment savedAppointment = appointmentService.addNewAppointment(appointment); - // Mock getAppointmentById to return the saved appointment - when(appointmentService.getAppointmentById(eq(savedAppointment.getIdAppointment()))).thenReturn(savedAppointment); - // mockUtilsService is configured in BeforeEach to DENY staticUnauthorizedMail for staticUnauthorizedProject + // Mock getAppointmentById to return the saved appointment + when(appointmentService.getAppointmentById(eq(savedAppointment.getIdAppointment()))) + .thenReturn(savedAppointment); + // mockUtilsService is configured in BeforeEach to DENY staticUnauthorizedMail for + // staticUnauthorizedProject - // Act & Assert - ResponseStatusException exception = assertThrows(ResponseStatusException.class, () -> { - sharedApiService.getPDFReport(savedAppointment.getIdAppointment(), staticUnauthorizedMail); // Unauthorized user mail - }); + // Act & Assert + ResponseStatusException exception = + assertThrows( + ResponseStatusException.class, + () -> { + sharedApiService.getPDFReport( + savedAppointment.getIdAppointment(), + staticUnauthorizedMail); // Unauthorized user mail + }); - assertEquals(HttpStatus.UNAUTHORIZED, exception.getStatusCode()); - } + assertEquals(HttpStatus.UNAUTHORIZED, exception.getStatusCode()); + } /* * Tests creating a new appointment request when the user is authorized @@ -478,7 +581,8 @@ public class SharedApiServiceTest { * Verifies that the appointment and its relationships are saved correctly in the database. */ // Tests createAppointmentRequest - /*@Test*/ // Commenting out failing test + /*@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"); @@ -488,29 +592,53 @@ public class SharedApiServiceTest { String subject = "Discuss Project Integrated"; String reportContent = "Initial Report Integrated"; - SectionCell linkedCell = sectionCellService.addNewSectionCell(getTestSectionCell(staticAuthorizedProject, 1L, "Related Section Content Integrated", LocalDateTime.now())); + 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); + Appointment newAppointment = + getTestAppointment( + date, time, duration, place, subject, List.of(linkedCell), newReport); - // mockUtilsService is configured in BeforeEach to allow staticAuthorizedMail for staticAuthorizedProject + // 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)); + assertDoesNotThrow( + () -> + sharedApiService.createAppointmentRequest( + newAppointment, staticAuthorizedMail)); - // Assert: Retrieve the appointment from the DB and verify it and its relationships were saved + // 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 projectCells = sectionCellService.getSectionCellsByProject(staticAuthorizedProject, 1L); // Fetch relevant cells - List projectAppointmentsList = new ArrayList<>(); - projectCells.forEach(cell -> projectAppointmentsList.addAll(sectionCellService.getAppointmentsBySectionCellId(cell.getIdSectionCell()))); // Get appointments for those cells + Iterable projectCells = + sectionCellService.getSectionCellsByProject( + staticAuthorizedProject, 1L); // Fetch relevant cells + List projectAppointmentsList = new ArrayList<>(); + projectCells.forEach( + cell -> + projectAppointmentsList.addAll( + sectionCellService.getAppointmentsBySectionCellId( + cell + .getIdSectionCell()))); // Get appointments for + // those cells - Optional createdAppointmentOpt = projectAppointmentsList.stream() - .filter(a -> a.getAppointmentDate().equals(date) && - a.getAppointmentTime().equals(time) && - a.getAppointmentPlace().equals(place) && - a.getAppointmentSubject().equals(subject)) - .findFirst(); + Optional 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(); @@ -518,40 +646,74 @@ public class SharedApiServiceTest { assertNotNull(createdAppointment.getAppointmentReport()); assertEquals(reportContent, createdAppointment.getAppointmentReport().getReportContent()); // FIX: Corrected bidirectional link check - assertEquals(createdAppointment.getIdAppointment(), createdAppointment.getAppointmentReport().getAppointmentReport().getIdAppointment()); + assertEquals( + createdAppointment.getIdAppointment(), + createdAppointment + .getAppointmentReport() + .getAppointmentReport() + .getIdAppointment()); assertEquals(1, createdAppointment.getAppointmentListSectionCell().size()); - assertTrue(createdAppointment.getAppointmentListSectionCell().stream().anyMatch(sc -> sc.getIdSectionCell().equals(linkedCell.getIdSectionCell()))); + assertTrue( + createdAppointment.getAppointmentListSectionCell().stream() + .anyMatch( + sc -> sc.getIdSectionCell().equals(linkedCell.getIdSectionCell()))); - List appointmentsLinkedToCell = TestUtils.toList(sectionCellService.getAppointmentsBySectionCellId(linkedCell.getIdSectionCell())); - assertTrue(appointmentsLinkedToCell.stream().anyMatch(a -> a.getIdAppointment().equals(createdAppointment.getIdAppointment()))); + List 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) --- + // --- Test Methods (Use static data from @BeforeAll where possible) --- /* * Tests retrieving section cells for a specific project and section ID before a given date * when the user is authorized and matching cells exist. * Verifies that only the correct cells are returned. */ - /*@Test*/ // Commenting out failing test + /*@Test*/ + // Commenting out failing test void testGetSectionCells_Authorized_Found() { // Arrange: Create specific SectionCells for this test scenario Long targetSectionId = 1L; LocalDateTime dateFilter = LocalDateTime.now().plusDays(1); - sectionCellService.addNewSectionCell(getTestSectionCell(staticAuthorizedProject, targetSectionId, "Old Content", LocalDateTime.now().minusDays(2))); - SectionCell recentCell = sectionCellService.addNewSectionCell(getTestSectionCell(staticAuthorizedProject, targetSectionId, "Recent Content", LocalDateTime.now().minusDays(1))); - sectionCellService.addNewSectionCell(getTestSectionCell(staticAuthorizedProject, 2L, "Other Section", LocalDateTime.now())); - sectionCellService.addNewSectionCell(getTestSectionCell(staticAuthorizedProject, targetSectionId, "Future Content", LocalDateTime.now().plusDays(2))); + sectionCellService.addNewSectionCell( + getTestSectionCell( + staticAuthorizedProject, + targetSectionId, + "Old Content", + LocalDateTime.now().minusDays(2))); + SectionCell recentCell = + sectionCellService.addNewSectionCell( + getTestSectionCell( + staticAuthorizedProject, + targetSectionId, + "Recent Content", + LocalDateTime.now().minusDays(1))); + sectionCellService.addNewSectionCell( + getTestSectionCell( + staticAuthorizedProject, 2L, "Other Section", LocalDateTime.now())); + sectionCellService.addNewSectionCell( + getTestSectionCell( + staticAuthorizedProject, + targetSectionId, + "Future Content", + LocalDateTime.now().plusDays(2))); // Act - Iterable result = sharedApiService.getSectionCells( - staticAuthorizedProject.getIdProject(), // Use static project ID - targetSectionId, - dateFilter.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")), - staticAuthorizedMail); // Use static authorized mail + Iterable result = + sharedApiService.getSectionCells( + staticAuthorizedProject.getIdProject(), // Use static project ID + targetSectionId, + dateFilter.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")), + staticAuthorizedMail); // Use static authorized mail List resultList = TestUtils.toList(result); @@ -566,50 +728,93 @@ public class SharedApiServiceTest { * Verifies that only the latest version of each referenced cell is returned. */ // Tests getAllSectionCells - /*@Test*/ // Commenting out failing test + /*@Test*/ + // Commenting out failing test void testGetAllSectionCells_Authorized_FoundLatest() { // Arrange: Create specific SectionCells for this test Long refId1 = 101L; Long refId2 = 102L; - SectionCell tempOldCell1 = getTestSectionCell(staticAuthorizedProject, 1L, "Ref1 Old", LocalDateTime.now().minusDays(3)); + SectionCell tempOldCell1 = + getTestSectionCell( + staticAuthorizedProject, 1L, "Ref1 Old", LocalDateTime.now().minusDays(3)); tempOldCell1.setIdReference(refId1); final SectionCell oldCell1 = sectionCellService.addNewSectionCell(tempOldCell1); - SectionCell tempNewerCell1 = getTestSectionCell(staticAuthorizedProject, 1L, "Ref1 Newer", LocalDateTime.now().minusDays(2)); + SectionCell tempNewerCell1 = + getTestSectionCell( + staticAuthorizedProject, + 1L, + "Ref1 Newer", + LocalDateTime.now().minusDays(2)); tempNewerCell1.setIdReference(refId1); final SectionCell newerCell1 = sectionCellService.addNewSectionCell(tempNewerCell1); - SectionCell tempOldCell2 = getTestSectionCell(staticAuthorizedProject, 2L, "Ref2 Old", LocalDateTime.now().minusDays(1)); - tempOldCell2.setIdReference(refId2); - final SectionCell oldCell2 = sectionCellService.addNewSectionCell(tempOldCell2); + SectionCell tempOldCell2 = + getTestSectionCell( + staticAuthorizedProject, 2L, "Ref2 Old", LocalDateTime.now().minusDays(1)); + tempOldCell2.setIdReference(refId2); + final SectionCell oldCell2 = sectionCellService.addNewSectionCell(tempOldCell2); - SectionCell tempNewerCell2 = getTestSectionCell(staticAuthorizedProject, 2L, "Ref2 Newer", LocalDateTime.now()); - tempNewerCell2.setIdReference(refId2); - final SectionCell newerCell2 = sectionCellService.addNewSectionCell(tempNewerCell2); + SectionCell tempNewerCell2 = + getTestSectionCell(staticAuthorizedProject, 2L, "Ref2 Newer", LocalDateTime.now()); + tempNewerCell2.setIdReference(refId2); + final SectionCell newerCell2 = sectionCellService.addNewSectionCell(tempNewerCell2); - Project otherProject = projectService.addNewProject(getTestProject("other_project_for_cell_test", administratorService.addAdministrator(getTestAdmin("other_admin_cell_test")))); - SectionCell tempOtherProjectCell = getTestSectionCell(otherProject, 1L, "Other Project Cell", LocalDateTime.now()); + Project otherProject = + projectService.addNewProject( + getTestProject( + "other_project_for_cell_test", + administratorService.addAdministrator( + getTestAdmin("other_admin_cell_test")))); + SectionCell tempOtherProjectCell = + getTestSectionCell(otherProject, 1L, "Other Project Cell", LocalDateTime.now()); tempOtherProjectCell.setIdReference(103L); - final SectionCell otherProjectCell = sectionCellService.addNewSectionCell(tempOtherProjectCell); - + final SectionCell otherProjectCell = + sectionCellService.addNewSectionCell(tempOtherProjectCell); // Act - Iterable result = sharedApiService.getAllSectionCells( - staticAuthorizedProject.getIdProject(), // Use static project ID - staticAuthorizedMail); // Use static authorized mail + Iterable result = + sharedApiService.getAllSectionCells( + staticAuthorizedProject.getIdProject(), // Use static project ID + staticAuthorizedMail); // Use static authorized mail List resultList = TestUtils.toList(result); // Assert assertEquals(2, resultList.size()); // Expect 2 cells (one per idReference) - assertTrue(resultList.stream().anyMatch(cell -> cell.getIdSectionCell().equals(newerCell1.getIdSectionCell()))); - assertTrue(resultList.stream().anyMatch(cell -> cell.getIdSectionCell().equals(newerCell2.getIdSectionCell()))); + assertTrue( + resultList.stream() + .anyMatch( + cell -> + cell.getIdSectionCell() + .equals(newerCell1.getIdSectionCell()))); + assertTrue( + resultList.stream() + .anyMatch( + cell -> + cell.getIdSectionCell() + .equals(newerCell2.getIdSectionCell()))); - assertFalse(resultList.stream().anyMatch(cell -> cell.getIdSectionCell().equals(oldCell1.getIdSectionCell()))); - assertFalse(resultList.stream().anyMatch(cell -> cell.getIdSectionCell().equals(oldCell2.getIdSectionCell()))); - assertFalse(resultList.stream().anyMatch(cell -> cell.getIdSectionCell().equals(otherProjectCell.getIdSectionCell()))); + assertFalse( + resultList.stream() + .anyMatch( + cell -> + cell.getIdSectionCell() + .equals(oldCell1.getIdSectionCell()))); + assertFalse( + resultList.stream() + .anyMatch( + cell -> + cell.getIdSectionCell() + .equals(oldCell2.getIdSectionCell()))); + assertFalse( + resultList.stream() + .anyMatch( + cell -> + cell.getIdSectionCell() + .equals(otherProjectCell.getIdSectionCell()))); } /* @@ -618,28 +823,37 @@ public class SharedApiServiceTest { * Verifies that the correct entrepreneurs are returned. */ // Tests getEntrepreneursByProjectId - /*@Test*/ // Commenting out failing test + /*@Test*/ + // Commenting out failing test void testGetEntrepreneursByProjectId_Authorized_Found() { // Arrange: Create entrepreneur and link to static project for this test - Entrepreneur linkedEntrepreneur = entrepreneurService.addEntrepreneur(getTestEntrepreneur("linked_entrepreneur_test")); + Entrepreneur linkedEntrepreneur = + entrepreneurService.addEntrepreneur( + getTestEntrepreneur("linked_entrepreneur_test")); // Fetch the static project to update its list - Project projectToUpdate = projectService.getProjectById(staticAuthorizedProject.getIdProject()); + Project projectToUpdate = + projectService.getProjectById(staticAuthorizedProject.getIdProject()); projectToUpdate.updateListEntrepreneurParticipation(linkedEntrepreneur); projectService.addNewProject(projectToUpdate); // Save the updated project - Entrepreneur otherEntrepreneur = entrepreneurService.addEntrepreneur(getTestEntrepreneur("other_entrepreneur_test")); + Entrepreneur otherEntrepreneur = + entrepreneurService.addEntrepreneur(getTestEntrepreneur("other_entrepreneur_test")); // Act - Iterable result = sharedApiService.getEntrepreneursByProjectId( - staticAuthorizedProject.getIdProject(), - staticAuthorizedMail); + Iterable result = + sharedApiService.getEntrepreneursByProjectId( + staticAuthorizedProject.getIdProject(), staticAuthorizedMail); List resultList = TestUtils.toList(result); // Assert assertEquals(1, resultList.size()); - assertTrue(resultList.stream().anyMatch(e -> e.getIdUser().equals(linkedEntrepreneur.getIdUser()))); - assertFalse(resultList.stream().anyMatch(e -> e.getIdUser().equals(otherEntrepreneur.getIdUser()))); + assertTrue( + resultList.stream() + .anyMatch(e -> e.getIdUser().equals(linkedEntrepreneur.getIdUser()))); + assertFalse( + resultList.stream() + .anyMatch(e -> e.getIdUser().equals(otherEntrepreneur.getIdUser()))); } /* @@ -648,38 +862,91 @@ public class SharedApiServiceTest { * Verifies that the correct appointments are returned. */ // Tests getAppointmentsByProjectId - /*@Test*/ // Commenting out failing test + /*@Test*/ + // Commenting out failing test void testGetAppointmentsByProjectId_Authorized_Found() { // Arrange: Create specific SectionCells and Appointments for this test - SectionCell cell1 = sectionCellService.addNewSectionCell(getTestSectionCell(staticAuthorizedProject, 1L, "Cell 1 Test", LocalDateTime.now())); - SectionCell cell2 = sectionCellService.addNewSectionCell(getTestSectionCell(staticAuthorizedProject, 2L, "Cell 2 Test", LocalDateTime.now())); - Project otherProject = projectService.addNewProject(getTestProject("other_project_app_test", administratorService.addAdministrator(getTestAdmin("other_admin_app_test")))); - SectionCell otherProjectCell = sectionCellService.addNewSectionCell(getTestSectionCell(otherProject, 1L, "Other Project Cell App Test", LocalDateTime.now())); + SectionCell cell1 = + sectionCellService.addNewSectionCell( + getTestSectionCell( + staticAuthorizedProject, 1L, "Cell 1 Test", LocalDateTime.now())); + SectionCell cell2 = + sectionCellService.addNewSectionCell( + getTestSectionCell( + staticAuthorizedProject, 2L, "Cell 2 Test", LocalDateTime.now())); + Project otherProject = + projectService.addNewProject( + getTestProject( + "other_project_app_test", + administratorService.addAdministrator( + getTestAdmin("other_admin_app_test")))); + SectionCell otherProjectCell = + sectionCellService.addNewSectionCell( + getTestSectionCell( + otherProject, + 1L, + "Other Project Cell App Test", + LocalDateTime.now())); - Appointment app1 = getTestAppointment(LocalDate.now().plusDays(10), LocalTime.NOON, LocalTime.of(0, 30), "Place 1 App Test", "Subject 1 App Test", List.of(cell1), null); + Appointment app1 = + getTestAppointment( + LocalDate.now().plusDays(10), + LocalTime.NOON, + LocalTime.of(0, 30), + "Place 1 App Test", + "Subject 1 App Test", + List.of(cell1), + null); Appointment savedApp1 = appointmentService.addNewAppointment(app1); - Appointment app2 = getTestAppointment(LocalDate.now().plusDays(11), LocalTime.NOON.plusHours(1), LocalTime.of(1, 0), "Place 2 App Test", "Subject 2 App Test", List.of(cell1, cell2), null); + Appointment app2 = + getTestAppointment( + LocalDate.now().plusDays(11), + LocalTime.NOON.plusHours(1), + LocalTime.of(1, 0), + "Place 2 App Test", + "Subject 2 App Test", + List.of(cell1, cell2), + null); Appointment savedApp2 = appointmentService.addNewAppointment(app2); - Appointment otherApp = getTestAppointment(LocalDate.now().plusDays(12), LocalTime.MIDNIGHT, LocalTime.of(0, 15), "Other Place App Test", "Other Subject App Test", List.of(otherProjectCell), null); + Appointment otherApp = + getTestAppointment( + LocalDate.now().plusDays(12), + LocalTime.MIDNIGHT, + LocalTime.of(0, 15), + "Other Place App Test", + "Other Subject App Test", + List.of(otherProjectCell), + null); appointmentService.addNewAppointment(otherApp); - // Act - Iterable result = sharedApiService.getAppointmentsByProjectId( - staticAuthorizedProject.getIdProject(), // Use static project ID - staticAuthorizedMail); // Use static authorized mail + Iterable result = + sharedApiService.getAppointmentsByProjectId( + staticAuthorizedProject.getIdProject(), // Use static project ID + staticAuthorizedMail); // Use static authorized mail List resultList = TestUtils.toList(result); // Assert assertEquals(2, resultList.size()); - assertTrue(resultList.stream().anyMatch(a -> a.getIdAppointment().equals(savedApp1.getIdAppointment()))); - assertTrue(resultList.stream().anyMatch(a -> a.getIdAppointment().equals(savedApp2.getIdAppointment()))); - - assertFalse(resultList.stream().anyMatch(a -> a.getIdAppointment().equals(otherApp.getIdAppointment()))); // Ensure appointment from other project is not included + assertTrue( + resultList.stream() + .anyMatch(a -> a.getIdAppointment().equals(savedApp1.getIdAppointment()))); + assertTrue( + resultList.stream() + .anyMatch(a -> a.getIdAppointment().equals(savedApp2.getIdAppointment()))); + assertFalse( + resultList.stream() + .anyMatch( + a -> + a.getIdAppointment() + .equals( + otherApp + .getIdAppointment()))); // Ensure + // appointment from other project is not included } -} \ No newline at end of file +}