Mule Functional Tests - totally confused - esb

We have a Mule application with 6 or seven flows with around 5 components per flow.
Here is the setup.
We send JMS requests to an ActiveMQ Queue. Mule listens to that. Based on content of the message we forward that to corresponding flows.
<flow name="MyAPPAutomationFlow" doc:name="MyAPPAutomationFlow">
<composite-source>
<jms:inbound-endpoint queue="MyAPPOrderQ" connector-ref="Active_MQ_1" doc:name="AMQ1 Inbound Endpoint"/>
<jms:inbound-endpoint queue="MyAPPOrderQ" connector-ref="Active_MQ_2" doc:name="AMQ2 Inbound Endpoint"/>
</composite-source>
<choice doc:name="Choice">
<when expression="payload.getProcessOrder().getOrderType().toString().equals("ANC")" evaluator="groovy">
<processor-chain>
<flow-ref name="ProcessOneFLow" doc:name="Go to ProcessOneFLow"/>
</processor-chain>
</when>
<when....
...........
</choice>
</flow>
<flow name="ProcessOneFLow" doc:name="ProcessOneFLow">
<vm:inbound-endpoint exchange-pattern="one-way" path="ProcessOneFLow" responseTimeout="10000" mimeType="text/xml" doc:name="New Process Order"/>
<component doc:name="Create A">
<spring-object bean="createA"/>
</component>
<component doc:name="Create B">
<spring-object bean="createB"/>
</component>
<component doc:name="Create C">
<spring-object bean="createC"/>
</component>
<component doc:name="Create D">
<spring-object bean="createD"/>
</component>
</flow>
<spring:beans>
<spring:import resource="classpath:spring/service.xml"/>
<spring:bean id="createA" name="createA" class="my.app.components.CreateAService"/>
<spring:bean id="createB" name="createB" class="my.app.components.CreateBService"/>
<spring:bean id="createC" name="createC" class="my.app.components.CreateCService"/>
<spring:bean id="createD" name="createD" class="my.app.components.CreateDService"/>
......
......
</spring:beans>
Now I am not sure how I can write Functional tests with them.
I went through the Functional Testing documentation in Mule website but there they have very simple tests.
Is Functional Testing not supposed to make actual backend updates using DAO or Service layers or is it just an extension of Unit tests where you mock up service layer?
I was of the idea - it can take in a request and use the inmemory Mule server to pass the request-response from one component to another in a flow.
Also kindly note there is no Outbound endpoint for any of our flows as they are mostly Fire and Forget type flows and status updates are managed by the DB updates the components do.
Also why do I need to create separate mule config xml files for tests? If I am not testing the flow xml that will actually be deployed on Live what's the point of this testing? I f I am creating separate xml configs just for tests that somewhat defeats the purpose to me...
Can some expert kindly elucidate a bit more and point to example tests similar to the ones we are using.
PS: the components inside Mule are dependent on external systems like webservices, databases etc. For Functional tests do we need to have those running or are we supposed to mock out those services/Db Access?

Functional testing your Mule application is no different from testing any application that relies on external resources, like databases or JMS brokers, so you need to use the same techniques you would do with a standard application.
Usually this means stubbing the resources out with in-memory implementations, like HSQLDB for databases or a transient ActiveMQ in-memory broker for JMS. For a Mule application, this implies modularizing your configuration so "live" transports are defined in a separate file, which you replace with one that contains the in-memory variants at testing time.
To validate Mule had the correct interaction with the resource, you can either read the resource directly using its Java client (for example JDBC or JMS), which is good if you want to ensure that purely non-Mule clients have no issue reading what Mule has dispatched, or use the MuleClient to read from these resources or create flows that consume these resources and pass messages to the <test:component>.
FYI These different techniques are explained and demonstrated in chapter 12 of Mule in Action, second edition.

https://blog.codecentric.de/en/2015/01/mule-esb-testing-part-13-unit-functional-testing/
https://developer.mulesoft.com/docs/display/current/Functional+Testing
Please refer this links
As you can see, it's an ordinary JUnit test extending FunctionalMunitSuite class.
There are two thing we need to do in our test:
Prepare MuleEvent object as an input to our flow. We can do that by using provided testEvent(Object payload) method.
Execute runFlow(String flowName, MuleEvent event) method specifying flow name to test against and event we just created in the first step.

Related

Testng listener to comply with Apache Ant JUnit XML Schema

