### Question:
ArrayContextualPreferenceReducer implements ContextualPreferenceReducer { @Override public Optional<ContextualPreference> reduce(final List<ContextualPreference> preferences) { if (preferences.isEmpty()) { return Optional.empty(); } final String mergedValue = preferences.stream() .map(ContextualPreference::getValue) .flatMap(instanceTypes -> Arrays.stream(instanceTypes.split(DELIMITER))) .distinct() .collect(Collectors.joining(DELIMITER)); final ContextualPreference reducedPreference = preferences.get(0) .withValue(mergedValue) .withCreatedDate(null) .withResource(null); return Optional.of(reducedPreference); }  @Override Optional<ContextualPreference> reduce(final List<ContextualPreference> preferences);  }### Answer:
@Test public void reduceShouldReturnPreferenceWithMergedValue() { final ContextualPreferenceExternalResource resource = new ContextualPreferenceExternalResource(LEVEL, RESOURCE_ID); final ContextualPreference preference1 = new ContextualPreference(NAME, VALUE_1, resource); final ContextualPreference preference2 = new ContextualPreference(NAME, VALUE_2, resource); final List<ContextualPreference> preferences = Arrays.asList(preference1, preference2); final Optional<ContextualPreference> reducedPreference = reducer.reduce(preferences); assertTrue(reducedPreference.isPresent()); assertThat(reducedPreference.get().getValue(), is(MERGED_VALUE)); }
@Test public void reduceShouldReturnEmptyOptionalIfTheGivenListOfPreferencesIsEmpty() { final Optional<ContextualPreference> reducedPreference = reducer.reduce(Collections.emptyList()); assertFalse(reducedPreference.isPresent()); }
@Test public void reduceShouldReturnPreferenceWithoutCreatedDateAndResource() { final ContextualPreferenceExternalResource resource = new ContextualPreferenceExternalResource(LEVEL, RESOURCE_ID); final ContextualPreference preference1 = new ContextualPreference(NAME, VALUE_1, resource); final ContextualPreference preference2 = new ContextualPreference(NAME, VALUE_2, resource); final List<ContextualPreference> preferences = Arrays.asList(preference1, preference2); final Optional<ContextualPreference> reducedPreference = reducer.reduce(preferences); assertTrue(reducedPreference.isPresent()); assertThat(reducedPreference.get().getCreatedDate(), is(nullValue())); assertThat(reducedPreference.get().getResource(), is(nullValue())); }