DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Kubernetes in the Enterprise: The latest expert insights on scaling, serverless, Kubernetes-powered AI, cluster security, FinOps, and more.

A Guide to Continuous Integration and Deployment: Learn the fundamentals and understand the use of CI/CD in your apps.

Trending

  • Comprehensive Guide on Integrating OpenAI ChatGPT API With React JS
  • Extend Your GPTs With C#
  • Unlocking Clean Code: 20 Architectural Tips for Every Developer
  • A Guide To Growing Your Technical Knowledge Portfolio

Testing Dataweave With MUnit

In this post, we take a look at how to configure and test your Dataweave app using Mulsesoft's MUnit tool. Read on for more detail!

Satheesh Kumar user avatar by
Satheesh Kumar
·
Updated Jan. 04, 18 · Tutorial
Like (7)
Save
Tweet
Share
12.5K Views

Join the DZone community and get the full member experience.

Join For Free

Its easier to test data weave when developing the code using the previewer, but when you are moving the code into CI/CD we need an automated testing where Munit comes into the picture. Most developers use an entire flow to test Dataweave, but this is not the recommended way.

Many developers are not using one of the best-hidden features of Dataweave, which is referring a dwl file from resources.

The advantages of using a separate dwl file are:

  1. Cleaner XML files.

  2. Reusability.

  3. Easy testing.

In this article, I am going to show you how to test Dataweave alone, using a simple example.

{
 "Name":"satheesh",
 "Role":"Engineer",
 "ID":"12343534"
  }

This JSON needs to be converted to

{
 "Name":"satheesh",
  "ID":"12343534",
 "Email":"sath1234@abc.com"

}

The Dataweave code for this should look like what I've got below:

%dw 1.0
%output application/json
---
{
Name: payload.Name,
ID: payload.ID,
Email: payload.Name[0..3] ++ payload.ID[0..3] ++ "@abc.com"
}

Now let create a new dwl file under src/main/resources/mapping

Image title


Add the Dataweave code in the file and refer to the file in the transform message, as shown below.

Image title

Now your config.xml will look cleaner and the Dataweave file can be used in other transformations too.

 <flow name="sftp-testFlow">
        <sftp:inbound-endpoint connector-ref="SFTP" host="xxxxx" port="22" path="xxxxxxx" user="santonyraj" password="yyyyyyy" responseTimeout="10000" doc:name="SFTP"/>
        <byte-array-to-string-transformer doc:name="Byte Array to String"/>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload resource="classpath:mapping/sample.dwl"/>
        </dw:transform-message>
    </flow>

Now let's see how to test the file using Munit:

  1. Create an MUnit test suite.

  2. Use a set Message to set your payload.

  3. Use a transform message and refer your dwl file (Don't use UI to refer the file as it will overwrite the original one. Instead, use the xml and add <dw:set-payload resource="classpath:mapping/sample.dwl"/> in the transform message).

  4. Use assert payload to check your output.

Image title

The config.xml should look like this:

    <munit:test name="dataweave-test" description="Test">
        <munit:set payload="#[getResource('input.json').asString()]" doc:name="Set Message"/>
        <dw:transform-message doc:name="Transform Message">
        <dw:set-payload resource="classpath:mapping/sample.dwl"/>
        </dw:transform-message>
        <munit:assert-payload-equals expectedValue="#[getResource('output.json').asString()]" doc:name="Assert Payload"/>
    </munit:test>

Image title

In this way, all the mapping can be tested.

Opinions expressed by DZone contributors are their own.


Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: