From 602945773536966f5d02c3c06e0a7e2e12f9823b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Le=20Lez?= Date: Wed, 16 Apr 2025 10:36:59 +0200 Subject: [PATCH] fix: more tests working, still need fixes --- .../enseirb/myinpulse/model/SectionCell.java | 15 ++++ .../service/EntrepreneurApiService.java | 48 +++++++++++- .../myinpulse/service/SharedApiService.java | 40 ++++++++++ .../service/database/EntrepreneurService.java | 4 - .../service/database/SectionCellService.java | 6 ++ .../myinpulse/EntrepreneurApiServiceTest.java | 73 ++++++++++++++++--- 6 files changed, 168 insertions(+), 18 deletions(-) diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/SectionCell.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/SectionCell.java index 2bd1888..fd7bee9 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/SectionCell.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/SectionCell.java @@ -2,6 +2,9 @@ package enseirb.myinpulse.model; import jakarta.persistence.*; +import org.hibernate.annotations.Generated; +import org.hibernate.generator.EventType; + import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; @@ -20,6 +23,10 @@ public class SectionCell { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long idSectionCell; + @Column(columnDefinition = "serial") + @Generated(event = EventType.INSERT) + private Long idReference; + @Column() private long sectionId; private String contentSectionCell; @@ -56,6 +63,14 @@ public class SectionCell { this.idSectionCell = idSectionCell; } + public Long getIdReference() { + return idReference; + } + + public void setIdReference(Long idReference) { + this.idReference = idReference; + } + public Long getSectionId() { return sectionId; } diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/EntrepreneurApiService.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/EntrepreneurApiService.java index fe0dc71..b33c2d1 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/EntrepreneurApiService.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/EntrepreneurApiService.java @@ -17,6 +17,8 @@ import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.web.server.ResponseStatusException; +import java.time.LocalDateTime; + @Service public class EntrepreneurApiService { @@ -64,7 +66,28 @@ public class EntrepreneurApiService { mail, sectionCellId, this.sectionCellService.getProjectId(sectionCellId)); - sectionCellService.updateSectionCell(sectionCellId, content, null, null, null); + SectionCell newSectionCell = + new SectionCell( + null, + sectionCell.getSectionId(), + content, + LocalDateTime.now(), + sectionCell.getProjectSectionCell()); + newSectionCell.setIdReference(sectionCell.getIdReference()); + sectionCell + .getAppointmentSectionCell() + .forEach( + appointment -> { + newSectionCell.updateAppointmentSectionCell(appointment); + }); + sectionCell + .getListAnnotation() + .forEach( + annotation -> { + newSectionCell.updateListAnnotation(annotation); + }); + this.addSectionCell(newSectionCell, mail); + // sectionCellService.updateSectionCell(sectionCellId, content, null, null, null); } public void removeSectionCell(Long sectionCellId, String mail) { @@ -89,7 +112,12 @@ public class EntrepreneurApiService { mail, sectionCellId, this.sectionCellService.getProjectId(sectionCellId)); - sectionCellService.removeSectionCellById(sectionCellId); + SectionCell removedSectionCell = new SectionCell(null, -1L, "", LocalDateTime.now(), null); + removedSectionCell.setIdReference(editSectionCell.getIdReference()); + sectionCellService.addNewSectionCell(removedSectionCell); + projectService.updateProjectListSectionCell( + sectionCellService.getProjectId(sectionCellId), removedSectionCell); + // sectionCellService.removeSectionCellById(sectionCellId); } public void addSectionCell(SectionCell sectionCell, String mail) { @@ -98,6 +126,11 @@ public class EntrepreneurApiService { throw new ResponseStatusException( HttpStatus.BAD_REQUEST, "La cellule de section fournie est vide"); } + if (sectionCell.getSectionId() == -1) { + System.err.println("Trying to create an illegal section cell"); + throw new ResponseStatusException( + HttpStatus.BAD_REQUEST, "La cellule de section fournie n'est pas valide"); + } if (!utilsService.isAllowedToCheckProject( mail, this.sectionCellService.getProjectId(sectionCell.getIdSectionCell()))) { logger.warn( @@ -112,7 +145,9 @@ public class EntrepreneurApiService { mail, sectionCell.getIdSectionCell(), this.sectionCellService.getProjectId(sectionCell.getIdSectionCell())); - SectionCell newSectionCell = sectionCellService.addNewSectionCell(sectionCell); + SectionCell newSectionCell = + sectionCellService.addNewSectionCell( + sectionCell); // if here, logger fails cause id is null (not added yet) newSectionCell.getProjectSectionCell().updateListSectionCell(newSectionCell); newSectionCell .getAppointmentSectionCell() @@ -135,7 +170,14 @@ public class EntrepreneurApiService { } logger.info("User {} created a new project with id {}", mail, project.getIdProject()); project.setProjectStatus(PENDING); + project.setEntrepreneurProposed((Entrepreneur) this.userService.getUserByEmail(mail)); projectService.addNewProject(project); + project.getProjectAdministrator().updateListProject(project); + project.getEntrepreneurProposed().setProjectProposed(project); + project.getListEntrepreneurParticipation() + .forEach(entrepreneur -> entrepreneur.setProjectParticipation(project)); + project.getListSectionCell() + .forEach(sectionCell -> sectionCell.setProjectSectionCell(project)); } public void createAccount(Entrepreneur e) { 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 23ad616..3c8585c 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/SharedApiService.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/SharedApiService.java @@ -25,6 +25,7 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; @Service public class SharedApiService { @@ -73,6 +74,45 @@ public class SharedApiService { project, sectionId, dateTime); } + // Retrieve all up to date (for every sectionId) sectionCells of a project + public Iterable getAllSectionCells(long projectId, String mail) { + if (!utilsService.isAllowedToCheckProject(mail, projectId)) { + logger.warn( + "User {} tried to check section cells of the project {} but is not allowed to.", + mail, + projectId); + throw new ResponseStatusException( + HttpStatus.UNAUTHORIZED, "You're not allowed to check this project"); + } + + Project project = this.projectService.getProjectById(projectId); + List allSectionCells = new ArrayList(); + project.getListSectionCell() + .forEach( + projectCell -> { + AtomicBoolean sameReferenceId = + new AtomicBoolean(false); // side effect lambdas + allSectionCells.forEach( + selectedCell -> { + if (projectCell + .getIdReference() + .equals(selectedCell.getIdReference())) { + sameReferenceId.set(true); + if (projectCell + .getModificationDate() + .isAfter(selectedCell.getModificationDate())) { + allSectionCells.remove(selectedCell); + allSectionCells.add(projectCell); + } + } + }); + if (!sameReferenceId.get()) { + allSectionCells.add(projectCell); + } + }); + return allSectionCells; + } + // TODO: test public Iterable getEntrepreneursByProjectId(long projectId, String mail) { if (!utilsService.isAllowedToCheckProject(mail, projectId)) { diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/EntrepreneurService.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/EntrepreneurService.java index f2dc225..67dbddf 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/EntrepreneurService.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/EntrepreneurService.java @@ -62,11 +62,7 @@ public class EntrepreneurService { public void updateEntrepreneurProjectParticipation( long idEntrepreneur, Project projectParticipation) { - System.out.println("expected"); - System.out.println(getEntrepreneurById(idEntrepreneur)); Entrepreneur entrepreneur = getEntrepreneurById(idEntrepreneur); - System.out.println("test"); - System.out.println(entrepreneur); entrepreneur.setProjectParticipation(projectParticipation); this.entrepreneurRepository.save(entrepreneur); } 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 3d052d6..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 @@ -51,6 +51,12 @@ public class SectionCellService { this.sectionCellRepository.deleteById(id); } + public void updateSectionCellReferenceId(Long idSectionCell, Long referenceId) { + SectionCell sectionCell = this.getSectionCellById(idSectionCell); + sectionCell.setIdReference(referenceId); + this.sectionCellRepository.save(sectionCell); + } + public void updateSectionCellContent(long idSectionCell, String content) { SectionCell sectionCell = getSectionCellById(idSectionCell); sectionCell.setContentSectionCell(content); diff --git a/MyINPulse-back/src/test/java/enseirb/myinpulse/EntrepreneurApiServiceTest.java b/MyINPulse-back/src/test/java/enseirb/myinpulse/EntrepreneurApiServiceTest.java index 18bb429..88f080e 100644 --- a/MyINPulse-back/src/test/java/enseirb/myinpulse/EntrepreneurApiServiceTest.java +++ b/MyINPulse-back/src/test/java/enseirb/myinpulse/EntrepreneurApiServiceTest.java @@ -65,15 +65,9 @@ public class EntrepreneurApiServiceTest { project = projectService.addNewProject( new Project("Project", null, LocalDate.now(), ACTIVE, null, entrepreneur)); - // projectService.updateProjectEntrepreneurParticipation(project.getIdProject(), - // entrepreneur); proxy error because why not entrepreneurService.updateEntrepreneurProjectProposed(entrepreneur.getIdUser(), project); entrepreneurService.updateEntrepreneurProjectParticipation( entrepreneur.getIdUser(), project); - System.out.println(("real")); - System.out.println(entrepreneur); - // System.out.println(entrepreneur.getProjectProposed()); - // System.out.println(entrepreneur.getProjectParticipation()); SectionCell s1 = sectionCellService.addNewSectionCell( new SectionCell( @@ -88,7 +82,7 @@ public class EntrepreneurApiServiceTest { new SectionCell( null, 3L, - "contenu très intéressant", + "contenu très intéressant2", LocalDateTime.now(), project)); sectionCells2 = sectionCellService.getSectionCellsByProject(project, 2L); @@ -104,13 +98,15 @@ public class EntrepreneurApiServiceTest { @Test void editValidSectionCell() { entrepreneurApiService.editSectionCell( - IterableToList(sectionCells2).getFirst().getIdSectionCell(), + IterableToList(sectionCells2).getLast().getIdSectionCell(), "modified content", "entrepreneur@mail.fr"); // We get the data from the database again. SectionCell s = - IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).getFirst(); + IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).getLast(); assertEquals("modified content", s.getContentSectionCell()); + assertEquals( + 2, IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).size()); } @Test @@ -120,6 +116,11 @@ public class EntrepreneurApiServiceTest { () -> entrepreneurApiService.editSectionCell( -1L, "should not be modified", "entrepreneur@mail.fr")); + SectionCell s = + IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).getLast(); + assertEquals("contenu très intéressant", s.getContentSectionCell()); + assertEquals( + 1, IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).size()); } @Test @@ -132,7 +133,7 @@ public class EntrepreneurApiServiceTest { "should not be modified", "testentrepreneur@mail.fr")); SectionCell s = - IterableToList(sectionCellService.getSectionCellsByProject(project, 3L)).getFirst(); + IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).getFirst(); assertEquals("contenu très intéressant", s.getContentSectionCell()); } @@ -148,7 +149,10 @@ public class EntrepreneurApiServiceTest { entrepreneurApiService.removeSectionCell( tmpCell.getIdSectionCell(), "entrepreneur@mail.fr"); assertEquals( - 1, IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).size()); + tmpCell.getIdReference(), + IterableToList(sectionCellService.getSectionCellsByProject(project, -1L)) + .getLast() + .getIdReference()); } @Test @@ -156,5 +160,52 @@ public class EntrepreneurApiServiceTest { assertThrows( ResponseStatusException.class, () -> entrepreneurApiService.removeSectionCell(-1L, "entrepreneur@mail.fr")); + SectionCell s = + IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).getFirst(); + + assertEquals("contenu très intéressant", s.getContentSectionCell()); + } + + @Test + void addValidSectionCell() { + SectionCell added = + sectionCellService.addNewSectionCell( + new SectionCell(null, 2L, "contenu ajouté", LocalDateTime.now(), project)); + entrepreneurApiService.addSectionCell(added, "entrepreneur@mail.fr"); + SectionCell s = + IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).getLast(); + assertEquals("contenu ajouté", s.getContentSectionCell()); + assertEquals( + 2, IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).size()); + } + + @Test + void addSectionCellInvalidAccess() { + System.out.println( + "content : " + + IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)) + .getLast() + .getContentSectionCell()); + SectionCell added = + sectionCellService.addNewSectionCell( + new SectionCell(null, 2L, "contenu ajouté", LocalDateTime.now(), project)); + assertThrows( + ResponseStatusException.class, + () -> entrepreneurApiService.addSectionCell(added, "fauxentrepreneur@mail.fr")); + SectionCell s = + IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).getLast(); + assertEquals( + 1, IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).size()); + assertEquals("contenu très intéressant", s.getContentSectionCell()); + } + + @Test + void addInvalidSectionCell() { + SectionCell added = + sectionCellService.addNewSectionCell( + new SectionCell(null, -1L, "contenu ajouté", LocalDateTime.now(), project)); + assertThrows( + ResponseStatusException.class, + () -> entrepreneurApiService.addSectionCell(added, "entrepreneur@mail.fr")); } }