trx
trx

Reputation: 2167

Munit tests for the POst request end point

I am very new to Mulesoft and struggling to understand how to create the MUnit Tests for the simple flow which looks like below

enter image description here

    <sub-flow name="salesforce-Opportunity-create-flow" doc:id="bd0263bf-7880-46d0-9c83-92376caa1bbb" >
        <json-logger:logger doc:name="Begin Flow (Info)" doc:id="1f263a47-bb39-4748-b5b2-9e2f946cac58" config-ref="JSON_Logger_Config" message="Starting salesforce-Opportunity-create-flow" category="${jsonlogger.category}">
            <json-logger:content><![CDATA[#[import modules::JSONLoggerModule output application/json ---
{
    name: payload.name,
    accountId: payload.accountId,
    recordTypeId: payload.recordTypeId
}]]]></json-logger:content>
        </json-logger:logger>
        <ee:transform doc:name="Map Request Body" doc:id="ecee64bf-1b14-4261-bb29-c22d01f89726" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
[{
    AccountId: payload.accountId,
    RecordTypeId: payload.recordTypeId,
    Name: payload.name,
    StageName: payload.stageName,
    CloseDate: payload.closeDate as Date,
    LeadSource: payload.leadSource,
    Pricebook2Id: payload.priceBook2ID,
    Date_of_First_Contact__c: payload.dateOfFirstContact as Date,
    Contact__c: payload.contact,
    Service_Type__c: payload.serviceType,
    SOW_Request_Date__c: payload.SowRequestDate as DateTime,
    SOW_Generation_Stage__c: payload.SowGenerationStage
}]]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <json-logger:logger doc:name="After Map Request Body (Info)" doc:id="6ef57816-a0ba-4f62-aa66-851467799ffc" config-ref="JSON_Logger_Config" message="After Map Request Body &amp; Before Salesforce Create Operation in salesforce-Opportunity-create-flow" category="${jsonlogger.category}" tracePoint="AFTER_TRANSFORM">
            <json-logger:content><![CDATA[#[import modules::JSONLoggerModule output application/json ---
{
    Name: payload.Name,
    AccountId: payload.AccountId,
    RecordTypeId: payload.RecordTypeId
}]]]></json-logger:content>
        </json-logger:logger>
        <salesforce:create doc:name="Create" doc:id="e6b01da3-e86b-4d05-966d-cc410d5a2b7d" config-ref="Salesforce_Config" type="Opportunity"/>
        <json-logger:logger doc:name="After Request to Salesforce (Info)" doc:id="5f397dec-ad2b-4672-a4a6-49b7cb55eccc" config-ref="JSON_Logger_Config" message="After Salesforce Create Operation in salesforce-Opportunity-create-flow" tracePoint="AFTER_REQUEST" category="${jsonlogger.category}">
            <json-logger:content><![CDATA[#[import modules::JSONLoggerModule output application/json ---
{
    success: payload.successful
}]]]></json-logger:content>
        </json-logger:logger>
        <ee:transform doc:name="Map Salesforce Response" doc:id="882badac-042c-420e-b58d-891345a51210" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
    Id: payload.items[0].id
}]]></ee:set-payload>
            </ee:message>
            <ee:variables >
                <ee:set-variable variableName="httpStatus" ><![CDATA[%dw 2.0
output application/json
---
payload.items[0].statusCode]]></ee:set-variable>
            </ee:variables>
        </ee:transform>
        <json-logger:logger doc:name="End Flow (Info)" doc:id="a2e40bdb-e6e3-43e0-a0b2-eb85cb8f2d64" config-ref="JSON_Logger_Config" message="After Mapping Salesforce Response &amp; End salesforce-Opportunity-create-flow" tracePoint="END" category="${jsonlogger.category}" >
            <json-logger:content ><![CDATA[#[import modules::JSONLoggerModule output application/json ---
{
    Id: payload.Id
}]]]></json-logger:content>
        </json-logger:logger>
    </sub-flow>

The above request is a POST endpoint that will create record in Salesforce. The articles I read was saying about Record Tests But I unable to do the record option for this application. The only other option I see is Create a blank test for the flow that just created the empty test

enter image description here

Most of the articles I referred was taking about GET. Any help/exaple of how I can mock the POST request will be great

Upvotes: 0

Views: 764

Answers (1)

aled
aled

Reputation: 25872

You can use the example in the documentation to understand how to mock an HTTP Request with method POST:

<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>

Upvotes: 0

Related Questions