## Data dictionaries

Data dictionaries in Citrus provide a new way to manipulate message payload data before a message is sent or received. The dictionary defines a set of keys and respective values. Just like every other dictionary it is used to translate things. In our case we translate message data elements.

You can translate common message elements that are used widely throughout your domain model. As Citrus deals with different types of message data (e.g. XML, JSON) we have different dictionary implementations that are described in the next sections.

### XML data dictionaries

XML data dictionaries do apply to XML message format payloads, of course. In general we add a dictionary to the basic Citrus Spring application context in order to make the dictionary visible to all test cases:

    <citrus:xml-data-dictionary id="nodeMappingDataDictionary">
      <citrus:mappings>
        <citrus:mapping path="TestMessage.MessageId" value="${messageId}"/>
        <citrus:mapping path="TestMessage.CorrelationId" value="${correlationId}"/>
        <citrus:mapping path="TestMessage.User" value="Christoph"/>
        <citrus:mapping path="TestMessage.TimeStamp" value="citrus:currentDate()"/>
      </citrus:mappings>
    </citrus:xml-data-dictionary>

As you can see the dictionary is nothing but a normal Spring bean definition. The NodeMappingDataDictionary implementation receives a map of key value pairs where the key is a message element path expression. For XML payloads the message element tree is traversed so the path expression is built for an exact message element inside the payload. If matched the respective value is set accordingly through the dictionary.

Besides defining the dictionary key value mappings as property map inside the bean definition we can extract the mapping data to an external file.

    <citrus:xml-data-dictionary id="nodeMappingDataDictionary">
      <citrus:mapping-file path="classpath:com/consol/citrus/sample.dictionary"/>
    </citrus:xml-data-dictionary>

The mapping file content just looks like a normal property file in Java:

    TestMessage.MessageId=${messageId}
    TestMessage.CorrelationId=${correlationId}
    TestMessage.User=Christoph
    TestMessage.TimeStamp=citrus:currentDate()

You can set any message element value inside the XML message payload. The path expression also supports XML attributes. Just use the attribute name as last part of the path expression. Let us have a closer look at a sample XML message payload with attributes:

    <TestMessage>
      <User name="Christoph" age="18"/>
    </TestMessage>

With this sample XML payload given we can access the attributes in the data dictionary as follows:

    <citrus:mapping path="TestMessage.User.name" value="${userName}"/>
    <citrus:mapping path="TestMessage.User.age" value="${userAge}"/>

The NodeMappingDataDictionary implementation is easy to use and fits the basic needs for XML data dictionaries. The message element path expressions are very simple and do fit basic needs. However when more complex XML payloads apply for translation we might reach the boundaries here.

For more complex XML message payloads XPath data dictionaries are very effective:

    <citrus:xpath-data-dictionary id="xpathMappingDataDictionary">
      <citrus:mappings>
        <citrus:mapping path="//TestMessage/MessageId" value="${messageId}"/>
        <citrus:mapping path="//TestMessage/CorrelationId" value="${correlationId}"/>
        <citrus:mapping path="//TestMessage/User" value="Christoph"/>
        <citrus:mapping path="//TestMessage/User/@id" value="123"/>
        <citrus:mapping path="//TestMessage/TimeStamp" value="citrus:currentDate()"/>
      </citrus:mappings>
    </citrus:xpath-data-dictionary>

As expected XPath mapping expressions are way more powerful and can also handle very complex scenarios with XML namespaces, attributes and node lists. Just like the node mapping dictionary the XPath mapping dictionary does also support variables, functions and an external mapping file.

XPath works fine with namespaces. In general it is good practice to define a namespace context where you map namespace URI values with prefix values. So your XPath expression is always exact and evaluation is strict. In Citrus the NamespaceContextBuilder which is also added as normal Spring bean to the application context manages namespaces used in your XPath expressions. See our XML and XPAth chapters in this documentation for detailed description how to accomplish fail safe XPath expressions with namespaces.

This completes the XML data dictionary usage in Citrus. Later on we will see some more advanced data dictionary scenarios where we will discuss the usage of dictionary scopes and mapping strategies. But before that let us have a look at other message formats like JSON messages.

### JSON data dictionaries

JSON data dictionaries complement with XML data dictionaries. As usual we have to add the JSON data dictionary to the basic Spring application context first. Once this is done the data dictionary automatically applies for all JSON message payloads in Citrus. This means that all JSON messages sent and received get translated with the JSON data dictionary implementation.

Citrus uses message types in order to evaluate which data dictionary may fit to the message that is currently processed. As usual you can define the message type directly in your test case as attribute inside the sending and receiving message action.

Let us see a simple dictionary for JSON data:

    <citrus:json-data-dictionary id="jsonMappingDataDictionary">
      <citrus:mappings>
        <citrus:mapping path="TestMessage.MessageId" value="${messageId}"/>
        <citrus:mapping path="TestMessage.CorrelationId" value="${correlationId}"/>
        <citrus:mapping path="TestMessage.User" value="Christoph"/>
        <citrus:mapping path="TestMessage.TimeStamp" value="citrus:currentDate()"/>
      </citrus:mappings>
    </citrus:json-data-dictionary>

