【问题标题】:Mule Munit with Choice followed by Dataweave transformMule Munit with Choice 后跟 Dataweave 转换
【发布时间】:2018-08-07 11:30:03
【问题描述】:

我有一个具有选择后跟 Dataweave 转换的流。 流的输入是 json。 选择有这个when语句:

<when expression="#[payload.resultSet1.size() >= 1]">

流程运行良好:

<flow name="test1">
 <db:stored-procedure doc:name="MyStoredProc"></db:stored-procedure>
 <choice doc:name="data found?">
        <when expression="#[payload.resultSet1.size() >= 1]">
            <dw:transform-message doc:name="CreateResponse">
                     <dw:set-payload resource="classpath:/dataweave/someDataweave.dwl" />
              </dw:transform-message>
        </when>
       <otherwise>
              <logger message="False:  payload:#[payload]" level="INFO" doc:name="Log-False"/>
        </otherwise>
    </choice>  
</flow>

现在我正在尝试为它创建一个 Munit 测试。为了让选择发挥作用,我不得不对数据进行一些操作。 该选择现在有效,但现在 dataweave 转换引发异常,因为它说 json 数据缺少引号。 单位:

<munit:test name="test1-tryit" description="Test">
      <mock:when messageProcessor=".*:.*" doc:name="Mock MyStoredProcResults">
        <mock:with-attributes>
            <mock:with-attribute name="doc:name" whereValue="#['MyStoredProc']"/>
        </mock:with-attributes>
        <mock:then-return payload="#[flowVars.jsonStoredProcResponse]" mimeType="text/json"/>
    </mock:when>
    <set-payload value="#[getResource('myTestData.json').asStream()]"  doc:name="GetStoredProcResponse" mimeType="application/json"/>
    <json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object"/>
    <set-variable variableName="jsonStoredProcResponse" value="#[payload]" doc:name="jsonStoredProcResponse" />
   <flow-ref name="test1" doc:name="Flow-ref to test1 flow" />

myTestData.json 文件中的数据如下所示: {"resultSet1": [{ "field1":"123" } ]}

不确定如何解决此问题并让选择和转换都起作用。

谢谢。

【问题讨论】:

  • 能否请您添加loggeg异常的全文?如果您可以添加有关问题的更多详细信息,则更容易找出问题所在。非常感谢。

标签: mule dataweave munit


【解决方案1】:
    Try using below groovy script:

    <scripting: script name="mockDataBaseResponse" engine="groovy" doc:name="Script"><![CDATA[
    Hashmap<String,String> cursor = new Hashmap<String,String>();
    cursor.put("field1","123");
    LinkedList List = new LinkedList();
    List.add(cursor);
    Hashmap<String,Object> cursorMap = new Hashmap<String,Object>();
    cursorMap.put("resultSet1",List);
    return cursorMap;]]>
    </scripting:script>

Mock payload using script:

<mock:then-return payload="#[resultOfScript('mockDataBaseresponse')]" mimeType="text/java"/>

Also include this script file in you Munit xml file.

【讨论】:

    猜你喜欢
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    相关资源
    最近更新 更多