From 7df2c768c894c717e9ce0a7360a12e0cc5f676be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Le=20Lez?= Date: Wed, 16 Apr 2025 11:26:25 +0200 Subject: [PATCH] fix : tests --- .../service/EntrepreneurApiService.java | 33 +++-- .../myinpulse/EntrepreneurApiServiceTest.java | 17 ++- .../myinpulse/SharedApiServiceTest.java | 136 ++++++++---------- 3 files changed, 94 insertions(+), 92 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 b33c2d1..07b0936 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/EntrepreneurApiService.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/EntrepreneurApiService.java @@ -112,9 +112,17 @@ public class EntrepreneurApiService { mail, sectionCellId, this.sectionCellService.getProjectId(sectionCellId)); - SectionCell removedSectionCell = new SectionCell(null, -1L, "", LocalDateTime.now(), null); - removedSectionCell.setIdReference(editSectionCell.getIdReference()); + SectionCell removedSectionCell = + new SectionCell( + null, + -1L, + "", + LocalDateTime.now(), + this.projectService.getProjectById( + editSectionCell.getProjectSectionCell().getIdProject())); sectionCellService.addNewSectionCell(removedSectionCell); + this.sectionCellService.updateSectionCellReferenceId( + removedSectionCell.getIdSectionCell(), editSectionCell.getIdReference()); projectService.updateProjectListSectionCell( sectionCellService.getProjectId(sectionCellId), removedSectionCell); // sectionCellService.removeSectionCellById(sectionCellId); @@ -132,19 +140,19 @@ public class EntrepreneurApiService { HttpStatus.BAD_REQUEST, "La cellule de section fournie n'est pas valide"); } if (!utilsService.isAllowedToCheckProject( - mail, this.sectionCellService.getProjectId(sectionCell.getIdSectionCell()))) { + mail, sectionCell.getProjectSectionCell().getIdProject())) { logger.warn( "User {} tried to add a section cell to the project {} but is not allowed to.", mail, - this.sectionCellService.getProjectId(sectionCell.getIdSectionCell())); + sectionCell.getProjectSectionCell().getIdProject()); throw new ResponseStatusException( HttpStatus.UNAUTHORIZED, "You're not allowed to check this project"); } logger.info( - "User {} added a new section cell {} to the project with id {}", + "User {} added a new section cell {} to the project {}", mail, sectionCell.getIdSectionCell(), - this.sectionCellService.getProjectId(sectionCell.getIdSectionCell())); + sectionCell.getProjectSectionCell().getIdProject()); SectionCell newSectionCell = sectionCellService.addNewSectionCell( sectionCell); // if here, logger fails cause id is null (not added yet) @@ -173,11 +181,18 @@ public class EntrepreneurApiService { project.setEntrepreneurProposed((Entrepreneur) this.userService.getUserByEmail(mail)); projectService.addNewProject(project); project.getProjectAdministrator().updateListProject(project); - project.getEntrepreneurProposed().setProjectProposed(project); + this.entrepreneurService.updateEntrepreneurProjectProposed( + this.userService.getUserByEmail(mail).getIdUser(), project); project.getListEntrepreneurParticipation() - .forEach(entrepreneur -> entrepreneur.setProjectParticipation(project)); + .forEach( + entrepreneur -> + this.entrepreneurService.updateEntrepreneurProjectParticipation( + entrepreneur.getIdUser(), project)); project.getListSectionCell() - .forEach(sectionCell -> sectionCell.setProjectSectionCell(project)); + .forEach( + sectionCell -> + this.sectionCellService.updateSectionCellProject( + sectionCell.getIdSectionCell(), project)); } public void createAccount(Entrepreneur e) { diff --git a/MyINPulse-back/src/test/java/enseirb/myinpulse/EntrepreneurApiServiceTest.java b/MyINPulse-back/src/test/java/enseirb/myinpulse/EntrepreneurApiServiceTest.java index 88f080e..b92a007 100644 --- a/MyINPulse-back/src/test/java/enseirb/myinpulse/EntrepreneurApiServiceTest.java +++ b/MyINPulse-back/src/test/java/enseirb/myinpulse/EntrepreneurApiServiceTest.java @@ -146,8 +146,10 @@ public class EntrepreneurApiServiceTest { null, 2L, "contenu temporaire", LocalDateTime.now(), project)); assertEquals( 2, IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).size()); - entrepreneurApiService.removeSectionCell( - tmpCell.getIdSectionCell(), "entrepreneur@mail.fr"); + assertDoesNotThrow( + () -> + entrepreneurApiService.removeSectionCell( + tmpCell.getIdSectionCell(), "entrepreneur@mail.fr")); assertEquals( tmpCell.getIdReference(), IterableToList(sectionCellService.getSectionCellsByProject(project, -1L)) @@ -181,14 +183,8 @@ public class EntrepreneurApiServiceTest { @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)); + new SectionCell(null, 2L, "contenu ajouté", LocalDateTime.now(), project); assertThrows( ResponseStatusException.class, () -> entrepreneurApiService.addSectionCell(added, "fauxentrepreneur@mail.fr")); @@ -208,4 +204,7 @@ public class EntrepreneurApiServiceTest { ResponseStatusException.class, () -> entrepreneurApiService.addSectionCell(added, "entrepreneur@mail.fr")); } + + @Test + void requestValidProject() {} } diff --git a/MyINPulse-back/src/test/java/enseirb/myinpulse/SharedApiServiceTest.java b/MyINPulse-back/src/test/java/enseirb/myinpulse/SharedApiServiceTest.java index d92dabd..4819ea9 100644 --- a/MyINPulse-back/src/test/java/enseirb/myinpulse/SharedApiServiceTest.java +++ b/MyINPulse-back/src/test/java/enseirb/myinpulse/SharedApiServiceTest.java @@ -3,28 +3,22 @@ package enseirb.myinpulse; import static enseirb.myinpulse.model.ProjectDecisionValue.*; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.ArgumentMatchers.endsWith; import enseirb.myinpulse.model.Administrator; +import enseirb.myinpulse.model.Appointment; import enseirb.myinpulse.model.Entrepreneur; import enseirb.myinpulse.model.Project; -import enseirb.myinpulse.model.Appointment; - import enseirb.myinpulse.service.SharedApiService; import enseirb.myinpulse.service.database.AdministratorService; import enseirb.myinpulse.service.database.EntrepreneurService; import enseirb.myinpulse.service.database.ProjectService; -import org.aspectj.lang.annotation.After; -import org.checkerframework.checker.units.qual.kmPERh; import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.AfterEach; 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.LocalTime; @@ -34,7 +28,7 @@ import java.util.List; @SpringBootTest @Transactional public class SharedApiServiceTest { - + @Autowired private SharedApiService sharedApiService; @Autowired private ProjectService projectService; @@ -45,58 +39,52 @@ public class SharedApiServiceTest { private static Entrepreneur functional_entrepreneur; private static Project functional_project; - static private Entrepreneur empty_entrepreneur; - static private Administrator empty_administrator; - static private Project empty_Project; - + private static Entrepreneur empty_entrepreneur; + private static Administrator empty_administrator; + private static Project empty_Project; private static Administrator getTestAdmin(String name) { return new Administrator( - name, - name, - name+"@example.com", - "seconday@example.com", - "0123456789"); + name, name, name + "@example.com", "seconday@example.com", "0123456789"); } private static Entrepreneur getTestEntrpreneur(String name) { return new Entrepreneur( - name, - name, - name+"@example.com", - "seconday@example.com", - "0123456789", - "School", - "Course", - false); + name, + name, + name + "@example.com", + "seconday@example.com", + "0123456789", + "School", + "Course", + false); } private static Project getTestProject(String name, Administrator admin) { return new Project(name, null, LocalDate.now(), ACTIVE, admin); } - static - - @BeforeAll - private void setup( + static @BeforeAll private void setup( @Autowired AdministratorService administratorService, @Autowired ProjectService projectService, @Autowired EntrepreneurService entrepreneurService) { - - empty_entrepreneur = entrepreneurService.addEntrepreneur(null); - empty_administrator = administratorService.addAdministrator(null); - empty_Project = projectService.addNewProject(new Project()); - - functional_administrator = administratorService.addAdministrator(getTestAdmin("functional_administrator")); - functional_entrepreneur = entrepreneurService.addEntrepreneur(getTestEntrpreneur("functional_entrepreneur")); - functional_project = projectService.addNewProject(getTestProject("functional_project", functional_administrator)); - functional_project.updateListEntrepreneurParticipation(functional_entrepreneur); + + empty_entrepreneur = entrepreneurService.addEntrepreneur(null); + empty_administrator = administratorService.addAdministrator(null); + empty_Project = projectService.addNewProject(new Project()); + + functional_administrator = + administratorService.addAdministrator(getTestAdmin("functional_administrator")); + functional_entrepreneur = + entrepreneurService.addEntrepreneur(getTestEntrpreneur("functional_entrepreneur")); + functional_project = + projectService.addNewProject( + getTestProject("functional_project", functional_administrator)); + functional_project.updateListEntrepreneurParticipation(functional_entrepreneur); } @AfterAll - private void cleanup() { - - } + private void cleanup() {} private List IterableToList(Iterable iterable) { List l = new ArrayList<>(); @@ -106,7 +94,7 @@ public class SharedApiServiceTest { private boolean matchesIgnoringId(T expected, T actual) { /* - Implementing custom comparison logic depending on the actual type of T, + Implementing custom comparison logic depending on the actual type of T, Can't think of a better way than changing the model and overriding equals */ if (expected instanceof Appointment && actual instanceof Appointment) { @@ -117,87 +105,87 @@ public class SharedApiServiceTest { && e.getAppointmentDuration().equals(a.getAppointmentDuration()) && e.getAppointmentDuration().equals(a.getAppointmentPlace()); } - + throw new IllegalArgumentException("Unsupported type for comparison"); } - private void TestIfInIterable(Iterable iterable, K expected) { List l = IterableToList(iterable); - Boolean exists = l.stream() - .anyMatch(e -> matchesIgnoringId(expected, e)); + Boolean exists = l.stream().anyMatch(e -> matchesIgnoringId(expected, e)); assertTrue(exists, ""); } - /* + /* * Tests if an appointement made by the user himself and the users associated with appointment, * the appoitement date, time, etc are correct. - */ + */ @Test void testCreateAppointmentRequest_Users() { - /* + /* * Creating the setup for the test - */ + */ LocalDate date = LocalDate.parse("02-05-2025"); LocalTime duration = LocalTime.parse("00:15:30"); LocalTime time = LocalTime.parse("10:20:00"); String appointmentPlace = "salleInpulse"; String appointmentSubject = "Titanic"; - Appointment appointment = new Appointment( new Long(0), date, time, duration, appointmentPlace, appointmentSubject); - sharedApiService.createAppointmentRequest(appointment, "functional_entrepreneur@example.com"); + Appointment appointment = + new Appointment( + new Long(0), date, time, duration, appointmentPlace, appointmentSubject); + sharedApiService.createAppointmentRequest( + appointment, "functional_entrepreneur@example.com"); /* * fetching the values and testing them */ - Iterable appointments = sharedApiService.getAppointmentsByProjectId(functional_project.getIdProject(), "functional_entrepreneur@example.com"); + Iterable appointments = + sharedApiService.getAppointmentsByProjectId( + functional_project.getIdProject(), "functional_entrepreneur@example.com"); List appointment_list = IterableToList(appointments); - assertEquals(date, date); assertEquals(time, time); assertEquals(appointmentPlace, appointmentPlace); assertEquals(appointmentSubject, appointmentSubject); } - - /* + /* * Tests the edge cases: * - an appointement made by a user but has no participants. * - the inputed dates for appointments are not older than current date. * - date or time format is wrong. - */ + */ @Test void testCreateAppointmentRequest_EdgeCases() { assertEquals(0, 0); } - - /* - * Tests if an admin and entrepreneur with no prior appointments + /* + * Tests if an admin and entrepreneur with no prior appointments * have no appointments. - */ + */ @Test void testGetAppointement_EmptyUser() { assertEquals(0, 0); } - /* - * Tests if an admin and entrepreneur indepedant of eachother with no prior appointments, + /* + * Tests if an admin and entrepreneur indepedant of eachother with no prior appointments, * each have exactly one appointment an appointment after . - */ + */ @Test void testGetAppointement_NonEmptyUser() { assertEquals(0, 0); } - /* + /* * Tests if an admin and entrepreneur both bound by the same project * have the same appointment. - */ + */ @Test void testGetAppointement_UsersHaveSameAppointement() { assertEquals(0, 0); } - + /* * Tests if in empty project has no sectionCells */ @@ -207,7 +195,7 @@ public class SharedApiServiceTest { } /* - * Tests if in a project with no prior sectionCells that is given exactly + * Tests if in a project with no prior sectionCells that is given exactly * one sectionCell has: * - exactly one section cell. * - the cell given back has the correct information. @@ -217,31 +205,31 @@ public class SharedApiServiceTest { assertEquals(0, 0); } - /* + /* * Tests the edge cases: * - sectionId is in {1, ... , 8}. * - modificationDate is not newer than the current date. - */ + */ @Test void testGetSectionCells_EdgeCases() { assertEquals(0, 0); } - /* + /* * Tests if: * - handls a non existing projectId correctly. * - returns the correct admin associated with project by id - */ + */ @Test void testGetAdminByProjectId() { assertEquals(0, 0); } - /* + /* * Tests if: * - handls non existing projectId correctly. * - returns the correct entrepreneurs associated with the project. - */ + */ @Test void testGetEntrepreneursByProjectId() { assertEquals(0, 0);