Class ComparingNormalizedFields

    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      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.
      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.String getDescription()
      Returns a human-readable description of the strategy to be used in error messages.
      private java.lang.String normalize​(java.lang.Object node, java.lang.String fieldName)
      Normalize the field name and keep track of the normalized name -> original name
      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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • originalFieldNamesByNormalizedFieldNameByNode

        private final java.util.Map<java.lang.Object,​java.util.Map<java.lang.String,​java.lang.String>> originalFieldNamesByNormalizedFieldNameByNode
    • Constructor Detail

      • ComparingNormalizedFields

        public ComparingNormalizedFields()
    • 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:
        getChildrenNodeNamesOf in interface RecursiveComparisonIntrospectionStrategy
        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 actual is a Person with camel case fields like firstName and expected is a PersonDto with snake case field like first_name.

        The default recursive comparison gathers all actual and expected fields to compare them but fails as it can't know that actual.firstName must be compared to expected.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, getting firstName works fine for actual but not for expected, we have to get the original field name first_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
      • normalize

        private java.lang.String normalize​(java.lang.Object node,
                                           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 (ComparingNormalizedFields implementation tracks which original field names resulted in a specific normalized field name).

        For example, let's assume actual is a Person with camel case fields like firstName and expected is a PersonDto with snake case field like first_name and we normalize all fields names to be camel case. In this case, getting firstName works fine for actual but not for expected, for the latter it succeeds with the original field name first_name.

        Specified by:
        getChildNodeValue in interface RecursiveComparisonIntrospectionStrategy
        Parameters:
        fieldName - the field name
        instance - the object to read the field from
        Returns:
        the object field value