package io.monokkel.core.utils; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import io.monokkel.core.DataCleaner; import io.monokkel.domain.PageData; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.Map; import static io.monokkel.core.utils.MapTransformation.transformAgainstMap; import static org.junit.Assert.*; import static org.mockito.Mockito.*; public class MapTransformationTest { @Before public void setUp() throws Exception { } @Test public void transformAgainstMap_withMultiSubExpressingsInSubObjectsAndCleanerFunction_expectPatternToClean(){ final String objectValues = "1 577pattern"; final String fieldNames = "object"; Map<String, Object> fieldsAndCleanExpressions = ImmutableMap.of("object",Lists.newArrayList("\\s", "pattern", Maps.newHashMap())); Map data = Maps.newHashMap(); data.put(fieldNames, objectValues); MapTransformationFunction mockedFunction = mock(MapTransformationFunction.class); when(mockedFunction.transform(anyString(),anyString(),anyMap())).thenReturn("transformedValue"); transformAgainstMap(data, fieldsAndCleanExpressions, mockedFunction); verify(mockedFunction, times(1)).transform(eq(fieldNames), eq(objectValues), eq(fieldsAndCleanExpressions)); } @Test public void transformAgainstMap_withMultiSubExpressingsInSubObjectsAndCleanerFunction_expectFunctionToBeCalled(){ final String objectValues = "1 577pattern"; final String fieldNames = "notMatched"; Map<String,Object> subObject = ImmutableMap.of("anotherObject","\\s"); Map<String, Object> fieldsAndCleanExpressions = ImmutableMap.of("object",Lists.newArrayList("\\s", "pattern", Maps.newHashMap()),"subObject",subObject); Map data = Maps.newHashMap(); data.put(fieldNames, objectValues); MapTransformationFunction mockedFunction = mock(MapTransformationFunction.class); when(mockedFunction.transform(anyString(),anyString(),anyMap())).thenReturn("transformedValue"); transformAgainstMap(data, fieldsAndCleanExpressions, mockedFunction); verify(mockedFunction, times(1)).transform(eq(fieldNames), eq(objectValues), eq(fieldsAndCleanExpressions)); } private Map<String, Object> setupMultiExpressionMap() { Map<String,Object> fieldsAndCleanExpressions = Maps.newHashMap(); // The hashmap is added to detect if the filtering work final ArrayList subObjectList = Lists.newArrayList("\\s", "pattern", Maps.newHashMap()); fieldsAndCleanExpressions.put("object", subObjectList); Map<String, Object> subMap = Maps.newHashMap(); subMap.put("object", subObjectList); fieldsAndCleanExpressions.put("subobject",subMap); return fieldsAndCleanExpressions; } }