Mulesoft Murmurings

Introduction


This blog is a series of tips, tricks and how to's relating to developing APIs in Mulesoft Anypoint Studio.




MEL Expressions

Create a boolean IF THEN statement

In MEL expression, the creates an if statement based on the expression before it.
#[flowVars.flowName == empty ? "updateCustomer"]
To add a else clause, add after setting the if condition.
#[flowVars.flowName == empty ? "updateCustomer" :  flowVars.flowName + "->updateCustomer"]

How to's 

Add time taken to a http flow

1.) Add a variable component at the beginning of the flow.
2.) Set the Value to  #[System.currentTimeTimeMillis()]
3.) Set the Name to 'flowName'+StartTime. (FLOWNAME MUST BE UNIQUE)
4.) Add a logger component at the end of the flow.
5.) Set Value  #[timeTaken=#[System.currentTimeMillis() - flowVars.flownameStartTime]]
NOTE: FlowName variable cannot contain - (dash) or _ (underscore), or it will give a 500 error when the API is called.

Choice Exception Strategy Setup

To correctly check which exception has been raised in a choice exception routing strategy:
#[exception.causedBy(org.mule.module.apikit.exception.NotFoundException)]

Mock Salesforce data for MUNIT Test

Gotcha!: If you have multiple salesforce connector in a single mule-configuration file, be sure each one has a unique name! Otherwise the test will execute all flows that contain a salesforce connector with the name in the MUnit test.
1.) Create a Unit test, adding a mock , set message etc.
2.) create a new Java source file under /src/test/java. (You can create a package if you want)
3.) create a new public class as shown below:

public class MockDataHelper {
    public static Map<String,Object> mockSalesforceAccount(){
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("Id""a2tO0000003fmNXIAY");
        data.put("CustomerId__c""ABC1234567");
        data.put("CreatedDate","01-03-2018");
        return data;
    }

Mock JMS Connector

To prevent a JMS connector from trying to connect when running a Unit test, add the line below to the Jms Queue property in the app.properties file under /src/test/resources:
jmsQueue= vm://embeddedBroker?marshal=true&broker.persistent=false

Add Global Function to Mule.

1.) Create a new java standard class.
2.) Add a STATIC void or function that will return the result.
3.) Add the following config, update the package, class, function and parameter/s name to match those that have been created.

Global Function Config:

<configuration doc:name="Configuration">
        <expression-language>
            <import class="helper.Utility" />
            <global-functions>
                def myFunction(inputParam)
                {
                    return helper.Utility.myFunction(inputParam);
                }
           </global-functions>
         </expression-language>  
    </configuration>



Comments