MenuFilter implements Function<List<RestoMenu>, List<RestoMenu>> { @NonNull @Override public List<RestoMenu> apply(@NonNull List<RestoMenu> restoMenus) { LocalTime closingHour = LocalTime.parse( preferences.getString( RestoPreferenceFragment.PREF_RESTO_CLOSING_HOUR, RestoPreferenceFragment.DEFAULT_CLOSING_TIME ) ); LocalDate today = LocalDate.now(clock); boolean isEarlyEnough = LocalDateTime.now(clock).isBefore(LocalDateTime.of(today, closingHour)); return restoMenus.stream() .filter(m -> m.getDate().isAfter(today) || (m.getDate().isEqual(today) && isEarlyEnough)) .collect(Collectors.toList()); } MenuFilter(Context context); @VisibleForTesting  MenuFilter(Context context, Clock clock); @VisibleForTesting  MenuFilter(SharedPreferences preferences, Clock clock); @NonNull @Override List<RestoMenu> apply(@NonNull List<RestoMenu> restoMenus);  }
@Test public void testDefaults() throws IOException { Moshi moshi = InstanceProvider.getMoshi(); List<RestoMenu> menus = Utils.readJson(moshi, "resto/menu_default.json", Types.newParameterizedType(List.class, RestoMenu.class)); for (int i = 0; i < menus.size(); i++) { if (i < menus.size() / 2) { menus.get(i).setDate(LocalDateTime.ofInstant(cutOff, ZoneId.systemDefault()).toLocalDate().minusDays(menus.size() / 2 - i)); } else { menus.get(i).setDate(LocalDateTime.ofInstant(cutOff, ZoneId.systemDefault()).toLocalDate().plusDays(i - menus.size() / 2)); } } MenuFilter filter = new MenuFilter(context, clock); List<RestoMenu> result = filter.apply(menus); assertThat(result, hasSize(menus.size() / 2)); assertTrue(result.stream().noneMatch(restoMenu -> restoMenu.getDate().isBefore(cutOff.atZone(ZoneId.systemDefault()).toLocalDate()))); }
@Test public void testSameDayBefore() { RestoMenu restoMenu = generate(RestoMenu.class); restoMenu.setDate(LocalDateTime.ofInstant(cutOff, ZoneId.systemDefault()).toLocalDate()); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); preferences.edit() .putString(RestoPreferenceFragment.PREF_RESTO_CLOSING_HOUR, "11:00") .apply(); MenuFilter filter = new MenuFilter(context, clock); List<RestoMenu> result = filter.apply(Collections.singletonList(restoMenu)); assertThat(result, contains(restoMenu)); }
@Test public void testSameDayAfter() { RestoMenu restoMenu = generate(RestoMenu.class); restoMenu.setDate(LocalDateTime.ofInstant(cutOff, ZoneId.systemDefault()).toLocalDate()); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); preferences.edit() .putString(RestoPreferenceFragment.PREF_RESTO_CLOSING_HOUR, "09:00") .apply(); MenuFilter filter = new MenuFilter(context, clock); List<RestoMenu> result = filter.apply(Collections.singletonList(restoMenu)); assertThat(result, empty()); }
@Test public void testSameDayOnMoment() { RestoMenu restoMenu = generate(RestoMenu.class); restoMenu.setDate(LocalDateTime.ofInstant(cutOff, ZoneId.systemDefault()).toLocalDate()); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); preferences.edit() .putString(RestoPreferenceFragment.PREF_RESTO_CLOSING_HOUR, "10:15") .apply(); MenuFilter filter = new MenuFilter(preferences, clock); List<RestoMenu> result = filter.apply(Collections.singletonList(restoMenu)); assertThat(result, empty()); }