Class ComparingNormalizedFields
- java.lang.Object
-
- org.assertj.core.api.recursive.comparison.ComparingNormalizedFields
-
- All Implemented Interfaces:
RecursiveComparisonIntrospectionStrategy
- Direct Known Subclasses:
ComparingSnakeOrCamelCaseFields
public abstract class ComparingNormalizedFields extends java.lang.Object implements RecursiveComparisonIntrospectionStrategy
ARecursiveComparisonIntrospectionStrategythat 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
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.Class<?>,java.util.Set<java.lang.String>>fieldNamesPerClassprivate static java.lang.StringNO_FIELD_FOUNDprivate java.util.Map<java.lang.Class<?>,java.util.Map<java.lang.String,java.lang.String>>originalFieldNameByNormalizedFieldNameByType
-
Constructor Summary
Constructors Constructor Description ComparingNormalizedFields()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description java.lang.ObjectgetChildNodeValue(java.lang.String fieldName, java.lang.Object instance)Returns the value of the given object field identified by the fieldName parameter.java.util.Set<java.lang.String>getChildrenNodeNamesOf(java.lang.Object node)Returns the normalized names of the children nodes of the given object that will be used in the recursive comparison.java.lang.StringgetDescription()Returns a human-readable description of the strategy to be used in error messages.private java.lang.StringgetOriginalFieldName(java.lang.String fieldName, java.lang.Object instance)private java.lang.Stringnormalize(java.lang.Class<?> nodeClass, java.lang.String fieldName)Normalize the field name and keep track of the normalized name -> original nameprotected abstract java.lang.StringnormalizeFieldName(java.lang.String fieldName)Returns the normalized version of the given field name to allow actual and expected fields to be matched.
-
-
-
Field Detail
-
NO_FIELD_FOUND
private static final java.lang.String NO_FIELD_FOUND
- See Also:
- Constant Field Values
-
originalFieldNameByNormalizedFieldNameByType
private final java.util.Map<java.lang.Class<?>,java.util.Map<java.lang.String,java.lang.String>> originalFieldNameByNormalizedFieldNameByType
-
fieldNamesPerClass
private final java.util.Map<java.lang.Class<?>,java.util.Set<java.lang.String>> fieldNamesPerClass
-
-
Method Detail
-
getChildrenNodeNamesOf
public java.util.Set<java.lang.String> getChildrenNodeNamesOf(java.lang.Object node)
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
protected abstract java.lang.String normalizeFieldName(java.lang.String fieldName)
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
-
normalize
private java.lang.String normalize(java.lang.Class<?> nodeClass, java.lang.String fieldName)Normalize the field name and keep track of the normalized name -> original name- Parameters:
fieldName- the field name to normalize- Returns:
- the normalized field name
-
getChildNodeValue
public java.lang.Object getChildNodeValue(java.lang.String fieldName, java.lang.Object instance)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
-
getOriginalFieldName
private java.lang.String getOriginalFieldName(java.lang.String fieldName, java.lang.Object instance)
-
getDescription
public java.lang.String 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
-
-