java.lang.Object
test.de.iip_ecosphere.platform.services.environment.metricsProvider.utils.TestUtils

public class TestUtils extends Object
Class to avoid repetition of code in testing environment.
This class extracts a JsonObject from a resource and has some timed methods.
Author:
Miguel Gomez
  • Field Details

  • Constructor Details

    • TestUtils

      public TestUtils()
  • Method Details

    • readJsonFromResources

      public static de.iip_ecosphere.platform.support.json.JsonObject readJsonFromResources(String folder, String filename) throws IOException
      Extracts a JsonObject from a resource.
      The resource will be nested in a directory nested under the directory jsonsamples in the resource directory.
      Parameters:
      folder - name of the directory where the resource is found
      filename - name of the file containing the resource
      Returns:
      JSON representation of the resource
      Throws:
      IOException - if the file cannot be found or an error occurs when opening the stream
    • oneSecondRunnable

      public static void oneSecondRunnable()
      Runs/waits for one second.
    • twoSecondRunnable

      public static void twoSecondRunnable()
      Runs/waits for two seconds.
    • threeSecondRunnable

      public static void threeSecondRunnable()
      Runs/waits for three seconds.
    • oneSecondSupplier

      public static String oneSecondSupplier()
      Runs/waits for one second and returns DATA.
      Returns:
      DATA
    • twoSecondSupplier

      public static String twoSecondSupplier()
      Runs/waits for two seconds and returns DATA.
      Returns:
      DATA
    • threeSecondSupplier

      public static String threeSecondSupplier()
      Runs/waits for three seconds and returns DATA.
      Returns:
      DATA
    • assertThrows

      public static <T extends Throwable> T assertThrows(Class<T> expectedThrowable, ThrowingRunnable runnable)
      Asserts that runnable throws an exception of type expectedThrowable when executed. If it does, the exception object is returned. If it does not throw an exception, an AssertionError is thrown. If it throws the wrong type of exception, an AssertionError is thrown describing the mismatch; the exception that was actually thrown can be obtained by calling Throwable.getCause().
      Type Parameters:
      T - throwable type
      Parameters:
      expectedThrowable - the expected type of the exception
      runnable - a function that is expected to throw an exception when executed
      Returns:
      the exception thrown by runnable
    • assertThrows

      public static <T extends Throwable> T assertThrows(String message, Class<T> expectedThrowable, ThrowingRunnable runnable)
      Asserts that runnable throws an exception of type expectedThrowable when executed. If it does, the exception object is returned. If it does not throw an exception, an AssertionError is thrown. If it throws the wrong type of exception, an AssertionError is thrown describing the mismatch; the exception that was actually thrown can be obtained by calling Throwable.getCause().
      Type Parameters:
      T - throwable type
      Parameters:
      message - the identifying message for the AssertionError (null okay)
      expectedThrowable - the expected type of the exception
      runnable - a function that is expected to throw an exception when executed
      Returns:
      the exception thrown by runnable
    • buildPrefix

      private static String buildPrefix(String message)
      Builds a message prefix.
      Parameters:
      message - the message
      Returns:
      the message with prefix
    • formatClass

      private static String formatClass(Class<?> value)
      Formats the given class name.
      Parameters:
      value - the class
      Returns:
      the formatted class name
    • format

      static String format(String message, Object expected, Object actual)
      Formats a message with given expected and actual value.
      Parameters:
      message - the message
      expected - the expected object
      actual - the actual object
      Returns:
      the message
    • formatClassAndValue

      private static String formatClassAndValue(Object value, String valueString)
      Formats a class name including value.
      Parameters:
      value - the class/object value
      valueString - the value
      Returns:
      the formatted string
    • equalsRegardingNull

      private static boolean equalsRegardingNull(Object expected, Object actual)
      Returns whether expected and actual are considered equal regarding null.
      Parameters:
      expected - the expected value
      actual - the actual value
      Returns:
      true for equal, false else
    • isEquals

      private static boolean isEquals(Object expected, Object actual)
      Returns whether expected and actual are considered equal.
      Parameters:
      expected - the expected value
      actual - the actual value
      Returns:
      true for equal, false else