Spock, for tedious database conditions testing

As I mentioned earlier in my previous post, spock is a very nice groovy based testing framework, that helps us to write better readable test code. This is done through the use of groovy and by the use of behaviour driven development. In behaviour driven development you define what the system is supposed to do and express yourself in clear, hopefully, non programming terms. Groovy and spock helps you do just that. For instance, if I would like to test a valid login based on certain conditions, I could express that as follows:
Continue reading “Spock, for tedious database conditions testing”

Continuous integration with Git, Jenkins and Maven

Git is an excellent version control system, maven and jenkins are excellent ways for continuous integration and deployment, but how can you set them all up, so they will all work happily and content next to each other?

Requirements
To start it all of, you are going to need :

  • a maven project
  • a central git repository,
  • and jenkins.

Continue reading “Continuous integration with Git, Jenkins and Maven”

Twitter 406 error code

Integrating with social sites, such as twitter, seems to be so easy, and it actually can be, once you get the configuration set up correctly. Last day however, I spend most of my time trying to hunt down a 406 error code when trying to login using a twitter account. According to the twitter site I have to :

  • Think of a name for my application that wants to use the twitter login, well that was not too hard
  • Enter a description
  • Tell on which domain your twitter application will be hosted, but you can enter a placeholder for this if you don’t know that yet, or want to test

And then you should be set and ready to go.

Twitter will then provide you with 2 keys that you need to use to communicate with twitter. Since there are already many libraries available that will take care of the communications for you with twitter, it looks like that was all that I needed to do. Started a webapp, using the playframework and the SecureSocial module and logged in using twitter. Or at least, I tried to login with twitter. Unfortunately, I was not allowed to login, since twitter returned me a 406 error:

WARNING: POST request for "https://api.twitter.com/oauth/request_token" resulted in 406 (Not Acceptable); invoking error handle

Checked it at the twitter error codes and it says :

406 Not Acceptable: Returned by the Search API when an invalid format is specified in the request.

Which is weird since I did not try to use the search api, I was trying to signin with the given keys. Googling around this error I ran into a number of solutions, none of them really worked for me, but after trying out a combination of solutions I finally got it working. This is what I had to do:

  • Allthough it is not required, you have to give a callback url. This url should be back to localhost but that is not a valid domain according to twitter. So I set the callback url to http://localhost.com
  • Changed my /etc/hosts file where localhost.com will now return the ip address of localhost

and now it was working. I could finally login. I think the 406 code explanation should be altered a bit to indicate that it might also be a configuration issue.

I found out later today that I could also have solved this by setting the callback url to http://127.0.0.1

Hope this helps someone sometime

Playframework artikel van java magazine

Het playframework is een action based web applicatie framework waarbij snelheid van ontwikkeling voorop staat. DIt wordt gerealiseerd door een eigen classloader systeem die elke aanpassing aan java sources en resources meteen oppikt en opnieuw inlaadt zonder enige tussenkomst van de ontwikkelaar. Dit betekent dat er bijna geen re-starts van de server meer nodig zijn. Het playframework zal zelf geen data op de server vasthouden zodat applicaties in principe onbeperkt schaalbaar zijn. Elke actie die op de applicatie uitgevoerd kan worden, kan aan dynamische urls verbonden worden, zodat er nette REST urls gevormd kunnen worden. Het playframework kan complexe data structuren opbouwen aan de hand van http parameters door middel van data binding zonder dat je daar zelf veel voor hoeft te doen. Kortom het playframework maakt het bouwen van webapplicaties weer een stuk leuker. Continue reading “Playframework artikel van java magazine”

Playframework: handling of validated objects

In my previous blog (see validations for the play framework) I showed you how easy it can be to handle validations with the play framework. This time I will try to explain how this can be combined with the controllers in the playframework to handle these errors.

