From bee47473d51649e14f7627787dfb65bccfc64d0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Le=20Lez?= Date: Wed, 23 Apr 2025 11:54:18 +0200 Subject: [PATCH] feat: finsihed tests for EntrepreneurApiService --- .../service/EntrepreneurApiService.java | 60 +++++--- .../service/database/AnnotationService.java | 14 ++ .../myinpulse/EntrepreneurApiServiceTest.java | 134 ++++++++++++++++-- 3 files changed, 175 insertions(+), 33 deletions(-) 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 07b0936..c89a35f 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/EntrepreneurApiService.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/EntrepreneurApiService.java @@ -5,10 +5,7 @@ import static enseirb.myinpulse.model.ProjectDecisionValue.PENDING; import enseirb.myinpulse.model.Entrepreneur; import enseirb.myinpulse.model.Project; import enseirb.myinpulse.model.SectionCell; -import enseirb.myinpulse.service.database.EntrepreneurService; -import enseirb.myinpulse.service.database.ProjectService; -import enseirb.myinpulse.service.database.SectionCellService; -import enseirb.myinpulse.service.database.UserService; +import enseirb.myinpulse.service.database.*; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -29,6 +26,9 @@ public class EntrepreneurApiService { private final UtilsService utilsService; private final UserService userService; private final EntrepreneurService entrepreneurService; + private final AdministratorService administratorService; + private final AppointmentService appointmentService; + private final AnnotationService annotationService; @Autowired EntrepreneurApiService( @@ -36,21 +36,27 @@ public class EntrepreneurApiService { ProjectService projectService, UtilsService utilsService, UserService userService, - EntrepreneurService entrepreneurService) { + EntrepreneurService entrepreneurService, + AdministratorService administratorService, + AppointmentService appointmentService, + AnnotationService annotationService) { this.sectionCellService = sectionCellService; this.projectService = projectService; this.utilsService = utilsService; this.userService = userService; this.entrepreneurService = entrepreneurService; + this.administratorService = administratorService; + this.appointmentService = appointmentService; + this.annotationService = annotationService; } public void editSectionCell(Long sectionCellId, String content, String mail) { - SectionCell sectionCell = sectionCellService.getSectionCellById(sectionCellId); - if (sectionCell == null) { - System.err.println("Trying to edit unknown section cell"); + if (sectionCellId == null) { + logger.warn("Trying to edit unknown section cell"); throw new ResponseStatusException( HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas"); } + SectionCell sectionCell = sectionCellService.getSectionCellById(sectionCellId); if (!utilsService.isAllowedToCheckProject( mail, this.sectionCellService.getProjectId(sectionCellId))) { logger.warn( @@ -74,29 +80,30 @@ public class EntrepreneurApiService { LocalDateTime.now(), sectionCell.getProjectSectionCell()); newSectionCell.setIdReference(sectionCell.getIdReference()); + this.addSectionCell(newSectionCell, mail); sectionCell .getAppointmentSectionCell() .forEach( appointment -> { - newSectionCell.updateAppointmentSectionCell(appointment); + this.appointmentService.updateAppointmentListSectionCell( + appointment.getIdAppointment(), newSectionCell); }); sectionCell .getListAnnotation() .forEach( annotation -> { - newSectionCell.updateListAnnotation(annotation); + this.annotationService.updateAnnotationSectionCell( + annotation.getIdAnnotation(), newSectionCell); }); - this.addSectionCell(newSectionCell, mail); - // sectionCellService.updateSectionCell(sectionCellId, content, null, null, null); } public void removeSectionCell(Long sectionCellId, String mail) { - SectionCell editSectionCell = sectionCellService.getSectionCellById(sectionCellId); - if (editSectionCell == null) { - System.err.println("Trying to remove unknown section cell"); + if (sectionCellId == null) { + logger.warn("Trying to remove unknown section cell"); throw new ResponseStatusException( HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas"); } + SectionCell editSectionCell = sectionCellService.getSectionCellById(sectionCellId); if (!utilsService.isAllowedToCheckProject( mail, this.sectionCellService.getProjectId(sectionCellId))) { logger.warn( @@ -130,12 +137,12 @@ public class EntrepreneurApiService { public void addSectionCell(SectionCell sectionCell, String mail) { if (sectionCell == null) { - System.err.println("Trying to create an empty section cell"); + logger.warn("Trying to create an empty section cell"); 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"); + logger.warn("Trying to create an illegal section cell"); throw new ResponseStatusException( HttpStatus.BAD_REQUEST, "La cellule de section fournie n'est pas valide"); } @@ -161,28 +168,35 @@ public class EntrepreneurApiService { .getAppointmentSectionCell() .forEach( appointment -> { - appointment.updateListSectionCell(newSectionCell); + this.appointmentService.updateAppointmentListSectionCell( + appointment.getIdAppointment(), newSectionCell); }); newSectionCell .getListAnnotation() .forEach( annotation -> { - annotation.setSectionCellAnnotation(newSectionCell); + this.annotationService.updateAnnotationSectionCell( + annotation.getIdAnnotation(), newSectionCell); }); } public void requestNewProject(Project project, String mail) { if (project == null) { - logger.error("Trying to request the creation of a null project"); + logger.warn("Trying to request the creation of a null project"); throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Le projet fourni est vide"); } - logger.info("User {} created a new project with id {}", mail, project.getIdProject()); - project.setProjectStatus(PENDING); + logger.info("User {} created a new project named {}", mail, project.getProjectName()); project.setEntrepreneurProposed((Entrepreneur) this.userService.getUserByEmail(mail)); projectService.addNewProject(project); - project.getProjectAdministrator().updateListProject(project); + this.projectService.updateProjectStatus(project.getIdProject(), PENDING); + if (project.getProjectAdministrator() != null) { + this.administratorService.updateAdministratorListProject( + project.getProjectAdministrator().getIdUser(), project); + } this.entrepreneurService.updateEntrepreneurProjectProposed( this.userService.getUserByEmail(mail).getIdUser(), project); + this.entrepreneurService.updateEntrepreneurProjectParticipation( + this.userService.getUserByEmail(mail).getIdUser(), project); project.getListEntrepreneurParticipation() .forEach( entrepreneur -> diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/AnnotationService.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/AnnotationService.java index 88c119e..f107b35 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/AnnotationService.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/AnnotationService.java @@ -1,6 +1,8 @@ package enseirb.myinpulse.service.database; +import enseirb.myinpulse.model.Administrator; import enseirb.myinpulse.model.Annotation; +import enseirb.myinpulse.model.SectionCell; import enseirb.myinpulse.repository.AnnotationRepository; import org.apache.logging.log4j.LogManager; @@ -64,4 +66,16 @@ public class AnnotationService { } return this.annotationRepository.save(annotation.get()); } + + public void updateAnnotationSectionCell(long idAnnotation, SectionCell sectionCell) { + Annotation annotation = getAnnotationById(idAnnotation); + annotation.setSectionCellAnnotation(sectionCell); + this.annotationRepository.save(annotation); + } + + public void updateAnnotationAdministrator(long idAnnotation, Administrator administrator) { + Annotation annotation = getAnnotationById(idAnnotation); + annotation.setAdministratorAnnotation(administrator); + this.annotationRepository.save(annotation); + } } diff --git a/MyINPulse-back/src/test/java/enseirb/myinpulse/EntrepreneurApiServiceTest.java b/MyINPulse-back/src/test/java/enseirb/myinpulse/EntrepreneurApiServiceTest.java index b92a007..18c1188 100644 --- a/MyINPulse-back/src/test/java/enseirb/myinpulse/EntrepreneurApiServiceTest.java +++ b/MyINPulse-back/src/test/java/enseirb/myinpulse/EntrepreneurApiServiceTest.java @@ -4,13 +4,9 @@ import static enseirb.myinpulse.model.ProjectDecisionValue.*; import static org.junit.jupiter.api.Assertions.*; -import enseirb.myinpulse.model.Entrepreneur; -import enseirb.myinpulse.model.Project; -import enseirb.myinpulse.model.SectionCell; +import enseirb.myinpulse.model.*; import enseirb.myinpulse.service.EntrepreneurApiService; -import enseirb.myinpulse.service.database.EntrepreneurService; -import enseirb.myinpulse.service.database.ProjectService; -import enseirb.myinpulse.service.database.SectionCellService; +import enseirb.myinpulse.service.database.*; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -21,6 +17,7 @@ import org.springframework.web.server.ResponseStatusException; import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.LocalTime; import java.util.ArrayList; import java.util.List; @@ -35,6 +32,8 @@ public class EntrepreneurApiServiceTest { @Autowired private EntrepreneurService entrepreneurService; @Autowired private ProjectService projectService; @Autowired private SectionCellService sectionCellService; + @Autowired private AnnotationService annotationService; + @Autowired private AppointmentService appointmentService; @BeforeAll static void setup( @@ -97,10 +96,23 @@ public class EntrepreneurApiServiceTest { @Test void editValidSectionCell() { + System.out.println("editValidSectionCell : "); + SectionCell modified = IterableToList(sectionCells2).getLast(); + this.sectionCellService.updateSectionCellListAnnotation( + modified.getIdSectionCell(), + annotationService.addNewAnnotation(new Annotation(null, "oui j'annote encore"))); + this.sectionCellService.updateSectionCellListAppointment( + modified.getIdSectionCell(), + appointmentService.addNewAppointment( + new Appointment( + null, + LocalDate.now(), + LocalTime.now(), + LocalTime.of(2, 5), + "TD14", + "clément s'est plaint"))); entrepreneurApiService.editSectionCell( - IterableToList(sectionCells2).getLast().getIdSectionCell(), - "modified content", - "entrepreneur@mail.fr"); + modified.getIdSectionCell(), "modified content", "entrepreneur@mail.fr"); // We get the data from the database again. SectionCell s = IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).getLast(); @@ -111,6 +123,7 @@ public class EntrepreneurApiServiceTest { @Test void editInvalidSectionCell() { + System.out.println("editInvalidSectionCell : "); assertThrows( ResponseStatusException.class, () -> @@ -125,6 +138,7 @@ public class EntrepreneurApiServiceTest { @Test void editSectionCellInvalidAccess() { + System.out.println("editSectionCellInvalidAccess : "); assertThrows( ResponseStatusException.class, () -> @@ -138,8 +152,19 @@ public class EntrepreneurApiServiceTest { assertEquals("contenu très intéressant", s.getContentSectionCell()); } + @Test + void editNullSectionCell() { + System.out.println("editNullSectionCell : "); + assertThrows( + ResponseStatusException.class, + () -> + entrepreneurApiService.editSectionCell( + null, "modified content", "entrepreneur@mail.fr")); + } + @Test void removeValidSectionCell() { + System.out.println("removeValidSectionCell : "); SectionCell tmpCell = sectionCellService.addNewSectionCell( new SectionCell( @@ -159,6 +184,7 @@ public class EntrepreneurApiServiceTest { @Test void removeInvalidSectionCell() { + System.out.println("removeInvalidSectionCell : "); assertThrows( ResponseStatusException.class, () -> entrepreneurApiService.removeSectionCell(-1L, "entrepreneur@mail.fr")); @@ -168,11 +194,31 @@ public class EntrepreneurApiServiceTest { assertEquals("contenu très intéressant", s.getContentSectionCell()); } + @Test + void removeNullSectionCell() { + System.out.println("removeNullSectionCell : "); + assertThrows( + ResponseStatusException.class, + () -> entrepreneurApiService.removeSectionCell(null, "entrepreneur@mail.fr")); + } + @Test void addValidSectionCell() { + System.out.println("addValidSectionCell : "); SectionCell added = sectionCellService.addNewSectionCell( new SectionCell(null, 2L, "contenu ajouté", LocalDateTime.now(), project)); + added.updateListAnnotation( + annotationService.addNewAnnotation(new Annotation(null, "oui j'annote"))); + added.updateAppointmentSectionCell( + appointmentService.addNewAppointment( + new Appointment( + null, + LocalDate.now(), + LocalTime.now(), + LocalTime.of(2, 5), + "TD15", + "clément qui se plaint"))); entrepreneurApiService.addSectionCell(added, "entrepreneur@mail.fr"); SectionCell s = IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).getLast(); @@ -183,6 +229,7 @@ public class EntrepreneurApiServiceTest { @Test void addSectionCellInvalidAccess() { + System.out.println("addSectionCellInvalidAccess : "); SectionCell added = new SectionCell(null, 2L, "contenu ajouté", LocalDateTime.now(), project); assertThrows( @@ -197,6 +244,7 @@ public class EntrepreneurApiServiceTest { @Test void addInvalidSectionCell() { + System.out.println("addInvalidSectionCell : "); SectionCell added = sectionCellService.addNewSectionCell( new SectionCell(null, -1L, "contenu ajouté", LocalDateTime.now(), project)); @@ -206,5 +254,71 @@ public class EntrepreneurApiServiceTest { } @Test - void requestValidProject() {} + void addNullSectionCell() { + System.out.println("addNullSectionCell : "); + assertThrows( + ResponseStatusException.class, + () -> entrepreneurApiService.addSectionCell(null, "entrepreneur@mail.fr")); + } + + @Test + void requestValidProject() { + System.out.println("requestValidProject : "); + int nb_project = IterableToList(this.projectService.getAllProjects()).size(); + Project validProject = + new Project("validProject", null, LocalDate.now(), ACTIVE, null, entrepreneur); + validProject.updateListEntrepreneurParticipation( + IterableToList(entrepreneurService.getAllEntrepreneurs()).getLast()); + validProject.updateListSectionCell((IterableToList(sectionCells2).getFirst())); + entrepreneurApiService.requestNewProject(validProject, "entrepreneur@mail.fr"); + assertEquals(PENDING, validProject.getProjectStatus()); + assertEquals((nb_project + 1), IterableToList(this.projectService.getAllProjects()).size()); + assertEquals( + IterableToList(entrepreneurService.getAllEntrepreneurs()).getLast(), + validProject.getListEntrepreneurParticipation().getLast()); + assertEquals( + IterableToList(sectionCells2).getFirst().getIdSectionCell(), + validProject.getListSectionCell().getFirst().getIdSectionCell()); + } + + @Test + void requestNullProject() { + System.out.println("requestNullProject : "); + assertThrows( + ResponseStatusException.class, + () -> entrepreneurApiService.requestNewProject(null, "entrepreneur@mail.fr")); + } + + @Test + void createNewValidAccount() { + System.out.println("createNewValidAccount : "); + int nb_entrepreneur = IterableToList(this.entrepreneurService.getAllEntrepreneurs()).size(); + Entrepreneur newEntrepreneur = + new Entrepreneur( + "New", + "Test", + "mailtest@test.fr", + "mailtest2@test.fr", + "0888888888", + "ENSEIRB", + "ELEC", + false, + true); + assertDoesNotThrow(() -> entrepreneurApiService.createAccount(newEntrepreneur)); + assertEquals( + (nb_entrepreneur + 1), + IterableToList(this.entrepreneurService.getAllEntrepreneurs()).size()); + } + + @Test + void createExistingAccount() { + System.out.println("createExistingAccount : "); + int nb_entrepreneur = IterableToList(this.entrepreneurService.getAllEntrepreneurs()).size(); + assertThrows( + ResponseStatusException.class, + () -> entrepreneurApiService.createAccount(entrepreneur)); + assertEquals( + nb_entrepreneur, + IterableToList(this.entrepreneurService.getAllEntrepreneurs()).size()); + } }