Skip to content
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

6586 postnews api an authorized user can likes its own comments and replies #7862

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,12 @@ public void like(Long commentId, UserVO userVO, Locale locale) {

removeDislikeIfExists(comment, userVO);

comment.getUsersLiked().add(modelMapper.map(userVO, User.class));
User mappedUser = modelMapper.map(userVO, User.class);
if (mappedUser.equals(comment.getUser())) {
return;
}

comment.getUsersLiked().add(mappedUser);
achievementCalculation.calculateAchievement(userVO,
AchievementCategoryType.LIKE_COMMENT_OR_REPLY, AchievementAction.ASSIGN);
ratingCalculation.ratingCalculation(ratingPointsRepo.findByNameOrThrow("LIKE_COMMENT_OR_REPLY"), userVO);
Expand Down
30 changes: 30 additions & 0 deletions service/src/test/java/greencity/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,23 @@ public static User getUser() {
.build();
}

public static User getUserNotCommentOwner() {
return User.builder()
.id(2L)
.email(TestConst.EMAIL)
.name(TestConst.NAME)
.role(Role.ROLE_USER)
.userStatus(UserStatus.ACTIVATED)
.lastActivityTime(localDateTime)
.verifyEmail(new VerifyEmail())
.dateOfRegistration(localDateTime)
.subscribedEvents(new HashSet<>())
.favoriteEcoNews(new HashSet<>())
.favoriteEvents(new HashSet<>())
.language(getLanguage())
.build();
}

public static User getAttenderUser() {
return User.builder()
.id(2L)
Expand Down Expand Up @@ -519,6 +536,19 @@ public static UserVO getUserVO() {
.build();
}

public static UserVO getUserVONotCommentOwner() {
return UserVO.builder()
.id(2L)
.email(TestConst.EMAIL)
.name(TestConst.NAME)
.role(Role.ROLE_USER)
.lastActivityTime(localDateTime)
.verifyEmail(new VerifyEmailVO())
.dateOfRegistration(localDateTime)
.languageVO(getLanguageVO())
.build();
}

public static UserVO getAuthorVO() {
return UserVO.builder()
.id(2L)
Expand Down
61 changes: 40 additions & 21 deletions service/src/test/java/greencity/service/CommentServiceImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,8 @@
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import static greencity.ModelUtils.getAddCommentDtoResponse;
import static greencity.ModelUtils.getAmountCommentLikesDto;
import static greencity.ModelUtils.getComment;
import static greencity.ModelUtils.getCommentDto;
import static greencity.ModelUtils.getCommentVO;
import static greencity.ModelUtils.getEcoNews;
import static greencity.ModelUtils.getEvent;
import static greencity.ModelUtils.getHabit;
import static greencity.ModelUtils.getHabitTranslation;
import static greencity.ModelUtils.getMultipartImageFiles;
import static greencity.ModelUtils.getUser;
import static greencity.ModelUtils.getUserSearchDto;
import static greencity.ModelUtils.getUserTagDto;
import static greencity.ModelUtils.getUserVO;

import static greencity.ModelUtils.*;
Copy link
Collaborator

Choose a reason for hiding this comment

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

wildcard import

import static greencity.constant.ErrorMessage.ECO_NEW_NOT_FOUND_BY_ID;
import static greencity.constant.ErrorMessage.HABIT_NOT_FOUND_BY_ID;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -1040,8 +1028,8 @@ void countAllActiveRepliesNotFoundParentCommentTest() {
@Test
void likeTest() {
Long commentId = 1L;
UserVO userVO = getUserVO();
User user = getUser();
UserVO userVO = getUserVONotCommentOwner();
User user = getUserNotCommentOwner();
Comment comment = getComment();
RatingPoints ratingPoints = RatingPoints.builder().id(1L).name("LIKE_COMMENT_OR_REPLY").points(1).build();
Long articleId = 10L;
Expand Down Expand Up @@ -1069,11 +1057,42 @@ void likeTest() {
}

@Test
void likeEcoNewsCommentTest() {
void likeTest_OwnerUserLikesTheirComment_ShouldNotLike() {
Long commentId = 1L;
UserVO userVO = getUserVO();
User user = getUser();
Comment comment = getComment();
RatingPoints ratingPoints = RatingPoints.builder().id(1L).name("LIKE_COMMENT_OR_REPLY").points(1).build();
Long articleId = 10L;
Habit habit = getHabit();
habit.setUserId(user.getId());
HabitTranslation habitTranslation = getHabitTranslation();

when(userRepo.findById(user.getId())).thenReturn(Optional.of(user));
when(habitRepo.findById(articleId)).thenReturn(Optional.of(habit));
when(habitTranslationRepo.findByHabitAndLanguageCode(habit, Locale.of("en").getLanguage()))
.thenReturn(Optional.ofNullable(habitTranslation));
when(ratingPointsRepo.findByNameOrThrow("LIKE_COMMENT_OR_REPLY")).thenReturn(ratingPoints);
when(commentRepo.findByIdAndStatusNot(commentId, CommentStatus.DELETED)).thenReturn(Optional.of(comment));
when(modelMapper.map(userVO, User.class)).thenReturn(user);
doNothing().when(userNotificationService).createNotification(
any(UserVO.class), any(UserVO.class), any(NotificationType.class),
anyLong(), anyString(), anyLong(), anyString());

commentService.like(commentId, userVO, Locale.ENGLISH);

assertFalse(comment.getUsersLiked().contains(user));

verify(commentRepo).findByIdAndStatusNot(commentId, CommentStatus.DELETED);
verify(modelMapper).map(userVO, User.class);
}

@Test
void likeEcoNewsCommentTest() {
Long commentId = 1L;
UserVO userVO = getUserVONotCommentOwner();
User user = getUserNotCommentOwner();
Comment comment = getComment();
comment.setArticleType(ArticleType.ECO_NEWS);
RatingPoints ratingPoints = RatingPoints.builder().id(1L).name("LIKE_COMMENT_OR_REPLY").points(1).build();
Long articleId = 10L;
Expand All @@ -1099,8 +1118,8 @@ void likeEcoNewsCommentTest() {
@Test
void likeEventCommentTest() {
Long commentId = 1L;
UserVO userVO = getUserVO();
User user = getUser();
UserVO userVO = getUserVONotCommentOwner();
User user = getUserNotCommentOwner();
Comment comment = getComment();
comment.setArticleType(ArticleType.EVENT);
RatingPoints ratingPoints = RatingPoints.builder().id(1L).name("LIKE_COMMENT_OR_REPLY").points(1).build();
Expand Down Expand Up @@ -1162,8 +1181,8 @@ void likeNotFoundCommentTest() {
@Test
void givenCommentDislikedByUser_whenLikedByUser_shouldRemoveDislikeAndAddLike() {
Long commentId = 1L;
UserVO userVO = getUserVO();
User user = getUser();
UserVO userVO = getUserVONotCommentOwner();
User user = getUserNotCommentOwner();
Comment comment = getComment();
RatingPoints ratingPoints = RatingPoints.builder().id(1L).name("LIKE_COMMENT_OR_REPLY").points(1).build();
Long articleId = 10L;
Expand Down
Loading