-
Notifications
You must be signed in to change notification settings - Fork 3
bug/116-fixed edit faculty not working on lector #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DrDeathDrop
wants to merge
1
commit into
main
Choose a base branch
from
bug/116-edit-faculty-doesnt-work-on-lector
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
@@ -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() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
@@ -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))); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.