部署到 Cloudhub,具有 Salesforce 创建连接器的 Munit 测试失败
Deploying to Cloudhub, Munit tests fails that has Salesforce create connector
我有一个具有 Salesforce 创建连接器的流程,引用该流程的 Munit 测试在本地运行良好。但是当我尝试使用 Azure Devops CI/CD 管道将它们部署到 CloudHub 时会抛出类似
的错误
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"}
需要Munit测试的流程是
<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 & 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>
Munit 测试如下
<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='{
"name": "X",
"SowGenerationStage": "SOW Requested",
"dateOfFirstContact": "2022-03-28",
"closeDate": "2022-03-28",
"SowRequestDate": "2022-03-28T12:48:16Z",
"accountId": "001T7IAM",
"contact": "003U0",
"leadSource": "Project Management",
"recordTypeId": "012U0000000QBXWIA4",
"serviceType": "SPR",
"priceBook2ID": "01s",
"stageName": "Discover"
}' 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>
但在我创建 MUnit 测试之前,我将流程部署到 CloudHub,构建管道没有任何问题或错误。我仍然可以访问端点。添加 Munit 测试并部署后会抛出错误。所有凭据都存储在管道变量中..
问题不在于部署本身,而在于 MUnit 测试用例。正如您提到的,Salesforce 连接器未被模拟并尝试连接。您应该模拟子流或连接器。使用 <munit-tools:mock-when>
到 select 的属性来模拟正确的工件。如问题所示,试图模拟问题中未显示的流程(“post:\opportunity:application\json:salesforce-system-api-config”)。看来你真的想模拟 Salesforce 连接器操作。
您可以在本地执行单元测试,或者先不尝试部署以验证它是否正常工作。
我有一个具有 Salesforce 创建连接器的流程,引用该流程的 Munit 测试在本地运行良好。但是当我尝试使用 Azure Devops CI/CD 管道将它们部署到 CloudHub 时会抛出类似
的错误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"}
需要Munit测试的流程是
<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 & 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>
Munit 测试如下
<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='{
"name": "X",
"SowGenerationStage": "SOW Requested",
"dateOfFirstContact": "2022-03-28",
"closeDate": "2022-03-28",
"SowRequestDate": "2022-03-28T12:48:16Z",
"accountId": "001T7IAM",
"contact": "003U0",
"leadSource": "Project Management",
"recordTypeId": "012U0000000QBXWIA4",
"serviceType": "SPR",
"priceBook2ID": "01s",
"stageName": "Discover"
}' 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>
但在我创建 MUnit 测试之前,我将流程部署到 CloudHub,构建管道没有任何问题或错误。我仍然可以访问端点。添加 Munit 测试并部署后会抛出错误。所有凭据都存储在管道变量中..
问题不在于部署本身,而在于 MUnit 测试用例。正如您提到的,Salesforce 连接器未被模拟并尝试连接。您应该模拟子流或连接器。使用 <munit-tools:mock-when>
到 select 的属性来模拟正确的工件。如问题所示,试图模拟问题中未显示的流程(“post:\opportunity:application\json:salesforce-system-api-config”)。看来你真的想模拟 Salesforce 连接器操作。
您可以在本地执行单元测试,或者先不尝试部署以验证它是否正常工作。