Class TestContext

java.lang.Object
org.citrusframework.context.TestContext
All Implemented Interfaces:
TestActionListenerAware, ReferenceResolverAware

public class TestContext extends Object implements ReferenceResolverAware, TestActionListenerAware
Class holding and managing test variables. The test context also provides utility methods for replacing dynamic content(variables and functions) in message payloads and headers.
  • Field Details

    • variables

      protected Map<String,Object> variables
      Local variables
    • timers

      protected Map<String,StopTimer> timers
      Timers registered in test context, that can be stopped
  • Constructor Details

    • TestContext

      public TestContext()
      Default constructor
  • Method Details

    • getVariable

      public String getVariable(String variableExpression)
      Gets the value for the given variable expression. Expression usually is the simple variable name, with optional expression prefix/suffix.

      In case variable is not known to the context throw runtime exception.

      Parameters:
      variableExpression - expression to search for.
      Returns:
      value of the variable
      Throws:
      CitrusRuntimeException
    • getVariable

      public <T> T getVariable(String variableExpression, Class<T> type)
      Gets typed variable value.
      Type Parameters:
      T -
      Parameters:
      variableExpression -
      type -
      Returns:
    • getVariableObject

      public Object getVariableObject(String variableExpression)
      Gets the value for the given variable as object representation. Use this method if you seek for test objects stored in the context.
      Parameters:
      variableExpression - expression to search for.
      Returns:
      value of the variable as object
      Throws:
      CitrusRuntimeException
    • setVariable

      public void setVariable(String variableName, Object value)
      Creates a new variable in this test context with the respective value. In case variable already exists variable is overwritten.
      Parameters:
      variableName - the name of the new variable
      value - the new variable value
      Throws:
      CitrusRuntimeException
    • addVariables

      public void addVariables(String[] variableNames, Object[] variableValues)
      Add variables to context.
      Parameters:
      variableNames - the variable names to set
      variableValues - the variable values to set
    • addVariables

      public void addVariables(Map<String,Object> variablesToSet)
      Add several new variables to test context. Existing variables will be overwritten.
      Parameters:
      variablesToSet - the list of variables to set.
    • resolveDynamicValuesInMap

      public <T> Map<String,T> resolveDynamicValuesInMap(Map<String,T> map)
      Replaces variables and functions inside a map with respective values and returns a new map representation.
      Parameters:
      map - optionally having variable entries.
      Returns:
      the constructed map without variable entries.
    • resolveDynamicValuesInList

      public <T> List<T> resolveDynamicValuesInList(List<T> list)
      Replaces variables and functions in a list with respective values and returns the new list representation.
      Parameters:
      list - having optional variable entries.
      Returns:
      the constructed list without variable entries.
    • resolveDynamicValuesInArray

      public <T> T[] resolveDynamicValuesInArray(T[] array)
      Replaces variables and functions in array with respective values and returns the new array representation.
      Parameters:
      array - having optional variable entries.
      Returns:
      the constructed list without variable entries.
    • clear

      public void clear()
      Clears variables in this test context. Initially adds all global variables.
    • hasVariables

      public boolean hasVariables()
      Checks if variables are present right now.
      Returns:
      boolean flag to mark existence
    • replaceDynamicContentInString

      public String replaceDynamicContentInString(String str)
      Method replacing variable declarations and place holders as well as function expressions in a string
      Parameters:
      str - the string to parse.
      Returns:
      resulting string without any variable place holders.
    • replaceDynamicContentInString

      public String replaceDynamicContentInString(String str, boolean enableQuoting)
      Method replacing variable declarations and functions in a string, optionally the variable values get surrounded with single quotes.
      Parameters:
      str - the string to parse for variable place holders.
      enableQuoting - flag marking surrounding quotes should be added or not.
      Returns:
      resulting string without any variable place holders.
    • resolveDynamicValue

      public String resolveDynamicValue(String expression)
      Checks weather the given expression is a variable or function and resolves the value accordingly
      Parameters:
      expression - the expression to resolve
      Returns:
      the resolved expression value
    • handleError

      public CitrusRuntimeException handleError(String testName, String packageName, String message, Exception cause)
      Handles error creating a new CitrusRuntimeException and informs test listeners.
      Parameters:
      testName -
      packageName -
      message -
      cause -
      Returns:
    • setVariables

      public void setVariables(Map<String,Object> variables)
      Setter for test variables in this context.
      Parameters:
      variables -
    • getVariables

      public Map<String,Object> getVariables()
      Getter for test variables in this context.
      Returns:
      test variables for this test context.
    • setGlobalVariables

      public void setGlobalVariables(GlobalVariables globalVariables)
      Copies the passed globalVariables and adds them to the test context.
      If any of the copied global variables contain dynamic content (references to other global variables or functions) then this is resolved now. As a result it is important setFunctionRegistry(FunctionRegistry) is called first before calling this method.
      Parameters:
      globalVariables -
    • getGlobalVariables

      public Map<String,Object> getGlobalVariables()
      Set global variables.
      Returns:
      the globalVariables
    • setMessageStore

      public void setMessageStore(MessageStore messageStore)
      Sets the messageStore property.
      Parameters:
      messageStore -
    • getMessageStore

      public MessageStore getMessageStore()
      Gets the value of the messageStore property.
      Returns:
      the messageStore
    • getFunctionRegistry

      public FunctionRegistry getFunctionRegistry()
      Get the current function registry.
      Returns:
      the functionRegistry
    • setFunctionRegistry

      public void setFunctionRegistry(FunctionRegistry functionRegistry)
      Set the function registry.
      Parameters:
      functionRegistry - the functionRegistry to set
    • setMessageValidatorRegistry

      public void setMessageValidatorRegistry(MessageValidatorRegistry messageValidatorRegistry)
      Set the message validator registry.
      Parameters:
      messageValidatorRegistry - the messageValidatorRegistry to set
    • getMessageValidatorRegistry

      public MessageValidatorRegistry getMessageValidatorRegistry()
      Get the message validator registry.
      Returns:
      the messageValidatorRegistry
    • getValidationMatcherRegistry

      public ValidationMatcherRegistry getValidationMatcherRegistry()
      Get the current validation matcher registry
      Returns:
    • setValidationMatcherRegistry

      public void setValidationMatcherRegistry(ValidationMatcherRegistry validationMatcherRegistry)
      Set the validation matcher registry
      Parameters:
      validationMatcherRegistry -
    • getMessageListeners

      public MessageListeners getMessageListeners()
      Gets the message listeners.
      Returns:
    • setMessageListeners

      public void setMessageListeners(MessageListeners messageListeners)
      Set the message listeners.
      Parameters:
      messageListeners -
    • getTestListeners

      public TestListeners getTestListeners()
      Gets the test listeners.
      Returns:
    • setTestListeners

      public void setTestListeners(TestListeners testListeners)
      Set the test listeners.
      Parameters:
      testListeners -
    • getTestActionListeners

      public TestActionListeners getTestActionListeners()
      Obtains the testActionListeners.
      Returns:
    • setTestActionListeners

      public void setTestActionListeners(TestActionListeners testActionListeners)
      Specifies the testActionListeners.
      Parameters:
      testActionListeners -
    • addTestActionListener

      public void addTestActionListener(TestActionListener listener)
      Description copied from interface: TestActionListenerAware
      Adds a new test action listener.
      Specified by:
      addTestActionListener in interface TestActionListenerAware
    • getBeforeTest

      public List<BeforeTest> getBeforeTest()
      Obtains the beforeTest.
      Returns:
    • setBeforeTest

      public void setBeforeTest(List<BeforeTest> beforeTest)
      Specifies the beforeTest.
      Parameters:
      beforeTest -
    • getAfterTest

      public List<AfterTest> getAfterTest()
      Obtains the afterTest.
      Returns:
    • setAfterTest

      public void setAfterTest(List<AfterTest> afterTest)
      Specifies the afterTest.
      Parameters:
      afterTest -
    • getSegmentVariableExtractorRegistry

      public SegmentVariableExtractorRegistry getSegmentVariableExtractorRegistry()
      Obtains the segmentVariableExtractorRegistry
      Returns:
    • setSegmentVariableExtractorRegistry

      public void setSegmentVariableExtractorRegistry(SegmentVariableExtractorRegistry segmentVariableExtractorRegistry)
      Specifies the segmentVariableExtractorRegistry
      Parameters:
      segmentVariableExtractorRegistry -
    • getMessageProcessors

      public List<MessageProcessor> getMessageProcessors(MessageDirection direction)
      Gets the global message processors for given direction.
      Returns:
    • getMessageProcessors

      public MessageProcessors getMessageProcessors()
      Gets the global message processors.
      Returns:
    • setMessageProcessors

      public void setMessageProcessors(MessageProcessors messageProcessors)
      Sets the global message processors.
      Parameters:
      messageProcessors -
    • getEndpointFactory

      public EndpointFactory getEndpointFactory()
      Gets the endpoint factory.
      Returns:
    • setEndpointFactory

      public void setEndpointFactory(EndpointFactory endpointFactory)
      Sets the endpoint factory.
      Parameters:
      endpointFactory -
    • getReferenceResolver

      public ReferenceResolver getReferenceResolver()
      Gets the value of the referenceResolver property.
      Returns:
      the referenceResolver
    • setReferenceResolver

      public void setReferenceResolver(ReferenceResolver referenceResolver)
      Description copied from interface: ReferenceResolverAware
      Sets the reference resolver.
      Specified by:
      setReferenceResolver in interface ReferenceResolverAware
    • setNamespaceContextBuilder

      public void setNamespaceContextBuilder(NamespaceContextBuilder namespaceContextBuilder)
      Sets the namespace context builder.
      Parameters:
      namespaceContextBuilder -
    • getNamespaceContextBuilder

      public NamespaceContextBuilder getNamespaceContextBuilder()
      Gets the namespace context builder.
      Returns:
    • getTypeConverter

      public TypeConverter getTypeConverter()
      Obtains the typeConverter.
      Returns:
    • setTypeConverter

      public void setTypeConverter(TypeConverter typeConverter)
      Specifies the typeConverter.
      Parameters:
      typeConverter -
    • getLogModifier

      public LogModifier getLogModifier()
      Gets the logModifier.
      Returns:
    • setLogModifier

      public void setLogModifier(LogModifier logModifier)
      Sets the logModifier.
      Parameters:
      logModifier -
    • onInboundMessage

      public void onInboundMessage(Message receivedMessage)
      Informs message listeners if present that inbound message was received.
      Parameters:
      receivedMessage -
    • onOutboundMessage

      public void onOutboundMessage(Message message)
      Informs message listeners if present that new outbound message is about to be sent.
      Parameters:
      message -
    • registerTimer

      public void registerTimer(String timerId, StopTimer timer)
      Registers a StopTimer in the test context, so that the associated timer can be stopped later on.
      Parameters:
      timerId - a unique timer id
    • stopTimer

      public boolean stopTimer(String timerId)
      Stops the timer matching the supplied id
      Parameters:
      timerId -
      Returns:
      true if time found and stopped, matching the supplied timerId
    • stopTimers

      public void stopTimers()
      Stops all timers
    • addException

      public void addException(CitrusRuntimeException exception)
      Add new exception to the context marking the test as failed. This is usually used by actions to mark exceptions during forked operations.
      Parameters:
      exception -
    • getExceptions

      public List<CitrusRuntimeException> getExceptions()
      Gets the value of the exceptions property.
      Returns:
      the exceptions
    • hasExceptions

      public boolean hasExceptions()
      Gets exception collection state.
      Returns:
    • isSuccess

      public boolean isSuccess(TestResult testResult)
      Checks test result success in combination with this context exception state.
      Parameters:
      testResult -
      Returns: