Skip to content

Commit

Permalink
correct response in econews/comments/replies/{parentCommentId} (#6487)
Browse files Browse the repository at this point in the history
* 1)changed findAllReplies method in EcoNewsCommentServiceImpl
2)added findAllByParentCommentId method in EcoNewsCommentRepo
3)added new error message
4) added new test in EcoNewsCommentServiceImplTest
  • Loading branch information
LiliaMokhnatska authored and ospodaryk committed Oct 4, 2023
1 parent 0905027 commit efebc06
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
11 changes: 11 additions & 0 deletions dao/src/main/java/greencity/repository/EcoNewsCommentRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;

@Repository
public interface EcoNewsCommentRepo extends JpaRepository<EcoNewsComment, Long> {
Expand All @@ -22,6 +24,15 @@ public interface EcoNewsCommentRepo extends JpaRepository<EcoNewsComment, Long>
Page<EcoNewsComment> findAllByParentCommentIsNullAndEcoNewsIdOrderByCreatedDateDesc(Pageable pageable,
Long ecoNewsId);

/**
* Method returns all replies to comment, specified by parentCommentId.
*
* @param parentCommentId id of comment, replies to which we get.
* @return all replies to comment, specified by parentCommentId.
*/
@Query("SELECT ec from EcoNewsComment ec where ec.parentComment.id = :parentCommentId ")
Optional<List<EcoNewsComment>> findAllByParentCommentId(Long parentCommentId);

/**
* Method returns all replies to comment, specified by parentCommentId and by
* page.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ public final class ErrorMessage {
public static final String PLACE_NOT_FOUND_BY_ID = "The place does not exist by this id: ";
public static final String PLACE_STATUS_NOT_DIFFERENT = "Place with id: %d already has this status: %s";
public static final String COMMENT_NOT_FOUND_EXCEPTION = "The comment with entered id doesn't exist";
public static final String COMMENT_NOT_FOUND_BY_PARENT_COMMENT_ID =
"The comment with entered parent_comment_id doesn't exist";
public static final String COMMENT_PROPERTY_TYPE_NOT_FOUND = "For type comment not found this property :";
public static final String CANNOT_REPLY_THE_REPLY = "Can not make a reply to a reply";
public static final String NOT_A_CURRENT_USER = "You can't perform actions with the data of other user";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -132,6 +133,10 @@ public PageableDto<EcoNewsCommentDto> findAllComments(Pageable pageable, UserVO
*/
@Override
public PageableDto<EcoNewsCommentDto> findAllReplies(Pageable pageable, Long parentCommentId, UserVO userVO) {
Optional<List<EcoNewsComment>> checkExistComment = ecoNewsCommentRepo.findAllByParentCommentId(parentCommentId);
if (checkExistComment.isEmpty()) {
throw new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_BY_PARENT_COMMENT_ID);
}
Page<EcoNewsComment> pages = ecoNewsCommentRepo
.findAllByParentCommentIdOrderByCreatedDateDesc(pageable, parentCommentId);
List<EcoNewsCommentDto> ecoNewsCommentDtos = pages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,15 @@ void findAllReplies() {
int pageSize = 3;
Pageable pageable = PageRequest.of(pageNumber, pageSize);
UserVO userVO = getUserVO();
User user = getUser();
Long parentCommentId = 1L;
EcoNewsComment ecoNewsCommentChild = ModelUtils.getEcoNewsComment();
ecoNewsCommentChild.setParentComment(ModelUtils.getEcoNewsComment());
ecoNewsCommentChild.setUsersLiked(new HashSet<>());
Page<EcoNewsComment> pages = new PageImpl<>(Collections.singletonList(ecoNewsCommentChild), pageable, 1);

when(ecoNewsCommentRepo.findAllByParentCommentId(parentCommentId))
.thenReturn(Optional.of(List.of(ecoNewsCommentChild)));

when(ecoNewsCommentRepo.findAllByParentCommentIdOrderByCreatedDateDesc(pageable, parentCommentId))
.thenReturn(pages);
when(modelMapper.map(ecoNewsCommentChild, EcoNewsCommentDto.class))
Expand All @@ -220,6 +222,28 @@ void findAllReplies() {
assertEquals(4, allReplies.getTotalElements());
assertEquals(1, allReplies.getCurrentPage());
assertEquals(1, allReplies.getPage().size());
verify(ecoNewsCommentRepo).findAllByParentCommentId(parentCommentId);
verify(ecoNewsCommentRepo).findAllByParentCommentIdOrderByCreatedDateDesc(pageable, parentCommentId);
verify(modelMapper).map(ecoNewsCommentChild, EcoNewsCommentDto.class);
}

@Test
void findAllRepliesThrewException() {
int pageNumber = 1;
int pageSize = 3;
Pageable pageable = PageRequest.of(pageNumber, pageSize);
UserVO userVO = getUserVO();
Long parentCommentId = 1L;
EcoNewsComment ecoNewsCommentChild = ModelUtils.getEcoNewsComment();
ecoNewsCommentChild.setParentComment(ModelUtils.getEcoNewsComment());
ecoNewsCommentChild.setUsersLiked(new HashSet<>());

when(ecoNewsCommentRepo.findAllByParentCommentId(parentCommentId)).thenReturn(Optional.empty());

assertThrows(NotFoundException.class,
() -> ecoNewsCommentService.findAllReplies(pageable, parentCommentId, userVO));

verify(ecoNewsCommentRepo).findAllByParentCommentId(parentCommentId);
}

@Test
Expand Down

0 comments on commit efebc06

Please sign in to comment.