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 StringnormalizeFieldName(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:
getChildrenNodeNamesOfin 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
actualis aPersonwith camel case fields likefirstNameandexpectedis aPersonDtowith snake case field likefirst_name.The default recursive comparison gathers all
actualandexpectedfields to compare them but fails as it can't know thatactual.firstNamemust 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, gettingfirstNameworks fine foractualbut not forexpected, we have to get the original field namefirst_nameto get the value (ComparingNormalizedFieldsimplementation 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 (ComparingNormalizedFieldsimplementation tracks which original field names resulted in a specific normalized field name).For example, let's assume
actualis aPersonwith camel case fields likefirstNameandexpectedis aPersonDtowith snake case field likefirst_nameand we normalize all fields names to be camel case. In this case, gettingfirstNameworks fine foractualbut not forexpected, for the latter it succeeds with the original field namefirst_name.- Specified by:
getChildNodeValuein interfaceRecursiveComparisonIntrospectionStrategy- Parameters:
fieldName- the field nameinstance- the object to read the field from- Returns:
- the object field value
-
getDescription
Description copied from interface:RecursiveComparisonIntrospectionStrategyReturns a human-readable description of the strategy to be used in error messages.The default implementation returns
this.getClass().getSimpleName().- Specified by:
getDescriptionin interfaceRecursiveComparisonIntrospectionStrategy- Returns:
- a description of the strategy
-