Adsense

October 25, 2019

Exclude flows/files from MUnit coverage


In MuleSoft development before deploying our Mule applications and APIs we write test cases for unit and integration testing. 

MUnit is a Mule testing framework that lets you easily automate testing Mule applications, using both unit and integration tests. MUnit also provides a set of tools, such as a message processor mocking framework that lets you test units of code in actual isolation. 
It helps us to generate coverage reports automatically.

In some of the scenarios if you want to skip flows and files from the coverage reports.
The procedure varies slightly depending on the type of test that you are trying to skip in the coverage report.

Ignoring Flows: 

If you are trying to ignore a flow, you can add the following piece of code in the POM file:


<coverage>
  <ignoreFlows>
      <ignoreFlow>flow-name</ignoreFlow>
  </ignoreFlows>
</coverage>


Ignoring File:

If you want to remove a file from your Coverage report, you can add the following piece of code in the POM file:

<coverage> 
     <ignoreFiles>
          <ignoreFile>file-name.xml</ignoreFile> 
     </ignoreFiles> 
</coverage>


Note: 

Please keep in mind that the coverage tag must be included in the Mule Maven plugin configuration tag. For example:

<plugins>
  <plugin>
    <groupId>com.mulesoft.munit.tools</groupId>
    <artifactId>munit-maven-plugin</artifactId>
    <configuration>
      ...
      <coverage>
       ...
      </coverage>
      ...
    </configuration>
  </plugin>
</plugins>


The steps mentioned in this article are only useful for terminal console.


Coverage report with ignore tags: mvn clean install


Coverage report with ignore tags


Coverage report without ignore tags: mvn clean install

Coverage report without ignore tags


Ignore MUnit from command line

mvn clean package -DskipTests

or 

you can add skip test configuration inside pom as below:
<plugins>
  <plugin>
      <groupId>com.mulesoft.munit.tools</groupId>
      <artifactId>munit-maven-plugin</artifactId>
      <configuration>
      ...
      <skipMunitTests>true</skipMunitTests>
      ...
    </configuration>
  </plugin>
</plugins>


Please find sample Mule project in Github Munit coverage report mule4

Happy learning :)
Read More »

October 16, 2019

Mulesoft Basics

Mule Application Building Blocks

In this article we will find the basic concepts before start building Mule application using Anypoint studio. Here we will see things about


  • Mule Palette
  • Project explorer
  • Pom basic configuration
  • Shortcuts
  • Selecting runtimes
  • Munits coverage view
  • Test your application and many more.
Mule application building blocks are separated into categories in the Mule 3 Palette:
Anypoint Studio Palette



In Mule 4 build concept is almost same but in Mule 4 runtime decoupled the extra libraries from core runtime libraries and providing only the core libraries to build basic app flows, which makes it lightweight runtime. However we can add modules or import from exchange as per requirement.

batchflow control
Componentsscheduler

error handling

transformers
scopes
      


Add modules in Mule 4

Mule 4 Palette


Search from Exchange and Add in Mule 4

Search from Exchange Mule 4



Now lets discuss about components

Message Sources :

The first building block of most flows is a receiver that receives new messages and places them in the queue for processing.
  • Message sources are usually Anypoint Connectors.
  • Connectors provide connectivity to external resources, such as:
    • Databases, protocols, or APIs.
    • Standard protocols like HTTP, FTP, SMTP, AMQP.
    • Third-party APIs like Salesforce, Twitter, or MongoDB.

Anypoint Connectors :

  • There are 2 main types:
    • Endpoint-based connectors
    • Operation-based connectors

Endpoint-Based Connectors :

  • Are either inbound or outbound endpoints in a flow.
  • Inbound endpoints serve as a message source for a flow.
  • Outbound endpoints send information to external systems.

Endpoint based connectors


Operation-Based Connectors :

Operation based connector

  • These connectors require the specification of an operation in order to perform.
  • This category includes most connectors not based on a standard communication protocol


Connector vs Endpoint :

  • A connector is a Mule-specific connection to an external resource of any kind.
  • An endpoint is a flow-level element that is configured to receive and/or send messages from and/or to external resources.
  • When you drag a connector from the Mule Palette, an endpoint is created.
  • Connectors and endpoints are global elements.

Creating Mule Applications With Anypoint Studio

