From 281188dd0045a3aacd7deb082e712469df14324b Mon Sep 17 00:00:00 2001 From: vnglnk <128087718+holotsvan@users.noreply.github.com> Date: Mon, 9 Dec 2024 13:39:14 +0200 Subject: [PATCH] Dislikes (#7896) * eco news dislikes * self like dislike test * self like dislike check on events * event like dislike tests fix * issues --- .../greencity/service/CommentServiceImpl.java | 16 +++- .../greencity/service/EcoNewsServiceImpl.java | 92 +++++++++++++------ .../greencity/service/EventServiceImpl.java | 17 ++-- .../greencity/service/HabitServiceImpl.java | 22 +++-- .../service/CommentServiceImplTest.java | 52 +++++++++-- .../service/EcoNewsServiceImplTest.java | 43 ++++++--- .../service/EventServiceImplTest.java | 61 ++++++++---- .../service/HabitServiceImplTest.java | 64 ++++++++----- 8 files changed, 265 insertions(+), 102 deletions(-) diff --git a/service/src/main/java/greencity/service/CommentServiceImpl.java b/service/src/main/java/greencity/service/CommentServiceImpl.java index 135b46e34..a5ee74c71 100644 --- a/service/src/main/java/greencity/service/CommentServiceImpl.java +++ b/service/src/main/java/greencity/service/CommentServiceImpl.java @@ -528,6 +528,12 @@ public void like(Long commentId, UserVO userVO, Locale locale) { Comment comment = commentRepo.findByIdAndStatusNot(commentId, CommentStatus.DELETED) .orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_BY_ID + commentId)); + boolean isAuthor = comment.getUser().getId().equals(userVO.getId()); + + if (isAuthor) { + throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION); + } + if (removeLikeIfExists(comment, userVO)) { return; } @@ -556,12 +562,18 @@ public void dislike(Long commentId, UserVO userVO, Locale locale) { Comment comment = commentRepo.findByIdAndStatusNot(commentId, CommentStatus.DELETED) .orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_BY_ID + commentId)); - if (removeDislikeIfExists(comment, userVO)) { - return; + boolean isAuthor = comment.getUser().getId().equals(userVO.getId()); + + if (isAuthor) { + throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION); } removeLikeIfExists(comment, userVO); + if (removeDislikeIfExists(comment, userVO)) { + return; + } + comment.getUsersDisliked().add(modelMapper.map(userVO, User.class)); commentRepo.save(comment); diff --git a/service/src/main/java/greencity/service/EcoNewsServiceImpl.java b/service/src/main/java/greencity/service/EcoNewsServiceImpl.java index eac1554e3..facfdcf2e 100644 --- a/service/src/main/java/greencity/service/EcoNewsServiceImpl.java +++ b/service/src/main/java/greencity/service/EcoNewsServiceImpl.java @@ -428,29 +428,24 @@ private Page getCommonEcoNews(Page sortedPage, Page p @Override public void like(UserVO userVO, Long id) { EcoNews ecoNews = findEcoNewsById(id); + boolean isAuthor = ecoNews.getAuthor().getId().equals(userVO.getId()); + if (isAuthor) { + throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION); + } - ecoNews.getUsersDislikedNews().removeIf(u -> u.getId().equals(userVO.getId())); + if (removeLikeIfExists(ecoNews, userVO, ecoNews.getAuthor())) { + return; + } - boolean isLiked = ecoNews.getUsersLikedNews().stream() - .anyMatch(u -> u.getId().equals(userVO.getId())); + removeDislikeIfExists(ecoNews, userVO); - boolean isAuthor = ecoNews.getAuthor().getId().equals(userVO.getId()); + ecoNews.getUsersLikedNews().add(modelMapper.map(userVO, User.class)); + achievementCalculation.calculateAchievement(userVO, + AchievementCategoryType.LIKE_NEWS, AchievementAction.ASSIGN); + ratingCalculation.ratingCalculation(ratingPointsRepo.findByNameOrThrow("LIKE_NEWS"), userVO); - if (isLiked) { - achievementCalculation.calculateAchievement(userVO, - AchievementCategoryType.LIKE_NEWS, AchievementAction.DELETE); - ratingCalculation.ratingCalculation(ratingPointsRepo.findByNameOrThrow("UNDO_LIKE_NEWS"), - userVO); - ecoNews.getUsersLikedNews().removeIf(u -> u.getId().equals(userVO.getId())); - } else { - if (isAuthor) { - throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION); - } - achievementCalculation.calculateAchievement(userVO, - AchievementCategoryType.LIKE_NEWS, AchievementAction.ASSIGN); - ratingCalculation.ratingCalculation(ratingPointsRepo.findByNameOrThrow("LIKE_NEWS"), userVO); - ecoNews.getUsersLikedNews().add(modelMapper.map(userVO, User.class)); - } + boolean isLiked = ecoNews.getUsersLikedNews().stream() + .anyMatch(u -> u.getId().equals(userVO.getId())); sendNotification(ecoNews, userVO, !isLiked); ecoNewsRepo.save(modelMapper.map(ecoNews, EcoNews.class)); @@ -483,16 +478,21 @@ private String formatNewsTitle(String newsTitle) { @Override public void dislike(UserVO userVO, Long id) { EcoNews ecoNews = findEcoNewsById(id); - if (ecoNews.getUsersLikedNews().stream().anyMatch(user -> user.getId().equals(userVO.getId()))) { - ecoNews.getUsersLikedNews().removeIf(u -> u.getId().equals(userVO.getId())); - userNotificationService.removeActionUserFromNotification(modelMapper.map(ecoNews.getAuthor(), UserVO.class), - userVO, id, NotificationType.ECONEWS_LIKE); + boolean isAuthor = ecoNews.getAuthor().getId().equals(userVO.getId()); + + if (isAuthor) { + throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION); } - if (ecoNews.getUsersDislikedNews().stream().anyMatch(user -> user.getId().equals(userVO.getId()))) { - ecoNews.getUsersDislikedNews().removeIf(u -> u.getId().equals(userVO.getId())); - } else { - ecoNews.getUsersDislikedNews().add(modelMapper.map(userVO, User.class)); + + removeLikeIfExists(ecoNews, userVO, ecoNews.getAuthor()); + + if (removeDislikeIfExists(ecoNews, userVO)) { + ecoNewsRepo.save(ecoNews); + return; } + + ecoNews.getUsersDislikedNews().add(modelMapper.map(userVO, User.class)); + ecoNewsRepo.save(ecoNews); } @@ -778,4 +778,42 @@ public void setHiddenValue(Long id, UserVO user, boolean value) { public List getThreeInterestingEcoNews() { return mapEcoNewsListToEcoNewsDtoList(ecoNewsRepo.findThreeInterestingEcoNews()); } + + /** + * Removes a like from the eco news if the user has already liked it. Returns + * true if a like was removed, false otherwise. + */ + private boolean removeLikeIfExists(EcoNews ecoNews, UserVO userVO, User econewsAuthor) { + boolean userLiked = ecoNews.getUsersLikedNews().stream() + .anyMatch(user -> user.getId().equals(userVO.getId())); + + if (userLiked) { + ecoNews.getUsersLikedNews().removeIf(user -> user.getId().equals(userVO.getId())); + achievementCalculation.calculateAchievement(userVO, AchievementCategoryType.LIKE_NEWS, + AchievementAction.DELETE); + ratingCalculation.ratingCalculation(ratingPointsRepo.findByNameOrThrow("UNDO_LIKE_NEWS"), userVO); + + if (econewsAuthor != null) { + userNotificationService.removeActionUserFromNotification( + modelMapper.map(econewsAuthor, UserVO.class), userVO, ecoNews.getId(), NotificationType.EVENT_LIKE); + } + return true; + } + return false; + } + + /** + * Removes a dislike from the eco news if the user has already disliked it. + * Returns true if a dislike was removed, false otherwise. + */ + private boolean removeDislikeIfExists(EcoNews ecoNews, UserVO userVO) { + boolean userDisliked = ecoNews.getUsersDislikedNews().stream() + .anyMatch(user -> user.getId().equals(userVO.getId())); + + if (userDisliked) { + ecoNews.getUsersDislikedNews().removeIf(user -> user.getId().equals(userVO.getId())); + return true; + } + return false; + } } \ No newline at end of file diff --git a/service/src/main/java/greencity/service/EventServiceImpl.java b/service/src/main/java/greencity/service/EventServiceImpl.java index 78a8dc95d..77980f715 100644 --- a/service/src/main/java/greencity/service/EventServiceImpl.java +++ b/service/src/main/java/greencity/service/EventServiceImpl.java @@ -775,6 +775,11 @@ public Long getCountOfOrganizedEventsByUserId(Long userId) { public void like(Long eventId, UserVO userVO) { Event event = findEventId(eventId); User eventAuthor = getEventAuthor(event); + boolean isAuthor = Objects.nonNull(event.getOrganizer()) && event.getOrganizer().getId().equals(userVO.getId()); + + if (isAuthor) { + throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION); + } if (removeLikeIfExists(event, userVO, eventAuthor)) { return; @@ -787,9 +792,7 @@ public void like(Long eventId, UserVO userVO) { AchievementAction.ASSIGN); ratingCalculation.ratingCalculation(ratingPointsRepo.findByNameOrThrow("LIKE_EVENT"), userVO); - if (eventAuthor != null) { - sendEventLikeNotification(eventAuthor, userVO, eventId, event); - } + sendEventLikeNotification(eventAuthor, userVO, eventId, event); eventRepo.save(event); } @@ -797,6 +800,11 @@ public void like(Long eventId, UserVO userVO) { @Override public void dislike(UserVO userVO, Long eventId) { Event event = findEventId(eventId); + boolean isAuthor = Objects.nonNull(event.getOrganizer()) && event.getOrganizer().getId().equals(userVO.getId()); + + if (isAuthor) { + throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION); + } removeLikeIfExists(event, userVO, getEventAuthor(event)); @@ -806,9 +814,6 @@ public void dislike(UserVO userVO, Long eventId) { } event.getUsersDislikedEvents().add(modelMapper.map(userVO, User.class)); - achievementCalculation.calculateAchievement(userVO, AchievementCategoryType.LIKE_EVENT, - AchievementAction.DELETE); - ratingCalculation.ratingCalculation(ratingPointsRepo.findByNameOrThrow("UNDO_LIKE_EVENT"), userVO); eventRepo.save(event); } diff --git a/service/src/main/java/greencity/service/HabitServiceImpl.java b/service/src/main/java/greencity/service/HabitServiceImpl.java index 30ac907c5..f31d82fa6 100644 --- a/service/src/main/java/greencity/service/HabitServiceImpl.java +++ b/service/src/main/java/greencity/service/HabitServiceImpl.java @@ -550,9 +550,14 @@ public void deleteCustomHabit(Long customHabitId, String ownerEmail) { @Override public void like(Long habitId, UserVO userVO) { Habit habit = findHabitById(habitId); - User habitAuthor = getHabitAuthor(habit); + User author = getHabitAuthor(habit); + boolean isAuthor = habit.getUserId().equals(userVO.getId()); - if (removeLikeIfExists(habit, userVO, habitAuthor)) { + if (isAuthor) { + throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION); + } + + if (removeLikeIfExists(habit, userVO, author)) { return; } @@ -563,9 +568,7 @@ public void like(Long habitId, UserVO userVO) { achievementCalculation.calculateAchievement(userVO, AchievementCategoryType.LIKE_HABIT, AchievementAction.ASSIGN); - if (habitAuthor != null) { - sendHabitLikeNotification(habitAuthor, userVO, habitId, habit); - } + sendHabitLikeNotification(author, userVO, habitId, habit); habitRepo.save(habit); } @@ -573,6 +576,11 @@ public void like(Long habitId, UserVO userVO) { @Override public void dislike(Long habitId, UserVO userVO) { Habit habit = findHabitById(habitId); + boolean isAuthor = habit.getUserId().equals(userVO.getId()); + + if (isAuthor) { + throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION); + } removeLikeIfExists(habit, userVO, getHabitAuthor(habit)); @@ -581,10 +589,6 @@ public void dislike(Long habitId, UserVO userVO) { } habit.getUsersDisliked().add(modelMapper.map(userVO, User.class)); - achievementCalculation.calculateAchievement(userVO, AchievementCategoryType.LIKE_HABIT, - AchievementAction.DELETE); - - ratingCalculation.ratingCalculation(ratingPointsRepo.findByNameOrThrow("UNDO_LIKE_HABIT"), userVO); habitRepo.save(habit); } diff --git a/service/src/test/java/greencity/service/CommentServiceImplTest.java b/service/src/test/java/greencity/service/CommentServiceImplTest.java index c7afa5f51..e35acd1f8 100644 --- a/service/src/test/java/greencity/service/CommentServiceImplTest.java +++ b/service/src/test/java/greencity/service/CommentServiceImplTest.java @@ -57,8 +57,22 @@ import java.util.Locale; import java.util.Optional; import java.util.Set; - -import static greencity.ModelUtils.*; +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.getUserNotCommentOwner; +import static greencity.ModelUtils.getUserSearchDto; +import static greencity.ModelUtils.getUserTagDto; +import static greencity.ModelUtils.getUserVO; +import static greencity.ModelUtils.getUserVONotCommentOwner; 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; @@ -1056,6 +1070,18 @@ void likeTest() { verify(modelMapper).map(userVO, User.class); } + @Test + void testLikeOwn() { + UserVO userVO = getUserVO(); + Comment comment = getComment(); + CommentVO commentVO = getCommentVO(); + commentVO.setUsersLiked(new HashSet<>()); + + when(commentRepo.findByIdAndStatusNot(1L, CommentStatus.DELETED)).thenReturn(Optional.of(comment)); + + assertThrows(BadRequestException.class, () -> commentService.like(1L, userVO, Locale.ENGLISH)); + } + @Test void likeTest_OwnerUserLikesTheirComment_ShouldNotLike() { Long commentId = 1L; @@ -1079,12 +1105,7 @@ void likeTest_OwnerUserLikesTheirComment_ShouldNotLike() { 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); + assertThrows(BadRequestException.class, () -> commentService.like(1L, userVO, Locale.ENGLISH)); } @Test @@ -1151,6 +1172,7 @@ void unlikeTest() { Comment comment = getComment(); comment.setCurrentUserLiked(true); comment.getUsersLiked().add(user); + comment.getUser().setId(2L); RatingPoints ratingPoints = RatingPoints.builder().id(1L).name("UNDO_LIKE_COMMENT_OR_REPLY").points(-1).build(); when(ratingPointsRepo.findByNameOrThrow("UNDO_LIKE_COMMENT_OR_REPLY")).thenReturn(ratingPoints); @@ -1213,6 +1235,7 @@ void dislikeTest() { UserVO userVO = getUserVO(); User user = getUser(); Comment comment = getComment(); + comment.getUser().setId(2L); comment.setUsersDisliked(new HashSet<>()); when(commentRepo.findByIdAndStatusNot(1L, CommentStatus.DELETED)).thenReturn(Optional.of(comment)); @@ -1224,11 +1247,24 @@ void dislikeTest() { assertEquals(1L, comment.getUsersDisliked().size()); } + @Test + void testDislikeOwn() { + UserVO userVO = getUserVO(); + Comment comment = getComment(); + CommentVO commentVO = getCommentVO(); + commentVO.setUsersLiked(new HashSet<>()); + + when(commentRepo.findByIdAndStatusNot(1L, CommentStatus.DELETED)).thenReturn(Optional.of(comment)); + + assertThrows(BadRequestException.class, () -> commentService.dislike(1L, userVO, Locale.ENGLISH)); + } + @Test void givenEventLikedByUser_whenDislikedByUser_shouldRemoveLikeAndAddDislike() { UserVO userVO = getUserVO(); User user = getUser(); Comment comment = getComment(); + comment.getUser().setId(2L); comment.setUsersLiked(new HashSet<>(Set.of(user))); comment.setUsersDisliked(new HashSet<>()); diff --git a/service/src/test/java/greencity/service/EcoNewsServiceImplTest.java b/service/src/test/java/greencity/service/EcoNewsServiceImplTest.java index 7e05808e8..c76f53fa5 100644 --- a/service/src/test/java/greencity/service/EcoNewsServiceImplTest.java +++ b/service/src/test/java/greencity/service/EcoNewsServiceImplTest.java @@ -494,12 +494,14 @@ void buildSearchCriteriaWithStartDateTest() { @Test void likeTest() { UserVO userVO = ModelUtils.getUserVO(); + User user = ModelUtils.getUser(); EcoNewsVO ecoNewsVO = ModelUtils.getEcoNewsVO(); ecoNewsVO.getAuthor().setId(2L); ecoNewsVO.setUsersLikedNews(new HashSet<>()); ecoNews.getAuthor().setId(2L); - + ecoNews.setUsersLikedNews(new HashSet<>()); when(ecoNewsRepo.findById(1L)).thenReturn(Optional.of(ecoNews)); + when(modelMapper.map(userVO, User.class)).thenReturn(user); when(modelMapper.map(ecoNews, EcoNewsVO.class)).thenReturn(ecoNewsVO); when(modelMapper.map(ecoNewsVO, EcoNews.class)).thenReturn(ecoNews); @@ -524,12 +526,14 @@ void likeOwnTest() { @Test void givenEcoNewsLikedByUser_whenLikedByUser_shouldRemoveLike() { UserVO userVO = ModelUtils.getUserVO(); + User user = ModelUtils.getUser(); EcoNewsVO ecoNewsVO = ModelUtils.getEcoNewsVO(); ecoNewsVO.getAuthor().setId(2L); ecoNewsVO.setUsersLikedNews(new HashSet<>()); ecoNews.getAuthor().setId(2L); when(ecoNewsRepo.findById(1L)).thenReturn(Optional.of(ecoNews)); + when(modelMapper.map(userVO, User.class)).thenReturn(user); when(modelMapper.map(ecoNews, EcoNewsVO.class)).thenReturn(ecoNewsVO); when(modelMapper.map(ecoNewsVO, EcoNews.class)).thenReturn(ecoNews); @@ -543,6 +547,8 @@ void dislikeTest() { UserVO userVO = ModelUtils.getUserVO(); EcoNewsVO ecoNewsVO = ModelUtils.getEcoNewsVO(); ecoNewsVO.setUsersDislikedNews(new HashSet<>()); + ecoNewsVO.getAuthor().setId(2L); + ecoNews.getAuthor().setId(2L); when(ecoNewsRepo.findById(anyLong())).thenReturn(Optional.of(ecoNews)); when(modelMapper.map(ecoNews, EcoNewsVO.class)).thenReturn(ecoNewsVO); when(modelMapper.map(ecoNewsVO, EcoNews.class)).thenReturn(ecoNews); @@ -553,12 +559,27 @@ void dislikeTest() { verify(ecoNewsRepo).save(ecoNews); } + @Test + void dislikeOwnTest() { + UserVO userVO = ModelUtils.getUserVO(); + EcoNewsVO ecoNewsVO = ModelUtils.getEcoNewsVO(); + ecoNewsVO.setUsersDislikedNews(new HashSet<>()); + + when(ecoNewsRepo.findById(1L)).thenReturn(Optional.of(ecoNews)); + when(modelMapper.map(ecoNews, EcoNewsVO.class)).thenReturn(ecoNewsVO); + when(modelMapper.map(ecoNewsVO, EcoNews.class)).thenReturn(ecoNews); + + assertThrows(BadRequestException.class, () -> ecoNewsService.dislike(userVO, 1L)); + } + @Test void givenEcoNewsLikedByUser_whenDislikedByUser_shouldRemoveLikeAndAddDislike() { UserVO userVO = ModelUtils.getUserVO(); EcoNewsVO ecoNewsVO = ModelUtils.getEcoNewsVO(); ecoNewsVO.setUsersLikedNews(new HashSet<>(Set.of(userVO))); ecoNewsVO.setUsersDislikedNews(new HashSet<>()); + ecoNews.getAuthor().setId(2L); + ecoNewsVO.getAuthor().setId(2L); when(ecoNewsRepo.findById(anyLong())).thenReturn(Optional.of(ecoNews)); when(modelMapper.map(ecoNews, EcoNewsVO.class)).thenReturn(ecoNewsVO); when(modelMapper.map(ecoNewsVO, EcoNews.class)).thenReturn(ecoNews); @@ -744,31 +765,33 @@ void findUsersWhoDislikedPost() { @Test void testLikeAddLike() { - UserVO actionUser = ModelUtils.getUserVO(); - UserVO targetUser = ModelUtils.getAuthorVO(); + UserVO actionUserVO = ModelUtils.getUserVO(); + User actionUser = ModelUtils.getUser(); + UserVO targetUserVO = ModelUtils.getAuthorVO(); EcoNewsVO ecoNewsVO = ModelUtils.getEcoNewsVO(); - ecoNewsVO.setAuthor(targetUser); + ecoNewsVO.setAuthor(targetUserVO); ecoNewsVO.setUsersLikedNews(new HashSet<>()); EcoNews ecoNewsWithAuthor = getEcoNews(); ecoNewsWithAuthor.setAuthor(User.builder() - .id(targetUser.getId()) + .id(targetUserVO.getId()) .build()); RatingPoints ratingPoints = RatingPoints.builder().id(1L).name("LIKE_NEWS").points(1).build(); when(ratingPointsRepo.findByNameOrThrow("LIKE_NEWS")).thenReturn(ratingPoints); + when(modelMapper.map(actionUserVO, User.class)).thenReturn(actionUser); when(ecoNewsRepo.save(any(EcoNews.class))).thenReturn(ecoNewsWithAuthor); when(ecoNewsRepo.findById(anyLong())).thenReturn(Optional.of(ecoNewsWithAuthor)); when(modelMapper.map(any(EcoNews.class), eq(EcoNewsVO.class))).thenReturn(ecoNewsVO); - when(userService.findById(anyLong())).thenReturn(targetUser); + when(userService.findById(anyLong())).thenReturn(targetUserVO); - ecoNewsService.like(actionUser, ecoNewsVO.getId()); + ecoNewsService.like(actionUserVO, ecoNewsVO.getId()); verify(userNotificationService, times(1)).createOrUpdateLikeNotification( any(LikeNotificationDto.class)); - verify(achievementCalculation, times(1)).calculateAchievement(actionUser, + verify(achievementCalculation, times(1)).calculateAchievement(actionUserVO, AchievementCategoryType.LIKE_NEWS, AchievementAction.ASSIGN); verify(ratingCalculation, times(1)) - .ratingCalculation(ratingPoints, actionUser); + .ratingCalculation(ratingPoints, actionUserVO); } @Test @@ -808,8 +831,6 @@ void testLikeUndoLike() { assertTrue(ecoNewsVO.getUsersLikedNews().contains(actionUser)); - verify(userNotificationService, times(1)).createOrUpdateLikeNotification( - any(LikeNotificationDto.class)); verify(achievementCalculation, times(1)) .calculateAchievement(actionUser, AchievementCategoryType.LIKE_NEWS, AchievementAction.DELETE); verify(ratingCalculation, times(1)) diff --git a/service/src/test/java/greencity/service/EventServiceImplTest.java b/service/src/test/java/greencity/service/EventServiceImplTest.java index cee9cd13d..89a20f22a 100644 --- a/service/src/test/java/greencity/service/EventServiceImplTest.java +++ b/service/src/test/java/greencity/service/EventServiceImplTest.java @@ -28,7 +28,6 @@ import greencity.entity.event.EventDateLocation; import greencity.entity.event.EventGrade; import greencity.entity.event.EventImages; -import greencity.entity.event.EventGrade; import greencity.enums.NotificationType; import greencity.enums.Role; import greencity.enums.TagType; @@ -1225,8 +1224,8 @@ void likeTest() { UserVO userVO = getUserVO(); UserVO eventAuthorVO = getAuthorVO(); User user = getUser(); - User eventAuthor = getUser(); - Event event = getEvent(); + User eventAuthor = getUser().setId(2L); + Event event = getEvent().setOrganizer(eventAuthor); RatingPoints ratingPoints = RatingPoints.builder().id(1L).name("LIKE_EVENT").points(1).build(); when(eventRepo.findById(event.getId())).thenReturn(Optional.of(event)); @@ -1247,16 +1246,29 @@ void likeTest() { } @Test - void removeLikeTest() { + void testLikeOwn() { UserVO userVO = getUserVO(); User user = getUser(); Event event = getEvent(); + + when(userRepo.findById(1L)).thenReturn(Optional.of(user)); + when(eventRepo.findById(1L)).thenReturn(Optional.of(event)); + + assertThrows(BadRequestException.class, () -> eventService.like(1L, userVO)); + } + + @Test + void removeLikeTest() { + UserVO userVO = getUserVO(); + User eventAuthor = getUser().setId(2L); + User user = getUser(); + Event event = getEvent().setOrganizer(eventAuthor); event.getUsersLikedEvents().add(user); RatingPoints ratingPoints = RatingPoints.builder().id(1L).name("UNDO_LIKE_EVENT").points(-1).build(); when(eventRepo.findById(event.getId())).thenReturn(Optional.of(event)); - when(userRepo.findById(user.getId())).thenReturn(Optional.of(user)); - when(modelMapper.map(userVO, User.class)).thenReturn(event.getOrganizer()); + when(userRepo.findById(2L)).thenReturn(Optional.of(eventAuthor)); + when(modelMapper.map(userVO, User.class)).thenReturn(user); when(ratingPointsRepo.findByNameOrThrow("UNDO_LIKE_EVENT")).thenReturn(ratingPoints); eventService.like(event.getId(), userVO); @@ -1265,7 +1277,7 @@ void removeLikeTest() { verify(userNotificationService, times(1)).removeActionUserFromNotification( modelMapper.map(user, UserVO.class), userVO, event.getId(), NotificationType.EVENT_LIKE); verify(eventRepo).findById(event.getId()); - verify(userRepo).findById(user.getId()); + verify(userRepo).findById(eventAuthor.getId()); } @Test @@ -1342,15 +1354,16 @@ void removeLikeWithNullEventOrganizerTest() { } @Test - void givenEventDislikedByUser_whenLikedByUser_shouldRemoveDislikeAndAddLike() { + void givenEventDislikedByUser_whenLikedByUser_shouldRemoveDislikeAddLike() { UserVO userVO = getUserVO(); User user = getUser(); - Event event = getEvent(); + User eventAuthor = getUser().setId(2L); + Event event = getEvent().setOrganizer(eventAuthor); event.setUsersLikedEvents(new HashSet<>()); event.setUsersDislikedEvents(new HashSet<>(Set.of(user))); when(eventRepo.findById(anyLong())).thenReturn(Optional.of(event)); - when(userRepo.findById(user.getId())).thenReturn(Optional.of(user)); + when(userRepo.findById(event.getOrganizer().getId())).thenReturn(Optional.of(eventAuthor)); eventService.like(1L, userVO); @@ -1361,12 +1374,12 @@ void givenEventDislikedByUser_whenLikedByUser_shouldRemoveDislikeAndAddLike() { @Test void dislikeTest() { UserVO userVO = getUserVO(); - User user = getUser(); - Event event = getEvent(); + User eventAuthor = getUser().setId(2L); + Event event = getEvent().setOrganizer(eventAuthor); event.setUsersDislikedEvents(new HashSet<>()); when(eventRepo.findById(anyLong())).thenReturn(Optional.of(event)); - when(userRepo.findById(user.getId())).thenReturn(Optional.of(user)); + when(userRepo.findById(event.getOrganizer().getId())).thenReturn(Optional.of(eventAuthor)); eventService.dislike(userVO, event.getId()); @@ -1375,16 +1388,29 @@ void dislikeTest() { } @Test - void testDislikeIfWasAlreadyPlaced() { + void testDislikeOwn() { UserVO userVO = getUserVO(); User user = getUser(); Event event = getEvent(); + + when(userRepo.findById(1L)).thenReturn(Optional.of(user)); + when(eventRepo.findById(1L)).thenReturn(Optional.of(event)); + + assertThrows(BadRequestException.class, () -> eventService.dislike(userVO, 1L)); + } + + @Test + void testDislikeIfWasAlreadyPlaced() { + UserVO userVO = getUserVO(); + User user = getUser(); + User eventAuthor = getUser().setId(2L); + Event event = getEvent().setOrganizer(eventAuthor); Set usersDisliked = new HashSet<>(); usersDisliked.add(user); event.setUsersDislikedEvents(usersDisliked); when(eventRepo.findById(anyLong())).thenReturn(Optional.of(event)); - when(userRepo.findById(user.getId())).thenReturn(Optional.of(user)); + when(userRepo.findById(event.getOrganizer().getId())).thenReturn(Optional.of(eventAuthor)); eventService.dislike(userVO, event.getId()); verify(eventRepo).save(event); @@ -1395,12 +1421,13 @@ void testDislikeIfWasAlreadyPlaced() { void givenEventLikedByUser_whenDislikedByUser_shouldRemoveLikeAndAddDislike() { UserVO userVO = getUserVO(); User user = getUser(); - Event event = getEvent(); + User eventAuthor = getUser().setId(2L); + Event event = getEvent().setOrganizer(eventAuthor); event.setUsersLikedEvents(new HashSet<>(Set.of(user))); event.setUsersDislikedEvents(new HashSet<>()); when(eventRepo.findById(anyLong())).thenReturn(Optional.of(event)); - when(userRepo.findById(user.getId())).thenReturn(Optional.of(user)); + when(userRepo.findById(event.getOrganizer().getId())).thenReturn(Optional.of(eventAuthor)); eventService.dislike(userVO, 1L); diff --git a/service/src/test/java/greencity/service/HabitServiceImplTest.java b/service/src/test/java/greencity/service/HabitServiceImplTest.java index 9f486fd05..9cf143597 100644 --- a/service/src/test/java/greencity/service/HabitServiceImplTest.java +++ b/service/src/test/java/greencity/service/HabitServiceImplTest.java @@ -1206,13 +1206,14 @@ void checkAccessOfOwnerToCustomHabitThrowsUserHasNoPermissionToAccessExceptionTe void likeTest() { UserVO userVO = getUserVO(); User user = getUser(); - Habit habit = getHabit().setUserId(user.getId()); + User habitAuthor = getUser().setId(2L); + Habit habit = getHabit().setUserId(2L); RatingPoints ratingPoints = RatingPoints.builder().id(1L).name("LIKE_COMMENT_OR_REPLY").points(1).build(); when(ratingPointsRepo.findByNameOrThrow("LIKE_COMMENT_OR_REPLY")).thenReturn(ratingPoints); when(habitRepo.findById(habit.getId())).thenReturn(Optional.of(habit)); + when(userRepo.findById(habit.getUserId())).thenReturn(Optional.of(habitAuthor)); when(modelMapper.map(userVO, User.class)).thenReturn(user); - when(userRepo.findById(user.getId())).thenReturn(Optional.of(user)); habitService.like(habit.getId(), userVO); @@ -1220,32 +1221,33 @@ void likeTest() { verify(modelMapper).map(userVO, User.class); verify(habitRepo).findById(habit.getId()); - verify(userRepo).findById(user.getId()); + verify(userRepo).findById(habitAuthor.getId()); } @Test void removeLikeTest() { UserVO userVO = getUserVO(); User user = getUser(); - Habit habit = getHabit().setUserId(user.getId()); + User habitAuthor = getUser().setId(2L); + Habit habit = getHabit().setUserId(2L); habit.getUsersLiked().add(user); RatingPoints ratingPoints = RatingPoints.builder().id(1L).name("UNDO_LIKE_HABIT").points(-1).build(); when(ratingPointsRepo.findByNameOrThrow("UNDO_LIKE_HABIT")).thenReturn(ratingPoints); when(habitRepo.findById(habit.getId())).thenReturn(Optional.of(habit)); - when(userRepo.findById(user.getId())).thenReturn(Optional.of(user)); + when(userRepo.findById(habit.getUserId())).thenReturn(Optional.of(habitAuthor)); habitService.like(habit.getId(), userVO); assertFalse(habit.getUsersLiked().stream().anyMatch(u -> u.getId().equals(userVO.getId()))); verify(habitRepo).findById(habit.getId()); - verify(userRepo).findById(user.getId()); + verify(userRepo).findById(habitAuthor.getId()); } @Test void removeLikeRemoveIfTest() { User user = getUser(); - Habit habit = getHabit(); + Habit habit = getHabit().setUserId(2L); habit.getUsersLiked().add(user); UserVO userVO = getUserVO(); @@ -1279,7 +1281,7 @@ void likeHabitUserNotFoundTest() { Habit habit = getHabit(); Long habitId = habit.getId(); User user = getUser(); - habit.setUserId(user.getId()); + habit.setUserId(3L); when(habitRepo.findById(habit.getId())).thenReturn(Optional.of(habit)); when(userRepo.findById(habit.getUserId())).thenReturn(Optional.empty()); @@ -1296,11 +1298,12 @@ void likeHabitUserNotFoundTest() { void dislikeTest() { UserVO userVO = getUserVO(); User user = getUser(); - Habit habit = getHabit().setUserId(user.getId()); + User habitAuthor = getUser().setId(2L); + Habit habit = getHabit().setUserId(2L); - when(habitRepo.findById(habit.getId())).thenReturn(Optional.of(habit)); + when(habitRepo.findById(1L)).thenReturn(Optional.of(habit)); + when(userRepo.findById(habit.getUserId())).thenReturn(Optional.of(habitAuthor)); when(modelMapper.map(userVO, User.class)).thenReturn(user); - when(userRepo.findById(user.getId())).thenReturn(Optional.of(user)); habitService.dislike(habit.getId(), userVO); @@ -1308,37 +1311,52 @@ void dislikeTest() { verify(modelMapper).map(userVO, User.class); verify(habitRepo).findById(habit.getId()); - verify(userRepo).findById(user.getId()); + verify(userRepo).findById(habitAuthor.getId()); } @Test - void removeDislikeTest() { + void dislikeOwnTest() { UserVO userVO = getUserVO(); User user = getUser(); Habit habit = getHabit().setUserId(user.getId()); + Long habitId = habit.getId(); + when(habitRepo.findById(habit.getId())).thenReturn(Optional.of(habit)); + when(modelMapper.map(userVO, User.class)).thenReturn(user); + when(userRepo.findById(user.getId())).thenReturn(Optional.of(user)); + + assertThrows(BadRequestException.class, () -> habitService.dislike(habitId, userVO)); + } + + @Test + void removeDislikeTest() { + UserVO userVO = getUserVO(); + User user = getUser(); + User habitAuthor = getUser().setId(2L); + Habit habit = getHabit().setUserId(2L); habit.getUsersDisliked().add(user); when(habitRepo.findById(habit.getId())).thenReturn(Optional.of(habit)); - when(userRepo.findById(user.getId())).thenReturn(Optional.of(user)); + when(userRepo.findById(habit.getUserId())).thenReturn(Optional.of(habitAuthor)); habitService.dislike(habit.getId(), userVO); assertFalse(habit.getUsersLiked().stream().anyMatch(u -> u.getId().equals(userVO.getId()))); verify(habitRepo).findById(habit.getId()); - verify(userRepo).findById(user.getId()); + verify(userRepo).findById(habitAuthor.getId()); } @Test void removeDislikeRemoveIfTest() { User user = getUser(); - Habit habit = getHabit(); + User habitAuthor = getUser().setId(2L); + Habit habit = getHabit().setUserId(2L); habit.getUsersDisliked().add(user); UserVO userVO = getUserVO(); userVO.setName("New Name"); when(habitRepo.findById(habit.getId())).thenReturn(Optional.of(habit)); - when(userRepo.findById(habit.getUserId())).thenReturn(Optional.of(user)); + when(userRepo.findById(habit.getUserId())).thenReturn(Optional.of(habitAuthor)); habitService.dislike(habit.getId(), userVO); assertFalse(habit.getUsersLiked().stream().anyMatch(u -> u.getId().equals(userVO.getId()))); verify(habitRepo).findById(habit.getId()); @@ -1365,7 +1383,7 @@ void dislikeHabitUserNotFoundTest() { Habit habit = getHabit(); Long habitId = habit.getId(); User user = getUser(); - habit.setUserId(user.getId()); + habit.setUserId(3L); when(habitRepo.findById(habit.getId())).thenReturn(Optional.of(habit)); when(userRepo.findById(habit.getUserId())).thenReturn(Optional.empty()); @@ -1382,12 +1400,13 @@ void dislikeHabitUserNotFoundTest() { void givenHabitLikedByUser_whenDislikedByUser_shouldRemoveLikeAndAddDislike() { UserVO userVO = getUserVO(); User user = getUser(); - Habit habit = getHabit(); + User habitAuthor = getUser().setId(2L); + Habit habit = getHabit().setUserId(2L); habit.setUsersLiked(new HashSet<>(Set.of(user))); habit.setUsersDisliked(new HashSet<>()); when(habitRepo.findById(anyLong())).thenReturn(Optional.of(habit)); - when(userRepo.findById(user.getId())).thenReturn(Optional.of(user)); + when(userRepo.findById(habit.getUserId())).thenReturn(Optional.of(habitAuthor)); habitService.dislike(1L, userVO); @@ -1399,12 +1418,13 @@ void givenHabitLikedByUser_whenDislikedByUser_shouldRemoveLikeAndAddDislike() { void givenHabitDislikedByUser_whenLikedByUser_shouldRemoveDislikeAndAddLike() { UserVO userVO = getUserVO(); User user = getUser(); - Habit habit = getHabit(); + User habitAuthor = getUser().setId(2L); + Habit habit = getHabit().setUserId(2L); habit.setUsersLiked(new HashSet<>()); habit.setUsersDisliked(new HashSet<>(Set.of(user))); when(habitRepo.findById(anyLong())).thenReturn(Optional.of(habit)); - when(userRepo.findById(user.getId())).thenReturn(Optional.of(user)); + when(userRepo.findById(habit.getUserId())).thenReturn(Optional.of(habitAuthor)); habitService.like(1L, userVO);