General
In the previous posts I started describing a validation / filtering framework we’re building.
While showing the code, I am trying to show clean code, test orientation and code evolution.
It has some agility in the process; We know the end requirements, but the exact details are evolving over time.
During the development we have changed the code to be more general as we saw some patterns in it.
The code evolved as the flow evolved as well.
The flow as we now understand it
Here’s a diagram of the flow we’ll implement

Request Sequence
The Pattern
At each step of the sequence (validation, filtering, action), we recognized the same pattern:
- We have specific implementations (filters, validations)
- We have an engine that wraps up the specific implementations
- We need to map the implementations by flag, and upon request’s flags, select the appropriate implementations.
- We need to have a class that calls the mapper and then the engine
A diagram showing the pattern

The Pattern
Source Code
In order to show some of the evolution of the code, and how refactoring changed it, I added tags in GitHub after major changes.
Code Examples
Let’s see what came up from the mapper pattern.
public interface MapperByFlag<T> {
List<T> getOperations(Request request);
}
public abstract class AbstractMapperByFlag<T> implements MapperByFlag<T> {
private List<T> defaultOperations;
private Map<String, List<T>> mapOfOperations;
public AbstractMapperByFlag(List<T> defaultOperations, Map<String, List<T>> mapOfOperations) {
this.defaultOperations = defaultOperations;
this.mapOfOperations = mapOfOperations;
}
@Override
public final List<T> getOperations(Request request) {
Set<T> selectedFilters = Sets.newHashSet(defaultOperations);
Set<String> flags = request.getFlags();
for (String flag : flags) {
if (mapOfOperations.containsKey(flag)) {
selectedFilters.addAll(mapOfOperations.get(flag));
}
}
return Lists.newArrayList(selectedFilters);
}
}
public RequestValidationByFlagMapper(List<RequestValidation> defaultValidations,
map<String, List<RequestValidation>> mapOfValidations) {
super(defaultValidations, mapOfValidations);
}
public ItemFiltersByFlagMapper(List<Filter> defaultFilters, Map<String, List<Filter>> mapOfFilters) {
super(defaultFilters, mapOfFilters);
}
I created a test for the abstract class, to show the flow itself.
The tests of the implementations use Java Reflection to verify that the correct injected parameters are sent to the super.
I am showing the imports here as well. To have some reference for the static imports, mockito and hamcrest packages and classes.
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;
import java.util.List;
import java.util.Map;
import org.eyal.requestvalidation.model.Request;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
@RunWith(MockitoJUnitRunner.class)
public class AbstractMapperByFlagTest {
private final static String FLAG_1 = "flag 1";
private final static String FLAG_2 = "flag 2";
@Mock
private Request request;
private String defaultOperation1 = "defaultOperation1";
private String defaultOperation2 = "defaultOperation2";
private String mapOperation11 = "mapOperation11";
private String mapOperation12 = "mapOperation12";
private String mapOperation23 = "mapOperation23";
private MapperByFlag<String> mapper;
@Before
public void setup() {
List<String> defaults = Lists.newArrayList(defaultOperation1, defaultOperation2);
Map<String, List<String>> mapped = ImmutableMap.<String, List<String>> builder()
.put(FLAG_1, Lists.newArrayList(mapOperation11, mapOperation12))
.put(FLAG_2, Lists.newArrayList(mapOperation23, mapOperation11)).build();
mapper = new AbstractMapperByFlag<String>(defaults, mapped) {
};
}
@Test
public void whenRequestDoesNotHaveFlagsShouldReturnDefaultFiltersOnly() {
when(request.getFlags()).thenReturn(Sets.<String> newHashSet());
List<String> filters = mapper.getOperations(request);
assertThat(filters, containsInAnyOrder(defaultOperation1, defaultOperation2));
}
@Test
public void whenRequestHasFlagsNotInMappingShouldReturnDefaultFiltersOnly() {
when(request.getFlags()).thenReturn(Sets.<String> newHashSet("un-mapped-flag"));
List<String> filters = mapper.getOperations(request);
assertThat(filters, containsInAnyOrder(defaultOperation1, defaultOperation2));
}
@Test
public void whenRequestHasOneFlagShouldReturnWithDefaultAndMappedFilters() {
when(request.getFlags()).thenReturn(Sets.<String> newHashSet(FLAG_1));
List<String> filters = mapper.getOperations(request);
assertThat(filters, containsInAnyOrder(mapOperation12, defaultOperation1, mapOperation11, defaultOperation2));
}
@Test
public void whenRequestHasTwoFlagsShouldReturnWithDefaultAndMappedFiltersWithoutDuplications() {
when(request.getFlags()).thenReturn(Sets.<String> newHashSet(FLAG_1, FLAG_2));
List<String> filters = mapper.getOperations(request);
assertThat(filters, containsInAnyOrder(mapOperation12, defaultOperation1, mapOperation11, defaultOperation2, mapOperation23));
}
}
@RunWith(MockitoJUnitRunner.class)
public class RequestValidationByFlagMapperTest {
@Mock
private List<RequestValidation> defaultValidations;
@Mock
private Map<String, List<RequestValidation>> mapOfValidations;
@InjectMocks
private RequestValidationByFlagMapper mapper;
@SuppressWarnings("unchecked")
@Test
public void verifyParameters() throws NoSuchFieldException, SecurityException, IllegalArgumentException,
IllegalAccessException {
Field defaultOperationsField = AbstractMapperByFlag.class.getDeclaredField("defaultOperations");
defaultOperationsField.setAccessible(true);
List<RequestValidation> actualFilters = (List<RequestValidation>) defaultOperationsField.get(mapper);
assertThat(actualFilters, sameInstance(defaultValidations));
Field mapOfFiltersField = AbstractMapperByFlag.class.getDeclaredField("mapOfOperations");
mapOfFiltersField.setAccessible(true);
Map<String, List<RequestValidation>> actualMapOfFilters = (Map<String, List<RequestValidation>>) mapOfFiltersField.get(mapper);
assertThat(actualMapOfFilters, sameInstance(mapOfValidations));
}
}
To Do
There are other classes that might be candidate for refactoring of some sort.
RequestFlowValidation and RequestFilter are similar.
And
RequestValidationsEngineImpl and FiltersEngine
To Do 2
Create a Matcher for the reflection part.
Code
As always, all the code can be found at:
A Tag for this post: all-components-in
Conclusion
The infrastructure is almost done.
During this time we are also implementing actual classes for the flow (validations, filters, actions).
These are not covered in the posts, nor in GitHub.
The infrastructure will be wired to a service we have using Spring.
This will be explained in future posts.