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 24, 2023
1 parent a57c168 commit 64158fd
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import greencity.constant.HttpStatuses;
import greencity.dto.achievement.AchievementDTO;
import greencity.dto.achievement.AchievementVO;
import greencity.enums.AchievementCategoryType;
import greencity.service.AchievementService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ void testRatingPoints() {
}

@Test
public void testFindEnumByNameValidName() {
void testFindEnumByNameValidName() {
assertEquals(RatingCalculationEnum.DAYS_OF_HABIT_IN_PROGRESS,
RatingCalculationEnum.findEnumByName("DAYS_OF_HABIT_IN_PROGRESS"));
assertEquals(RatingCalculationEnum.CREATE_NEWS, RatingCalculationEnum.findEnumByName("CREATE_NEWS"));
}

@Test
public void testFindEnumByNameInvalidName() {
void testFindEnumByNameInvalidName() {
assertThrows(NotFoundException.class, () -> RatingCalculationEnum.findEnumByName("INVALID_NAME"));
}

@Test
public void testErrorMessageOnNotFound() {
void testErrorMessageOnNotFound() {
NotFoundException exception =
assertThrows(NotFoundException.class, () -> RatingCalculationEnum.findEnumByName("INVALID_NAME"));
assertEquals(ErrorMessage.RATING_CALCULATION_ENUM_NOT_FOUND_BY_NAME + "INVALID_NAME", exception.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.springframework.stereotype.Component;

import java.util.NoSuchElementException;
import java.util.concurrent.CompletableFuture;

@Component
public class AchievementCalculation {
Expand Down
2 changes: 0 additions & 2 deletions service/src/main/java/greencity/rating/RatingCalculation.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

import javax.servlet.http.HttpServletRequest;

import static greencity.constant.AppConstant.AUTHORIZATION;

@Component
@AllArgsConstructor
public class RatingCalculation {
Expand Down
5 changes: 3 additions & 2 deletions service/src/test/java/greencity/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,7 @@ public static AdvicePostDto getAdvicePostDto() {

public static Achievement getAchievement() {
return new Achievement(1L,
"Test", "Test", "Test",
"CREATED_5_NEWS", "CREATED_5_NEWS", "CREATED_5_NEWS",
Collections.emptyList(),
new AchievementCategory(), 1);
}
Expand All @@ -1388,7 +1388,8 @@ public static AchievementCategory getAchievementCategory() {
}

public static AchievementVO getAchievementVO() {
return new AchievementVO(1L, "Test", "Test", "Test", new AchievementCategoryVO(), 1);
return new AchievementVO(1L, "CREATED_5_NEWS", "CREATED_5_NEWS", "CREATED_5_NEWS", new AchievementCategoryVO(),
1);
}

public static AchievementPostDto getAchievementPostDto() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import greencity.ModelUtils;
import greencity.client.RestClient;
import greencity.dto.achievement.AchievementVO;
import greencity.dto.achievementcategory.AchievementCategoryVO;
import greencity.dto.user.UserVO;
import greencity.dto.useraction.UserActionVO;
import greencity.entity.User;
import greencity.entity.UserAchievement;
import greencity.enums.AchievementCategoryType;
import greencity.enums.AchievementAction;
import greencity.enums.RatingCalculationEnum;
import greencity.enums.*;
import greencity.rating.RatingCalculation;
import greencity.repository.AchievementCategoryRepo;
import greencity.repository.AchievementRepo;
import greencity.repository.UserAchievementRepo;
Expand All @@ -21,6 +22,7 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.modelmapper.ModelMapper;

import greencity.entity.Achievement;
import java.util.Collections;
import java.util.Optional;

Expand Down Expand Up @@ -54,9 +56,61 @@ class AchievementCalculationTest {
private AchievementCalculation achievementCalculation;
@Mock
private UserService userService;
@Mock
private RatingCalculation ratingCalculation;

@Test
void calculateAchievement() {
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();
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("ACHIEVEMENT")).thenReturn(achievementCategoryVO);
when(achievementCategoryService.findByName("CREATE_NEWS")).thenReturn(achievementCategoryVO2);
when(achievementService.findByCategoryIdAndCondition(2L, 1)).thenReturn(achievementVO);
achievementCalculation.calculateAchievement(1L, AchievementCategoryType.CREATE_NEWS, AchievementAction.ASSIGN);
assertEquals(2, userActionVO.getCount());
}

@Test
void calculateAchievement_UNDO() {
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();
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("ACHIEVEMENT")).thenReturn(achievementCategoryVO);
when(achievementCategoryService.findByName("CREATE_NEWS")).thenReturn(achievementCategoryVO2);
when(achievementService.findByCategoryIdAndCondition(2L, -1)).thenReturn(achievementVO);
achievementCalculation.calculateAchievement(1L, AchievementCategoryType.CREATE_NEWS, AchievementAction.DELETE);
assertEquals(-2, userActionVO.getCount());
}

@Test
void calculateAchievement_achievemmentNull() {
AchievementCategoryVO achievementCategoryVO = ModelUtils.getAchievementCategoryVO();
AchievementCategoryVO achievementCategoryVO2 = ModelUtils.getAchievementCategoryVO();
achievementCategoryVO2.setId(2L);
Expand Down

0 comments on commit 64158fd

Please sign in to comment.