org.appng.testsupport.validation

## Class WritingJsonValidator

- java.lang.Object
- - org.appng.testsupport.validation.WritingJsonValidator

-     public class WritingJsonValidator
      extends Object

  Utility class that supports testing if two JSON documents have the same content.
  Author:
    Matthias Müller

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `WritingJsonValidator.JsonWrapper` A wrapper class for `Action`s and `Datasource`s

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static String` | `controlFileSource` The default relative path to write control-files to when `writeJson` is `true` (default: `src/test/resources/`).
    `static boolean` | `logJson` Set to `true` to log the actual JSON document (debug level) when validating (default `false`).
    `static boolean` | `sortPropertiesAlphabetically` Set to `true` to let the object mapper sort the properties of an object alphabetically.
    `static boolean` | `writeJson` Set to `true` to (over)write the control-files on (default `false`) (see also `controlFileSource`).

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `WritingJsonValidator()`

  - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static File` | `getControlFile(String controlFile)` Reads the given control file from the classpath
    `static String` | `normalizeLines(String string)` Normalizes the line breaks of the string to use `System.lineSeparator()`.
    `static String` | `toJSON(Object object)` Maps the given `object` to a JSON string.
    `static String` | `toJSON(com.fasterxml.jackson.databind.ObjectMapper objectMapper, Object object)` Maps the given `object` to a JSON string using the given `ObjectMapper`.
    `static void` | `validate(Action action, String controlFile)` Validates that the JSON created from the `action` is equal to the document parsed from the `controlFile`.
    `static void` | `validate(Datasource datasource, String controlFile)` Validates that the JSON created from the `datasource` is equal to the document parsed from the `controlFile`.
    `static void` | `validate(com.fasterxml.jackson.databind.ObjectMapper objectMapper, Object object, String controlFile)` Validates that the JSON created from the `object` is equal to the document parsed from the `controlFile`.
    `static void` | `validate(Object object, String controlFile)` Validates that the JSON created from the `object` is equal to the document parsed from the `controlFile`.
    `static void` | `validate(String json, String controlFile)` Validates that `json`-string is equal to the document parsed from the `controlFile`.
    `static File` | `writeToDisk(Object data, String name)` Writes the document represented by `data` to a `File`.
    `static File` | `writeToDiskPlain(String json, String controlFile)` Writes the document represented by `json` to a `File`.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### 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

    - #### writeToDisk

          public static File writeToDisk(Object data,
                                         String name)
                                  throws IOException

      Writes the document represented by `data` to a `File`.
      Parameters:
        `data` - a `WritingJsonValidator.JsonWrapper`
        `name` - the path to the file (relative to `controlFileSource` )
      Returns:
        the generated `File`
      Throws:
        `IOException` - if an I/O error occurs while writing the file

    - #### 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

Copyright © 2011–2023 aiticon GmbH. All rights reserved.