Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Request Validation and Filtering by Flags – Filtering an Item

DZone's Guide to

Request Validation and Filtering by Flags – Filtering an Item

· Java Zone ·
Free Resource

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

On a previous post, I introduced a system requirement of validating and filtering a request by setting flags on it.

Reference: Introduction

In this post I want to show the filtering system.

Here are general UML diagrams of the filtering components and sequence.

Filtering UML Diagram

General Components

Item
public interface Item {
String getName();
}
Request
public interface Request {
Set getFlags();
List getItems();
}

Filter Mechanism (as described in the UML above)

Filter
public interface Filter extends Predicate {
String errorMessage();
}

FilterEngine is a cool part, which takes several Filters and apply to each the items. Below you can see the code of it. Above, the sequence diagram shows how it’s done.

FilterEngine
public class FiltersEngine {
public FiltersEngine() {
}
public ItemsFilterResponse applyFilters(List filters, List items) {
List validItems = Lists.newLinkedList(items);
List invalidItemInformations = Lists.newLinkedList();
for (Filter validator : filters) {
ItemsFilterResponse responseFromFilter = responseFromFilter(validItems, validator);
validItems = responseFromFilter.getValidItems();
invalidItemInformations.addAll(responseFromFilter.getInvalidItemsInformations());
}
return new ItemsFilterResponse(validItems, invalidItemInformations);
}
private ItemsFilterResponse responseFromFilter(List items, Filter filter) {
List validItems = Lists.newLinkedList();
List invalidItemInformations = Lists.newLinkedList();
for (Item item : items) {
if (filter.apply(item)) {
validItems.add(item);
} else {
invalidItemInformations.add(new InvalidItemInformation(item, filter.errorMessage()));
}
}
return new ItemsFilterResponse(validItems, invalidItemInformations);
}
}

And of course, we need to test it:

Some explanation about the test
You can see that I don’t care about the implementation of Filter. Actually, I don’t even have any implementation of it.
I also don’t have implementation of the Item nor the request.
You can see an example of how to create a BaseMatcher to be used with assertThat(…)

Coding
Try to see whether it is ‘clean’. Can you understand the story of the code? Can you tell what the code does by reading it line by line?

On the following post I will show how I applied the flag mapping to select the correct filters for a request.

You can find all the code in: https://github.com/eyalgo/request-validation

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}