Viewing By Entry / Main
July 6, 2006
I'm building a REST adapter right now - a CFC that offers an XML POST endpoint for REST web services that essentially wraps and exposes any remote methods on any CFCs you have on your server - and I'm absolutely in love with unit testing for this project!

The REST adapter exposes a single method. You POST an XML packet to it with a 'payload' argument whose root element names a component and has an attribute that specifies the method to call. Child XML elements specify the arguments to pass to the method. The REST adapter understands essentially the (Cold)Spring DTD XML definitions for values, lists, maps and beans and handles inbound arguments in that form and then encodes results in that form.

Because this is such a critical generic component, I've been writing test cases extensively - in advance of writing the actual code - so that I have a good degree of confidence in the resulting code.

It's been an interesting exercise. Write a test case. Run the test. It fails. Write the code in the REST adapter. Run the test. It passes. Well, in theory it does. Rinse and repeat. The nice thing about this is that you're thinking about the API and the error messages, not the implementation. You define the expected behavior, the concrete requirements... then you think about how to make it behave like that.

Try it, you might like it.

Download cfcUnit today!

p.s. I'll look into whether or not I can open source the REST adapter - I suspect other people may find it useful.

Comments

We find it useful indeed! I love everything about web services, plus I think SOAP is not enough... I'd like to see future version of CF supporting JSON RPC; I wasn't sure about built in REST, but your adapter plus a URL Rewrite tool should be a great combination.


I second that it sounds very useful! You might get some argument over whether it's really RESTful, though -- it sounds more like RPC to me since it's method-based rather than resource-based and since there's only one URI that represents many different services depending on the body of the request. But that's a question of terminology, not utility -- whatever you call it, it sounds awesome and I hope you end up being allowed to release it to the community!


Sean, I just came across your article on REST Web services. I'm I correcting in asuming that this related to the ability to consume web services from coldfusion using HTTP Post and Get (binding node in the wsdl). I'm trying to produce a web service that can be consumed in this fashion, instead of RPC.

Thanks -Luis


Luis, yes, XML over HTTP via GET/POST, returning XML. REST sometimes uses PUT and DELETE HTTP operations to provide four CRUD-like functions.


Sean, where exactly can I get the adaptor? I clicked on the link for cfunit thinking that it might be on the site.

-Luis


Luis, you seem to be a bit confused: this is a post about unit testing - in particular, unit testing with cfcUnit, an excellent unit testing framework for ColdFusion. I just happened to mention that what I'm unit testing is a REST adapter that I'm writing at work.

As I said: "I'll look into whether or not I can open source the REST adapter - I suspect other people may find it useful."


Sean, any chance you may release the unit tests for this? I'd love to see some "Real World" test cases for a component like this.

Actually, I'd love to get your thoughts on how to design "Testable" software and how it relates to the whole Bean, DAO, Gateway, Service patterns. I'm battling between merging my Beans and DAOs to have a "Smart/Non-Anemic" model, and I'm also thinking about merging my Service and Gateway objects since services always seem to pass-through calls to my gateways. Obviously if I kept them all separate, I wouldn't have to worry about my persistence layer so much when testing, thus reducing the # of variables. However I'm in a mental battle over reducing "busy work" and maximizing cohesion, yet still keeping things maintainable. There's quite a battle going on :)

Anyway, I'd love to see your perspective on this sometime and was hoping you may have some good resources for me to check out.

Thanks for all the awesome blogging and community contributions, I don't know how you do it!


@Kurt, yes, I can make the cfcUnit test case available. I've updated the ZIP file with the test case CFC and helper CFC. I'll make a new blog post talking about this and the idea of "testable" software.


Post Your Comments
Name:
Email Address:
Comments
*** Please note that all comments require moderation so it may be some time before your comment posts to this blog! ***
Remember My Information:
 



Hosting provided by