feat: finsihed tests for EntrepreneurApiService
All checks were successful
Format / formatting (push) Successful in 6s
Build / build (push) Successful in 40s
CI / build (push) Successful in 13s

This commit is contained in:
Théo Le Lez 2025-04-23 11:54:18 +02:00
parent e7739af80b
commit bee47473d5
3 changed files with 175 additions and 33 deletions

View File

@ -5,10 +5,7 @@ import static enseirb.myinpulse.model.ProjectDecisionValue.PENDING;
import enseirb.myinpulse.model.Entrepreneur; import enseirb.myinpulse.model.Entrepreneur;
import enseirb.myinpulse.model.Project; import enseirb.myinpulse.model.Project;
import enseirb.myinpulse.model.SectionCell; import enseirb.myinpulse.model.SectionCell;
import enseirb.myinpulse.service.database.EntrepreneurService; import enseirb.myinpulse.service.database.*;
import enseirb.myinpulse.service.database.ProjectService;
import enseirb.myinpulse.service.database.SectionCellService;
import enseirb.myinpulse.service.database.UserService;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -29,6 +26,9 @@ public class EntrepreneurApiService {
private final UtilsService utilsService; private final UtilsService utilsService;
private final UserService userService; private final UserService userService;
private final EntrepreneurService entrepreneurService; private final EntrepreneurService entrepreneurService;
private final AdministratorService administratorService;
private final AppointmentService appointmentService;
private final AnnotationService annotationService;
@Autowired @Autowired
EntrepreneurApiService( EntrepreneurApiService(
@ -36,21 +36,27 @@ public class EntrepreneurApiService {
ProjectService projectService, ProjectService projectService,
UtilsService utilsService, UtilsService utilsService,
UserService userService, UserService userService,
EntrepreneurService entrepreneurService) { EntrepreneurService entrepreneurService,
AdministratorService administratorService,
AppointmentService appointmentService,
AnnotationService annotationService) {
this.sectionCellService = sectionCellService; this.sectionCellService = sectionCellService;
this.projectService = projectService; this.projectService = projectService;
this.utilsService = utilsService; this.utilsService = utilsService;
this.userService = userService; this.userService = userService;
this.entrepreneurService = entrepreneurService; this.entrepreneurService = entrepreneurService;
this.administratorService = administratorService;
this.appointmentService = appointmentService;
this.annotationService = annotationService;
} }
public void editSectionCell(Long sectionCellId, String content, String mail) { public void editSectionCell(Long sectionCellId, String content, String mail) {
SectionCell sectionCell = sectionCellService.getSectionCellById(sectionCellId); if (sectionCellId == null) {
if (sectionCell == null) { logger.warn("Trying to edit unknown section cell");
System.err.println("Trying to edit unknown section cell");
throw new ResponseStatusException( throw new ResponseStatusException(
HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas"); HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas");
} }
SectionCell sectionCell = sectionCellService.getSectionCellById(sectionCellId);
if (!utilsService.isAllowedToCheckProject( if (!utilsService.isAllowedToCheckProject(
mail, this.sectionCellService.getProjectId(sectionCellId))) { mail, this.sectionCellService.getProjectId(sectionCellId))) {
logger.warn( logger.warn(
@ -74,29 +80,30 @@ public class EntrepreneurApiService {
LocalDateTime.now(), LocalDateTime.now(),
sectionCell.getProjectSectionCell()); sectionCell.getProjectSectionCell());
newSectionCell.setIdReference(sectionCell.getIdReference()); newSectionCell.setIdReference(sectionCell.getIdReference());
this.addSectionCell(newSectionCell, mail);
sectionCell sectionCell
.getAppointmentSectionCell() .getAppointmentSectionCell()
.forEach( .forEach(
appointment -> { appointment -> {
newSectionCell.updateAppointmentSectionCell(appointment); this.appointmentService.updateAppointmentListSectionCell(
appointment.getIdAppointment(), newSectionCell);
}); });
sectionCell sectionCell
.getListAnnotation() .getListAnnotation()
.forEach( .forEach(
annotation -> { 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) { public void removeSectionCell(Long sectionCellId, String mail) {
SectionCell editSectionCell = sectionCellService.getSectionCellById(sectionCellId); if (sectionCellId == null) {
if (editSectionCell == null) { logger.warn("Trying to remove unknown section cell");
System.err.println("Trying to remove unknown section cell");
throw new ResponseStatusException( throw new ResponseStatusException(
HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas"); HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas");
} }
SectionCell editSectionCell = sectionCellService.getSectionCellById(sectionCellId);
if (!utilsService.isAllowedToCheckProject( if (!utilsService.isAllowedToCheckProject(
mail, this.sectionCellService.getProjectId(sectionCellId))) { mail, this.sectionCellService.getProjectId(sectionCellId))) {
logger.warn( logger.warn(
@ -130,12 +137,12 @@ public class EntrepreneurApiService {
public void addSectionCell(SectionCell sectionCell, String mail) { public void addSectionCell(SectionCell sectionCell, String mail) {
if (sectionCell == null) { 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( throw new ResponseStatusException(
HttpStatus.BAD_REQUEST, "La cellule de section fournie est vide"); HttpStatus.BAD_REQUEST, "La cellule de section fournie est vide");
} }
if (sectionCell.getSectionId() == -1) { 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( throw new ResponseStatusException(
HttpStatus.BAD_REQUEST, "La cellule de section fournie n'est pas valide"); HttpStatus.BAD_REQUEST, "La cellule de section fournie n'est pas valide");
} }
@ -161,28 +168,35 @@ public class EntrepreneurApiService {
.getAppointmentSectionCell() .getAppointmentSectionCell()
.forEach( .forEach(
appointment -> { appointment -> {
appointment.updateListSectionCell(newSectionCell); this.appointmentService.updateAppointmentListSectionCell(
appointment.getIdAppointment(), newSectionCell);
}); });
newSectionCell newSectionCell
.getListAnnotation() .getListAnnotation()
.forEach( .forEach(
annotation -> { annotation -> {
annotation.setSectionCellAnnotation(newSectionCell); this.annotationService.updateAnnotationSectionCell(
annotation.getIdAnnotation(), newSectionCell);
}); });
} }
public void requestNewProject(Project project, String mail) { public void requestNewProject(Project project, String mail) {
if (project == null) { 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"); throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Le projet fourni est vide");
} }
logger.info("User {} created a new project with id {}", mail, project.getIdProject()); logger.info("User {} created a new project named {}", mail, project.getProjectName());
project.setProjectStatus(PENDING);
project.setEntrepreneurProposed((Entrepreneur) this.userService.getUserByEmail(mail)); project.setEntrepreneurProposed((Entrepreneur) this.userService.getUserByEmail(mail));
projectService.addNewProject(project); 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.entrepreneurService.updateEntrepreneurProjectProposed(
this.userService.getUserByEmail(mail).getIdUser(), project); this.userService.getUserByEmail(mail).getIdUser(), project);
this.entrepreneurService.updateEntrepreneurProjectParticipation(
this.userService.getUserByEmail(mail).getIdUser(), project);
project.getListEntrepreneurParticipation() project.getListEntrepreneurParticipation()
.forEach( .forEach(
entrepreneur -> entrepreneur ->

View File

@ -1,6 +1,8 @@
package enseirb.myinpulse.service.database; package enseirb.myinpulse.service.database;
import enseirb.myinpulse.model.Administrator;
import enseirb.myinpulse.model.Annotation; import enseirb.myinpulse.model.Annotation;
import enseirb.myinpulse.model.SectionCell;
import enseirb.myinpulse.repository.AnnotationRepository; import enseirb.myinpulse.repository.AnnotationRepository;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -64,4 +66,16 @@ public class AnnotationService {
} }
return this.annotationRepository.save(annotation.get()); 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);
}
} }

View File

@ -4,13 +4,9 @@ import static enseirb.myinpulse.model.ProjectDecisionValue.*;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import enseirb.myinpulse.model.Entrepreneur; import enseirb.myinpulse.model.*;
import enseirb.myinpulse.model.Project;
import enseirb.myinpulse.model.SectionCell;
import enseirb.myinpulse.service.EntrepreneurApiService; import enseirb.myinpulse.service.EntrepreneurApiService;
import enseirb.myinpulse.service.database.EntrepreneurService; import enseirb.myinpulse.service.database.*;
import enseirb.myinpulse.service.database.ProjectService;
import enseirb.myinpulse.service.database.SectionCellService;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -21,6 +17,7 @@ import org.springframework.web.server.ResponseStatusException;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -35,6 +32,8 @@ public class EntrepreneurApiServiceTest {
@Autowired private EntrepreneurService entrepreneurService; @Autowired private EntrepreneurService entrepreneurService;
@Autowired private ProjectService projectService; @Autowired private ProjectService projectService;
@Autowired private SectionCellService sectionCellService; @Autowired private SectionCellService sectionCellService;
@Autowired private AnnotationService annotationService;
@Autowired private AppointmentService appointmentService;
@BeforeAll @BeforeAll
static void setup( static void setup(
@ -97,10 +96,23 @@ public class EntrepreneurApiServiceTest {
@Test @Test
void editValidSectionCell() { 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( entrepreneurApiService.editSectionCell(
IterableToList(sectionCells2).getLast().getIdSectionCell(), modified.getIdSectionCell(), "modified content", "entrepreneur@mail.fr");
"modified content",
"entrepreneur@mail.fr");
// We get the data from the database again. // We get the data from the database again.
SectionCell s = SectionCell s =
IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).getLast(); IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).getLast();
@ -111,6 +123,7 @@ public class EntrepreneurApiServiceTest {
@Test @Test
void editInvalidSectionCell() { void editInvalidSectionCell() {
System.out.println("editInvalidSectionCell : ");
assertThrows( assertThrows(
ResponseStatusException.class, ResponseStatusException.class,
() -> () ->
@ -125,6 +138,7 @@ public class EntrepreneurApiServiceTest {
@Test @Test
void editSectionCellInvalidAccess() { void editSectionCellInvalidAccess() {
System.out.println("editSectionCellInvalidAccess : ");
assertThrows( assertThrows(
ResponseStatusException.class, ResponseStatusException.class,
() -> () ->
@ -138,8 +152,19 @@ public class EntrepreneurApiServiceTest {
assertEquals("contenu très intéressant", s.getContentSectionCell()); 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 @Test
void removeValidSectionCell() { void removeValidSectionCell() {
System.out.println("removeValidSectionCell : ");
SectionCell tmpCell = SectionCell tmpCell =
sectionCellService.addNewSectionCell( sectionCellService.addNewSectionCell(
new SectionCell( new SectionCell(
@ -159,6 +184,7 @@ public class EntrepreneurApiServiceTest {
@Test @Test
void removeInvalidSectionCell() { void removeInvalidSectionCell() {
System.out.println("removeInvalidSectionCell : ");
assertThrows( assertThrows(
ResponseStatusException.class, ResponseStatusException.class,
() -> entrepreneurApiService.removeSectionCell(-1L, "entrepreneur@mail.fr")); () -> entrepreneurApiService.removeSectionCell(-1L, "entrepreneur@mail.fr"));
@ -168,11 +194,31 @@ public class EntrepreneurApiServiceTest {
assertEquals("contenu très intéressant", s.getContentSectionCell()); 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 @Test
void addValidSectionCell() { void addValidSectionCell() {
System.out.println("addValidSectionCell : ");
SectionCell added = SectionCell added =
sectionCellService.addNewSectionCell( sectionCellService.addNewSectionCell(
new SectionCell(null, 2L, "contenu ajouté", LocalDateTime.now(), project)); 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"); entrepreneurApiService.addSectionCell(added, "entrepreneur@mail.fr");
SectionCell s = SectionCell s =
IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).getLast(); IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).getLast();
@ -183,6 +229,7 @@ public class EntrepreneurApiServiceTest {
@Test @Test
void addSectionCellInvalidAccess() { void addSectionCellInvalidAccess() {
System.out.println("addSectionCellInvalidAccess : ");
SectionCell added = SectionCell added =
new SectionCell(null, 2L, "contenu ajouté", LocalDateTime.now(), project); new SectionCell(null, 2L, "contenu ajouté", LocalDateTime.now(), project);
assertThrows( assertThrows(
@ -197,6 +244,7 @@ public class EntrepreneurApiServiceTest {
@Test @Test
void addInvalidSectionCell() { void addInvalidSectionCell() {
System.out.println("addInvalidSectionCell : ");
SectionCell added = SectionCell added =
sectionCellService.addNewSectionCell( sectionCellService.addNewSectionCell(
new SectionCell(null, -1L, "contenu ajouté", LocalDateTime.now(), project)); new SectionCell(null, -1L, "contenu ajouté", LocalDateTime.now(), project));
@ -206,5 +254,71 @@ public class EntrepreneurApiServiceTest {
} }
@Test @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());
}
} }