java.lang.Object
de.iip_ecosphere.platform.services.environment.DataMapper

public class DataMapper extends Object
Maps data from a stream to input instances for a service. This class is intended as a basis for testing (here avoiding the test scope for generated code). The idea is that all input types are represented as attributes of a generated class (given in terms of a JSON file/stream). The generated service test calls this class providing a consumer to take over the data. As we read JSON through Jackson, currently the fields must comply with camel case Java naming convention irrespective how the fields are written in the generated Java class.
Author:
Holger Eichelberger, SSE
  • Constructor Details

    • DataMapper

      public DataMapper()
  • Method Details

    • createBaseDataUnitClass

      public static <T> Class<? extends T> createBaseDataUnitClass(Class<T> cls)
      Creates a dynamic class extending cls and implementing DataMapper.BaseDataUnitFunctions.
      Type Parameters:
      T - the type of the class
      Parameters:
      cls - the class to extend (assuming it'S a generated data class)
      Returns:
      the created class
    • mapJsonData

      public static <T> void mapJsonData(InputStream stream, Class<T> cls, Consumer<T> cons) throws IOException
      Maps the data in stream to instances of cls, one instance per line. Calls cons per instance/line. Closes stream. Ignores unknown attributes in cls.
      Type Parameters:
      T - the type of data to read
      Parameters:
      stream - the stream to read (may be null for none)
      cls - the type of data to read
      cons - the consumer to be called per instance
      Throws:
      IOException - if I/O or JSON parsing errors occur
    • mapJsonData

      public static <T> void mapJsonData(InputStream stream, Class<T> cls, Consumer<T> cons, boolean failOnUnknownProperties) throws IOException
      Maps the data in stream to instances of cls, one instance per line. Calls cons per instance/line. Closes stream.
      Type Parameters:
      T - the type of data to read
      Parameters:
      stream - the stream to read (may be null for none)
      cls - the type of data to read
      cons - the consumer to be called per instance
      failOnUnknownProperties - whether parsing shall be tolerant or not, the latter may be helpful for debugging
      Throws:
      IOException - if I/O or JSON parsing errors occur
    • mapJsonData

      public static <T> void mapJsonData(InputStream stream, Class<T> cls, Consumer<T> cons, boolean failOnUnknownProperties, Supplier<Boolean> continueFunction) throws IOException
      Maps the data in stream to instances of cls, one instance per line. Calls cons per instance/line. Closes stream.
      Type Parameters:
      T - the type of data to read
      Parameters:
      stream - the stream to read (may be null for none)
      cls - the type of data to read
      cons - the consumer to be called per instance
      failOnUnknownProperties - whether parsing shall be tolerant or not, the latter may be helpful for debugging
      continueFunction - optional function that tells the data mapper to go on reading the input, may be null for none
      Throws:
      IOException - if I/O or JSON parsing errors occur
    • mapJsonDataToIterator

      public static <T> de.iip_ecosphere.platform.support.json.IOIterator<T> mapJsonDataToIterator(InputStream stream, Class<T> cls) throws IOException
      Maps the data in stream to instances of cls, one instance per line, returned in terms of an iterator. Ignores unknown attributes in cls.
      Type Parameters:
      T - the type of data to read
      Parameters:
      stream - the stream to read (may be null for none)
      cls - the type of data to read
      Returns:
      the data iterator
      Throws:
      IOException - if I/O or JSON parsing errors occur
    • mapJsonDataToIterator

      public static <T> de.iip_ecosphere.platform.support.json.IOIterator<T> mapJsonDataToIterator(InputStream stream, Class<T> cls, boolean failOnUnknownProperties) throws IOException
      Maps the data in stream to instances of cls, one instance per line, returned in terms of an iterator. Ignores unknown attributes in cls.
      Type Parameters:
      T - the type of data to read
      Parameters:
      stream - the stream to read (may be null for none)
      cls - the type of data to read
      failOnUnknownProperties - whether parsing shall be tolerant or not, the latter may be helpful for debugging
      Returns:
      the data iterator
      Throws:
      IOException - if I/O or JSON parsing errors occur