From e4c79a450e6fb55a5e2b31f7b4f2f5fab791643c Mon Sep 17 00:00:00 2001 From: Admin Date: Fri, 22 Dec 2023 20:25:33 +0200 Subject: [PATCH] fix: bugs with author and user id in postController --- .../service/impl/PostServiceImpl.java | 25 +++++++++++++------ .../service/impl/PostServiceImplTest.java | 14 +++++++++++ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/softserveinc/dokazovi/service/impl/PostServiceImpl.java b/src/main/java/com/softserveinc/dokazovi/service/impl/PostServiceImpl.java index 48812c8c..bba97eff 100644 --- a/src/main/java/com/softserveinc/dokazovi/service/impl/PostServiceImpl.java +++ b/src/main/java/com/softserveinc/dokazovi/service/impl/PostServiceImpl.java @@ -7,6 +7,7 @@ import com.softserveinc.dokazovi.dto.post.PostPublishedAtDTO; import com.softserveinc.dokazovi.dto.post.PostSaveFromUserDTO; import com.softserveinc.dokazovi.dto.post.PostStatusDTO; +import com.softserveinc.dokazovi.entity.AuthorEntity; import com.softserveinc.dokazovi.entity.DirectionEntity; import com.softserveinc.dokazovi.entity.PostEntity; import com.softserveinc.dokazovi.entity.UserEntity; @@ -213,8 +214,10 @@ public Page findAllByDirection( @Override public Page findPostsByAuthorIdAndDirections( Pageable pageable, Integer authorId, Set directions) { - - return postRepository.findPostsByAuthorIdAndDirections(pageable, authorId, directions) + AuthorEntity author = authorRepository.findById(authorId) + .orElseThrow(() -> new EntityNotFoundException("Author with id " + authorId + " not found")); + Integer userId = author.getProfile().getId(); + return postRepository.findPostsByAuthorIdAndDirections(pageable, userId, directions) .map(postMapper::toPostDTO); } @@ -338,34 +341,40 @@ public Page findLatestByPostTypesAndOriginsForMobile(Pageable p @Override public Page findAllByExpertAndTypeAndDirections(Integer expertId, Set typeId, Set directionId, Pageable pageable) { + AuthorEntity author = authorRepository.findById(expertId) + .orElseThrow(() -> new EntityNotFoundException("Author with id " + expertId + " not found")); + Integer userId = author.getProfile().getId(); if (typeId == null && directionId == null) { - return postRepository.findAllByAuthorIdAndStatusOrderByPublishedAtDesc(expertId, PostStatus.PUBLISHED, + return postRepository.findAllByAuthorIdAndStatusOrderByPublishedAtDesc(userId, PostStatus.PUBLISHED, pageable) .map(postMapper::toPostDTO); } if (typeId == null) { - return postRepository.findPostsByAuthorIdAndDirections(pageable, expertId, directionId) + return postRepository.findPostsByAuthorIdAndDirections(pageable, userId, directionId) .map(postMapper::toPostDTO); } if (directionId == null) { return postRepository - .findAllByAuthorIdAndTypeIdInAndStatus(expertId, typeId, PostStatus.PUBLISHED, pageable) + .findAllByAuthorIdAndTypeIdInAndStatus(userId, typeId, PostStatus.PUBLISHED, pageable) .map(postMapper::toPostDTO); } - return postRepository.findAllByExpertAndByDirectionsAndByPostType(expertId, typeId, directionId, pageable) + return postRepository.findAllByExpertAndByDirectionsAndByPostType(userId, typeId, directionId, pageable) .map(postMapper::toPostDTO); } @Override public Page findAllByExpertAndTypeAndStatus(Integer expertId, Set typeId, PostStatus postStatus, Pageable pageable) { + AuthorEntity author = authorRepository.findById(expertId) + .orElseThrow(() -> new EntityNotFoundException("Author with id " + expertId + " not found")); + Integer userId = author.getProfile().getId(); if (typeId == null) { return postRepository - .findAllByAuthorIdAndStatusOrderByPublishedAtDesc(expertId, postStatus, pageable) + .findAllByAuthorIdAndStatusOrderByPublishedAtDesc(userId, postStatus, pageable) .map(postMapper::toPostDTO); } return postRepository - .findAllByAuthorIdAndTypeIdInAndStatus(expertId, typeId, postStatus, pageable) + .findAllByAuthorIdAndTypeIdInAndStatus(userId, typeId, postStatus, pageable) .map(postMapper::toPostDTO); } diff --git a/src/test/java/com/softserveinc/dokazovi/service/impl/PostServiceImplTest.java b/src/test/java/com/softserveinc/dokazovi/service/impl/PostServiceImplTest.java index 3ac578f7..95c46a57 100644 --- a/src/test/java/com/softserveinc/dokazovi/service/impl/PostServiceImplTest.java +++ b/src/test/java/com/softserveinc/dokazovi/service/impl/PostServiceImplTest.java @@ -60,6 +60,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anySet; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.times; @@ -84,6 +85,7 @@ class PostServiceImplTest { private PostServiceImpl postService; private Page postEntityPage; private UserEntity userEntity; + private AuthorEntity authorEntity; @Mock private GoogleAnalytics googleAnalytics; @Mock @@ -107,6 +109,10 @@ void init() { .password("12345") .role(roleEntity) .build(); + authorEntity = AuthorEntity.builder() + .id(1) + .profile(userEntity) + .build(); } @Test @@ -292,6 +298,7 @@ void findImportantPosts() { void findPostsByAuthorIdAndDirections_WhenWrong_ThrowException() { Set directions = Set.of(1, 4); Pageable pageable = PageRequest.of(0, 12); + when(authorRepository.findById(anyInt())).thenReturn(Optional.of(authorEntity)); when(postRepository.findPostsByAuthorIdAndDirections(any(), any(), any())) .thenThrow(new EntityNotFoundException("Id does not exist")); assertThrows(EntityNotFoundException.class, () -> postService @@ -302,6 +309,7 @@ void findPostsByAuthorIdAndDirections_WhenWrong_ThrowException() { void findPostsByAuthorIdAndDirections() { Set directions = Set.of(1, 4); Pageable pageable = PageRequest.of(0, 12); + when(authorRepository.findById(anyInt())).thenReturn(Optional.of(authorEntity)); when(postRepository.findPostsByAuthorIdAndDirections(any(), any(), any())) .thenReturn(postEntityPage); postService.findPostsByAuthorIdAndDirections(pageable, 1, directions); @@ -355,6 +363,7 @@ void findAllByDirectionAndTypeAndTags() { @Test void findAllByExpert() { Integer expertId = 3; + when(authorRepository.findById(anyInt())).thenReturn(Optional.of(authorEntity)); when(postRepository.findAllByAuthorIdAndStatusOrderByPublishedAtDesc( any(Integer.class), any(PostStatus.class), any(Pageable.class))) .thenReturn(postEntityPage); @@ -366,6 +375,7 @@ void findAllByExpert() { void findAllByExpertAndType() { Integer expertId = 5; Set typeId = Set.of(1, 2); + when(authorRepository.findById(anyInt())).thenReturn(Optional.of(authorEntity)); when(postRepository.findAllByAuthorIdAndTypeIdInAndStatus(any(Integer.class), anySet(), any(PostStatus.class), any(Pageable.class))) .thenReturn(postEntityPage); @@ -377,6 +387,7 @@ void findAllByExpertAndType() { void findAllByExpertAndDirections() { Integer expertId = 5; Set directionId = Set.of(2, 3); + when(authorRepository.findById(anyInt())).thenReturn(Optional.of(authorEntity)); when(postRepository.findPostsByAuthorIdAndDirections(any(Pageable.class), any(Integer.class), anySet())) .thenReturn(postEntityPage); @@ -389,6 +400,7 @@ void findAllByExpertAndTypeAndDirection() { Integer expertId = 5; Set directionId = Set.of(2, 3); Set typeId = Set.of(1, 2); + when(authorRepository.findById(anyInt())).thenReturn(Optional.of(authorEntity)); when(postRepository.findAllByExpertAndByDirectionsAndByPostType(any(Integer.class), anySet(), anySet(), any(Pageable.class))) .thenReturn(postEntityPage); @@ -400,6 +412,7 @@ void findAllByExpertAndTypeAndDirection() { void findAllByExpertAndStatus() { Integer expertId = 3; PostStatus postStatus = PostStatus.DRAFT; + when(authorRepository.findById(anyInt())).thenReturn(Optional.of(authorEntity)); when(postRepository.findAllByAuthorIdAndStatusOrderByPublishedAtDesc( any(Integer.class), any(PostStatus.class), any(Pageable.class))) .thenReturn(postEntityPage); @@ -412,6 +425,7 @@ void findAllByExpertAndTypeAndStatus() { Integer expertId = 5; Set typeId = Set.of(1, 2); PostStatus postStatus = PostStatus.DRAFT; + when(authorRepository.findById(anyInt())).thenReturn(Optional.of(authorEntity)); when(postRepository.findAllByAuthorIdAndTypeIdInAndStatus(any(Integer.class), anySet(), any(PostStatus.class), any(Pageable.class))) .thenReturn(postEntityPage);