fix : tests
Some checks failed
CI / build (push) Waiting to run
Format / formatting (push) Successful in 5s
Build / build (push) Has been cancelled

This commit is contained in:
Théo Le Lez 2025-04-16 11:26:25 +02:00
parent 6029457735
commit 7df2c768c8
3 changed files with 94 additions and 92 deletions

View File

@ -112,9 +112,17 @@ public class EntrepreneurApiService {
mail, mail,
sectionCellId, sectionCellId,
this.sectionCellService.getProjectId(sectionCellId)); this.sectionCellService.getProjectId(sectionCellId));
SectionCell removedSectionCell = new SectionCell(null, -1L, "", LocalDateTime.now(), null); SectionCell removedSectionCell =
removedSectionCell.setIdReference(editSectionCell.getIdReference()); new SectionCell(
null,
-1L,
"",
LocalDateTime.now(),
this.projectService.getProjectById(
editSectionCell.getProjectSectionCell().getIdProject()));
sectionCellService.addNewSectionCell(removedSectionCell); sectionCellService.addNewSectionCell(removedSectionCell);
this.sectionCellService.updateSectionCellReferenceId(
removedSectionCell.getIdSectionCell(), editSectionCell.getIdReference());
projectService.updateProjectListSectionCell( projectService.updateProjectListSectionCell(
sectionCellService.getProjectId(sectionCellId), removedSectionCell); sectionCellService.getProjectId(sectionCellId), removedSectionCell);
// sectionCellService.removeSectionCellById(sectionCellId); // sectionCellService.removeSectionCellById(sectionCellId);
@ -132,19 +140,19 @@ public class EntrepreneurApiService {
HttpStatus.BAD_REQUEST, "La cellule de section fournie n'est pas valide"); HttpStatus.BAD_REQUEST, "La cellule de section fournie n'est pas valide");
} }
if (!utilsService.isAllowedToCheckProject( if (!utilsService.isAllowedToCheckProject(
mail, this.sectionCellService.getProjectId(sectionCell.getIdSectionCell()))) { mail, sectionCell.getProjectSectionCell().getIdProject())) {
logger.warn( logger.warn(
"User {} tried to add a section cell to the project {} but is not allowed to.", "User {} tried to add a section cell to the project {} but is not allowed to.",
mail, mail,
this.sectionCellService.getProjectId(sectionCell.getIdSectionCell())); sectionCell.getProjectSectionCell().getIdProject());
throw new ResponseStatusException( throw new ResponseStatusException(
HttpStatus.UNAUTHORIZED, "You're not allowed to check this project"); HttpStatus.UNAUTHORIZED, "You're not allowed to check this project");
} }
logger.info( logger.info(
"User {} added a new section cell {} to the project with id {}", "User {} added a new section cell {} to the project {}",
mail, mail,
sectionCell.getIdSectionCell(), sectionCell.getIdSectionCell(),
this.sectionCellService.getProjectId(sectionCell.getIdSectionCell())); sectionCell.getProjectSectionCell().getIdProject());
SectionCell newSectionCell = SectionCell newSectionCell =
sectionCellService.addNewSectionCell( sectionCellService.addNewSectionCell(
sectionCell); // if here, logger fails cause id is null (not added yet) 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)); project.setEntrepreneurProposed((Entrepreneur) this.userService.getUserByEmail(mail));
projectService.addNewProject(project); projectService.addNewProject(project);
project.getProjectAdministrator().updateListProject(project); project.getProjectAdministrator().updateListProject(project);
project.getEntrepreneurProposed().setProjectProposed(project); this.entrepreneurService.updateEntrepreneurProjectProposed(
this.userService.getUserByEmail(mail).getIdUser(), project);
project.getListEntrepreneurParticipation() project.getListEntrepreneurParticipation()
.forEach(entrepreneur -> entrepreneur.setProjectParticipation(project)); .forEach(
entrepreneur ->
this.entrepreneurService.updateEntrepreneurProjectParticipation(
entrepreneur.getIdUser(), project));
project.getListSectionCell() project.getListSectionCell()
.forEach(sectionCell -> sectionCell.setProjectSectionCell(project)); .forEach(
sectionCell ->
this.sectionCellService.updateSectionCellProject(
sectionCell.getIdSectionCell(), project));
} }
public void createAccount(Entrepreneur e) { public void createAccount(Entrepreneur e) {

View File

@ -146,8 +146,10 @@ public class EntrepreneurApiServiceTest {
null, 2L, "contenu temporaire", LocalDateTime.now(), project)); null, 2L, "contenu temporaire", LocalDateTime.now(), project));
assertEquals( assertEquals(
2, IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).size()); 2, IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).size());
entrepreneurApiService.removeSectionCell( assertDoesNotThrow(
tmpCell.getIdSectionCell(), "entrepreneur@mail.fr"); () ->
entrepreneurApiService.removeSectionCell(
tmpCell.getIdSectionCell(), "entrepreneur@mail.fr"));
assertEquals( assertEquals(
tmpCell.getIdReference(), tmpCell.getIdReference(),
IterableToList(sectionCellService.getSectionCellsByProject(project, -1L)) IterableToList(sectionCellService.getSectionCellsByProject(project, -1L))
@ -181,14 +183,8 @@ public class EntrepreneurApiServiceTest {
@Test @Test
void addSectionCellInvalidAccess() { void addSectionCellInvalidAccess() {
System.out.println(
"content : "
+ IterableToList(sectionCellService.getSectionCellsByProject(project, 2L))
.getLast()
.getContentSectionCell());
SectionCell added = SectionCell added =
sectionCellService.addNewSectionCell( new SectionCell(null, 2L, "contenu ajouté", LocalDateTime.now(), project);
new SectionCell(null, 2L, "contenu ajouté", LocalDateTime.now(), project));
assertThrows( assertThrows(
ResponseStatusException.class, ResponseStatusException.class,
() -> entrepreneurApiService.addSectionCell(added, "fauxentrepreneur@mail.fr")); () -> entrepreneurApiService.addSectionCell(added, "fauxentrepreneur@mail.fr"));
@ -208,4 +204,7 @@ public class EntrepreneurApiServiceTest {
ResponseStatusException.class, ResponseStatusException.class,
() -> entrepreneurApiService.addSectionCell(added, "entrepreneur@mail.fr")); () -> entrepreneurApiService.addSectionCell(added, "entrepreneur@mail.fr"));
} }
@Test
void requestValidProject() {}
} }

