multipartmulesoftmunit

MuleSoft - set payload contentLength from MUnit Set Event (multipart payload)


I have a Mule app which checks the payload's size using the contentLength metadata selector:

payload.^contentLength <= 512

When I regularly send a request to the Mule app (using Postman / SoapUI), the app works correctly.

However, when I use MUnit Set Event with the same payload, I get the following error:

org.mule.munit.runner.model.TestExecutionException: Error [MULE:EXPRESSION] while running test 'poc7-test-suite-api-mainTest':"Types `Null` and `Number` can not be compared.
Trace:
  at main (Unknown)" evaluating expression: "payload.^contentLength <= 512".

So contentLength was not populated. How can I set it for the test?

If it helps, my MUnit Set Event is defined as follows (using the example from MuleSoft's documentation):

<munit:set-event doc:name="Set Event" doc:id="f48cd1dc-c1bb-40f7-91fc-cc4438a93ad2" >
    <munit:payload value='#[&#10;%dw 2.0&#10;output multipart/form-data&#10;ns soap http://schemas.xmlsoap.org/soap/envelope/&#10;---&#10;{&#10;  parts: {&#10;    part0: {&#10;      headers: {&#10;        "Content-Type": "text/xml",&#10;        "Content-Length": "1602",&#10;        "Content-ID": "&lt;mm7-submit&gt;"&#10;      },&#10;      content: {&#10;        soap#Envelope: "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt; &lt;msg:outboundMessageRequest xmlns:msg=\"urn:oma:xml:rest:netapi:messaging:1\"&gt;  &lt;address&gt;tel:+19585550103&lt;/address&gt;  &lt;address&gt;tel:+19585550104&lt;/address&gt;  &lt;senderAddress&gt;tel:+19585550100&lt;/senderAddress&gt;  &lt;senderName&gt;MyName&lt;/senderName&gt;  &lt;receiptRequest&gt;   &lt;notifyURL&gt;http://application.example.com/notifications/DeliveryInfoNotification/77777&lt;/notifyURL&gt;   &lt;callbackData&gt;12345&lt;/callbackData&gt;  &lt;/receiptRequest&gt;  &lt;outboundMMSMessage&gt;   &lt;subject&gt;hello from the rest of us!&lt;/subject&gt;   &lt;priority&gt;High&lt;/priority&gt;  &lt;/outboundMMSMessage&gt;  &lt;clientCorrelator&gt;567895&lt;/clientCorrelator&gt; &lt;/msg:outboundMessageRequest&gt;"&#10;      }&#10;    },&#10;    part1: {&#10;      headers: {&#10;        "Content-Type": "multipart/mixed; boundary=\"------=_Part_2_3815517668157287202\"",&#10;        "Content-Length": "1602",&#10;        "Content-ID": "&lt;attachment&gt;"&#10;      },&#10;      content: {&#10;        parts: {&#10;          part0: {&#10;            headers: {&#10;              "Content-Type": "text/plain; name=text1.txt",&#10;              "Content-Length": "1602",&#10;              "Content-ID": "&lt;text1.txt&gt;",&#10;              "Content-Location": "bundled/text1.txt"&#10;            },&#10;            content: "This is the text part I want to access\n"&#10;          },&#10;          part1: {&#10;            headers: {&#10;              "Content-Type": "image/png",&#10;              "Content-Transfer-Encoding": "base64",&#10;              "Content-Length": "1602",&#10;              "Content-ID": "&lt;image.png&gt;",&#10;              "Content-Location": "bundled/image.jpg"&#10;            },&#10;            content: "aGk="&#10;          }&#10;        }&#10;      }&#10;    }&#10;  }&#10;}&#10;]' />
</munit:set-event>

Solution

  • The metadata selector can only extract metadata that was previously added to the Mule Event. Because the event is being mocked, I'm not sure you can actually mock metadata. You can try to add the attribute for content length to the event and see if that helps the extractor. Otherwise think of how to calculate the length using sizeOf(). You may need to transform the payload to a String to be able to use sizeOf().