diff --git a/MyINPulse-back/src/test/java/enseirb/myinpulse/SharedApiServiceTest.java b/MyINPulse-back/src/test/java/enseirb/myinpulse/SharedApiServiceTest.java new file mode 100644 index 0000000..d92dabd --- /dev/null +++ b/MyINPulse-back/src/test/java/enseirb/myinpulse/SharedApiServiceTest.java @@ -0,0 +1,249 @@ +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.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; +import java.util.ArrayList; +import java.util.List; + +@SpringBootTest +@Transactional +public class SharedApiServiceTest { + + @Autowired private SharedApiService sharedApiService; + + @Autowired private ProjectService projectService; + @Autowired private AdministratorService adminService; + @Autowired private EntrepreneurService entrepreneurService; + + private static Administrator functional_administrator; + 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 Administrator getTestAdmin(String name) { + return new Administrator( + 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); + } + + private static Project getTestProject(String name, Administrator admin) { + return new Project(name, null, LocalDate.now(), ACTIVE, admin); + } + + 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); + } + + @AfterAll + private void cleanup() { + + } + + private List IterableToList(Iterable iterable) { + List l = new ArrayList<>(); + iterable.forEach(l::add); + return l; + } + + private boolean matchesIgnoringId(T expected, T actual) { + /* + 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) { + Appointment e = (Appointment) expected; + Appointment a = (Appointment) actual; + return e.getAppointmentDate().equals(a.getAppointmentDate()) + && e.getAppointmentTime().equals(a.getAppointmentTime()) + && 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)); + 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"); + /* + * fetching the values and testing them + */ + 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 + * have no appointments. + */ + @Test + void testGetAppointement_EmptyUser() { + assertEquals(0, 0); + } + + /* + * 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 + */ + @Test + void testGetSectionCells_EmptyProject() { + assertEquals(0, 0); + } + + /* + * 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. + */ + @Test + void testGetSectionCells_NonEmptyProject() { + 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); + } +}