View File

@ -3,28 +3,22 @@ package enseirb.myinpulse;
import static enseirb.myinpulse.model.ProjectDecisionValue.*; import static enseirb.myinpulse.model.ProjectDecisionValue.*;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.endsWith;
import enseirb.myinpulse.model.Administrator; import enseirb.myinpulse.model.Administrator;
import enseirb.myinpulse.model.Appointment;
import enseirb.myinpulse.model.Entrepreneur; import enseirb.myinpulse.model.Entrepreneur;
import enseirb.myinpulse.model.Project; import enseirb.myinpulse.model.Project;
import enseirb.myinpulse.model.Appointment;
import enseirb.myinpulse.service.SharedApiService; import enseirb.myinpulse.service.SharedApiService;
import enseirb.myinpulse.service.database.AdministratorService; import enseirb.myinpulse.service.database.AdministratorService;
import enseirb.myinpulse.service.database.EntrepreneurService; import enseirb.myinpulse.service.database.EntrepreneurService;
import enseirb.myinpulse.service.database.ProjectService; 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.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalTime; import java.time.LocalTime;
@ -34,7 +28,7 @@ import java.util.List;
@SpringBootTest @SpringBootTest
@Transactional @Transactional
public class SharedApiServiceTest { public class SharedApiServiceTest {
@Autowired private SharedApiService sharedApiService; @Autowired private SharedApiService sharedApiService;
@Autowired private ProjectService projectService; @Autowired private ProjectService projectService;
@ -45,58 +39,52 @@ public class SharedApiServiceTest {
private static Entrepreneur functional_entrepreneur; private static Entrepreneur functional_entrepreneur;
private static Project functional_project; private static Project functional_project;
static private Entrepreneur empty_entrepreneur; private static Entrepreneur empty_entrepreneur;
static private Administrator empty_administrator; private static Administrator empty_administrator;
static private Project empty_Project; private static Project empty_Project;
private static Administrator getTestAdmin(String name) { private static Administrator getTestAdmin(String name) {
return new Administrator( return new Administrator(
name, name, name, name + "@example.com", "seconday@example.com", "0123456789");
name,
name+"@example.com",
"seconday@example.com",
"0123456789");
} }
private static Entrepreneur getTestEntrpreneur(String name) { private static Entrepreneur getTestEntrpreneur(String name) {
return new Entrepreneur( return new Entrepreneur(
name, name,
name, name,
name+"@example.com", name + "@example.com",
"seconday@example.com", "seconday@example.com",
"0123456789", "0123456789",
"School", "School",
"Course", "Course",
false); false);
} }
private static Project getTestProject(String name, Administrator admin) { private static Project getTestProject(String name, Administrator admin) {
return new Project(name, null, LocalDate.now(), ACTIVE, admin); return new Project(name, null, LocalDate.now(), ACTIVE, admin);
} }
static static @BeforeAll private void setup(
@BeforeAll
private void setup(
@Autowired AdministratorService administratorService, @Autowired AdministratorService administratorService,
@Autowired ProjectService projectService, @Autowired ProjectService projectService,
@Autowired EntrepreneurService entrepreneurService) { @Autowired EntrepreneurService entrepreneurService) {
empty_entrepreneur = entrepreneurService.addEntrepreneur(null); empty_entrepreneur = entrepreneurService.addEntrepreneur(null);
empty_administrator = administratorService.addAdministrator(null); empty_administrator = administratorService.addAdministrator(null);
empty_Project = projectService.addNewProject(new Project()); empty_Project = projectService.addNewProject(new Project());
functional_administrator = administratorService.addAdministrator(getTestAdmin("functional_administrator")); functional_administrator =
functional_entrepreneur = entrepreneurService.addEntrepreneur(getTestEntrpreneur("functional_entrepreneur")); administratorService.addAdministrator(getTestAdmin("functional_administrator"));
functional_project = projectService.addNewProject(getTestProject("functional_project", functional_administrator)); functional_entrepreneur =
functional_project.updateListEntrepreneurParticipation(functional_entrepreneur); entrepreneurService.addEntrepreneur(getTestEntrpreneur("functional_entrepreneur"));
functional_project =
projectService.addNewProject(
getTestProject("functional_project", functional_administrator));
functional_project.updateListEntrepreneurParticipation(functional_entrepreneur);
} }
@AfterAll @AfterAll
private void cleanup() { private void cleanup() {}
}
private <T> List<T> IterableToList(Iterable<T> iterable) { private <T> List<T> IterableToList(Iterable<T> iterable) {
List<T> l = new ArrayList<>(); List<T> l = new ArrayList<>();
@ -106,7 +94,7 @@ public class SharedApiServiceTest {
private <T> boolean matchesIgnoringId(T expected, T actual) { private <T> 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 Can't think of a better way than changing the model and overriding equals
*/ */
if (expected instanceof Appointment && actual instanceof Appointment) { if (expected instanceof Appointment && actual instanceof Appointment) {
@ -117,87 +105,87 @@ public class SharedApiServiceTest {
&& e.getAppointmentDuration().equals(a.getAppointmentDuration()) && e.getAppointmentDuration().equals(a.getAppointmentDuration())
&& e.getAppointmentDuration().equals(a.getAppointmentPlace()); && e.getAppointmentDuration().equals(a.getAppointmentPlace());
} }
throw new IllegalArgumentException("Unsupported type for comparison"); throw new IllegalArgumentException("Unsupported type for comparison");
} }
private <T, K> void TestIfInIterable(Iterable<T> iterable, K expected) { private <T, K> void TestIfInIterable(Iterable<T> iterable, K expected) {
List<T> l = IterableToList(iterable); List<T> l = IterableToList(iterable);
Boolean exists = l.stream() Boolean exists = l.stream().anyMatch(e -> matchesIgnoringId(expected, e));
.anyMatch(e -> matchesIgnoringId(expected, e));
assertTrue(exists, ""); assertTrue(exists, "");
} }
/* /*
* Tests if an appointement made by the user himself and the users associated with appointment, * Tests if an appointement made by the user himself and the users associated with appointment,
* the appoitement date, time, etc are correct. * the appoitement date, time, etc are correct.
*/ */
@Test @Test
void testCreateAppointmentRequest_Users() { void testCreateAppointmentRequest_Users() {
/* /*
* Creating the setup for the test * Creating the setup for the test
*/ */
LocalDate date = LocalDate.parse("02-05-2025"); LocalDate date = LocalDate.parse("02-05-2025");
LocalTime duration = LocalTime.parse("00:15:30"); LocalTime duration = LocalTime.parse("00:15:30");
LocalTime time = LocalTime.parse("10:20:00"); LocalTime time = LocalTime.parse("10:20:00");
String appointmentPlace = "salleInpulse"; String appointmentPlace = "salleInpulse";
String appointmentSubject = "Titanic"; String appointmentSubject = "Titanic";
Appointment appointment = new Appointment( new Long(0), date, time, duration, appointmentPlace, appointmentSubject); Appointment appointment =
sharedApiService.createAppointmentRequest(appointment, "functional_entrepreneur@example.com"); new Appointment(
new Long(0), date, time, duration, appointmentPlace, appointmentSubject);
sharedApiService.createAppointmentRequest(
appointment, "functional_entrepreneur@example.com");
/* /*
* fetching the values and testing them * fetching the values and testing them
*/ */
Iterable<Appointment> appointments = sharedApiService.getAppointmentsByProjectId(functional_project.getIdProject(), "functional_entrepreneur@example.com"); Iterable<Appointment> appointments =
sharedApiService.getAppointmentsByProjectId(
functional_project.getIdProject(), "functional_entrepreneur@example.com");
List<Appointment> appointment_list = IterableToList(appointments); List<Appointment> appointment_list = IterableToList(appointments);
assertEquals(date, date); assertEquals(date, date);
assertEquals(time, time); assertEquals(time, time);
assertEquals(appointmentPlace, appointmentPlace); assertEquals(appointmentPlace, appointmentPlace);
assertEquals(appointmentSubject, appointmentSubject); assertEquals(appointmentSubject, appointmentSubject);
} }
/*
/*
* Tests the edge cases: * Tests the edge cases:
* - an appointement made by a user but has no participants. * - an appointement made by a user but has no participants.
* - the inputed dates for appointments are not older than current date. * - the inputed dates for appointments are not older than current date.
* - date or time format is wrong. * - date or time format is wrong.
*/ */
@Test @Test
void testCreateAppointmentRequest_EdgeCases() { void testCreateAppointmentRequest_EdgeCases() {
assertEquals(0, 0); 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. * have no appointments.
*/ */
@Test @Test
void testGetAppointement_EmptyUser() { void testGetAppointement_EmptyUser() {
assertEquals(0, 0); 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 . * each have exactly one appointment an appointment after .
*/ */
@Test @Test
void testGetAppointement_NonEmptyUser() { void testGetAppointement_NonEmptyUser() {
assertEquals(0, 0); assertEquals(0, 0);
} }
/* /*
* Tests if an admin and entrepreneur both bound by the same project * Tests if an admin and entrepreneur both bound by the same project
* have the same appointment. * have the same appointment.
*/ */
@Test @Test
void testGetAppointement_UsersHaveSameAppointement() { void testGetAppointement_UsersHaveSameAppointement() {
assertEquals(0, 0); assertEquals(0, 0);
} }
/* /*
* Tests if in empty project has no sectionCells * 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: * one sectionCell has:
* - exactly one section cell. * - exactly one section cell.
* - the cell given back has the correct information. * - the cell given back has the correct information.
@ -217,31 +205,31 @@ public class SharedApiServiceTest {
assertEquals(0, 0); assertEquals(0, 0);
} }
/* /*
* Tests the edge cases: * Tests the edge cases:
* - sectionId is in {1, ... , 8}. * - sectionId is in {1, ... , 8}.
* - modificationDate is not newer than the current date. * - modificationDate is not newer than the current date.
*/ */
@Test @Test
void testGetSectionCells_EdgeCases() { void testGetSectionCells_EdgeCases() {
assertEquals(0, 0); assertEquals(0, 0);
} }
/* /*
* Tests if: * Tests if:
* - handls a non existing projectId correctly. * - handls a non existing projectId correctly.
* - returns the correct admin associated with project by id * - returns the correct admin associated with project by id
*/ */
@Test @Test
void testGetAdminByProjectId() { void testGetAdminByProjectId() {
assertEquals(0, 0); assertEquals(0, 0);
} }
/* /*
* Tests if: * Tests if:
* - handls non existing projectId correctly. * - handls non existing projectId correctly.
* - returns the correct entrepreneurs associated with the project. * - returns the correct entrepreneurs associated with the project.
*/ */
@Test @Test
void testGetEntrepreneursByProjectId() { void testGetEntrepreneursByProjectId() {
assertEquals(0, 0); assertEquals(0, 0);