An example of XML payload testing - 6.2

Talend MDM Platform Studio User Guide

EnrichVersion
6.2
EnrichProdName
Talend MDM Platform
task
Data Governance
Data Quality and Preparation
Design and Development
EnrichPlatform
Talend Studio

This section will show you an example to test a Route that processes XML messages. The following sample XML file is used as the message source:

<Customers>
	<Customer>
		<Number>1</Number>
		<FirstName>Fred</FirstName>
		<LastName>Landis</LastName>
		<Address>
			<Street>Oakstreet</Street>
			<City>Boston</City>
			<ZIP>23320</ZIP>
			<State>MA</State>
		</Address>
	</Customer>
	<Customer>
		<Number>2</Number>
		<FirstName>Michelle</FirstName>
		<LastName>Butler</LastName>
		<Address>
			<Street>First Avenue</Street>
			<City>San-Francisco</City>
			<ZIP>44324</ZIP>
			<State>CA</State>
		</Address>
	</Customer>
	<Customer>
		<Number>3</Number>
		<FirstName>Ted</FirstName>
		<LastName>Little</LastName>
		<Address>
			<Street>Long Way</Street>
			<City>Los-Angeles</City>
			<ZIP>34424</ZIP>
			<State>CA</State>
		</Address>
	</Customer>
</Customers>

Building the Route

  1. From the Palette, drag and drop cFile, a cConvertBodyTo, a cSetbody and a cLog components onto the design workspace.

  2. Link the components using the Row > Route connection.

  3. Double-click the cFile component to open its Basic settings view in the Component tab.

  4. In the Path field, enter the path of the message source.

    In the fileName field, enter the name of the message source.

  5. Double-click the cConvertBodyTo component to open its Basic settings view in the Component tab.

  6. In the Target Class Name field, enter your target class name, String.class in this scenario.

  7. Double-click the cSetBody component to open its Basic settings view in the Component tab.

  8. From the Language list, select XPATH.

    In the Expression field, type in "/Customers/Customer[Address/State='MA']" to extract the customer whose State is MA.

  9. Keep the default settings of the cLog to log the message exchanges.

  10. Press Ctrl+S to save your Route.

Testing the Route

Creating the test case

  1. Select the cConvertBodyTo and cSetbody component in the Route. Right-click any of the selected components and select Create Test Case from the contextual menu.

  2. The [Create Route Test Case] wizard opens. Enter a name for the test case in the Name field, and the purpose and description in the corresponding fields. Click Next.

  3. The [Define Test Skeleton details] view of the wizard opens, select the options as shown below to use a cDataset as the message producer, and a cMock to check the result. Click Finish.

  4. The test case is then created and opened in the design workspace. Now it looks like:

  5. Add a cMock, cConfig and a cProcessor to the Route by typing the name of the component directly in the design workspace.

Configuring the test case

  1. Double-click the cConfig component to open its Basic settings view in the Component tab.

  2. In the Dependencies list, add the xmlunit-1.6.jar, which can be obtained from the site http://www.xmlunit.org/. Note that you need to add this dependency when testing XML messages. It helps to validate the content of the XML messages against the schema.

  3. Double-click the cProcessor component to open its Basic settings view in the Component tab.

  4. In the Import area, enter the following code to import the external libraries:

    import org.custommonkey.xmlunit.Diff;
    import org.custommonkey.xmlunit.XMLUnit;
    import java.io.FileReader;
    import java.io.File;

    In the Code area, enter the following code to compare the received XML file and the target XML file and return identical or different as the result.

    String in_xml = exchange.getIn().getBody(String.class);
    
    FileReader reference_file1_reader = new FileReader(new File(context.reference_file1));
    
    Diff diff = XMLUnit.compareXML(in_xml, reference_file1_reader);
    
    exchange.getIn().setBody(diff.identical() ? "identical" : "different");
  5. Double-click the first cDataset component to open its Basic settings view in the Component tab.

  6. In the Arguments table, the row with the value context.input_file1 has been added automatically. Add another row "----------------" to treat the input as one message.

    Note that when using the cDataset as the message producer, if the input message contains multiple lines, you need to add a line separator in the Basic settings of the cDataset. The line separator must be the same as the split line in the input file so that cDataset will split each message and process testing correctly. In case there is only one multiple-line message in the input file, you still need to set the line separator in the Basic settings of the cDataset so that it can be treated as one message. If the line separator is not specified, each line from the input will be treated as one message.

  7. Double-click the first cMock component to open its Basic settings view in the Component tab.

  8. Select the Simulate check box and select the cProcessor_1 to produce the message.

  9. Double-click the second cMock component to open its Basic settings view in the Component tab.