From cbef042e97533960af7dc5b30c227eb8063a25af Mon Sep 17 00:00:00 2001 From: MAILLAL Anas Date: Tue, 29 Apr 2025 12:48:29 +0200 Subject: [PATCH] found that idReference Incrementing when it shoudn't still looking for a fix, and fixed some sectioncell fetching logic in SectionCellService.java --- .../myinpulse/service/SharedApiService.java | 2 +- .../service/database/SectionCellService.java | 35 ++++ .../myinpulse/SharedApiServiceTest.java | 181 +++++++++++------- 3 files changed, 151 insertions(+), 67 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 769a6cc..c899534 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.getSectionCellsByProjectAndSectionIdBeforeDate( + return this.sectionCellService.getLatestSectionCellsByIdReferenceBeforeDate( 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 843bd0b..d1a444d 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,7 +14,9 @@ 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 @@ -132,4 +134,37 @@ 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 22c125b..281870c 100644 --- a/MyINPulse-back/src/test/java/enseirb/myinpulse/SharedApiServiceTest.java +++ b/MyINPulse-back/src/test/java/enseirb/myinpulse/SharedApiServiceTest.java @@ -123,20 +123,6 @@ 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) --- @@ -176,6 +162,17 @@ 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, @@ -307,6 +304,107 @@ 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."); + + } + /* * _____ _ ____ _ ____ _ _ ____ * |_ _|__ ___| |_ / ___| ___| |_| _ \ _ __ ___ (_) ___ ___| |_| __ ) _ _ @@ -590,6 +688,8 @@ public class SharedApiServiceTest { assertEquals(HttpStatus.UNAUTHORIZED, exception.getStatusCode()); } + + /* _____ _ _ _ @@ -612,58 +712,7 @@ 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