3

I have the following Mule flow that is built to retrieve some details from an external API and then map this to some payload to return to the calling client:

<flow name="get:/clients/{clientId}:api-config">
<http:request config-ref="HTTPS_Request_Configuration" path="/api/clients/{clientId}" method="GET" host="host.com" port="443" doc:name="Find Client by ID">
    <http:request-builder>
        <http:uri-param paramName="clientId" value="#[flowVars['clientId']]"/>
    </http:request-builder>
    <http:success-status-code-validator values="0..599"/>
</http:request>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
...

I have created the following unit test where I mock the HTTP request using the MUnit mock processor:

<munit:test name="get:/clients/{clientId}:api-config-200-application/json-FlowTest" description="Verifying functionality of [get:/clients/{clientId}:api-config-200-application/json]">
<mock:when messageProcessor=".*:.*" doc:name="Mock">
    <mock:with-attributes>
        <mock:with-attribute name="doc:name" whereValue="#['Find Client by ID']"/>
    </mock:with-attributes>
    <mock:then-return payload="#[getResource('sample-client.json').asStream()]" mimeType="application/json"/>
</mock:when>
<set-variable variableName="clientId" value="#['null']" doc:name="clientId"/>
<http:request config-ref="HTTP_Request_Configuration" path="/clients/#[flowVars['clientId']]" method="GET" doc:name="HTTP"/>
<object-to-string-transformer doc:name="http response to string"/>
<munit:assert-true message="The HTTP Status code is not correct!" condition="#[messageInboundProperty('http.status').is(eq(200))]" doc:name="assert that - http.status eq 200"/>
<munit:assert-on-equals message="The response payload is not correct!" expectedValue="..."/>

I'm getting an unusual error when I try and run this test. It seems as if the test is failing due to an HTTP timeout. I don't understand how this is possible when the HTTP request processor is being mocked and therefore no external calls should be getting made?

The error message I'm seeing is as follows:

ERROR 2016-02-19 12:25:59,359 [main] org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : Error sending HTTP request. Message payload is of type: String
Code                  : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. Timeout exceeded (java.util.concurrent.TimeoutException)
  com.ning.http.client.providers.grizzly.GrizzlyAsyncHttpProvider:426 (null)
2. Error sending HTTP request. Message payload is of type: String (org.mule.api.MessagingException)
  org.mule.module.http.internal.request.DefaultHttpRequester:287 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/MessagingException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
java.util.concurrent.TimeoutException: Timeout exceeded
    at com.ning.http.client.providers.grizzly.GrizzlyAsyncHttpProvider.timeout(GrizzlyAsyncHttpProvider.java:426)
    at com.ning.http.client.providers.grizzly.GrizzlyAsyncHttpProvider$3.onTimeout(GrizzlyAsyncHttpProvider.java:274)
    at org.glassfish.grizzly.utils.IdleTimeoutFilter$DefaultWorker.doWork(IdleTimeoutFilter.java:398)
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************
danw
  • 1,608
  • 4
  • 29
  • 48

1 Answers1

3

In your test, why are you making call to http:endpoint here <http:request config-ref="HTTP_Request_Configuration" path="/clients/#[flowVars['clientId']]" method="GET" doc:name="HTTP"/>

ALso, I don't see a <flow-ref...> call to call your actual flow under test.

EDIT:

The basic test structure may follow this skeleton:

     <munit:config name="munit" doc:name="MUnit configuration"/>
        <spring:beans>
            <spring:import resource="classpath:munit-test.xml"/>
        </spring:beans>
        <munit:test name="new-test-suite-get:/clients/{clientId}:api-configTest" description="Test">

         <mock:when messageProcessor=".*:.*" doc:name="Mock">
                <mock:with-attributes>
                    <mock:with-attribute name="doc:name"
                        whereValue="#['Find Client by ID']" />
                </mock:with-attributes>
                <mock:then-return payload="#[getResource('sample-client.json').asStream()]"
                    mimeType="application/json" />
            </mock:when> 
            <set-variable variableName="clientId" value="#['null']"
                doc:name="clientId" />
<!-- Below will make an actual call to the flow -->
            <flow-ref name="get:/clients/{clientId}:api-config" doc:name="Flow-ref to get:/clients/{clientId}:api-config"/>
        </munit:test>
Charu Khurana
  • 4,511
  • 8
  • 47
  • 81
  • Thanks for your comment. This test was generated from an APIKit module and modified to include the mock. I'm quite new to using MUnit so would appreciate any advice if there is a better way to achieve what I'm trying to. The flow I'm testing is in my original post. – danw Feb 22 '16 at 10:34
  • I've added sample test for your flow to get you started – Charu Khurana Feb 22 '16 at 15:07