MuleSoft - how to catch an error/exception into Munit test? - mule

Into a flow i raised and error and i would like to test it in Munit.
The documentation doesn't seem to contain and explain this particular case.
what is the text that i need to insert into :
expected error type
expected exception

All errors thrown in Mule contain meta-data including a TYPE.
For example, here is a list of some specific HTTP: errors thrown by the HTTP module:
HTTP:UNSUPPORTED_MEDIA_TYPE
HTTP:CONNECTIVITY
HTTP:INTERNAL_SERVER_ERROR
HTTP:METHOD_NOT_ALLOWED
Each module's documentation should contain all specific error types thrown by that module. Here is the HTTP one example: https://docs.mulesoft.com/connectors/http/http-documentation#throws
In your screenshot for example it uses APIKIT module. APIKIT module has its own errors again. Think of it as certain Java classes throwing custom exceptions specific to that class.
And here is a full list of core error types you can catch like EXPRESSION for example:
https://docs.mulesoft.com/mule-runtime/4.1/mule-error-concept
The attribute expectedErrorType expects an error type ID that needs to be defined inside the application being tested. This attribute allows you to validate that a defined error type in your application is thrown. If you define an errorType that does not exists in your application, the test does not run.
<munit:test name="MUnit-test-suite"
description="Test Error Type"
expectedErrorType="FTP:ILLEGAL_PATH">
...
</munit:test>
This Error Type test expects that an FTP operation will throw an FTP:ILLEGAL_PATH error.
You will need to configure this for your specific type you are expecting.
The attribute expectException expects a literal exception class name (canonical form). When you provide a literal value, it should take the form of the canonical class name of the exception that is expected. In these cases, Mule always throws a MuleMessagingException. MUnit validates the provided classname if the underlying cause of the MuleMessagingException thrown is of the exact same type.
<munit:test name="testExceptions"
description="Test Exceptions"
expectedException="java.lang.RuntimeException">
...
</munit:test>
If you define that your test expects an exception and none is thrown, the test fails immediately.
So you don't need to provide both.

Related

How can I use Format Arguments in BusinessException?

I have noticed the abp localization provide a Format Arguments mechanism to help generate realtime local string by this way, and I want to know how can I do the same thing in calling a BusinessException while all its overloads are not suitable for this purpose.
Please see the documentation: https://docs.abp.io/en/abp/latest/Exception-Handling#exception-localization
It is possible to set an exception code and data related to the exception. Then ABP automatically localizes the exception message by also using the data arguments you've provided.
Example exception:
throw new BusinessException("App:010046")
.WithData("UserName", "john");
And the related localization entry in the json file:
"App:010046": "Username should be unique. '{UserName}' is already taken!"
It is not using {0}, {1}... but using parameter names instead.

Java error from distriqt Adverts extension

