@Test public void getUserGoalsTest() { UserGoal customUserGoal = ModelUtils.getCustomUserGoal(); UserGoal predefinedUserGoal = ModelUtils.getPredefinedUserGoal(); List<UserGoal> userGoals = Arrays.asList(customUserGoal, predefinedUserGoal); UserGoalResponseDto customDtoWithoutText = UserGoalResponseDto.builder().id(1L).status(GoalStatus.ACTIVE).build(); UserGoalResponseDto predefinedDtoWithoutText = UserGoalResponseDto.builder().id(2L).status(GoalStatus.ACTIVE).build(); UserGoalResponseDto customUserGoalDto = ModelUtils.getCustomUserGoalDto(); UserGoalResponseDto predefinedUserGoalDto = ModelUtils.getPredefinedUserGoalDto(); List<UserGoalResponseDto> userGoalDtos = Arrays.asList(customUserGoalDto, predefinedUserGoalDto); List<GoalTranslation> goalTranslations = ModelUtils.getGoalTranslations(); CustomGoal customGoal = CustomGoal.builder().id(8L).text("Buy electric car").build(); when(userGoalRepo.findAllByUserId(user.getId())).thenReturn(userGoals); when(modelMapper.map(userGoals.get(0), UserGoalResponseDto.class)).thenReturn(customDtoWithoutText); when(modelMapper.map(userGoals.get(1), UserGoalResponseDto.class)).thenReturn(predefinedDtoWithoutText); when(userGoalRepo.findGoalByUserGoalId(userGoals.get(0).getId())).thenReturn(Optional.empty()); when(userGoalRepo.findGoalByUserGoalId(userGoals.get(1).getId())) .thenReturn(Optional.of(predefinedUserGoal.getGoal())); when(goalTranslationRepo.findByUserIdLangAndUserGoalId(anyLong(), anyString(), anyLong())) .thenReturn(goalTranslations.get(0)); when(customGoalRepo.findByUserGoalIdAndUserId(anyLong(), anyLong())).thenReturn(customGoal); assertEquals(userService.getUserGoals(user.getId(), "en"), userGoalDtos); }
@Transactional @Override public List<UserGoalResponseDto> getUserGoals(Long userId, String language) { List<UserGoalResponseDto> userGoalResponseDtos = userGoalRepo .findAllByUserId(userId) .stream() .map(userGoal -> modelMapper.map(userGoal, UserGoalResponseDto.class)) .collect(Collectors.toList()); if (userGoalResponseDtos.isEmpty()) { throw new UserHasNoGoalsException(USER_HAS_NO_GOALS); } userGoalResponseDtos.stream().forEach(el -> setTextForAnyUserGoal(el, userId, language)); return userGoalResponseDtos; }
UserServiceImpl implements UserService { @Transactional @Override public List<UserGoalResponseDto> getUserGoals(Long userId, String language) { List<UserGoalResponseDto> userGoalResponseDtos = userGoalRepo .findAllByUserId(userId) .stream() .map(userGoal -> modelMapper.map(userGoal, UserGoalResponseDto.class)) .collect(Collectors.toList()); if (userGoalResponseDtos.isEmpty()) { throw new UserHasNoGoalsException(USER_HAS_NO_GOALS); } userGoalResponseDtos.stream().forEach(el -> setTextForAnyUserGoal(el, userId, language)); return userGoalResponseDtos; } }
UserServiceImpl implements UserService { @Transactional @Override public List<UserGoalResponseDto> getUserGoals(Long userId, String language) { List<UserGoalResponseDto> userGoalResponseDtos = userGoalRepo .findAllByUserId(userId) .stream() .map(userGoal -> modelMapper.map(userGoal, UserGoalResponseDto.class)) .collect(Collectors.toList()); if (userGoalResponseDtos.isEmpty()) { throw new UserHasNoGoalsException(USER_HAS_NO_GOALS); } userGoalResponseDtos.stream().forEach(el -> setTextForAnyUserGoal(el, userId, language)); return userGoalResponseDtos; }  }
UserServiceImpl implements UserService { @Transactional @Override public List<UserGoalResponseDto> getUserGoals(Long userId, String language) { List<UserGoalResponseDto> userGoalResponseDtos = userGoalRepo .findAllByUserId(userId) .stream() .map(userGoal -> modelMapper.map(userGoal, UserGoalResponseDto.class)) .collect(Collectors.toList()); if (userGoalResponseDtos.isEmpty()) { throw new UserHasNoGoalsException(USER_HAS_NO_GOALS); } userGoalResponseDtos.stream().forEach(el -> setTextForAnyUserGoal(el, userId, language)); return userGoalResponseDtos; }  @Override User save(User user); @Override User findById(Long id); @Override PageableDto<UserForListDto> findByPage(Pageable pageable); @Override PageableDto<UserManagementDto> findUserForManagementByPage(Pageable pageable); @Override void updateUser(UserManagementDto dto); @Override void deleteById(Long id); @Override User findByEmail(String email); @Override Long findIdByEmail(String email); @Override UserRoleDto updateRole(Long id, ROLE role, String email); @Override UserStatusDto updateStatus(Long id, UserStatus userStatus, String email); @Override RoleDto getRoles(); @Override List<EmailNotification> getEmailNotificationsStatuses(); @Override User updateLastVisit(User user); PageableDto<UserForListDto> getUsersByFilter(FilterUserDto filterUserDto, Pageable pageable); @Override UserUpdateDto getUserUpdateDtoByEmail(String email); @Override User update(UserUpdateDto dto, String email); @Transactional @Override List<UserGoalResponseDto> getUserGoals(Long userId, String language); @Transactional @Override List<GoalDto> getAvailableGoals(Long userId, String language); @Transactional @Override List<UserGoalResponseDto> saveUserGoals(Long userId, BulkSaveUserGoalDto bulkDto, String language); @Transactional @Override List<Long> deleteUserGoals(String ids); @Transactional @Override UserGoalResponseDto updateUserGoalStatus(Long userId, Long goalId, String language); @Override int updateUserRefreshToken(String refreshTokenKey, Long id); @Transactional @Override List<greencity.dto.user.HabitDictionaryDto> getAvailableHabitDictionary(Long userId, String language); @Override List<HabitCreateDto> createUserHabit(Long userId, List<HabitIdDto> habitIdDto, String language); @Transactional @Override List<CustomGoalResponseDto> getAvailableCustomGoals(Long userId); @Override long getActivatedUsersAmount(); @Transactional @Override void deleteHabitByUserIdAndHabitDictionary(Long userId, Long habitId); @Override void addDefaultHabit(Long userId, String language); @Override String getProfilePicturePathByUserId(Long id); @Override User updateUserProfilePicture(MultipartFile image, String email); @Override List<User> getAllUserFriends(Long userId); @Override @Transactional void deleteUserFriendById(Long userId, Long friendId); @Override @Transactional void addNewFriend(Long userId, Long friendId); @Override List<UserProfilePictureDto> getSixFriendsWithTheHighestRating(Long userId); @Override UserProfileDtoResponse saveUserProfile(UserProfileDtoRequest userProfileDtoRequest, MultipartFile image,
                                                  String email); @Override UserProfileDtoResponse getUserProfileInformation(Long userId); @Override void updateUserLastActivityTime(Long userId, Date userLastActivityTime); @Override boolean checkIfTheUserIsOnline(Long userId); @Override UserProfileStatisticsDto getUserProfileStatistics(Long userId); @Override UserAndFriendsWithOnlineStatusDto getUserAndSixFriendsWithOnlineStatus(Long userId); @Override UserAndAllFriendsWithOnlineStatusDto getAllFriendsWithTheOnlineStatus(Long userId, Pageable pageable); @Override void deactivateUser(Long id); }
