@Test void saveFailedTest() { String email = ModelUtils.getUser().getEmail(); when(modelMapper.map(tipsAndTricksDtoRequest, TipsAndTricks.class)).thenReturn(tipsAndTricks); when(userService.findByEmail(TestConst.EMAIL)).thenReturn(ModelUtils.getUser()); when(tagService.findTipsAndTricksTagsByNames(anyList())) .thenReturn(Collections.singletonList(tipsAndTricksTag)); when(tipsAndTricksRepo.save(tipsAndTricks)).thenThrow(DataIntegrityViolationException.class); assertThrows(NotSavedException.class, () -> tipsAndTricksService.save(tipsAndTricksDtoRequest, null, email)); }
@CacheEvict(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME, allEntries = true) @Override public TipsAndTricksDtoResponse save(TipsAndTricksDtoRequest tipsAndTricksDtoRequest, MultipartFile image, String email) { TipsAndTricks toSave = modelMapper.map(tipsAndTricksDtoRequest, TipsAndTricks.class); toSave.setAuthor(userService.findByEmail(email)); if (tipsAndTricksDtoRequest.getImage() != null && !tipsAndTricksDtoRequest.getImage().isEmpty()) { image = modelMapper.map(tipsAndTricksDtoRequest.getImage(), MultipartFile.class); } if (image != null) { toSave.setImagePath(fileService.upload(image).toString()); } toSave.setTags( tagService.findTipsAndTricksTagsByNames(tipsAndTricksDtoRequest.getTags())); try { tipsAndTricksRepo.save(toSave); } catch (DataIntegrityViolationException e) { throw new NotSavedException(ErrorMessage.TIPS_AND_TRICKS_NOT_SAVED); } return modelMapper.map(toSave, TipsAndTricksDtoResponse.class); }
TipsAndTricksServiceImpl implements TipsAndTricksService { @CacheEvict(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME, allEntries = true) @Override public TipsAndTricksDtoResponse save(TipsAndTricksDtoRequest tipsAndTricksDtoRequest, MultipartFile image, String email) { TipsAndTricks toSave = modelMapper.map(tipsAndTricksDtoRequest, TipsAndTricks.class); toSave.setAuthor(userService.findByEmail(email)); if (tipsAndTricksDtoRequest.getImage() != null && !tipsAndTricksDtoRequest.getImage().isEmpty()) { image = modelMapper.map(tipsAndTricksDtoRequest.getImage(), MultipartFile.class); } if (image != null) { toSave.setImagePath(fileService.upload(image).toString()); } toSave.setTags( tagService.findTipsAndTricksTagsByNames(tipsAndTricksDtoRequest.getTags())); try { tipsAndTricksRepo.save(toSave); } catch (DataIntegrityViolationException e) { throw new NotSavedException(ErrorMessage.TIPS_AND_TRICKS_NOT_SAVED); } return modelMapper.map(toSave, TipsAndTricksDtoResponse.class); } }
TipsAndTricksServiceImpl implements TipsAndTricksService { @CacheEvict(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME, allEntries = true) @Override public TipsAndTricksDtoResponse save(TipsAndTricksDtoRequest tipsAndTricksDtoRequest, MultipartFile image, String email) { TipsAndTricks toSave = modelMapper.map(tipsAndTricksDtoRequest, TipsAndTricks.class); toSave.setAuthor(userService.findByEmail(email)); if (tipsAndTricksDtoRequest.getImage() != null && !tipsAndTricksDtoRequest.getImage().isEmpty()) { image = modelMapper.map(tipsAndTricksDtoRequest.getImage(), MultipartFile.class); } if (image != null) { toSave.setImagePath(fileService.upload(image).toString()); } toSave.setTags( tagService.findTipsAndTricksTagsByNames(tipsAndTricksDtoRequest.getTags())); try { tipsAndTricksRepo.save(toSave); } catch (DataIntegrityViolationException e) { throw new NotSavedException(ErrorMessage.TIPS_AND_TRICKS_NOT_SAVED); } return modelMapper.map(toSave, TipsAndTricksDtoResponse.class); }  }
TipsAndTricksServiceImpl implements TipsAndTricksService { @CacheEvict(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME, allEntries = true) @Override public TipsAndTricksDtoResponse save(TipsAndTricksDtoRequest tipsAndTricksDtoRequest, MultipartFile image, String email) { TipsAndTricks toSave = modelMapper.map(tipsAndTricksDtoRequest, TipsAndTricks.class); toSave.setAuthor(userService.findByEmail(email)); if (tipsAndTricksDtoRequest.getImage() != null && !tipsAndTricksDtoRequest.getImage().isEmpty()) { image = modelMapper.map(tipsAndTricksDtoRequest.getImage(), MultipartFile.class); } if (image != null) { toSave.setImagePath(fileService.upload(image).toString()); } toSave.setTags( tagService.findTipsAndTricksTagsByNames(tipsAndTricksDtoRequest.getTags())); try { tipsAndTricksRepo.save(toSave); } catch (DataIntegrityViolationException e) { throw new NotSavedException(ErrorMessage.TIPS_AND_TRICKS_NOT_SAVED); } return modelMapper.map(toSave, TipsAndTricksDtoResponse.class); }  @CacheEvict(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME, allEntries = true) @Override TipsAndTricksDtoResponse save(TipsAndTricksDtoRequest tipsAndTricksDtoRequest, MultipartFile image,
                                         String email); @CacheEvict(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME, allEntries = true) @Override void update(TipsAndTricksDtoManagement tipsAndTricksDtoManagement,
                       MultipartFile image); @Cacheable(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME) @Override PageableDto<TipsAndTricksDtoResponse> findAll(Pageable page); @Override PageableDto<TipsAndTricksDtoResponse> find(Pageable page, List<String> tags); @Override TipsAndTricksDtoResponse findDtoById(Long id); @CacheEvict(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME, allEntries = true) @Override void delete(Long id); @CacheEvict(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME, allEntries = true) @Override void deleteAll(List<Long> listId); TipsAndTricks findById(Long id); @Override TipsAndTricksDtoManagement findManagementDtoById(Long id); @Override PageableDto<SearchTipsAndTricksDto> search(String searchQuery); @Override Long getAmountOfWrittenTipsAndTrickByUserId(Long id); }
