17

Mule ESB - Mock Salesforce Interface

Embed Size (px)

Citation preview

Page 2: Mule ESB - Mock Salesforce Interface

Mock ESB Endpoints using MUnit

Mock an Mule ESB endpoint without real time need of an interface (during development time)

Unit test your flows without & with real time interfaces

Automate your unit test by testing with real time or without provider interfaces using Munit (from Jenkins)

It is possible to mock SAP endpoint, JDBC Endpoint, Salesforce point and so on!

Let us see an example of how to mock Salesforce

Page 3: Mule ESB - Mock Salesforce Interface

What is MUnit? A Beta-version testing framework from Mulesoft

http://www.mulesoft.org/documentation/display/current/Introduction+to+Testing+Mule#IntroductiontoTestingMule-GoFurther

This goes the extra mile than Functional Test Case - as Functional Test case requires real time interfaces for unit testing your flows

Can switch to real time interface or use mock data easily using a flag from your CI tool

Runs within Anypoint Studio and from Maven/Jenkins

Page 4: Mule ESB - Mock Salesforce Interface

Pre-requisites Anypoint studio 15 Jan 2015 release

Mule CE or EE 3.5.2 runtime or 3.6.0 runtime

MUnit 3.5.x runtime libraries/API

MUnit interceptor API

Salesforce credentials (having a SFDC account, user name/password and security token)

Apache Maven installation

Patience

Page 5: Mule ESB - Mock Salesforce Interface

Get MUnit Go to https://github.com/mulesoft/munit

Download MUnit libraries

Download Munit mule-interceptor-module

Run pom.xml ($maven –X E clean insall, skip tests) on both the projects

Jar files for Munit and mule-interceptor-modules should be generated by now

Add these jar files (exclude sources) to Anypointstudio build class path

Page 6: Mule ESB - Mock Salesforce Interface

Salesforce Accounts Download Let us build a flow which downloads Accounts from

Salesforce

Triggered using Http endpoint (for example)

SOQL query executed in Salesforce endpoint is

SELECT ID, NAME, BILLINGCITY FROM ACCOUNT

Salesforce returns all Accounts as an ArrayList<HashMap>

Each HashMap contains SFDC Account information by having Name, ID or BILLINGCITY as key

Page 7: Mule ESB - Mock Salesforce Interface

Example Salesforce Flow

Page 8: Mule ESB - Mock Salesforce Interface

Identify payload returned using Debugger

Page 9: Mule ESB - Mock Salesforce Interface

So far… We identified Salesforce returns ArrayList<HashMap>

which contains Account information

SOQL used is

SELECT ID, NAME, BILLINGCITY FROM ACCOUNT

We had built MUnit libraries for unit testing this

Page 10: Mule ESB - Mock Salesforce Interface

Unit testing approach Prepare Test Data to be returned by Salesforce

Endpoint (mocked test data)

Prepare Assertion data which will assert the Test Data

Assert Test Data and Assertion Data

Page 11: Mule ESB - Mock Salesforce Interface

MUnit – Java based testing Overview

Extend your Munit test case from FunctionalMUnitSuite (org.mule.munit.runner.functional.FunctionalMunitSuite)

Define your flows to be loaded when Mule test case starts (like below):protected String getConfigResources() {

return "SalesforceMock2.xml";

}

Define whether to mock or use realtime interface// Return TRUE to mock endpoint, otherwise FALSE

public boolean haveToMockMuleConnectors() {

return true;

}

Page 12: Mule ESB - Mock Salesforce Interface

MUnit – Java based testing OverviewFollowing is XML file for the flow:<sfdc:config name="Salesforce" username="[email protected]" password=“aaaaaa" securityToken=“aaaaaa" doc:name="Salesforce">

<sfdc:connection-pooling-profileinitialisationPolicy="INITIALISE_ONE" exhaustedAction="WHEN_EXHAUSTED_GROW"/>

</sfdc:config>

<flow name="SalesforceMockFlow">

<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>

<logger message="***** Before invoking Salesforce for Accounts *****" level="INFO" doc:name="Logger"/>

<sfdc:query config-ref="Salesforce" query="select id, billingcity, name from Account" doc:name="Salesforce"/>

<logger message="#[payload] ==> Payload type is: #[payload.getClass()]" level="INFO" doc:name="Logger"/>

</flow>

Page 13: Mule ESB - Mock Salesforce Interface

Tell your MUnit java to mock To mock an endpoint (Salesforce), in your unit test case,

first specify to mock an endpoint with a test mock data (like below):

whenMessageProcessor("query").

ofNamespace("sfdc").

thenReturn(

muleMessageWithPayload(

accountsMockData )

);

Here, accountsMockData is the mock data payload

Salesforce endpoint is <sfdc:query…> in flow XML having sfdc as namespae and query as processor name

Page 14: Mule ESB - Mock Salesforce Interface

Next steps

We had told Munit to mock Salesforce endpoint with accountsMockData. So

We need to prepare mock data for Accounts

We need to prepare Assertion data for Accounts to Assert

We need to Assert mock data and Assertion data so that your flow is fully functional for the test case

Refer to complete test case java code

Page 15: Mule ESB - Mock Salesforce Interface

MUnit – Mock Salesforce Enpointpublic class SalesforceMockTest extends FunctionalMunitSuite {

@Test

public void testSalesforceAccounts() throws Exception {

// Step 1: Prepare Mock data for List of Salesforce Account objects using

SFDCTestAccountPayloadGenerator

SFDCTestAccountPayloadGenerator mockDataGenerator = new SFDCTestAccountPayloadGenerator();

ArrayList<HashMap<String,String>> accountsMockData =

mockDataGenerator.getSalesforceAccountsTestData();

// Step 2: Register Mock data to Salesforce processor which will be returned (when invoked

in the flow)

whenMessageProcessor("query").ofNamespace("sfdc").thenReturn( muleMessageWithPayload(

accountsMockData ));

// Step 3: Invoke the flow (Salesforce endpoint is mocked now! - using a test event)

MuleEvent resultEvent = runFlow( "SalesforceMockFlow", testEvent("Hello world!") );

ArrayList<HashMap<String,String>> flowResult = (ArrayList<HashMap<String,String>>)

resultEvent.getMessage().getPayload();

// Step 4: resultEvent above has the Mocked Salesforce data. Now we need to assert it

SFDCAssertAccountPayloadGenerator assertDataGenerator = new

SFDCAssertAccountPayloadGenerator();

ArrayList<HashMap<String,String>> assertionData =

assertDataGenerator.getAccountAssertionData();

assertEquals( assertionData, flowResult );

}

}

Note: getConfigResources() and haveToMockMuleConnectors() are omitted due to space constraints in this page

Page 16: Mule ESB - Mock Salesforce Interface

Run MUnit Test Case Right Click Project in Anypoint Studio

Select Test Case java file

Run As Junit Test Case (see results in Studio )

Page 17: Mule ESB - Mock Salesforce Interface

Thank you!

[email protected]