Mock When イベントプロセッサー

Mock When プロセッサーでは、定義された名前と属性に一致したときにイベントプロセッサーをモックできます。

たとえば、モックイベントプロセッサーを使用して、モックペイロードで 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>
xml

モックするプロセッサーを定義するように ​processor​ 属性を設定し、属性の名前と値を定義するように ​with-attribute​ 要素を設定できます。上記の例では、POST メソッドを定義しています。

DataWeave の機能とマッピングを使用して、​then-return​ 要素の ​value​ 属性を設定することもできます。たとえば、​src/test/resources/sample_data​ ファイル内に ​mockPost.dwl​ を作成します。

%dw 2.0
output application/json
---
{
	"foo" : "var"
}

次の Dataweave ファイルは、POST 要求で送信するペイロードを作成します。その後、​readUrl​ DataWeave 機能を使用してマッピングファイルを読み込むことができます。

<munit-tools:then-return>
  <munit-tools:payload value="#[readUrl('classpath://sample_data/mockPost.dwl')]" mediaType="application/json" encoding="UTF-8" />
</munit-tools:then-return>
xml

最後に、モックプロセッサーが返す応答種別を定義するように ​then-return​ 要素を設定できます。ペイロード、変数、属性、またはエラーにさえもすることができます。

then-return​ を使用して静的な定数値を返すか、​then-call​ を使用して返された値を経時的に変更する場合にフローを呼び出したり、特定の入力に応じて異なる応答を使用したりできます。

Then-Return を使用したモック

たとえば、次のような Web サービスコンシューマーをモックできます。

<wsc:config name="Web_Service_Consumer_Config">
  <wsc:connection wsdlLocation="tshirt.wsdl" service="TshirtService" port="TshirtServicePort" address="http://tshirt-service.cloudhub.io"/>
</wsc:config>

<wsc:consume config-ref="Web_Service_Consumer_Config" operation="OrderTshirt"/>
xml

次の例のように、​mock-when​ プロセッサーを設定します。

<munit-tools:mock-when processor="wsc:consume">
    <munit-tools:with-attributes>
        <munit-tools:with-attribute attributeName="operation" whereValue="#['OrderTshirt']"/>
    </munit-tools:with-attributes>
</munit-tools:mock-when>
xml

この ​mock-when​ プロセッサーは、WSDL 定義内の ​OrderTshirt​ 操作へのコールをモックします。

特定の変数をモックすることもできます。

<munit-tools:mock-when processor="http:request">
	<munit-tools:with-attributes>
	    <munit-tools:with-attribute attributeName="config-ref" whereValue="#['HTTP_Request_configuration']"/>
	</munit-tools:with-attributes>
	<munit-tools:then-return>
	  <munit-tools:variables>
	  	<munit-tools:variable key="aVariable" value="#['aValue']"/>
	  </munit-tools:variables>
	</munit-tools:then-return>
</munit-tools:mock-when>
xml

モックエラー

Mock When プロセッサーは、操作でのエラーをモックすることもできます。接続が失敗した場合に接続エラーをキャッチしてカスタムペイロードを返す、On-Error スコープを含む HTTP リクエスターがフロー内にあるとします。

<http:request-config name="HTTP_Request_Config">
    <http:request-connection host="localhost" port="8888"/>
</http:request-config>

<http:listener-config name="HTTP_Listener_Config">
    <http:listener-connection host="0.0.0.0" port="8081"/>
</http:listener-config>

<flow name="api-request">
    <http:listener config-ref="HTTP_Listener_Config" path="/"/>
    <http:request method="GET" config-ref="HTTP_Request_Config" path="/api"/>
    <error-handler>
        <on-error-continue enableNotifications="true" logException="true" type="HTTP:CONNECTIVITY">
            <set-payload value="#['Connection Error']"/>
        </on-error-continue>
    </error-handler>
</flow>
xml

HTTP 要求が失敗するたびに、アプリケーションはカスタムペイロードを返すとアサートできます。

<munit:test name="HTTP-fail-test" description="Asserts Custom Payload in HTTP Connectivity errors.">
    <munit:behavior>
        <munit-tools:mock-when processor="http:request">
            <munit-tools:with-attributes>
                <munit-tools:with-attribute attributeName="config-ref"
                                            whereValue="#['HTTP_Request_Config']"/> (1)
            </munit-tools:with-attributes>

            <munit-tools:then-return>
                <munit-tools:error typeId="#['HTTP:CONNECTIVITY']"/> (2)
            </munit-tools:then-return>
        </munit-tools:mock-when>
    </munit:behavior>

    <munit:execution>
        <flow-ref name="api-request"/> (3)
    </munit:execution>

    <munit:validation>
      <munit-tools:assert-that expression="#[payload]" is="#[MunitTools::equalToIgnoringCase('connection error')]"/> (4)
    </munit:validation>
</munit:test>
xml
1 フロー内のリクエスターの設定で HTTP 要求をモックします。
2 HTTP:CONNECTIVITY​ エラーをスローするように then-return 要素を設定します。
これにより、アプリケーション内の On-error スコープがトリガーされます。
3 リクエスターを含むフローを実行します。
4 返されたペイロードが On-error スコープ内で設定したものであるとアサートします。

Then-Call を使用したモック

変数を動的にモックするには、次のように ​then-call​ を使用できます。

1 これがモックするプロセッサーです。
2 このフローは、受信イベントに応じて変化する変数を設定するモックによってコールされます。
3 目的のフローをコールするようにモックを設定します。