@Test public void saveBadUserEmailTest() { FavoritePlaceDto dto = ModelUtils.getFavoritePlaceDto(); FavoritePlace fp = ModelUtils.getFavoritePlace(); String userEmail = "email"; Exception exception = assertThrows( WrongIdException.class, () -> { when(modelMapper.map(any(FavoritePlaceDto.class), eq(FavoritePlace.class))).thenReturn(fp); favoritePlaceService.save(dto, userEmail); }); String expectedMessage = ErrorMessage.PLACE_NOT_FOUND_BY_ID; String actualMessage = exception.getMessage(); assertTrue(actualMessage.contains(expectedMessage)); }
@Override public FavoritePlaceDto save(FavoritePlaceDto favoritePlaceDto, String userEmail) { log.info(LogMessage.IN_SAVE, favoritePlaceDto); FavoritePlace favoritePlace = modelMapper.map(favoritePlaceDto, FavoritePlace.class); if (!placeService.existsById(favoritePlace.getPlace().getId())) { throw new WrongIdException(ErrorMessage.PLACE_NOT_FOUND_BY_ID); } if (repo.findByPlaceIdAndUserEmail(favoritePlaceDto.getPlaceId(), userEmail) != null) { throw new WrongIdException(String.format( ErrorMessage.FAVORITE_PLACE_ALREADY_EXISTS, favoritePlaceDto.getPlaceId(), userEmail)); } favoritePlace.setUser(User.builder().email(userEmail).id(userService.findIdByEmail(userEmail)).build()); return modelMapper.map(repo.save(favoritePlace), FavoritePlaceDto.class); }
FavoritePlaceServiceImpl implements FavoritePlaceService { @Override public FavoritePlaceDto save(FavoritePlaceDto favoritePlaceDto, String userEmail) { log.info(LogMessage.IN_SAVE, favoritePlaceDto); FavoritePlace favoritePlace = modelMapper.map(favoritePlaceDto, FavoritePlace.class); if (!placeService.existsById(favoritePlace.getPlace().getId())) { throw new WrongIdException(ErrorMessage.PLACE_NOT_FOUND_BY_ID); } if (repo.findByPlaceIdAndUserEmail(favoritePlaceDto.getPlaceId(), userEmail) != null) { throw new WrongIdException(String.format( ErrorMessage.FAVORITE_PLACE_ALREADY_EXISTS, favoritePlaceDto.getPlaceId(), userEmail)); } favoritePlace.setUser(User.builder().email(userEmail).id(userService.findIdByEmail(userEmail)).build()); return modelMapper.map(repo.save(favoritePlace), FavoritePlaceDto.class); } }
FavoritePlaceServiceImpl implements FavoritePlaceService { @Override public FavoritePlaceDto save(FavoritePlaceDto favoritePlaceDto, String userEmail) { log.info(LogMessage.IN_SAVE, favoritePlaceDto); FavoritePlace favoritePlace = modelMapper.map(favoritePlaceDto, FavoritePlace.class); if (!placeService.existsById(favoritePlace.getPlace().getId())) { throw new WrongIdException(ErrorMessage.PLACE_NOT_FOUND_BY_ID); } if (repo.findByPlaceIdAndUserEmail(favoritePlaceDto.getPlaceId(), userEmail) != null) { throw new WrongIdException(String.format( ErrorMessage.FAVORITE_PLACE_ALREADY_EXISTS, favoritePlaceDto.getPlaceId(), userEmail)); } favoritePlace.setUser(User.builder().email(userEmail).id(userService.findIdByEmail(userEmail)).build()); return modelMapper.map(repo.save(favoritePlace), FavoritePlaceDto.class); }  }
FavoritePlaceServiceImpl implements FavoritePlaceService { @Override public FavoritePlaceDto save(FavoritePlaceDto favoritePlaceDto, String userEmail) { log.info(LogMessage.IN_SAVE, favoritePlaceDto); FavoritePlace favoritePlace = modelMapper.map(favoritePlaceDto, FavoritePlace.class); if (!placeService.existsById(favoritePlace.getPlace().getId())) { throw new WrongIdException(ErrorMessage.PLACE_NOT_FOUND_BY_ID); } if (repo.findByPlaceIdAndUserEmail(favoritePlaceDto.getPlaceId(), userEmail) != null) { throw new WrongIdException(String.format( ErrorMessage.FAVORITE_PLACE_ALREADY_EXISTS, favoritePlaceDto.getPlaceId(), userEmail)); } favoritePlace.setUser(User.builder().email(userEmail).id(userService.findIdByEmail(userEmail)).build()); return modelMapper.map(repo.save(favoritePlace), FavoritePlaceDto.class); }  @Override FavoritePlaceDto save(FavoritePlaceDto favoritePlaceDto, String userEmail); @Override FavoritePlaceDto update(FavoritePlaceDto favoritePlaceDto, String userEmail); @Override List<FavoritePlaceDto> findAllByUserEmail(String email); @Override @Transactional Long deleteByUserEmailAndPlaceId(Long placeId, String userEmail); @Override FavoritePlace findByPlaceId(Long placeId); @Override PlaceInfoDto getInfoFavoritePlace(Long placeId); @Override PlaceByBoundsDto getFavoritePlaceWithLocation(Long placeId, String email); }
FavoritePlaceServiceImpl implements FavoritePlaceService { @Override public FavoritePlaceDto save(FavoritePlaceDto favoritePlaceDto, String userEmail) { log.info(LogMessage.IN_SAVE, favoritePlaceDto); FavoritePlace favoritePlace = modelMapper.map(favoritePlaceDto, FavoritePlace.class); if (!placeService.existsById(favoritePlace.getPlace().getId())) { throw new WrongIdException(ErrorMessage.PLACE_NOT_FOUND_BY_ID); } if (repo.findByPlaceIdAndUserEmail(favoritePlaceDto.getPlaceId(), userEmail) != null) { throw new WrongIdException(String.format( ErrorMessage.FAVORITE_PLACE_ALREADY_EXISTS, favoritePlaceDto.getPlaceId(), userEmail)); } favoritePlace.setUser(User.builder().email(userEmail).id(userService.findIdByEmail(userEmail)).build()); return modelMapper.map(repo.save(favoritePlace), FavoritePlaceDto.class); }  @Override FavoritePlaceDto save(FavoritePlaceDto favoritePlaceDto, String userEmail); @Override FavoritePlaceDto update(FavoritePlaceDto favoritePlaceDto, String userEmail); @Override List<FavoritePlaceDto> findAllByUserEmail(String email); @Override @Transactional Long deleteByUserEmailAndPlaceId(Long placeId, String userEmail); @Override FavoritePlace findByPlaceId(Long placeId); @Override PlaceInfoDto getInfoFavoritePlace(Long placeId); @Override PlaceByBoundsDto getFavoritePlaceWithLocation(Long placeId, String email);  }