Anypoint Studio is an Eclipse-based integration development environment that provides:
  • Two-way editing between graphical and XML views.
  • Visual debugging (EE).
  • A data transformation framework and language (EE).
  • One-click deployment of applications.
  • Templates for common integration patterns (EE).
  • Integration with Maven for continuous build processes.

Sample Flow: Visual

Sample Mule flow design



Anypoint Studio View: 


AnypointStudio view


Running an Application:


Anypoint Studio comes with an embedded Mule runtime to test applications without leaving it.
The console outputs application logs and information:
AnypointStudio Console



Introducing Mule Flows and Message


Mule Flows

Mule applications accept and process messages through a series of message processors plugged together in the flow.
Mule Flow Structure


A typical flow has:

Message Source  - Accepts a message from an external source, triggering the execution of the flow.
Message Processors - Transforms, filters, enrichs, and processes messages.
An application can consist of:
  • A single flow.
  • Multiple flows/subflows connected together to create more complex applications.

Mule Message Structure

You can find more about Mule message structures and events

Anatomy of  a Flow


Mule Message structure





artifacts-json: In Mule 4


Mule artifact.json



Pom.xml: 

Pom file is now required for Mule 4 app build. All mule app in mule4 is packaging as mule-application 

Pom xml

Project Explorer: 

Below Project explorer shows how the structure of Mule 4 App will look like:

Mule Project Structure


























Runtime Selection: 


You can select your required runtime by selecting through Run/Debug as configuration
Mule Runtime Configuration



Menu Bar & Shortcuts

AnypointStudio Menu Bar

Quick Access

You can quickly search from Quick Access, it is placed at top right of your anypoint studio.

Anypoint Studio Quick Access


Munit Coverage View

For munit coverage and reports you can run and also view in anypoint studio.

Munit Coverage report



Testing Applications: For testing Mule Application


Some options:
A browser.
A cURL command-line utility.
A browser extension like Postman (for Google Chrome). 
Postman View


Read More »

October 10, 2019

Use secure properties in dataweave 2

We have seen In Mule 4 Secure Configuration Properties Module that can be used to encrypt properties files or individual properties. In this article, we will see how to use Secure properties inside DataWeave 2 or in Mule 4 expression statements.

Things to know: If you need to see how to configure your Mule project to use the Secure Configuration Properties Module, and Encrypt Properties, I recommend you read the my article securing-configuration-properties-in-mule.


Once you have Secure Properties configured, you can read secure properties inside your xml configuration by prefixing secure:: namespace before the property name.



Use of secure prop in global property: 

<global-property name="prop" value="my-${secure::property.key}"/>


Use of Secure prop in Flow

<flow name="test-flow">
    <set-payload value="${secure::encrypted.key}"/>
</flow>

Now, What about using it in DataWeave 2?


If you have used properties in DataWeave 1, you would know about a special function p(prop_name) that can be used inside DataWeave 1 script to read properties. The same function is available in Mule 4 and DataWeave 2.


Using in Expression:

<global-property name="prop" value="#[p('secure::encrypted.key')]"/>

Using in DataWeave 2.0:

%dw 2.0
output application/java
---
{
password: p('secure::encrypted.key')
}

Please find the sample Mule project in Github here


Happy learning :)
Read More »

October 06, 2019

@Future vs Batch Apex in salesforce

Future Annotation: 
  1. Future Annotation(@Future) is used to separate methods that are to be executed asynchronously.
  2. If Future Annotation is not used in a web service callout, the thread will wait until the response comes and other processes will not be executed.
  3. Mixed DML Exception can be avoided with the use of @future annotation.
  4. @future annotation - Must be static and return void - Specify (callout=true) to allow callouts.
  5. Parameters passed to this can only be primitive.
  6. Tracking future is difficult as you don't have any jobId to track it.
  7. Future can be used to resolve mixed DML exception.
