Mocking Multiple connectors in the flow

625 views Asked by At

I have a flow where I have a connector to query and connector create record in Salesforce. I am a newbie to Mulesoft and the Munit tests. I just created a simple Munit tests for the flow with one connector to Salesforce. Just trying to do the same but running in to issue with the Munit tests with two mock

Flow with two Salesforce connectors

    <sub-flow name="salesforce-Quote-create-flow" doc:id="310fe17f-64b3-448e-8c13-d5994352c3f5" >
        <json-logger:logger doc:name="Begin Flow (Info)" doc:id="92cbe814-9391-4ef0-900b-33c72643f3d7" config-ref="JSON_Logger_Config" message="Starting salesforce-Quote-create-flow" category="${jsonlogger.category}">
            <json-logger:content ><![CDATA[#[import modules::JSONLoggerModule output application/json ---
{
    name: payload.quoteID,
    opportunityId : payload.opportunityId,
}]]]></json-logger:content>
        </json-logger:logger>
        <set-variable value="#[payload]" doc:name="Set the input Payload" doc:id="6b46a184-fabb-4063-a1b8-a074622f73dc" variableName="inputPayload"/>
        <salesforce:query doc:name="Query Existing Quote" doc:id="19c396a3-8656-4600-beba-9e011e78d2b4" config-ref="Salesforce_Config">
            <salesforce:salesforce-query ><![CDATA[Select ID,Opportunity.recordTypeID,Contact_Information__c, Executive_Summary_checkbox__c, Milestones_checkbox__c, Gantt_Chart__c,
 Shipment_Schedule__c, Timeline_Tool__c, Product_Description__c, Materials_and_Pricing__c,
 To_accept_this_statement_of_work__c, Animal_Health_Policy__c, Cancelation_Policy__c, 
 Fees_and_Invoicing__c, Terms_and_Conditions__c, Signature__c, Customer_Signature__c, Executive_Summary__c, 
 (SELECT id,Quote__c FROM Quote_Sent_To__r) FROM Quote WHERE OpportunityId= ':opportunityId' Order by createddate desc limit 1  
        ]]></salesforce:salesforce-query>
            <salesforce:parameters ><![CDATA[#[output application/java
---
{
    "opportunityId" : vars.inputPayload.opportunityId
}]]]></salesforce:parameters>
        </salesforce:query>
        <set-variable value="#[payload]" doc:name="Set Variable exisingQuote" doc:id="9e591720-7e19-4f8f-b86f-98b05ee896f7" variableName="existingQuote"/>
        <ee:transform doc:name="Map the Request Body to Create New Quote" doc:id="f3d95e54-95a5-466d-b3c3-7d3323aef6b0">
                    <ee:message>
                        <ee:set-payload><![CDATA[%dw 2.0
output application/java
---
if(payload[0].Id != null)
[{
Name : vars.inputPayload.name,
OpportunityId: vars.inputPayload.opportunityId,
Pricebook2Id: vars.inputPayload.pricebook2d,
AX_Instance__c : vars.inputPayload.axInstance,
Multiline_Discount__c : vars.inputPayload.multilineDiscount,
Total_Discount__c: vars.inputPayload.totalDiscount,
Customer_Signature__c  : payload[0].Customer_Signature__c as Boolean,
Executive_Summary__c  : payload[0].Executive_Summary__c
}]
else
[{
Name : vars.inputPayload.name,
OpportunityId: vars.inputPayload.opportunityId,
Pricebook2Id: vars.inputPayload.pricebook2d,
AX_Instance__c : vars.inputPayload.axInstance,
Multiline_Discount__c : vars.inputPayload.multilineDiscount,
Total_Discount__c: vars.inputPayload.totalDiscount,
    }]]]></ee:set-payload>
                    </ee:message>
                </ee:transform>
        <set-variable value="#[payload[0]]" doc:name="Set Variable" doc:id="e31add26-4221-4004-9c19-b5d35d51a565" variableName="quoteBody"/>
        <json-logger:logger doc:name="After Map Request Body" doc:id="381c37f1-7dfa-4085-a294-7571424bdda2" config-ref="JSON_Logger_Config" message="After Building the Request body for the in salesforce-Quote-create-flow" tracePoint="AFTER_TRANSFORM" priority="DEBUG" category="${jsonlogger.category}">
            <json-logger:content ><![CDATA[#[import modules::JSONLoggerModule output application/json ---
{
    payload: payload
}]]]></json-logger:content>
        </json-logger:logger>
        <salesforce:create doc:name="Create Quote Record" doc:id="99e7fd61-af4f-4bed-bcb6-4f86e91c8a4b" config-ref="Salesforce_Config" type="Quote"/>
        <json-logger:logger doc:name="After Create Request to Salesforce (INFO)" doc:id="113e7428-eb0b-48b9-b007-afc7f2e44f0d" config-ref="JSON_Logger_Config" message="After Salesforce Create Operation in salesforce-Quote-create-flow" category="${jsonlogger.category}">
            <json-logger:content ><![CDATA[#[import modules::JSONLoggerModule output application/json ---
{
    success: payload.successful
}]]]></json-logger:content>
        </json-logger:logger>
    
