### Question:
BoardGameGeekRatingService implements GameRatingService { @Override public Optional<BigDecimal> getAverageRating(final UUID gameId) { return this.resolveBoardGameGeekId(gameId) .map(bggId -> this.bggClient.getItemsWithRating(bggId)) .map(Items::getItems) .map(List::stream) .flatMap(Stream::findFirst) .map(ItemWithStatistics::getStatistics) .map(Statistics::getRatings) .map(Ratings::getAverage) .map(BigDecimalValue::getValue); } BoardGameGeekRatingService(final BoardGameGeekClient bggClient, final BoardGameQueryRepository boardGameQueryRepository); @Override Optional<BigDecimal> getAverageRating(final UUID gameId);  }### Answer:
@Test public void rating_for_known_boardgame_is_successfully_returned() { BoardGameGeekRatingService boardGameGeekRatingService = new BoardGameGeekRatingService(bggClient, queryRepository); when(queryRepository.findBoardGameById(GAME_ID)).thenReturn(Optional.of(createBoardGame(BGG_ID))); when(bggClient.getItemsWithRating(BGG_ID)).thenReturn(createBggResponse()); Optional<BigDecimal> rating = boardGameGeekRatingService.getAverageRating(GAME_ID); assertThat(rating).isNotEmpty(); assertThat(rating).contains(new BigDecimal("8.2")); }
@Test public void rating_for_unknown_boardgame_is_empty_optional() { BoardGameGeekRatingService boardGameGeekRatingService = new BoardGameGeekRatingService(bggClient, queryRepository); when(queryRepository.findBoardGameById(GAME_ID)).thenReturn(Optional.empty()); Optional<BigDecimal> rating = boardGameGeekRatingService.getAverageRating(GAME_ID); assertThat(rating).isEmpty(); }
@Test public void rating_for_game_without_bgg_id_is_empty_optional() { BoardGameGeekRatingService boardGameGeekRatingService = new BoardGameGeekRatingService(bggClient, queryRepository); when(queryRepository.findBoardGameById(GAME_ID)).thenReturn(Optional.of(createBoardGame(null))); Optional<BigDecimal> rating = boardGameGeekRatingService.getAverageRating(GAME_ID); assertThat(rating).isEmpty(); verifyZeroInteractions(bggClient); }