package enseirb.myinpulse; import static enseirb.myinpulse.model.ProjectDecisionValue.*; import static org.junit.jupiter.api.Assertions.*; import enseirb.myinpulse.model.Entrepreneur; import enseirb.myinpulse.model.Project; import enseirb.myinpulse.model.SectionCell; import enseirb.myinpulse.service.EntrepreneurApiService; import enseirb.myinpulse.service.database.EntrepreneurService; import enseirb.myinpulse.service.database.ProjectService; import enseirb.myinpulse.service.database.SectionCellService; 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.util.ArrayList; import java.util.List; @SpringBootTest @Transactional public class EntrepreneurApiServiceTest { private static Entrepreneur entrepreneur; private static Iterable sectionCells; @Autowired private EntrepreneurApiService entrepreneurApiService; @Autowired private EntrepreneurService entrepreneurService; @Autowired private ProjectService projectService; @Autowired private SectionCellService sectionCellService; @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 project = projectService.addNewProject( new Project("Project", null, LocalDate.now(), ACTIVE, null, entrepreneur)); entrepreneur.setProjectParticipation(project); sectionCellService.addNewSectionCell( new SectionCell( null, 2L, "contenu très intéressant", LocalDateTime.now(), projectService.getProjectByName("Project"))); sectionCells = sectionCellService.getSectionCellsByProject( projectService.getProjectByName("Project"), 2L); } private List IterableToList(Iterable iterable) { List l = new ArrayList<>(); iterable.forEach(l::add); return l; } @Test void editValidSectionCell() { System.out.println(sectionCells); entrepreneurApiService.editSectionCell( IterableToList(sectionCells).getFirst().getIdSectionCell(), "modified content", "entrepreneur@mail.fr"); assertEquals( "modified content", IterableToList(sectionCells).getFirst().getContentSectionCell()); } @Test void editInvalidSectionCell() { assertThrows( ResponseStatusException.class, () -> entrepreneurApiService.editSectionCell( -1L, "should not be modified", "entrepreneur@mail.fr")); } @Test void editSectionCellInvalidAccess() { assertThrows( ResponseStatusException.class, () -> entrepreneurApiService.editSectionCell( IterableToList(sectionCells).getFirst().getIdSectionCell(), "should not be modified", "testentrepreneur@mail.fr")); assertEquals( "contenu très intéressant", IterableToList(sectionCells).getFirst().getContentSectionCell()); } @Test void removeValidSectionCell() { SectionCell tmpCell = sectionCellService.addNewSectionCell( new SectionCell( null, 2L, "contenu temporaire", LocalDateTime.now(), projectService.getProjectByName("Project"))); assertEquals( 2, IterableToList( sectionCellService.getSectionCellsByProject( projectService.getProjectByName("Project"), 2L)) .size()); entrepreneurApiService.removeSectionCell( tmpCell.getIdSectionCell(), "entrepreneur@mail.fr"); assertEquals( 1, IterableToList( sectionCellService.getSectionCellsByProject( projectService.getProjectByName("Project"), 2L)) .size()); } @Test void removeInvalidSectionCell() { assertThrows( ResponseStatusException.class, () -> entrepreneurApiService.removeSectionCell(-1L, "entrepreneur@mail.fr")); } }