Skip to content

Commit

Permalink
Formatted and added test to coverege
Browse files Browse the repository at this point in the history
  • Loading branch information
ospodaryk committed Sep 25, 2023
1 parent 64158fd commit 8e9ad2a
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;

Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,6 +71,9 @@ class EcoNewsCommentServiceImplTest {
private UserService userService;
@Mock
private RatingCalculation ratingCalculation;
@Mock
private AchievementCalculation achievementCalculation;

private String token = "token";

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TagVO> 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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -73,6 +74,8 @@ class EventCommentServiceImplTest {
HttpServletRequest httpServletRequest;
@Mock
private RatingCalculation ratingCalculation;
@Mock
private AchievementCalculation achievementCalculation;

@Test
void save() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -54,6 +55,8 @@ class PlaceCommentServiceImplTest {
private UserService userService;
@Mock
private RatingCalculation ratingCalculation;
@Mock
private AchievementCalculation achievementCalculation;

@Test
void findByIdTest() {
Expand Down

0 comments on commit 8e9ad2a

Please sign in to comment.