TipsAndTricksServiceImpl implements TipsAndTricksService { @CacheEvict(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME, allEntries = true) @Override public TipsAndTricksDtoResponse save(TipsAndTricksDtoRequest tipsAndTricksDtoRequest, MultipartFile image, String email) { TipsAndTricks toSave = modelMapper.map(tipsAndTricksDtoRequest, TipsAndTricks.class); toSave.setAuthor(userService.findByEmail(email)); if (tipsAndTricksDtoRequest.getImage() != null && !tipsAndTricksDtoRequest.getImage().isEmpty()) { image = modelMapper.map(tipsAndTricksDtoRequest.getImage(), MultipartFile.class); } if (image != null) { toSave.setImagePath(fileService.upload(image).toString()); } toSave.setTags( tagService.findTipsAndTricksTagsByNames(tipsAndTricksDtoRequest.getTags())); try { tipsAndTricksRepo.save(toSave); } catch (DataIntegrityViolationException e) { throw new NotSavedException(ErrorMessage.TIPS_AND_TRICKS_NOT_SAVED); } return modelMapper.map(toSave, TipsAndTricksDtoResponse.class); }  @CacheEvict(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME, allEntries = true) @Override TipsAndTricksDtoResponse save(TipsAndTricksDtoRequest tipsAndTricksDtoRequest, MultipartFile image,
                                         String email); @CacheEvict(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME, allEntries = true) @Override void update(TipsAndTricksDtoManagement tipsAndTricksDtoManagement,
                       MultipartFile image); @Cacheable(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME) @Override PageableDto<TipsAndTricksDtoResponse> findAll(Pageable page); @Override PageableDto<TipsAndTricksDtoResponse> find(Pageable page, List<String> tags); @Override TipsAndTricksDtoResponse findDtoById(Long id); @CacheEvict(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME, allEntries = true) @Override void delete(Long id); @CacheEvict(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME, allEntries = true) @Override void deleteAll(List<Long> listId); TipsAndTricks findById(Long id); @Override TipsAndTricksDtoManagement findManagementDtoById(Long id); @Override PageableDto<SearchTipsAndTricksDto> search(String searchQuery); @Override Long getAmountOfWrittenTipsAndTrickByUserId(Long id);  }