Cannot make the Mock for the Salesforce Connectors work in the Munit Tests

581 views Asked by At

I have been struggling with the Munit tests for the flows I created. The Flow have couple of Salesforce connectors to Query, Create. So in my Munit tests I have the Mocks added but it seems like I am missing something. The Flow is like below

<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 Munit tests is

<munit:test name="salesforce-sys-implementation-test-suite-salesforce-Quote-create-flowTest" doc:id="40f70f6a-3453-4a8b-9ed8-673e4796c0be" description="Test">

    <munit:behavior>
        <munit-tools:mock-when doc:name="Mock when" doc:id="5bfeb53b-2da1-45f9-bad3-67cf4d658fc2" 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:then-return >
                <munit-tools:payload value="#[output application/json --- readUrl('classpath://samples/quoteQueryResp.dwl')]" mediaType="application/java" encoding="UTF-8" />
            </munit-tools:then-return>
        </munit-tools:mock-when>
        <munit-tools:mock-when doc:name="Mock when" doc:id="2d8c3c7b-04fd-4119-a535-d8b72c3af685" 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:then-return >
                <munit-tools:payload value="#[output application/json --- readUrl('classpath://samples/resp.dwl')]" mediaType="application/java" encoding="UTF-8" />
            </munit-tools:then-return>
    </munit-tools:mock-when>
 </munit:behavior>
    <munit:execution>
        <munit:set-event doc:name="Set Event" doc:id="b09dba54-76cc-401b-85aa-5657849861c4">
            <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="875fa5b7-290d-4d3f-abfe-3098bf6c7ce1" 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="6fd61167-5025-4da3-8277-c9303ba9da57" variableName="expectedPayload"/>
        <munit-tools:assert-that doc:name="Assert that Not Null Response" doc:id="dc4d4008-83bd-4da6-8773-fc4aa0285d0c" is="#[MunitTools::notNullValue()]" expression="#[vars.expectedPayload.Id]"/>
    </munit:validation>
</munit:test>

The quoteQueryResp.dwl file is like below

[
  {
    "Product_Description__c": "false",
    "Shipment_Schedule__c": "false",
    "Executive_Summary_checkbox__c": "false",
    "type": "Quote",
    "Milestones_checkbox__c": "false",
    "Terms_and_Conditions__c": "true",
    "Timeline_Tool__c": "false",
    "Signature__c": "false",
    "Executive_Summary__c": null,
    "Quote_Sent_To__r": null,
    "Contact_Information__c": "true",
    "Gantt_Chart__c": "false",
    "Animal_Health_Policy__c": "true",
    "Opportunity": {
      "RecordTypeId": "012U0000000QtmpIAC",
      "Id": null,
      "type": "Opportunity"
    },
    "Materials_and_Pricing__c": "false",
    "Cancelation_Policy__c": "true",
    "To_accept_this_statement_of_work__c": "true",
    "Customer_Signature__c": "false",
    "Id": "0Q02D0000005vu7SAA",
    "Fees_and_Invoicing__c": "true"
  }
]

But I keep getting the error

org.mule.runtime.api.el.ExpressionExecutionException: You called the function 'Value Selector' with these arguments: 
1: String ("[")
2: Name ("Id")
   
4| if(payload[0].Id != null)

   ^^^^^^^^^^^^^
Trace:
at anonymous::main (line: 4, column: 4)

Not sure what I am missing. Any help is greatly appreciated this is my first time creating the Mulesoft application

2

There are 2 answers

0
aled On BEST ANSWER

quoteQueryResp.dwl seems to be really a JSON document, which will work a DataWeave script but is missing the headers. Then you are adding an output to JSON in the expression instead of the script, then setting the media type of the mock to application/java. Maybe all these conversions are confusing DataWeave to believe the payload is a string. Try moving the output to the script and change the media type to match what is defined as output in the script. See the MUnit examples in the documentation to guide you.

0
Harshank Bansal On

Are you trying to read the mock .dwl's as java or json? Try to give the same media type in both places. Or better, remove the output application/json --- before readUrls and just give the output type in the mediaType attribute. Like this

<munit-tools:payload value="#[readUrl('classpath://samples/quoteQueryResp.dwl')]" mediaType="application/java" encoding="UTF-8" />