diff --git a/src/main/java/org/unilab/uniplan/lector/LectorService.java b/src/main/java/org/unilab/uniplan/lector/LectorService.java index f7236fc0..60ae5832 100644 --- a/src/main/java/org/unilab/uniplan/lector/LectorService.java +++ b/src/main/java/org/unilab/uniplan/lector/LectorService.java @@ -8,7 +8,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.unilab.uniplan.exception.ResourceNotFoundException; -import org.unilab.uniplan.faculty.FacultyService; +import org.unilab.uniplan.faculty.FacultyRepository; import org.unilab.uniplan.lector.dto.LectorDto; @Service @@ -19,12 +19,16 @@ public class LectorService { private final LectorMapper lectorMapper; - private final FacultyService facultyService; + private final FacultyRepository facultyRepository; @Transactional public LectorDto createLector(LectorDto lectorDto) { final Lector lector = lectorMapper.toEntity(lectorDto); + if (lectorDto.facultyId() != null) { + lector.setFaculty(facultyRepository.getReferenceById(lectorDto.facultyId())); + } + return saveEntityAndConvertToDto(lector); } @@ -58,8 +62,13 @@ public void deleteLector(UUID id) { } private LectorDto updateEntityAndConvertToDto(final LectorDto dto, - final Lector entity) { + final Lector entity) { lectorMapper.updateEntityFromDto(dto, entity); + + if (dto.facultyId() != null) { + entity.setFaculty(facultyRepository.getReferenceById(dto.facultyId())); + } + return saveEntityAndConvertToDto(entity); } diff --git a/src/test/java/org/unilab/uniplan/lector/LectorServiceTest.java b/src/test/java/org/unilab/uniplan/lector/LectorServiceTest.java index 10a90bcd..c72e98f2 100644 --- a/src/test/java/org/unilab/uniplan/lector/LectorServiceTest.java +++ b/src/test/java/org/unilab/uniplan/lector/LectorServiceTest.java @@ -1,12 +1,12 @@ package org.unilab.uniplan.lector; - import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; import java.util.List; @@ -19,6 +19,8 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.unilab.uniplan.exception.ResourceNotFoundException; +import org.unilab.uniplan.faculty.Faculty; +import org.unilab.uniplan.faculty.FacultyRepository; import org.unilab.uniplan.lector.dto.LectorDto; @ExtendWith(MockitoExtension.class) @@ -30,37 +32,73 @@ class LectorServiceTest { @Mock private LectorMapper lectorMapper; + @Mock + private FacultyRepository facultyRepository; + @InjectMocks private LectorService lectorService; private UUID id; private String firstName; - private String lastName; + private String lastName; private UUID facultyId; private String email; private LectorDto lectorDto; + private LectorDto lectorDtoWithoutFaculty; + + @Mock private Lector lector; + @Mock + private Faculty faculty; + @BeforeEach - void setUp(){ + void setUp() { id = UUID.randomUUID(); facultyId = UUID.randomUUID(); firstName = "Ivan"; lastName = "Ivanov"; email = "i.ivanov@gmail.com"; lectorDto = new LectorDto(id, facultyId, email, firstName, lastName); - lector = new Lector(); + lectorDtoWithoutFaculty = new LectorDto(id, null, email, firstName, lastName); } @Test void testCreateLectorShouldSaveAndReturnDto() { + when(lectorMapper.toEntity(lectorDtoWithoutFaculty)).thenReturn(lector); + when(lectorRepository.save(lector)).thenReturn(lector); + when(lectorMapper.toDto(lector)).thenReturn(lectorDtoWithoutFaculty); + + LectorDto result = lectorService.createLector(lectorDtoWithoutFaculty); + + assertEquals(lectorDtoWithoutFaculty, result); + verifyNoInteractions(facultyRepository); + } + + @Test + void testCreateLectorShouldSetFacultyWhenFacultyIdIsNotNull() { when(lectorMapper.toEntity(lectorDto)).thenReturn(lector); + when(facultyRepository.getReferenceById(facultyId)).thenReturn(faculty); when(lectorRepository.save(lector)).thenReturn(lector); when(lectorMapper.toDto(lector)).thenReturn(lectorDto); LectorDto result = lectorService.createLector(lectorDto); assertEquals(lectorDto, result); + verify(facultyRepository).getReferenceById(facultyId); + verify(lector).setFaculty(faculty); + } + + @Test + void testCreateLectorShouldNotSetFacultyWhenFacultyIdIsNull() { + when(lectorMapper.toEntity(lectorDtoWithoutFaculty)).thenReturn(lector); + when(lectorRepository.save(lector)).thenReturn(lector); + when(lectorMapper.toDto(lector)).thenReturn(lectorDtoWithoutFaculty); + + LectorDto result = lectorService.createLector(lectorDtoWithoutFaculty); + + assertEquals(lectorDtoWithoutFaculty, result); + verifyNoInteractions(facultyRepository); } @Test @@ -87,7 +125,7 @@ void testGetLectorByIdShouldReturnLectorDtoIfFound() { } @Test - void testGetLectorByIdShouldReturnEmptyOptionalIfLectorNotFound() { + void testGetLectorByIdShouldThrowIfNotFound() { when(lectorRepository.findById(id)).thenReturn(Optional.empty()); ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class, @@ -98,23 +136,51 @@ void testGetLectorByIdShouldReturnEmptyOptionalIfLectorNotFound() { @Test void testUpdateLectorShouldUpdateAndReturnDtoIfFound() { + when(lectorRepository.findById(id)).thenReturn(Optional.of(lector)); + doAnswer(invocation -> null).when(lectorMapper).updateEntityFromDto(lectorDtoWithoutFaculty, lector); + when(lectorRepository.save(lector)).thenReturn(lector); + when(lectorMapper.toDto(lector)).thenReturn(lectorDtoWithoutFaculty); + + LectorDto result = lectorService.updateLector(id, lectorDtoWithoutFaculty); + + assertEquals(lectorDtoWithoutFaculty, result); + verifyNoInteractions(facultyRepository); + } + + @Test + void testUpdateLectorShouldSetFacultyWhenFacultyIdIsNotNull() { when(lectorRepository.findById(id)).thenReturn(Optional.of(lector)); doAnswer(invocation -> null).when(lectorMapper).updateEntityFromDto(lectorDto, lector); + when(facultyRepository.getReferenceById(facultyId)).thenReturn(faculty); when(lectorRepository.save(lector)).thenReturn(lector); when(lectorMapper.toDto(lector)).thenReturn(lectorDto); LectorDto result = lectorService.updateLector(id, lectorDto); assertEquals(lectorDto, result); + verify(facultyRepository).getReferenceById(facultyId); + verify(lector).setFaculty(faculty); } @Test - void testUpdateLectorShouldReturnEmptyOptionalIfNotFound() { + void testUpdateLectorShouldNotSetFacultyWhenFacultyIdIsNull() { + when(lectorRepository.findById(id)).thenReturn(Optional.of(lector)); + doAnswer(invocation -> null).when(lectorMapper).updateEntityFromDto(lectorDtoWithoutFaculty, lector); + when(lectorRepository.save(lector)).thenReturn(lector); + when(lectorMapper.toDto(lector)).thenReturn(lectorDtoWithoutFaculty); + + LectorDto result = lectorService.updateLector(id, lectorDtoWithoutFaculty); + + assertEquals(lectorDtoWithoutFaculty, result); + verifyNoInteractions(facultyRepository); + } + + @Test + void testUpdateLectorShouldThrowIfNotFound() { when(lectorRepository.findById(id)).thenReturn(Optional.empty()); ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class, - () -> lectorService.updateLector(id, - lectorDto)); + () -> lectorService.updateLector(id, lectorDto)); assertTrue(exception.getMessage().contains(String.valueOf(id))); } @@ -132,9 +198,9 @@ void testDeleteLectorShouldDeleteLectorIfFound() { void testDeleteLectorShouldThrowIfNotFound() { when(lectorRepository.findById(id)).thenReturn(Optional.empty()); - ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class, () -> - lectorService.deleteLector(id)); + ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class, + () -> lectorService.deleteLector(id)); assertTrue(exception.getMessage().contains(String.valueOf(id))); } -} +} \ No newline at end of file