I'm trying to get the distriqt Adverts extension to work in my AIR app.
On every device that I try my app on, I get that Adverts.service.interstitials.isSupported returns false.
On one device, I also get these Java errors, which is probably the reason why:
Error #2044: Unhandled error:. text=Attempt to invoke virtual method 'java.lang.String com.adobe.fre.FREObject.getAsString()' on a null object reference
Error #2044: Unhandled error:. text=Attempt to invoke interface method 'boolean com.distriqt.extension.adverts.platforms.AdvertPlatform.isInterstitialsSupported()' on a null object reference
I checked and double checked that all required extension are listed in the <extensions> section in the application descriptor xml, and that they are actually included in the APK.
Here is an excerpt of the AS code:
if (Adverts.isSupported)
{
var r:int = GoogleApiAvailability.instance.isGooglePlayServicesAvailable();
if (r == ConnectionResult.SUCCESS)
{
Adverts.service.initialisePlatform(AdvertPlatform.PLATFORM_ADMOB, myAppId);
if (Adverts.service.interstitials.isSupported)
{
// etc...
}
else trace("Interstitials not supported");
What am I doing wrong, or what could I do to find out what I am doing wrong?
(Could not add the adverts tag to this question because not enough reputation)
Check what the value of your myAppId is.
That first error would indicate that it is null and the initialisePlatform() call is failing.

apache camel REST failing with [Error creating bean with name 'rest-configuration']

My route looks like below -
#Override
public void configure() throws Exception {
from("kafka:adapterTopic")
.to("rest://post:gatewayinbound-dev11.devsite.com");
}
I have tried with this as well,
.to("rest:post:gatewayinbound-dev11.devsite.com");
and this as well,
restConfigutation().host("gatewayinbound-dev11.devsite.com");
from("kafka:adapterTopic")
.to("rest:post:provideStatus/");
I have tried with camel-rest-starter dependency in the classpath and without it as well.
I have tried putting camel-rest instead of camel-rest-starter in the pom.
But nothing is making the exception go away, below is the stacktrace-
{"timestamp":"2020-04-21 18:17:45.327","severity":"ERROR","class":"org.springframework.boot.SpringApplication","crId":"","msg":"Application run failed","exception":"org.apache.camel.RuntimeCamelException","cause":"org.apache.camel.FailedToCreateRouteException: Failed to create route route10 at: >>> To[rest:post:gatewayinbound-dev11.devsite.com] <<< in route: Route(route10)[[From[kafka:adapterTopic]] -> [To[rest:... because of Failed to resolve endpoint: rest:\/\/post:gatewayinbound-dev11.devsite.com due to: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'rest-configuration' defined in class path resource [org\/apache\/camel\/model\/rest\/springboot\/RestConfigurationDefinitionAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.camel.spi.RestConfiguration]: Factory method 'configureRestConfigurationDefinition' threw exception; nested exception is java.lang.IllegalArgumentException: Cannot configure option [useXForwardHeaders] with value [true] as the bean class [org.apache.camel.spi.RestConfiguration] has no suitable setter method, or not possible to lookup a bean with the id [true] in Spring Boot registry"
Please help me here. Looking at the examples at Camel's website it looks quite easy to configure a rest producer endpoint but for me it has been very difficult.
Edit-
As Rest component is part of camel-core, I was using camel-core-2.22.1.
Then as suggested by Raúl Cancino, I tried this as well-
to("rest:post:provideStatus?host=gatewayinbound-dev11.devsite.com:443")
please try the following uri format on your to(), as a starting point:
to("rest:post:provideStatus?host=gatewayinbound-dev11.devsite.com:443")
then you can switch to rest configuration
restConfiguration().host("gatewayinbound-dev11.devsite.com:443");
also, using camel-http this would be:
.setHeader(Exchange.HTTP_METHOD,constant(org.apache.camel.component.http.HttpMethods.POST))
.to("https:gatewayinbound-dev11.devsite.com/provideStatus?bridgeEndpoint=true")
Hope it works for you
The problem was with the version of camel-core library my applcation was using (as transitive dependency), when I switched to camel-core-2.24.2 the problem got resolved.
I compared org.apache.camel.spi.RestConfiguration classes from both versions of camel-core, 2.22.1 and 2.24.2 and found that property useXForwardHeaders was missing in older version.

How to fetch the #[app.name] , environment variable and #[exception.getMessage()] in Mule dataweave

Actually what I am trying to do is whenever exception/error occurs in application it will come to catch exception strategy, here I'm trying to create a xml request which contains application name, timestamp and error details and calling one rest service with this xml as input. Could you please help me in doing this..?? Thanks in Advance
There nothing you can do access those expressions in dataweave, you might need to store those in flowVars then access the flowVars in dataweave like flowVars."name_of_var".
Regards,
Ralph
You can find error handling block in your flow.
Inside that you can catch the exception with the MEL syntax.
#[Exception.causedBy(corresponding class)]
once if there is an exception based on some class then inside that you can define your strategy.
In general you can catch any exception by #[Exception!=null]
If there is any exception occurs automatically the control will be passed here.
In that you can setpayload #[app.name], #[server.dateTime],#[Exception.getMessage()]
Then you can proceed as you want.
Thanks!

How to read properties from .properties file in Mule

I'm trying to use Mule Credentials Vault security feature.
I've created .properties file, Security Property Placeholder and defined the key and encryption algorithm.
Now I want to use some of the properties from the file when I return HTTP response.
I have the file src/main/resources/data.properties that contains for example:
In my canvas, under Configuration XML I added:
<secure-property-placeholder:config name="Secure_Property_Placeholder" key="24681357" location="data.properties" doc:name="Secure Property Placeholder" encryptionAlgorithm="DES"/>
<set-variable variableName="card.number" value="${number}" />
In my canvas I have message flow that builds xml 'Create XML response based on User'. The value in settings is:
This doesn't work. The error I get is:
-> org.mule.module.launcher.DeploymentInitException: IllegalArgumentException: Could not resolve placeholder 'key' in string value "${key}"
-> Caused by: org.mule.api.lifecycle.InitialisationException: Invalid bean definition with name 'org.mule.autogen.bean.13' defined in null: Could not resolve placeholder 'key' in string value "${key}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'key' in string value "${key}"
-> Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'key' in string value "${key}"
Does anyone know how can I read the properties from .properties file (credentials vault)? And then use it in my flow?
Thanks,
Keren
If you simply want to get the value for the property number and add it into the XML you can use ${number} from .properties. No need to define any other variables in Configuration XML.
<set-payload value="<user><name>Royal Bank of Canada</name><id>Royal_Bank_Of_Canada</id><cc><company>>Visa</company><number>${number}</number><secret>123</secret></cc></user>" doc:name="Set Payload"/>
However note that the property placeholder is resolved at startup so you will not be able to dynamically retrieve a property based on some user input. For this you will have to do some Java coding. This SO post gives you some hints on how this can be achieved. Based on those answers I have created a simple example on how this can be done with a very simple helper bean.
I'm afraid you just can't. The Mule Credentials Vault is an enterprise feature and therefore tipically you won't have access to the source code unless you are a MuleSoft customer.
Even if you were a customer, the api you'd use would be sort of unsupported. I suggest to manually create a custom java component levearing your code and Jasypt (not as a property placeholder but as a library).
The other option, if you are a customer (I guess you are given you are using the credentials vault) is to contact the official support so they take care of it for you.
The property placeholder is used resolve at startup so you will not be able to dynamically retrieve a property based on some user input.
Use ${propertyName} from .properties in MEL to access particular property
From Dataweave you can read it as given below
p('variablename')
where variablename is defined in property files ex: variablename = 15