312 lines
12 KiB
Java
312 lines
12 KiB
Java
package enseirb.myinpulse;
|
|
|
|
import static enseirb.myinpulse.model.ProjectDecisionValue.*;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
import enseirb.myinpulse.model.*;
|
|
import enseirb.myinpulse.service.AdminApiService;
|
|
import enseirb.myinpulse.service.database.AdministratorService;
|
|
import enseirb.myinpulse.service.database.AppointmentService;
|
|
import enseirb.myinpulse.service.database.EntrepreneurService;
|
|
import enseirb.myinpulse.service.database.ProjectService;
|
|
|
|
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.LocalTime;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
@SpringBootTest
|
|
@Transactional
|
|
public class AdminApiServiceTest {
|
|
private static long administratorid;
|
|
private static Administrator administrator;
|
|
private static Entrepreneur entrepreneur;
|
|
private static Appointment appt;
|
|
@Autowired private AdminApiService adminApiService;
|
|
@Autowired private ProjectService projectService;
|
|
@Autowired private EntrepreneurService entrepreneurService;
|
|
|
|
@BeforeAll
|
|
static void setup(
|
|
@Autowired AdministratorService administratorService,
|
|
@Autowired ProjectService projectService,
|
|
@Autowired EntrepreneurService entrepreneurService,
|
|
@Autowired AppointmentService appoitmentService) {
|
|
administratorService.addAdministrator(
|
|
new Administrator(
|
|
"admin",
|
|
"admin",
|
|
"testAdminEmpty@example.com",
|
|
"testAdmin@example.com",
|
|
""));
|
|
administrator =
|
|
administratorService.addAdministrator(
|
|
new Administrator(
|
|
"admin2",
|
|
"admin2",
|
|
"testAdminFull@example.com",
|
|
"testAdmin@example.com",
|
|
""));
|
|
administratorid = administrator.getIdUser();
|
|
|
|
entrepreneur =
|
|
new Entrepreneur(
|
|
"JeSuisUnEntrepreneurDeCompet",
|
|
"EtUé",
|
|
"Entrepreneur@inpulse.com",
|
|
"mail2",
|
|
"phone",
|
|
"Ensimag nan jdeconne ENSEIRB (-matmeca mais on s'en fout)",
|
|
"info ofc",
|
|
false);
|
|
entrepreneurService.addEntrepreneur(entrepreneur);
|
|
|
|
Entrepreneur entrepreneur2 =
|
|
new Entrepreneur(
|
|
"GDProjets", "", "Entrepreneur2@inpulse.com", "", "", "", "info ofc", true);
|
|
entrepreneurService.addEntrepreneur(entrepreneur2);
|
|
|
|
Project p =
|
|
projectService.addNewProject(
|
|
new Project(
|
|
"sampleProjectAdminApiService",
|
|
null,
|
|
LocalDate.now(),
|
|
ACTIVE,
|
|
administratorService.getAdministratorByPrimaryMain(
|
|
"testAdminFull@example.com")));
|
|
|
|
entrepreneurService.updateEntrepreneurProjectParticipation(entrepreneur2.getIdUser(), p);
|
|
|
|
appt =
|
|
new Appointment(
|
|
null,
|
|
LocalDate.now(),
|
|
LocalTime.now(),
|
|
LocalTime.now(),
|
|
"Salle TD 15",
|
|
"Discussion importante");
|
|
appoitmentService.addNewAppointment(appt);
|
|
}
|
|
|
|
private <T> List<T> IterableToList(Iterable<T> iterable) {
|
|
List<T> l = new ArrayList<>();
|
|
iterable.forEach(l::add);
|
|
return l;
|
|
}
|
|
|
|
@Test
|
|
void getProjectOfAdminIsEmpty() {
|
|
Iterable<Project> projects =
|
|
adminApiService.getProjectsOfAdmin("testAdminEmpty@example.com");
|
|
assertEquals(0, IterableToList(projects).size());
|
|
}
|
|
|
|
@Test
|
|
void getProjectOfInexistantAdminFails() {
|
|
String nonExistentAdminEmail = "testInexistantAdmin@example.com";
|
|
|
|
assertThrows(
|
|
ResponseStatusException.class,
|
|
() -> {
|
|
adminApiService.getProjectsOfAdmin(nonExistentAdminEmail);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
void getProjectOfAdminNotEmpty() {
|
|
Iterable<Project> projects =
|
|
adminApiService.getProjectsOfAdmin("testAdminFull@example.com");
|
|
List<Project> l = IterableToList(projects);
|
|
assertEquals(1, l.size());
|
|
Project p = l.getFirst();
|
|
assertEquals("sampleProjectAdminApiService", p.getProjectName());
|
|
}
|
|
|
|
@Test
|
|
void getPendingProjectsEmpty() {
|
|
assertEquals(0, IterableToList(this.adminApiService.getPendingProjects()).size());
|
|
}
|
|
|
|
@Test
|
|
void getPendingProjectsNotEmpty() {
|
|
this.projectService.addNewProject(
|
|
new Project(
|
|
"PendingProjectAdminApiService1", null, LocalDate.now(), PENDING, null));
|
|
this.projectService.addNewProject(
|
|
new Project(
|
|
"PendingProjectAdminApiService2", null, LocalDate.now(), PENDING, null));
|
|
Iterable<Project> pendingProjects = this.adminApiService.getPendingProjects();
|
|
List<Project> pendingProjectsList = IterableToList(pendingProjects);
|
|
assertEquals(2, pendingProjectsList.size());
|
|
assertTrue(
|
|
List.of("PendingProjectAdminApiService1", "PendingProjectAdminApiService2")
|
|
.contains(pendingProjectsList.getFirst().getProjectName()));
|
|
assertTrue(
|
|
List.of("PendingProjectAdminApiService1", "PendingProjectAdminApiService2")
|
|
.contains(pendingProjectsList.getLast().getProjectName()));
|
|
}
|
|
|
|
@Test
|
|
void validateInexistantProject() {
|
|
ProjectDecision d = new ProjectDecision(-1, 0, 1);
|
|
assertThrows(ResponseStatusException.class, () -> this.adminApiService.validateProject(d));
|
|
}
|
|
|
|
@Test
|
|
void validateExistantProject() {
|
|
Project p =
|
|
new Project("PendingProjectAdminApiService2", null, LocalDate.now(), PENDING, null);
|
|
this.projectService.addNewProject(p);
|
|
assertEquals(PENDING, p.getProjectStatus());
|
|
ProjectDecision d = new ProjectDecision(p.getIdProject(), administratorid, 1);
|
|
this.adminApiService.validateProject(d);
|
|
assertEquals(ACTIVE, p.getProjectStatus());
|
|
|
|
// Check if the project was really updated in the database
|
|
assertEquals(0, IterableToList(this.adminApiService.getPendingProjects()).size());
|
|
}
|
|
|
|
@Test
|
|
void refuseExistantProject() {
|
|
Project p =
|
|
new Project("PendingProjectAdminApiService2", null, LocalDate.now(), PENDING, null);
|
|
this.projectService.addNewProject(p);
|
|
assertEquals(PENDING, p.getProjectStatus());
|
|
ProjectDecision d = new ProjectDecision(p.getIdProject(), administratorid, 0);
|
|
this.adminApiService.validateProject(d);
|
|
assertEquals(REJECTED, p.getProjectStatus());
|
|
|
|
// Check if the project was really updated in the database
|
|
assertEquals(0, IterableToList(this.adminApiService.getPendingProjects()).size());
|
|
}
|
|
|
|
@Test
|
|
void addProject() {
|
|
assertEquals(0, IterableToList(this.adminApiService.getPendingProjects()).size());
|
|
Project p1 =
|
|
new Project("PendingProjectAdminApiService2", null, LocalDate.now(), PENDING, null);
|
|
this.adminApiService.addNewProject(p1);
|
|
|
|
assertEquals(1, IterableToList(this.adminApiService.getPendingProjects()).size());
|
|
}
|
|
|
|
@Test
|
|
void addProjectToAdmin() {
|
|
assertEquals(0, administrator.getListProject().size());
|
|
Project p1 = new Project("addProjectToAdmin", null, LocalDate.now(), ACTIVE, administrator);
|
|
this.adminApiService.addNewProject(p1);
|
|
assertEquals(1, administrator.getListProject().size());
|
|
}
|
|
|
|
@Test
|
|
void addProjectToUser() {
|
|
assertNull(entrepreneur.getProjectParticipation());
|
|
Project p1 =
|
|
new Project("addProjectToAdmin", null, LocalDate.now(), ACTIVE, null, entrepreneur);
|
|
this.adminApiService.addNewProject(p1);
|
|
assertEquals(p1, entrepreneur.getProjectParticipation());
|
|
}
|
|
|
|
@Test
|
|
void addProjectWithManyUsers() {
|
|
Entrepreneur e1 = new Entrepreneur();
|
|
Entrepreneur e2 = new Entrepreneur();
|
|
Entrepreneur e3 = new Entrepreneur();
|
|
assertNull(e1.getProjectParticipation());
|
|
assertNull(e2.getProjectParticipation());
|
|
assertNull(e3.getProjectParticipation());
|
|
Project p1 = new Project("addProjectToAdmin", null, LocalDate.now(), ACTIVE, null, null);
|
|
p1.updateListEntrepreneurParticipation(e1);
|
|
p1.updateListEntrepreneurParticipation(e2);
|
|
p1.updateListEntrepreneurParticipation(e3);
|
|
this.adminApiService.addNewProject(p1);
|
|
assertEquals(p1, e1.getProjectParticipation());
|
|
assertEquals(p1, e2.getProjectParticipation());
|
|
assertEquals(p1, e3.getProjectParticipation());
|
|
}
|
|
|
|
@Test
|
|
void addDuplicateProject() {
|
|
Project p1 =
|
|
new Project("PendingProjectAdminApiService2", null, LocalDate.now(), PENDING, null);
|
|
Project p2 =
|
|
new Project("PendingProjectAdminApiService2", null, LocalDate.now(), PENDING, null);
|
|
this.adminApiService.addNewProject(p1);
|
|
assertThrows(ResponseStatusException.class, () -> this.adminApiService.addNewProject(p2));
|
|
}
|
|
|
|
// We could do a delete active project, but it's not really useful.
|
|
@Test
|
|
void deletePendingProject() {
|
|
int oldsize = IterableToList(this.adminApiService.getPendingProjects()).size();
|
|
Project p1 =
|
|
new Project("PendingProjectAdminApiService2", null, LocalDate.now(), PENDING, null);
|
|
Project p2 = this.adminApiService.addNewProject(p1);
|
|
|
|
assertEquals(oldsize + 1, IterableToList(this.adminApiService.getPendingProjects()).size());
|
|
this.adminApiService.deleteProject(p2.getIdProject());
|
|
|
|
assertEquals(oldsize, IterableToList(this.adminApiService.getPendingProjects()).size());
|
|
for (int i = 0; i < oldsize; i++) {
|
|
assertNotEquals(
|
|
p1.getIdProject(),
|
|
IterableToList(this.adminApiService.getPendingProjects())
|
|
.get(i)
|
|
.getIdProject());
|
|
}
|
|
}
|
|
|
|
@Test
|
|
void getUpcommingAppointmentUnkwnownUser() {
|
|
assertThrows(
|
|
ResponseStatusException.class,
|
|
() -> {
|
|
Iterable<Appointment> a =
|
|
this.adminApiService.getUpcomingAppointments(
|
|
"entrepreneur-inexistent@mail.fr");
|
|
});
|
|
}
|
|
|
|
@Test
|
|
void getUpcommingAppointmentNoProject() {
|
|
assertThrows(
|
|
ResponseStatusException.class,
|
|
() -> {
|
|
Iterable<Appointment> a =
|
|
this.adminApiService.getUpcomingAppointments(
|
|
"Entrepreneur@inpulse.com");
|
|
});
|
|
}
|
|
|
|
@Test
|
|
void getUpcommingAppointmentEmpty() {
|
|
Iterable<Appointment> a =
|
|
this.adminApiService.getUpcomingAppointments("Entrepreneur2@inpulse.com");
|
|
assertEquals(0, IterableToList(a).size());
|
|
}
|
|
|
|
@Test
|
|
void validateEntrepreneurAccount() {
|
|
assertTrue(entrepreneurService.getEntrepreneurById(entrepreneur.getIdUser()).isPending());
|
|
adminApiService.validateEntrepreneurAccount(entrepreneur.getIdUser(), "");
|
|
assertFalse(entrepreneurService.getEntrepreneurById(entrepreneur.getIdUser()).isPending());
|
|
}
|
|
|
|
@Test
|
|
void testCreateApptRepport() {
|
|
this.adminApiService.createAppointmentReport(
|
|
appt.getIdAppointment(),
|
|
new Report(null, "je rapporte de fou"),
|
|
"testAdminFull@example.com");
|
|
}
|
|
}
|