diff --git a/service/src/main/java/greencity/achievement/AchievementCalculation.java b/service/src/main/java/greencity/achievement/AchievementCalculation.java index 7dabc89525..0e08af8a1a 100644 --- a/service/src/main/java/greencity/achievement/AchievementCalculation.java +++ b/service/src/main/java/greencity/achievement/AchievementCalculation.java @@ -48,8 +48,7 @@ public AchievementCalculation( AchievementCategoryService achievementCategoryService, UserAchievementRepo userAchievementRepo, UserRepo userRepo, - AchievementRepo achievementRepo, - AchievementCategoryRepo achievementCategoryRepo, RatingCalculation ratingCalculation, UserService userService) { + AchievementRepo achievementRepo, RatingCalculation ratingCalculation, UserService userService) { this.userActionService = userActionService; this.achievementService = achievementService; this.achievementCategoryService = achievementCategoryService; @@ -136,10 +135,7 @@ private void saveAchievementToUser(Long userId, Long achievementCategoryId, int .build(); RatingCalculationEnum reason = RatingCalculationEnum.findEnumByName(achievement.getTitle()); UserVO user = userService.findById(userId); - - if (reason != null) { - ratingCalculation.ratingCalculation(reason, user); - } + ratingCalculation.ratingCalculation(reason, user); userAchievementRepo.save(userAchievement); calculateAchievement(userId, AchievementCategoryType.ACHIEVEMENT, AchievementAction.ASSIGN); } diff --git a/service/src/main/java/greencity/service/EcoNewsServiceImpl.java b/service/src/main/java/greencity/service/EcoNewsServiceImpl.java index fbf69bbbec..1f4c014dc6 100644 --- a/service/src/main/java/greencity/service/EcoNewsServiceImpl.java +++ b/service/src/main/java/greencity/service/EcoNewsServiceImpl.java @@ -23,6 +23,7 @@ import greencity.exception.exceptions.UnsupportedSortException; import greencity.filters.EcoNewsSpecification; import greencity.filters.SearchCriteria; +import greencity.mapping.AddEcoNewsCommentDtoResponseMapper; import greencity.repository.EcoNewsRepo; import greencity.repository.EcoNewsSearchRepo; import lombok.RequiredArgsConstructor; @@ -37,6 +38,7 @@ import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.ui.Model; import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartFile; diff --git a/service/src/test/java/greencity/achievement/AchievementCalculationTest.java b/service/src/test/java/greencity/achievement/AchievementCalculationTest.java index a45dab8a06..5e73a2a001 100644 --- a/service/src/test/java/greencity/achievement/AchievementCalculationTest.java +++ b/service/src/test/java/greencity/achievement/AchievementCalculationTest.java @@ -9,6 +9,7 @@ import greencity.entity.User; import greencity.entity.UserAchievement; import greencity.enums.*; +import greencity.exception.exceptions.NotFoundException; import greencity.rating.RatingCalculation; import greencity.repository.AchievementCategoryRepo; import greencity.repository.AchievementRepo; @@ -27,6 +28,7 @@ import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; @@ -59,6 +61,59 @@ class AchievementCalculationTest { @Mock private RatingCalculation ratingCalculation; + @Test + void calculateAchievement_reasonNull() { + AchievementCategoryVO achievementCategoryVO = new AchievementCategoryVO(1L, "ACHIEVEMENT"); + AchievementCategoryVO achievementCategoryVO2 = new AchievementCategoryVO(2L, "CREATE_NEWS"); + UserVO userVO = ModelUtils.getUserVO(); + UserActionVO userActionVO = new UserActionVO(1L, userVO, achievementCategoryVO, 0); + User user = ModelUtils.getUser(); + Achievement achievement = ModelUtils.getAchievement(); + achievement.setTitle("bla bla"); + UserAchievement userAchievement = ModelUtils.getUserAchievement(); + user.setUserAchievements(Collections.singletonList(userAchievement)); + AchievementVO achievementVO = + new AchievementVO(1L, "CREATED_5_NEWS", "CREATED_5_NEWS", "CREATED_5_NEWS", new AchievementCategoryVO(), 1); + when(achievementCategoryService.findByName(AchievementCategoryType.CREATE_NEWS.name())) + .thenReturn(achievementCategoryVO); + when(userActionService.findUserActionByUserIdAndAchievementCategory(any(), any())).thenReturn(userActionVO); + when(achievementRepo.findByAchievementCategoryIdAndCondition(anyLong(), any())) + .thenReturn(Optional.of(achievement)); + when(userRepo.findById(anyLong())).thenReturn(Optional.of(user)); + when(achievementCategoryService.findByName("CREATE_NEWS")).thenReturn(achievementCategoryVO2); + when(achievementService.findByCategoryIdAndCondition(2L, 1)).thenReturn(achievementVO); + + assertThrows(NotFoundException.class, () -> achievementCalculation.calculateAchievement(1L, + AchievementCategoryType.CREATE_NEWS, AchievementAction.ASSIGN)); + } + + @Test + void calculateAchievement_UNDO_reasonNull() { + AchievementCategoryVO achievementCategoryVO = new AchievementCategoryVO(1L, "ACHIEVEMENT"); + AchievementCategoryVO achievementCategoryVO2 = new AchievementCategoryVO(2L, "CREATE_NEWS"); + UserVO userVO = ModelUtils.getUserVO(); + UserActionVO userActionVO = new UserActionVO(1L, userVO, achievementCategoryVO, 0); + User user = ModelUtils.getUser(); + Achievement achievement = ModelUtils.getAchievement(); + achievement.setTitle("Bla bla"); + UserAchievement userAchievement = ModelUtils.getUserAchievement(); + user.setUserAchievements(Collections.singletonList(userAchievement)); + AchievementVO achievementVO = + new AchievementVO(1L, "CREATED_5_NEWS___", "CREATED_5_NEWS", "CREATED_5_NEWS", new AchievementCategoryVO(), + 1); + when(achievementCategoryService.findByName(AchievementCategoryType.CREATE_NEWS.name())) + .thenReturn(achievementCategoryVO); + when(userActionService.findUserActionByUserIdAndAchievementCategory(any(), any())).thenReturn(userActionVO); + when(achievementRepo.findByAchievementCategoryIdAndCondition(anyLong(), any())) + .thenReturn(Optional.of(achievement)); + when(userRepo.findById(anyLong())).thenReturn(Optional.of(user)); + when(achievementCategoryService.findByName("CREATE_NEWS")).thenReturn(achievementCategoryVO2); + when(achievementService.findByCategoryIdAndCondition(2L, -1)).thenReturn(achievementVO); + + assertThrows(NotFoundException.class, () -> achievementCalculation.calculateAchievement(1L, + AchievementCategoryType.CREATE_NEWS, AchievementAction.DELETE)); + } + @Test void calculateAchievement() { AchievementCategoryVO achievementCategoryVO = new AchievementCategoryVO(1L, "ACHIEVEMENT"); diff --git a/service/src/test/java/greencity/service/EcoNewsCommentServiceImplTest.java b/service/src/test/java/greencity/service/EcoNewsCommentServiceImplTest.java index 01760bd84d..9155b3d277 100644 --- a/service/src/test/java/greencity/service/EcoNewsCommentServiceImplTest.java +++ b/service/src/test/java/greencity/service/EcoNewsCommentServiceImplTest.java @@ -3,6 +3,7 @@ import static greencity.ModelUtils.getUser; import static greencity.ModelUtils.getUserVO; +import greencity.achievement.AchievementCalculation; import greencity.enums.CommentStatus; import greencity.exception.exceptions.UserHasNoPermissionToAccessException; import javax.servlet.http.HttpServletRequest; @@ -70,6 +71,9 @@ class EcoNewsCommentServiceImplTest { private UserService userService; @Mock private RatingCalculation ratingCalculation; + @Mock + private AchievementCalculation achievementCalculation; + private String token = "token"; @Test diff --git a/service/src/test/java/greencity/service/EcoNewsServiceImplTest.java b/service/src/test/java/greencity/service/EcoNewsServiceImplTest.java index 2c932d8114..7c37939184 100644 --- a/service/src/test/java/greencity/service/EcoNewsServiceImplTest.java +++ b/service/src/test/java/greencity/service/EcoNewsServiceImplTest.java @@ -127,14 +127,15 @@ void saveWithExistedImage() throws IOException { MultipartFile image = ModelUtils.getFile(); String imageToEncode = Base64.getEncoder().encodeToString(image.getBytes()); addEcoNewsDtoRequest.setImage(imageToEncode); - when(modelMapper.map(addEcoNewsDtoRequest, EcoNews.class)).thenReturn(ecoNews); when(restClient.findByEmail(TestConst.EMAIL)).thenReturn(ModelUtils.getUserVO()); when(fileService.upload(any(MultipartFile.class))).thenReturn(ModelUtils.getUrl().toString()); List tagVOList = Collections.singletonList(ModelUtils.getTagVO()); when(tagService.findTagsByNamesAndType(anyList(), eq(TagType.ECO_NEWS))).thenReturn(tagVOList); when(ecoNewsRepo.save(any(EcoNews.class))).thenReturn(ecoNews); + addEcoNewsDtoResponse.setEcoNewsAuthorDto(ModelUtils.getEcoNewsAuthorDto()); when(modelMapper.map(ecoNews, AddEcoNewsDtoResponse.class)).thenReturn(addEcoNewsDtoResponse); + when(modelMapper.map(ModelUtils.getUserVO(), User.class)).thenReturn(ModelUtils.getUser()); AddEcoNewsDtoResponse actual = ecoNewsService.save(addEcoNewsDtoRequest, image, TestConst.EMAIL); diff --git a/service/src/test/java/greencity/service/EventCommentServiceImplTest.java b/service/src/test/java/greencity/service/EventCommentServiceImplTest.java index eb45e335e6..1fb71dfe5d 100644 --- a/service/src/test/java/greencity/service/EventCommentServiceImplTest.java +++ b/service/src/test/java/greencity/service/EventCommentServiceImplTest.java @@ -1,6 +1,7 @@ package greencity.service; import greencity.ModelUtils; +import greencity.achievement.AchievementCalculation; import greencity.client.RestClient; import greencity.constant.ErrorMessage; import greencity.dto.PageableDto; @@ -73,6 +74,8 @@ class EventCommentServiceImplTest { HttpServletRequest httpServletRequest; @Mock private RatingCalculation ratingCalculation; + @Mock + private AchievementCalculation achievementCalculation; @Test void save() { diff --git a/service/src/test/java/greencity/service/PlaceCommentServiceImplTest.java b/service/src/test/java/greencity/service/PlaceCommentServiceImplTest.java index 38853f0b09..250fcd9a3d 100644 --- a/service/src/test/java/greencity/service/PlaceCommentServiceImplTest.java +++ b/service/src/test/java/greencity/service/PlaceCommentServiceImplTest.java @@ -1,6 +1,7 @@ package greencity.service; import greencity.ModelUtils; +import greencity.achievement.AchievementCalculation; import greencity.client.RestClient; import greencity.dto.PageableDto; import greencity.dto.comment.AddCommentDto; @@ -54,6 +55,8 @@ class PlaceCommentServiceImplTest { private UserService userService; @Mock private RatingCalculation ratingCalculation; + @Mock + private AchievementCalculation achievementCalculation; @Test void findByIdTest() {