Using Grails' mockParams with Associations

If you have a class Book which contains a list of authors and would like to test this functionality in your Controller's unit tests. This is how you would do it, using the Grails mockParams.

void testSaveOfBookWithAuthor() {
mockRequest.method = 'POST'
mockParams.title = "Book Title"
mockParams["authors"] = [["":"123", "author":["id":"123"]]]

Even More Git Commands

Recent Git commands that have proved very helpful to me along with brief descriptions:
  • git instaweb - this allows you to view your repo in a browswer; you can configure which web server you would like to use in your ~/.gitconfig file
  • git log -n <filename> - you can restrict the number of commits that the log will show you for a given file
  • git blame <filename> - you can see who author every line of code in a given file
  • git diff ..origin/master <filename> - allows you to see the differences between your local file and the remote
  • git merge --squash <branchname> - allows you to merge your commits from a local branch together as a group of modified files into the current branch

Spring Security vs. Apache Shiro

This is a high level comparison of Spring Security and Apache Shiro, two Security frameworks that have plugins available in Grails.

Spring Security (a.k.a. ACEGI)
  • has users with roles
  • to restrict users on a more fine grained level, use interceptors in your controller
  • LDAP support
  • integration with CAS single-sign-on
  • has a new ACL option for the Grails Plugin
  • somewhat 'heavy-handed'
Example (Using Grails Interceptor):
def beforeInterceptor = [action:checkViewableSites, except: "list"]

def checkViewableSites = {
if(!user.viewableSites.contains(Site.get( )) {
response.sendError 403
return false

Apache Shiro (a.k.a. JSecurity)
  • has support for permissions out of the box
  • LDAP support with Shiro plugin
  • easy to customize
  • Grails Nimble plugin that allows permissions on three levels: users, groups, and roles
  • Nimble plugin has an easy to use UI component for adding/modifying users and permissions
  • Nimble plugin has support for authentication through Facebook and OpenID out of the box
Example (Using Shiro Permissions):
  • profile:edit:1 - this means a person can edit only the Profile object with id 1
  • profile:*:* - this means that a person can do anything to any Profile object

Monitor Hibernate Cache Statistics in Grails Application

After going through the effort of adding cache:true all over your application to take advantage of the Hibernate caching you will want to monitor the results. In order to do so you only need to modify two files, DataSource.groovy and a desired Controller.

//In your DataSource.groovy
hibernate {

//In your Controller
def sessionFactory

def showCacheStatistics = {
def statistics = sessionFactory.statistics
render statistics

Here is the output of calling the showCacheStatistics method on your controller:
Statistics[start time=1269058393252,sessions opened=27,sessions closed=25,transactions=18,successful transactions=3,optimistic lock failures=0,flushes=21,connections obtained=15,statements prepared=219,statements closed=219,second level cache puts=45,second level cache hits=1,second level cache misses=0,entities loaded=40,entities updated=5,entities inserted=48,entities deleted=0,entities fetched=26,collections loaded=20,collections updated=0,collections removed=0,collections recreated=12,collections fetched=18,queries executed to database=93,query cache puts=6,query cache hits=8,query cache misses=6,max query time=89]

Currently I'm showing just a direct rendering of the statistics object, but you could specify which parameters are important to you and only show those, or perhaps send the data to a gsp file and have it formatted in some way.