Deploying to Cloudhub, Munit tests fails that has Salesforce create connector

567 views Asked by At

I have a flow that has the Salesforce create connector, the Munit test that references the flow Runs fine locally. But when I try to deploy them to the CloudHub using the Azure Devops CI/CD pipeline throws error like

WARN 2022-04-07 23:41:54,103 [[MuleRuntime].uber.04: [mule-salesforce-sys].salesforce-Opportunity-create-flow/processors/3.ps.BLOCKING @3c7baec] org.mule.extension.salesforce.internal.connection.provider.BasicConnectionProvider: Couldn't obtain access token because an exception has occurred. Runtime to handle the ConnectionException. Root exception was: Invalid status code: 400, response body: {"error":"invalid_client","error_description":"invalid client credentials"}
org.mule.runtime.extension.api.exception.ModuleException: Invalid status code: 400, response body: {"error":"invalid_client","error_description":"invalid client credentials"}

Flow that needs the Munit test is

<sub-flow name="salesforce-Opportunity-create-flow" doc:id="bd0263bf-7880-46d0-9c83-92376caa1bbb" >
        <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>
        <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 Munit test is like below

enter image description here

<munit:test name="salesforce-sys-implementation-test-suite-salesforce-Opportunity-create-flowTest" doc:id="c9779a45-00fe-4e08-b3e7-2580e68352ef" description="Test">
        <munit:behavior>
            <munit-tools:mock-when doc:name="Mock when" doc:id="a2ff9717-a4ee-4bea-9caf-9e3d5fe591b7" processor="flow">
                <munit-tools:with-attributes >
                    <munit-tools:with-attribute whereValue="post:\opportunity:application\json:salesforce-system-api-config" attributeName="name" />
                </munit-tools:with-attributes>
                <munit-tools:then-return >
                    <munit-tools:payload value="#[{}]" mediaType="application/json" encoding="UTF-8" />
                </munit-tools:then-return>
            </munit-tools:mock-when>
        </munit:behavior>
        <munit:execution >
            <munit:set-event doc:name="Set Event" doc:id="4c21ba83-a2d8-47f7-a327-33a772f980c6" >
                <munit:payload value='{
&#10;  "name": "X",
&#10;  "SowGenerationStage": "SOW Requested",
&#10;  "dateOfFirstContact": "2022-03-28",
&#10;  "closeDate": "2022-03-28",
&#10;  "SowRequestDate": "2022-03-28T12:48:16Z",
&#10;  "accountId": "001T7IAM",
&#10;  "contact": "003U0",
&#10;  "leadSource": "Project Management",
&#10;  "recordTypeId": "012U0000000QBXWIA4",
&#10;  "serviceType": "SPR",
&#10;  "priceBook2ID": "01s",
&#10;  "stageName": "Discover"
&#10;}' mediaType="application/json" />
            </munit:set-event>
            <flow-ref doc:name="Flow-ref to salesforce-Opportunity-create-flow" doc:id="3c3d043f-f1ad-4b8d-9290-d407400d14a6" name="salesforce-Opportunity-create-flow"/>
        </munit:execution>
        <munit:validation >
            <munit-tools:assert-that doc:name="Assert that Not Null Response" doc:id="da165ba2-619f-462e-8278-e66ee771cb08" is="#[MunitTools::notNullValue()]" expression="#[payload.Id]"/>
        </munit:validation>
    </munit:test>

But before I created the MUnit tests I deployed the flow to the CloudHub and there was no issues or error with Build Pipeline. I can still access the endpoint. After adding the Munit tests and deploying it throws error. All the credentials are stored in the Pipeline Variables..

1

There are 1 answers

0
aled On BEST ANSWER

The problem is not with deployment itself, but with the MUnit test case. As you mentioned the Salesforce connector is not mocked and trying to connect. You should mock the subflow or the connector. Use the attributes of <munit-tools:mock-when> to select the right artifact to mock. As shown in the question is trying to mock a flow not shown in the question ("post:\opportunity:application\json:salesforce-system-api-config"). It seems that you really want to mock the Salesforce connector operation.

You could execute the unit test locally, or without trying to deploy to verify it is working first.