From 1f91ab72d8ef82a5350328ea06dc97de8a5ee729 Mon Sep 17 00:00:00 2001 From: MAILLAL Anas Date: Tue, 29 Apr 2025 13:00:58 +0200 Subject: [PATCH] This reverts commit cbef042e97533960af7dc5b30c227eb8063a25af. I was working on front and back and pushed stuff to the wrong end --- .../myinpulse/service/SharedApiService.java | 2 +- .../service/database/SectionCellService.java | 35 ---- .../myinpulse/SharedApiServiceTest.java | 181 +++++++----------- 3 files changed, 67 insertions(+), 151 deletions(-) diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/SharedApiService.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/SharedApiService.java index c899534..769a6cc 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/SharedApiService.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/SharedApiService.java @@ -79,7 +79,7 @@ public class SharedApiService { LocalDateTime dateTime = LocalDateTime.parse(date, formatter); Project project = this.projectService.getProjectById(projectId); - return this.sectionCellService.getLatestSectionCellsByIdReferenceBeforeDate( + return this.sectionCellService.getSectionCellsByProjectAndSectionIdBeforeDate( project, sectionId, dateTime); } diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/SectionCellService.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/SectionCellService.java index d1a444d..843bd0b 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/SectionCellService.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/SectionCellService.java @@ -14,9 +14,7 @@ import org.springframework.stereotype.Service; import org.springframework.web.server.ResponseStatusException; import java.time.LocalDateTime; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Optional; @Service @@ -134,37 +132,4 @@ public class SectionCellService { return sectionCellRepository.findByProjectSectionCellAndSectionIdAndModificationDateBefore( project, sectionId, date); } - - public Iterable getLatestSectionCellsByIdReferenceBeforeDate( - Project project, long sectionId, LocalDateTime date) { - - // 1. Fetch ALL relevant SectionCells modified before the date - Iterable allMatchingCells = - sectionCellRepository.findByProjectSectionCellAndSectionIdAndModificationDateBefore( - project, sectionId, date); - - // 2. Find the latest for each idReference - Map latestCellsByIdReference = new HashMap<>(); - - for (SectionCell cell : allMatchingCells) { - Long idReference = cell.getIdReference(); - - // Check if we've seen this idReference before - if (latestCellsByIdReference.containsKey(idReference)) { - // If yes, compare modification dates - SectionCell existingLatest = latestCellsByIdReference.get(idReference); - - // If the current cell is more recent, update the map - if (cell.getModificationDate().isAfter(existingLatest.getModificationDate())) { - latestCellsByIdReference.put(idReference, cell); - } - } else { - // If this is the first time we see this idReference, add it to the map - latestCellsByIdReference.put(idReference, cell); - } - } - - // 3. Return the collection of the latest cells (the values from the map) - return latestCellsByIdReference.values(); - } } diff --git a/MyINPulse-back/src/test/java/enseirb/myinpulse/SharedApiServiceTest.java b/MyINPulse-back/src/test/java/enseirb/myinpulse/SharedApiServiceTest.java index 281870c..22c125b 100644 --- a/MyINPulse-back/src/test/java/enseirb/myinpulse/SharedApiServiceTest.java +++ b/MyINPulse-back/src/test/java/enseirb/myinpulse/SharedApiServiceTest.java @@ -123,6 +123,20 @@ public class SharedApiServiceTest { 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)); + // // 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 + } // --- Helper Methods (Can remain non-static or static as needed) --- @@ -162,17 +176,6 @@ public class SharedApiServiceTest { return sectionCell; } - private static SectionCell getTestSectionCell( - Project project, Long sectionId, String content, LocalDateTime date, Long refrenceId) { - SectionCell sectionCell = new SectionCell(); - sectionCell.setProjectSectionCell(project); - sectionCell.setSectionId(sectionId); - sectionCell.setContentSectionCell(content); - sectionCell.setModificationDate(date); - sectionCell.setIdReference(refrenceId); - return sectionCell; - } - private static Appointment getTestAppointment( LocalDate date, LocalTime time, @@ -304,107 +307,6 @@ public class SharedApiServiceTest { assertEquals(HttpStatus.UNAUTHORIZED, exception.getStatusCode()); } - /* - * 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 - void testGetSectionCells_Authorized_Found() { - Long targetSectionId = 1L; - // Set a date filter slightly in the future so our "latest before" cell is included - LocalDateTime dateFilter = LocalDateTime.now().plusMinutes(5); - - // Creating versions of the SAME SectionCell (share the same idReference) - - // the first version. This will get a GENERATED idReference. - SectionCell firstVersion = getTestSectionCell( - staticAuthorizedProject, - targetSectionId, - "Content V1 (Oldest)", - LocalDateTime.now().minusDays(3) // Oldest date - ); - sectionCellService.addNewSectionCell(firstVersion); - - Long sharedIdReference = firstVersion.getIdReference(); - assertNotNull(sharedIdReference, "idReference should be generated after saving the first version"); - System.out.println("Generated sharedIdReference: " + sharedIdReference); - - - // Create subsequent versions and MANUALLY set the SAME idReference. - // These represent updates to the cell identified by sharedIdReference. - - SectionCell middleVersion = getTestSectionCell( - staticAuthorizedProject, - targetSectionId, - "Content V2 (Middle)", - LocalDateTime.now().minusDays(2), // Middle date, before filter - sharedIdReference - ); - sectionCellService.addNewSectionCell(middleVersion); - - - SectionCell latestBeforeFilter = getTestSectionCell( - staticAuthorizedProject, - targetSectionId, - "Content V3 (Latest Before Filter)", - LocalDateTime.now().minusDays(1), // Latest date before filter - sharedIdReference - ); - sectionCellService.addNewSectionCell(latestBeforeFilter); - - - SectionCell futureVersion = getTestSectionCell( - staticAuthorizedProject, - targetSectionId, - "Content V4 (Future - Should Be Excluded)", - LocalDateTime.now().plusDays(1), // Date is AFTER the filter - sharedIdReference - ); - sectionCellService.addNewSectionCell(futureVersion); - - - // --- Create other SectionCells that should NOT be included (different sectionId or project) --- - - // Cell in a different section ID - sectionCellService.addNewSectionCell( - getTestSectionCell( - staticAuthorizedProject, - 99L, // Different sectionId - "Content in Different Section", - LocalDateTime.now() - ) - ); - - // 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 - - List resultList = TestUtils.toList(result); - - // Assert - - assertEquals(latestBeforeFilter.getIdReference(), resultList.get(0).getIdReference(), "The 0"); - assertEquals(latestBeforeFilter.getIdReference(), resultList.get(1).getIdReference(), "The 1"); - assertEquals(latestBeforeFilter.getIdReference(), resultList.get(2).getIdReference(), "The 2"); - - assertEquals(1, resultList.size()); - // Verify that the returned cell is the 'latestBeforeFilter' cell - // Comparing by idSectionCell is a good way to verify the exact entity - assertEquals(latestBeforeFilter.getIdSectionCell(), resultList.get(0).getIdSectionCell(), - "The returned SectionCell should be the one with the latest modification date before the filter."); - - // Also assert the idReference and content - assertEquals(sharedIdReference, resultList.get(0).getIdReference(), "The returned cell should have the shared idReference."); - assertEquals("Content V3 (Latest Before Filter)", resultList.get(0).getContentSectionCell(), "The returned cell should have the correct content."); - - } - /* * _____ _ ____ _ ____ _ _ ____ * |_ _|__ ___| |_ / ___| ___| |_| _ \ _ __ ___ (_) ___ ___| |_| __ ) _ _ @@ -688,8 +590,6 @@ public class SharedApiServiceTest { assertEquals(HttpStatus.UNAUTHORIZED, exception.getStatusCode()); } - - /* _____ _ _ _ @@ -712,7 +612,58 @@ public class SharedApiServiceTest { * probably and look at peer tests to see what they have done but for now * I pushed this half-human code. */ - + + // --- 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 + 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))); + + // 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 + + List resultList = TestUtils.toList(result); + + // Assert + assertEquals(1, resultList.size()); + assertEquals(recentCell.getIdSectionCell(), resultList.get(0).getIdSectionCell()); + } /* * Tests retrieving the most recent section cell for each unique idReference