Package com.consol.citrus.context
Class TestContext
- java.lang.Object
-
- com.consol.citrus.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.
-
-
Constructor Summary
Constructors Constructor Description TestContext()Default constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddException(CitrusRuntimeException exception)Add new exception to the context marking the test as failed.voidaddTestActionListener(TestActionListener listener)Adds a new test action listener.voidaddVariables(String[] variableNames, Object[] variableValues)Add variables to context.voidaddVariables(Map<String,Object> variablesToSet)Add several new variables to test context.voidclear()Clears variables in this test context.List<AfterTest>getAfterTest()Obtains the afterTest.List<BeforeTest>getBeforeTest()Obtains the beforeTest.EndpointFactorygetEndpointFactory()Gets the endpoint factory.List<CitrusRuntimeException>getExceptions()Gets the value of the exceptions property.FunctionRegistrygetFunctionRegistry()Get the current function registry.Map<String,Object>getGlobalVariables()Set global variables.LogModifiergetLogModifier()Gets the logModifier.MessageListenersgetMessageListeners()Gets the message listeners.MessageProcessorsgetMessageProcessors()Gets the global message processors.List<MessageProcessor>getMessageProcessors(MessageDirection direction)Gets the global message processors for given direction.MessageStoregetMessageStore()Gets the value of the messageStore property.MessageValidatorRegistrygetMessageValidatorRegistry()Get the message validator registry.NamespaceContextBuildergetNamespaceContextBuilder()Gets the namespace context builder.ReferenceResolvergetReferenceResolver()Gets the value of the referenceResolver property.SegmentVariableExtractorRegistrygetSegmentVariableExtractorRegistry()Obtains the segmentVariableExtractorRegistryTestActionListenersgetTestActionListeners()Obtains the testActionListeners.TestListenersgetTestListeners()Gets the test listeners.TypeConvertergetTypeConverter()Obtains the typeConverter.ValidationMatcherRegistrygetValidationMatcherRegistry()Get the current validation matcher registryStringgetVariable(String variableExpression)Gets the value for the given variable expression.<T> TgetVariable(String variableExpression, Class<T> type)Gets typed variable value.ObjectgetVariableObject(String variableExpression)Gets the value for the given variable as object representation.Map<String,Object>getVariables()Getter for test variables in this context.CitrusRuntimeExceptionhandleError(String testName, String packageName, String message, Exception cause)Handles error creating a new CitrusRuntimeException and informs test listeners.booleanhasExceptions()Gets exception collection state.booleanhasVariables()Checks if variables are present right now.booleanisSuccess(TestResult testResult)Checks test result success in combination with this context exception state.voidonInboundMessage(Message receivedMessage)Informs message listeners if present that inbound message was received.voidonOutboundMessage(Message message)Informs message listeners if present that new outbound message is about to be sent.voidregisterTimer(String timerId, StopTimer timer)Registers a StopTimer in the test context, so that the associated timer can be stopped later on.StringreplaceDynamicContentInString(String str)Method replacing variable declarations and place holders as well as function expressions in a stringStringreplaceDynamicContentInString(String str, boolean enableQuoting)Method replacing variable declarations and functions in a string, optionally the variable values get surrounded with single quotes.StringresolveDynamicValue(String expression)Checks weather the given expression is a variable or function and resolves the value accordingly<T> T[]resolveDynamicValuesInArray(T[] array)Replaces variables and functions in array with respective values and returns the new array representation.<T> List<T>resolveDynamicValuesInList(List<T> list)Replaces variables and functions in a list with respective values and returns the new list representation.<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.voidsetAfterTest(List<AfterTest> afterTest)Specifies the afterTest.voidsetBeforeTest(List<BeforeTest> beforeTest)Specifies the beforeTest.voidsetEndpointFactory(EndpointFactory endpointFactory)Sets the endpoint factory.voidsetFunctionRegistry(FunctionRegistry functionRegistry)Set the function registry.voidsetGlobalVariables(GlobalVariables globalVariables)Copies the passedglobalVariablesand adds them to the test context.voidsetLogModifier(LogModifier logModifier)Sets the logModifier.voidsetMessageListeners(MessageListeners messageListeners)Set the message listeners.voidsetMessageProcessors(MessageProcessors messageProcessors)Sets the global message processors.voidsetMessageStore(MessageStore messageStore)Sets the messageStore property.voidsetMessageValidatorRegistry(MessageValidatorRegistry messageValidatorRegistry)Set the message validator registry.voidsetNamespaceContextBuilder(NamespaceContextBuilder namespaceContextBuilder)Sets the namespace context builder.voidsetReferenceResolver(ReferenceResolver referenceResolver)Sets the reference resolver.voidsetSegmentVariableExtractorRegistry(SegmentVariableExtractorRegistry segmentVariableExtractorRegistry)Specifies the segmentVariableExtractorRegistryvoidsetTestActionListeners(TestActionListeners testActionListeners)Specifies the testActionListeners.voidsetTestListeners(TestListeners testListeners)Set the test listeners.voidsetTypeConverter(TypeConverter typeConverter)Specifies the typeConverter.voidsetValidationMatcherRegistry(ValidationMatcherRegistry validationMatcherRegistry)Set the validation matcher registryvoidsetVariable(String variableName, Object value)Creates a new variable in this test context with the respective value.voidsetVariables(Map<String,Object> variables)Setter for test variables in this context.booleanstopTimer(String timerId)Stops the timer matching the supplied idvoidstopTimers()Stops all timers
-
-
-
Method Detail
-
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 variablevalue- the new variable value- Throws:
CitrusRuntimeException
-
addVariables
public void addVariables(String[] variableNames, Object[] variableValues)
Add variables to context.- Parameters:
variableNames- the variable names to setvariableValues- 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 passedglobalVariablesand 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 importantsetFunctionRegistry(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:TestActionListenerAwareAdds a new test action listener.- Specified by:
addTestActionListenerin interfaceTestActionListenerAware
-
getBeforeTest
public List<BeforeTest> getBeforeTest()
Obtains the beforeTest.- Returns:
-
setBeforeTest
public void setBeforeTest(List<BeforeTest> beforeTest)
Specifies the beforeTest.- Parameters:
beforeTest-
-
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:ReferenceResolverAwareSets the reference resolver.- Specified by:
setReferenceResolverin interfaceReferenceResolverAware
-
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:
-
-