FieldNormalizer implements Normalizer<T> { @Override public List<T> normalize(List<T> objectsToNormalize) { var distinctValues = getDistinctFieldValues(objectsToNormalize); var normalizedLobMap = distinctValues.stream() .collect(Collectors.toMap( Function.identity(), distinctValue -> RootTermDistanceCalculator.nearestRootTerm(fieldDistanceCalculators, distinctValue, 7) )); return objectsToNormalize.parallelStream() .map(objToNormalize -> { var currentLob = getFieldValue(objToNormalize); if(currentLob != null) { var normalizedLob = normalizedLobMap.get(currentLob); return setNormalizedFieldValue(objToNormalize, normalizedLob); } return objToNormalize; }) .collect(Collectors.toList()); } protected  FieldNormalizer(Set<RootTermDistanceCalculator> fieldDistanceCalculators); @Override List<T> normalize(List<T> objectsToNormalize);  }
@Test public void normalizeNormalizesLobOfAllContainers() { var firstContainerName = "red thing"; var normalizedFirstContainerName = "Tomato"; var firstContainer = new FruitNameContainer(firstContainerName); var normalizedFirstContainer = new FruitNameContainer(normalizedFirstContainerName); var secondContainerName = "green thing"; var normalizedSecondContainerName = "Apple"; var secondContainer = new FruitNameContainer(secondContainerName); var normalizedSecondContainer = new FruitNameContainer(normalizedSecondContainerName); var givenContainers = List.of(firstContainer, secondContainer); var expectedNormalizedContainers = List.of(normalizedFirstContainer, normalizedSecondContainer); given(firstDistanceCalculatorMock.getOriginalRootTerm()).willReturn(normalizedFirstContainerName); given(firstDistanceCalculatorMock.calculateMinimalDistance(any())).willReturn(100); given(firstDistanceCalculatorMock.calculateMinimalDistance(firstContainerName)).willReturn(0); given(secondDistanceCalculatorMock.getOriginalRootTerm()).willReturn(normalizedSecondContainerName); given(secondDistanceCalculatorMock.calculateMinimalDistance(any())).willReturn(100); given(secondDistanceCalculatorMock.calculateMinimalDistance(secondContainerName)).willReturn(0); var actualNormalizedContainers = fieldNormalizer.normalize(givenContainers); assertThat(actualNormalizedContainers).containsExactlyInAnyOrderElementsOf(expectedNormalizedContainers); }