Example: You cannot insert a setup object and a non-setup object in same context. You'll encounter mixed DML exception. @Future comes for rescue in this situation.
public class MixedDMLFuture {
    public static void useFutureMethod() { 
        //First DML operation- Insertion of non-setup object(Account)
        Account a = new Account(Name='Vydehi Hospital'); 
        insert a; 
        
        // This next operation (insert a user with a role)  can't be mixed with the previous insert unless it is within a future method. 
        /*// Other DML Operation -- commented the code with a reason, if you use the commented code you will get mixed DML exception error
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        UserRole r = [SELECT Id FROM UserRole WHERE Name='COO']; 
        // Create new user with a non-null user role ID 
        User u = new User(alias = 'ranvk', email='dummyMail@gmail',                   
                          emailencodingkey='UTF-8', lastname='niru',languagelocalekey='en_US',     
                          localesidkey='en_US', profileid = p.Id, userroleid = r.Id,  
                          timezonesidkey='America/Los_Angeles',  
                          username='dM@gmail.com'); 
        insert u; */
        //Call future method to insert a user(setup object) with a role. 
        UtilClass.insertUserWithRole( 'dummyMail@gmail.com', 'ranvk', 'dM@gmail.com', 'niru'); 
    } 
}

public class UtilClass { 
      @future 
       public static void insertUserWithRole(String uname, String al, String em, String lname) { 
               Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
               UserRole r = [SELECT Id FROM UserRole WHERE Name='COO']; 
               // Create new user with a non-null user role ID 
               User u = new User(alias = al, email=em, emailencodingkey='UTF-8',   
                              lastname=lname, languagelocalekey='en_US', localesidkey='en_US', 
                              profileid = p.Id, userroleid = r.Id, timezonesidkey='America/Los_Angeles',  
                              username=uname); 
               insert u; 
       } 
}


Batch Apex:
  1. Batch Apex is used to separate tasks that are going to handle more records(complex long running jobs) in background process. 
  2. To avoid hitting governor limits we use Batch Apex for handling bulk records.
  3. It can process up to 50 million records. It can also be scheduled to run at a particular time.
  4. Only 5 concurrent batch can execute simultaneously. It is difficult to track the progress of the execution.
  5. We cannot use @future annotation. As asynchronous call is not allowed from batch apex.


