Class WritingJsonValidator


  • public class WritingJsonValidator
    extends Object
    Utility class that supports testing if two JSON documents have the same content.
    Author:
    Matthias Müller
    • Field Detail

      • writeJson

        public static boolean writeJson
        Set to true to (over)write the control-files on (default false) (see also controlFileSource).
      • logJson

        public static boolean logJson
        Set to true to log the actual JSON document (debug level) when validating (default false).
      • sortPropertiesAlphabetically

        public static boolean sortPropertiesAlphabetically
        Set to true to let the object mapper sort the properties of an object alphabetically. For further information see MapperFeature.SORT_PROPERTIES_ALPHABETICALLY
      • controlFileSource

        public static String controlFileSource
        The default relative path to write control-files to when writeJson is true (default: src/test/resources/).
    • Constructor Detail

      • WritingJsonValidator

        public WritingJsonValidator()
    • Method Detail

      • writeToDiskPlain

        public static File writeToDiskPlain​(String json,
                                            String controlFile)
                                     throws IOException
        Writes the document represented by json to a File.
        Parameters:
        json - a JSON string
        controlFile - the path to the file (relative to controlFileSource )
        Returns:
        the generated File
        Throws:
        IOException - if an I/O error occurs while writing the file
      • validate

        public static void validate​(Action action,
                                    String controlFile)
                             throws IOException
        Validates that the JSON created from the action is equal to the document parsed from the controlFile.
        Parameters:
        action - the Action to create JSON from
        controlFile - the path to the control file (relative to the classpath)
        Throws:
        IOException - if an error occurs while creating the JSON or while reading the control file
      • validate

        public static void validate​(Datasource datasource,
                                    String controlFile)
                             throws IOException
        Validates that the JSON created from the datasource is equal to the document parsed from the controlFile.
        Parameters:
        datasource - the Datasource to create JSON from
        controlFile - the path to the control file (relative to the classpath)
        Throws:
        IOException - if an error occurs while creating the JSON or while reading the control file
      • validate

        public static void validate​(Object object,
                                    String controlFile)
                             throws IOException
        Validates that the JSON created from the object is equal to the document parsed from the controlFile.
        Parameters:
        object - the object to create JSON from
        controlFile - the path to the control file (relative to the classpath)
        Throws:
        IOException - if an error occurs while creating the JSON or while reading the control file
      • validate

        public static void validate​(com.fasterxml.jackson.databind.ObjectMapper objectMapper,
                                    Object object,
                                    String controlFile)
                             throws IOException
        Validates that the JSON created from the object is equal to the document parsed from the controlFile.
        Parameters:
        objectMapper - the custom ObjectMapper to use
        object - the object to create JSON from
        controlFile - the path to the control file (relative to the classpath)
        Throws:
        IOException - if an error occurs while creating the JSON or while reading the control file
      • validate

        public static void validate​(String json,
                                    String controlFile)
                             throws IOException
        Validates that json-string is equal to the document parsed from the controlFile.
        Parameters:
        json - the JSON string
        controlFile - the path to the control file (relative to the classpath)
        Throws:
        IOException - if an error occurs while parsing the JSON string or while reading the control file
      • normalizeLines

        public static String normalizeLines​(String string)
        Normalizes the line breaks of the string to use System.lineSeparator().
        Parameters:
        string - the string to normalize
        Returns:
        the normalized string
      • getControlFile

        public static File getControlFile​(String controlFile)
                                   throws IOException
        Reads the given control file from the classpath
        Parameters:
        controlFile - the path to the control file (relative to the classpath)
        Returns:
        the file
        Throws:
        IOException - if an error occurs while reading the control file
      • toJSON

        public static String toJSON​(Object object)
                             throws IOException
        Maps the given object to a JSON string.
        Parameters:
        object - the object to be mapped
        Returns:
        the JSON mapped from the object
        Throws:
        IOException - if an error occurs while mapping the object to JSON
      • toJSON

        public static String toJSON​(com.fasterxml.jackson.databind.ObjectMapper objectMapper,
                                    Object object)
                             throws IOException
        Maps the given object to a JSON string using the given ObjectMapper.
        Parameters:
        objectMapper - the the custom ObjectMapper to use
        object - the object to be mapped
        Returns:
        the JSON mapped from the object
        Throws:
        IOException - if an error occurs while mapping the object to JSON