@Test() void saveThrowsNotSavedException() throws MalformedURLException { MultipartFile image = ModelUtils.getFile(); when(modelMapper.map(addEcoNewsDtoRequest, EcoNews.class)).thenReturn(ecoNews); when(ecoNewsRepo.save(ecoNews)).thenThrow(DataIntegrityViolationException.class); when(userService.findByEmail(TestConst.EMAIL)).thenReturn(ModelUtils.getUser()); when(fileService.upload(image)).thenReturn(ModelUtils.getUrl()); assertThrows(NotSavedException.class, () -> ecoNewsService.save(addEcoNewsDtoRequest, image, TestConst.EMAIL) ); }
@CacheEvict(value = CacheConstants.NEWEST_ECO_NEWS_CACHE_NAME, allEntries = true) @Override public AddEcoNewsDtoResponse save(AddEcoNewsDtoRequest addEcoNewsDtoRequest, MultipartFile image, String email) { EcoNews toSave = modelMapper.map(addEcoNewsDtoRequest, EcoNews.class); toSave.setAuthor(userService.findByEmail(email)); if (addEcoNewsDtoRequest.getImage() != null) { image = convertToMultipartImage(addEcoNewsDtoRequest.getImage()); } if (image != null) { toSave.setImagePath(fileService.upload(image).toString()); } Set<String> tagsSet = new HashSet<>(addEcoNewsDtoRequest.getTags()); if (tagsSet.size() < addEcoNewsDtoRequest.getTags().size()) { throw new NotSavedException(ErrorMessage.ECO_NEWS_NOT_SAVED); } toSave.setTags( tagService.findEcoNewsTagsByNames(addEcoNewsDtoRequest.getTags())); try { ecoNewsRepo.save(toSave); } catch (DataIntegrityViolationException e) { throw new NotSavedException(ErrorMessage.ECO_NEWS_NOT_SAVED); } rabbitTemplate.convertAndSend(sendEmailTopic, RabbitConstants.ADD_ECO_NEWS_ROUTING_KEY, buildAddEcoNewsMessage(toSave)); return modelMapper.map(toSave, AddEcoNewsDtoResponse.class); }
EcoNewsServiceImpl implements EcoNewsService { @CacheEvict(value = CacheConstants.NEWEST_ECO_NEWS_CACHE_NAME, allEntries = true) @Override public AddEcoNewsDtoResponse save(AddEcoNewsDtoRequest addEcoNewsDtoRequest, MultipartFile image, String email) { EcoNews toSave = modelMapper.map(addEcoNewsDtoRequest, EcoNews.class); toSave.setAuthor(userService.findByEmail(email)); if (addEcoNewsDtoRequest.getImage() != null) { image = convertToMultipartImage(addEcoNewsDtoRequest.getImage()); } if (image != null) { toSave.setImagePath(fileService.upload(image).toString()); } Set<String> tagsSet = new HashSet<>(addEcoNewsDtoRequest.getTags()); if (tagsSet.size() < addEcoNewsDtoRequest.getTags().size()) { throw new NotSavedException(ErrorMessage.ECO_NEWS_NOT_SAVED); } toSave.setTags( tagService.findEcoNewsTagsByNames(addEcoNewsDtoRequest.getTags())); try { ecoNewsRepo.save(toSave); } catch (DataIntegrityViolationException e) { throw new NotSavedException(ErrorMessage.ECO_NEWS_NOT_SAVED); } rabbitTemplate.convertAndSend(sendEmailTopic, RabbitConstants.ADD_ECO_NEWS_ROUTING_KEY, buildAddEcoNewsMessage(toSave)); return modelMapper.map(toSave, AddEcoNewsDtoResponse.class); } }
EcoNewsServiceImpl implements EcoNewsService { @CacheEvict(value = CacheConstants.NEWEST_ECO_NEWS_CACHE_NAME, allEntries = true) @Override public AddEcoNewsDtoResponse save(AddEcoNewsDtoRequest addEcoNewsDtoRequest, MultipartFile image, String email) { EcoNews toSave = modelMapper.map(addEcoNewsDtoRequest, EcoNews.class); toSave.setAuthor(userService.findByEmail(email)); if (addEcoNewsDtoRequest.getImage() != null) { image = convertToMultipartImage(addEcoNewsDtoRequest.getImage()); } if (image != null) { toSave.setImagePath(fileService.upload(image).toString()); } Set<String> tagsSet = new HashSet<>(addEcoNewsDtoRequest.getTags()); if (tagsSet.size() < addEcoNewsDtoRequest.getTags().size()) { throw new NotSavedException(ErrorMessage.ECO_NEWS_NOT_SAVED); } toSave.setTags( tagService.findEcoNewsTagsByNames(addEcoNewsDtoRequest.getTags())); try { ecoNewsRepo.save(toSave); } catch (DataIntegrityViolationException e) { throw new NotSavedException(ErrorMessage.ECO_NEWS_NOT_SAVED); } rabbitTemplate.convertAndSend(sendEmailTopic, RabbitConstants.ADD_ECO_NEWS_ROUTING_KEY, buildAddEcoNewsMessage(toSave)); return modelMapper.map(toSave, AddEcoNewsDtoResponse.class); }  }
EcoNewsServiceImpl implements EcoNewsService { @CacheEvict(value = CacheConstants.NEWEST_ECO_NEWS_CACHE_NAME, allEntries = true) @Override public AddEcoNewsDtoResponse save(AddEcoNewsDtoRequest addEcoNewsDtoRequest, MultipartFile image, String email) { EcoNews toSave = modelMapper.map(addEcoNewsDtoRequest, EcoNews.class); toSave.setAuthor(userService.findByEmail(email)); if (addEcoNewsDtoRequest.getImage() != null) { image = convertToMultipartImage(addEcoNewsDtoRequest.getImage()); } if (image != null) { toSave.setImagePath(fileService.upload(image).toString()); } Set<String> tagsSet = new HashSet<>(addEcoNewsDtoRequest.getTags()); if (tagsSet.size() < addEcoNewsDtoRequest.getTags().size()) { throw new NotSavedException(ErrorMessage.ECO_NEWS_NOT_SAVED); } toSave.setTags( tagService.findEcoNewsTagsByNames(addEcoNewsDtoRequest.getTags())); try { ecoNewsRepo.save(toSave); } catch (DataIntegrityViolationException e) { throw new NotSavedException(ErrorMessage.ECO_NEWS_NOT_SAVED); } rabbitTemplate.convertAndSend(sendEmailTopic, RabbitConstants.ADD_ECO_NEWS_ROUTING_KEY, buildAddEcoNewsMessage(toSave)); return modelMapper.map(toSave, AddEcoNewsDtoResponse.class); }  @CacheEvict(value = CacheConstants.NEWEST_ECO_NEWS_CACHE_NAME, allEntries = true) @Override AddEcoNewsDtoResponse save(AddEcoNewsDtoRequest addEcoNewsDtoRequest,
                                      MultipartFile image, String email); @Cacheable(value = CacheConstants.NEWEST_ECO_NEWS_CACHE_NAME) @Override List<EcoNewsDto> getThreeLastEcoNews(); @Override List<EcoNewsDto> getThreeRecommendedEcoNews(Long openedEcoNewsId); @Override PageableDto<EcoNewsDto> findAll(Pageable page); @Override PageableDto<EcoNewsDto> find(Pageable page, List<String> tags); @Override EcoNews findById(Long id); @Override EcoNewsDto findDtoById(Long id); @CacheEvict(value = CacheConstants.NEWEST_ECO_NEWS_CACHE_NAME, allEntries = true) @Override void delete(Long id); @Override PageableDto<SearchNewsDto> search(String searchQuery); @Override Long getAmountOfPublishedNewsByUserId(Long id); }