Example: Delete all the account having Rating 'Cold'
global class deleteAccounts implements Database.Batchable{
global Database.QueryLocator start(Database.BatchableContext BC){
string query = 'select id,name,Rating,Owner from Account';
return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC,List scope){
List <Account> lstAccount = new list<Account>();
for(Sobject s : scope){
Account a = new Account();
if(s.Rating=='Cold'){
lstAccount.add(a);
}
}
Delete lstAccount;
}

global void finish(Database.BatchableContext BC){
//Send an email to the User after your batch completes
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'dummyemail@gmail.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Batch Job is completed');
mail.setPlainTextBody('The batch Apex job processed ');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

Read More »

October 05, 2019

Securing configuration properties in mule

In this article I will show you how to use mule credential vault and how to start securing your properties file using Anypoint Security suite. You will also get to know about the best practices of using secure properties.


Step I: Installing Anypoint Security Suite

Anypoint Enterprise Security is a collection of security features that enforce secure access to information in Mule applications.It helps application developers to develop security solutions as per security requirements, prevent security breaches, and facilitate proper authorized access to data.

You can find how to install Anypoint security suite my another article here
Before starting you should understand how Mule Credentials Vault actually works. you can go through the documentation here

How to Encrypt Properties
1. Create a Mule project in Anypoint Studio.

2. Add maven dependency to project pom.xml


<dependency>

<groupId>com.mulesoft.modules</groupId>
<artifactId>mule-secure-configuration-property-module</artifactId>
<version>1.0.2</version>
<classifier>mule-plugin</classifier>
</dependency>
3. In the src/main/resources folder of the project, right-click and select New -> File.


Anypoint, Configuration, dataweave 2, Decryption, Encryption, Integration, Maven, Middleware, Mule, Mule 3, Mule 4, MuleCode, MuleProject, MuleSoft, Properties, Tutorial, Mule Credentials Vault, Maven, secure, Security,

4. In the New file wizard, provide a file name, which should include .properties extension. Ex: dev.secure.properties. Click finish.
5. In the project explorer, right-click the .properties file and select Open with -> Mule Properties Editor.
Anypoint, Configuration, dataweave 2, Decryption, Encryption, Integration, Maven, Middleware, Mule, Mule 3, Mule 4, MuleCode, MuleProject, MuleSoft, Properties, Tutorial, Mule Credentials Vault, Maven, secure, Security,

6. Click on the green + icon in the studio. In the Add New Property window, add a key and a value. Click on Encrypt button if you want to encrypt the value, and do not if you don't want to. 
Anypoint, Configuration, dataweave 2, Decryption, Encryption, Integration, Maven, Middleware, Mule, Mule 3, Mule 4, MuleCode, MuleProject, MuleSoft, Properties, Tutorial, Mule Credentials Vault, Maven, secure, Security,

7. If you click on Encrypt, in the Setup encryption information dialog box that appears, select an algorithm and provide a key that will be used to encrypt the value. Click OK.
Anypoint, Configuration, dataweave 2, Decryption, Encryption, Integration, Maven, Middleware, Mule, Mule 3, Mule 4, MuleCode, MuleProject, MuleSoft, Properties, Tutorial, Mule Credentials Vault, Maven, secure, Security,

8. After you click OK, the encrypted value looks as shown below.
Anypoint, Configuration, dataweave 2, Decryption, Encryption, Integration, Maven, Middleware, Mule, Mule 3, Mule 4, MuleCode, MuleProject, MuleSoft, Properties, Tutorial, Mule Credentials Vault, Maven, secure, Security,

8. Repeat the above steps to add more values to the Credentials Vault.

1. Click on the Global Elements tab. Create a global property ${encrypted.key}. This will be provided to secure property place holder. ${encryption.key} is the key that we will provide at runtime of the application.


Anypoint, Configuration, dataweave 2, Decryption, Encryption, Integration, Maven, Middleware, Mule, Mule 3, Mule 4, MuleCode, MuleProject, MuleSoft, Properties, Tutorial, Mule Credentials Vault, Maven, secure, Security,

2. In the Secure Property Placeholder wizard, set the Encryption Algorithm, Encryption Mode, and key. The Encryption Algorithm will be the same as you used at the time of the encryption process above. The key will be the value that you used to encrypt your value above (STEP 6).


Anypoint, Configuration, dataweave 2, Decryption, Encryption, Integration, Maven, Middleware, Mule, Mule 3, Mule 4, MuleCode, MuleProject, MuleSoft, Properties, Tutorial, Mule Credentials Vault, Maven, secure, Security,

Here, ${encrypted.key} is provided through global property. 

Local Setup for running your application

1. Click on the project--> Run as --> Run Configuration
Anypoint, Configuration, dataweave 2, Decryption, Encryption, Integration, Maven, Middleware, Mule, Mule 3, Mule 4, MuleCode, MuleProject, MuleSoft, Properties, Tutorial, Mule Credentials Vault, Maven, secure, Security,

1234 above is my key. You need to add your encryption key in runtime arguments here as shown above.


2. After you run this application, call the following flow and look at the console. You will see that the encrypted value is getting printed after decryption.
INFO  2019-10-02 12:33:17,486 [[MuleRuntime].cpuLight.08: [retrieve-secure-properties-using-dw2].retrieve-secure-properties-using-dw2.CPU_LITE @17e305df] [event: b5c46681-e4e2-11e9-a182-e6a471bf06f3] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: secure  prop value>> Hello Manish

INFO  2019-10-02 12:33:22,768 [[MuleRuntime].cpuIntensive.02: [retrieve-secure-properties-using-dw2].retrieve-secure-properties-using-dw2.CPU_INTENSIVE @51ad86c5] [event: b5c46681-e4e2-11e9-a182-e6a471bf06f3] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: "secure  prop value>>" {password=*****}




Anypoint, Configuration, dataweave 2, Decryption, Encryption, Integration, Maven, Middleware, Mule, Mule 3, Mule 4, MuleCode, MuleProject, MuleSoft, Properties, Tutorial, Mule Credentials Vault, Maven, secure, Security,
Logging secure properties


Anypoint, Configuration, dataweave 2, Decryption, Encryption, Integration, Maven, Middleware, Mule, Mule 3, Mule 4, MuleCode, MuleProject, MuleSoft, Properties, Tutorial, Mule Credentials Vault, Maven, secure, Security,
Logging secure prop as payload

To build your application through command line

Anypoint, Configuration, dataweave 2, Decryption, Encryption, Integration, Maven, Middleware, Mule, Mule 3, Mule 4, MuleCode, MuleProject, MuleSoft, Properties, Tutorial, Mule Credentials Vault, Maven, secure, Security,

Note:


  • It is recommended to never log your secure prop values.
  • Always externalize your secure properties.
  • In cloud deployment you can pass secure properties like encryption key in properties editor tab.


Please find the sample Mule project here

Read More »