The controllers in the playframework are the central entry point from the web to your domain. Each controller should extend playframework’s Controller and have a static void method for each action that can be done on the controllers. These actions are static since they are supposed to be a binding point between the stateless aspect of http towards the statefull objects of your models. The actions are bound to urls and type of request (POST or GET) through the routes configuration file. Using the routes configuration file you can create nice looking rest-like url’s that are bound to the controllers. Also this routes file will be used to generate the urls for you if you add them to your view template files. For instance, given that your routes file contains the following route: Continue reading “Playframework: handling of validated objects”

Validations for the Play Framework

It can be very easy to add validations to the play framework. Just adding annotations to the objects will usually do the trick, but these are for the values of one field only, like @Required or @Email. If you want to check if some combined properties are valid, then you can use the @CheckWith annotation. With this annotation you have to define a class that will be used for the validation and optionally a message if this fails. For instance, a check on multiple fields for uniqueness in the database can be handled this way. The checkwith class must extend Check. An example will make it more clear
Continue reading “Validations for the Play Framework”

Playframework: Comparing flash parameters for lookup tables

After working with play a bit more I ran into an issue with the flash parameters. If you validate some input form, and there are errors, you can store the previously filled parameters in the flash scope. After that you can re-populate the content of the form again with the flash parameters. However if you have a selection with a lookup table, this will most of the times fail.

Here is a bit of code to explain this situation in more detail. In the html there is a list of entities available:

<select name="orderline.id" id="orderline_id">
  <option value="">Select one
  #{list items: orderlines, as: 'orderline'}
    <option value='${orderline.id}' #{if flash['orderline.id'] != null && flash['orderline.id'] == orderline.id}selected="selected">${orderline.name}
  #{/list}
</select>

Continue reading “Playframework: Comparing flash parameters for lookup tables”

Play framework impressions

The play framework is a web action-based framework that allows you to create web applications in fast and simple way. The play framework can start fast and reload your java classes in a fast way without restarting your application. Play focuses on developer productivity and targets RESTful architectures. Play doesn’t use a java webserver to start the application so the webserver starts up fast, but it does use many standard java libraries, like jpa with hibernate. Another nice feature of play is that you can start it up in test mode and then you have an embedded selenium test available to you.

Models
You can use existing jpa entities or create new ones extending from the Model that play provides. Extending this base class will get you an id and a number of find methods that can be used as static methods.
The properties are set as public fields in these model classes. This looks like a no-no to any experienced java developer, but if you think about it a bit more, this may not be such an awfull thing. Most models that I have seen are nothing more then a bunch of getters and setters for some properties. So why not make them public. The play framework will create the getters and setters and will use them, however this is done at run time.

Continue reading “Play framework impressions”

Common lift gotcha’s

Going to keep this here so maybe others will find this usefull as well and I will adding more gotcha’s here as I ran into them. So this basically is an overview to my thickness to understand lift correctly 🙂

lift Error locating template Message: Expected ‘;’
What this means is, that I copied an example of a snippet that does not work (anymore) on my computer. Using scala 2.8.1 and lift 2.2, the example of the simply lift website is like this:
<div id="main" class="lift:surround?with=default&at=content">

However it should be:
<div id="main" class="lift:surround?with=default;at=content">

Notice the amp between default and at was changed into a ‘;’
Continue reading “Common lift gotcha’s”

Access control for dwr with spring

With dwr it is possible to define methods on a bean that can be invoked from dwr-ajax calls. That much is easily to achieve following all the default tutorials on dwr on the web. However I wanted to restrict access to certain methods based on the roles that the users have. Otherwise it would be possible for certain users to call ajax methods that are not allowed for that user. But how can I change that behaviour. Looking at the documentation it is not obvious how that can be done, so I started the debugger to see how the integration really works. After some researching I came upon the DwrController that has a method call to ContainerUtil.setupDefaults. In here you can change the default behaviour classes, like the DefaultAccessControl class. So after having changed my own AccessControl implementation all I had to do is add it as parameter to the dwrController tag like so:
Continue reading “Access control for dwr with spring”