0
votes

I have the db connector in mule, which has the select query to count the number of records and the response from the database is as below:

MULE CODE SNIPPET:

     <db:select config-ref="Oracle_Configuration" doc:name="FetchDetails">
            <db:parameterized-query><![CDATA[SELECT COUNT(1) AS COUNT from MAPPER_T where ST_NO=#[flowVars.fetchNO] and DATE=SYSDATE]]></db:parameterized-query>
        </db:select>

 <choice doc:name="EvaluateCount">
            <when expression="#[payload[0]['COUNT'] == 0]">
           <logger message="#[payload]" level="INFO" category="DO SOMETHING X" doc:name="Logger"/>
            </when>
 <otherwise>
                    <logger message="#[payload]" level="INFO" category="DO SOMETHING Y" doc:name="Logger"/>
            </otherwise>
        </choice>

Response from the database call using logger with mel #[message.payload]

[{COUNT=1}]

In the munit implementation i am trying to mock this response and i have used the MOCK component of the MULE and below is my code:

 <munit:test name="api-Happy-Scenario-test-suiteTest" description="MUnit Test">
  	 <mock:when messageProcessor=".*:.*" doc:name="Mock - Evaluate Results">
            <mock:with-attributes>
                <mock:with-attribute name="doc:name" whereValue="#['FetchDetails']"/>
            </mock:with-attributes>
            <mock:then-return payload="#[{COUNT: 0}]"/>
        </mock:when>
               <munit:set payload="#[getResource('json/ResultsRequest.json').asString()]" mimeType="application/json" doc:name="Set Message">
	</munit:set>
        <flow-ref name="post:/results:api-config" doc:name="InvokeResultsService"/>
        <munit:assert-true message="The HTTP Status code is not correct!" condition="#[messageInboundProperty('http.status').is(eq(201))]" doc:name="assert that - http.status eq 201"/>
        <munit:assert-null doc:name="Assert Null Payload"/>
    </munit:test>

But while executing the test case's i am getting the below exception:

Caused by: org.mule.api.expression.ExpressionRuntimeException: Execution of the expression "payload[0]['COUNT']" failed.
Caused by: [Error: java.lang.Character cannot be cast to java.lang.Class]
[Near : {... payload[0]['COUNT'] ....}]
             ^
[Line: 1, Column: 1]

Could you please help me how i can mock the response. Thanks !!

2

2 Answers

1
votes

I know it has been a while you asked this question, but I'm going to answer it anyways for future viewers. This is what worked for me.

Usually, the response from db connecter is a hash map. You will have to reference groovy file to mock db response.

MOCK example:

<mock:when messageProcessor="db:select" doc:name="Mock - Evaluate Results">
    <mock:with-attributes>
        <mock:with-attribute name="doc:name" whereValue="FetchDetails"/>
    </mock:with-attributes>
    <mock:then-return payload="#[resultOfScript('FetchDetailsGroovyScript')]"/>
</mock:when>

Also include the following line to munit config.xml to reference groovy script.

<scripting:script name="FetchDetails" engine="groovy" file="mock-data/Fetch-Details-Groovy-Script.groovy" doc:name="Script"/>

Groovy script example: Fetch-Details-Groovy-Script.groovy

    def map1 = new org.mule.util.CaseInsensitiveHashMap()
map1.put('COUNT',0)


def listOfMap = new LinkedList<org.mule.util.CaseInsensitiveHashMap>()
listOfMap.add(map1)

return listOfMap
0
votes

Your syntax in the mock:then-return isn't valid.

You're missing a couple things:

  1. The pound sign to begin a MEL expression is missing. You should have something that looks like this: #[...code goes here...]
  2. The map isn't being created correctly. You want something like this: {COUNT: 1}

So your mock:then-return should look like this:

<mock:then-return payload="#[[{COUNT: 1}]]"/>