DeviationYearComparator extends AbstractMovieComparator { @Override public boolean areEqual(@NonNull Movie movie1, @NonNull Movie movie2) { int yearDeviation = config.getInt("year_deviation"); int movie2Year = movie2.getYear(); boolean result = Range.closed(movie2Year - yearDeviation, movie2Year + yearDeviation).contains(movie1.getYear()); LOG.debug( "Comparing year '{}' with year '{}', with deviation '{}', matches = '{}'", movie1.getYear(), movie2Year, yearDeviation, result ); return result; } DeviationYearComparator(@NonNull Config config); @Override boolean areEqual(@NonNull Movie movie1, @NonNull Movie movie2);  }
@Test public void areEqualWithinDeviation() throws Exception { deviations.forEach(deviation -> { MovieComparator comparator = makeComparator(deviation); MovieTestData.MOVIES_LIST.forEach(movie -> { Movie movieWithForwardDeviation = new Movie(movie); movieWithForwardDeviation.setYear(movieWithForwardDeviation.getYear() + deviation); assertTrue(comparator.areEqual(movieWithForwardDeviation, movie)); Movie movieWithBackwardDeviation = new Movie(movie); movieWithBackwardDeviation.setYear(movieWithBackwardDeviation.getYear() - deviation); assertTrue(comparator.areEqual(movieWithBackwardDeviation, movie)); }); }); }
@Test public void areEqualWithDeviationOverflow() throws Exception { deviations.forEach(deviation -> { MovieComparator comparator = makeComparator(deviation); MovieTestData.MOVIES_LIST.forEach(movie -> { Movie deviatedMovie = new Movie(movie); deviatedMovie.setYear(deviatedMovie.getYear() + deviation + 1); assertFalse(comparator.areEqual(deviatedMovie, movie)); }); }); }
@Test public void areEqualWithDeviationUnderflow() throws Exception { deviations.forEach(deviation -> { MovieComparator comparator = makeComparator(deviation); MovieTestData.MOVIES_LIST.forEach(movie -> { Movie deviatedMovie = new Movie(movie); deviatedMovie.setYear(deviatedMovie.getYear() - deviation); assertTrue(comparator.areEqual(deviatedMovie, movie)); }); }); }