Refactoring and new test

Renamed boolean in updateHeadSupervisor method from toAdd -> shouldChange

Added test to cover the branching in updateHeadSupervisor
This commit is contained in:
Nico Athanassiadis 2025-02-20 11:22:13 +01:00
parent 82c5a7a85a
commit 9bdd22d91d
2 changed files with 33 additions and 3 deletions
daisy-integration/src
main/java/se/su/dsv/scipro/integration/daisy/workers
test/java/se/su/dsv/scipro/integration/daisy/workers

@ -258,14 +258,14 @@ public class ProjectExporter extends AbstractWorker {
}
private void updateHeadSupervisor(Project project) throws ExternalExportException {
boolean toAdd = true;
boolean shouldChange = true;
Set<ProjectParticipant> contributors = daisyAPI.getContributors(project.getIdentifier());
for (ProjectParticipant contributor : contributors) {
if (contributor.getRole() == Role.SUPERVISOR) {
toAdd = !contributor.getPerson().getId().equals(project.getHeadSupervisor().getIdentifier());
shouldChange = !contributor.getPerson().getId().equals(project.getHeadSupervisor().getIdentifier());
}
}
if (toAdd) {
if (shouldChange) {
externalExporter.addContributorToProject(project, project.getHeadSupervisor(), Role.SUPERVISOR);
}
}

@ -2,6 +2,7 @@ package se.su.dsv.scipro.integration.daisy.workers;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ -172,6 +173,35 @@ public class ProjectExporterTest {
verify(externalExporter).addContributorToProject(exportedProject, newSupervisor, Role.SUPERVISOR);
}
@Test
public void should_not_change_head_supervisor_if_already_exists() throws Exception {
User newSupervisor = User.builder()
.firstName("New")
.lastName("Supervisor")
.emailAddress("new@supervisor.com")
.build();
Unit unit = new Unit();
unit.setIdentifier(239478);
newSupervisor.setIdentifier(SUPERVISOR_IDENTIFIER);
newSupervisor.setUnit(unit);
exportedProject.setHeadSupervisor(newSupervisor);
Set<ProjectParticipant> contributors = new HashSet<>();
ProjectParticipant oldSupervisor = new ProjectParticipant();
oldSupervisor.setRole(Role.SUPERVISOR);
Person oldPerson = new Person();
oldPerson.setId(SUPERVISOR_IDENTIFIER);
oldSupervisor.setPerson(oldPerson);
contributors.add(oldSupervisor);
when(daisyAPI.getContributors(exportedProject.getIdentifier())).thenReturn(contributors);
projectExporter.run();
verify(externalExporter, never()).addContributorToProject(exportedProject, newSupervisor, Role.SUPERVISOR);
}
@Test
public void adds_missing_co_supervisor_to_project() throws Exception {
User user = User.builder().firstName("co").lastName("supervisor").emailAddress("co@supervisor.com").build();