feat: started test for EntrepreneurServiceApi
This commit is contained in:
parent
137bc84c21
commit
5b6b647697
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,5 +1,6 @@
|
|||||||
.env
|
.env
|
||||||
.idea
|
.idea
|
||||||
keycloak/CAS/target
|
keycloak/CAS/target
|
||||||
|
keycloak/.installed
|
||||||
docker-compose.yaml
|
docker-compose.yaml
|
||||||
postgres/data
|
postgres/data
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package enseirb.myinpulse.controller;
|
package enseirb.myinpulse.controller;
|
||||||
|
|
||||||
import enseirb.myinpulse.model.SectionCell;
|
|
||||||
import enseirb.myinpulse.model.Project;
|
import enseirb.myinpulse.model.Project;
|
||||||
|
import enseirb.myinpulse.model.SectionCell;
|
||||||
import enseirb.myinpulse.service.EntrepreneurApiService;
|
import enseirb.myinpulse.service.EntrepreneurApiService;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -31,10 +31,10 @@ public class EntrepreneurApi {
|
|||||||
@PutMapping("/entrepreneur/lcsection/modify/{sectionId}")
|
@PutMapping("/entrepreneur/lcsection/modify/{sectionId}")
|
||||||
public void editSectionCell(
|
public void editSectionCell(
|
||||||
@PathVariable Long sectionId,
|
@PathVariable Long sectionId,
|
||||||
@RequestBody SectionCell sectionCell,
|
@RequestBody String content,
|
||||||
@AuthenticationPrincipal Jwt principal) {
|
@AuthenticationPrincipal Jwt principal) {
|
||||||
entrepreneurApiService.editSectionCell(
|
entrepreneurApiService.editSectionCell(
|
||||||
sectionId, sectionCell, principal.getClaimAsString("email"));
|
sectionId, content, principal.getClaimAsString("email"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -5,6 +5,7 @@ import jakarta.persistence.*;
|
|||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "project")
|
@Table(name = "project")
|
||||||
@ -66,6 +67,21 @@ public class Project {
|
|||||||
this.entrepreneurProposed = entrepreneurProposed;
|
this.entrepreneurProposed = entrepreneurProposed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
Project project = (Project) o;
|
||||||
|
return Objects.equals(listEntrepreneurParticipation, project.listEntrepreneurParticipation)
|
||||||
|
&& Objects.equals(listSectionCell, project.listSectionCell)
|
||||||
|
&& Objects.equals(idProject, project.idProject)
|
||||||
|
&& Objects.equals(projectName, project.projectName)
|
||||||
|
&& Objects.deepEquals(logo, project.logo)
|
||||||
|
&& Objects.equals(creationDate, project.creationDate)
|
||||||
|
&& projectStatus == project.projectStatus
|
||||||
|
&& Objects.equals(projectAdministrator, project.projectAdministrator)
|
||||||
|
&& Objects.equals(entrepreneurProposed, project.entrepreneurProposed);
|
||||||
|
}
|
||||||
|
|
||||||
public Long getIdProject() {
|
public Long getIdProject() {
|
||||||
return idProject;
|
return idProject;
|
||||||
}
|
}
|
||||||
|
@ -33,9 +33,9 @@ public class EntrepreneurApiService {
|
|||||||
this.utilsService = utilsService;
|
this.utilsService = utilsService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void editSectionCell(Long sectionCellId, SectionCell sectionCell, String mail) {
|
public void editSectionCell(Long sectionCellId, String content, String mail) {
|
||||||
SectionCell editSectionCell = sectionCellService.getSectionCellById(sectionCellId);
|
SectionCell sectionCell = sectionCellService.getSectionCellById(sectionCellId);
|
||||||
if (editSectionCell == null) {
|
if (sectionCell == null) {
|
||||||
System.err.println("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");
|
||||||
@ -55,11 +55,7 @@ public class EntrepreneurApiService {
|
|||||||
mail,
|
mail,
|
||||||
sectionCellId,
|
sectionCellId,
|
||||||
this.sectionCellService.getProjectId(sectionCellId));
|
this.sectionCellService.getProjectId(sectionCellId));
|
||||||
sectionCellService.updateSectionCell(
|
sectionCellService.updateSectionCell(sectionCellId, content);
|
||||||
sectionCellId,
|
|
||||||
sectionCell.getSectionId(),
|
|
||||||
sectionCell.getContentSectionCell(),
|
|
||||||
sectionCell.getModificationDate());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeSectionCell(Long sectionCellId, String mail) {
|
public void removeSectionCell(Long sectionCellId, String mail) {
|
||||||
|
@ -50,22 +50,16 @@ public class SectionCellService {
|
|||||||
this.sectionCellRepository.deleteById(id);
|
this.sectionCellRepository.deleteById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SectionCell updateSectionCell(
|
public SectionCell updateSectionCell(Long id, String contentSectionCell) {
|
||||||
Long id, Long sectionId, String contentSectionCell, LocalDateTime modificationDate) {
|
|
||||||
Optional<SectionCell> sectionCell = this.sectionCellRepository.findById(id);
|
Optional<SectionCell> sectionCell = this.sectionCellRepository.findById(id);
|
||||||
if (sectionCell.isEmpty()) {
|
if (sectionCell.isEmpty()) {
|
||||||
logger.error("updateSectionCell : No sectionCell found with id {}", id);
|
logger.error("updateSectionCell : No sectionCell found with id {}", id);
|
||||||
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");
|
||||||
}
|
}
|
||||||
if (sectionId != null) {
|
|
||||||
sectionCell.get().setSectionId(sectionId);
|
|
||||||
}
|
|
||||||
if (contentSectionCell != null) {
|
if (contentSectionCell != null) {
|
||||||
sectionCell.get().setContentSectionCell(contentSectionCell);
|
sectionCell.get().setContentSectionCell(contentSectionCell);
|
||||||
}
|
sectionCell.get().setModificationDate(LocalDateTime.now());
|
||||||
if (modificationDate != null) {
|
|
||||||
sectionCell.get().setModificationDate(modificationDate);
|
|
||||||
}
|
}
|
||||||
return this.sectionCellRepository.save(sectionCell.get());
|
return this.sectionCellRepository.save(sectionCell.get());
|
||||||
}
|
}
|
||||||
|
@ -106,7 +106,7 @@ public class AdminApiServiceTest {
|
|||||||
List<Project> l = IterableToList(projects);
|
List<Project> l = IterableToList(projects);
|
||||||
assertEquals(1, l.size());
|
assertEquals(1, l.size());
|
||||||
Project p = l.getFirst();
|
Project p = l.getFirst();
|
||||||
assertEquals(p.getProjectName(), "sampleProjectAdminApiService");
|
assertEquals("sampleProjectAdminApiService", p.getProjectName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -180,7 +180,7 @@ public class AdminApiServiceTest {
|
|||||||
@Test
|
@Test
|
||||||
void addProjectToAdmin() {
|
void addProjectToAdmin() {
|
||||||
assertEquals(0, administrator.getListProject().size());
|
assertEquals(0, administrator.getListProject().size());
|
||||||
Project p1 = new Project("assProjectToAdmin", null, LocalDate.now(), ACTIVE, administrator);
|
Project p1 = new Project("addProjectToAdmin", null, LocalDate.now(), ACTIVE, administrator);
|
||||||
this.adminApiService.addNewProject(p1);
|
this.adminApiService.addNewProject(p1);
|
||||||
assertEquals(1, administrator.getListProject().size());
|
assertEquals(1, administrator.getListProject().size());
|
||||||
}
|
}
|
||||||
@ -189,7 +189,7 @@ public class AdminApiServiceTest {
|
|||||||
void addProjectToUser() {
|
void addProjectToUser() {
|
||||||
assertNull(entrepreneur.getProjectParticipation());
|
assertNull(entrepreneur.getProjectParticipation());
|
||||||
Project p1 =
|
Project p1 =
|
||||||
new Project("assProjectToAdmin", null, LocalDate.now(), ACTIVE, null, entrepreneur);
|
new Project("addProjectToAdmin", null, LocalDate.now(), ACTIVE, null, entrepreneur);
|
||||||
this.adminApiService.addNewProject(p1);
|
this.adminApiService.addNewProject(p1);
|
||||||
assertEquals(p1, entrepreneur.getProjectParticipation());
|
assertEquals(p1, entrepreneur.getProjectParticipation());
|
||||||
}
|
}
|
||||||
@ -202,7 +202,7 @@ public class AdminApiServiceTest {
|
|||||||
assertNull(e1.getProjectParticipation());
|
assertNull(e1.getProjectParticipation());
|
||||||
assertNull(e2.getProjectParticipation());
|
assertNull(e2.getProjectParticipation());
|
||||||
assertNull(e3.getProjectParticipation());
|
assertNull(e3.getProjectParticipation());
|
||||||
Project p1 = new Project("assProjectToAdmin", null, LocalDate.now(), ACTIVE, null, null);
|
Project p1 = new Project("addProjectToAdmin", null, LocalDate.now(), ACTIVE, null, null);
|
||||||
p1.updateListEntrepreneurParticipation(e1);
|
p1.updateListEntrepreneurParticipation(e1);
|
||||||
p1.updateListEntrepreneurParticipation(e2);
|
p1.updateListEntrepreneurParticipation(e2);
|
||||||
p1.updateListEntrepreneurParticipation(e3);
|
p1.updateListEntrepreneurParticipation(e3);
|
||||||
|
@ -0,0 +1,152 @@
|
|||||||
|
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<SectionCell> 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 <T> List<T> IterableToList(Iterable<T> iterable) {
|
||||||
|
List<T> 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"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user