From 5b6b6476975b2504d41eae0623efceb5edb11ffd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Le=20Lez?= Date: Wed, 2 Apr 2025 10:25:50 +0200 Subject: [PATCH] feat: started test for EntrepreneurServiceApi --- .gitignore | 3 +- .../myinpulse/controller/EntrepreneurApi.java | 6 +- .../java/enseirb/myinpulse/model/Project.java | 16 ++ .../service/EntrepreneurApiService.java | 12 +- .../service/database/SectionCellService.java | 10 +- .../myinpulse/AdminApiServiceTest.java | 8 +- .../myinpulse/EntrepreneurApiServiceTest.java | 152 ++++++++++++++++++ 7 files changed, 183 insertions(+), 24 deletions(-) create mode 100644 MyINPulse-back/src/test/java/enseirb/myinpulse/EntrepreneurApiServiceTest.java diff --git a/.gitignore b/.gitignore index 7117a86..5e0ae91 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .env .idea keycloak/CAS/target +keycloak/.installed docker-compose.yaml -postgres/data \ No newline at end of file +postgres/data diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/controller/EntrepreneurApi.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/controller/EntrepreneurApi.java index b0de5b7..6b35855 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/controller/EntrepreneurApi.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/controller/EntrepreneurApi.java @@ -1,7 +1,7 @@ package enseirb.myinpulse.controller; -import enseirb.myinpulse.model.SectionCell; import enseirb.myinpulse.model.Project; +import enseirb.myinpulse.model.SectionCell; import enseirb.myinpulse.service.EntrepreneurApiService; import org.springframework.beans.factory.annotation.Autowired; @@ -31,10 +31,10 @@ public class EntrepreneurApi { @PutMapping("/entrepreneur/lcsection/modify/{sectionId}") public void editSectionCell( @PathVariable Long sectionId, - @RequestBody SectionCell sectionCell, + @RequestBody String content, @AuthenticationPrincipal Jwt principal) { entrepreneurApiService.editSectionCell( - sectionId, sectionCell, principal.getClaimAsString("email")); + sectionId, content, principal.getClaimAsString("email")); } /** diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Project.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Project.java index 866e685..e4085b8 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Project.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Project.java @@ -5,6 +5,7 @@ import jakarta.persistence.*; import java.time.LocalDate; import java.util.ArrayList; import java.util.List; +import java.util.Objects; @Entity @Table(name = "project") @@ -66,6 +67,21 @@ public class Project { this.entrepreneurProposed = entrepreneurProposed; } + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) return false; + Project project = (Project) o; + return Objects.equals(listEntrepreneurParticipation, project.listEntrepreneurParticipation) + && Objects.equals(listSectionCell, project.listSectionCell) + && Objects.equals(idProject, project.idProject) + && Objects.equals(projectName, project.projectName) + && Objects.deepEquals(logo, project.logo) + && Objects.equals(creationDate, project.creationDate) + && projectStatus == project.projectStatus + && Objects.equals(projectAdministrator, project.projectAdministrator) + && Objects.equals(entrepreneurProposed, project.entrepreneurProposed); + } + public Long getIdProject() { return idProject; } 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 1e9cd92..10479c3 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/EntrepreneurApiService.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/EntrepreneurApiService.java @@ -33,9 +33,9 @@ public class EntrepreneurApiService { this.utilsService = utilsService; } - public void editSectionCell(Long sectionCellId, SectionCell sectionCell, String mail) { - SectionCell editSectionCell = sectionCellService.getSectionCellById(sectionCellId); - if (editSectionCell == null) { + 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"); throw new ResponseStatusException( HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas"); @@ -55,11 +55,7 @@ public class EntrepreneurApiService { mail, sectionCellId, this.sectionCellService.getProjectId(sectionCellId)); - sectionCellService.updateSectionCell( - sectionCellId, - sectionCell.getSectionId(), - sectionCell.getContentSectionCell(), - sectionCell.getModificationDate()); + sectionCellService.updateSectionCell(sectionCellId, content); } public void removeSectionCell(Long sectionCellId, String mail) { 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 59c7397..9ef84fd 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 @@ -50,22 +50,16 @@ public class SectionCellService { this.sectionCellRepository.deleteById(id); } - public SectionCell updateSectionCell( - Long id, Long sectionId, String contentSectionCell, LocalDateTime modificationDate) { + public SectionCell updateSectionCell(Long id, String contentSectionCell) { Optional sectionCell = this.sectionCellRepository.findById(id); if (sectionCell.isEmpty()) { logger.error("updateSectionCell : No sectionCell found with id {}", id); throw new ResponseStatusException( HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas"); } - if (sectionId != null) { - sectionCell.get().setSectionId(sectionId); - } if (contentSectionCell != null) { sectionCell.get().setContentSectionCell(contentSectionCell); - } - if (modificationDate != null) { - sectionCell.get().setModificationDate(modificationDate); + sectionCell.get().setModificationDate(LocalDateTime.now()); } return this.sectionCellRepository.save(sectionCell.get()); } diff --git a/MyINPulse-back/src/test/java/enseirb/myinpulse/AdminApiServiceTest.java b/MyINPulse-back/src/test/java/enseirb/myinpulse/AdminApiServiceTest.java index 676dfc9..8fd0bd6 100644 --- a/MyINPulse-back/src/test/java/enseirb/myinpulse/AdminApiServiceTest.java +++ b/MyINPulse-back/src/test/java/enseirb/myinpulse/AdminApiServiceTest.java @@ -106,7 +106,7 @@ public class AdminApiServiceTest { List l = IterableToList(projects); assertEquals(1, l.size()); Project p = l.getFirst(); - assertEquals(p.getProjectName(), "sampleProjectAdminApiService"); + assertEquals("sampleProjectAdminApiService", p.getProjectName()); } @Test @@ -180,7 +180,7 @@ public class AdminApiServiceTest { @Test void addProjectToAdmin() { assertEquals(0, administrator.getListProject().size()); - Project p1 = new Project("assProjectToAdmin", null, LocalDate.now(), ACTIVE, administrator); + Project p1 = new Project("addProjectToAdmin", null, LocalDate.now(), ACTIVE, administrator); this.adminApiService.addNewProject(p1); assertEquals(1, administrator.getListProject().size()); } @@ -189,7 +189,7 @@ public class AdminApiServiceTest { void addProjectToUser() { assertNull(entrepreneur.getProjectParticipation()); Project p1 = - new Project("assProjectToAdmin", null, LocalDate.now(), ACTIVE, null, entrepreneur); + new Project("addProjectToAdmin", null, LocalDate.now(), ACTIVE, null, entrepreneur); this.adminApiService.addNewProject(p1); assertEquals(p1, entrepreneur.getProjectParticipation()); } @@ -202,7 +202,7 @@ public class AdminApiServiceTest { assertNull(e1.getProjectParticipation()); assertNull(e2.getProjectParticipation()); assertNull(e3.getProjectParticipation()); - Project p1 = new Project("assProjectToAdmin", null, LocalDate.now(), ACTIVE, null, null); + Project p1 = new Project("addProjectToAdmin", null, LocalDate.now(), ACTIVE, null, null); p1.updateListEntrepreneurParticipation(e1); p1.updateListEntrepreneurParticipation(e2); p1.updateListEntrepreneurParticipation(e3); diff --git a/MyINPulse-back/src/test/java/enseirb/myinpulse/EntrepreneurApiServiceTest.java b/MyINPulse-back/src/test/java/enseirb/myinpulse/EntrepreneurApiServiceTest.java new file mode 100644 index 0000000..53ce8e3 --- /dev/null +++ b/MyINPulse-back/src/test/java/enseirb/myinpulse/EntrepreneurApiServiceTest.java @@ -0,0 +1,152 @@ +package enseirb.myinpulse; + +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.service.EntrepreneurApiService; +import enseirb.myinpulse.service.database.EntrepreneurService; +import enseirb.myinpulse.service.database.ProjectService; +import enseirb.myinpulse.service.database.SectionCellService; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.server.ResponseStatusException; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + +@SpringBootTest +@Transactional +public class EntrepreneurApiServiceTest { + private static Entrepreneur entrepreneur; + private static Iterable sectionCells; + @Autowired private EntrepreneurApiService entrepreneurApiService; + @Autowired private EntrepreneurService entrepreneurService; + @Autowired private ProjectService projectService; + @Autowired private SectionCellService sectionCellService; + + @BeforeAll + static void setup( + @Autowired EntrepreneurService entrepreneurService, + @Autowired ProjectService projectService, + @Autowired SectionCellService sectionCellService) { + entrepreneur = + entrepreneurService.addEntrepreneur( + new Entrepreneur( + "entre", + "preneur", + "entrepreneur@mail.fr", + "entrepreneur2@mail.fr", + "01 45 71 25 48", + "ENSEIRB", + "Info", + false)); + entrepreneurService.addEntrepreneur( + new Entrepreneur( + "entre2", + "preneur2", + "testentrepreneur@mail.fr", + "testentrepreneur2@mail.fr", + "", + "ENSEGID", + "", + true)); + Project project = + projectService.addNewProject( + new Project("Project", null, LocalDate.now(), ACTIVE, null, entrepreneur)); + entrepreneur.setProjectParticipation(project); + sectionCellService.addNewSectionCell( + new SectionCell( + null, + 2L, + "contenu très intéressant", + LocalDateTime.now(), + projectService.getProjectByName("Project"))); + sectionCells = + sectionCellService.getSectionCellsByProject( + projectService.getProjectByName("Project"), 2L); + } + + private List IterableToList(Iterable iterable) { + List l = new ArrayList<>(); + iterable.forEach(l::add); + return l; + } + + @Test + void editValidSectionCell() { + System.out.println(sectionCells); + entrepreneurApiService.editSectionCell( + IterableToList(sectionCells).getFirst().getIdSectionCell(), + "modified content", + "entrepreneur@mail.fr"); + assertEquals( + "modified content", + IterableToList(sectionCells).getFirst().getContentSectionCell()); + } + + @Test + void editInvalidSectionCell() { + assertThrows( + ResponseStatusException.class, + () -> + entrepreneurApiService.editSectionCell( + -1L, "should not be modified", "entrepreneur@mail.fr")); + } + + @Test + void editSectionCellInvalidAccess() { + assertThrows( + ResponseStatusException.class, + () -> + entrepreneurApiService.editSectionCell( + IterableToList(sectionCells).getFirst().getIdSectionCell(), + "should not be modified", + "testentrepreneur@mail.fr")); + assertEquals( + "contenu très intéressant", + IterableToList(sectionCells).getFirst().getContentSectionCell()); + } + + @Test + void removeValidSectionCell() { + SectionCell tmpCell = + sectionCellService.addNewSectionCell( + new SectionCell( + null, + 2L, + "contenu temporaire", + LocalDateTime.now(), + projectService.getProjectByName("Project"))); + assertEquals( + 2, + IterableToList( + sectionCellService.getSectionCellsByProject( + projectService.getProjectByName("Project"), 2L)) + .size()); + entrepreneurApiService.removeSectionCell( + tmpCell.getIdSectionCell(), "entrepreneur@mail.fr"); + assertEquals( + 1, + IterableToList( + sectionCellService.getSectionCellsByProject( + projectService.getProjectByName("Project"), 2L)) + .size()); + } + + @Test + void removeInvalidSectionCell() { + assertThrows( + ResponseStatusException.class, + () -> entrepreneurApiService.removeSectionCell(-1L, "entrepreneur@mail.fr")); + } +}