Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/main/java/org/unilab/uniplan/lector/LectorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Internal dtos will not be used.

lector.setFaculty(facultyRepository.getReferenceById(lectorDto.facultyId()));
}

return saveEntityAndConvertToDto(lector);
}

Expand Down Expand Up @@ -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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mapping should be in the facade layer.


if (dto.facultyId() != null) {
entity.setFaculty(facultyRepository.getReferenceById(dto.facultyId()));
}

return saveEntityAndConvertToDto(entity);
}

Expand Down
88 changes: 77 additions & 11 deletions src/test/java/org/unilab/uniplan/lector/LectorServiceTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand All @@ -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() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change naming convention. For example "findById_shouldReturnEntity_whenFacultyExists"

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
Expand All @@ -87,7 +125,7 @@ void testGetLectorByIdShouldReturnLectorDtoIfFound() {
}

@Test
void testGetLectorByIdShouldReturnEmptyOptionalIfLectorNotFound() {
void testGetLectorByIdShouldThrowIfNotFound() {
when(lectorRepository.findById(id)).thenReturn(Optional.empty());

ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class,
Expand All @@ -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)));
}
Expand All @@ -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)));
}
}
}
Loading