### Question:
MovieIndexHelper { public void indexMovie(final Movie movie) { try { if (movie.isDeleted()) { movieIndexRepository.deleteById(movie.getId()); movieLikeRepository.deleteByMovieId(movie.getId()); movieRepository.deleteById(movie.getId()); return; } movie.getPendingLikes().forEach(account -> { final MovieLike movieLike = new MovieLike(); movieLike.setId(movie.getId() + "-" + account); movieLike.setMovieId(movie.getId()); movieLike.setAccount(account); movieLikeRepository.save(movieLike); }); movie.setPendingLikes(new HashSet<>()); movie.getPendingUnlikes() .forEach(account -> movieLikeRepository.deleteById(movie.getId() + "-" + account)); movie.setPendingUnlikes(new HashSet<>()); movie.setTotalLikes(movieLikeRepository.countByMovieId(movie.getId())); final MovieIndex movieIndex = movieMapper.movieToMovieIndex(movie); movieIndexRepository.save(movieIndex); final String currentRevision = movie.getRevision(); movie.setRevision(UUID.randomUUID().toString()); movie.setUpdated(false); movieRepository.updateMovie(movie, currentRevision); } catch (final Exception ex) { log.error(String.format("An error occured while indexing movie %s: %s", movie.getId(), ex.getMessage()), ex); } }  void indexMovie(final Movie movie);  }### Answer:
@Test public void test_indexMovie_deleteMovie() { final Movie movie = new Movie(); movie.setId(UUID.randomUUID().toString()); movie.setDeleted(true); movieIndexHelper.indexMovie(movie); verify(movieRepository).deleteById(movie.getId()); verify(movieIndexRepository).deleteById(movie.getId()); verify(movieLikeRepository).deleteByMovieId(movie.getId()); }
@Test public void test_indexMovie_updateMovie() { final String currentRevision = UUID.randomUUID().toString(); final Movie movie = new Movie(); movie.setId(UUID.randomUUID().toString()); movie.setRevision(currentRevision); movie.setPendingLikes(new HashSet<>(Arrays.asList("account1"))); movie.setPendingUnlikes(new HashSet<>(Arrays.asList("account2"))); final MovieIndex movieIndex = new MovieIndex(); final MovieLike movieLike = new MovieLike(); movieLike.setId(movie.getId() + "-account1"); movieLike.setMovieId(movie.getId()); movieLike.setAccount("account1"); when(movieRepository.findByUpdated(true)).thenReturn(Arrays.asList(movie)); when(movieMapper.movieToMovieIndex(movie)).thenReturn(movieIndex); when(movieLikeRepository.countByMovieId(movie.getId())).thenReturn(5L); movieIndexHelper.indexMovie(movie); verify(movieLikeRepository).save(movieLike); verify(movieLikeRepository).deleteById(movie.getId() + "-account2"); verify(movieIndexRepository).save(movieIndex); verify(movieRepository).updateMovie(movie, currentRevision); assertEquals(5L, movie.getTotalLikes()); assertTrue(movie.getPendingLikes().isEmpty()); assertTrue(movie.getPendingUnlikes().isEmpty()); assertFalse(movie.isUpdated()); }
@Test public void test_indexMovie_processException() { final Movie movie = new Movie(); movie.setId(UUID.randomUUID().toString()); movie.setDeleted(true); doThrow(new RuntimeException()).when(movieLikeRepository).deleteByMovieId(movie.getId()); movieIndexHelper.indexMovie(movie); verify(movieRepository, never()).deleteById(movie.getId()); }