Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

Obtaining a Preview of Mapped Data

Anypoint™ DataMapper gives you the ability to preview the result of your data mappings before they are actually implemented. Once data mapping has been configured in a DataMapper building block, generating a preview requires the following steps:

  • Go to DataMapper’s Preview tab

  • Click Run Mapping

Requirements For Generating a Preview

You need to have a configured DataMapper building block in order to map data.

How to obtain a preview will depend on how Anypoint DataMapper is configured. Preview needs to read its input data from a file. If DataMapper is configured to read from an input file, then you can select that same input file to generate the preview from. But if DataMapper is configured to read from Java or from a map or list of maps, then there will be no input file for Preview to read from. In that case, you will need to provide an input file for Preview to work. Both cases are covered in the examples below.

Examples

Using Preview With CSV, XML, JSON Or Excel Input Files

This example was originally provided in Mapping Elements Inside Lists. The example illustrates a simple mapping from an XML file to JSON. You can follow the instructions to create the mapping and obtain the same results as in this document, or you can use this document to obtain a preview of your own mapping.

Once you have your DataMapper building block configured to map data, proceed to obtain the preview by following the steps provided below.

 Click to see the input and output files used in this example

DataMapper’s Preview Tab

Once the DataMapper building block is properly configured, locate the Preview tab in the DataMapper view (highlighted below):

1.preview

Click the Preview tab. The view will be similar to the following:

2.preview

The Input sample data input box defines the name of the input file whose data will be used for the preview. In this example, since the DataMapper building block has already been configured, the input box already contains the name of the input file (which was specified when configuring the DataMapper building block). You can select a different input file by clicking the ellipsis (…​) button to the right of the Input sample data field.

Generating the Preview

Once the input file has been selected, click Run Mapping to generate the preview.

In this example, DataMapper generates a preview of the JSON output file:

3.preview

To see a graphical representation of the mapping, click the Graphic View tab:

4.preview

Using Preview With Java Or Mapping As Input

If your DataMapper building block has been configured to collect input data from Java, or from a map or list of maps, then there will be no input file to enable Preview to work. You will need to supply your own input file. This input file must contain a Groovy script to provide the input data.

In this example, the DataMapper building block has been configured to read input from a Java class called User, and outputs to a JSON file.

 Click to see the “User” Java class and the sample JSON output file used in this example

In this configuration, in order to generate a preview you will need to create an input file with a Groovy script that emulates the input Java class. To do so, complete the following steps:

  1. In the Package Explorer pane under the root folder for your project, locate a suitable directory for the new file. In this example, we will use ${Project_Home}/src/test/resources/.

  2. Right-click the desired directory, then select New, then File.

  3. The New File window pops up. In the File name field, type a name for the new file. For this example, we will use input.groovy.

    2.2.preview

  4. Click OK. Studio automatically opens the new file for editing in a new tab.
    2.3.preview

  5. In the new tab, write the Groovy script suitable for your mapping. For this example, we will use the following:


          
       
1
2
3
4
5
6
import org.mule.test.*;
 
User result = new User();
result.setName("John")
result.setLastName("Doe")
return result

It is now possible to generate a preview by selecting the newly-created file, ${Project_Home}/src/test/resources/input.groovy, in DataMapper’s Preview tab. To do so, complete the following steps:

  1. Go to DataMapper’s Preview tab.

  2. Select the file input.groovy. This can be done in one of three ways:

    1. Drag and drop the file from the Package Explorer pane to the input field beside Input sample data in the Preview, as shown below:

      2.4.preview

    2. Click the ellipsis (…​) button to the right of the Input sample data field, and use the file browser to select the file.

    3. Type the path of the file into the Input sample data input field.

  3. Once the file is selected, click Run Mapping. DataMapper generates the preview of the JSON output file:

    2.5.preview

Input Arguments

For details on Anypoint DataMapper’s Input arguments feature, consult DataMapper - Using Input Arguments.

DataMapper’s Preview feature allows you to incorporate the value of an input argument into the previewed data.

It is not possible to incorporate an input argument itself, since an input argument is a Mule expression, and Preview does not execute Mule expressions but only simulates their results. You must supply the result itself, as a string enclosed in double quotes.

To include the value of an input argument in a DataMapper preview, complete the following steps:

  1. In DataMapper’s Mappings tab, ensure that your input argument is properly mapped to a field in the Output pane.

     Click to see an example of a mapped input argument
  2. In the Preview tab, click Input Arguments next to Run Mapping. The Input arguments window pops up:

    2.7.preview

  3. Click the empty space in the Value column, and type the desired value of your input argument, enclosed in double quotes.
    2.7-2.preview

  4. Click OK.

  5. Click Run Mapping.

The output of the preview should now include the "mock" value of the input argument:

2.9.preview