The message path expressions do look very similar to those used in XML data dictionaries. Here the path expression keys do apply to the JSON object graph. See the following sample JSON data which perfectly applies to the dictionary expressions above.

    {"TestMessage": {
      "MessageId": "1122334455",
      "CorrelationId": "100000001",
      "User": "Christoph",
      "TimeStamp": 1234567890 }
    }

The path expressions will match a very specific message element inside the JSON object graph. The dictionary will automatically set the message element values then. The path expressions are easy to use as you can traverse the JSON object graph very easy.

Of course the data dictionary does also support test variables, functions. Also very interesting is the usage of JSON arrays. A JSON array element is referenced in a data dictionary like this:

    <citrus:mapping path="TestMessage.Users[0]" value="Christoph"/>
      <citrus:mapping path="TestMessage.Users[1]" value="Julia"/>

The Users element is a JSON array, so we can access the elements with index. Nesting JSON objects and arrays is also supported so you can also handle more complex JSON data.

### Dictionary scopes

Now that we have learned how to add data dictionaries to Citrus we need to discuss some advanced topics. Data dictionary scopes do define the boundaries where the dictionary may apply. By default data dictionaries are global scope dictionaries. This means that the data dictionary applies to all messages sent and received with Citrus. Of course message types are considered so XML data dictionaries do only apply to XML message types. However global scope dictionaries will be activated throughout all test cases and actions.

You can overwrite the dictionary scope. For instance in order to use an explicit scope. When this is done the dictionary wil not apply automatically but the user has to explicitly set the data dictionary in sending or receiving test action. This way you can activate the dictionary to a very special set of test actions.

    <citrus:xml-data-dictionary id="specialDataDictionary" global-scope="false">
      <citrus:mapping-file path="classpath:com/consol/citrus/sample.dictionary"/>
    </citrus:xml-data-dictionary>

We set the global scope property to false so the dictionary is handled in explicit scope. This means that you have to set the data dictionary explicitly in your test actions:

XML DSL

    <send endpoint="myEndpoint">
      <message data-dictionary="specialDataDictionary">
        <payload>
          <TestMessage>Hello Citrus"/TestMessage>
        </payload>
      </message>
    </send>

Java DSL designer and runner

    @CitrusTest
    public void dictionaryTest() {
        send(myEndpoint)
            .payload("<TestMessage>Hello Citrus"/TestMessage>")
            .dictionary("specialDataDictionary");
    }

The sample above is a sending test action with an explicit data dictionary reference set. Before sending the message the dictionary is asked for translation. So all matching message element values will be set by the dictionary accordingly. Other global data dictionaries do also apply for this message but the explicit dictionary will always overwrite the message element values.

### Path mapping strategies

Another advanced topic about data dictionaries is the path mapping strategy. When using simple path expressions the default strategy is always EXACT_MATCH . This means that the path expression has to evaluate exactly to a message element within the payload data. And only this exact message element is translated.

You can set your own path mapping strategy in order to change this behavior. For instance another mapping strategy would be STARS_WITH . All elements are translated that start with a certain path expression. Let us clarify this with an example:

    <citrus:xml-data-dictionary id="nodeMappingDataDictionary" mapping-strategy="STARTS_WITH">
      <citrus:mappings>
        <citrus:mapping path="TestMessage.Property" value="citrus:randomString()"/>
      </citrus:mappings>
    </citrus:xml-data-dictionary>

Now with the path mapping strategy set to STARS_WITH all message element path expressions starting with TestMessage.Property will find translation in this dictionary. Following sample message payload would be translated accordingly:

    <TestMessage>
        <Property>XXX</Property>
        <PropertyName>XXX</PropertyName>
        <PropertyValue>XXX</PropertyValue>
      </TestMessage>

All child elements of TestMessage starting with Property will be translated with this data dictionary. In the resulting message payload Citrus will use a random string as value for these elements as we used the citrus:randomString() function in the dictionary mapping.

The next mapping strategy would be ENDS_WITH . No surprises here - this mapping strategy looks for message elements that end with a certain path expression. Again a simple example will clarify this for you.

    <citrus:xml-data-dictionary id="nodeMappingDataDictionary" mapping-strategy="ENDS_WITH">
      <citrus:mappings>
        <citrus:mapping path="Id" value="citrus:randomNumber()"/>
      </citrus:mappings>
    </citrus:xml-data-dictionary>

Again let us see some sample message payload for this dictionary usage:

    <TestMessage>
      <RequestId>XXX</RequestId>
      <Properties>
        <Property>
          <PropertyId>XXX</PropertyId>
          <PropertyValue>XXX</PropertyValue>
        </Property>
        <Property>
          <PropertyId>XXX</PropertyId>
          <PropertyValue>XXX</PropertyValue>
        </Property>
      </Properties>
      </TestMessage>

In this sample all message elements ending with Id would be translated with a random number. No matter where in the message tree the elements are located. This is quite useful but also very powerful. So be careful to use this strategy in global data dictionaries as it may translate message elements that you would not expect in the first place.