From a6b38d1c4729c161af9e6468b256f59e6febcad5 Mon Sep 17 00:00:00 2001 From: ospodaryk Date: Wed, 11 Oct 2023 22:00:37 +0300 Subject: [PATCH 01/16] Added endpoint to get achievements for user --- .../controller/AchievementController.java | 23 +++++++++++++++++++ .../greencity/service/AchievementService.java | 3 +++ .../service/AchievementServiceImpl.java | 20 +++++++++++++++- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/greencity/controller/AchievementController.java b/core/src/main/java/greencity/controller/AchievementController.java index fbe5af16e6..b153465aee 100644 --- a/core/src/main/java/greencity/controller/AchievementController.java +++ b/core/src/main/java/greencity/controller/AchievementController.java @@ -1,19 +1,25 @@ package greencity.controller; +import greencity.annotations.ApiPageableWithoutSort; import greencity.constant.HttpStatuses; +import greencity.dto.PageableAdvancedDto; import greencity.dto.achievement.AchievementDTO; import greencity.dto.achievement.AchievementVO; +import greencity.dto.event.EventDto; import greencity.service.AchievementService; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; +import java.security.Principal; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import springfox.documentation.annotations.ApiIgnore; @RestController @RequestMapping("/achievements") @@ -43,4 +49,21 @@ public AchievementController(AchievementService achievementService) { public ResponseEntity> getAll() { return ResponseEntity.status(HttpStatus.OK).body(achievementService.findAll()); } + + /** + * Method returns all achievements, available for achieving. + * + * @return list of {@link AchievementDTO} + */ + @ApiOperation(value = "Get all achievements.") + @ApiResponses(value = { + @ApiResponse(code = 200, message = HttpStatuses.OK), + @ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST), + @ApiResponse(code = 401, message = HttpStatuses.UNAUTHORIZED), + }) + @GetMapping("") + public ResponseEntity> getAllAchievementsByUserID(@ApiIgnore Principal principal) { + return ResponseEntity.status(HttpStatus.OK).body(achievementService.findAllByUserID(principal.getName())); + } + } diff --git a/service-api/src/main/java/greencity/service/AchievementService.java b/service-api/src/main/java/greencity/service/AchievementService.java index 37615be645..37a60f1af2 100644 --- a/service-api/src/main/java/greencity/service/AchievementService.java +++ b/service-api/src/main/java/greencity/service/AchievementService.java @@ -6,9 +6,12 @@ import greencity.enums.AchievementAction; import org.springframework.data.domain.Pageable; +import java.security.Principal; import java.util.List; public interface AchievementService { + public List findAllByUserID(String principalEmail); + /** * Method for finding all the achievements. * diff --git a/service/src/main/java/greencity/service/AchievementServiceImpl.java b/service/src/main/java/greencity/service/AchievementServiceImpl.java index 47dd3fcec2..34d2e9b881 100644 --- a/service/src/main/java/greencity/service/AchievementServiceImpl.java +++ b/service/src/main/java/greencity/service/AchievementServiceImpl.java @@ -12,6 +12,7 @@ import greencity.entity.Achievement; import greencity.entity.AchievementCategory; +import greencity.entity.User; import greencity.enums.AchievementCategoryType; import greencity.enums.AchievementAction; import greencity.exception.exceptions.NotDeletedException; @@ -19,9 +20,11 @@ import greencity.exception.exceptions.NotUpdatedException; import greencity.repository.AchievementRepo; +import java.security.Principal; import java.util.List; import java.util.stream.Collectors; +import greencity.repository.UserAchievementRepo; import lombok.AllArgsConstructor; import org.modelmapper.ModelMapper; import org.springframework.cache.annotation.Cacheable; @@ -43,6 +46,7 @@ public class AchievementServiceImpl implements AchievementService { private final UserActionService userActionService; private AchievementCalculation achievementCalculation; private UserService userService; + private final UserAchievementRepo userAchievementRepo; /** * {@inheritDoc} @@ -88,7 +92,21 @@ public AchievementVO save(AchievementPostDto achievementPostDto) { @Cacheable(value = CacheConstants.ALL_ACHIEVEMENTS_CACHE_NAME) @Override public List findAll() { - return achievementRepo.findAll() + return userAchievementRepo.findAll() + .stream() + .map(achieve -> modelMapper.map(achieve, AchievementVO.class)) + .collect(Collectors.toList()); + } + + /** + * {@inheritDoc} + * + * @author Oksana Spodaryk + */ + @Override + public List findAllByUserID(String principalEmail) { + User currentUser = modelMapper.map(userService.findByEmail(principalEmail), User.class); + return userAchievementRepo.getUserAchievementByUserId(currentUser.getId()) .stream() .map(achieve -> modelMapper.map(achieve, AchievementVO.class)) .collect(Collectors.toList()); From e558cad2854fee284f2511cbeb3ceb6ef897d12c Mon Sep 17 00:00:00 2001 From: ospodaryk Date: Thu, 12 Oct 2023 10:09:59 +0300 Subject: [PATCH 02/16] Added method to get achevements --- .../java/greencity/config/SecurityConfig.java | 1 + .../controller/AchievementController.java | 2 +- .../greencity/service/AchievementService.java | 11 ++++++++++- .../greencity/service/AchievementServiceImpl.java | 15 ++++++++++++--- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/greencity/config/SecurityConfig.java b/core/src/main/java/greencity/config/SecurityConfig.java index 84db7c579d..8537675f2a 100644 --- a/core/src/main/java/greencity/config/SecurityConfig.java +++ b/core/src/main/java/greencity/config/SecurityConfig.java @@ -174,6 +174,7 @@ protected void configure(HttpSecurity http) throws Exception { .permitAll() .antMatchers(HttpMethod.GET, "/achievements", + "/achievements/achieved", "/advices/random/{habitId}", "/advices", CUSTOM_SHOPPING_LIST_ITEMS, diff --git a/core/src/main/java/greencity/controller/AchievementController.java b/core/src/main/java/greencity/controller/AchievementController.java index b153465aee..a3226c8f4a 100644 --- a/core/src/main/java/greencity/controller/AchievementController.java +++ b/core/src/main/java/greencity/controller/AchievementController.java @@ -61,7 +61,7 @@ public ResponseEntity> getAll() { @ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST), @ApiResponse(code = 401, message = HttpStatuses.UNAUTHORIZED), }) - @GetMapping("") + @GetMapping("/achieved") public ResponseEntity> getAllAchievementsByUserID(@ApiIgnore Principal principal) { return ResponseEntity.status(HttpStatus.OK).body(achievementService.findAllByUserID(principal.getName())); } diff --git a/service-api/src/main/java/greencity/service/AchievementService.java b/service-api/src/main/java/greencity/service/AchievementService.java index 37a60f1af2..efc53f62cf 100644 --- a/service-api/src/main/java/greencity/service/AchievementService.java +++ b/service-api/src/main/java/greencity/service/AchievementService.java @@ -10,7 +10,16 @@ import java.util.List; public interface AchievementService { - public List findAllByUserID(String principalEmail); + /** + * Retrieves a list of achievements associated with a user identified by the + * provided email. + * + * @param principalEmail The email address of the user whose achievements are to + * be fetched. + * @return A list of {@link AchievementVO} representing the achievements of the + * user. The list can be empty if the user has no achievements. + */ + List findAllByUserID(String principalEmail); /** * Method for finding all the achievements. diff --git a/service/src/main/java/greencity/service/AchievementServiceImpl.java b/service/src/main/java/greencity/service/AchievementServiceImpl.java index 9201f3123b..7bd84db56a 100644 --- a/service/src/main/java/greencity/service/AchievementServiceImpl.java +++ b/service/src/main/java/greencity/service/AchievementServiceImpl.java @@ -16,6 +16,7 @@ import greencity.entity.AchievementCategory; import greencity.entity.User; +import greencity.entity.UserAchievement; import greencity.enums.AchievementCategoryType; import greencity.enums.AchievementAction; import greencity.exception.exceptions.NotDeletedException; @@ -23,7 +24,7 @@ import greencity.exception.exceptions.NotUpdatedException; import greencity.repository.AchievementRepo; -import java.security.Principal; +import java.util.Optional; import java.util.List; import java.util.stream.Collectors; @@ -95,7 +96,7 @@ public AchievementVO save(AchievementPostDto achievementPostDto) { @Cacheable(value = CacheConstants.ALL_ACHIEVEMENTS_CACHE_NAME) @Override public List findAll() { - return userAchievementRepo.findAll() + return achievementRepo.findAll() .stream() .map(achieve -> modelMapper.map(achieve, AchievementVO.class)) .collect(Collectors.toList()); @@ -106,11 +107,19 @@ public List findAll() { * * @author Oksana Spodaryk */ + @Override public List findAllByUserID(String principalEmail) { User currentUser = modelMapper.map(userService.findByEmail(principalEmail), User.class); - return userAchievementRepo.getUserAchievementByUserId(currentUser.getId()) + List achievemnetsId = userAchievementRepo.getUserAchievementByUserId(currentUser.getId()) + .stream() + .map(userAchievement -> userAchievement.getAchievement().getId()) + .collect(Collectors.toList()); + return achievemnetsId .stream() + .map(achievementRepo::findById) + .filter(Optional::isPresent) + .map(Optional::get) .map(achieve -> modelMapper.map(achieve, AchievementVO.class)) .collect(Collectors.toList()); } From d6332334c8b77c07a167908b1153c56f6667544d Mon Sep 17 00:00:00 2001 From: ospodaryk Date: Thu, 12 Oct 2023 10:12:37 +0300 Subject: [PATCH 03/16] Added tests --- .../controller/AchievementControllerTest.java | 11 ++++++++++ .../service/AchievementServiceImpl.java | 1 - .../service/AchievementServiceImplTest.java | 21 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/core/src/test/java/greencity/controller/AchievementControllerTest.java b/core/src/test/java/greencity/controller/AchievementControllerTest.java index f0dc6d5727..d0c16bc27b 100644 --- a/core/src/test/java/greencity/controller/AchievementControllerTest.java +++ b/core/src/test/java/greencity/controller/AchievementControllerTest.java @@ -1,6 +1,7 @@ package greencity.controller; import greencity.enums.AchievementCategoryType; +import greencity.repository.AchievementRepo; import greencity.service.AchievementService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -14,6 +15,10 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import java.security.Principal; + +import static greencity.ModelUtils.getPrincipal; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.verify; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; @@ -24,6 +29,7 @@ class AchievementControllerTest { private static final String achievementLink = "/achievements"; private MockMvc mockMvc; + private final Principal principal = getPrincipal(); @InjectMocks private AchievementController achievementController; @@ -44,4 +50,9 @@ void findAllTest() throws Exception { verify(achievementService).findAll(); } + @Test + void findAllByUserIDTest() throws Exception { + mockMvc.perform(get(achievementLink + "/achieved").principal(principal)).andExpect(status().isOk()); + verify(achievementService).findAllByUserID(anyString()); + } } diff --git a/service/src/main/java/greencity/service/AchievementServiceImpl.java b/service/src/main/java/greencity/service/AchievementServiceImpl.java index 7bd84db56a..c22487ff82 100644 --- a/service/src/main/java/greencity/service/AchievementServiceImpl.java +++ b/service/src/main/java/greencity/service/AchievementServiceImpl.java @@ -107,7 +107,6 @@ public List findAll() { * * @author Oksana Spodaryk */ - @Override public List findAllByUserID(String principalEmail) { User currentUser = modelMapper.map(userService.findByEmail(principalEmail), User.class); diff --git a/service/src/test/java/greencity/service/AchievementServiceImplTest.java b/service/src/test/java/greencity/service/AchievementServiceImplTest.java index 2c23fbcaef..2dab984688 100644 --- a/service/src/test/java/greencity/service/AchievementServiceImplTest.java +++ b/service/src/test/java/greencity/service/AchievementServiceImplTest.java @@ -12,6 +12,7 @@ import greencity.entity.Achievement; import greencity.entity.AchievementCategory; +import greencity.entity.User; import greencity.enums.AchievementCategoryType; import greencity.enums.AchievementAction; import greencity.exception.exceptions.NotDeletedException; @@ -99,6 +100,26 @@ void findAllByPageableTest() { assertEquals(10, pageableAdvancedDto.getTotalElements()); } + @Test + void findAllByUserIDTest() { + when(userService.findByEmail("email@gmail.com")).thenReturn(ModelUtils.getUserVO()); + when(modelMapper.map(ModelUtils.getUserVO(), User.class)).thenReturn(ModelUtils.getUser()); + when(userAchievementRepo.getUserAchievementByUserId(anyLong())) + .thenReturn(Arrays.asList(ModelUtils.getUserAchievement())); + when(achievementRepo.findById(anyLong())).thenReturn(Optional.of(ModelUtils.getAchievement())); + when(modelMapper.map(ModelUtils.getAchievement(), AchievementVO.class)) + .thenReturn(ModelUtils.getAchievementVO()); + + List findAllResult = achievementService.findAllByUserID("email@gmail.com"); + assertEquals(findAllResult, Arrays.asList(ModelUtils.getAchievementVO())); + + verify(userService).findByEmail("email@gmail.com"); + verify(modelMapper).map(ModelUtils.getUserVO(), User.class); + verify(userAchievementRepo).getUserAchievementByUserId(anyLong()); + verify(modelMapper).map(ModelUtils.getAchievement(), AchievementVO.class); + + } + @Test void saveTest() { Achievement achievement = ModelUtils.getAchievement(); From bbdc308e5d0d06d4518ca73d17489d896571c1ef Mon Sep 17 00:00:00 2001 From: ospodaryk Date: Thu, 12 Oct 2023 10:32:00 +0300 Subject: [PATCH 04/16] formatted --- .../controller/AchievementController.java | 1 - .../greencity/service/AchievementService.java | 22 +++---- .../service/AchievementServiceImpl.java | 60 +++++++++---------- 3 files changed, 40 insertions(+), 43 deletions(-) diff --git a/core/src/main/java/greencity/controller/AchievementController.java b/core/src/main/java/greencity/controller/AchievementController.java index a3226c8f4a..3697e7626c 100644 --- a/core/src/main/java/greencity/controller/AchievementController.java +++ b/core/src/main/java/greencity/controller/AchievementController.java @@ -65,5 +65,4 @@ public ResponseEntity> getAll() { public ResponseEntity> getAllAchievementsByUserID(@ApiIgnore Principal principal) { return ResponseEntity.status(HttpStatus.OK).body(achievementService.findAllByUserID(principal.getName())); } - } diff --git a/service-api/src/main/java/greencity/service/AchievementService.java b/service-api/src/main/java/greencity/service/AchievementService.java index efc53f62cf..2519ebf8f7 100644 --- a/service-api/src/main/java/greencity/service/AchievementService.java +++ b/service-api/src/main/java/greencity/service/AchievementService.java @@ -10,17 +10,6 @@ import java.util.List; public interface AchievementService { - /** - * Retrieves a list of achievements associated with a user identified by the - * provided email. - * - * @param principalEmail The email address of the user whose achievements are to - * be fetched. - * @return A list of {@link AchievementVO} representing the achievements of the - * user. The list can be empty if the user has no achievements. - */ - List findAllByUserID(String principalEmail); - /** * Method for finding all the achievements. * @@ -106,4 +95,15 @@ public interface AchievementService { */ void calculateAchievements(Long id, AchievementCategoryType achievementCategory, AchievementAction achievementAction); + + /** + * Retrieves a list of achievements associated with a user identified by the + * provided email. + * + * @param principalEmail The email address of the user whose achievements are to + * be fetched. + * @return A list of {@link AchievementVO} representing the achievements of the + * user. The list can be empty if the user has no achievements. + */ + List findAllByUserID(String principalEmail); } diff --git a/service/src/main/java/greencity/service/AchievementServiceImpl.java b/service/src/main/java/greencity/service/AchievementServiceImpl.java index c22487ff82..330e386fbe 100644 --- a/service/src/main/java/greencity/service/AchievementServiceImpl.java +++ b/service/src/main/java/greencity/service/AchievementServiceImpl.java @@ -16,7 +16,6 @@ import greencity.entity.AchievementCategory; import greencity.entity.User; -import greencity.entity.UserAchievement; import greencity.enums.AchievementCategoryType; import greencity.enums.AchievementAction; import greencity.exception.exceptions.NotDeletedException; @@ -52,6 +51,35 @@ public class AchievementServiceImpl implements AchievementService { private UserService userService; private final UserAchievementRepo userAchievementRepo; + /** + * {@inheritDoc} + * + * @author Yuriy Olkhovskyi + */ + @Cacheable(value = CacheConstants.ALL_ACHIEVEMENTS_CACHE_NAME) + @Override + public List findAll() { + return achievementRepo.findAll() + .stream() + .map(achieve -> modelMapper.map(achieve, AchievementVO.class)) + .collect(Collectors.toList()); + } + + /** + * {@inheritDoc} + * + * @author Orest Mamchuk + */ + @Override + public PageableAdvancedDto findAll(Pageable page) { + Page pages = achievementRepo.findAll(page); + List achievementVOS = pages + .stream() + .map(achievement -> modelMapper.map(achievement, AchievementVO.class)) + .collect(Collectors.toList()); + return createPageable(achievementVOS, pages); + } + /** * {@inheritDoc} * @@ -65,7 +93,6 @@ public AchievementVO save(AchievementPostDto achievementPostDto) { achievement.setTitle(achievementPostDto.getTitle()); achievement.setName(achievementPostDto.getName()); achievement.setNameEng(achievementPostDto.getNameEng()); - achievement.setAchievementCategory(modelMapper.map(achievementCategoryVO, AchievementCategory.class)); AchievementVO achievementVO = modelMapper.map(achievementRepo.save(achievement), AchievementVO.class); UserAchievementVO userAchievementVO = new UserAchievementVO(); @@ -88,20 +115,6 @@ public AchievementVO save(AchievementPostDto achievementPostDto) { return achievementVO; } - /** - * {@inheritDoc} - * - * @author Yuriy Olkhovskyi - */ - @Cacheable(value = CacheConstants.ALL_ACHIEVEMENTS_CACHE_NAME) - @Override - public List findAll() { - return achievementRepo.findAll() - .stream() - .map(achieve -> modelMapper.map(achieve, AchievementVO.class)) - .collect(Collectors.toList()); - } - /** * {@inheritDoc} * @@ -123,21 +136,6 @@ public List findAllByUserID(String principalEmail) { .collect(Collectors.toList()); } - /** - * {@inheritDoc} - * - * @author Orest Mamchuk - */ - @Override - public PageableAdvancedDto findAll(Pageable page) { - Page pages = achievementRepo.findAll(page); - List achievementVOS = pages - .stream() - .map(achievement -> modelMapper.map(achievement, AchievementVO.class)) - .collect(Collectors.toList()); - return createPageable(achievementVOS, pages); - } - private PageableAdvancedDto createPageable(List achievementVOS, Page pages) { return new PageableAdvancedDto<>( From c14bb542b07a75a520733e2a341c764a1417f69d Mon Sep 17 00:00:00 2001 From: ospodaryk Date: Thu, 12 Oct 2023 10:38:40 +0300 Subject: [PATCH 05/16] fixed codesmell --- .../main/java/greencity/controller/AchievementController.java | 4 ---- .../src/main/java/greencity/service/AchievementService.java | 1 - 2 files changed, 5 deletions(-) diff --git a/core/src/main/java/greencity/controller/AchievementController.java b/core/src/main/java/greencity/controller/AchievementController.java index 3697e7626c..0ca4c6098e 100644 --- a/core/src/main/java/greencity/controller/AchievementController.java +++ b/core/src/main/java/greencity/controller/AchievementController.java @@ -1,11 +1,8 @@ package greencity.controller; -import greencity.annotations.ApiPageableWithoutSort; import greencity.constant.HttpStatuses; -import greencity.dto.PageableAdvancedDto; import greencity.dto.achievement.AchievementDTO; import greencity.dto.achievement.AchievementVO; -import greencity.dto.event.EventDto; import greencity.service.AchievementService; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; @@ -15,7 +12,6 @@ import java.util.List; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; diff --git a/service-api/src/main/java/greencity/service/AchievementService.java b/service-api/src/main/java/greencity/service/AchievementService.java index 2519ebf8f7..8e3b45dc4e 100644 --- a/service-api/src/main/java/greencity/service/AchievementService.java +++ b/service-api/src/main/java/greencity/service/AchievementService.java @@ -6,7 +6,6 @@ import greencity.enums.AchievementAction; import org.springframework.data.domain.Pageable; -import java.security.Principal; import java.util.List; public interface AchievementService { From b5ef14bbac1ba13b676ce1f70ccd57f904e84f66 Mon Sep 17 00:00:00 2001 From: ospodaryk Date: Thu, 12 Oct 2023 10:41:31 +0300 Subject: [PATCH 06/16] fixed unnesesary changes --- .../service/AchievementServiceImpl.java | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/service/src/main/java/greencity/service/AchievementServiceImpl.java b/service/src/main/java/greencity/service/AchievementServiceImpl.java index 330e386fbe..daf02eb4b7 100644 --- a/service/src/main/java/greencity/service/AchievementServiceImpl.java +++ b/service/src/main/java/greencity/service/AchievementServiceImpl.java @@ -51,35 +51,6 @@ public class AchievementServiceImpl implements AchievementService { private UserService userService; private final UserAchievementRepo userAchievementRepo; - /** - * {@inheritDoc} - * - * @author Yuriy Olkhovskyi - */ - @Cacheable(value = CacheConstants.ALL_ACHIEVEMENTS_CACHE_NAME) - @Override - public List findAll() { - return achievementRepo.findAll() - .stream() - .map(achieve -> modelMapper.map(achieve, AchievementVO.class)) - .collect(Collectors.toList()); - } - - /** - * {@inheritDoc} - * - * @author Orest Mamchuk - */ - @Override - public PageableAdvancedDto findAll(Pageable page) { - Page pages = achievementRepo.findAll(page); - List achievementVOS = pages - .stream() - .map(achievement -> modelMapper.map(achievement, AchievementVO.class)) - .collect(Collectors.toList()); - return createPageable(achievementVOS, pages); - } - /** * {@inheritDoc} * @@ -115,6 +86,35 @@ public AchievementVO save(AchievementPostDto achievementPostDto) { return achievementVO; } + /** + * {@inheritDoc} + * + * @author Yuriy Olkhovskyi + */ + @Cacheable(value = CacheConstants.ALL_ACHIEVEMENTS_CACHE_NAME) + @Override + public List findAll() { + return achievementRepo.findAll() + .stream() + .map(achieve -> modelMapper.map(achieve, AchievementVO.class)) + .collect(Collectors.toList()); + } + + /** + * {@inheritDoc} + * + * @author Orest Mamchuk + */ + @Override + public PageableAdvancedDto findAll(Pageable page) { + Page pages = achievementRepo.findAll(page); + List achievementVOS = pages + .stream() + .map(achievement -> modelMapper.map(achievement, AchievementVO.class)) + .collect(Collectors.toList()); + return createPageable(achievementVOS, pages); + } + /** * {@inheritDoc} * From bc53a63c1729176a57bfca80d74b96629492ff1a Mon Sep 17 00:00:00 2001 From: ospodaryk Date: Thu, 12 Oct 2023 14:18:25 +0300 Subject: [PATCH 07/16] Fixed javadoc --- .../main/java/greencity/controller/AchievementController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/greencity/controller/AchievementController.java b/core/src/main/java/greencity/controller/AchievementController.java index 0ca4c6098e..3453a65e56 100644 --- a/core/src/main/java/greencity/controller/AchievementController.java +++ b/core/src/main/java/greencity/controller/AchievementController.java @@ -47,11 +47,11 @@ public ResponseEntity> getAll() { } /** - * Method returns all achievements, available for achieving. + * Method returns all achievements, achieved by user. * * @return list of {@link AchievementDTO} */ - @ApiOperation(value = "Get all achievements.") + @ApiOperation(value = "Get all achievements by user.") @ApiResponses(value = { @ApiResponse(code = 200, message = HttpStatuses.OK), @ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST), From 9147642c29185be1e14c0d4f0df1a50808f8d0f4 Mon Sep 17 00:00:00 2001 From: ospodaryk Date: Thu, 12 Oct 2023 14:22:56 +0300 Subject: [PATCH 08/16] Fixed name od method --- .../main/java/greencity/controller/AchievementController.java | 2 +- .../java/greencity/controller/AchievementControllerTest.java | 2 +- .../src/main/java/greencity/service/AchievementService.java | 2 +- .../src/main/java/greencity/service/AchievementServiceImpl.java | 2 +- .../test/java/greencity/service/AchievementServiceImplTest.java | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/greencity/controller/AchievementController.java b/core/src/main/java/greencity/controller/AchievementController.java index 3453a65e56..337c3b3845 100644 --- a/core/src/main/java/greencity/controller/AchievementController.java +++ b/core/src/main/java/greencity/controller/AchievementController.java @@ -59,6 +59,6 @@ public ResponseEntity> getAll() { }) @GetMapping("/achieved") public ResponseEntity> getAllAchievementsByUserID(@ApiIgnore Principal principal) { - return ResponseEntity.status(HttpStatus.OK).body(achievementService.findAllByUserID(principal.getName())); + return ResponseEntity.status(HttpStatus.OK).body(achievementService.findAllByUserEmail(principal.getName())); } } diff --git a/core/src/test/java/greencity/controller/AchievementControllerTest.java b/core/src/test/java/greencity/controller/AchievementControllerTest.java index d0c16bc27b..111b2cd9a4 100644 --- a/core/src/test/java/greencity/controller/AchievementControllerTest.java +++ b/core/src/test/java/greencity/controller/AchievementControllerTest.java @@ -53,6 +53,6 @@ void findAllTest() throws Exception { @Test void findAllByUserIDTest() throws Exception { mockMvc.perform(get(achievementLink + "/achieved").principal(principal)).andExpect(status().isOk()); - verify(achievementService).findAllByUserID(anyString()); + verify(achievementService).findAllByUserEmail(anyString()); } } diff --git a/service-api/src/main/java/greencity/service/AchievementService.java b/service-api/src/main/java/greencity/service/AchievementService.java index 8e3b45dc4e..2b5993f571 100644 --- a/service-api/src/main/java/greencity/service/AchievementService.java +++ b/service-api/src/main/java/greencity/service/AchievementService.java @@ -104,5 +104,5 @@ void calculateAchievements(Long id, AchievementCategoryType achievementCategory, * @return A list of {@link AchievementVO} representing the achievements of the * user. The list can be empty if the user has no achievements. */ - List findAllByUserID(String principalEmail); + List findAllByUserEmail(String principalEmail); } diff --git a/service/src/main/java/greencity/service/AchievementServiceImpl.java b/service/src/main/java/greencity/service/AchievementServiceImpl.java index daf02eb4b7..6ee2c7070c 100644 --- a/service/src/main/java/greencity/service/AchievementServiceImpl.java +++ b/service/src/main/java/greencity/service/AchievementServiceImpl.java @@ -121,7 +121,7 @@ public PageableAdvancedDto findAll(Pageable page) { * @author Oksana Spodaryk */ @Override - public List findAllByUserID(String principalEmail) { + public List findAllByUserEmail(String principalEmail) { User currentUser = modelMapper.map(userService.findByEmail(principalEmail), User.class); List achievemnetsId = userAchievementRepo.getUserAchievementByUserId(currentUser.getId()) .stream() diff --git a/service/src/test/java/greencity/service/AchievementServiceImplTest.java b/service/src/test/java/greencity/service/AchievementServiceImplTest.java index 2dab984688..b658d8ca90 100644 --- a/service/src/test/java/greencity/service/AchievementServiceImplTest.java +++ b/service/src/test/java/greencity/service/AchievementServiceImplTest.java @@ -110,7 +110,7 @@ void findAllByUserIDTest() { when(modelMapper.map(ModelUtils.getAchievement(), AchievementVO.class)) .thenReturn(ModelUtils.getAchievementVO()); - List findAllResult = achievementService.findAllByUserID("email@gmail.com"); + List findAllResult = achievementService.findAllByUserEmail("email@gmail.com"); assertEquals(findAllResult, Arrays.asList(ModelUtils.getAchievementVO())); verify(userService).findByEmail("email@gmail.com"); From 36ef0448327016e8ef14aba8bdb2a952d7a3ece3 Mon Sep 17 00:00:00 2001 From: ospodaryk Date: Thu, 12 Oct 2023 15:30:38 +0300 Subject: [PATCH 09/16] Separeted by methods --- .../java/greencity/config/SecurityConfig.java | 1 - .../controller/AchievementController.java | 27 ++++--------- .../controller/AchievementControllerTest.java | 10 +---- .../greencity/service/AchievementService.java | 23 +++++------ .../service/AchievementServiceImpl.java | 38 ++++++++++--------- .../service/AchievementServiceImplTest.java | 34 +++++++++++++---- 6 files changed, 67 insertions(+), 66 deletions(-) diff --git a/core/src/main/java/greencity/config/SecurityConfig.java b/core/src/main/java/greencity/config/SecurityConfig.java index 8537675f2a..84db7c579d 100644 --- a/core/src/main/java/greencity/config/SecurityConfig.java +++ b/core/src/main/java/greencity/config/SecurityConfig.java @@ -174,7 +174,6 @@ protected void configure(HttpSecurity http) throws Exception { .permitAll() .antMatchers(HttpMethod.GET, "/achievements", - "/achievements/achieved", "/advices/random/{habitId}", "/advices", CUSTOM_SHOPPING_LIST_ITEMS, diff --git a/core/src/main/java/greencity/controller/AchievementController.java b/core/src/main/java/greencity/controller/AchievementController.java index 337c3b3845..86d135f322 100644 --- a/core/src/main/java/greencity/controller/AchievementController.java +++ b/core/src/main/java/greencity/controller/AchievementController.java @@ -5,6 +5,7 @@ import greencity.dto.achievement.AchievementVO; import greencity.service.AchievementService; import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; @@ -35,30 +36,18 @@ public AchievementController(AchievementService achievementService) { * * @return list of {@link AchievementDTO} */ - @ApiOperation(value = "Get all achievements.") + @ApiOperation(value = "Get all achievements by type.") @ApiResponses(value = { @ApiResponse(code = 200, message = HttpStatuses.OK), @ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST), @ApiResponse(code = 401, message = HttpStatuses.UNAUTHORIZED), }) @GetMapping("") - public ResponseEntity> getAll() { - return ResponseEntity.status(HttpStatus.OK).body(achievementService.findAll()); - } - - /** - * Method returns all achievements, achieved by user. - * - * @return list of {@link AchievementDTO} - */ - @ApiOperation(value = "Get all achievements by user.") - @ApiResponses(value = { - @ApiResponse(code = 200, message = HttpStatuses.OK), - @ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST), - @ApiResponse(code = 401, message = HttpStatuses.UNAUTHORIZED), - }) - @GetMapping("/achieved") - public ResponseEntity> getAllAchievementsByUserID(@ApiIgnore Principal principal) { - return ResponseEntity.status(HttpStatus.OK).body(achievementService.findAllByUserEmail(principal.getName())); + public ResponseEntity> getAll(@ApiIgnore Principal principal, + @ApiParam(value = "Available values : UNACHIEVED, UNACHIEVED." + + " Leave this field empty if you need items with any status") @RequestParam( + required = false) String achievementStatus) { + return ResponseEntity.status(HttpStatus.OK) + .body(achievementService.findAllByType(achievementStatus, principal.getName())); } } diff --git a/core/src/test/java/greencity/controller/AchievementControllerTest.java b/core/src/test/java/greencity/controller/AchievementControllerTest.java index 111b2cd9a4..3648408b2b 100644 --- a/core/src/test/java/greencity/controller/AchievementControllerTest.java +++ b/core/src/test/java/greencity/controller/AchievementControllerTest.java @@ -46,13 +46,7 @@ void setup() { @Test void findAllTest() throws Exception { - mockMvc.perform(get(achievementLink)).andExpect(status().isOk()); - verify(achievementService).findAll(); - } - - @Test - void findAllByUserIDTest() throws Exception { - mockMvc.perform(get(achievementLink + "/achieved").principal(principal)).andExpect(status().isOk()); - verify(achievementService).findAllByUserEmail(anyString()); + mockMvc.perform(get(achievementLink).principal(principal)).andExpect(status().isOk()); + verify(achievementService).findAllByType(null, "test@gmail.com"); } } diff --git a/service-api/src/main/java/greencity/service/AchievementService.java b/service-api/src/main/java/greencity/service/AchievementService.java index 2b5993f571..f6577fad26 100644 --- a/service-api/src/main/java/greencity/service/AchievementService.java +++ b/service-api/src/main/java/greencity/service/AchievementService.java @@ -9,13 +9,6 @@ import java.util.List; public interface AchievementService { - /** - * Method for finding all the achievements. - * - * @return list of all{@link AchievementDTO}. - */ - List findAll(); - /** * Find {@link AchievementVO} for management by page . * @@ -96,13 +89,15 @@ void calculateAchievements(Long id, AchievementCategoryType achievementCategory, AchievementAction achievementAction); /** - * Retrieves a list of achievements associated with a user identified by the - * provided email. + * Retrieves a list of achievements based on the given type and the principal's + * email. * - * @param principalEmail The email address of the user whose achievements are to - * be fetched. - * @return A list of {@link AchievementVO} representing the achievements of the - * user. The list can be empty if the user has no achievements. + * @param principalEmail The email of the principal (usually the logged-in + * user) for whom the achievements need to be fetched. + * @param achievementStatus The status of the achievements to filter by (e.g., + * "ACHIEVED", "UNACHIEVED"). + * @return List AchievementVO Returns a list of achievements matching the given + * criteria. */ - List findAllByUserEmail(String principalEmail); + List findAllByType(String principalEmail, String achievementStatus); } diff --git a/service/src/main/java/greencity/service/AchievementServiceImpl.java b/service/src/main/java/greencity/service/AchievementServiceImpl.java index 6ee2c7070c..258e331861 100644 --- a/service/src/main/java/greencity/service/AchievementServiceImpl.java +++ b/service/src/main/java/greencity/service/AchievementServiceImpl.java @@ -30,7 +30,6 @@ import greencity.repository.UserAchievementRepo; import lombok.AllArgsConstructor; import org.modelmapper.ModelMapper; -import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.EnableCaching; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.data.domain.Page; @@ -86,20 +85,6 @@ public AchievementVO save(AchievementPostDto achievementPostDto) { return achievementVO; } - /** - * {@inheritDoc} - * - * @author Yuriy Olkhovskyi - */ - @Cacheable(value = CacheConstants.ALL_ACHIEVEMENTS_CACHE_NAME) - @Override - public List findAll() { - return achievementRepo.findAll() - .stream() - .map(achieve -> modelMapper.map(achieve, AchievementVO.class)) - .collect(Collectors.toList()); - } - /** * {@inheritDoc} * @@ -115,15 +100,34 @@ public PageableAdvancedDto findAll(Pageable page) { return createPageable(achievementVOS, pages); } + private List findAll() { + return achievementRepo.findAll() + .stream() + .map(achieve -> modelMapper.map(achieve, AchievementVO.class)) + .collect(Collectors.toList()); + } + /** * {@inheritDoc} * * @author Oksana Spodaryk */ @Override - public List findAllByUserEmail(String principalEmail) { + public List findAllByType(String principalEmail, String achievementStatus) { User currentUser = modelMapper.map(userService.findByEmail(principalEmail), User.class); - List achievemnetsId = userAchievementRepo.getUserAchievementByUserId(currentUser.getId()) + Long userId = currentUser.getId(); + if (achievementStatus.equals("ACHIEVED")) { + return findAllAchieved(userId); + } else if (achievementStatus.equals("UNACHIEVED")) { + List unAchievedList = findAll(); + unAchievedList.removeAll(findAllAchieved(userId)); + return unAchievedList; + } + return findAll(); + } + + private List findAllAchieved(Long userId) { + List achievemnetsId = userAchievementRepo.getUserAchievementByUserId(userId) .stream() .map(userAchievement -> userAchievement.getAchievement().getId()) .collect(Collectors.toList()); diff --git a/service/src/test/java/greencity/service/AchievementServiceImplTest.java b/service/src/test/java/greencity/service/AchievementServiceImplTest.java index b658d8ca90..ac771395af 100644 --- a/service/src/test/java/greencity/service/AchievementServiceImplTest.java +++ b/service/src/test/java/greencity/service/AchievementServiceImplTest.java @@ -72,19 +72,24 @@ class AchievementServiceImplTest { @Test void findAllWithEmptyListTest() { + when(userService.findByEmail("email@gmail.com")).thenReturn(ModelUtils.getUserVO()); + when(modelMapper.map(ModelUtils.getUserVO(), User.class)).thenReturn(ModelUtils.getUser()); when(achievementRepo.findAll()).thenReturn(Collections.emptyList()); - List findAllResult = achievementService.findAll(); + List findAllResult = achievementService.findAllByType("email@gmail.com", ""); assertTrue(findAllResult.isEmpty()); } @Test void findAllWithOneValueInRepoTest() { Achievement achievement = ModelUtils.getAchievement(); + when(userService.findByEmail("email@gmail.com")).thenReturn(ModelUtils.getUserVO()); + when(modelMapper.map(ModelUtils.getUserVO(), User.class)).thenReturn(ModelUtils.getUser()); when(achievementRepo.findAll()) .thenReturn(Collections.singletonList(achievement)); when(modelMapper.map(achievement, AchievementVO.class)) .thenReturn(ModelUtils.getAchievementVO()); - List findAllResult = achievementService.findAll(); + when(userService.findByEmail("email@gmail.com")).thenReturn(ModelUtils.getUserVO()); + List findAllResult = achievementService.findAllByType("email@gmail.com", ""); assertEquals(1L, (long) findAllResult.get(0).getId()); } @@ -101,7 +106,7 @@ void findAllByPageableTest() { } @Test - void findAllByUserIDTest() { + void findAllACHIEVEDInRepoTest() { when(userService.findByEmail("email@gmail.com")).thenReturn(ModelUtils.getUserVO()); when(modelMapper.map(ModelUtils.getUserVO(), User.class)).thenReturn(ModelUtils.getUser()); when(userAchievementRepo.getUserAchievementByUserId(anyLong())) @@ -109,15 +114,30 @@ void findAllByUserIDTest() { when(achievementRepo.findById(anyLong())).thenReturn(Optional.of(ModelUtils.getAchievement())); when(modelMapper.map(ModelUtils.getAchievement(), AchievementVO.class)) .thenReturn(ModelUtils.getAchievementVO()); - - List findAllResult = achievementService.findAllByUserEmail("email@gmail.com"); - assertEquals(findAllResult, Arrays.asList(ModelUtils.getAchievementVO())); - + List findAllResult = achievementService.findAllByType("email@gmail.com", "ACHIEVED"); + assertEquals(1L, (long) findAllResult.get(0).getId()); verify(userService).findByEmail("email@gmail.com"); verify(modelMapper).map(ModelUtils.getUserVO(), User.class); verify(userAchievementRepo).getUserAchievementByUserId(anyLong()); verify(modelMapper).map(ModelUtils.getAchievement(), AchievementVO.class); + } + @Test + void findAllUNACHIEVEDInRepoTest() { + when(userService.findByEmail("email@gmail.com")).thenReturn(ModelUtils.getUserVO()); + when(achievementRepo.findAll()).thenReturn(Arrays.asList(ModelUtils.getAchievement())); + when(modelMapper.map(ModelUtils.getUserVO(), User.class)).thenReturn(ModelUtils.getUser()); + when(userAchievementRepo.getUserAchievementByUserId(anyLong())) + .thenReturn(Arrays.asList(ModelUtils.getUserAchievement())); + when(achievementRepo.findById(anyLong())).thenReturn(Optional.of(ModelUtils.getAchievement())); + when(modelMapper.map(ModelUtils.getAchievement(), AchievementVO.class)) + .thenReturn(ModelUtils.getAchievementVO()); + List findAllResult = achievementService.findAllByType("email@gmail.com", "UNACHIEVED"); + assertTrue(findAllResult.isEmpty()); + verify(userService).findByEmail("email@gmail.com"); + verify(modelMapper).map(ModelUtils.getUserVO(), User.class); + verify(userAchievementRepo).getUserAchievementByUserId(anyLong()); + verify(modelMapper, times(2)).map(ModelUtils.getAchievement(), AchievementVO.class); } @Test From 321d0e1dbe7705723352b6fd676199a3b1ed1062 Mon Sep 17 00:00:00 2001 From: ospodaryk Date: Thu, 12 Oct 2023 15:46:32 +0300 Subject: [PATCH 10/16] Fixed bug --- .../controller/AchievementController.java | 4 ++-- .../greencity/service/AchievementServiceImpl.java | 14 ++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/greencity/controller/AchievementController.java b/core/src/main/java/greencity/controller/AchievementController.java index 86d135f322..be51725f1f 100644 --- a/core/src/main/java/greencity/controller/AchievementController.java +++ b/core/src/main/java/greencity/controller/AchievementController.java @@ -44,10 +44,10 @@ public AchievementController(AchievementService achievementService) { }) @GetMapping("") public ResponseEntity> getAll(@ApiIgnore Principal principal, - @ApiParam(value = "Available values : UNACHIEVED, UNACHIEVED." + @ApiParam(value = "Available values : ACHIEVED, UNACHIEVED." + " Leave this field empty if you need items with any status") @RequestParam( required = false) String achievementStatus) { return ResponseEntity.status(HttpStatus.OK) - .body(achievementService.findAllByType(achievementStatus, principal.getName())); + .body(achievementService.findAllByType(principal.getName(), achievementStatus)); } } diff --git a/service/src/main/java/greencity/service/AchievementServiceImpl.java b/service/src/main/java/greencity/service/AchievementServiceImpl.java index 258e331861..778288b130 100644 --- a/service/src/main/java/greencity/service/AchievementServiceImpl.java +++ b/service/src/main/java/greencity/service/AchievementServiceImpl.java @@ -116,12 +116,14 @@ private List findAll() { public List findAllByType(String principalEmail, String achievementStatus) { User currentUser = modelMapper.map(userService.findByEmail(principalEmail), User.class); Long userId = currentUser.getId(); - if (achievementStatus.equals("ACHIEVED")) { - return findAllAchieved(userId); - } else if (achievementStatus.equals("UNACHIEVED")) { - List unAchievedList = findAll(); - unAchievedList.removeAll(findAllAchieved(userId)); - return unAchievedList; + if (achievementStatus != null) { + if (achievementStatus.equals("ACHIEVED")) { + return findAllAchieved(userId); + } else if (achievementStatus.equals("UNACHIEVED")) { + List unAchievedList = findAll(); + unAchievedList.removeAll(findAllAchieved(userId)); + return unAchievedList; + } } return findAll(); } From 61816bf113d2baf47b658cc9d4b763cf120467e1 Mon Sep 17 00:00:00 2001 From: ospodaryk Date: Thu, 12 Oct 2023 15:49:07 +0300 Subject: [PATCH 11/16] removed codesmell --- .../src/main/java/greencity/service/AchievementServiceImpl.java | 1 - 1 file changed, 1 deletion(-) diff --git a/service/src/main/java/greencity/service/AchievementServiceImpl.java b/service/src/main/java/greencity/service/AchievementServiceImpl.java index 778288b130..b0a974c20a 100644 --- a/service/src/main/java/greencity/service/AchievementServiceImpl.java +++ b/service/src/main/java/greencity/service/AchievementServiceImpl.java @@ -2,7 +2,6 @@ import greencity.achievement.AchievementCalculation; import greencity.client.RestClient; -import greencity.constant.CacheConstants; import greencity.constant.ErrorMessage; import greencity.dto.PageableAdvancedDto; import greencity.dto.achievement.AchievementManagementDto; From 2042f440c16fd50661ad71f67b70c086ebaecacd Mon Sep 17 00:00:00 2001 From: ospodaryk Date: Thu, 12 Oct 2023 16:04:41 +0300 Subject: [PATCH 12/16] Added method to get UnAchievedAchievements --- .../java/greencity/repository/AchievementRepo.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/dao/src/main/java/greencity/repository/AchievementRepo.java b/dao/src/main/java/greencity/repository/AchievementRepo.java index ef1c027eb4..9fa0d7bdff 100644 --- a/dao/src/main/java/greencity/repository/AchievementRepo.java +++ b/dao/src/main/java/greencity/repository/AchievementRepo.java @@ -7,6 +7,7 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import java.util.List; import java.util.Optional; @Repository @@ -35,4 +36,17 @@ public interface AchievementRepo extends JpaRepository { * @author Orest Mamchuk */ Optional findByAchievementCategoryIdAndCondition(Long achievementCategoryId, Integer condition); + + /** + * Searches for achievements that have not yet been achieved by the specified + * user. + * + * @param userId The ID of the user for whom to find unachieved achievements. + * @return A list of achievements that the user has not yet achieved. + */ + @Query(value = "SELECT * from achievements " + + "where id not in (select achievement_id " + + " from user_achievements " + + " where user_id = :userId)", nativeQuery = true) + List searchAchievementsUnAchieved(Long userId); } From 03f1527704cbedfa0daf16eb386fe30dfa760e74 Mon Sep 17 00:00:00 2001 From: ospodaryk Date: Thu, 12 Oct 2023 16:04:52 +0300 Subject: [PATCH 13/16] Fixe --- .../service/AchievementServiceImpl.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/service/src/main/java/greencity/service/AchievementServiceImpl.java b/service/src/main/java/greencity/service/AchievementServiceImpl.java index b0a974c20a..c92476cf85 100644 --- a/service/src/main/java/greencity/service/AchievementServiceImpl.java +++ b/service/src/main/java/greencity/service/AchievementServiceImpl.java @@ -48,6 +48,8 @@ public class AchievementServiceImpl implements AchievementService { private AchievementCalculation achievementCalculation; private UserService userService; private final UserAchievementRepo userAchievementRepo; + private static final String ACHIEVED = "ACHIEVED"; + private static final String UNACHIEVED = "UNACHIEVED"; /** * {@inheritDoc} @@ -115,14 +117,12 @@ private List findAll() { public List findAllByType(String principalEmail, String achievementStatus) { User currentUser = modelMapper.map(userService.findByEmail(principalEmail), User.class); Long userId = currentUser.getId(); - if (achievementStatus != null) { - if (achievementStatus.equals("ACHIEVED")) { - return findAllAchieved(userId); - } else if (achievementStatus.equals("UNACHIEVED")) { - List unAchievedList = findAll(); - unAchievedList.removeAll(findAllAchieved(userId)); - return unAchievedList; - } + if (ACHIEVED.equals(achievementStatus)) { + return findAllAchieved(userId); + } else if (UNACHIEVED.equals(achievementStatus)) { + return achievementRepo.searchAchievementsUnAchieved(userId).stream() + .map(achieve -> modelMapper.map(achieve, AchievementVO.class)) + .collect(Collectors.toList()); } return findAll(); } From 80658db15f513953684f161f64a834172bd1cef4 Mon Sep 17 00:00:00 2001 From: ospodaryk Date: Thu, 12 Oct 2023 16:13:13 +0300 Subject: [PATCH 14/16] Added tests --- .../controller/AchievementControllerTest.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/core/src/test/java/greencity/controller/AchievementControllerTest.java b/core/src/test/java/greencity/controller/AchievementControllerTest.java index 3648408b2b..4afde62828 100644 --- a/core/src/test/java/greencity/controller/AchievementControllerTest.java +++ b/core/src/test/java/greencity/controller/AchievementControllerTest.java @@ -47,6 +47,20 @@ void setup() { @Test void findAllTest() throws Exception { mockMvc.perform(get(achievementLink).principal(principal)).andExpect(status().isOk()); - verify(achievementService).findAllByType(null, "test@gmail.com"); + verify(achievementService).findAllByType("test@gmail.com", null); + } + + @Test + void findAllAchievedTest() throws Exception { + mockMvc.perform(get(achievementLink).principal(principal).param("achievementStatus", "ACHIEVED")) + .andExpect(status().isOk()); + verify(achievementService).findAllByType("test@gmail.com", "ACHIEVED"); + } + + @Test + void findAllUnAchievedTest() throws Exception { + mockMvc.perform(get(achievementLink).principal(principal).param("achievementStatus", "UNACHIEVED")) + .andExpect(status().isOk()); + verify(achievementService).findAllByType("test@gmail.com", "UNACHIEVED"); } } From e9118755718c51d56c2f1a8b1592a33c5dce52f4 Mon Sep 17 00:00:00 2001 From: ospodaryk Date: Thu, 12 Oct 2023 16:18:06 +0300 Subject: [PATCH 15/16] Added ignorecase and dded tests --- .../controller/AchievementControllerTest.java | 14 ++++++++++++++ .../greencity/service/AchievementServiceImpl.java | 4 ++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/core/src/test/java/greencity/controller/AchievementControllerTest.java b/core/src/test/java/greencity/controller/AchievementControllerTest.java index 4afde62828..6a662dd4dc 100644 --- a/core/src/test/java/greencity/controller/AchievementControllerTest.java +++ b/core/src/test/java/greencity/controller/AchievementControllerTest.java @@ -63,4 +63,18 @@ void findAllUnAchievedTest() throws Exception { .andExpect(status().isOk()); verify(achievementService).findAllByType("test@gmail.com", "UNACHIEVED"); } + + @Test + void findAllAchievedIgnoreCaseTest() throws Exception { + mockMvc.perform(get(achievementLink).principal(principal).param("achievementStatus", "AchieVED")) + .andExpect(status().isOk()); + verify(achievementService).findAllByType("test@gmail.com", "AchieVED"); + } + + @Test + void findAllUnAchievedIgnoreCaseTest() throws Exception { + mockMvc.perform(get(achievementLink).principal(principal).param("achievementStatus", "unAchieVED")) + .andExpect(status().isOk()); + verify(achievementService).findAllByType("test@gmail.com", "unAchieVED"); + } } diff --git a/service/src/main/java/greencity/service/AchievementServiceImpl.java b/service/src/main/java/greencity/service/AchievementServiceImpl.java index c92476cf85..b174cec743 100644 --- a/service/src/main/java/greencity/service/AchievementServiceImpl.java +++ b/service/src/main/java/greencity/service/AchievementServiceImpl.java @@ -117,9 +117,9 @@ private List findAll() { public List findAllByType(String principalEmail, String achievementStatus) { User currentUser = modelMapper.map(userService.findByEmail(principalEmail), User.class); Long userId = currentUser.getId(); - if (ACHIEVED.equals(achievementStatus)) { + if (ACHIEVED.equalsIgnoreCase(achievementStatus)) { return findAllAchieved(userId); - } else if (UNACHIEVED.equals(achievementStatus)) { + } else if (UNACHIEVED.equalsIgnoreCase(achievementStatus)) { return achievementRepo.searchAchievementsUnAchieved(userId).stream() .map(achieve -> modelMapper.map(achieve, AchievementVO.class)) .collect(Collectors.toList()); From 193df330425a997f18e1c4a24faa19a190a74457 Mon Sep 17 00:00:00 2001 From: ospodaryk Date: Thu, 12 Oct 2023 16:30:49 +0300 Subject: [PATCH 16/16] Fixed tests --- .../greencity/service/AchievementServiceImplTest.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/service/src/test/java/greencity/service/AchievementServiceImplTest.java b/service/src/test/java/greencity/service/AchievementServiceImplTest.java index ac771395af..21f2b71dc1 100644 --- a/service/src/test/java/greencity/service/AchievementServiceImplTest.java +++ b/service/src/test/java/greencity/service/AchievementServiceImplTest.java @@ -125,19 +125,16 @@ void findAllACHIEVEDInRepoTest() { @Test void findAllUNACHIEVEDInRepoTest() { when(userService.findByEmail("email@gmail.com")).thenReturn(ModelUtils.getUserVO()); - when(achievementRepo.findAll()).thenReturn(Arrays.asList(ModelUtils.getAchievement())); when(modelMapper.map(ModelUtils.getUserVO(), User.class)).thenReturn(ModelUtils.getUser()); - when(userAchievementRepo.getUserAchievementByUserId(anyLong())) - .thenReturn(Arrays.asList(ModelUtils.getUserAchievement())); - when(achievementRepo.findById(anyLong())).thenReturn(Optional.of(ModelUtils.getAchievement())); + when(achievementRepo.searchAchievementsUnAchieved(anyLong())) + .thenReturn(Arrays.asList(ModelUtils.getAchievement())); when(modelMapper.map(ModelUtils.getAchievement(), AchievementVO.class)) .thenReturn(ModelUtils.getAchievementVO()); List findAllResult = achievementService.findAllByType("email@gmail.com", "UNACHIEVED"); - assertTrue(findAllResult.isEmpty()); + assertEquals(1L, (long) findAllResult.get(0).getId()); verify(userService).findByEmail("email@gmail.com"); verify(modelMapper).map(ModelUtils.getUserVO(), User.class); - verify(userAchievementRepo).getUserAchievementByUserId(anyLong()); - verify(modelMapper, times(2)).map(ModelUtils.getAchievement(), AchievementVO.class); + verify(achievementRepo).searchAchievementsUnAchieved(anyLong()); } @Test