0

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 !!

user3368821
  • 79
  • 2
  • 9

2 Answers2

1

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
vennapu
  • 45
  • 7
0

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}]]"/>

jerney
  • 2,187
  • 1
  • 19
  • 31
  • Sorry, Jerney, it throws an error stating that: Caused by: [Error: unresolvable property or identifier: COUNT] – user3368821 Sep 04 '18 at 16:05
  • Oh I think I see the issue. Your code is expecting a List, I'm asking you to return a Map in the mock. I've edited my answer so it returns a list containing `{COUNT: 1}`, please give that a try! – jerney Sep 04 '18 at 20:01
  • Hello Jerney.. no luck same error i am getting Caused by: [Error: unresolvable property or identifier: COUNT] – user3368821 Sep 04 '18 at 20:32
  • Where is the error thrown? What is the state of the payload after the mock? – jerney Sep 04 '18 at 21:01
  • Hello Jerney, in the mock as you suggested i am using the expression as #[{COUNT=0}], in the mule flow right after the db call i have a choice component where i have wrote a expression as #[payload[0]['COUNT'] == 0] to check whether the count is greater than zero or not and this is the place where it fails in the munit but when i execute the flow without munits it works as expected. I want to mock the response of the database select (count query) to 1 and zero in the munit test and thats why i have asked the question !! – user3368821 Sep 05 '18 at 12:15
  • Total error is --> Caused by: org.mule.api.expression.ExpressionRuntimeException: Execution of the expression "payload[0]['COUNT'] == 0" failed. at org.mule.el.mvel.MVELExpressionLanguage.evaluateInternal(MVELExpressionLanguage.java:232) at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:27) Caused by: [Error: java.lang.Integer cannot be cast to java.lang.Class] [Near : {... payload[0]['COUNT'] == 0 ....}] – user3368821 Sep 05 '18 at 12:22
  • Figure out the difference between what the mock returns, and what your code normally returns, and there's your issue. – jerney Sep 05 '18 at 22:45