As part of a testng automation test suite I would like to automatically push results from jenkins to testrail. I currently have this plugin installed on my jenkins server: https://github.com/jenkinsci/testrail-plugin
The read me states the output must comply with the junit schema: https://github.com/windyroad/JUnit-Schema/blob/master/JUnit.xsd
I have reference How do I get one junit report from TestNG for all my test cases? and added
<listeners>
<listener class-name="org.testng.reporters.JUnitXMLReporter"></listener>
</listeners>
to my listeners; however, this does not seem to create a file in the correct format as this causes jenkins to fail with the message :
Uploading results to TestRail.
Error pushing results to TestRail
Posting to index.php?/api/v2/add_results_for_cases/236 returned an error! Response from TestRail is:
{"error":"Field :results cannot be empty (one result is required)"}
Build step 'TestRail Plugin' marked build as failure
Finished: FAILURE
I am wondering if there is a different listener I should be using instead.
Thank you for the help.
I used the xsd file that was shared in the question to create a TestNG reporter that complies with the xsd.
To consume this reporter, please add a dependency as below
<dependency>
<groupId>com.rationaleemotions</groupId>
<artifactId>junitreport</artifactId>
<version>1.0.0</version>
</dependency>
This reporter makes use of the service loader approach to wire in itself. So it doesn't need to be added explicitly via the <listeners> tag (or) the #Listeners annotation.
Details can be found here

How to send messages from server to client?

