package enseirb.myinpulse; import static enseirb.myinpulse.model.ProjectDecisionValue.*; import static org.junit.jupiter.api.Assertions.*; import enseirb.myinpulse.model.*; import enseirb.myinpulse.service.EntrepreneurApiService; import enseirb.myinpulse.service.database.*; 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.time.LocalTime; import java.util.ArrayList; import java.util.List; @SpringBootTest @Transactional public class EntrepreneurApiServiceTest { private static Entrepreneur entrepreneur; private static Project project; private static Iterable sectionCells2; private static Iterable sectionCells3; @Autowired private EntrepreneurApiService entrepreneurApiService; @Autowired private EntrepreneurService entrepreneurService; @Autowired private ProjectService projectService; @Autowired private SectionCellService sectionCellService; @Autowired private AnnotationService annotationService; @Autowired private AppointmentService appointmentService; @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 = projectService.addNewProject( new Project("Project", null, LocalDate.now(), ACTIVE, null, entrepreneur)); entrepreneurService.updateEntrepreneurProjectProposed(entrepreneur.getIdUser(), project); entrepreneurService.updateEntrepreneurProjectParticipation( entrepreneur.getIdUser(), project); SectionCell s1 = sectionCellService.addNewSectionCell( new SectionCell( null, 2L, "contenu très intéressant", LocalDateTime.now(), project)); SectionCell s2 = sectionCellService.addNewSectionCell( new SectionCell( null, 3L, "contenu très intéressant2", LocalDateTime.now(), project)); sectionCells2 = sectionCellService.getSectionCellsByProject(project, 2L); sectionCells3 = sectionCellService.getSectionCellsByProject(project, 3L); } private List IterableToList(Iterable iterable) { List l = new ArrayList<>(); iterable.forEach(l::add); return l; } @Test 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( modified.getIdSectionCell(), "modified content", "entrepreneur@mail.fr"); // We get the data from the database again. SectionCell s = IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).getLast(); assertEquals("modified content", s.getContentSectionCell()); assertEquals( 2, IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).size()); } @Test void editInvalidSectionCell() { System.out.println("editInvalidSectionCell : "); assertThrows( ResponseStatusException.class, () -> entrepreneurApiService.editSectionCell( -1L, "should not be modified", "entrepreneur@mail.fr")); SectionCell s = IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).getLast(); assertEquals("contenu très intéressant", s.getContentSectionCell()); assertEquals( 1, IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).size()); } @Test void editSectionCellInvalidAccess() { System.out.println("editSectionCellInvalidAccess : "); assertThrows( ResponseStatusException.class, () -> entrepreneurApiService.editSectionCell( IterableToList(sectionCells3).getFirst().getIdSectionCell(), "should not be modified", "testentrepreneur@mail.fr")); SectionCell s = IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).getFirst(); 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 void removeValidSectionCell() { System.out.println("removeValidSectionCell : "); SectionCell tmpCell = sectionCellService.addNewSectionCell( new SectionCell( null, 2L, "contenu temporaire", LocalDateTime.now(), project)); assertEquals( 2, IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).size()); assertDoesNotThrow( () -> entrepreneurApiService.removeSectionCell( tmpCell.getIdSectionCell(), "entrepreneur@mail.fr")); assertEquals( tmpCell.getIdReference(), IterableToList(sectionCellService.getSectionCellsByProject(project, -1L)) .getLast() .getIdReference()); } @Test void removeInvalidSectionCell() { System.out.println("removeInvalidSectionCell : "); assertThrows( ResponseStatusException.class, () -> entrepreneurApiService.removeSectionCell(-1L, "entrepreneur@mail.fr")); SectionCell s = IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).getFirst(); 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 void addValidSectionCell() { System.out.println("addValidSectionCell : "); SectionCell added = sectionCellService.addNewSectionCell( 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"); SectionCell s = IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).getLast(); assertEquals("contenu ajouté", s.getContentSectionCell()); assertEquals( 2, IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).size()); } @Test void addSectionCellInvalidAccess() { System.out.println("addSectionCellInvalidAccess : "); SectionCell added = new SectionCell(null, 2L, "contenu ajouté", LocalDateTime.now(), project); assertThrows( ResponseStatusException.class, () -> entrepreneurApiService.addSectionCell(added, "fauxentrepreneur@mail.fr")); SectionCell s = IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).getLast(); assertEquals( 1, IterableToList(sectionCellService.getSectionCellsByProject(project, 2L)).size()); assertEquals("contenu très intéressant", s.getContentSectionCell()); } @Test void addInvalidSectionCell() { System.out.println("addInvalidSectionCell : "); SectionCell added = sectionCellService.addNewSectionCell( new SectionCell(null, -1L, "contenu ajouté", LocalDateTime.now(), project)); assertThrows( ResponseStatusException.class, () -> entrepreneurApiService.addSectionCell(added, "entrepreneur@mail.fr")); } @Test 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()); } }