@Test public void shouldSearch() throws Exception { testee.search("title", MediaIdType.TVTITLE); verify(tvMazeHandlerMock).search("title"); testee.search("title", MediaIdType.MOVIETITLE); verify(tmdbHandlerMock).search("title", null); }
@Cacheable(cacheNames = "titles", sync = true) public List<MediaInfo> search(String title, MediaIdType titleType) throws InfoProviderException { try { List<MediaInfo> infos; switch (titleType) { case TVTITLE: { List<TvMazeSearchResult> results = tvMazeHandler.search(title); infos = results.stream().map(MediaInfo::new).collect(Collectors.toList()); for (MediaInfo mediaInfo : infos) { TvInfo tvInfo = new TvInfo(mediaInfo); if (tvInfoRepository.findByTvrageIdOrTvmazeIdOrTvdbIdOrImdbId(tvInfo.getTvrageId().orElse("-1"), tvInfo.getTvmazeId().orElse("-1"), tvInfo.getTvdbId().orElse("-1"), tvInfo.getImdbId().orElse("-1")) == null) { tvInfoRepository.save(tvInfo); } } break; } case MOVIETITLE: { List<TmdbSearchResult> results = tmdbHandler.search(title, null); infos = results.stream().map(MediaInfo::new).collect(Collectors.toList()); break; } default: throw new IllegalArgumentException("Wrong IdType"); } return infos; } catch (Exception e) { logger.error("Error while searching for " + titleType + " " + title, e); Throwables.throwIfInstanceOf(e, InfoProviderException.class); throw new InfoProviderException("Unexpected error while converting infos", e); } }
InfoProvider { @Cacheable(cacheNames = "titles", sync = true) public List<MediaInfo> search(String title, MediaIdType titleType) throws InfoProviderException { try { List<MediaInfo> infos; switch (titleType) { case TVTITLE: { List<TvMazeSearchResult> results = tvMazeHandler.search(title); infos = results.stream().map(MediaInfo::new).collect(Collectors.toList()); for (MediaInfo mediaInfo : infos) { TvInfo tvInfo = new TvInfo(mediaInfo); if (tvInfoRepository.findByTvrageIdOrTvmazeIdOrTvdbIdOrImdbId(tvInfo.getTvrageId().orElse("-1"), tvInfo.getTvmazeId().orElse("-1"), tvInfo.getTvdbId().orElse("-1"), tvInfo.getImdbId().orElse("-1")) == null) { tvInfoRepository.save(tvInfo); } } break; } case MOVIETITLE: { List<TmdbSearchResult> results = tmdbHandler.search(title, null); infos = results.stream().map(MediaInfo::new).collect(Collectors.toList()); break; } default: throw new IllegalArgumentException("Wrong IdType"); } return infos; } catch (Exception e) { logger.error("Error while searching for " + titleType + " " + title, e); Throwables.throwIfInstanceOf(e, InfoProviderException.class); throw new InfoProviderException("Unexpected error while converting infos", e); } } }
InfoProvider { @Cacheable(cacheNames = "titles", sync = true) public List<MediaInfo> search(String title, MediaIdType titleType) throws InfoProviderException { try { List<MediaInfo> infos; switch (titleType) { case TVTITLE: { List<TvMazeSearchResult> results = tvMazeHandler.search(title); infos = results.stream().map(MediaInfo::new).collect(Collectors.toList()); for (MediaInfo mediaInfo : infos) { TvInfo tvInfo = new TvInfo(mediaInfo); if (tvInfoRepository.findByTvrageIdOrTvmazeIdOrTvdbIdOrImdbId(tvInfo.getTvrageId().orElse("-1"), tvInfo.getTvmazeId().orElse("-1"), tvInfo.getTvdbId().orElse("-1"), tvInfo.getImdbId().orElse("-1")) == null) { tvInfoRepository.save(tvInfo); } } break; } case MOVIETITLE: { List<TmdbSearchResult> results = tmdbHandler.search(title, null); infos = results.stream().map(MediaInfo::new).collect(Collectors.toList()); break; } default: throw new IllegalArgumentException("Wrong IdType"); } return infos; } catch (Exception e) { logger.error("Error while searching for " + titleType + " " + title, e); Throwables.throwIfInstanceOf(e, InfoProviderException.class); throw new InfoProviderException("Unexpected error while converting infos", e); } }  }
InfoProvider { @Cacheable(cacheNames = "titles", sync = true) public List<MediaInfo> search(String title, MediaIdType titleType) throws InfoProviderException { try { List<MediaInfo> infos; switch (titleType) { case TVTITLE: { List<TvMazeSearchResult> results = tvMazeHandler.search(title); infos = results.stream().map(MediaInfo::new).collect(Collectors.toList()); for (MediaInfo mediaInfo : infos) { TvInfo tvInfo = new TvInfo(mediaInfo); if (tvInfoRepository.findByTvrageIdOrTvmazeIdOrTvdbIdOrImdbId(tvInfo.getTvrageId().orElse("-1"), tvInfo.getTvmazeId().orElse("-1"), tvInfo.getTvdbId().orElse("-1"), tvInfo.getImdbId().orElse("-1")) == null) { tvInfoRepository.save(tvInfo); } } break; } case MOVIETITLE: { List<TmdbSearchResult> results = tmdbHandler.search(title, null); infos = results.stream().map(MediaInfo::new).collect(Collectors.toList()); break; } default: throw new IllegalArgumentException("Wrong IdType"); } return infos; } catch (Exception e) { logger.error("Error while searching for " + titleType + " " + title, e); Throwables.throwIfInstanceOf(e, InfoProviderException.class); throw new InfoProviderException("Unexpected error while converting infos", e); } }  boolean canConvert(MediaIdType from, MediaIdType to); static Set<MediaIdType> getConvertibleFrom(MediaIdType from); boolean canConvertAny(Set<MediaIdType> from, Set<MediaIdType> to); MediaInfo convert(Map<MediaIdType, String> identifiers); @Cacheable(cacheNames = "infos", sync = true) //sync=true is currently apparently not supported by Caffeine. synchronizing by method is good enough because we'll likely rarely hit this method concurrently with different parameters synchronized MediaInfo convert(String value, MediaIdType fromType); TvInfo findTvInfoInDatabase(Map<MediaIdType, String> ids); MovieInfo findMovieInfoInDatabase(Map<MediaIdType, String> ids); @Cacheable(cacheNames = "titles", sync = true) List<MediaInfo> search(String title, MediaIdType titleType); }
InfoProvider { @Cacheable(cacheNames = "titles", sync = true) public List<MediaInfo> search(String title, MediaIdType titleType) throws InfoProviderException { try { List<MediaInfo> infos; switch (titleType) { case TVTITLE: { List<TvMazeSearchResult> results = tvMazeHandler.search(title); infos = results.stream().map(MediaInfo::new).collect(Collectors.toList()); for (MediaInfo mediaInfo : infos) { TvInfo tvInfo = new TvInfo(mediaInfo); if (tvInfoRepository.findByTvrageIdOrTvmazeIdOrTvdbIdOrImdbId(tvInfo.getTvrageId().orElse("-1"), tvInfo.getTvmazeId().orElse("-1"), tvInfo.getTvdbId().orElse("-1"), tvInfo.getImdbId().orElse("-1")) == null) { tvInfoRepository.save(tvInfo); } } break; } case MOVIETITLE: { List<TmdbSearchResult> results = tmdbHandler.search(title, null); infos = results.stream().map(MediaInfo::new).collect(Collectors.toList()); break; } default: throw new IllegalArgumentException("Wrong IdType"); } return infos; } catch (Exception e) { logger.error("Error while searching for " + titleType + " " + title, e); Throwables.throwIfInstanceOf(e, InfoProviderException.class); throw new InfoProviderException("Unexpected error while converting infos", e); } }  boolean canConvert(MediaIdType from, MediaIdType to); static Set<MediaIdType> getConvertibleFrom(MediaIdType from); boolean canConvertAny(Set<MediaIdType> from, Set<MediaIdType> to); MediaInfo convert(Map<MediaIdType, String> identifiers); @Cacheable(cacheNames = "infos", sync = true) //sync=true is currently apparently not supported by Caffeine. synchronizing by method is good enough because we'll likely rarely hit this method concurrently with different parameters synchronized MediaInfo convert(String value, MediaIdType fromType); TvInfo findTvInfoInDatabase(Map<MediaIdType, String> ids); MovieInfo findMovieInfoInDatabase(Map<MediaIdType, String> ids); @Cacheable(cacheNames = "titles", sync = true) List<MediaInfo> search(String title, MediaIdType titleType); static Set<MediaIdType> TV_ID_TYPES; static Set<MediaIdType> MOVIE_ID_TYPES; static Set<MediaIdType> REAL_ID_TYPES; }