-
Get a monthly update on best practices for delivering successful software.

Recently I have taken a bit of a detour into the world of telephony, working with Asterisk-Java, which by itself is a very valuable tool, and worth knowing a bit about if you are integrating a system with Asterisk. While it is a Java-based library, I am integrating it into a Grails application.
We have a fairly comprehensive suite of unit tests asserting that desired behaviors are triggered upon certain events initiated through the Event API. This is made even easier, as usual, with Groovy-- specifically on the testing front. Here I show two hypothetical test cases, followed by supporting code that works specifically with the Asterisk-Java classes...
import org.gmock.GMockTestCase import org.asteriskjava.manager.event.JoinEvent import org.asteriskjava.manager.event.LeaveEvent def queueObject = // .... some object representing an internal queue /* * Verifies caller entering a call queue will trigger behavior */ void testCallIAddedToQueue() { mock(queueObject).add('SIP/123', '3125551234').once() dispatchJoinEvent([channel: "SIP/123", callerId: "3125551234"]) } /* * Verifies caller leaving call queue will trigger behavior */ void testCallRemovedFromQueue() { mock(queueObject).remove("SIP/123").once() dispatchLeaveEvent([channel: "SIP/123", dateReceived: new Date()]) } // ...
The testcases above are fairly straightforward, hiding much of the details involved when working with Asterisk-Java. The 'queueObject' can be anything, really, as long as it contains behavior which we wired into the event-handling system Asterisk-Java provides. The important part here is how we can easily bind properties to the underlying Event types, and dispatch them so that our behavior is invoked.
private def dispatchJoinEvent(properties) { dispatchEvent(JoinEvent, properties) } private def dispatchLeaveEvent(properties) { dispatchEvent(LeaveEvent, properties) } private def dispatchEvent(eventType, eventProperties) { def event = eventType.newInstance([:]) eventProperties.each { name, value -> event."${name}" = value } play { asteriskService.connection.dispatchEvent(event) } }
Here I have a 'service' class defined in Grails which I call 'asteriskService'. It encapsulates access to the Asterisk-Java ManagerConnection instance, which in turn can dispatch specific events.
Of interest to those new with Groovy is the binding of properties within 'dispatchEvent()'. It looks perhaps a bit more complex than one would hope, but unfortunately JoinEvent and LeaveEvent do not have default constructors, requiring the code to 'new' them up with a placeholder argument, and then set any properties on the fly.
Asterisk-Java is worth checking out, but particularly nice is using a bit of Groovy to tie it in nicely. While this can benefit the application, you can see how it helps ease testing-- particularly as you start using more and more of what the library offers.
Related posts:
Topics: asterisk, Grails, Groovy, telephony, Test Driven Development, unit testing
Would it help to have default constructors added to the Event classes in Asterisk-Java? If so feel free to file a Jira at https://secure.reucon.net/issues/browse/AJ and we’ll be happy to add them.
Comment by Stefan, Saturday, October 24, 2009 @ 10:18 am