Class ComparingNormalizedFields
- All Implemented Interfaces:
RecursiveComparisonIntrospectionStrategy
- Direct Known Subclasses:
ComparingSnakeOrCamelCaseFields
RecursiveComparisonIntrospectionStrategy
that introspects fields provided their normalized name.
Subclass this and simply provide the normalization to apply by implementing normalizeFieldName(String)
.
It is recommended to override getDescription()
to get more informative error messages.
- Since:
- 3.24.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetChildNodeValue
(String fieldName, Object instance) Returns the value of the given object field identified by the fieldName parameter.getChildrenNodeNamesOf
(Object node) Returns the normalized names of the children nodes of the given object that will be used in the recursive comparison.Returns a human-readable description of the strategy to be used in error messages.protected abstract String
normalizeFieldName
(String fieldName) Returns the normalized version of the given field name to allow actual and expected fields to be matched.
-
Constructor Details
-
ComparingNormalizedFields
public ComparingNormalizedFields()
-
-
Method Details
-
getChildrenNodeNamesOf
Returns the normalized names of the children nodes of the given object that will be used in the recursive comparison.The names are normalized according to
normalizeFieldName(String)
.- Specified by:
getChildrenNodeNamesOf
in interfaceRecursiveComparisonIntrospectionStrategy
- Parameters:
node
- the object to get the child nodes from- Returns:
- the normalized names of the children nodes of the given object
-
normalizeFieldName
Returns the normalized version of the given field name to allow actual and expected fields to be matched.For example, let's assume
actual
is aPerson
with camel case fields likefirstName
andexpected
is aPersonDto
with snake case field likefirst_name
.The default recursive comparison gathers all
actual
andexpected
fields to compare them but fails as it can't know thatactual.firstName
must be compared toexpected.first_name
.
By normalizing fields names first, the recursive comparison can now operate on fields that can be matched.In our example, we can either normalize fields to be camel case or snake case (camel case would be more natural though).
Note that
getChildNodeValue(String, Object)
receives the normalized field name, it tries to get its value first and if failing to do so, it tries the original field name.
In our example, if we normalize to camel case, gettingfirstName
works fine foractual
but not forexpected
, we have to get the original field namefirst_name
to get the value (ComparingNormalizedFields
implementation tracks which original field names resulted in a specific normalized field name).- Parameters:
fieldName
- the field name to normalize- Returns:
- the normalized field name
-
getChildNodeValue
Returns the value of the given object field identified by the fieldName parameter.Note that this method receives the normalized field name with (see
normalizeFieldName(String)
), it tries to get its value first and if failing to do so, it tries the original field name (ComparingNormalizedFields
implementation tracks which original field names resulted in a specific normalized field name).For example, let's assume
actual
is aPerson
with camel case fields likefirstName
andexpected
is aPersonDto
with snake case field likefirst_name
and we normalize all fields names to be camel case. In this case, gettingfirstName
works fine foractual
but not forexpected
, for the latter it succeeds with the original field namefirst_name
.- Specified by:
getChildNodeValue
in interfaceRecursiveComparisonIntrospectionStrategy
- Parameters:
fieldName
- the field nameinstance
- the object to read the field from- Returns:
- the object field value
-
getDescription
Description copied from interface:RecursiveComparisonIntrospectionStrategy
Returns a human-readable description of the strategy to be used in error messages.The default implementation returns
this.getClass().getSimpleName()
.- Specified by:
getDescription
in interfaceRecursiveComparisonIntrospectionStrategy
- Returns:
- a description of the strategy
-