Tag Archives: TDD

How to unit test CQL statements in an Oracle Complex Event Processor version 11.1

Although the Oracle Complex Event Processor version 11.1 is a very powerful  tool, it doesn’t come with a mechanism for testing CQL statements in isolation to the rest of the application.

A while ago a client wanted me to implement an application to leverage the CQL language shipped with the Oracle Complex Event processor version 11.1.1.7 for routing important financial messages around an internal business domain.

This meant that there was a lot of emphasis placed on the routing rules themselves, therefore there was a need to perform  rules testing.  Naturally, common sense dictated that a semi-automated test harness was the best was forward – quick, robotic therefore reliable, robotic therefore deterministic, robotic therefore executable at any time, robotic therefore … you get the picture.

Naturally the rest of the application’s java code was covered by unit and unit integration tests which were easy to write and exercised, well, robotically.

The wrinkle in our robotic CQL testing concept was that it required a fully operational Oracle Complex Event Processor to be running,  in order for the rules to be fired in something that approximates production. As we all know from TDD and other testing approaches, our CQL testing  concept is well beyond the unit test scope where mocking resources is the norm.

The solution? Hack a running CEP, replacing the internal publishers and consumers around each CQL processor within the EPN with dummy producers and consumers.  The  dummy producer then injects a pre-constructed canonical object into the feed channel to the CQL processor in question while multiple consumers are attached to the outbound consumers after the CQL processor.

The stage is then set to inject and  retrieve crafted canonical objects designed probe a particular CQL rule.

Easy, right?

Well, in fact its not as hard as you think.  Dynamically changing the behaviour of an application by INJECTING new code during runtime is not a new idea (see aspect oriented programming and ilk), being able to do code injection through a web interface by dropping in another application into an application container is new indeed. And I suspect, may be useful in AI operations – more on this in a later blog.

OSGi with resource sharing is the key here . For those unfamiliar with OCEP, it is based on a standard weblogic application container, but is flavoured with an OSGi application management layer.

What is this OSGi thing? Wikipedia tells us it is “OSGi (Open Service Gateway Initiative) is a Java framework for developing and deploying modular software programs and libraries“. Not a very useful explanation.  Practically, OSGi allows multiple programs to share resources through public interfaces, meaning that an app can be sand-boxed for all intents an purposes, except when you don’t want it to be. This is “advertising” in OCEP/OSGi speak.

In this case, when you set a channel to be “advertised”, another application via a discovery mechanism which leverages the subscription mechanism in the OSGI BundleContext, get access to that channel and reprogram the channel to use a set of publishers and consumers different from that which was originally deployed.

OK. Baffling. Here is the howto:

  1. within the META-INF/spring/[myapplicationcontext.xml] set all channels around your CQL processors in your application using the wlevs:channel attribute setting “advertise=true”
  2. create a new application, the one which contains all of the testing artefacts – dummy publishers and consumers, test canonical object construction code, test data that is converted into the test canonical objects
  3. finally write a utility in the new application which gets references to all live channels, reconfigures them to use new publishers and listeners, injects the test canonical objects and grades the routing outcome by detecting which listener should and should not have received the canonical object.

Again, easy right?

In my code repository you can find code snippets which you can use, free, to reconstruct the test harness. Sadly I can’t give you cut and paste code, as the implementation is customised to your real application test cases.  But you will get the idea.

The client took some convincing that it was worth spending the 5 or so days building this test harness, but the results speak for themselves 4 years on. Given the 150+ routing pathways through the application and the many corner cases associated, being able to prove and re-prove the rule behaviour during each release has eliminated hundreds of hours troubleshooting and thousands of dollars in commercial side effects by avoiding production incidents when compared to the previous non-OCEP implementation.

Happy testing!

Testing my patience or “Why do we make this so hard for ourselves?”

Anyone following my intermittent rants on rubbish code would surely have got the sense that I don’t tend to suffer fools too gladly. It is also no secret that I don’t believe in commoditising the act of writing code.  The results are always poor and always expensive.

Here, I fear, is yet another example of why I feel this way.

Recently I was wondering aloud with an offshore colleague how it can be that a simple bug which we budgeted half a day to fix was taking 3 days and as almost many attempts to rectify. Paraphrasing for you:

Me: Show me your unit tests

Them: Um.

Me: You DO have unit tests, right?

Them: Um. Well. Yes but they have been disabled.

Me: … Really?  Please tell me why.

Them : Not sure.

Me: Let me see if I have got this right. You have attempted to fix a simple bug and its taken you two failed attempts over three days and you STILL don’t have  unit tests?

Them: …. [silence} … [a sort of gargling sound, which I put down to a noise representing an uncomfortable mental state] … Um.

Me: [swearing internally] OK. Show me your code.

Therein ensued a 45 minute discussion which I shall summarise for you the dear and busy reader:

  1. Colleague did not know why the existing unit tests were disabled
  2.  Colleague discovered why. They didn’t work.  Better out of sight and out of mind, eh?
  3.  I (strongly) suggest that the key methods be broken up into smaller test-able methods.  This was done
  4.  New unit tests were written
  5.  Unit tests were then rewritten as esteemed Colleague had not understood the concept of determinism i.e. you put values into the test with the expectation that they will produce a predictable result
  6. Unit tests now passed
  7. Third bugfix attempt was deployed to test
  8. Third bugfix attempt was passed by the tester
  9.  For completion, the colleague was then advised to write unit integration tests.  This was initially deemed “impossible” due to a dependency on external services, to which I issued a sharp “rubbish!” in reply
  10.  Mocking of the services and creation of the unit integration test then went ahead smoothly, once I pointed out that it is indeed possible to have more than one constructor per class (sic)
  11.  Unit integration tests well under way
  12.  *sigh*

I am going to take up drinking hard stuff on a regular basis to thin my blood if I see more of this sort of behaviour

My heart can’t take it.