Friday, September 8, 2017

Mulesoft Dataweave - setting payload fields to null and how to validate with MUnit

This blog post is I will explain how we are using Mulesoft's DataWeave to set a field in the Payload object to null and how to write a Munit test to validate it.

Why would you need to set the field in the payload to null?

The use case this is solving is using Mulesoft to read a CSV file and insert each row as a record into an Oracle database that allows for nullable values on date and number columns. The Mulesoft file input process will cast all fields from the CSV file to string fields in the payload object.  The date fields in the payload object will need to be converted to a Date object prior to the database insert and the number columns will need to be converted if they are null to a null object(Assuming the data is a number).  If this is not done the inserts will fail due to trying to insert an empty string into either a date or number column.

How DataWeave is used to prepare the payload for the insert into Oracle

Here is the snippet of the DataWeave that uses the when/otherwise expression to formats the date field if it is not an empty string and if it is an empty string set the payload field to a null objecct


1
2
3
$.EffectiveDate as :date {format: "M/d/yyyy"} as :string {format: "yyyy-MM-dd"}
    when ($.EffectiveDate != "" )
        otherwise null)

  • This same can be done for number objects minus the formating 
  • This is required due to Mulesoft's file connector converts empty fields to '' when processing a CSV file

How do validate the DataWeave with MUnit

Now we should validate that the objects are indeed null


1
2
3
4
5
6
<munit:test name="dataweave-test-suiteTest-Null-Id" description="Testing that a null Id works">
        <munit:set payload="#[getResource('src/test/resources/id/null_id_test_data.csv').asString()]" mimeType="text/csv" metadata:id="1225baba-0d21-4ddd-87c9-1273f42177c1" doc:name="Set Message"/>
        <flow-ref name="dataweave_set_null_example_sub_flow_transform" doc:name="Flow-ref to dataweave_set_null_example_sub_flow_transform"/>
        <set-payload value="#[payload[0].Id]" doc:name="Set Payload to record 1 Id " doc:description="This is required since there is no good way to assert a value is null in the MUnit framework.  So I take the value I want to check and load it to the payload and then run the Assert Null Payload next"/>
        <munit:assert-null message="Id for record 1 is NOT null" doc:name="Assert Null Payload - Id for record 1 is null"/>
    </munit:test>


  • The set payload uses a resource file that has one empty field to load the payload
  • The Dataweave was moved to a subflow to allow testing isolation and it is called directly from the MUnit test
  • The assertion of the null value is done by taking the payload field that should have the null value and setting the payload to it.  Then the assert-null is used to validate the payload is indeed null
    • This was done due not finding an easy way to check for a null value with MEL


Helpful Links


2 comments:

  1. Excellent article. You have provided very useful information in this article. I have read many articles in various sites but this article gives detailed explanation.
    Mulesoft Online Training
    Mulesoft Training in Hyderabad

    ReplyDelete