0

I have an API on Anypoint Platform, it's important specs are:

  1. http scheme
  2. request endpoint = "retrieveInformation"
  3. queryParams = {"key": "value", isAllow: true}

I built the Mule flows using the RAML v1.0 having the above specs, and all functionality works fine. Now, I am trying to develop the mUnit for the mule flows. For a test case I set the below event and call flowReference to Mule flow.

#[{
    headers: {
        'client_space': 'ABCD-1234',
        'client_code': 'XYZ-0001'
    },
    'queryParams': {
        'key': '12345678',
        'isAllow': true
    },
    'requestPath': '/api/retrieveInformation',
    'rawRequestPath': '/api/retrieveInformation',
    'requestUri': '',
    'listenerPath': '/api/*',
    'relativePath': '/retrieveInformation',
    'method': 'GET',
    'scheme': 'HTTP',
    'version': 'HTTP/1.1',
    'localAddress': '/127.0.0.1:8091',
    'remoteAddress': ''
} as Object {
    class: 'org.mule.extension.http.api.HttpRequestAttributes' 
}]

Now running the units, APIKit:Router throws error i.e., cannot cast case from Boolean to String. If I change to " 'isAllow': 'true' ", then APIKit:Router throws valid error APIKIT:NOT_IMPLEMENTED.

Can anyone help how to achieve this scenario without changing API Specs?

Sambit Swain
  • 131
  • 1
  • 13
  • Does this answer your question? [MUNIT4 :java.util.LinkedHashMap cannot be cast to org.mule.extension.http.api.HttpRequestAttributes](https://stackoverflow.com/questions/61928177/munit4-java-util-linkedhashmap-cannot-be-cast-to-org-mule-extension-http-api-ht) – aled Oct 05 '21 at 14:54
  • I followed the steps: right click on router > create test suit abc-test.xml. This caused an error on studio. So, I manually created a test suit and in 1 of the test case, added a SET EVENT with attributes as shown in my post, then invoked the mule flow. This is where I am getting error. – Sambit Swain Oct 05 '21 at 15:05
  • To add more analysis over my post, I changed the API specs by setting isAllow type as String in RAML. Then the mUnit works fine with EVENT attribute 'queryParams': {'isAllow': 'true'}. The problem seems for boolean type only, and boolean type works fine for mule execution, but unit doesn't. – Sambit Swain Oct 05 '21 at 15:13

1 Answers1

1

Internal classes from the connector (class: 'org.mule.extension.http.api.HttpRequestAttributes') are not meant to be used directly in tests. This is not a good idea to implement tests. It is better to try to resolve what error you had originally with the test creation. Be sure to be using the latest version of Studio.

There may be a problem with the API. If it still fails you can create a test suite for a simpler API and then use it as an example on how to build your test cases manually using the HTTP Requester instead of trying to mock the requests. Example:

<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:munit="http://www.mulesoft.org/schema/mule/munit" xmlns:munit-tools="http://www.mulesoft.org/schema/mule/munit-tools" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/munit http://www.mulesoft.org/schema/mule/munit/current/mule-munit.xsd http://www.mulesoft.org/schema/mule/munit-tools http://www.mulesoft.org/schema/mule/munit-tools/current/mule-munit-tools.xsd ">
    <munit:config name="simple-api-spec-apikit-test.xml" />
    <http:request-config name="HTTP_Request_Configuration" basePath="/api">
        <http:request-connection host="localhost" port="8081" />
    </http:request-config>
    <munit:test name="get:\ping:simple-api-spec-config-200-application\json-FlowTest" description="Verifying functionality of [get:\ping:simple-api-spec-config-200-application\json]">
        <munit:enable-flow-sources>
            <munit:enable-flow-source value="simple-api-spec-main" />
            <munit:enable-flow-source value="get:\ping:simple-api-spec-config" />
        </munit:enable-flow-sources>
        <munit:execution>
            <http:request config-ref="HTTP_Request_Configuration" method="GET" path="/ping">
                <http:headers>#[{"Accept":"application/json"}]</http:headers>
            </http:request>
        </munit:execution>
        <munit:validation>
            <munit-tools:assert-that expression="#[attributes.statusCode]" is="#[MunitTools::equalTo(200)]" message="The HTTP Status code is not correct!" doc:name="Assert That Status Code is 200" />
            <munit-tools:assert-that expression="#[output application/java ---write(payload, 'application/json') as String]" is="#[MunitTools::equalTo(MunitTools::getResourceAsString('scaffolder/response/get_200_ping_application_json.json'))]" message="The response payload is not correct!" doc:name="Assert That - Payload is Expected" />
        </munit:validation>
    </munit:test>
</mule>
aled
  • 21,330
  • 3
  • 27
  • 34
  • Thanks, I used the approach you suggested above. Made HTTP Request from Execution section in mUnit, and added the relevant flows in mUnit. But, the APIKit: Router again throws APIKIT:NOT_IMPLEMENTED error. Where as the same mule flow works fine when request sent from postman with boolean queryParams – Sambit Swain Oct 05 '21 at 15:56
  • I recommend to create a new question about the APIKIT:NOT_IMPLEMENTED error providing more details of the API, versions and execution log snippets. There are several possible causes for that to happen. Note that using the class may break with any future releases in Mule, even patches. – aled Oct 05 '21 at 16:05