Class ExpectedOutputReceiver

java.lang.Object
org.opentestfactory.test.harness.ExpectedOutputReceiver

public class ExpectedOutputReceiver extends Object
This class may be used to define expectations on output messages from a given service. When expectations are setup, call the system under test THEN call waitAndVerifyExpectedCall(Duration) on your ExpectedOutputReceiver instance to check if the expected messages where received (and optionally, check that no unwanted message was received).
  • Constructor Details

    • ExpectedOutputReceiver

      public ExpectedOutputReceiver(org.mockserver.integration.ClientAndServer mockServer)
      Create an original receiver instance based on a given mock server.
      Parameters:
      mockServer - the server object to use.
    • ExpectedOutputReceiver

      public ExpectedOutputReceiver(org.mockserver.integration.ClientAndServer mockServer, List<org.mockserver.model.HttpRequest> expectedRequests, List<org.mockserver.model.HttpRequest> unwantedRequests, JsonVariableMappings variableMappings, boolean ignoreArrayOrder)
      Create a complete expectation definition.
      Parameters:
      mockServer - the mock server instance to use.
      expectedRequests - a List of expected HttpRequests : the receiver will verify that it got each request at least once after the wait delay.
      unwantedRequests - a List of unwanted HttpRequests : the receiver will chaque that it was sent NONE of these requests after the wait delay.
      variableMappings - a JsonVariableMappings describing key-value variableMappings to replace in JSON request templates.
      See Also:
  • Method Details

    • basedOnMock

      public static ExpectedOutputReceiver basedOnMock(org.mockserver.integration.ClientAndServer mockServer)
      Static factory method to create an initial based on the given mock server. This is for use in combination with the withExpectedRequests(HttpRequest...) and withUnwantedRequests(HttpRequest...) to build a configured receiver instance.
      Parameters:
      mockServer - the mock server to use.
      Returns:
      the instance.
    • withVariableMapping

      public ExpectedOutputReceiver withVariableMapping(String key, String value)
      Add variable mappings to build expected requests. the variable must be inserted on its own line as #{key} as the value of a json property. The mapping must appear on its own line in the template, like this (if there is no other property after it): "property": #{key} or like this (if there is a property after it): "property": #{key},
      Parameters:
      key - the key to replace. This must be an az-AZ-09+ string.
      value - the textual value by which to replace the variable construct.
      Returns:
      the new ExpectedOutputReceiver instance.
    • withMockAttachment

      public ExpectedOutputReceiver withMockAttachment(String attachmentResourceName) throws IOException, URISyntaxException
      Tells the mockserver to serve said resource. A mapping will be added for envelope substitution (the mappng key will be base on the resource name with slashes replaced by underscores, the value the exposed URL).
      Parameters:
      attachmentResourceName - the resource name. If this matchs no known resource, an UnknownTestResource exception will be thrown.
      Returns:
      the new receiver with a mapping for the resource URL.
      Throws:
      IOException - if resource access fails (may happen)
      URISyntaxException - if the computed attachemnt URI is invalid (should NEVER happen)
    • withExpectedRequestTemplate

      public ExpectedOutputReceiver withExpectedRequestTemplate(String path, InputStream bodyStream) throws IOException
      Throws:
      IOException
    • withExpectedRequests

      public ExpectedOutputReceiver withExpectedRequests(org.mockserver.model.HttpRequest... expectedRequests)
    • withUnwantedRequests

      public ExpectedOutputReceiver withUnwantedRequests(org.mockserver.model.HttpRequest... unwantedRequests)
    • waitAndVerifyExpectedCall

      public ExpectedOutputReceiver waitAndVerifyExpectedCall(Duration delay) throws InterruptedException
      Wait some time to give time for callbacks, then verify if expected requests were received, and check that no unwanted request came.
      Parameters:
      delay - the base delay. A duration factor is applied based on TestConfiguration.getDurationFactorPercent().
      Returns:
      Throws:
      InterruptedException
      See Also:
    • withIgnoreArrayElementOrder

      public ExpectedOutputReceiver withIgnoreArrayElementOrder()
      Create a receiver that ignores array element order. The default behavior is to check for array element order.
      Returns:
    • mappings

      public JsonVariableMappings mappings()