I am going to implement something similar to Facebook notification and this website (StackOverflow's notification which is notifying us if anyone write a comment/answer etc for our question). Please note users are going to use my application as a website not mobile application.
I came across following answer which fetch the results, but I need to push the results not fetch.
Based on suggestions I have created a simple method in my entity class and added the #PostPersist to it but it has not worked so based on this answer I added the persistence.xml file to define the listeners but after session.save(user) the aftersave method does not get triggered.
User.java
#Entity
public class User{
.....
#PostPersist
public void aftersave(){
System.err.println("*****this is post persist method****");
}
}
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<property name="hibernate.ejb.event.pre-insert" value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.pre-update" value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.pre-delete" value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.post-insert" value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.post-update" value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.post-delete" value="my.hibernate.events.listeners.Listener" />
pom.xml
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.1.Final</version>
<type>jar</type>
</dependency>
Sounds like a task for WebSockets. It is part of Java EE 7 so the Glassfish should be one of the first AS that will support it.
For intercepting the DB access you can use #PostUpdate and #PostPersist. Here is related question.
There are many ways to do the so called Server Push for notifying the connected clients:
polling (the link you've provided in the question ("Are we there yet? Are we there yet? ..."))
long polling (smarter way of polling - long-lived HTTP technique using keepalive messages)
WebSockets (JSR 356)
piggy-backing
SPDY(wiki)
Server-Sent Events (related answer, wiki)
EDIT: In the Java world, there are couple of frameworks where server push (reverse ajax) is implemented out-of-the box. If you are familiar with GWT, I would suggest Errai. Other alternative is the Atmospere. The downside of the Atmospere is the fact that it requires standalone running process next to your regular application server with your web app. I was playing with it a year ago so this may have been changed since then.
In general, it is hard to provide you with a concrete piece of code, because it depends on the framework you will choose. I am familiar with Errai so here is an example in it:
Server Side:
#ApplicationScoped
public class TickerService {
#Inject
private Event<Tick> tickEvent;
private void sendTick() {
tickEvent.fire(new Tick());
}
}
Client Side:
#EntryPoint
public class TickerClient {
public void tickHappened(#Observes Tick tick) {
// update the UI with the new data
}
}
Other benefits of using the Errai is having the CDI on the server and on the client out-of-the-box, another thing that is nice is using the web-sockets under the covers if it is supported and falling back to other solutions otherwise.
Whatever you choose, it should fit to your existing infrastructure and to your client side UI framework.
mqtt can be used for server pushing and message broadcasting.
There are more detail information in http://mqtt.org/.
======================================
Updated: Jul 11, 2013
Mqtt is a publish/subscribe, extremely simple and lightweight messaging protocol. If server is a publisher and client browser subscribe the topic which server publish to, then server can push message to client directly.
Some useful resource:
Mosquitto is an open sourced mqtt server. Easy to install and configure.
mqtt-client is a proven powerful java mqtt client.
Use Node JS and socket.io
This technology chooses the best transportation method based on the browser that the client is using.
For latest browsers it uses Web Sockets and for others it degrades gracefully to Flash Socket or Long Pooling. See more here
What you need to do is set up a server using these technologies. The server would run at a particular port. All clients would listen to that port and server would be able to push data to the client through that port.
Comet also known as Reverse Ajax, is a web application model in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it.
Comet (AKA long lived http, server push) allows the server to start answering the browser's request for information very slowly, and to continue answering on a schedule dictated by the server. For more information about Comet, see the following:
Alex Russell's original post coining the term
The Wikipedia article on Comet
Comet Daily, a blog with regular posts on the subject
DWR is a Java library that enables Java on the server and JavaScript in a browser to interact and call each other as simply as possible.
With Reverse Ajax, DWR allows Java code running on a server to use client side APIs to publish updates to arbitrary groups of browsers. This allows interaction 2 ways - browser calling server and server calling browser. DWR supports Comet, Polling and Piggyback (sending data in with normal requests) as ways to publish to browsers.
DWR provides integration with Spring, Struts, Guice, Hibernate and others.
You can read more from here.
Other Comet and Reverse AJAX frameworks:
Grizzly Comet API
Atmosphere
AJAX push with ICEfaces
Asynchronous Servlet using Servlet 3.0
but after session.save(user) the aftersave method does not get triggered.
#PostPersist is a JPA callback.
session.save() is a non-JPA, hibernate proprietary method. JPA uses entityManager.persist().
you're using incompatible features
Check for update from server on every 30 Seconds or as per requirement.
window.setInterval(function(){
/// call your function here
//Make AJAX call
//Update Respective HTML Contact i,e, DIV
}, 30000);

Help with Castle Windsor XML configuration

I have the following three components defined in the Caste-Windsor XML configuration for my application:
<component id="StringFactory"
service="IStringFactory, MyApp"
type="DefaultStringFactory, MyApp"
lifestyle="singleton"
/>
<component id="TheString"
type="System.String"
factoryId="StringFactory"
factoryCreate="CreateString"
>
<parameters>
<name>SomeString</name>
</parameters>
</component>
<component id="TheTarget"
service="ITarget, MyApp"
type="TheTarget, MyApp"
lifestyle="transient"
>
<parameters>
<aString>${TheString}</aString>
</parameters>
</component>
And the following facility defined:
<facility id="factory.support"
type="Castle.Facilities.FactorySupport.FactorySupportFacility, Castle.MicroKernel"
/>
When I run the application and set a breakpoint in the constructor of the TheObject class, the value passed in as the aString parameter is "${TheString}" when I expect it to resolve to the value of the component with that name.
Also, I have a breakpoint in the StringFactory constructor and CreateString method, neither of which are hit. I know the configuration is being used as other components are resolving correctly.
What am I missing or doing wrong here?
UPDATE
In light of the huge tangient this topic has taken, I've refactored the code above to remove anything to do with connection strings. The original intent of this post was about injecting a property with the value returned from a method on another object. Somehow that point was lost in a discussion about why I'm using XML versus code-based configuration and if this is a good way to inject a connection string.
The above approach is far from an original idea and it was pulled from several other discussions on this topic and our requirements are what they are. I'd like help understanding why the configuration as it is in place (whether the right approach or not) isn't working as expected.
I did verify that the first two components are being instantiated correctly. When I call Container.Resolve("TheString"), I get the correct value back. For whatever reason, The parameter syntax is not working correctly.
Any ideas?
While not a definitive solution to what I need to do in my application, I believe I've figured out what is wrong with the code. Or at least I've found a way to make it work which hints at the original problem.
I replaced the String type for TheString with a custom class. That's it. Once I did that, everything worked fine.
My guess is that it has something to do with the fact that I was trying to use a ValueType (primitive) as a component. I guess Castle doesn't support it.
So, knowing that's the case, I can now move on to figuring out if this approach is really going to work or if we need to change direction.
UPDATE
For the sake of completeness, I thought I'd go ahead and explain what I did to solve my problem AND satisfy my requirements.
As before, I have access to my configuration settings through an IConfigurationService defined as:
<component id="ConfigurationService"
service="MyApp.IConfigurationService, MyApp"
type="MyApp.RuntimeConfigurationService, MyApp"
lifestyle="singleton"
/>
This is automatically injected into my (new) IConnectionFactory which is responsible for generating IDbConnection objects based on the connection strings defined in the application's configuration file. The factory is declared as:
<component id="ConnectionFactory"
service="MyApp.Factories.IConnectionFactory, MyApp"
type="MyApp.Factories.DefaultConnectionFactory, MyApp"
lifestyle="singleton"
/>
In order to resolve what connection is used by my repository, I declare each connection as a component using the ConnectionFactory to create each instance:
<component id="MyDbConnection"
type="System.Data.IDbConnection,
System.Data, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"
factoryId="ConnectionFactory"
factoryCreate="CreateConnection"
lifestyle="transient"
>
<parameters>
<connectionStringName>MyDB</connectionStringName>
</parameters>
</component>
Notice the fully described reference to System.Data. I found this is necessary whenever referencing assemblies in the GAC.
Finally, my repository is defined as:
<component id="MyRepository"
service="MyApp.Repositories.IMyRepository, MyApp"
type="MyApp.Sql.SqlMyRepository, MyApp.Sql"
lifestyle="transient"
>
<parameters>
<connection>${MyDbConnection}</connection>
</parameters>
</component>
Now everything resolves correctly and I don't have ANY hard-coded strings compiled into my code. No connection string names, app setting keys or whatever. The app is completely reconfigurable from the XML files which is a requirement I must satisfy. Plus, other devs that will be working with the solution can manage the actual connection strings in the way they are used to. Win-win.
Hope this helps anyone else that runs into a similar scenario.
You don't really need XML registrations here, since you probably don't need to swap components or change the method used without recompiling. Writing a configurable app does not imply having to use XML registrations.
The problem with this particular XML registration you posted is that the connection string is a parameter, but it's treated like a service.
Doing this with code registrations is much easier, e.g.:
var container = new WindsorContainer();
container.Register(Component.For<IConfigurationService>().ImplementedBy<RuntimeConfigurationService>());
container.Register(Component.For<ITheRepository>().ImplementedBy<TheRepository>()
.LifeStyle.Transient
.DynamicParameters((k, d) => {
var cfg = k.Resolve<IConfigurationService>();
d["connectionString"] = cfg.GetConnectionString();
k.ReleaseComponent(cfg);
}));
Or if you don't want to depend on IConfigurationService, you could do something like:
container.Register(Component.For<ITheRepository>().ImplementedBy<TheRepository>()
.LifeStyle.Transient
.DependsOn(Property.ForKey("connectionString")
.Is(ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["connName"]].ConnectionString))

Spring integration delayer element

I'm trying to configure my spring integration and want to use the element, which basically sits between a retryFilter and a queue.
All works fine if I go straight from the retryFilter to the queue, however, as soon as I put the delayer element in between them, the config file fails to be loaded (as happens when there is an error in it).
Config for this section is as follows:
<!-- Retry filter -->
<filter
input-channel="retryChannel"
ref="retryFilter"
method="doRetry"
output-channel="queueChannel" />
<channel id="delayChannel" />
<delayer input-channel="delayChannel" default-delay="10000" output-channel="queueChannel"/>
<channel id="queueChannel">
<queue capacity="100" />
</channel>
<poller id="poller" default="true">
<interval-trigger interval="1000"/>
</poller>
Any help greatly appreciated.
Dave
I've tried out your sample and got it working fine on Spring Integration 2.0.0.BUILD-SNAPSHOT. You can see my commit here:
http://github.com/iwein/Spring-Integration-Sandbox/commit/c274a12f057b6750dcf18663486a99970368e68e
There are a couple of things I changed:
channel renames (in, out) instead of
longer names
filter outputs to
delayer input, instead of passing by
the delayer
Are you using an older version of Spring Integration perhaps?
You can check out my little gradle project ( http://github.com/iwein/Spring-Integration-Sandbox/tree/master/quick-samples/router-test/) which could help you experiment. If you still can't get it working it would be good if you shared a stacktrace and the exact version you are using.

Log4net - log parts of code, used in a couple of methods

I have some trouble.
My application could be divided to 3 logical parts (import, processing and export). There are some parts of code which are used in several parts of my application. How can I determine which part of code called my log4net object?
What is best practice to log info in parts of code which are called from several places in the application?
I want to turn on and off the ability to log parts of my application from a config file.
If I turn off logging for the processing part of my app, how could I log info in the export part of my app when both of them use one method, in which I initialize my logger object?
You could add a separate logger for each section of your app that you want to log and then turn them off and on as needed. They would all be independent from one another and this can all be setup via the config.
By setting the additivity property to false, the loggers will all be independent of one another. Here's an example of the config portion:
<logger name="Logger1" additivity="false">
<level value="INFO" />
<appender-ref ref="Logger1File" />
</logger>
To use it in your code, reference it like this:
private static ILog _Logger1= LogManager.GetLogger("Logger1");
Anything you log to Logger1 will be separate from any other logger, including the root one.
log4net provides contexts for this purpose. I would suggest using a context stack like this:
using(log4net.ThreadContext.Stacks["Part"].Push("Import"))
log.Info("Message during importing");
using(log4net.ThreadContext.Stacks["Part"].Push("Processing"))
log.Info("Message during processing");
using(log4net.ThreadContext.Stacks["Part"].Push("Export"))
log.Info("Message during exporting");
The value on the stack can be shown in the logs by including %property{Part} in a PatternLayout.