### Question:
SystemPreferenceHandler extends AbstractContextualPreferenceHandler { @Override public Optional<ContextualPreference> search(final List<String> preferences, final List<ContextualPreferenceExternalResource> resources) { final Optional<ContextualPreference> preference = preferences.stream() .map(this::loadPreference) .filter(Optional::isPresent) .map(Optional::get) .findFirst(); return preference.isPresent() ? preference : searchUsingNextHandler(preferences, resources); } SystemPreferenceHandler(final PreferenceManager preferenceManager,
                                   final ContextualPreferenceHandler nextHandler);  SystemPreferenceHandler(final PreferenceManager preferenceManager); @Override Optional<ContextualPreference> search(final List<String> preferences,
                                                 final List<ContextualPreferenceExternalResource> resources);  }### Answer:
@Test public void searchShouldReturnPreferenceIfItExists() { final ContextualPreference preference = new ContextualPreference(NAME, VALUE); when(preferenceManager.load(eq(NAME))).thenReturn(Optional.of(systemPreference(NAME, VALUE))); final Optional<ContextualPreference> searchedPreference = handler().search(SINGLE_NAME, Collections.emptyList()); assertTrue(searchedPreference.isPresent()); assertThat(searchedPreference.get(), is(preference)); verify(preferenceManager).load(eq(NAME)); }
@Test public void searchShouldDelegateExecutionToTheNextHandlerIfPreferenceDoesNotExist() { final ContextualPreference preference = new ContextualPreference(NAME, VALUE, resource); final List<ContextualPreferenceExternalResource> resources = Collections.emptyList(); when(preferenceManager.load(eq(NAME))).thenReturn(Optional.empty()); when(nextHandler.search(eq(SINGLE_NAME), eq(resources))).thenReturn(Optional.of(preference)); final Optional<ContextualPreference> searchedPreference = handler().search(SINGLE_NAME, resources); assertTrue(searchedPreference.isPresent()); assertThat(searchedPreference.get(), is(preference)); verify(preferenceManager).load(eq(NAME)); verify(nextHandler).search(eq(SINGLE_NAME), eq(resources)); }
@Test public void searchShouldLoadPreferenceByAnotherNameIfThereIsNoPreferenceWithTheFirstName() { final ContextualPreference preference = new ContextualPreference(ANOTHER_NAME, VALUE); when(preferenceManager.load(eq(NAME))).thenReturn(Optional.empty()); when(preferenceManager.load(eq(ANOTHER_NAME))).thenReturn(Optional.of(systemPreference(ANOTHER_NAME, VALUE))); final Optional<ContextualPreference> searchedPreference = handler().search(SEVERAL_NAMES, Collections.emptyList()); assertTrue(searchedPreference.isPresent()); assertThat(searchedPreference.get(), is(preference)); verify(preferenceManager).load(eq(NAME)); verify(preferenceManager).load(eq(ANOTHER_NAME)); }
@Test public void searchShouldDelegateExecutionToTheNextHandlerIfNoneOfThePreferencesExist() { final ContextualPreference preference = new ContextualPreference(NAME, VALUE, resource); final List<ContextualPreferenceExternalResource> resources = Collections.emptyList(); when(preferenceManager.load(eq(NAME))).thenReturn(Optional.empty()); when(preferenceManager.load(eq(ANOTHER_NAME))).thenReturn(Optional.empty()); when(nextHandler.search(eq(SEVERAL_NAMES), eq(resources))).thenReturn(Optional.of(preference)); final Optional<ContextualPreference> searchedPreference = handler().search(SEVERAL_NAMES, resources); assertTrue(searchedPreference.isPresent()); assertThat(searchedPreference.get(), is(preference)); verify(preferenceManager).load(eq(NAME)); verify(preferenceManager).load(eq(ANOTHER_NAME)); verify(nextHandler).search(eq(SEVERAL_NAMES), eq(resources)); }