UserServiceImpl implements UserService { @Transactional @Override public List<UserGoalResponseDto> getUserGoals(Long userId, String language) { List<UserGoalResponseDto> userGoalResponseDtos = userGoalRepo .findAllByUserId(userId) .stream() .map(userGoal -> modelMapper.map(userGoal, UserGoalResponseDto.class)) .collect(Collectors.toList()); if (userGoalResponseDtos.isEmpty()) { throw new UserHasNoGoalsException(USER_HAS_NO_GOALS); } userGoalResponseDtos.stream().forEach(el -> setTextForAnyUserGoal(el, userId, language)); return userGoalResponseDtos; }  @Override User save(User user); @Override User findById(Long id); @Override PageableDto<UserForListDto> findByPage(Pageable pageable); @Override PageableDto<UserManagementDto> findUserForManagementByPage(Pageable pageable); @Override void updateUser(UserManagementDto dto); @Override void deleteById(Long id); @Override User findByEmail(String email); @Override Long findIdByEmail(String email); @Override UserRoleDto updateRole(Long id, ROLE role, String email); @Override UserStatusDto updateStatus(Long id, UserStatus userStatus, String email); @Override RoleDto getRoles(); @Override List<EmailNotification> getEmailNotificationsStatuses(); @Override User updateLastVisit(User user); PageableDto<UserForListDto> getUsersByFilter(FilterUserDto filterUserDto, Pageable pageable); @Override UserUpdateDto getUserUpdateDtoByEmail(String email); @Override User update(UserUpdateDto dto, String email); @Transactional @Override List<UserGoalResponseDto> getUserGoals(Long userId, String language); @Transactional @Override List<GoalDto> getAvailableGoals(Long userId, String language); @Transactional @Override List<UserGoalResponseDto> saveUserGoals(Long userId, BulkSaveUserGoalDto bulkDto, String language); @Transactional @Override List<Long> deleteUserGoals(String ids); @Transactional @Override UserGoalResponseDto updateUserGoalStatus(Long userId, Long goalId, String language); @Override int updateUserRefreshToken(String refreshTokenKey, Long id); @Transactional @Override List<greencity.dto.user.HabitDictionaryDto> getAvailableHabitDictionary(Long userId, String language); @Override List<HabitCreateDto> createUserHabit(Long userId, List<HabitIdDto> habitIdDto, String language); @Transactional @Override List<CustomGoalResponseDto> getAvailableCustomGoals(Long userId); @Override long getActivatedUsersAmount(); @Transactional @Override void deleteHabitByUserIdAndHabitDictionary(Long userId, Long habitId); @Override void addDefaultHabit(Long userId, String language); @Override String getProfilePicturePathByUserId(Long id); @Override User updateUserProfilePicture(MultipartFile image, String email); @Override List<User> getAllUserFriends(Long userId); @Override @Transactional void deleteUserFriendById(Long userId, Long friendId); @Override @Transactional void addNewFriend(Long userId, Long friendId); @Override List<UserProfilePictureDto> getSixFriendsWithTheHighestRating(Long userId); @Override UserProfileDtoResponse saveUserProfile(UserProfileDtoRequest userProfileDtoRequest, MultipartFile image,
                                                  String email); @Override UserProfileDtoResponse getUserProfileInformation(Long userId); @Override void updateUserLastActivityTime(Long userId, Date userLastActivityTime); @Override boolean checkIfTheUserIsOnline(Long userId); @Override UserProfileStatisticsDto getUserProfileStatistics(Long userId); @Override UserAndFriendsWithOnlineStatusDto getUserAndSixFriendsWithOnlineStatus(Long userId); @Override UserAndAllFriendsWithOnlineStatusDto getAllFriendsWithTheOnlineStatus(Long userId, Pageable pageable); @Override void deactivateUser(Long id);  }