</sub-flow>

And the Munit Test is like below

    <munit:test name="salesforce-sys-implementation-test-suite-salesforce-Quote-create-flowTest" doc:id="801d6bae-4579-4e52-8405-871c30fc1a98" description="Test">
        <munit:behavior>
            <munit-tools:mock-when doc:name="Mock when" doc:id="fe9d022f-e6c8-47a4-9a28-b1d50e72eabd" processor="salesforce:query">
                <munit-tools:with-attributes >
                    <munit-tools:with-attribute whereValue="Query Existing Quote" attributeName="doc:name" />
                    <munit-tools:with-attribute whereValue="19c396a3-8656-4600-beba-9e011e78d2b4" attributeName="doc:id" />
                </munit-tools:with-attributes>
            </munit-tools:mock-when>
            <munit-tools:mock-when doc:name="Mock when" doc:id="f1a78a02-6664-4bde-ae98-17c81eb1b181" processor="salesforce:create">
                <munit-tools:with-attributes >
                    <munit-tools:with-attribute whereValue="Create Quote Record" attributeName="doc:name" />
                    <munit-tools:with-attribute whereValue="99e7fd61-af4f-4bed-bcb6-4f86e91c8a4b" attributeName="doc:id" />
                </munit-tools:with-attributes>
            </munit-tools:mock-when>
        
</munit:behavior>
        <munit:execution>
            <munit:set-event doc:name="Set Event" doc:id="8c0f220b-d687-4de5-ba72-7d08fb75cc17">
                <munit:payload value="#[MunitTools::getResourceAsString('samples/quote.json')]" encoding="UTF-8" mediaType="application/json" />
            </munit:set-event>
            <flow-ref doc:name="Flow Reference" doc:id="e312ebeb-dbe5-4728-b13d-8e43d916bd84" name="salesforce-Quote-create-flow"/>
        </munit:execution>
        <munit:validation >
            <set-variable value="#[output application/json --- readUrl('classpath://samples/quoteResponse.json', 'application/json')]" doc:name="Set Variable" doc:id="1cec72cf-dea7-4a24-935b-aa0d497930d1" variableName="expectedPayload"/>
            <munit-tools:assert-that doc:name="Assert that Not Null Response" doc:id="9a66b251-8dd6-4f5a-980d-0f607de91863" is="#[MunitTools::notNullValue()]" expression="#[vars.expectedPayload.Id]"/>
        </munit:validation>
    </munit:test>

It thows error saying

org.mule.runtime.api.el.ExpressionExecutionException: You called the function 'Value Selector' with these arguments: 
  1: String ("Q138446")
  2: Name ("Id")

But it expects one of these combinations:
  (Array, Name)
  (Array, String)
  (Date, Name)
  (DateTime, Name)
  (LocalDateTime, Name)
  (LocalTime, Name)
  (Object, Name)
  (Object, String)
  (Period, Name)
  (Time, Name)

4| if(payload[0].Id != null)
      ^^^^^^^^^^^^^
Trace:
  at anonymous::main (line: 4, column: 4)

The transform before the create connector is erroring. How can I set the payload there and mock the create connector in my flow. Any help is greatly appreciated

1

There are 1 answers

2
aled On BEST ANSWER

It appears you are setting the condition for the mock but didn't actually set a value to replace the execution.

See this example from the documentation:



<munit-tools:mock-when processor="http:request">
   <munit-tools:with-attributes>
       <munit-tools:with-attribute attributeName="method" whereValue="#['POST']"/>
   </munit-tools:with-attributes>
   <munit-tools:then-return>
       <munit-tools:payload value="#['mockPayload']"/>
   </munit-tools:then-return>
</munit-tools:mock-when>

Notice that after <munit-tools:with-attributes> there is a <munit-tools:then-return> element that sets the mock payload.

Read the documentation link above to understand how to use mock and the various options it has.