EcoNewsServiceImpl implements EcoNewsService { @CacheEvict(value = CacheConstants.NEWEST_ECO_NEWS_CACHE_NAME, allEntries = true) @Override public AddEcoNewsDtoResponse save(AddEcoNewsDtoRequest addEcoNewsDtoRequest, MultipartFile image, String email) { EcoNews toSave = modelMapper.map(addEcoNewsDtoRequest, EcoNews.class); toSave.setAuthor(userService.findByEmail(email)); if (addEcoNewsDtoRequest.getImage() != null) { image = convertToMultipartImage(addEcoNewsDtoRequest.getImage()); } if (image != null) { toSave.setImagePath(fileService.upload(image).toString()); } Set<String> tagsSet = new HashSet<>(addEcoNewsDtoRequest.getTags()); if (tagsSet.size() < addEcoNewsDtoRequest.getTags().size()) { throw new NotSavedException(ErrorMessage.ECO_NEWS_NOT_SAVED); } toSave.setTags( tagService.findEcoNewsTagsByNames(addEcoNewsDtoRequest.getTags())); try { ecoNewsRepo.save(toSave); } catch (DataIntegrityViolationException e) { throw new NotSavedException(ErrorMessage.ECO_NEWS_NOT_SAVED); } rabbitTemplate.convertAndSend(sendEmailTopic, RabbitConstants.ADD_ECO_NEWS_ROUTING_KEY, buildAddEcoNewsMessage(toSave)); return modelMapper.map(toSave, AddEcoNewsDtoResponse.class); }  @CacheEvict(value = CacheConstants.NEWEST_ECO_NEWS_CACHE_NAME, allEntries = true) @Override AddEcoNewsDtoResponse save(AddEcoNewsDtoRequest addEcoNewsDtoRequest,
                                      MultipartFile image, String email); @Cacheable(value = CacheConstants.NEWEST_ECO_NEWS_CACHE_NAME) @Override List<EcoNewsDto> getThreeLastEcoNews(); @Override List<EcoNewsDto> getThreeRecommendedEcoNews(Long openedEcoNewsId); @Override PageableDto<EcoNewsDto> findAll(Pageable page); @Override PageableDto<EcoNewsDto> find(Pageable page, List<String> tags); @Override EcoNews findById(Long id); @Override EcoNewsDto findDtoById(Long id); @CacheEvict(value = CacheConstants.NEWEST_ECO_NEWS_CACHE_NAME, allEntries = true) @Override void delete(Long id); @Override PageableDto<SearchNewsDto> search(String searchQuery); @Override Long getAmountOfPublishedNewsByUserId(Long id);  }